blob: b55ef700f5c237a834866d5a02e3eef274fec048 [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"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000017#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMConstantPoolValue.h"
19#include "ARMISelLowering.h"
20#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMRegisterInfo.h"
23#include "ARMSubtarget.h"
24#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000025#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000026#include "MCTargetDesc/ARMAddressingModes.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"
Bill Wendling2a850152011-10-05 00:02:33 +000041#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000042#include "llvm/CodeGen/MachineRegisterInfo.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000043#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000044#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000046#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000047#include "llvm/ADT/VectorExtras.h"
Evan Cheng55d42002011-01-08 01:24:27 +000048#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000049#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000050#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000051#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000052#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000053#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000054#include <sstream>
Evan Chenga8e29892007-01-19 07:51:42 +000055using namespace llvm;
56
Dale Johannesen51e28e62010-06-03 21:09:53 +000057STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000058STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000059
Bob Wilson703af3a2010-08-13 22:43:33 +000060// This option should go away when tail calls fully work.
61static cl::opt<bool>
62EnableARMTailCalls("arm-tail-calls", cl::Hidden,
63 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
64 cl::init(false));
65
Eric Christopher836c6242010-12-15 23:47:29 +000066cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000067EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000068 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000069 cl::init(false));
70
Evan Cheng46df4eb2010-06-16 07:35:02 +000071static cl::opt<bool>
72ARMInterworking("arm-interworking", cl::Hidden,
73 cl::desc("Enable / disable ARM interworking (for debugging only)"),
74 cl::init(true));
75
Cameron Zwaricha86686e2011-06-10 20:59:24 +000076namespace llvm {
77 class ARMCCState : public CCState {
78 public:
79 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
80 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
81 LLVMContext &C, ParmContext PC)
82 : CCState(CC, isVarArg, MF, TM, locs, C) {
83 assert(((PC == Call) || (PC == Prologue)) &&
84 "ARMCCState users must specify whether their context is call"
85 "or prologue generation.");
86 CallOrPrologue = PC;
87 }
88 };
89}
90
Stuart Hastingsc7315872011-04-20 16:47:52 +000091// The APCS parameter registers.
92static const unsigned GPRArgRegs[] = {
93 ARM::R0, ARM::R1, ARM::R2, ARM::R3
94};
95
Owen Andersone50ed302009-08-10 22:56:29 +000096void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
97 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000098 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000100 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
101 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000102
Owen Anderson70671842009-08-10 20:18:46 +0000103 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000104 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000105 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000106 }
107
Owen Andersone50ed302009-08-10 22:56:29 +0000108 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000110 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000111 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000112 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000113 if (ElemTy == MVT::i32) {
114 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
115 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
116 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
117 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
118 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000119 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
120 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
121 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
122 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
123 }
Owen Anderson70671842009-08-10 20:18:46 +0000124 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
125 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000126 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000127 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000128 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
129 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000130 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000132 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
133 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
134 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000135 }
136
137 // Promote all bit-wise operations.
138 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000139 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000140 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
141 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000142 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000143 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000144 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000145 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000146 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000147 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000148 }
Bob Wilson16330762009-09-16 00:17:28 +0000149
150 // Neon does not support vector divide/remainder operations.
151 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
154 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
155 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
156 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000157}
158
Owen Andersone50ed302009-08-10 22:56:29 +0000159void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000160 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000162}
163
Owen Andersone50ed302009-08-10 22:56:29 +0000164void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000165 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000167}
168
Chris Lattnerf0144122009-07-28 03:13:23 +0000169static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
170 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000171 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000172
Chris Lattner80ec2792009-08-02 00:34:36 +0000173 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000174}
175
Evan Chenga8e29892007-01-19 07:51:42 +0000176ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000177 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000178 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000179 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000180 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000181
Duncan Sands28b77e92011-09-06 19:07:46 +0000182 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
183
Evan Chengb1df8f22007-04-27 08:15:43 +0000184 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Uses VFP for Thumb libfuncs if available.
186 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
187 // Single-precision floating-point arithmetic.
188 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
189 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
190 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
191 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000192
Evan Chengb1df8f22007-04-27 08:15:43 +0000193 // Double-precision floating-point arithmetic.
194 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
195 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
196 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
197 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000198
Evan Chengb1df8f22007-04-27 08:15:43 +0000199 // Single-precision comparisons.
200 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
201 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
202 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
203 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
204 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
205 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
206 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
207 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000208
Evan Chengb1df8f22007-04-27 08:15:43 +0000209 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000217
Evan Chengb1df8f22007-04-27 08:15:43 +0000218 // Double-precision comparisons.
219 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
220 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
221 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
222 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
223 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
224 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
225 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
226 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000227
Evan Chengb1df8f22007-04-27 08:15:43 +0000228 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
233 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
234 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
235 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000236
Evan Chengb1df8f22007-04-27 08:15:43 +0000237 // Floating-point to integer conversions.
238 // i64 conversions are done via library routines even when generating VFP
239 // instructions, so use the same ones.
240 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
241 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
242 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
243 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000244
Evan Chengb1df8f22007-04-27 08:15:43 +0000245 // Conversions between floating types.
246 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
247 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
248
249 // Integer to floating-point conversions.
250 // i64 conversions are done via library routines even when generating VFP
251 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000252 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
253 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000254 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
255 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
256 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
257 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
258 }
Evan Chenga8e29892007-01-19 07:51:42 +0000259 }
260
Bob Wilson2f954612009-05-22 17:38:41 +0000261 // These libcalls are not available in 32-bit.
262 setLibcallName(RTLIB::SHL_I128, 0);
263 setLibcallName(RTLIB::SRL_I128, 0);
264 setLibcallName(RTLIB::SRA_I128, 0);
265
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000266 if (Subtarget->isAAPCS_ABI()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000267 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000268 // RTABI chapter 4.1.2, Table 2
269 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
270 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
271 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
272 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
273 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
274 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
275 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
276 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
277
278 // Double-precision floating-point comparison helper functions
279 // RTABI chapter 4.1.2, Table 3
280 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
281 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
282 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
283 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
284 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
285 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
286 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
287 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
288 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
289 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
290 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
291 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
292 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
293 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
294 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
295 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
296 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
301 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
302 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
303 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
304
305 // Single-precision floating-point arithmetic helper functions
306 // RTABI chapter 4.1.2, Table 4
307 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
308 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
309 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
310 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
311 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
312 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
313 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
314 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
315
316 // Single-precision floating-point comparison helper functions
317 // RTABI chapter 4.1.2, Table 5
318 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
319 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
320 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
321 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
322 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
323 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
324 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
325 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
326 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
327 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
328 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
329 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
330 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
331 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
332 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
333 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
334 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
339 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
340 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
341 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
342
343 // Floating-point to integer conversions.
344 // RTABI chapter 4.1.2, Table 6
345 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
346 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
347 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
348 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
349 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
350 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
351 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
352 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
353 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
358 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
359 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
360 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
361
362 // Conversions between floating types.
363 // RTABI chapter 4.1.2, Table 7
364 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
365 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
366 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000367 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000368
369 // Integer to floating-point conversions.
370 // RTABI chapter 4.1.2, Table 8
371 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
372 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
373 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
374 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
375 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
376 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
377 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
378 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
379 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
384 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
385 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
386 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
387
388 // Long long helper functions
389 // RTABI chapter 4.2, Table 9
390 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
391 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
392 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
393 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
394 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
395 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
396 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
398 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
399 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
400 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
401 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
402
403 // Integer division functions
404 // RTABI chapter 4.3.1
405 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
406 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
407 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
408 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
409 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
410 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
411 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
415 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000416 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000417
418 // Memory operations
419 // RTABI chapter 4.3.4
420 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
421 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
422 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000423 }
424
Bob Wilson2fef4572011-10-07 16:59:21 +0000425 // Use divmod compiler-rt calls for iOS 5.0 and later.
426 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
427 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
428 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
429 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
430 }
431
David Goodwinf1daf7d2009-07-08 23:10:31 +0000432 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000434 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000436 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000438 if (!Subtarget->isFPOnlySP())
439 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000440
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000442 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000443
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000444 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
446 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
447 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
448 setTruncStoreAction((MVT::SimpleValueType)VT,
449 (MVT::SimpleValueType)InnerVT, Expand);
450 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
453 }
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
471 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
472 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
473 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
474 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
475 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
476 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000477 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
479 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
480 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
481 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
482 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
483 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
484 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
485 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
486 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
487 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
488 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
489 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
490 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
491 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
492 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
493 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
494 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
495
Bob Wilson642b3292009-09-16 00:32:15 +0000496 // Neon does not support some operations on v1i64 and v2i64 types.
497 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000498 // Custom handling for some quad-vector types to detect VMULL.
499 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
500 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
501 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000502 // Custom handling for some vector types to avoid expensive expansions
503 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
504 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
505 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
506 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000507 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
508 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000509 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
510 // a destination type that is wider than the source.
511 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
512 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000513
Bob Wilson1c3ef902011-02-07 17:43:21 +0000514 setTargetDAGCombine(ISD::INTRINSIC_VOID);
515 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000516 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
517 setTargetDAGCombine(ISD::SHL);
518 setTargetDAGCombine(ISD::SRL);
519 setTargetDAGCombine(ISD::SRA);
520 setTargetDAGCombine(ISD::SIGN_EXTEND);
521 setTargetDAGCombine(ISD::ZERO_EXTEND);
522 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000523 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000524 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000525 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000526 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
527 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000528 setTargetDAGCombine(ISD::FP_TO_SINT);
529 setTargetDAGCombine(ISD::FP_TO_UINT);
530 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000531
532 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000533 }
534
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000535 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000536
537 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000539
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000540 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000542
Evan Chenga8e29892007-01-19 07:51:42 +0000543 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000544 if (!Subtarget->isThumb1Only()) {
545 for (unsigned im = (unsigned)ISD::PRE_INC;
546 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setIndexedLoadAction(im, MVT::i1, Legal);
548 setIndexedLoadAction(im, MVT::i8, Legal);
549 setIndexedLoadAction(im, MVT::i16, Legal);
550 setIndexedLoadAction(im, MVT::i32, Legal);
551 setIndexedStoreAction(im, MVT::i1, Legal);
552 setIndexedStoreAction(im, MVT::i8, Legal);
553 setIndexedStoreAction(im, MVT::i16, Legal);
554 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000555 }
Evan Chenga8e29892007-01-19 07:51:42 +0000556 }
557
558 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000559 setOperationAction(ISD::MUL, MVT::i64, Expand);
560 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000561 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
563 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000564 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000565 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
566 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000567 setOperationAction(ISD::MULHS, MVT::i32, Expand);
568
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000569 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000570 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000571 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setOperationAction(ISD::SRL, MVT::i64, Custom);
573 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000574
Evan Cheng342e3162011-08-30 01:34:54 +0000575 if (!Subtarget->isThumb1Only()) {
576 // FIXME: We should do this for Thumb1 as well.
577 setOperationAction(ISD::ADDC, MVT::i32, Custom);
578 setOperationAction(ISD::ADDE, MVT::i32, Custom);
579 setOperationAction(ISD::SUBC, MVT::i32, Custom);
580 setOperationAction(ISD::SUBE, MVT::i32, Custom);
581 }
582
Evan Chenga8e29892007-01-19 07:51:42 +0000583 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000585 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000587 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000589
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000590 // Only ARMv6 has BSWAP.
591 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000592 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000593
Evan Chenga8e29892007-01-19 07:51:42 +0000594 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000595 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000596 // v7M has a hardware divider
597 setOperationAction(ISD::SDIV, MVT::i32, Expand);
598 setOperationAction(ISD::UDIV, MVT::i32, Expand);
599 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::SREM, MVT::i32, Expand);
601 setOperationAction(ISD::UREM, MVT::i32, Expand);
602 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
603 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000604
Owen Anderson825b72b2009-08-11 20:47:22 +0000605 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
606 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
607 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
608 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000609 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000610
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000611 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000612
Evan Chenga8e29892007-01-19 07:51:42 +0000613 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 setOperationAction(ISD::VASTART, MVT::Other, Custom);
615 setOperationAction(ISD::VAARG, MVT::Other, Expand);
616 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
617 setOperationAction(ISD::VAEND, MVT::Other, Expand);
618 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
619 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Jim Grosbachbff39232009-08-12 17:38:44 +0000620 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000621 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
622 setExceptionPointerRegister(ARM::R0);
623 setExceptionSelectorRegister(ARM::R1);
624
Evan Cheng3a1588a2010-04-15 22:20:34 +0000625 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000626 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
627 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000628 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000629 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000630 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000631 // membarrier needs custom lowering; the rest are legal and handled
632 // normally.
633 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000634 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000635 // Custom lowering for 64-bit ops
636 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
637 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
638 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
639 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
640 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
641 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000642 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000643 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
644 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000645 } else {
646 // Set them all for expansion, which will force libcalls.
647 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000648 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000649 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000650 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000651 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000652 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000653 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000654 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000655 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000656 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000657 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000658 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000659 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000660 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000661 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
662 // Unordered/Monotonic case.
663 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
664 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000665 // Since the libcalls include locking, fold in the fences
666 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000667 }
Evan Chenga8e29892007-01-19 07:51:42 +0000668
Evan Cheng416941d2010-11-04 05:19:35 +0000669 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000670
Eli Friedmana2c6f452010-06-26 04:36:50 +0000671 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
672 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
674 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000675 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000677
Nate Begemand1fb5832010-08-03 21:31:55 +0000678 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000679 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
680 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000681 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000682 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
683 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000684
685 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000687 if (Subtarget->isTargetDarwin()) {
688 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
689 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Jim Grosbache4ad3872010-10-19 23:27:08 +0000690 setOperationAction(ISD::EH_SJLJ_DISPATCHSETUP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000691 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000692 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::SETCC, MVT::i32, Expand);
695 setOperationAction(ISD::SETCC, MVT::f32, Expand);
696 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000697 setOperationAction(ISD::SELECT, MVT::i32, Custom);
698 setOperationAction(ISD::SELECT, MVT::f32, Custom);
699 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
701 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
702 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000703
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
705 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
706 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
707 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
708 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000709
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000710 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::FSIN, MVT::f64, Expand);
712 setOperationAction(ISD::FSIN, MVT::f32, Expand);
713 setOperationAction(ISD::FCOS, MVT::f32, Expand);
714 setOperationAction(ISD::FCOS, MVT::f64, Expand);
715 setOperationAction(ISD::FREM, MVT::f64, Expand);
716 setOperationAction(ISD::FREM, MVT::f32, Expand);
David Goodwinf1daf7d2009-07-08 23:10:31 +0000717 if (!UseSoftFloat && Subtarget->hasVFP2() && !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
719 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000720 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::FPOW, MVT::f64, Expand);
722 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000723
Cameron Zwarich33390842011-07-08 21:39:21 +0000724 setOperationAction(ISD::FMA, MVT::f64, Expand);
725 setOperationAction(ISD::FMA, MVT::f32, Expand);
726
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000727 // Various VFP goodness
728 if (!UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000729 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
730 if (Subtarget->hasVFP2()) {
731 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
732 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
733 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
734 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
735 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000736 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000737 if (!Subtarget->hasFP16()) {
738 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
739 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000740 }
Evan Cheng110cf482008-04-01 01:50:16 +0000741 }
Evan Chenga8e29892007-01-19 07:51:42 +0000742
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000743 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000744 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000745 setTargetDAGCombine(ISD::ADD);
746 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000747 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000748
Owen Anderson080c0922010-11-05 19:27:46 +0000749 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON())
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000750 setTargetDAGCombine(ISD::OR);
Owen Anderson080c0922010-11-05 19:27:46 +0000751 if (Subtarget->hasNEON())
752 setTargetDAGCombine(ISD::AND);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000753
Evan Chenga8e29892007-01-19 07:51:42 +0000754 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000755
Evan Chengf7d87ee2010-05-21 00:43:17 +0000756 if (UseSoftFloat || Subtarget->isThumb1Only() || !Subtarget->hasVFP2())
757 setSchedulingPreference(Sched::RegPressure);
758 else
759 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000760
Evan Cheng05219282011-01-06 06:52:41 +0000761 //// temporary - rewrite interface to use type
762 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000763 maxStoresPerMemset = 16;
764 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000765
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000766 // On ARM arguments smaller than 4 bytes are extended, so all arguments
767 // are at least 4 bytes aligned.
768 setMinStackArgumentAlignment(4);
769
Evan Chengfff606d2010-09-24 19:07:23 +0000770 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000771
772 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000773}
774
Andrew Trick32cec0a2011-01-19 02:35:27 +0000775// FIXME: It might make sense to define the representative register class as the
776// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
777// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
778// SPR's representative would be DPR_VFP2. This should work well if register
779// pressure tracking were modified such that a register use would increment the
780// pressure of the register class's representative and all of it's super
781// classes' representatives transitively. We have not implemented this because
782// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000783// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000784// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000785std::pair<const TargetRegisterClass*, uint8_t>
786ARMTargetLowering::findRepresentativeClass(EVT VT) const{
787 const TargetRegisterClass *RRC = 0;
788 uint8_t Cost = 1;
789 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000790 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000791 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000792 // Use DPR as representative register class for all floating point
793 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
794 // the cost is 1 for both f32 and f64.
795 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000796 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000797 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000798 // When NEON is used for SP, only half of the register file is available
799 // because operations that define both SP and DP results will be constrained
800 // to the VFP2 class (D0-D15). We currently model this constraint prior to
801 // coalescing by double-counting the SP regs. See the FIXME above.
802 if (Subtarget->useNEONForSinglePrecisionFP())
803 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000804 break;
805 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
806 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000807 RRC = ARM::DPRRegisterClass;
808 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000809 break;
810 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000811 RRC = ARM::DPRRegisterClass;
812 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000813 break;
814 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000815 RRC = ARM::DPRRegisterClass;
816 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000817 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000818 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000819 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000820}
821
Evan Chenga8e29892007-01-19 07:51:42 +0000822const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
823 switch (Opcode) {
824 default: return 0;
825 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000826 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000827 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000828 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
829 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000830 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000831 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
832 case ARMISD::tCALL: return "ARMISD::tCALL";
833 case ARMISD::BRCOND: return "ARMISD::BRCOND";
834 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000835 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000836 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
837 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
838 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000839 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000840 case ARMISD::CMPFP: return "ARMISD::CMPFP";
841 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000842 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000843 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
844 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000845
Jim Grosbach3482c802010-01-18 19:58:49 +0000846 case ARMISD::RBIT: return "ARMISD::RBIT";
847
Bob Wilson76a312b2010-03-19 22:51:32 +0000848 case ARMISD::FTOSI: return "ARMISD::FTOSI";
849 case ARMISD::FTOUI: return "ARMISD::FTOUI";
850 case ARMISD::SITOF: return "ARMISD::SITOF";
851 case ARMISD::UITOF: return "ARMISD::UITOF";
852
Evan Chenga8e29892007-01-19 07:51:42 +0000853 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
854 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
855 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000856
Evan Cheng342e3162011-08-30 01:34:54 +0000857 case ARMISD::ADDC: return "ARMISD::ADDC";
858 case ARMISD::ADDE: return "ARMISD::ADDE";
859 case ARMISD::SUBC: return "ARMISD::SUBC";
860 case ARMISD::SUBE: return "ARMISD::SUBE";
861
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000862 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
863 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000864
Evan Chengc5942082009-10-28 06:55:03 +0000865 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
866 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
Jim Grosbache4ad3872010-10-19 23:27:08 +0000867 case ARMISD::EH_SJLJ_DISPATCHSETUP:return "ARMISD::EH_SJLJ_DISPATCHSETUP";
Evan Chengc5942082009-10-28 06:55:03 +0000868
Dale Johannesen51e28e62010-06-03 21:09:53 +0000869 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000870
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000871 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000872
Evan Cheng86198642009-08-07 00:34:42 +0000873 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
874
Jim Grosbach3728e962009-12-10 00:11:09 +0000875 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000876 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000877
Evan Chengdfed19f2010-11-03 06:34:55 +0000878 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
879
Bob Wilson5bafff32009-06-22 23:27:02 +0000880 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000881 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000882 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000883 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
884 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000885 case ARMISD::VCGEU: return "ARMISD::VCGEU";
886 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000887 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
888 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000889 case ARMISD::VCGTU: return "ARMISD::VCGTU";
890 case ARMISD::VTST: return "ARMISD::VTST";
891
892 case ARMISD::VSHL: return "ARMISD::VSHL";
893 case ARMISD::VSHRs: return "ARMISD::VSHRs";
894 case ARMISD::VSHRu: return "ARMISD::VSHRu";
895 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
896 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
897 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
898 case ARMISD::VSHRN: return "ARMISD::VSHRN";
899 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
900 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
901 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
902 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
903 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
904 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
905 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
906 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
907 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
908 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
909 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
910 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
911 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
912 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000913 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000914 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000915 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000916 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000917 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000918 case ARMISD::VREV64: return "ARMISD::VREV64";
919 case ARMISD::VREV32: return "ARMISD::VREV32";
920 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000921 case ARMISD::VZIP: return "ARMISD::VZIP";
922 case ARMISD::VUZP: return "ARMISD::VUZP";
923 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000924 case ARMISD::VTBL1: return "ARMISD::VTBL1";
925 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000926 case ARMISD::VMULLs: return "ARMISD::VMULLs";
927 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000928 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000929 case ARMISD::FMAX: return "ARMISD::FMAX";
930 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000931 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000932 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
933 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000934 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000935 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
936 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
937 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000938 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
939 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
940 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
941 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
942 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
943 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
944 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
945 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
946 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
947 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
948 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
949 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
950 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
951 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
952 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
953 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
954 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +0000955 }
956}
957
Duncan Sands28b77e92011-09-06 19:07:46 +0000958EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
959 if (!VT.isVector()) return getPointerTy();
960 return VT.changeVectorElementTypeToInteger();
961}
962
Evan Cheng06b666c2010-05-15 02:18:07 +0000963/// getRegClassFor - Return the register class that should be used for the
964/// specified value type.
965TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
966 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
967 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
968 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +0000969 if (Subtarget->hasNEON()) {
970 if (VT == MVT::v4i64)
971 return ARM::QQPRRegisterClass;
972 else if (VT == MVT::v8i64)
973 return ARM::QQQQPRRegisterClass;
974 }
Evan Cheng06b666c2010-05-15 02:18:07 +0000975 return TargetLowering::getRegClassFor(VT);
976}
977
Eric Christopherab695882010-07-21 22:26:11 +0000978// Create a fast isel object.
979FastISel *
980ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
981 return ARM::createFastISel(funcInfo);
982}
983
Anton Korobeynikovcec36f42010-07-24 21:52:08 +0000984/// getMaximalGlobalOffset - Returns the maximal possible offset which can
985/// be used for loads / stores from the global.
986unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
987 return (Subtarget->isThumb1Only() ? 127 : 4095);
988}
989
Evan Cheng1cc39842010-05-20 23:26:43 +0000990Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +0000991 unsigned NumVals = N->getNumValues();
992 if (!NumVals)
993 return Sched::RegPressure;
994
995 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +0000996 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000997 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +0000998 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +0000999 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001000 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001001 }
Evan Chengc10f5432010-05-28 23:25:23 +00001002
1003 if (!N->isMachineOpcode())
1004 return Sched::RegPressure;
1005
1006 // Load are scheduled for latency even if there instruction itinerary
1007 // is not available.
1008 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001009 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001010
Evan Chenge837dea2011-06-28 19:10:37 +00001011 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001012 return Sched::RegPressure;
1013 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001014 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001015 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001016
Evan Cheng1cc39842010-05-20 23:26:43 +00001017 return Sched::RegPressure;
1018}
1019
Evan Chenga8e29892007-01-19 07:51:42 +00001020//===----------------------------------------------------------------------===//
1021// Lowering Code
1022//===----------------------------------------------------------------------===//
1023
Evan Chenga8e29892007-01-19 07:51:42 +00001024/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1025static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1026 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001027 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001028 case ISD::SETNE: return ARMCC::NE;
1029 case ISD::SETEQ: return ARMCC::EQ;
1030 case ISD::SETGT: return ARMCC::GT;
1031 case ISD::SETGE: return ARMCC::GE;
1032 case ISD::SETLT: return ARMCC::LT;
1033 case ISD::SETLE: return ARMCC::LE;
1034 case ISD::SETUGT: return ARMCC::HI;
1035 case ISD::SETUGE: return ARMCC::HS;
1036 case ISD::SETULT: return ARMCC::LO;
1037 case ISD::SETULE: return ARMCC::LS;
1038 }
1039}
1040
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001041/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1042static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001043 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001044 CondCode2 = ARMCC::AL;
1045 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001046 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001047 case ISD::SETEQ:
1048 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1049 case ISD::SETGT:
1050 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1051 case ISD::SETGE:
1052 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1053 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001054 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001055 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1056 case ISD::SETO: CondCode = ARMCC::VC; break;
1057 case ISD::SETUO: CondCode = ARMCC::VS; break;
1058 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1059 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1060 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1061 case ISD::SETLT:
1062 case ISD::SETULT: CondCode = ARMCC::LT; break;
1063 case ISD::SETLE:
1064 case ISD::SETULE: CondCode = ARMCC::LE; break;
1065 case ISD::SETNE:
1066 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1067 }
Evan Chenga8e29892007-01-19 07:51:42 +00001068}
1069
Bob Wilson1f595bb2009-04-17 19:07:39 +00001070//===----------------------------------------------------------------------===//
1071// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001072//===----------------------------------------------------------------------===//
1073
1074#include "ARMGenCallingConv.inc"
1075
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001076/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1077/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001078CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001079 bool Return,
1080 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001081 switch (CC) {
1082 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001083 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001084 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001085 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001086 if (!Subtarget->isAAPCS_ABI())
1087 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1088 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1089 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1090 }
1091 // Fallthrough
1092 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001093 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001094 if (!Subtarget->isAAPCS_ABI())
1095 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1096 else if (Subtarget->hasVFP2() &&
1097 FloatABIType == FloatABI::Hard && !isVarArg)
1098 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1099 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1100 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001101 case CallingConv::ARM_AAPCS_VFP:
Evan Cheng76f920d2010-10-22 18:23:05 +00001102 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001103 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001104 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001105 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001106 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001107 }
1108}
1109
Dan Gohman98ca4f22009-08-05 01:29:28 +00001110/// LowerCallResult - Lower the result values of a call into the
1111/// appropriate copies out of appropriate physical registers.
1112SDValue
1113ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001114 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001115 const SmallVectorImpl<ISD::InputArg> &Ins,
1116 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001117 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001118
Bob Wilson1f595bb2009-04-17 19:07:39 +00001119 // Assign locations to each value returned by this call.
1120 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001121 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1122 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001123 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001124 CCAssignFnForNode(CallConv, /* Return*/ true,
1125 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001126
1127 // Copy all of the result registers out of their specified physreg.
1128 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1129 CCValAssign VA = RVLocs[i];
1130
Bob Wilson80915242009-04-25 00:33:20 +00001131 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001133 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001134 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001136 Chain = Lo.getValue(1);
1137 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001138 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001139 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001140 InFlag);
1141 Chain = Hi.getValue(1);
1142 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001143 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001144
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 if (VA.getLocVT() == MVT::v2f64) {
1146 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1147 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1148 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001149
1150 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001152 Chain = Lo.getValue(1);
1153 InFlag = Lo.getValue(2);
1154 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001155 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001156 Chain = Hi.getValue(1);
1157 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001158 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001159 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1160 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001161 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001162 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001163 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1164 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001165 Chain = Val.getValue(1);
1166 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001167 }
Bob Wilson80915242009-04-25 00:33:20 +00001168
1169 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001170 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001171 case CCValAssign::Full: break;
1172 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001173 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001174 break;
1175 }
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001178 }
1179
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181}
1182
Bob Wilsondee46d72009-04-17 20:35:10 +00001183/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1186 SDValue StackPtr, SDValue Arg,
1187 DebugLoc dl, SelectionDAG &DAG,
1188 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001189 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001190 unsigned LocMemOffset = VA.getLocMemOffset();
1191 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1192 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001194 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001195 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001196}
1197
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001199 SDValue Chain, SDValue &Arg,
1200 RegsToPassVector &RegsToPass,
1201 CCValAssign &VA, CCValAssign &NextVA,
1202 SDValue &StackPtr,
1203 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001204 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001205
Jim Grosbache5165492009-11-09 00:11:35 +00001206 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001208 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1209
1210 if (NextVA.isRegLoc())
1211 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1212 else {
1213 assert(NextVA.isMemLoc());
1214 if (StackPtr.getNode() == 0)
1215 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1216
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1218 dl, DAG, NextVA,
1219 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001220 }
1221}
1222
Dan Gohman98ca4f22009-08-05 01:29:28 +00001223/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001224/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1225/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001227ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001228 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001229 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001231 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001232 const SmallVectorImpl<ISD::InputArg> &Ins,
1233 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001234 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001235 MachineFunction &MF = DAG.getMachineFunction();
1236 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1237 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001238 // Disable tail calls if they're not supported.
1239 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001240 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001241 if (isTailCall) {
1242 // Check if it's really possible to do a tail call.
1243 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1244 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001245 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001246 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1247 // detected sibcalls.
1248 if (isTailCall) {
1249 ++NumTailCalls;
1250 IsSibCall = true;
1251 }
1252 }
Evan Chenga8e29892007-01-19 07:51:42 +00001253
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 // Analyze operands of the call, assigning locations to each operand.
1255 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001256 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1257 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001259 CCAssignFnForNode(CallConv, /* Return*/ false,
1260 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001261
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 // Get a count of how many bytes are to be pushed on the stack.
1263 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001264
Dale Johannesen51e28e62010-06-03 21:09:53 +00001265 // For tail calls, memory operands are available in our caller's stack.
1266 if (IsSibCall)
1267 NumBytes = 0;
1268
Evan Chenga8e29892007-01-19 07:51:42 +00001269 // Adjust the stack pointer for the new arguments...
1270 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001271 if (!IsSibCall)
1272 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001273
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001274 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001275
Bob Wilson5bafff32009-06-22 23:27:02 +00001276 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001277 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001278
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001280 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001281 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1282 i != e;
1283 ++i, ++realArgIdx) {
1284 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001285 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001287 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001288
Bob Wilson1f595bb2009-04-17 19:07:39 +00001289 // Promote the value if needed.
1290 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001291 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 case CCValAssign::Full: break;
1293 case CCValAssign::SExt:
1294 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1295 break;
1296 case CCValAssign::ZExt:
1297 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1298 break;
1299 case CCValAssign::AExt:
1300 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1301 break;
1302 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001303 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001304 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001305 }
1306
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001307 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 if (VA.getLocVT() == MVT::v2f64) {
1310 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1311 DAG.getConstant(0, MVT::i32));
1312 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1313 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001316 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1317
1318 VA = ArgLocs[++i]; // skip ahead to next loc
1319 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001321 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1322 } else {
1323 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001324
Dan Gohman98ca4f22009-08-05 01:29:28 +00001325 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1326 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001327 }
1328 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001330 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001331 }
1332 } else if (VA.isRegLoc()) {
1333 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001334 } else if (isByVal) {
1335 assert(VA.isMemLoc());
1336 unsigned offset = 0;
1337
1338 // True if this byval aggregate will be split between registers
1339 // and memory.
1340 if (CCInfo.isFirstByValRegValid()) {
1341 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1342 unsigned int i, j;
1343 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1344 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1345 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1346 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1347 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001348 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001349 MemOpChains.push_back(Load.getValue(1));
1350 RegsToPass.push_back(std::make_pair(j, Load));
1351 }
1352 offset = ARM::R4 - CCInfo.getFirstByValReg();
1353 CCInfo.clearFirstByValReg();
1354 }
1355
1356 unsigned LocMemOffset = VA.getLocMemOffset();
1357 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1358 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1359 StkPtrOff);
1360 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1361 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1362 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1363 MVT::i32);
1364 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1365 Flags.getByValAlign(),
1366 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001367 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001368 MachinePointerInfo(0),
1369 MachinePointerInfo(0)));
1370
1371 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001372 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001373
Dan Gohman98ca4f22009-08-05 01:29:28 +00001374 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1375 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376 }
Evan Chenga8e29892007-01-19 07:51:42 +00001377 }
1378
1379 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001380 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001381 &MemOpChains[0], MemOpChains.size());
1382
1383 // Build a sequence of copy-to-reg nodes chained together with token chain
1384 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001385 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001386 // Tail call byval lowering might overwrite argument registers so in case of
1387 // tail call optimization the copies to registers are lowered later.
1388 if (!isTailCall)
1389 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1390 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1391 RegsToPass[i].second, InFlag);
1392 InFlag = Chain.getValue(1);
1393 }
Evan Chenga8e29892007-01-19 07:51:42 +00001394
Dale Johannesen51e28e62010-06-03 21:09:53 +00001395 // For tail calls lower the arguments to the 'real' stack slot.
1396 if (isTailCall) {
1397 // Force all the incoming stack arguments to be loaded from the stack
1398 // before any new outgoing arguments are stored to the stack, because the
1399 // outgoing stack slots may alias the incoming argument stack slots, and
1400 // the alias isn't otherwise explicit. This is slightly more conservative
1401 // than necessary, because it means that each store effectively depends
1402 // on every argument instead of just those arguments it would clobber.
1403
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001404 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001405 InFlag = SDValue();
1406 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1407 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1408 RegsToPass[i].second, InFlag);
1409 InFlag = Chain.getValue(1);
1410 }
1411 InFlag =SDValue();
1412 }
1413
Bill Wendling056292f2008-09-16 21:48:12 +00001414 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1415 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1416 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001417 bool isDirect = false;
1418 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001419 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001420 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001421
1422 if (EnableARMLongCalls) {
1423 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1424 && "long-calls with non-static relocation model!");
1425 // Handle a global address or an external symbol. If it's not one of
1426 // those, the target's already in a register, so we don't need to do
1427 // anything extra.
1428 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001429 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001430 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001431 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001432 ARMConstantPoolValue *CPV =
1433 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1434
Jim Grosbache7b52522010-04-14 22:28:31 +00001435 // Get the address of the callee into a register
1436 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1437 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1438 Callee = DAG.getLoad(getPointerTy(), dl,
1439 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001440 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001441 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001442 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1443 const char *Sym = S->getSymbol();
1444
1445 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001446 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001447 ARMConstantPoolValue *CPV =
1448 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1449 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001450 // Get the address of the callee into a register
1451 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1452 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1453 Callee = DAG.getLoad(getPointerTy(), dl,
1454 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001455 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001456 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001457 }
1458 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001459 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001460 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001461 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001462 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001463 getTargetMachine().getRelocationModel() != Reloc::Static;
1464 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001465 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001466 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001467 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001468 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001469 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001470 ARMConstantPoolValue *CPV =
1471 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001472 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001473 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001474 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001475 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001476 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001477 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001478 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001479 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001480 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001481 } else {
1482 // On ELF targets for PIC code, direct calls should go through the PLT
1483 unsigned OpFlags = 0;
1484 if (Subtarget->isTargetELF() &&
1485 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1486 OpFlags = ARMII::MO_PLT;
1487 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1488 }
Bill Wendling056292f2008-09-16 21:48:12 +00001489 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001490 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001491 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001492 getTargetMachine().getRelocationModel() != Reloc::Static;
1493 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001494 // tBX takes a register source operand.
1495 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001496 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001497 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001498 ARMConstantPoolValue *CPV =
1499 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1500 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001501 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001502 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001503 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001504 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001505 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001506 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001507 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001508 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001509 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001510 } else {
1511 unsigned OpFlags = 0;
1512 // On ELF targets for PIC code, direct calls should go through the PLT
1513 if (Subtarget->isTargetELF() &&
1514 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1515 OpFlags = ARMII::MO_PLT;
1516 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1517 }
Evan Chenga8e29892007-01-19 07:51:42 +00001518 }
1519
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001520 // FIXME: handle tail calls differently.
1521 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001522 if (Subtarget->isThumb()) {
1523 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001524 CallOpc = ARMISD::CALL_NOLINK;
1525 else
1526 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1527 } else {
1528 CallOpc = (isDirect || Subtarget->hasV5TOps())
Evan Cheng277f0742007-06-19 21:05:09 +00001529 ? (isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL)
1530 : ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001531 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001532
Dan Gohman475871a2008-07-27 21:46:04 +00001533 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001534 Ops.push_back(Chain);
1535 Ops.push_back(Callee);
1536
1537 // Add argument registers to the end of the list so that they are known live
1538 // into the call.
1539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1540 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1541 RegsToPass[i].second.getValueType()));
1542
Gabor Greifba36cb52008-08-28 21:40:38 +00001543 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001544 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001545
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001546 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001547 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001548 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001549
Duncan Sands4bdcb612008-07-02 17:40:58 +00001550 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001551 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001552 InFlag = Chain.getValue(1);
1553
Chris Lattnere563bbc2008-10-11 22:08:30 +00001554 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1555 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001556 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001557 InFlag = Chain.getValue(1);
1558
Bob Wilson1f595bb2009-04-17 19:07:39 +00001559 // Handle result values, copying them out of physregs into vregs that we
1560 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1562 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001563}
1564
Stuart Hastingsf222e592011-02-28 17:17:53 +00001565/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001566/// on the stack. Remember the next parameter register to allocate,
1567/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001568/// this.
1569void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001570llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1571 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1572 assert((State->getCallOrPrologue() == Prologue ||
1573 State->getCallOrPrologue() == Call) &&
1574 "unhandled ParmContext");
1575 if ((!State->isFirstByValRegValid()) &&
1576 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1577 State->setFirstByValReg(reg);
1578 // At a call site, a byval parameter that is split between
1579 // registers and memory needs its size truncated here. In a
1580 // function prologue, such byval parameters are reassembled in
1581 // memory, and are not truncated.
1582 if (State->getCallOrPrologue() == Call) {
1583 unsigned excess = 4 * (ARM::R4 - reg);
1584 assert(size >= excess && "expected larger existing stack allocation");
1585 size -= excess;
1586 }
1587 }
1588 // Confiscate any remaining parameter registers to preclude their
1589 // assignment to subsequent parameters.
1590 while (State->AllocateReg(GPRArgRegs, 4))
1591 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001592}
1593
Dale Johannesen51e28e62010-06-03 21:09:53 +00001594/// MatchingStackOffset - Return true if the given stack call argument is
1595/// already available in the same position (relatively) of the caller's
1596/// incoming argument stack.
1597static
1598bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1599 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1600 const ARMInstrInfo *TII) {
1601 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1602 int FI = INT_MAX;
1603 if (Arg.getOpcode() == ISD::CopyFromReg) {
1604 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001605 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001606 return false;
1607 MachineInstr *Def = MRI->getVRegDef(VR);
1608 if (!Def)
1609 return false;
1610 if (!Flags.isByVal()) {
1611 if (!TII->isLoadFromStackSlot(Def, FI))
1612 return false;
1613 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001614 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001615 }
1616 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1617 if (Flags.isByVal())
1618 // ByVal argument is passed in as a pointer but it's now being
1619 // dereferenced. e.g.
1620 // define @foo(%struct.X* %A) {
1621 // tail call @bar(%struct.X* byval %A)
1622 // }
1623 return false;
1624 SDValue Ptr = Ld->getBasePtr();
1625 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1626 if (!FINode)
1627 return false;
1628 FI = FINode->getIndex();
1629 } else
1630 return false;
1631
1632 assert(FI != INT_MAX);
1633 if (!MFI->isFixedObjectIndex(FI))
1634 return false;
1635 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1636}
1637
1638/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1639/// for tail call optimization. Targets which want to do tail call
1640/// optimization should implement this function.
1641bool
1642ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1643 CallingConv::ID CalleeCC,
1644 bool isVarArg,
1645 bool isCalleeStructRet,
1646 bool isCallerStructRet,
1647 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001648 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001649 const SmallVectorImpl<ISD::InputArg> &Ins,
1650 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001651 const Function *CallerF = DAG.getMachineFunction().getFunction();
1652 CallingConv::ID CallerCC = CallerF->getCallingConv();
1653 bool CCMatch = CallerCC == CalleeCC;
1654
1655 // Look for obvious safe cases to perform tail call optimization that do not
1656 // require ABI changes. This is what gcc calls sibcall.
1657
Jim Grosbach7616b642010-06-16 23:45:49 +00001658 // Do not sibcall optimize vararg calls unless the call site is not passing
1659 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001660 if (isVarArg && !Outs.empty())
1661 return false;
1662
1663 // Also avoid sibcall optimization if either caller or callee uses struct
1664 // return semantics.
1665 if (isCalleeStructRet || isCallerStructRet)
1666 return false;
1667
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001668 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001669 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1670 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1671 // support in the assembler and linker to be used. This would need to be
1672 // fixed to fully support tail calls in Thumb1.
1673 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001674 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1675 // LR. This means if we need to reload LR, it takes an extra instructions,
1676 // which outweighs the value of the tail call; but here we don't know yet
1677 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001678 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001679 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001680
1681 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1682 // but we need to make sure there are enough registers; the only valid
1683 // registers are the 4 used for parameters. We don't currently do this
1684 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001685 if (Subtarget->isThumb1Only())
1686 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001687
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 // If the calling conventions do not match, then we'd better make sure the
1689 // results are returned in the same way as what the caller expects.
1690 if (!CCMatch) {
1691 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001692 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1693 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1695
1696 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001697 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1698 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001699 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1700
1701 if (RVLocs1.size() != RVLocs2.size())
1702 return false;
1703 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1704 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1705 return false;
1706 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1707 return false;
1708 if (RVLocs1[i].isRegLoc()) {
1709 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1710 return false;
1711 } else {
1712 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1713 return false;
1714 }
1715 }
1716 }
1717
1718 // If the callee takes no arguments then go on to check the results of the
1719 // call.
1720 if (!Outs.empty()) {
1721 // Check if stack adjustment is needed. For now, do not do this if any
1722 // argument is passed on the stack.
1723 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001724 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1725 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001726 CCInfo.AnalyzeCallOperands(Outs,
1727 CCAssignFnForNode(CalleeCC, false, isVarArg));
1728 if (CCInfo.getNextStackOffset()) {
1729 MachineFunction &MF = DAG.getMachineFunction();
1730
1731 // Check if the arguments are already laid out in the right way as
1732 // the caller's fixed stack objects.
1733 MachineFrameInfo *MFI = MF.getFrameInfo();
1734 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1735 const ARMInstrInfo *TII =
1736 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001737 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1738 i != e;
1739 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001740 CCValAssign &VA = ArgLocs[i];
1741 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001742 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001743 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001744 if (VA.getLocInfo() == CCValAssign::Indirect)
1745 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001746 if (VA.needsCustom()) {
1747 // f64 and vector types are split into multiple registers or
1748 // register/stack-slot combinations. The types will not match
1749 // the registers; give up on memory f64 refs until we figure
1750 // out what to do about this.
1751 if (!VA.isRegLoc())
1752 return false;
1753 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001754 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001755 if (RegVT == MVT::v2f64) {
1756 if (!ArgLocs[++i].isRegLoc())
1757 return false;
1758 if (!ArgLocs[++i].isRegLoc())
1759 return false;
1760 }
1761 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001762 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1763 MFI, MRI, TII))
1764 return false;
1765 }
1766 }
1767 }
1768 }
1769
1770 return true;
1771}
1772
Dan Gohman98ca4f22009-08-05 01:29:28 +00001773SDValue
1774ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001775 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001777 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001778 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001779
Bob Wilsondee46d72009-04-17 20:35:10 +00001780 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001781 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001782
Bob Wilsondee46d72009-04-17 20:35:10 +00001783 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001784 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1785 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001786
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001788 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1789 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001790
1791 // If this is the first return lowered for this function, add
1792 // the regs to the liveout set for the function.
1793 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1794 for (unsigned i = 0; i != RVLocs.size(); ++i)
1795 if (RVLocs[i].isRegLoc())
1796 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001797 }
1798
Bob Wilson1f595bb2009-04-17 19:07:39 +00001799 SDValue Flag;
1800
1801 // Copy the result values into the output registers.
1802 for (unsigned i = 0, realRVLocIdx = 0;
1803 i != RVLocs.size();
1804 ++i, ++realRVLocIdx) {
1805 CCValAssign &VA = RVLocs[i];
1806 assert(VA.isRegLoc() && "Can only return in registers!");
1807
Dan Gohmanc9403652010-07-07 15:54:55 +00001808 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001809
1810 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001811 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001812 case CCValAssign::Full: break;
1813 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001814 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001815 break;
1816 }
1817
Bob Wilson1f595bb2009-04-17 19:07:39 +00001818 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001819 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001820 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001821 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1822 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001823 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001825
1826 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1827 Flag = Chain.getValue(1);
1828 VA = RVLocs[++i]; // skip ahead to next loc
1829 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1830 HalfGPRs.getValue(1), Flag);
1831 Flag = Chain.getValue(1);
1832 VA = RVLocs[++i]; // skip ahead to next loc
1833
1834 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1836 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001837 }
1838 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1839 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001840 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001841 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001842 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001843 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001844 VA = RVLocs[++i]; // skip ahead to next loc
1845 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1846 Flag);
1847 } else
1848 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1849
Bob Wilsondee46d72009-04-17 20:35:10 +00001850 // Guarantee that all emitted copies are
1851 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001852 Flag = Chain.getValue(1);
1853 }
1854
1855 SDValue result;
1856 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001858 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001860
1861 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001862}
1863
Evan Cheng3d2125c2010-11-30 23:55:39 +00001864bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1865 if (N->getNumValues() != 1)
1866 return false;
1867 if (!N->hasNUsesOfValue(1, 0))
1868 return false;
1869
1870 unsigned NumCopies = 0;
1871 SDNode* Copies[2];
1872 SDNode *Use = *N->use_begin();
1873 if (Use->getOpcode() == ISD::CopyToReg) {
1874 Copies[NumCopies++] = Use;
1875 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1876 // f64 returned in a pair of GPRs.
1877 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1878 UI != UE; ++UI) {
1879 if (UI->getOpcode() != ISD::CopyToReg)
1880 return false;
1881 Copies[UI.getUse().getResNo()] = *UI;
1882 ++NumCopies;
1883 }
1884 } else if (Use->getOpcode() == ISD::BITCAST) {
1885 // f32 returned in a single GPR.
1886 if (!Use->hasNUsesOfValue(1, 0))
1887 return false;
1888 Use = *Use->use_begin();
1889 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1890 return false;
1891 Copies[NumCopies++] = Use;
1892 } else {
1893 return false;
1894 }
1895
1896 if (NumCopies != 1 && NumCopies != 2)
1897 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001898
1899 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001900 for (unsigned i = 0; i < NumCopies; ++i) {
1901 SDNode *Copy = Copies[i];
1902 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1903 UI != UE; ++UI) {
1904 if (UI->getOpcode() == ISD::CopyToReg) {
1905 SDNode *Use = *UI;
1906 if (Use == Copies[0] || Use == Copies[1])
1907 continue;
1908 return false;
1909 }
1910 if (UI->getOpcode() != ARMISD::RET_FLAG)
1911 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001912 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001913 }
1914 }
1915
Evan Cheng1bf891a2010-12-01 22:59:46 +00001916 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001917}
1918
Evan Cheng485fafc2011-03-21 01:19:09 +00001919bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1920 if (!EnableARMTailCalls)
1921 return false;
1922
1923 if (!CI->isTailCall())
1924 return false;
1925
1926 return !Subtarget->isThumb1Only();
1927}
1928
Bob Wilsonb62d2572009-11-03 00:02:05 +00001929// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
1930// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
1931// one of the above mentioned nodes. It has to be wrapped because otherwise
1932// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1933// be used to form addressing mode. These wrapped nodes will be selected
1934// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00001935static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001937 // FIXME there is no actual debug info here
1938 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00001939 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00001941 if (CP->isMachineConstantPoolEntry())
1942 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1943 CP->getAlignment());
1944 else
1945 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1946 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00001948}
1949
Jim Grosbache1102ca2010-07-19 17:20:38 +00001950unsigned ARMTargetLowering::getJumpTableEncoding() const {
1951 return MachineJumpTableInfo::EK_Inline;
1952}
1953
Dan Gohmand858e902010-04-17 15:26:15 +00001954SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
1955 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00001956 MachineFunction &MF = DAG.getMachineFunction();
1957 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1958 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00001959 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00001960 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00001961 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00001962 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
1963 SDValue CPAddr;
1964 if (RelocM == Reloc::Static) {
1965 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
1966 } else {
1967 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001968 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001969 ARMConstantPoolValue *CPV =
1970 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
1971 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00001972 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
1973 }
1974 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
1975 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001976 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001977 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00001978 if (RelocM == Reloc::Static)
1979 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00001980 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00001981 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00001982}
1983
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001984// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00001985SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001986ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00001987 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001988 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00001989 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001990 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00001991 MachineFunction &MF = DAG.getMachineFunction();
1992 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001993 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00001994 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00001995 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
1996 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001997 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00001999 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002000 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002001 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002002 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002003
Evan Chenge7e0d622009-11-06 22:24:13 +00002004 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002005 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002006
2007 // call __tls_get_addr.
2008 ArgListTy Args;
2009 ArgListEntry Entry;
2010 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002011 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002012 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002013 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002014 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002015 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002016 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002017 0, CallingConv::C, false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002018 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002019 return CallResult.first;
2020}
2021
2022// Lower ISD::GlobalTLSAddress using the "initial exec" or
2023// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002024SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002025ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002026 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002027 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002028 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Offset;
2030 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002032 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002033 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002034
Chris Lattner4fb63d02009-07-15 04:12:33 +00002035 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002036 MachineFunction &MF = DAG.getMachineFunction();
2037 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002038 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002039 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002040 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2041 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002042 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2043 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2044 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002045 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002046 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002047 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002048 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002049 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002050 Chain = Offset.getValue(1);
2051
Evan Chenge7e0d622009-11-06 22:24:13 +00002052 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002053 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002054
Evan Cheng9eda6892009-10-31 03:39:36 +00002055 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002056 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002057 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002058 } else {
2059 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002060 ARMConstantPoolValue *CPV =
2061 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002062 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002063 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002064 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002065 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002066 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002067 }
2068
2069 // The address of the thread local variable is the add of the thread
2070 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002071 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002072}
2073
Dan Gohman475871a2008-07-27 21:46:04 +00002074SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002075ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002076 // TODO: implement the "local dynamic" model
2077 assert(Subtarget->isTargetELF() &&
2078 "TLS not implemented for non-ELF targets");
2079 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2080 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2081 // otherwise use the "Local Exec" TLS Model
2082 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2083 return LowerToTLSGeneralDynamicModel(GA, DAG);
2084 else
2085 return LowerToTLSExecModels(GA, DAG);
2086}
2087
Dan Gohman475871a2008-07-27 21:46:04 +00002088SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002089 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002090 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002091 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002092 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002093 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2094 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002095 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002096 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002097 ARMConstantPoolConstant::Create(GV,
2098 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002099 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002101 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002102 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002103 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002104 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002106 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002107 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002108 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002109 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002110 MachinePointerInfo::getGOT(),
2111 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002112 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002113 }
2114
2115 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002116 // pair. This is always cheaper.
2117 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002118 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002119 // FIXME: Once remat is capable of dealing with instructions with register
2120 // operands, expand this into two nodes.
2121 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2122 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002123 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002124 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2125 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2126 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2127 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002128 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002129 }
2130}
2131
Dan Gohman475871a2008-07-27 21:46:04 +00002132SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002133 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002134 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002135 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002136 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002137 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002138 MachineFunction &MF = DAG.getMachineFunction();
2139 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2140
Evan Cheng4abce0c2011-05-27 20:11:27 +00002141 // FIXME: Enable this for static codegen when tool issues are fixed.
Evan Chengf31151f2011-10-26 01:17:44 +00002142 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002143 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002144 // FIXME: Once remat is capable of dealing with instructions with register
2145 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002146 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002147 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2148 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2149
Evan Cheng53519f02011-01-21 18:55:51 +00002150 unsigned Wrapper = (RelocM == Reloc::PIC_)
2151 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2152 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002153 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002154 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2155 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002156 MachinePointerInfo::getGOT(),
2157 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002158 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002159 }
2160
2161 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002162 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002163 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002164 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002165 } else {
2166 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002167 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2168 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002169 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2170 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002171 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002172 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002174
Evan Cheng9eda6892009-10-31 03:39:36 +00002175 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002176 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002177 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002178 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002179
2180 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002181 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002182 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002183 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002184
Evan Cheng63476a82009-09-03 07:04:02 +00002185 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002186 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002187 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002188
2189 return Result;
2190}
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002193 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002194 assert(Subtarget->isTargetELF() &&
2195 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002196 MachineFunction &MF = DAG.getMachineFunction();
2197 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002198 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002200 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002201 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002202 ARMConstantPoolValue *CPV =
2203 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2204 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002205 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002207 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002208 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002209 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002210 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002212}
2213
Jim Grosbach0e0da732009-05-12 23:59:14 +00002214SDValue
Jim Grosbache4ad3872010-10-19 23:27:08 +00002215ARMTargetLowering::LowerEH_SJLJ_DISPATCHSETUP(SDValue Op, SelectionDAG &DAG)
2216 const {
2217 DebugLoc dl = Op.getDebugLoc();
2218 return DAG.getNode(ARMISD::EH_SJLJ_DISPATCHSETUP, dl, MVT::Other,
Bill Wendling61512ba2011-05-11 01:11:55 +00002219 Op.getOperand(0), Op.getOperand(1));
Jim Grosbache4ad3872010-10-19 23:27:08 +00002220}
2221
2222SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002223ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2224 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002225 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002226 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2227 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002228 Op.getOperand(1), Val);
2229}
2230
2231SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002232ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2233 DebugLoc dl = Op.getDebugLoc();
2234 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2235 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2236}
2237
2238SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002239ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002240 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002241 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002242 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002243 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002244 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002245 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002246 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002247 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2248 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002249 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002250 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002251 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002252 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002253 EVT PtrVT = getPointerTy();
2254 DebugLoc dl = Op.getDebugLoc();
2255 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2256 SDValue CPAddr;
2257 unsigned PCAdj = (RelocM != Reloc::PIC_)
2258 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002259 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002260 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2261 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002262 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002263 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002264 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002265 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002266 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002267 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002268
2269 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002270 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002271 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2272 }
2273 return Result;
2274 }
Evan Cheng92e39162011-03-29 23:06:19 +00002275 case Intrinsic::arm_neon_vmulls:
2276 case Intrinsic::arm_neon_vmullu: {
2277 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2278 ? ARMISD::VMULLs : ARMISD::VMULLu;
2279 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2280 Op.getOperand(1), Op.getOperand(2));
2281 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002282 }
2283}
2284
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002285static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002286 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002287 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002288 if (!Subtarget->hasDataBarrier()) {
2289 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2290 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2291 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002292 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002293 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002294 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002295 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002296 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002297
2298 SDValue Op5 = Op.getOperand(5);
2299 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2300 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2301 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2302 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2303
2304 ARM_MB::MemBOpt DMBOpt;
2305 if (isDeviceBarrier)
2306 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2307 else
2308 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2309 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2310 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002311}
2312
Eli Friedman26689ac2011-08-03 21:06:02 +00002313
2314static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2315 const ARMSubtarget *Subtarget) {
2316 // FIXME: handle "fence singlethread" more efficiently.
2317 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002318 if (!Subtarget->hasDataBarrier()) {
2319 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2320 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2321 // here.
2322 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2323 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002324 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002325 DAG.getConstant(0, MVT::i32));
2326 }
2327
Eli Friedman26689ac2011-08-03 21:06:02 +00002328 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002329 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002330}
2331
Evan Chengdfed19f2010-11-03 06:34:55 +00002332static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2333 const ARMSubtarget *Subtarget) {
2334 // ARM pre v5TE and Thumb1 does not have preload instructions.
2335 if (!(Subtarget->isThumb2() ||
2336 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2337 // Just preserve the chain.
2338 return Op.getOperand(0);
2339
2340 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002341 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2342 if (!isRead &&
2343 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2344 // ARMv7 with MP extension has PLDW.
2345 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002346
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002347 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2348 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002349 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002350 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002351 isData = ~isData & 1;
2352 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002353
2354 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002355 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2356 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002357}
2358
Dan Gohman1e93df62010-04-17 14:41:14 +00002359static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2360 MachineFunction &MF = DAG.getMachineFunction();
2361 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2362
Evan Chenga8e29892007-01-19 07:51:42 +00002363 // vastart just stores the address of the VarArgsFrameIndex slot into the
2364 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002365 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002366 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002367 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002368 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002369 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2370 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002371}
2372
Dan Gohman475871a2008-07-27 21:46:04 +00002373SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002374ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2375 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002376 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002377 MachineFunction &MF = DAG.getMachineFunction();
2378 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2379
2380 TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002381 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002382 RC = ARM::tGPRRegisterClass;
2383 else
2384 RC = ARM::GPRRegisterClass;
2385
2386 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002387 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002388 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002389
2390 SDValue ArgValue2;
2391 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002392 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002393 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002394
2395 // Create load node to retrieve arguments from the stack.
2396 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002397 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002398 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002399 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002400 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002401 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002403 }
2404
Jim Grosbache5165492009-11-09 00:11:35 +00002405 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002406}
2407
Stuart Hastingsc7315872011-04-20 16:47:52 +00002408void
2409ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2410 unsigned &VARegSize, unsigned &VARegSaveSize)
2411 const {
2412 unsigned NumGPRs;
2413 if (CCInfo.isFirstByValRegValid())
2414 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2415 else {
2416 unsigned int firstUnalloced;
2417 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2418 sizeof(GPRArgRegs) /
2419 sizeof(GPRArgRegs[0]));
2420 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2421 }
2422
2423 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2424 VARegSize = NumGPRs * 4;
2425 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2426}
2427
2428// The remaining GPRs hold either the beginning of variable-argument
2429// data, or the beginning of an aggregate passed by value (usuall
2430// byval). Either way, we allocate stack slots adjacent to the data
2431// provided by our caller, and store the unallocated registers there.
2432// If this is a variadic function, the va_list pointer will begin with
2433// these values; otherwise, this reassembles a (byval) structure that
2434// was split between registers and memory.
2435void
2436ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2437 DebugLoc dl, SDValue &Chain,
2438 unsigned ArgOffset) const {
2439 MachineFunction &MF = DAG.getMachineFunction();
2440 MachineFrameInfo *MFI = MF.getFrameInfo();
2441 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2442 unsigned firstRegToSaveIndex;
2443 if (CCInfo.isFirstByValRegValid())
2444 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2445 else {
2446 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2447 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2448 }
2449
2450 unsigned VARegSize, VARegSaveSize;
2451 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2452 if (VARegSaveSize) {
2453 // If this function is vararg, store any remaining integer argument regs
2454 // to their spots on the stack so that they may be loaded by deferencing
2455 // the result of va_next.
2456 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002457 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2458 ArgOffset + VARegSaveSize
2459 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002460 false));
2461 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2462 getPointerTy());
2463
2464 SmallVector<SDValue, 4> MemOps;
2465 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
2466 TargetRegisterClass *RC;
2467 if (AFI->isThumb1OnlyFunction())
2468 RC = ARM::tGPRRegisterClass;
2469 else
2470 RC = ARM::GPRRegisterClass;
2471
2472 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2473 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2474 SDValue Store =
2475 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002476 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002477 false, false, 0);
2478 MemOps.push_back(Store);
2479 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2480 DAG.getConstant(4, getPointerTy()));
2481 }
2482 if (!MemOps.empty())
2483 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2484 &MemOps[0], MemOps.size());
2485 } else
2486 // This will point to the next argument passed via stack.
2487 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2488}
2489
Bob Wilson5bafff32009-06-22 23:27:02 +00002490SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002491ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002492 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002493 const SmallVectorImpl<ISD::InputArg>
2494 &Ins,
2495 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002496 SmallVectorImpl<SDValue> &InVals)
2497 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002498 MachineFunction &MF = DAG.getMachineFunction();
2499 MachineFrameInfo *MFI = MF.getFrameInfo();
2500
Bob Wilson1f595bb2009-04-17 19:07:39 +00002501 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2502
2503 // Assign locations to all of the incoming arguments.
2504 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002505 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2506 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002507 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002508 CCAssignFnForNode(CallConv, /* Return*/ false,
2509 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002510
2511 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002512 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002513
Stuart Hastingsf222e592011-02-28 17:17:53 +00002514 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2516 CCValAssign &VA = ArgLocs[i];
2517
Bob Wilsondee46d72009-04-17 20:35:10 +00002518 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002519 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002520 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002521
Bob Wilson1f595bb2009-04-17 19:07:39 +00002522 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002523 // f64 and vector types are split up into multiple registers or
2524 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002525 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002526 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002527 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002528 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002529 SDValue ArgValue2;
2530 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002531 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002532 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2533 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002534 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002535 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002536 } else {
2537 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2538 Chain, DAG, dl);
2539 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002540 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2541 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002542 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002543 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002544 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2545 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002546 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002547
Bob Wilson5bafff32009-06-22 23:27:02 +00002548 } else {
2549 TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002550
Owen Anderson825b72b2009-08-11 20:47:22 +00002551 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002552 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002553 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002554 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002555 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002556 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002557 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002558 RC = (AFI->isThumb1OnlyFunction() ?
2559 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002560 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002561 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002562
2563 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002564 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002566 }
2567
2568 // If this is an 8 or 16-bit value, it is really passed promoted
2569 // to 32 bits. Insert an assert[sz]ext to capture this, then
2570 // truncate to the right size.
2571 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002572 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002573 case CCValAssign::Full: break;
2574 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002575 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002576 break;
2577 case CCValAssign::SExt:
2578 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2579 DAG.getValueType(VA.getValVT()));
2580 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2581 break;
2582 case CCValAssign::ZExt:
2583 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2584 DAG.getValueType(VA.getValVT()));
2585 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2586 break;
2587 }
2588
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002590
2591 } else { // VA.isRegLoc()
2592
2593 // sanity check
2594 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002595 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002596
Stuart Hastingsf222e592011-02-28 17:17:53 +00002597 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002598
Stuart Hastingsf222e592011-02-28 17:17:53 +00002599 // Some Ins[] entries become multiple ArgLoc[] entries.
2600 // Process them only once.
2601 if (index != lastInsIndex)
2602 {
2603 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002604 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002605 // This can be changed with more analysis.
2606 // In case of tail call optimization mark all arguments mutable.
2607 // Since they could be overwritten by lowering of arguments in case of
2608 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002609 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002610 unsigned VARegSize, VARegSaveSize;
2611 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2612 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2613 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002614 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002615 int FI = MFI->CreateFixedObject(Bytes,
2616 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002617 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2618 } else {
2619 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2620 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002621
Stuart Hastingsf222e592011-02-28 17:17:53 +00002622 // Create load nodes to retrieve arguments from the stack.
2623 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2624 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2625 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002626 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002627 }
2628 lastInsIndex = index;
2629 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002630 }
2631 }
2632
2633 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002634 if (isVarArg)
2635 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002636
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002638}
2639
2640/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002641static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002642 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002643 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002644 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002645 // Maybe this has already been legalized into the constant pool?
2646 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002647 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002648 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002649 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002650 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002651 }
2652 }
2653 return false;
2654}
2655
Evan Chenga8e29892007-01-19 07:51:42 +00002656/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2657/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002658SDValue
2659ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002660 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002661 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002662 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002663 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002664 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002665 // Constant does not fit, try adjusting it by one?
2666 switch (CC) {
2667 default: break;
2668 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002669 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002670 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002671 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002672 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002673 }
2674 break;
2675 case ISD::SETULT:
2676 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002677 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002678 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002679 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002680 }
2681 break;
2682 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002683 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002684 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002685 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002686 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002687 }
2688 break;
2689 case ISD::SETULE:
2690 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002691 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002692 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002694 }
2695 break;
2696 }
2697 }
2698 }
2699
2700 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002701 ARMISD::NodeType CompareType;
2702 switch (CondCode) {
2703 default:
2704 CompareType = ARMISD::CMP;
2705 break;
2706 case ARMCC::EQ:
2707 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002708 // Uses only Z Flag
2709 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002710 break;
2711 }
Evan Cheng218977b2010-07-13 19:27:42 +00002712 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002713 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002714}
2715
2716/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002717SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002718ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002719 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002720 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002721 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002722 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002723 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002724 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2725 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002726}
2727
Bob Wilson79f56c92011-03-08 01:17:20 +00002728/// duplicateCmp - Glue values can have only one use, so this function
2729/// duplicates a comparison node.
2730SDValue
2731ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2732 unsigned Opc = Cmp.getOpcode();
2733 DebugLoc DL = Cmp.getDebugLoc();
2734 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2735 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2736
2737 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2738 Cmp = Cmp.getOperand(0);
2739 Opc = Cmp.getOpcode();
2740 if (Opc == ARMISD::CMPFP)
2741 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2742 else {
2743 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2744 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2745 }
2746 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2747}
2748
Bill Wendlingde2b1512010-08-11 08:43:16 +00002749SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2750 SDValue Cond = Op.getOperand(0);
2751 SDValue SelectTrue = Op.getOperand(1);
2752 SDValue SelectFalse = Op.getOperand(2);
2753 DebugLoc dl = Op.getDebugLoc();
2754
2755 // Convert:
2756 //
2757 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2758 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2759 //
2760 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2761 const ConstantSDNode *CMOVTrue =
2762 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2763 const ConstantSDNode *CMOVFalse =
2764 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2765
2766 if (CMOVTrue && CMOVFalse) {
2767 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2768 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2769
2770 SDValue True;
2771 SDValue False;
2772 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2773 True = SelectTrue;
2774 False = SelectFalse;
2775 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2776 True = SelectFalse;
2777 False = SelectTrue;
2778 }
2779
2780 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002781 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002782 SDValue ARMcc = Cond.getOperand(2);
2783 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002784 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002785 assert(True.getValueType() == VT);
2786 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002787 }
2788 }
2789 }
2790
2791 return DAG.getSelectCC(dl, Cond,
2792 DAG.getConstant(0, Cond.getValueType()),
2793 SelectTrue, SelectFalse, ISD::SETNE);
2794}
2795
Dan Gohmand858e902010-04-17 15:26:15 +00002796SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002797 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002798 SDValue LHS = Op.getOperand(0);
2799 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002800 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002801 SDValue TrueVal = Op.getOperand(2);
2802 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002803 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002804
Owen Anderson825b72b2009-08-11 20:47:22 +00002805 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002806 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002808 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002809 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002810 }
2811
2812 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002813 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002814
Evan Cheng218977b2010-07-13 19:27:42 +00002815 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2816 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002817 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002818 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002819 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002820 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002821 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002822 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002823 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002824 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002825 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002826 }
2827 return Result;
2828}
2829
Evan Cheng218977b2010-07-13 19:27:42 +00002830/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2831/// to morph to an integer compare sequence.
2832static bool canChangeToInt(SDValue Op, bool &SeenZero,
2833 const ARMSubtarget *Subtarget) {
2834 SDNode *N = Op.getNode();
2835 if (!N->hasOneUse())
2836 // Otherwise it requires moving the value from fp to integer registers.
2837 return false;
2838 if (!N->getNumValues())
2839 return false;
2840 EVT VT = Op.getValueType();
2841 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2842 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2843 // vmrs are very slow, e.g. cortex-a8.
2844 return false;
2845
2846 if (isFloatingPointZero(Op)) {
2847 SeenZero = true;
2848 return true;
2849 }
2850 return ISD::isNormalLoad(N);
2851}
2852
2853static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2854 if (isFloatingPointZero(Op))
2855 return DAG.getConstant(0, MVT::i32);
2856
2857 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2858 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002859 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002860 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002861 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002862
2863 llvm_unreachable("Unknown VFP cmp argument!");
2864}
2865
2866static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2867 SDValue &RetVal1, SDValue &RetVal2) {
2868 if (isFloatingPointZero(Op)) {
2869 RetVal1 = DAG.getConstant(0, MVT::i32);
2870 RetVal2 = DAG.getConstant(0, MVT::i32);
2871 return;
2872 }
2873
2874 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2875 SDValue Ptr = Ld->getBasePtr();
2876 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2877 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002878 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002879 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002880 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002881
2882 EVT PtrType = Ptr.getValueType();
2883 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2884 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2885 PtrType, Ptr, DAG.getConstant(4, PtrType));
2886 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2887 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002888 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002889 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002890 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002891 return;
2892 }
2893
2894 llvm_unreachable("Unknown VFP cmp argument!");
2895}
2896
2897/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2898/// f32 and even f64 comparisons to integer ones.
2899SDValue
2900ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2901 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002902 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002903 SDValue LHS = Op.getOperand(2);
2904 SDValue RHS = Op.getOperand(3);
2905 SDValue Dest = Op.getOperand(4);
2906 DebugLoc dl = Op.getDebugLoc();
2907
2908 bool SeenZero = false;
2909 if (canChangeToInt(LHS, SeenZero, Subtarget) &&
2910 canChangeToInt(RHS, SeenZero, Subtarget) &&
Evan Cheng60108e92010-07-15 22:07:12 +00002911 // If one of the operand is zero, it's safe to ignore the NaN case since
2912 // we only care about equality comparisons.
2913 (SeenZero || (DAG.isKnownNeverNaN(LHS) && DAG.isKnownNeverNaN(RHS)))) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002914 // If unsafe fp math optimization is enabled and there are no other uses of
2915 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002916 // to an integer comparison.
2917 if (CC == ISD::SETOEQ)
2918 CC = ISD::SETEQ;
2919 else if (CC == ISD::SETUNE)
2920 CC = ISD::SETNE;
2921
2922 SDValue ARMcc;
2923 if (LHS.getValueType() == MVT::f32) {
2924 LHS = bitcastf32Toi32(LHS, DAG);
2925 RHS = bitcastf32Toi32(RHS, DAG);
2926 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
2927 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
2928 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
2929 Chain, Dest, ARMcc, CCR, Cmp);
2930 }
2931
2932 SDValue LHS1, LHS2;
2933 SDValue RHS1, RHS2;
2934 expandf64Toi32(LHS, DAG, LHS1, LHS2);
2935 expandf64Toi32(RHS, DAG, RHS1, RHS2);
2936 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
2937 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002938 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002939 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
2940 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
2941 }
2942
2943 return SDValue();
2944}
2945
2946SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2947 SDValue Chain = Op.getOperand(0);
2948 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2949 SDValue LHS = Op.getOperand(2);
2950 SDValue RHS = Op.getOperand(3);
2951 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00002952 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002953
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002955 SDValue ARMcc;
2956 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00002959 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002960 }
2961
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00002963
2964 if (UnsafeFPMath &&
2965 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
2966 CC == ISD::SETNE || CC == ISD::SETUNE)) {
2967 SDValue Result = OptimizeVFPBrcond(Op, DAG);
2968 if (Result.getNode())
2969 return Result;
2970 }
2971
Evan Chenga8e29892007-01-19 07:51:42 +00002972 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002973 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002974
Evan Cheng218977b2010-07-13 19:27:42 +00002975 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2976 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002978 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00002979 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00002980 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002981 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002982 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
2983 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00002984 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00002985 }
2986 return Res;
2987}
2988
Dan Gohmand858e902010-04-17 15:26:15 +00002989SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue Chain = Op.getOperand(0);
2991 SDValue Table = Op.getOperand(1);
2992 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002993 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002994
Owen Andersone50ed302009-08-10 22:56:29 +00002995 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00002996 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
2997 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00002998 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00002999 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003000 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003001 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3002 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003003 if (Subtarget->isThumb2()) {
3004 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3005 // which does another jump to the destination. This also makes it easier
3006 // to translate it to TBB / TBH later.
3007 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003009 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003010 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003011 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003012 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003013 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003014 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003015 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003016 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003017 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003018 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003019 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003020 MachinePointerInfo::getJumpTable(),
3021 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003022 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003023 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003024 }
Evan Chenga8e29892007-01-19 07:51:42 +00003025}
3026
Eli Friedman14e809c2011-11-09 23:36:02 +00003027static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3028 EVT VT = Op.getValueType();
3029 assert(VT.getVectorElementType() == MVT::i32 && "Unexpected custom lowering");
3030
3031 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3032 return Op;
3033 return DAG.UnrollVectorOp(Op.getNode());
3034}
3035
Bob Wilson76a312b2010-03-19 22:51:32 +00003036static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003037 EVT VT = Op.getValueType();
3038 if (VT.isVector())
3039 return LowerVectorFP_TO_INT(Op, DAG);
3040
Bob Wilson76a312b2010-03-19 22:51:32 +00003041 DebugLoc dl = Op.getDebugLoc();
3042 unsigned Opc;
3043
3044 switch (Op.getOpcode()) {
3045 default:
3046 assert(0 && "Invalid opcode!");
3047 case ISD::FP_TO_SINT:
3048 Opc = ARMISD::FTOSI;
3049 break;
3050 case ISD::FP_TO_UINT:
3051 Opc = ARMISD::FTOUI;
3052 break;
3053 }
3054 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003055 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003056}
3057
Cameron Zwarich3007d332011-03-29 21:41:55 +00003058static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3059 EVT VT = Op.getValueType();
3060 DebugLoc dl = Op.getDebugLoc();
3061
Eli Friedman14e809c2011-11-09 23:36:02 +00003062 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3063 if (VT.getVectorElementType() == MVT::f32)
3064 return Op;
3065 return DAG.UnrollVectorOp(Op.getNode());
3066 }
3067
Duncan Sands1f6a3292011-08-12 14:54:45 +00003068 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3069 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003070 if (VT != MVT::v4f32)
3071 return DAG.UnrollVectorOp(Op.getNode());
3072
3073 unsigned CastOpc;
3074 unsigned Opc;
3075 switch (Op.getOpcode()) {
3076 default:
3077 assert(0 && "Invalid opcode!");
3078 case ISD::SINT_TO_FP:
3079 CastOpc = ISD::SIGN_EXTEND;
3080 Opc = ISD::SINT_TO_FP;
3081 break;
3082 case ISD::UINT_TO_FP:
3083 CastOpc = ISD::ZERO_EXTEND;
3084 Opc = ISD::UINT_TO_FP;
3085 break;
3086 }
3087
3088 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3089 return DAG.getNode(Opc, dl, VT, Op);
3090}
3091
Bob Wilson76a312b2010-03-19 22:51:32 +00003092static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3093 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003094 if (VT.isVector())
3095 return LowerVectorINT_TO_FP(Op, DAG);
3096
Bob Wilson76a312b2010-03-19 22:51:32 +00003097 DebugLoc dl = Op.getDebugLoc();
3098 unsigned Opc;
3099
3100 switch (Op.getOpcode()) {
3101 default:
3102 assert(0 && "Invalid opcode!");
3103 case ISD::SINT_TO_FP:
3104 Opc = ARMISD::SITOF;
3105 break;
3106 case ISD::UINT_TO_FP:
3107 Opc = ARMISD::UITOF;
3108 break;
3109 }
3110
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003111 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003112 return DAG.getNode(Opc, dl, VT, Op);
3113}
3114
Evan Cheng515fe3a2010-07-08 02:08:50 +00003115SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003116 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003117 SDValue Tmp0 = Op.getOperand(0);
3118 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003119 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003120 EVT VT = Op.getValueType();
3121 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003122 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3123 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3124 bool UseNEON = !InGPR && Subtarget->hasNEON();
3125
3126 if (UseNEON) {
3127 // Use VBSL to copy the sign bit.
3128 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3129 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3130 DAG.getTargetConstant(EncodedVal, MVT::i32));
3131 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3132 if (VT == MVT::f64)
3133 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3134 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3135 DAG.getConstant(32, MVT::i32));
3136 else /*if (VT == MVT::f32)*/
3137 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3138 if (SrcVT == MVT::f32) {
3139 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3140 if (VT == MVT::f64)
3141 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3142 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3143 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003144 } else if (VT == MVT::f32)
3145 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3146 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3147 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003148 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3149 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3150
3151 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3152 MVT::i32);
3153 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3154 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3155 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003156
Evan Chenge573fb32011-02-23 02:24:55 +00003157 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3158 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3159 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003160 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003161 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3162 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3163 DAG.getConstant(0, MVT::i32));
3164 } else {
3165 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3166 }
3167
3168 return Res;
3169 }
Evan Chengc143dd42011-02-11 02:28:55 +00003170
3171 // Bitcast operand 1 to i32.
3172 if (SrcVT == MVT::f64)
3173 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3174 &Tmp1, 1).getValue(1);
3175 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3176
Evan Chenge573fb32011-02-23 02:24:55 +00003177 // Or in the signbit with integer operations.
3178 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3179 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3180 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3181 if (VT == MVT::f32) {
3182 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3183 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3184 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3185 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003186 }
3187
Evan Chenge573fb32011-02-23 02:24:55 +00003188 // f64: Or the high part with signbit and then combine two parts.
3189 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3190 &Tmp0, 1);
3191 SDValue Lo = Tmp0.getValue(0);
3192 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3193 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3194 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003195}
3196
Evan Cheng2457f2c2010-05-22 01:47:14 +00003197SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3198 MachineFunction &MF = DAG.getMachineFunction();
3199 MachineFrameInfo *MFI = MF.getFrameInfo();
3200 MFI->setReturnAddressIsTaken(true);
3201
3202 EVT VT = Op.getValueType();
3203 DebugLoc dl = Op.getDebugLoc();
3204 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3205 if (Depth) {
3206 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3207 SDValue Offset = DAG.getConstant(4, MVT::i32);
3208 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3209 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003210 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003211 }
3212
3213 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003214 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003215 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3216}
3217
Dan Gohmand858e902010-04-17 15:26:15 +00003218SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003219 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3220 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003221
Owen Andersone50ed302009-08-10 22:56:29 +00003222 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003223 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3224 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003225 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003226 ? ARM::R7 : ARM::R11;
3227 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3228 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003229 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3230 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003231 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003232 return FrameAddr;
3233}
3234
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003235/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003236/// expand a bit convert where either the source or destination type is i64 to
3237/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3238/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3239/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003240static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003241 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3242 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003244
Bob Wilson9f3f0612010-04-17 05:30:19 +00003245 // This function is only supposed to be called for i64 types, either as the
3246 // source or destination of the bit convert.
3247 EVT SrcVT = Op.getValueType();
3248 EVT DstVT = N->getValueType(0);
3249 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003250 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003251
Bob Wilson9f3f0612010-04-17 05:30:19 +00003252 // Turn i64->f64 into VMOVDRR.
3253 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3255 DAG.getConstant(0, MVT::i32));
3256 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3257 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003258 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003259 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003260 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003261
Jim Grosbache5165492009-11-09 00:11:35 +00003262 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003263 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3264 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3265 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3266 // Merge the pieces into a single i64 value.
3267 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3268 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003269
Bob Wilson9f3f0612010-04-17 05:30:19 +00003270 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003271}
3272
Bob Wilson5bafff32009-06-22 23:27:02 +00003273/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003274/// Zero vectors are used to represent vector negation and in those cases
3275/// will be implemented with the NEON VNEG instruction. However, VNEG does
3276/// not support i64 elements, so sometimes the zero vectors will need to be
3277/// explicitly constructed. Regardless, use a canonical VMOV to create the
3278/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003279static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003280 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003281 // The canonical modified immediate encoding of a zero vector is....0!
3282 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3283 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3284 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003285 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003286}
3287
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003288/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3289/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003290SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3291 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003292 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3293 EVT VT = Op.getValueType();
3294 unsigned VTBits = VT.getSizeInBits();
3295 DebugLoc dl = Op.getDebugLoc();
3296 SDValue ShOpLo = Op.getOperand(0);
3297 SDValue ShOpHi = Op.getOperand(1);
3298 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003299 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003300 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003301
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003302 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3303
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003304 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3305 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3306 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3307 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3308 DAG.getConstant(VTBits, MVT::i32));
3309 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3310 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003311 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003312
3313 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3314 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003315 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003316 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003317 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003318 CCR, Cmp);
3319
3320 SDValue Ops[2] = { Lo, Hi };
3321 return DAG.getMergeValues(Ops, 2, dl);
3322}
3323
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003324/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3325/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003326SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3327 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003328 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3329 EVT VT = Op.getValueType();
3330 unsigned VTBits = VT.getSizeInBits();
3331 DebugLoc dl = Op.getDebugLoc();
3332 SDValue ShOpLo = Op.getOperand(0);
3333 SDValue ShOpHi = Op.getOperand(1);
3334 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003335 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003336
3337 assert(Op.getOpcode() == ISD::SHL_PARTS);
3338 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3339 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3340 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3341 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3342 DAG.getConstant(VTBits, MVT::i32));
3343 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3344 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3345
3346 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3347 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3348 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003349 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003350 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003351 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003352 CCR, Cmp);
3353
3354 SDValue Ops[2] = { Lo, Hi };
3355 return DAG.getMergeValues(Ops, 2, dl);
3356}
3357
Jim Grosbach4725ca72010-09-08 03:54:02 +00003358SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003359 SelectionDAG &DAG) const {
3360 // The rounding mode is in bits 23:22 of the FPSCR.
3361 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3362 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3363 // so that the shift + and get folded into a bitfield extract.
3364 DebugLoc dl = Op.getDebugLoc();
3365 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3366 DAG.getConstant(Intrinsic::arm_get_fpscr,
3367 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003368 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003369 DAG.getConstant(1U << 22, MVT::i32));
3370 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3371 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003372 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003373 DAG.getConstant(3, MVT::i32));
3374}
3375
Jim Grosbach3482c802010-01-18 19:58:49 +00003376static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3377 const ARMSubtarget *ST) {
3378 EVT VT = N->getValueType(0);
3379 DebugLoc dl = N->getDebugLoc();
3380
3381 if (!ST->hasV6T2Ops())
3382 return SDValue();
3383
3384 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3385 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3386}
3387
Bob Wilson5bafff32009-06-22 23:27:02 +00003388static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3389 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003390 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003391 DebugLoc dl = N->getDebugLoc();
3392
Bob Wilsond5448bb2010-11-18 21:16:28 +00003393 if (!VT.isVector())
3394 return SDValue();
3395
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003397 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003398
Bob Wilsond5448bb2010-11-18 21:16:28 +00003399 // Left shifts translate directly to the vshiftu intrinsic.
3400 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003401 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003402 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3403 N->getOperand(0), N->getOperand(1));
3404
3405 assert((N->getOpcode() == ISD::SRA ||
3406 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3407
3408 // NEON uses the same intrinsics for both left and right shifts. For
3409 // right shifts, the shift amounts are negative, so negate the vector of
3410 // shift amounts.
3411 EVT ShiftVT = N->getOperand(1).getValueType();
3412 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3413 getZeroVector(ShiftVT, DAG, dl),
3414 N->getOperand(1));
3415 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3416 Intrinsic::arm_neon_vshifts :
3417 Intrinsic::arm_neon_vshiftu);
3418 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3419 DAG.getConstant(vshiftInt, MVT::i32),
3420 N->getOperand(0), NegatedCount);
3421}
3422
3423static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3424 const ARMSubtarget *ST) {
3425 EVT VT = N->getValueType(0);
3426 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003427
Eli Friedmance392eb2009-08-22 03:13:10 +00003428 // We can get here for a node like i32 = ISD::SHL i32, i64
3429 if (VT != MVT::i64)
3430 return SDValue();
3431
3432 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003433 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003434
Chris Lattner27a6c732007-11-24 07:07:01 +00003435 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3436 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003437 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003438 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003439
Chris Lattner27a6c732007-11-24 07:07:01 +00003440 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003441 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003442
Chris Lattner27a6c732007-11-24 07:07:01 +00003443 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003445 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003447 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003448
Chris Lattner27a6c732007-11-24 07:07:01 +00003449 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3450 // captures the result into a carry flag.
3451 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003452 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003453
Chris Lattner27a6c732007-11-24 07:07:01 +00003454 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003456
Chris Lattner27a6c732007-11-24 07:07:01 +00003457 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003459}
3460
Bob Wilson5bafff32009-06-22 23:27:02 +00003461static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3462 SDValue TmpOp0, TmpOp1;
3463 bool Invert = false;
3464 bool Swap = false;
3465 unsigned Opc = 0;
3466
3467 SDValue Op0 = Op.getOperand(0);
3468 SDValue Op1 = Op.getOperand(1);
3469 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003470 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003471 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3472 DebugLoc dl = Op.getDebugLoc();
3473
3474 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3475 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003476 default: llvm_unreachable("Illegal FP comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003477 case ISD::SETUNE:
3478 case ISD::SETNE: Invert = true; // Fallthrough
3479 case ISD::SETOEQ:
3480 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3481 case ISD::SETOLT:
3482 case ISD::SETLT: Swap = true; // Fallthrough
3483 case ISD::SETOGT:
3484 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3485 case ISD::SETOLE:
3486 case ISD::SETLE: Swap = true; // Fallthrough
3487 case ISD::SETOGE:
3488 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3489 case ISD::SETUGE: Swap = true; // Fallthrough
3490 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3491 case ISD::SETUGT: Swap = true; // Fallthrough
3492 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3493 case ISD::SETUEQ: Invert = true; // Fallthrough
3494 case ISD::SETONE:
3495 // Expand this to (OLT | OGT).
3496 TmpOp0 = Op0;
3497 TmpOp1 = Op1;
3498 Opc = ISD::OR;
3499 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3500 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3501 break;
3502 case ISD::SETUO: Invert = true; // Fallthrough
3503 case ISD::SETO:
3504 // Expand this to (OLT | OGE).
3505 TmpOp0 = Op0;
3506 TmpOp1 = Op1;
3507 Opc = ISD::OR;
3508 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3509 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3510 break;
3511 }
3512 } else {
3513 // Integer comparisons.
3514 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003515 default: llvm_unreachable("Illegal integer comparison"); break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003516 case ISD::SETNE: Invert = true;
3517 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3518 case ISD::SETLT: Swap = true;
3519 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3520 case ISD::SETLE: Swap = true;
3521 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3522 case ISD::SETULT: Swap = true;
3523 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3524 case ISD::SETULE: Swap = true;
3525 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3526 }
3527
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003528 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003529 if (Opc == ARMISD::VCEQ) {
3530
3531 SDValue AndOp;
3532 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3533 AndOp = Op0;
3534 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3535 AndOp = Op1;
3536
3537 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003538 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003539 AndOp = AndOp.getOperand(0);
3540
3541 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3542 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003543 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3544 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003545 Invert = !Invert;
3546 }
3547 }
3548 }
3549
3550 if (Swap)
3551 std::swap(Op0, Op1);
3552
Owen Andersonc24cb352010-11-08 23:21:22 +00003553 // If one of the operands is a constant vector zero, attempt to fold the
3554 // comparison to a specialized compare-against-zero form.
3555 SDValue SingleOp;
3556 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3557 SingleOp = Op0;
3558 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3559 if (Opc == ARMISD::VCGE)
3560 Opc = ARMISD::VCLEZ;
3561 else if (Opc == ARMISD::VCGT)
3562 Opc = ARMISD::VCLTZ;
3563 SingleOp = Op1;
3564 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003565
Owen Andersonc24cb352010-11-08 23:21:22 +00003566 SDValue Result;
3567 if (SingleOp.getNode()) {
3568 switch (Opc) {
3569 case ARMISD::VCEQ:
3570 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3571 case ARMISD::VCGE:
3572 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3573 case ARMISD::VCLEZ:
3574 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3575 case ARMISD::VCGT:
3576 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3577 case ARMISD::VCLTZ:
3578 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3579 default:
3580 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3581 }
3582 } else {
3583 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3584 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003585
3586 if (Invert)
3587 Result = DAG.getNOT(dl, Result, VT);
3588
3589 return Result;
3590}
3591
Bob Wilsond3c42842010-06-14 22:19:57 +00003592/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3593/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003594/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003595static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3596 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003597 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003598 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003599
Bob Wilson827b2102010-06-15 19:05:35 +00003600 // SplatBitSize is set to the smallest size that splats the vector, so a
3601 // zero vector will always have SplatBitSize == 8. However, NEON modified
3602 // immediate instructions others than VMOV do not support the 8-bit encoding
3603 // of a zero vector, and the default encoding of zero is supposed to be the
3604 // 32-bit version.
3605 if (SplatBits == 0)
3606 SplatBitSize = 32;
3607
Bob Wilson5bafff32009-06-22 23:27:02 +00003608 switch (SplatBitSize) {
3609 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003610 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003611 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003612 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003613 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003614 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003615 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003616 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003617 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003618
3619 case 16:
3620 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003621 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003622 if ((SplatBits & ~0xff) == 0) {
3623 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003624 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003625 Imm = SplatBits;
3626 break;
3627 }
3628 if ((SplatBits & ~0xff00) == 0) {
3629 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003630 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003631 Imm = SplatBits >> 8;
3632 break;
3633 }
3634 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003635
3636 case 32:
3637 // NEON's 32-bit VMOV supports splat values where:
3638 // * only one byte is nonzero, or
3639 // * the least significant byte is 0xff and the second byte is nonzero, or
3640 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003641 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003642 if ((SplatBits & ~0xff) == 0) {
3643 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003644 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003645 Imm = SplatBits;
3646 break;
3647 }
3648 if ((SplatBits & ~0xff00) == 0) {
3649 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003650 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003651 Imm = SplatBits >> 8;
3652 break;
3653 }
3654 if ((SplatBits & ~0xff0000) == 0) {
3655 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003656 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003657 Imm = SplatBits >> 16;
3658 break;
3659 }
3660 if ((SplatBits & ~0xff000000) == 0) {
3661 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003662 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003663 Imm = SplatBits >> 24;
3664 break;
3665 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003666
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003667 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3668 if (type == OtherModImm) return SDValue();
3669
Bob Wilson5bafff32009-06-22 23:27:02 +00003670 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003671 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3672 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003673 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003674 Imm = SplatBits >> 8;
3675 SplatBits |= 0xff;
3676 break;
3677 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003678
3679 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003680 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3681 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003682 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003683 Imm = SplatBits >> 16;
3684 SplatBits |= 0xffff;
3685 break;
3686 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003687
3688 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3689 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3690 // VMOV.I32. A (very) minor optimization would be to replicate the value
3691 // and fall through here to test for a valid 64-bit splat. But, then the
3692 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003693 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003694
3695 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003696 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003697 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003698 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003699 uint64_t BitMask = 0xff;
3700 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701 unsigned ImmMask = 1;
3702 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003703 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003704 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003705 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003706 Imm |= ImmMask;
3707 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003708 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003709 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003710 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003711 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003712 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003713 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003714 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003715 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003716 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003717 break;
3718 }
3719
Bob Wilson1a913ed2010-06-11 21:34:50 +00003720 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003721 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003722 return SDValue();
3723 }
3724
Bob Wilsoncba270d2010-07-13 21:16:48 +00003725 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3726 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003727}
3728
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003729static bool isVEXTMask(const SmallVectorImpl<int> &M, EVT VT,
3730 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003731 unsigned NumElts = VT.getVectorNumElements();
3732 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003733
3734 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3735 if (M[0] < 0)
3736 return false;
3737
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003738 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003739
3740 // If this is a VEXT shuffle, the immediate value is the index of the first
3741 // element. The other shuffle indices must be the successive elements after
3742 // the first one.
3743 unsigned ExpectedElt = Imm;
3744 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003745 // Increment the expected index. If it wraps around, it may still be
3746 // a VEXT but the source vectors must be swapped.
3747 ExpectedElt += 1;
3748 if (ExpectedElt == NumElts * 2) {
3749 ExpectedElt = 0;
3750 ReverseVEXT = true;
3751 }
3752
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003753 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003754 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003755 return false;
3756 }
3757
3758 // Adjust the index value if the source operands will be swapped.
3759 if (ReverseVEXT)
3760 Imm -= NumElts;
3761
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003762 return true;
3763}
3764
Bob Wilson8bb9e482009-07-26 00:39:34 +00003765/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3766/// instruction with the specified blocksize. (The order of the elements
3767/// within each block of the vector is reversed.)
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003768static bool isVREVMask(const SmallVectorImpl<int> &M, EVT VT,
3769 unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003770 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3771 "Only possible block sizes for VREV are: 16, 32, 64");
3772
Bob Wilson8bb9e482009-07-26 00:39:34 +00003773 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003774 if (EltSz == 64)
3775 return false;
3776
3777 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003778 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003779 // If the first shuffle index is UNDEF, be optimistic.
3780 if (M[0] < 0)
3781 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003782
3783 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3784 return false;
3785
3786 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003787 if (M[i] < 0) continue; // ignore UNDEF indices
3788 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003789 return false;
3790 }
3791
3792 return true;
3793}
3794
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003795static bool isVTBLMask(const SmallVectorImpl<int> &M, EVT VT) {
3796 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3797 // range, then 0 is placed into the resulting vector. So pretty much any mask
3798 // of 8 elements can work here.
3799 return VT == MVT::v8i8 && M.size() == 8;
3800}
3801
Bob Wilsonc692cb72009-08-21 20:54:19 +00003802static bool isVTRNMask(const SmallVectorImpl<int> &M, EVT VT,
3803 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003804 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3805 if (EltSz == 64)
3806 return false;
3807
Bob Wilsonc692cb72009-08-21 20:54:19 +00003808 unsigned NumElts = VT.getVectorNumElements();
3809 WhichResult = (M[0] == 0 ? 0 : 1);
3810 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003811 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3812 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003813 return false;
3814 }
3815 return true;
3816}
3817
Bob Wilson324f4f12009-12-03 06:40:55 +00003818/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3819/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3820/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
3821static bool isVTRN_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3822 unsigned &WhichResult) {
3823 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3824 if (EltSz == 64)
3825 return false;
3826
3827 unsigned NumElts = VT.getVectorNumElements();
3828 WhichResult = (M[0] == 0 ? 0 : 1);
3829 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003830 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3831 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003832 return false;
3833 }
3834 return true;
3835}
3836
Bob Wilsonc692cb72009-08-21 20:54:19 +00003837static bool isVUZPMask(const SmallVectorImpl<int> &M, EVT VT,
3838 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003839 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3840 if (EltSz == 64)
3841 return false;
3842
Bob Wilsonc692cb72009-08-21 20:54:19 +00003843 unsigned NumElts = VT.getVectorNumElements();
3844 WhichResult = (M[0] == 0 ? 0 : 1);
3845 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003846 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003847 if ((unsigned) M[i] != 2 * i + WhichResult)
3848 return false;
3849 }
3850
3851 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003852 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003853 return false;
3854
3855 return true;
3856}
3857
Bob Wilson324f4f12009-12-03 06:40:55 +00003858/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3859/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3860/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
3861static bool isVUZP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3862 unsigned &WhichResult) {
3863 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3864 if (EltSz == 64)
3865 return false;
3866
3867 unsigned Half = VT.getVectorNumElements() / 2;
3868 WhichResult = (M[0] == 0 ? 0 : 1);
3869 for (unsigned j = 0; j != 2; ++j) {
3870 unsigned Idx = WhichResult;
3871 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003872 int MIdx = M[i + j * Half];
3873 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003874 return false;
3875 Idx += 2;
3876 }
3877 }
3878
3879 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3880 if (VT.is64BitVector() && EltSz == 32)
3881 return false;
3882
3883 return true;
3884}
3885
Bob Wilsonc692cb72009-08-21 20:54:19 +00003886static bool isVZIPMask(const SmallVectorImpl<int> &M, EVT VT,
3887 unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003888 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3889 if (EltSz == 64)
3890 return false;
3891
Bob Wilsonc692cb72009-08-21 20:54:19 +00003892 unsigned NumElts = VT.getVectorNumElements();
3893 WhichResult = (M[0] == 0 ? 0 : 1);
3894 unsigned Idx = WhichResult * NumElts / 2;
3895 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003896 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3897 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003898 return false;
3899 Idx += 1;
3900 }
3901
3902 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003903 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003904 return false;
3905
3906 return true;
3907}
3908
Bob Wilson324f4f12009-12-03 06:40:55 +00003909/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
3910/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3911/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
3912static bool isVZIP_v_undef_Mask(const SmallVectorImpl<int> &M, EVT VT,
3913 unsigned &WhichResult) {
3914 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3915 if (EltSz == 64)
3916 return false;
3917
3918 unsigned NumElts = VT.getVectorNumElements();
3919 WhichResult = (M[0] == 0 ? 0 : 1);
3920 unsigned Idx = WhichResult * NumElts / 2;
3921 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003922 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3923 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00003924 return false;
3925 Idx += 1;
3926 }
3927
3928 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3929 if (VT.is64BitVector() && EltSz == 32)
3930 return false;
3931
3932 return true;
3933}
3934
Dale Johannesenf630c712010-07-29 20:10:08 +00003935// If N is an integer constant that can be moved into a register in one
3936// instruction, return an SDValue of such a constant (will become a MOV
3937// instruction). Otherwise return null.
3938static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
3939 const ARMSubtarget *ST, DebugLoc dl) {
3940 uint64_t Val;
3941 if (!isa<ConstantSDNode>(N))
3942 return SDValue();
3943 Val = cast<ConstantSDNode>(N)->getZExtValue();
3944
3945 if (ST->isThumb1Only()) {
3946 if (Val <= 255 || ~Val <= 255)
3947 return DAG.getConstant(Val, MVT::i32);
3948 } else {
3949 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
3950 return DAG.getConstant(Val, MVT::i32);
3951 }
3952 return SDValue();
3953}
3954
Bob Wilson5bafff32009-06-22 23:27:02 +00003955// If this is a case we can't handle, return null and let the default
3956// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00003957SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
3958 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00003959 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00003960 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003961 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003962
3963 APInt SplatBits, SplatUndef;
3964 unsigned SplatBitSize;
3965 bool HasAnyUndefs;
3966 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003967 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00003968 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003969 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00003970 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00003971 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003972 DAG, VmovVT, VT.is128BitVector(),
3973 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003974 if (Val.getNode()) {
3975 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003976 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00003977 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003978
3979 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00003980 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003981 Val = isNEONModifiedImm(NegatedImm,
3982 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003983 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003984 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003985 if (Val.getNode()) {
3986 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003987 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003988 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00003989 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00003990 }
3991
Bob Wilsonbe751cf2010-05-22 00:23:12 +00003992 // Scan through the operands to see if only one value is used.
3993 unsigned NumElts = VT.getVectorNumElements();
3994 bool isOnlyLowElement = true;
3995 bool usesOnlyOneValue = true;
3996 bool isConstant = true;
3997 SDValue Value;
3998 for (unsigned i = 0; i < NumElts; ++i) {
3999 SDValue V = Op.getOperand(i);
4000 if (V.getOpcode() == ISD::UNDEF)
4001 continue;
4002 if (i > 0)
4003 isOnlyLowElement = false;
4004 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4005 isConstant = false;
4006
4007 if (!Value.getNode())
4008 Value = V;
4009 else if (V != Value)
4010 usesOnlyOneValue = false;
4011 }
4012
4013 if (!Value.getNode())
4014 return DAG.getUNDEF(VT);
4015
4016 if (isOnlyLowElement)
4017 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4018
Dale Johannesenf630c712010-07-29 20:10:08 +00004019 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4020
Dale Johannesen575cd142010-10-19 20:00:17 +00004021 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4022 // i32 and try again.
4023 if (usesOnlyOneValue && EltSize <= 32) {
4024 if (!isConstant)
4025 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4026 if (VT.getVectorElementType().isFloatingPoint()) {
4027 SmallVector<SDValue, 8> Ops;
4028 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004029 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004030 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004031 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4032 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004033 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4034 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004035 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004036 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004037 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4038 if (Val.getNode())
4039 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004040 }
4041
4042 // If all elements are constants and the case above didn't get hit, fall back
4043 // to the default expansion, which will generate a load from the constant
4044 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004045 if (isConstant)
4046 return SDValue();
4047
Bob Wilson11a1dff2011-01-07 21:37:30 +00004048 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4049 if (NumElts >= 4) {
4050 SDValue shuffle = ReconstructShuffle(Op, DAG);
4051 if (shuffle != SDValue())
4052 return shuffle;
4053 }
4054
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004055 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004056 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4057 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004058 if (EltSize >= 32) {
4059 // Do the expansion with floating-point types, since that is what the VFP
4060 // registers are defined to use, and since i64 is not legal.
4061 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4062 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004063 SmallVector<SDValue, 8> Ops;
4064 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004065 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004066 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004067 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004068 }
4069
4070 return SDValue();
4071}
4072
Bob Wilson11a1dff2011-01-07 21:37:30 +00004073// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004074// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004075SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4076 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004077 DebugLoc dl = Op.getDebugLoc();
4078 EVT VT = Op.getValueType();
4079 unsigned NumElts = VT.getVectorNumElements();
4080
4081 SmallVector<SDValue, 2> SourceVecs;
4082 SmallVector<unsigned, 2> MinElts;
4083 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004084
Bob Wilson11a1dff2011-01-07 21:37:30 +00004085 for (unsigned i = 0; i < NumElts; ++i) {
4086 SDValue V = Op.getOperand(i);
4087 if (V.getOpcode() == ISD::UNDEF)
4088 continue;
4089 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4090 // A shuffle can only come from building a vector from various
4091 // elements of other vectors.
4092 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004093 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4094 VT.getVectorElementType()) {
4095 // This code doesn't know how to handle shuffles where the vector
4096 // element types do not match (this happens because type legalization
4097 // promotes the return type of EXTRACT_VECTOR_ELT).
4098 // FIXME: It might be appropriate to extend this code to handle
4099 // mismatched types.
4100 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004101 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004102
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103 // Record this extraction against the appropriate vector if possible...
4104 SDValue SourceVec = V.getOperand(0);
4105 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4106 bool FoundSource = false;
4107 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4108 if (SourceVecs[j] == SourceVec) {
4109 if (MinElts[j] > EltNo)
4110 MinElts[j] = EltNo;
4111 if (MaxElts[j] < EltNo)
4112 MaxElts[j] = EltNo;
4113 FoundSource = true;
4114 break;
4115 }
4116 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004117
Bob Wilson11a1dff2011-01-07 21:37:30 +00004118 // Or record a new source if not...
4119 if (!FoundSource) {
4120 SourceVecs.push_back(SourceVec);
4121 MinElts.push_back(EltNo);
4122 MaxElts.push_back(EltNo);
4123 }
4124 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004125
Bob Wilson11a1dff2011-01-07 21:37:30 +00004126 // Currently only do something sane when at most two source vectors
4127 // involved.
4128 if (SourceVecs.size() > 2)
4129 return SDValue();
4130
4131 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4132 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004133
Bob Wilson11a1dff2011-01-07 21:37:30 +00004134 // This loop extracts the usage patterns of the source vectors
4135 // and prepares appropriate SDValues for a shuffle if possible.
4136 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4137 if (SourceVecs[i].getValueType() == VT) {
4138 // No VEXT necessary
4139 ShuffleSrcs[i] = SourceVecs[i];
4140 VEXTOffsets[i] = 0;
4141 continue;
4142 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4143 // It probably isn't worth padding out a smaller vector just to
4144 // break it down again in a shuffle.
4145 return SDValue();
4146 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004147
Bob Wilson11a1dff2011-01-07 21:37:30 +00004148 // Since only 64-bit and 128-bit vectors are legal on ARM and
4149 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004150 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4151 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004152
Bob Wilson11a1dff2011-01-07 21:37:30 +00004153 if (MaxElts[i] - MinElts[i] >= NumElts) {
4154 // Span too large for a VEXT to cope
4155 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004156 }
4157
Bob Wilson11a1dff2011-01-07 21:37:30 +00004158 if (MinElts[i] >= NumElts) {
4159 // The extraction can just take the second half
4160 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004161 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4162 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004163 DAG.getIntPtrConstant(NumElts));
4164 } else if (MaxElts[i] < NumElts) {
4165 // The extraction can just take the first half
4166 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004167 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4168 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004169 DAG.getIntPtrConstant(0));
4170 } else {
4171 // An actual VEXT is needed
4172 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004173 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4174 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004175 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004176 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4177 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004178 DAG.getIntPtrConstant(NumElts));
4179 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4180 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4181 }
4182 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004183
Bob Wilson11a1dff2011-01-07 21:37:30 +00004184 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004185
Bob Wilson11a1dff2011-01-07 21:37:30 +00004186 for (unsigned i = 0; i < NumElts; ++i) {
4187 SDValue Entry = Op.getOperand(i);
4188 if (Entry.getOpcode() == ISD::UNDEF) {
4189 Mask.push_back(-1);
4190 continue;
4191 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004192
Bob Wilson11a1dff2011-01-07 21:37:30 +00004193 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004194 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4195 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004196 if (ExtractVec == SourceVecs[0]) {
4197 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4198 } else {
4199 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4200 }
4201 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004202
Bob Wilson11a1dff2011-01-07 21:37:30 +00004203 // Final check before we try to produce nonsense...
4204 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004205 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4206 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004207
Bob Wilson11a1dff2011-01-07 21:37:30 +00004208 return SDValue();
4209}
4210
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004211/// isShuffleMaskLegal - Targets can use this to indicate that they only
4212/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4213/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4214/// are assumed to be legal.
4215bool
4216ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4217 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004218 if (VT.getVectorNumElements() == 4 &&
4219 (VT.is128BitVector() || VT.is64BitVector())) {
4220 unsigned PFIndexes[4];
4221 for (unsigned i = 0; i != 4; ++i) {
4222 if (M[i] < 0)
4223 PFIndexes[i] = 8;
4224 else
4225 PFIndexes[i] = M[i];
4226 }
4227
4228 // Compute the index in the perfect shuffle table.
4229 unsigned PFTableIndex =
4230 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4231 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4232 unsigned Cost = (PFEntry >> 30);
4233
4234 if (Cost <= 4)
4235 return true;
4236 }
4237
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004238 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004239 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004240
Bob Wilson53dd2452010-06-07 23:53:38 +00004241 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4242 return (EltSize >= 32 ||
4243 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004244 isVREVMask(M, VT, 64) ||
4245 isVREVMask(M, VT, 32) ||
4246 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004247 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004248 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004249 isVTRNMask(M, VT, WhichResult) ||
4250 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004251 isVZIPMask(M, VT, WhichResult) ||
4252 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4253 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4254 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004255}
4256
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004257/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4258/// the specified operations to build the shuffle.
4259static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4260 SDValue RHS, SelectionDAG &DAG,
4261 DebugLoc dl) {
4262 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4263 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4264 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4265
4266 enum {
4267 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4268 OP_VREV,
4269 OP_VDUP0,
4270 OP_VDUP1,
4271 OP_VDUP2,
4272 OP_VDUP3,
4273 OP_VEXT1,
4274 OP_VEXT2,
4275 OP_VEXT3,
4276 OP_VUZPL, // VUZP, left result
4277 OP_VUZPR, // VUZP, right result
4278 OP_VZIPL, // VZIP, left result
4279 OP_VZIPR, // VZIP, right result
4280 OP_VTRNL, // VTRN, left result
4281 OP_VTRNR // VTRN, right result
4282 };
4283
4284 if (OpNum == OP_COPY) {
4285 if (LHSID == (1*9+2)*9+3) return LHS;
4286 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4287 return RHS;
4288 }
4289
4290 SDValue OpLHS, OpRHS;
4291 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4292 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4293 EVT VT = OpLHS.getValueType();
4294
4295 switch (OpNum) {
4296 default: llvm_unreachable("Unknown shuffle opcode!");
4297 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004298 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004299 if (VT.getVectorElementType() == MVT::i32 ||
4300 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004301 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4302 // vrev <4 x i16> -> VREV32
4303 if (VT.getVectorElementType() == MVT::i16)
4304 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4305 // vrev <4 x i8> -> VREV16
4306 assert(VT.getVectorElementType() == MVT::i8);
4307 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004308 case OP_VDUP0:
4309 case OP_VDUP1:
4310 case OP_VDUP2:
4311 case OP_VDUP3:
4312 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004313 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004314 case OP_VEXT1:
4315 case OP_VEXT2:
4316 case OP_VEXT3:
4317 return DAG.getNode(ARMISD::VEXT, dl, VT,
4318 OpLHS, OpRHS,
4319 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4320 case OP_VUZPL:
4321 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004322 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004323 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4324 case OP_VZIPL:
4325 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004326 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004327 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4328 case OP_VTRNL:
4329 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004330 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4331 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004332 }
4333}
4334
Bill Wendling69a05a72011-03-14 23:02:38 +00004335static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
4336 SmallVectorImpl<int> &ShuffleMask,
4337 SelectionDAG &DAG) {
4338 // Check to see if we can use the VTBL instruction.
4339 SDValue V1 = Op.getOperand(0);
4340 SDValue V2 = Op.getOperand(1);
4341 DebugLoc DL = Op.getDebugLoc();
4342
4343 SmallVector<SDValue, 8> VTBLMask;
4344 for (SmallVectorImpl<int>::iterator
4345 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4346 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4347
4348 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4349 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4350 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4351 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004352
Owen Anderson76706012011-04-05 21:48:57 +00004353 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004354 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4355 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004356}
4357
Bob Wilson5bafff32009-06-22 23:27:02 +00004358static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004359 SDValue V1 = Op.getOperand(0);
4360 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004361 DebugLoc dl = Op.getDebugLoc();
4362 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004363 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004364 SmallVector<int, 8> ShuffleMask;
Bob Wilsond8e17572009-08-12 22:31:50 +00004365
Bob Wilson28865062009-08-13 02:13:04 +00004366 // Convert shuffles that are directly supported on NEON to target-specific
4367 // DAG nodes, instead of keeping them as shuffles and matching them again
4368 // during code selection. This is more efficient and avoids the possibility
4369 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004370 // FIXME: floating-point vectors should be canonicalized to integer vectors
4371 // of the same time so that they get CSEd properly.
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004372 SVN->getMask(ShuffleMask);
4373
Bob Wilson53dd2452010-06-07 23:53:38 +00004374 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4375 if (EltSize <= 32) {
4376 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4377 int Lane = SVN->getSplatIndex();
4378 // If this is undef splat, generate it via "just" vdup, if possible.
4379 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004380
Dan Gohman65fd6562011-11-03 21:49:52 +00004381 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004382 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4383 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4384 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004385 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4386 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4387 // reaches it).
4388 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4389 !isa<ConstantSDNode>(V1.getOperand(0))) {
4390 bool IsScalarToVector = true;
4391 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4392 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4393 IsScalarToVector = false;
4394 break;
4395 }
4396 if (IsScalarToVector)
4397 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4398 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004399 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4400 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004401 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004402
4403 bool ReverseVEXT;
4404 unsigned Imm;
4405 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4406 if (ReverseVEXT)
4407 std::swap(V1, V2);
4408 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4409 DAG.getConstant(Imm, MVT::i32));
4410 }
4411
4412 if (isVREVMask(ShuffleMask, VT, 64))
4413 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4414 if (isVREVMask(ShuffleMask, VT, 32))
4415 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4416 if (isVREVMask(ShuffleMask, VT, 16))
4417 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4418
4419 // Check for Neon shuffles that modify both input vectors in place.
4420 // If both results are used, i.e., if there are two shuffles with the same
4421 // source operands and with masks corresponding to both results of one of
4422 // these operations, DAG memoization will ensure that a single node is
4423 // used for both shuffles.
4424 unsigned WhichResult;
4425 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4426 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4427 V1, V2).getValue(WhichResult);
4428 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4429 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4430 V1, V2).getValue(WhichResult);
4431 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4432 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4433 V1, V2).getValue(WhichResult);
4434
4435 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4436 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4437 V1, V1).getValue(WhichResult);
4438 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4439 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4440 V1, V1).getValue(WhichResult);
4441 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4442 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4443 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004444 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004445
Bob Wilsonc692cb72009-08-21 20:54:19 +00004446 // If the shuffle is not directly supported and it has 4 elements, use
4447 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004448 unsigned NumElts = VT.getVectorNumElements();
4449 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004450 unsigned PFIndexes[4];
4451 for (unsigned i = 0; i != 4; ++i) {
4452 if (ShuffleMask[i] < 0)
4453 PFIndexes[i] = 8;
4454 else
4455 PFIndexes[i] = ShuffleMask[i];
4456 }
4457
4458 // Compute the index in the perfect shuffle table.
4459 unsigned PFTableIndex =
4460 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004461 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4462 unsigned Cost = (PFEntry >> 30);
4463
4464 if (Cost <= 4)
4465 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4466 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004467
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004468 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004469 if (EltSize >= 32) {
4470 // Do the expansion with floating-point types, since that is what the VFP
4471 // registers are defined to use, and since i64 is not legal.
4472 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4473 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004474 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4475 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004476 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004477 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004478 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004479 Ops.push_back(DAG.getUNDEF(EltVT));
4480 else
4481 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4482 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4483 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4484 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004485 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004486 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004487 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004488 }
4489
Bill Wendling69a05a72011-03-14 23:02:38 +00004490 if (VT == MVT::v8i8) {
4491 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4492 if (NewOp.getNode())
4493 return NewOp;
4494 }
4495
Bob Wilson22cac0d2009-08-14 05:16:33 +00004496 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004497}
4498
Eli Friedman5c89cb82011-10-24 23:08:52 +00004499static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4500 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4501 SDValue Lane = Op.getOperand(2);
4502 if (!isa<ConstantSDNode>(Lane))
4503 return SDValue();
4504
4505 return Op;
4506}
4507
Bob Wilson5bafff32009-06-22 23:27:02 +00004508static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004509 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004510 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004511 if (!isa<ConstantSDNode>(Lane))
4512 return SDValue();
4513
4514 SDValue Vec = Op.getOperand(0);
4515 if (Op.getValueType() == MVT::i32 &&
4516 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4517 DebugLoc dl = Op.getDebugLoc();
4518 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4519 }
4520
4521 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004522}
4523
Bob Wilsona6d65862009-08-03 20:36:38 +00004524static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4525 // The only time a CONCAT_VECTORS operation can have legal types is when
4526 // two 64-bit vectors are concatenated to a 128-bit vector.
4527 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4528 "unexpected CONCAT_VECTORS");
4529 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004530 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004531 SDValue Op0 = Op.getOperand(0);
4532 SDValue Op1 = Op.getOperand(1);
4533 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004534 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004535 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004536 DAG.getIntPtrConstant(0));
4537 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004539 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004540 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004541 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004542}
4543
Bob Wilson626613d2010-11-23 19:38:38 +00004544/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4545/// element has been zero/sign-extended, depending on the isSigned parameter,
4546/// from an integer type half its size.
4547static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4548 bool isSigned) {
4549 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4550 EVT VT = N->getValueType(0);
4551 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4552 SDNode *BVN = N->getOperand(0).getNode();
4553 if (BVN->getValueType(0) != MVT::v4i32 ||
4554 BVN->getOpcode() != ISD::BUILD_VECTOR)
4555 return false;
4556 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4557 unsigned HiElt = 1 - LoElt;
4558 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4559 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4560 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4561 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4562 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4563 return false;
4564 if (isSigned) {
4565 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4566 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4567 return true;
4568 } else {
4569 if (Hi0->isNullValue() && Hi1->isNullValue())
4570 return true;
4571 }
4572 return false;
4573 }
4574
4575 if (N->getOpcode() != ISD::BUILD_VECTOR)
4576 return false;
4577
4578 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4579 SDNode *Elt = N->getOperand(i).getNode();
4580 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4581 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4582 unsigned HalfSize = EltSize / 2;
4583 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004584 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004585 return false;
4586 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004587 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004588 return false;
4589 }
4590 continue;
4591 }
4592 return false;
4593 }
4594
4595 return true;
4596}
4597
4598/// isSignExtended - Check if a node is a vector value that is sign-extended
4599/// or a constant BUILD_VECTOR with sign-extended elements.
4600static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4601 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4602 return true;
4603 if (isExtendedBUILD_VECTOR(N, DAG, true))
4604 return true;
4605 return false;
4606}
4607
4608/// isZeroExtended - Check if a node is a vector value that is zero-extended
4609/// or a constant BUILD_VECTOR with zero-extended elements.
4610static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4611 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4612 return true;
4613 if (isExtendedBUILD_VECTOR(N, DAG, false))
4614 return true;
4615 return false;
4616}
4617
4618/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4619/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004620static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4621 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4622 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004623 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4624 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4625 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004626 LD->isNonTemporal(), LD->isInvariant(),
4627 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004628 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4629 // have been legalized as a BITCAST from v4i32.
4630 if (N->getOpcode() == ISD::BITCAST) {
4631 SDNode *BVN = N->getOperand(0).getNode();
4632 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4633 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4634 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4635 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4636 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4637 }
4638 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4639 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4640 EVT VT = N->getValueType(0);
4641 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4642 unsigned NumElts = VT.getVectorNumElements();
4643 MVT TruncVT = MVT::getIntegerVT(EltSize);
4644 SmallVector<SDValue, 8> Ops;
4645 for (unsigned i = 0; i != NumElts; ++i) {
4646 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4647 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004648 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004649 }
4650 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4651 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004652}
4653
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004654static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4655 unsigned Opcode = N->getOpcode();
4656 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4657 SDNode *N0 = N->getOperand(0).getNode();
4658 SDNode *N1 = N->getOperand(1).getNode();
4659 return N0->hasOneUse() && N1->hasOneUse() &&
4660 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4661 }
4662 return false;
4663}
4664
4665static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4666 unsigned Opcode = N->getOpcode();
4667 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4668 SDNode *N0 = N->getOperand(0).getNode();
4669 SDNode *N1 = N->getOperand(1).getNode();
4670 return N0->hasOneUse() && N1->hasOneUse() &&
4671 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4672 }
4673 return false;
4674}
4675
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004676static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4677 // Multiplications are only custom-lowered for 128-bit vectors so that
4678 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4679 EVT VT = Op.getValueType();
4680 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4681 SDNode *N0 = Op.getOperand(0).getNode();
4682 SDNode *N1 = Op.getOperand(1).getNode();
4683 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004684 bool isMLA = false;
4685 bool isN0SExt = isSignExtended(N0, DAG);
4686 bool isN1SExt = isSignExtended(N1, DAG);
4687 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004688 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004689 else {
4690 bool isN0ZExt = isZeroExtended(N0, DAG);
4691 bool isN1ZExt = isZeroExtended(N1, DAG);
4692 if (isN0ZExt && isN1ZExt)
4693 NewOpc = ARMISD::VMULLu;
4694 else if (isN1SExt || isN1ZExt) {
4695 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4696 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4697 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4698 NewOpc = ARMISD::VMULLs;
4699 isMLA = true;
4700 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4701 NewOpc = ARMISD::VMULLu;
4702 isMLA = true;
4703 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4704 std::swap(N0, N1);
4705 NewOpc = ARMISD::VMULLu;
4706 isMLA = true;
4707 }
4708 }
4709
4710 if (!NewOpc) {
4711 if (VT == MVT::v2i64)
4712 // Fall through to expand this. It is not legal.
4713 return SDValue();
4714 else
4715 // Other vector multiplications are legal.
4716 return Op;
4717 }
4718 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004719
4720 // Legalize to a VMULL instruction.
4721 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004722 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004723 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004724 if (!isMLA) {
4725 Op0 = SkipExtension(N0, DAG);
4726 assert(Op0.getValueType().is64BitVector() &&
4727 Op1.getValueType().is64BitVector() &&
4728 "unexpected types for extended operands to VMULL");
4729 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4730 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004731
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004732 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4733 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4734 // vmull q0, d4, d6
4735 // vmlal q0, d5, d6
4736 // is faster than
4737 // vaddl q0, d4, d5
4738 // vmovl q1, d6
4739 // vmul q0, q0, q1
4740 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4741 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4742 EVT Op1VT = Op1.getValueType();
4743 return DAG.getNode(N0->getOpcode(), DL, VT,
4744 DAG.getNode(NewOpc, DL, VT,
4745 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4746 DAG.getNode(NewOpc, DL, VT,
4747 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004748}
4749
Owen Anderson76706012011-04-05 21:48:57 +00004750static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004751LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4752 // Convert to float
4753 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4754 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4755 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4756 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4757 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4758 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4759 // Get reciprocal estimate.
4760 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004761 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004762 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4763 // Because char has a smaller range than uchar, we can actually get away
4764 // without any newton steps. This requires that we use a weird bias
4765 // of 0xb000, however (again, this has been exhaustively tested).
4766 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4767 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4768 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4769 Y = DAG.getConstant(0xb000, MVT::i32);
4770 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4771 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4772 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4773 // Convert back to short.
4774 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4775 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4776 return X;
4777}
4778
Owen Anderson76706012011-04-05 21:48:57 +00004779static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004780LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4781 SDValue N2;
4782 // Convert to float.
4783 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4784 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4785 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4786 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4787 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4788 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004789
Nate Begeman7973f352011-02-11 20:53:29 +00004790 // Use reciprocal estimate and one refinement step.
4791 // float4 recip = vrecpeq_f32(yf);
4792 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004793 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004794 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004795 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004796 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4797 N1, N2);
4798 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4799 // Because short has a smaller range than ushort, we can actually get away
4800 // with only a single newton step. This requires that we use a weird bias
4801 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004802 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004803 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4804 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004805 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004806 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4807 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4808 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4809 // Convert back to integer and return.
4810 // return vmovn_s32(vcvt_s32_f32(result));
4811 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4812 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4813 return N0;
4814}
4815
4816static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4817 EVT VT = Op.getValueType();
4818 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4819 "unexpected type for custom-lowering ISD::SDIV");
4820
4821 DebugLoc dl = Op.getDebugLoc();
4822 SDValue N0 = Op.getOperand(0);
4823 SDValue N1 = Op.getOperand(1);
4824 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004825
Nate Begeman7973f352011-02-11 20:53:29 +00004826 if (VT == MVT::v8i8) {
4827 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4828 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004829
Nate Begeman7973f352011-02-11 20:53:29 +00004830 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4831 DAG.getIntPtrConstant(4));
4832 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004833 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004834 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4835 DAG.getIntPtrConstant(0));
4836 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4837 DAG.getIntPtrConstant(0));
4838
4839 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4840 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4841
4842 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4843 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004844
Nate Begeman7973f352011-02-11 20:53:29 +00004845 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4846 return N0;
4847 }
4848 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4849}
4850
4851static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4852 EVT VT = Op.getValueType();
4853 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4854 "unexpected type for custom-lowering ISD::UDIV");
4855
4856 DebugLoc dl = Op.getDebugLoc();
4857 SDValue N0 = Op.getOperand(0);
4858 SDValue N1 = Op.getOperand(1);
4859 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004860
Nate Begeman7973f352011-02-11 20:53:29 +00004861 if (VT == MVT::v8i8) {
4862 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4863 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004864
Nate Begeman7973f352011-02-11 20:53:29 +00004865 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4866 DAG.getIntPtrConstant(4));
4867 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004868 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004869 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4870 DAG.getIntPtrConstant(0));
4871 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4872 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004873
Nate Begeman7973f352011-02-11 20:53:29 +00004874 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4875 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004876
Nate Begeman7973f352011-02-11 20:53:29 +00004877 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4878 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004879
4880 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004881 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4882 N0);
4883 return N0;
4884 }
Owen Anderson76706012011-04-05 21:48:57 +00004885
Nate Begeman7973f352011-02-11 20:53:29 +00004886 // v4i16 sdiv ... Convert to float.
4887 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4888 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4889 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4890 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4891 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004892 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004893
4894 // Use reciprocal estimate and two refinement steps.
4895 // float4 recip = vrecpeq_f32(yf);
4896 // recip *= vrecpsq_f32(yf, recip);
4897 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004898 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004899 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00004900 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004901 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004902 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004903 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00004904 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004905 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004906 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00004907 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4908 // Simply multiplying by the reciprocal estimate can leave us a few ulps
4909 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
4910 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004911 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00004912 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4913 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
4914 N1 = DAG.getConstant(2, MVT::i32);
4915 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4916 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4917 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4918 // Convert back to integer and return.
4919 // return vmovn_u32(vcvt_s32_f32(result));
4920 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4921 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4922 return N0;
4923}
4924
Evan Cheng342e3162011-08-30 01:34:54 +00004925static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
4926 EVT VT = Op.getNode()->getValueType(0);
4927 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4928
4929 unsigned Opc;
4930 bool ExtraOp = false;
4931 switch (Op.getOpcode()) {
4932 default: assert(0 && "Invalid code");
4933 case ISD::ADDC: Opc = ARMISD::ADDC; break;
4934 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
4935 case ISD::SUBC: Opc = ARMISD::SUBC; break;
4936 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
4937 }
4938
4939 if (!ExtraOp)
4940 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4941 Op.getOperand(1));
4942 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
4943 Op.getOperand(1), Op.getOperand(2));
4944}
4945
Eli Friedman74bf18c2011-09-15 22:26:18 +00004946static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00004947 // Monotonic load/store is legal for all targets
4948 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
4949 return Op;
4950
4951 // Aquire/Release load/store is not legal for targets without a
4952 // dmb or equivalent available.
4953 return SDValue();
4954}
4955
4956
Eli Friedman2bdffe42011-08-31 00:31:29 +00004957static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00004958ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
4959 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00004960 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00004961 assert (Node->getValueType(0) == MVT::i64 &&
4962 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00004963
Eli Friedman4d3f3292011-08-31 17:52:22 +00004964 SmallVector<SDValue, 6> Ops;
4965 Ops.push_back(Node->getOperand(0)); // Chain
4966 Ops.push_back(Node->getOperand(1)); // Ptr
4967 // Low part of Val1
4968 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4969 Node->getOperand(2), DAG.getIntPtrConstant(0)));
4970 // High part of Val1
4971 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4972 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00004973 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00004974 // High part of Val1
4975 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4976 Node->getOperand(3), DAG.getIntPtrConstant(0)));
4977 // High part of Val2
4978 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
4979 Node->getOperand(3), DAG.getIntPtrConstant(1)));
4980 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00004981 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4982 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00004983 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00004984 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00004985 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00004986 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
4987 Results.push_back(Result.getValue(2));
4988}
4989
Dan Gohmand858e902010-04-17 15:26:15 +00004990SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00004991 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004992 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00004993 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00004994 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00004995 case ISD::GlobalAddress:
4996 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
4997 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00004998 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00004999 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005000 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5001 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005002 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005003 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005004 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005005 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005006 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005007 case ISD::SINT_TO_FP:
5008 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5009 case ISD::FP_TO_SINT:
5010 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005011 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005012 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005013 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005014 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005015 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005016 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbache4ad3872010-10-19 23:27:08 +00005017 case ISD::EH_SJLJ_DISPATCHSETUP: return LowerEH_SJLJ_DISPATCHSETUP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005018 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5019 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005020 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005021 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005022 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005023 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005024 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005025 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005026 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005027 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005028 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Dale Johannesenf630c712010-07-29 20:10:08 +00005029 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005030 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005031 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005032 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005033 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005034 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005035 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005036 case ISD::SDIV: return LowerSDIV(Op, DAG);
5037 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005038 case ISD::ADDC:
5039 case ISD::ADDE:
5040 case ISD::SUBC:
5041 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005042 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005043 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005044 }
Dan Gohman475871a2008-07-27 21:46:04 +00005045 return SDValue();
Evan Chenga8e29892007-01-19 07:51:42 +00005046}
5047
Duncan Sands1607f052008-12-01 11:39:25 +00005048/// ReplaceNodeResults - Replace the results of node with an illegal result
5049/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005050void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5051 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005052 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005053 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005054 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005055 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005056 llvm_unreachable("Don't know how to custom expand this!");
Bob Wilson164cd8b2010-04-14 20:45:23 +00005057 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005058 case ISD::BITCAST:
5059 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005060 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005061 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005062 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005063 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005064 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005065 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005066 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005067 return;
5068 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005069 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005070 return;
5071 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005072 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005073 return;
5074 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005075 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005076 return;
5077 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005078 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005079 return;
5080 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005081 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005082 return;
5083 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005084 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005085 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005086 case ISD::ATOMIC_CMP_SWAP:
5087 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5088 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005089 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005090 if (Res.getNode())
5091 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005092}
Chris Lattner27a6c732007-11-24 07:07:01 +00005093
Evan Chenga8e29892007-01-19 07:51:42 +00005094//===----------------------------------------------------------------------===//
5095// ARM Scheduler Hooks
5096//===----------------------------------------------------------------------===//
5097
5098MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005099ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5100 MachineBasicBlock *BB,
5101 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005102 unsigned dest = MI->getOperand(0).getReg();
5103 unsigned ptr = MI->getOperand(1).getReg();
5104 unsigned oldval = MI->getOperand(2).getReg();
5105 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005106 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5107 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005108 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005109
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005110 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5111 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005112 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005113 : ARM::GPRRegisterClass);
5114
5115 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005116 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5117 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5118 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005119 }
5120
Jim Grosbach5278eb82009-12-11 01:42:04 +00005121 unsigned ldrOpc, strOpc;
5122 switch (Size) {
5123 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005124 case 1:
5125 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005126 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005127 break;
5128 case 2:
5129 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5130 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5131 break;
5132 case 4:
5133 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5134 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5135 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005136 }
5137
5138 MachineFunction *MF = BB->getParent();
5139 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5140 MachineFunction::iterator It = BB;
5141 ++It; // insert the new blocks after the current block
5142
5143 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5144 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5145 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5146 MF->insert(It, loop1MBB);
5147 MF->insert(It, loop2MBB);
5148 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005149
5150 // Transfer the remainder of BB and its successor edges to exitMBB.
5151 exitMBB->splice(exitMBB->begin(), BB,
5152 llvm::next(MachineBasicBlock::iterator(MI)),
5153 BB->end());
5154 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005155
5156 // thisMBB:
5157 // ...
5158 // fallthrough --> loop1MBB
5159 BB->addSuccessor(loop1MBB);
5160
5161 // loop1MBB:
5162 // ldrex dest, [ptr]
5163 // cmp dest, oldval
5164 // bne exitMBB
5165 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005166 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5167 if (ldrOpc == ARM::t2LDREX)
5168 MIB.addImm(0);
5169 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005170 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005171 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005172 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5173 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005174 BB->addSuccessor(loop2MBB);
5175 BB->addSuccessor(exitMBB);
5176
5177 // loop2MBB:
5178 // strex scratch, newval, [ptr]
5179 // cmp scratch, #0
5180 // bne loop1MBB
5181 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005182 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5183 if (strOpc == ARM::t2STREX)
5184 MIB.addImm(0);
5185 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005186 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005187 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005188 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5189 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005190 BB->addSuccessor(loop1MBB);
5191 BB->addSuccessor(exitMBB);
5192
5193 // exitMBB:
5194 // ...
5195 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005196
Dan Gohman14152b42010-07-06 20:24:04 +00005197 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005198
Jim Grosbach5278eb82009-12-11 01:42:04 +00005199 return BB;
5200}
5201
5202MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005203ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5204 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005205 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5206 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5207
5208 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005209 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005210 MachineFunction::iterator It = BB;
5211 ++It;
5212
5213 unsigned dest = MI->getOperand(0).getReg();
5214 unsigned ptr = MI->getOperand(1).getReg();
5215 unsigned incr = MI->getOperand(2).getReg();
5216 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005217 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005218
5219 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5220 if (isThumb2) {
5221 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5222 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5223 }
5224
Jim Grosbachc3c23542009-12-14 04:22:04 +00005225 unsigned ldrOpc, strOpc;
5226 switch (Size) {
5227 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005228 case 1:
5229 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005230 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005231 break;
5232 case 2:
5233 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5234 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5235 break;
5236 case 4:
5237 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5238 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5239 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005240 }
5241
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005242 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5243 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5244 MF->insert(It, loopMBB);
5245 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005246
5247 // Transfer the remainder of BB and its successor edges to exitMBB.
5248 exitMBB->splice(exitMBB->begin(), BB,
5249 llvm::next(MachineBasicBlock::iterator(MI)),
5250 BB->end());
5251 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005252
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005253 TargetRegisterClass *TRC =
5254 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5255 unsigned scratch = MRI.createVirtualRegister(TRC);
5256 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005257
5258 // thisMBB:
5259 // ...
5260 // fallthrough --> loopMBB
5261 BB->addSuccessor(loopMBB);
5262
5263 // loopMBB:
5264 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005265 // <binop> scratch2, dest, incr
5266 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005267 // cmp scratch, #0
5268 // bne- loopMBB
5269 // fallthrough --> exitMBB
5270 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005271 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5272 if (ldrOpc == ARM::t2LDREX)
5273 MIB.addImm(0);
5274 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005275 if (BinOpcode) {
5276 // operand order needs to go the other way for NAND
5277 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5278 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5279 addReg(incr).addReg(dest)).addReg(0);
5280 else
5281 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5282 addReg(dest).addReg(incr)).addReg(0);
5283 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005284
Jim Grosbachb6aed502011-09-09 18:37:27 +00005285 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5286 if (strOpc == ARM::t2STREX)
5287 MIB.addImm(0);
5288 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005289 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005290 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005291 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5292 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005293
5294 BB->addSuccessor(loopMBB);
5295 BB->addSuccessor(exitMBB);
5296
5297 // exitMBB:
5298 // ...
5299 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005300
Dan Gohman14152b42010-07-06 20:24:04 +00005301 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005302
Jim Grosbachc3c23542009-12-14 04:22:04 +00005303 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005304}
5305
Jim Grosbachf7da8822011-04-26 19:44:18 +00005306MachineBasicBlock *
5307ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5308 MachineBasicBlock *BB,
5309 unsigned Size,
5310 bool signExtend,
5311 ARMCC::CondCodes Cond) const {
5312 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5313
5314 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5315 MachineFunction *MF = BB->getParent();
5316 MachineFunction::iterator It = BB;
5317 ++It;
5318
5319 unsigned dest = MI->getOperand(0).getReg();
5320 unsigned ptr = MI->getOperand(1).getReg();
5321 unsigned incr = MI->getOperand(2).getReg();
5322 unsigned oldval = dest;
5323 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005324 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005325
5326 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5327 if (isThumb2) {
5328 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5329 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5330 }
5331
Jim Grosbachf7da8822011-04-26 19:44:18 +00005332 unsigned ldrOpc, strOpc, extendOpc;
5333 switch (Size) {
5334 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5335 case 1:
5336 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5337 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005338 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005339 break;
5340 case 2:
5341 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5342 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005343 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005344 break;
5345 case 4:
5346 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5347 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5348 extendOpc = 0;
5349 break;
5350 }
5351
5352 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5353 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5354 MF->insert(It, loopMBB);
5355 MF->insert(It, exitMBB);
5356
5357 // Transfer the remainder of BB and its successor edges to exitMBB.
5358 exitMBB->splice(exitMBB->begin(), BB,
5359 llvm::next(MachineBasicBlock::iterator(MI)),
5360 BB->end());
5361 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5362
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005363 TargetRegisterClass *TRC =
5364 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5365 unsigned scratch = MRI.createVirtualRegister(TRC);
5366 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005367
5368 // thisMBB:
5369 // ...
5370 // fallthrough --> loopMBB
5371 BB->addSuccessor(loopMBB);
5372
5373 // loopMBB:
5374 // ldrex dest, ptr
5375 // (sign extend dest, if required)
5376 // cmp dest, incr
5377 // cmov.cond scratch2, dest, incr
5378 // strex scratch, scratch2, ptr
5379 // cmp scratch, #0
5380 // bne- loopMBB
5381 // fallthrough --> exitMBB
5382 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005383 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5384 if (ldrOpc == ARM::t2LDREX)
5385 MIB.addImm(0);
5386 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005387
5388 // Sign extend the value, if necessary.
5389 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005390 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005391 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5392 .addReg(dest)
5393 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005394 }
5395
5396 // Build compare and cmov instructions.
5397 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5398 .addReg(oldval).addReg(incr));
5399 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5400 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5401
Jim Grosbachb6aed502011-09-09 18:37:27 +00005402 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5403 if (strOpc == ARM::t2STREX)
5404 MIB.addImm(0);
5405 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005406 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5407 .addReg(scratch).addImm(0));
5408 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5409 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5410
5411 BB->addSuccessor(loopMBB);
5412 BB->addSuccessor(exitMBB);
5413
5414 // exitMBB:
5415 // ...
5416 BB = exitMBB;
5417
5418 MI->eraseFromParent(); // The instruction is gone now.
5419
5420 return BB;
5421}
5422
Eli Friedman2bdffe42011-08-31 00:31:29 +00005423MachineBasicBlock *
5424ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5425 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005426 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005427 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5428 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5429
5430 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5431 MachineFunction *MF = BB->getParent();
5432 MachineFunction::iterator It = BB;
5433 ++It;
5434
5435 unsigned destlo = MI->getOperand(0).getReg();
5436 unsigned desthi = MI->getOperand(1).getReg();
5437 unsigned ptr = MI->getOperand(2).getReg();
5438 unsigned vallo = MI->getOperand(3).getReg();
5439 unsigned valhi = MI->getOperand(4).getReg();
5440 DebugLoc dl = MI->getDebugLoc();
5441 bool isThumb2 = Subtarget->isThumb2();
5442
5443 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5444 if (isThumb2) {
5445 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5446 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5447 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5448 }
5449
5450 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5451 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5452
5453 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005454 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005455 if (IsCmpxchg) {
5456 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5457 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5458 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005459 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5460 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005461 if (IsCmpxchg) {
5462 MF->insert(It, contBB);
5463 MF->insert(It, cont2BB);
5464 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005465 MF->insert(It, exitMBB);
5466
5467 // Transfer the remainder of BB and its successor edges to exitMBB.
5468 exitMBB->splice(exitMBB->begin(), BB,
5469 llvm::next(MachineBasicBlock::iterator(MI)),
5470 BB->end());
5471 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5472
5473 TargetRegisterClass *TRC =
5474 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5475 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5476
5477 // thisMBB:
5478 // ...
5479 // fallthrough --> loopMBB
5480 BB->addSuccessor(loopMBB);
5481
5482 // loopMBB:
5483 // ldrexd r2, r3, ptr
5484 // <binopa> r0, r2, incr
5485 // <binopb> r1, r3, incr
5486 // strexd storesuccess, r0, r1, ptr
5487 // cmp storesuccess, #0
5488 // bne- loopMBB
5489 // fallthrough --> exitMBB
5490 //
5491 // Note that the registers are explicitly specified because there is not any
5492 // way to force the register allocator to allocate a register pair.
5493 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005494 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005495 // need to properly enforce the restriction that the two output registers
5496 // for ldrexd must be different.
5497 BB = loopMBB;
5498 // Load
5499 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5500 .addReg(ARM::R2, RegState::Define)
5501 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5502 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5503 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5504 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005505
5506 if (IsCmpxchg) {
5507 // Add early exit
5508 for (unsigned i = 0; i < 2; i++) {
5509 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5510 ARM::CMPrr))
5511 .addReg(i == 0 ? destlo : desthi)
5512 .addReg(i == 0 ? vallo : valhi));
5513 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5514 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5515 BB->addSuccessor(exitMBB);
5516 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5517 BB = (i == 0 ? contBB : cont2BB);
5518 }
5519
5520 // Copy to physregs for strexd
5521 unsigned setlo = MI->getOperand(5).getReg();
5522 unsigned sethi = MI->getOperand(6).getReg();
5523 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5524 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5525 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005526 // Perform binary operation
5527 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5528 .addReg(destlo).addReg(vallo))
5529 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5530 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5531 .addReg(desthi).addReg(valhi)).addReg(0);
5532 } else {
5533 // Copy to physregs for strexd
5534 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5535 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5536 }
5537
5538 // Store
5539 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5540 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5541 // Cmp+jump
5542 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5543 .addReg(storesuccess).addImm(0));
5544 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5545 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5546
5547 BB->addSuccessor(loopMBB);
5548 BB->addSuccessor(exitMBB);
5549
5550 // exitMBB:
5551 // ...
5552 BB = exitMBB;
5553
5554 MI->eraseFromParent(); // The instruction is gone now.
5555
5556 return BB;
5557}
5558
Bill Wendlingf1083d42011-10-07 22:08:37 +00005559/// EmitBasePointerRecalculation - For functions using a base pointer, we
5560/// rematerialize it (via the frame pointer).
5561void ARMTargetLowering::
5562EmitBasePointerRecalculation(MachineInstr *MI, MachineBasicBlock *MBB,
5563 MachineBasicBlock *DispatchBB) const {
5564 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5565 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
5566 MachineFunction &MF = *MI->getParent()->getParent();
5567 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
5568 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
5569
5570 if (!RI.hasBasePointer(MF)) return;
5571
5572 MachineBasicBlock::iterator MBBI = MI;
5573
5574 int32_t NumBytes = AFI->getFramePtrSpillOffset();
5575 unsigned FramePtr = RI.getFrameRegister(MF);
5576 assert(MF.getTarget().getFrameLowering()->hasFP(MF) &&
5577 "Base pointer without frame pointer?");
5578
5579 if (AFI->isThumb2Function())
5580 llvm::emitT2RegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5581 FramePtr, -NumBytes, ARMCC::AL, 0, *AII);
5582 else if (AFI->isThumbFunction())
5583 llvm::emitThumbRegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5584 FramePtr, -NumBytes, *AII, RI);
5585 else
5586 llvm::emitARMRegPlusImmediate(*MBB, MBBI, MI->getDebugLoc(), ARM::R6,
5587 FramePtr, -NumBytes, ARMCC::AL, 0, *AII);
5588
5589 if (!RI.needsStackRealignment(MF)) return;
5590
5591 // If there's dynamic realignment, adjust for it.
5592 MachineFrameInfo *MFI = MF.getFrameInfo();
5593 unsigned MaxAlign = MFI->getMaxAlignment();
5594 assert(!AFI->isThumb1OnlyFunction());
5595
5596 // Emit bic r6, r6, MaxAlign
5597 unsigned bicOpc = AFI->isThumbFunction() ? ARM::t2BICri : ARM::BICri;
5598 AddDefaultCC(
5599 AddDefaultPred(
5600 BuildMI(*MBB, MBBI, MI->getDebugLoc(), TII->get(bicOpc), ARM::R6)
5601 .addReg(ARM::R6, RegState::Kill)
5602 .addImm(MaxAlign - 1)));
5603}
5604
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005605/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5606/// registers the function context.
5607void ARMTargetLowering::
5608SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5609 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005610 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5611 DebugLoc dl = MI->getDebugLoc();
5612 MachineFunction *MF = MBB->getParent();
5613 MachineRegisterInfo *MRI = &MF->getRegInfo();
5614 MachineConstantPool *MCP = MF->getConstantPool();
5615 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5616 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005617
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005618 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005619 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005620
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005621 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005622 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005623 ARMConstantPoolValue *CPV =
5624 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5625 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5626
5627 const TargetRegisterClass *TRC =
5628 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5629
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005630 // Grab constant pool and fixed stack memory operands.
5631 MachineMemOperand *CPMMO =
5632 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5633 MachineMemOperand::MOLoad, 4, 4);
5634
5635 MachineMemOperand *FIMMOSt =
5636 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5637 MachineMemOperand::MOStore, 4, 4);
5638
Bill Wendlingf1083d42011-10-07 22:08:37 +00005639 EmitBasePointerRecalculation(MI, MBB, DispatchBB);
5640
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005641 // Load the address of the dispatch MBB into the jump buffer.
5642 if (isThumb2) {
5643 // Incoming value: jbuf
5644 // ldr.n r5, LCPI1_1
5645 // orr r5, r5, #1
5646 // add r5, pc
5647 // str r5, [$jbuf, #+4] ; &jbuf[1]
5648 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5649 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5650 .addConstantPoolIndex(CPI)
5651 .addMemOperand(CPMMO));
5652 // Set the low bit because of thumb mode.
5653 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5654 AddDefaultCC(
5655 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5656 .addReg(NewVReg1, RegState::Kill)
5657 .addImm(0x01)));
5658 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5659 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5660 .addReg(NewVReg2, RegState::Kill)
5661 .addImm(PCLabelId);
5662 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5663 .addReg(NewVReg3, RegState::Kill)
5664 .addFrameIndex(FI)
5665 .addImm(36) // &jbuf[1] :: pc
5666 .addMemOperand(FIMMOSt));
5667 } else if (isThumb) {
5668 // Incoming value: jbuf
5669 // ldr.n r1, LCPI1_4
5670 // add r1, pc
5671 // mov r2, #1
5672 // orrs r1, r2
5673 // add r2, $jbuf, #+4 ; &jbuf[1]
5674 // str r1, [r2]
5675 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5676 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5677 .addConstantPoolIndex(CPI)
5678 .addMemOperand(CPMMO));
5679 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5680 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5681 .addReg(NewVReg1, RegState::Kill)
5682 .addImm(PCLabelId);
5683 // Set the low bit because of thumb mode.
5684 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5685 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5686 .addReg(ARM::CPSR, RegState::Define)
5687 .addImm(1));
5688 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5689 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5690 .addReg(ARM::CPSR, RegState::Define)
5691 .addReg(NewVReg2, RegState::Kill)
5692 .addReg(NewVReg3, RegState::Kill));
5693 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5694 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5695 .addFrameIndex(FI)
5696 .addImm(36)); // &jbuf[1] :: pc
5697 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5698 .addReg(NewVReg4, RegState::Kill)
5699 .addReg(NewVReg5, RegState::Kill)
5700 .addImm(0)
5701 .addMemOperand(FIMMOSt));
5702 } else {
5703 // Incoming value: jbuf
5704 // ldr r1, LCPI1_1
5705 // add r1, pc, r1
5706 // str r1, [$jbuf, #+4] ; &jbuf[1]
5707 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5708 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5709 .addConstantPoolIndex(CPI)
5710 .addImm(0)
5711 .addMemOperand(CPMMO));
5712 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5713 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5714 .addReg(NewVReg1, RegState::Kill)
5715 .addImm(PCLabelId));
5716 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5717 .addReg(NewVReg2, RegState::Kill)
5718 .addFrameIndex(FI)
5719 .addImm(36) // &jbuf[1] :: pc
5720 .addMemOperand(FIMMOSt));
5721 }
5722}
5723
5724MachineBasicBlock *ARMTargetLowering::
5725EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5726 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5727 DebugLoc dl = MI->getDebugLoc();
5728 MachineFunction *MF = MBB->getParent();
5729 MachineRegisterInfo *MRI = &MF->getRegInfo();
5730 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5731 MachineFrameInfo *MFI = MF->getFrameInfo();
5732 int FI = MFI->getFunctionContextIndex();
5733
5734 const TargetRegisterClass *TRC =
5735 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5736
Bill Wendling04f15b42011-10-06 21:29:56 +00005737 // Get a mapping of the call site numbers to all of the landing pads they're
5738 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005739 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5740 unsigned MaxCSNum = 0;
5741 MachineModuleInfo &MMI = MF->getMMI();
5742 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5743 if (!BB->isLandingPad()) continue;
5744
5745 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5746 // pad.
5747 for (MachineBasicBlock::iterator
5748 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5749 if (!II->isEHLabel()) continue;
5750
5751 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005752 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005753
Bill Wendling5cbef192011-10-05 23:28:57 +00005754 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5755 for (SmallVectorImpl<unsigned>::iterator
5756 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5757 CSI != CSE; ++CSI) {
5758 CallSiteNumToLPad[*CSI].push_back(BB);
5759 MaxCSNum = std::max(MaxCSNum, *CSI);
5760 }
Bill Wendling2a850152011-10-05 00:02:33 +00005761 break;
5762 }
5763 }
5764
5765 // Get an ordered list of the machine basic blocks for the jump table.
5766 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005767 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005768 LPadList.reserve(CallSiteNumToLPad.size());
5769 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5770 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5771 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005772 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005773 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005774 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5775 }
Bill Wendling2a850152011-10-05 00:02:33 +00005776 }
5777
Bill Wendling5cbef192011-10-05 23:28:57 +00005778 assert(!LPadList.empty() &&
5779 "No landing pad destinations for the dispatch jump table!");
5780
Bill Wendling04f15b42011-10-06 21:29:56 +00005781 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005782 MachineJumpTableInfo *JTI =
5783 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5784 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5785 unsigned UId = AFI->createJumpTableUId();
5786
Bill Wendling04f15b42011-10-06 21:29:56 +00005787 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005788
5789 // Shove the dispatch's address into the return slot in the function context.
5790 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5791 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005792
Bill Wendlingbb734682011-10-05 00:39:32 +00005793 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005794 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005795 DispatchBB->addSuccessor(TrapBB);
5796
5797 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5798 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005799
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005800 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005801 MF->insert(MF->end(), DispatchBB);
5802 MF->insert(MF->end(), DispContBB);
5803 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005804
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005805 // Insert code into the entry block that creates and registers the function
5806 // context.
5807 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5808
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005809 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005810 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005811 MachineMemOperand::MOLoad |
5812 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005813
Bill Wendling952cb502011-10-18 22:49:07 +00005814 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005815 if (Subtarget->isThumb2()) {
5816 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5817 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5818 .addFrameIndex(FI)
5819 .addImm(4)
5820 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005821
Bill Wendling952cb502011-10-18 22:49:07 +00005822 if (NumLPads < 256) {
5823 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5824 .addReg(NewVReg1)
5825 .addImm(LPadList.size()));
5826 } else {
5827 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5828 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005829 .addImm(NumLPads & 0xFFFF));
5830
5831 unsigned VReg2 = VReg1;
5832 if ((NumLPads & 0xFFFF0000) != 0) {
5833 VReg2 = MRI->createVirtualRegister(TRC);
5834 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5835 .addReg(VReg1)
5836 .addImm(NumLPads >> 16));
5837 }
5838
Bill Wendling952cb502011-10-18 22:49:07 +00005839 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5840 .addReg(NewVReg1)
5841 .addReg(VReg2));
5842 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005843
Bill Wendling95ce2e92011-10-06 22:53:00 +00005844 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5845 .addMBB(TrapBB)
5846 .addImm(ARMCC::HI)
5847 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005848
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005849 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5850 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005851 .addJumpTableIndex(MJTI)
5852 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005853
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005854 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005855 AddDefaultCC(
5856 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005857 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5858 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005859 .addReg(NewVReg1)
5860 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5861
5862 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005863 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005864 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005865 .addJumpTableIndex(MJTI)
5866 .addImm(UId);
5867 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005868 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5869 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5870 .addFrameIndex(FI)
5871 .addImm(1)
5872 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005873
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005874 if (NumLPads < 256) {
5875 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5876 .addReg(NewVReg1)
5877 .addImm(NumLPads));
5878 } else {
5879 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005880 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5881 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5882
5883 // MachineConstantPool wants an explicit alignment.
5884 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5885 if (Align == 0)
5886 Align = getTargetData()->getTypeAllocSize(C->getType());
5887 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005888
5889 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5890 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5891 .addReg(VReg1, RegState::Define)
5892 .addConstantPoolIndex(Idx));
5893 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5894 .addReg(NewVReg1)
5895 .addReg(VReg1));
5896 }
5897
Bill Wendling083a8eb2011-10-06 23:37:36 +00005898 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5899 .addMBB(TrapBB)
5900 .addImm(ARMCC::HI)
5901 .addReg(ARM::CPSR);
5902
5903 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5904 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5905 .addReg(ARM::CPSR, RegState::Define)
5906 .addReg(NewVReg1)
5907 .addImm(2));
5908
5909 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005910 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005911 .addJumpTableIndex(MJTI)
5912 .addImm(UId));
5913
5914 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5915 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5916 .addReg(ARM::CPSR, RegState::Define)
5917 .addReg(NewVReg2, RegState::Kill)
5918 .addReg(NewVReg3));
5919
5920 MachineMemOperand *JTMMOLd =
5921 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5922 MachineMemOperand::MOLoad, 4, 4);
5923
5924 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5925 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5926 .addReg(NewVReg4, RegState::Kill)
5927 .addImm(0)
5928 .addMemOperand(JTMMOLd));
5929
5930 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5931 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5932 .addReg(ARM::CPSR, RegState::Define)
5933 .addReg(NewVReg5, RegState::Kill)
5934 .addReg(NewVReg3));
5935
5936 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
5937 .addReg(NewVReg6, RegState::Kill)
5938 .addJumpTableIndex(MJTI)
5939 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005940 } else {
5941 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5942 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
5943 .addFrameIndex(FI)
5944 .addImm(4)
5945 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00005946
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005947 if (NumLPads < 256) {
5948 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
5949 .addReg(NewVReg1)
5950 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00005951 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005952 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5953 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005954 .addImm(NumLPads & 0xFFFF));
5955
5956 unsigned VReg2 = VReg1;
5957 if ((NumLPads & 0xFFFF0000) != 0) {
5958 VReg2 = MRI->createVirtualRegister(TRC);
5959 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
5960 .addReg(VReg1)
5961 .addImm(NumLPads >> 16));
5962 }
5963
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005964 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5965 .addReg(NewVReg1)
5966 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00005967 } else {
5968 MachineConstantPool *ConstantPool = MF->getConstantPool();
5969 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5970 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5971
5972 // MachineConstantPool wants an explicit alignment.
5973 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5974 if (Align == 0)
5975 Align = getTargetData()->getTypeAllocSize(C->getType());
5976 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
5977
5978 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5979 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
5980 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00005981 .addConstantPoolIndex(Idx)
5982 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00005983 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
5984 .addReg(NewVReg1)
5985 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00005986 }
5987
Bill Wendling95ce2e92011-10-06 22:53:00 +00005988 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
5989 .addMBB(TrapBB)
5990 .addImm(ARMCC::HI)
5991 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00005992
Bill Wendling564392b2011-10-18 22:11:18 +00005993 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005994 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00005995 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005996 .addReg(NewVReg1)
5997 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00005998 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5999 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006000 .addJumpTableIndex(MJTI)
6001 .addImm(UId));
6002
6003 MachineMemOperand *JTMMOLd =
6004 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6005 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006006 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006007 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006008 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6009 .addReg(NewVReg3, RegState::Kill)
6010 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006011 .addImm(0)
6012 .addMemOperand(JTMMOLd));
6013
6014 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006015 .addReg(NewVReg5, RegState::Kill)
6016 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006017 .addJumpTableIndex(MJTI)
6018 .addImm(UId);
6019 }
Bill Wendling2a850152011-10-05 00:02:33 +00006020
Bill Wendlingbb734682011-10-05 00:39:32 +00006021 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006022 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006023 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006024 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6025 MachineBasicBlock *CurMBB = *I;
6026 if (PrevMBB != CurMBB)
6027 DispContBB->addSuccessor(CurMBB);
6028 PrevMBB = CurMBB;
6029 }
6030
Bill Wendling24bb9252011-10-17 05:25:09 +00006031 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006032 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6033 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6034 const unsigned *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006035 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006036 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6037 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6038 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006039
6040 // Remove the landing pad successor from the invoke block and replace it
6041 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006042 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6043 BB->succ_end());
6044 while (!Successors.empty()) {
6045 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006046 if (SMBB->isLandingPad()) {
6047 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006048 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006049 }
6050 }
6051
6052 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006053
6054 // Find the invoke call and mark all of the callee-saved registers as
6055 // 'implicit defined' so that they're spilled. This prevents code from
6056 // moving instructions to before the EH block, where they will never be
6057 // executed.
6058 for (MachineBasicBlock::reverse_iterator
6059 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
6060 if (!II->getDesc().isCall()) continue;
6061
6062 DenseMap<unsigned, bool> DefRegs;
6063 for (MachineInstr::mop_iterator
6064 OI = II->operands_begin(), OE = II->operands_end();
6065 OI != OE; ++OI) {
6066 if (!OI->isReg()) continue;
6067 DefRegs[OI->getReg()] = true;
6068 }
6069
6070 MachineInstrBuilder MIB(&*II);
6071
Bill Wendling5d798592011-10-14 23:55:44 +00006072 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006073 unsigned Reg = SavedRegs[i];
6074 if (Subtarget->isThumb2() &&
6075 !ARM::tGPRRegisterClass->contains(Reg) &&
6076 !ARM::hGPRRegisterClass->contains(Reg))
6077 continue;
6078 else if (Subtarget->isThumb1Only() &&
6079 !ARM::tGPRRegisterClass->contains(Reg))
6080 continue;
6081 else if (!Subtarget->isThumb() &&
6082 !ARM::GPRRegisterClass->contains(Reg))
6083 continue;
6084 if (!DefRegs[Reg])
6085 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006086 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006087
6088 break;
6089 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006090 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006091
Bill Wendlingf7b02072011-10-18 18:30:49 +00006092 // Mark all former landing pads as non-landing pads. The dispatch is the only
6093 // landing pad now.
6094 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6095 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6096 (*I)->setIsLandingPad(false);
6097
Bill Wendlingbb734682011-10-05 00:39:32 +00006098 // The instruction is gone now.
6099 MI->eraseFromParent();
6100
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006101 return MBB;
6102}
6103
Evan Cheng218977b2010-07-13 19:27:42 +00006104static
6105MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6106 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6107 E = MBB->succ_end(); I != E; ++I)
6108 if (*I != Succ)
6109 return *I;
6110 llvm_unreachable("Expecting a BB with two successors!");
6111}
6112
Jim Grosbache801dc42009-12-12 01:40:06 +00006113MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006114ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006115 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006116 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006117 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006118 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006119 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006120 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006121 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006122 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006123 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006124 // The Thumb2 pre-indexed stores have the same MI operands, they just
6125 // define them differently in the .td files from the isel patterns, so
6126 // they need pseudos.
6127 case ARM::t2STR_preidx:
6128 MI->setDesc(TII->get(ARM::t2STR_PRE));
6129 return BB;
6130 case ARM::t2STRB_preidx:
6131 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6132 return BB;
6133 case ARM::t2STRH_preidx:
6134 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6135 return BB;
6136
Jim Grosbach19dec202011-08-05 20:35:44 +00006137 case ARM::STRi_preidx:
6138 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006139 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006140 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6141 // Decode the offset.
6142 unsigned Offset = MI->getOperand(4).getImm();
6143 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6144 Offset = ARM_AM::getAM2Offset(Offset);
6145 if (isSub)
6146 Offset = -Offset;
6147
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006148 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006149 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006150 .addOperand(MI->getOperand(0)) // Rn_wb
6151 .addOperand(MI->getOperand(1)) // Rt
6152 .addOperand(MI->getOperand(2)) // Rn
6153 .addImm(Offset) // offset (skip GPR==zero_reg)
6154 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006155 .addOperand(MI->getOperand(6))
6156 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006157 MI->eraseFromParent();
6158 return BB;
6159 }
6160 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006161 case ARM::STRBr_preidx:
6162 case ARM::STRH_preidx: {
6163 unsigned NewOpc;
6164 switch (MI->getOpcode()) {
6165 default: llvm_unreachable("unexpected opcode!");
6166 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6167 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6168 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6169 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006170 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6171 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6172 MIB.addOperand(MI->getOperand(i));
6173 MI->eraseFromParent();
6174 return BB;
6175 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006176 case ARM::ATOMIC_LOAD_ADD_I8:
6177 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6178 case ARM::ATOMIC_LOAD_ADD_I16:
6179 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6180 case ARM::ATOMIC_LOAD_ADD_I32:
6181 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006182
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006183 case ARM::ATOMIC_LOAD_AND_I8:
6184 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6185 case ARM::ATOMIC_LOAD_AND_I16:
6186 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6187 case ARM::ATOMIC_LOAD_AND_I32:
6188 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006189
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006190 case ARM::ATOMIC_LOAD_OR_I8:
6191 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6192 case ARM::ATOMIC_LOAD_OR_I16:
6193 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6194 case ARM::ATOMIC_LOAD_OR_I32:
6195 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006196
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006197 case ARM::ATOMIC_LOAD_XOR_I8:
6198 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6199 case ARM::ATOMIC_LOAD_XOR_I16:
6200 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6201 case ARM::ATOMIC_LOAD_XOR_I32:
6202 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006203
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006204 case ARM::ATOMIC_LOAD_NAND_I8:
6205 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6206 case ARM::ATOMIC_LOAD_NAND_I16:
6207 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6208 case ARM::ATOMIC_LOAD_NAND_I32:
6209 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006210
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006211 case ARM::ATOMIC_LOAD_SUB_I8:
6212 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6213 case ARM::ATOMIC_LOAD_SUB_I16:
6214 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6215 case ARM::ATOMIC_LOAD_SUB_I32:
6216 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006217
Jim Grosbachf7da8822011-04-26 19:44:18 +00006218 case ARM::ATOMIC_LOAD_MIN_I8:
6219 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6220 case ARM::ATOMIC_LOAD_MIN_I16:
6221 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6222 case ARM::ATOMIC_LOAD_MIN_I32:
6223 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6224
6225 case ARM::ATOMIC_LOAD_MAX_I8:
6226 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6227 case ARM::ATOMIC_LOAD_MAX_I16:
6228 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6229 case ARM::ATOMIC_LOAD_MAX_I32:
6230 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6231
6232 case ARM::ATOMIC_LOAD_UMIN_I8:
6233 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6234 case ARM::ATOMIC_LOAD_UMIN_I16:
6235 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6236 case ARM::ATOMIC_LOAD_UMIN_I32:
6237 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6238
6239 case ARM::ATOMIC_LOAD_UMAX_I8:
6240 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6241 case ARM::ATOMIC_LOAD_UMAX_I16:
6242 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6243 case ARM::ATOMIC_LOAD_UMAX_I32:
6244 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6245
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006246 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6247 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6248 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006249
6250 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6251 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6252 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006253
Eli Friedman2bdffe42011-08-31 00:31:29 +00006254
6255 case ARM::ATOMADD6432:
6256 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006257 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6258 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006259 case ARM::ATOMSUB6432:
6260 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006261 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6262 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006263 case ARM::ATOMOR6432:
6264 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006265 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006266 case ARM::ATOMXOR6432:
6267 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006268 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006269 case ARM::ATOMAND6432:
6270 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006271 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006272 case ARM::ATOMSWAP6432:
6273 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006274 case ARM::ATOMCMPXCHG6432:
6275 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6276 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6277 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006278
Evan Cheng007ea272009-08-12 05:17:19 +00006279 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006280 // To "insert" a SELECT_CC instruction, we actually have to insert the
6281 // diamond control-flow pattern. The incoming instruction knows the
6282 // destination vreg to set, the condition code register to branch on, the
6283 // true/false values to select between, and a branch opcode to use.
6284 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006285 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006286 ++It;
6287
6288 // thisMBB:
6289 // ...
6290 // TrueVal = ...
6291 // cmpTY ccX, r1, r2
6292 // bCC copy1MBB
6293 // fallthrough --> copy0MBB
6294 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006295 MachineFunction *F = BB->getParent();
6296 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6297 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006298 F->insert(It, copy0MBB);
6299 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006300
6301 // Transfer the remainder of BB and its successor edges to sinkMBB.
6302 sinkMBB->splice(sinkMBB->begin(), BB,
6303 llvm::next(MachineBasicBlock::iterator(MI)),
6304 BB->end());
6305 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6306
Dan Gohman258c58c2010-07-06 15:49:48 +00006307 BB->addSuccessor(copy0MBB);
6308 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006309
Dan Gohman14152b42010-07-06 20:24:04 +00006310 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6311 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6312
Evan Chenga8e29892007-01-19 07:51:42 +00006313 // copy0MBB:
6314 // %FalseValue = ...
6315 // # fallthrough to sinkMBB
6316 BB = copy0MBB;
6317
6318 // Update machine-CFG edges
6319 BB->addSuccessor(sinkMBB);
6320
6321 // sinkMBB:
6322 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6323 // ...
6324 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006325 BuildMI(*BB, BB->begin(), dl,
6326 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006327 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6328 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6329
Dan Gohman14152b42010-07-06 20:24:04 +00006330 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006331 return BB;
6332 }
Evan Cheng86198642009-08-07 00:34:42 +00006333
Evan Cheng218977b2010-07-13 19:27:42 +00006334 case ARM::BCCi64:
6335 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006336 // If there is an unconditional branch to the other successor, remove it.
6337 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006338
Evan Cheng218977b2010-07-13 19:27:42 +00006339 // Compare both parts that make up the double comparison separately for
6340 // equality.
6341 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6342
6343 unsigned LHS1 = MI->getOperand(1).getReg();
6344 unsigned LHS2 = MI->getOperand(2).getReg();
6345 if (RHSisZero) {
6346 AddDefaultPred(BuildMI(BB, dl,
6347 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6348 .addReg(LHS1).addImm(0));
6349 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6350 .addReg(LHS2).addImm(0)
6351 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6352 } else {
6353 unsigned RHS1 = MI->getOperand(3).getReg();
6354 unsigned RHS2 = MI->getOperand(4).getReg();
6355 AddDefaultPred(BuildMI(BB, dl,
6356 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6357 .addReg(LHS1).addReg(RHS1));
6358 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6359 .addReg(LHS2).addReg(RHS2)
6360 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6361 }
6362
6363 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6364 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6365 if (MI->getOperand(0).getImm() == ARMCC::NE)
6366 std::swap(destMBB, exitMBB);
6367
6368 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6369 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006370 if (isThumb2)
6371 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6372 else
6373 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006374
6375 MI->eraseFromParent(); // The pseudo instruction is gone now.
6376 return BB;
6377 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006378
Bill Wendling5bc85282011-10-17 20:37:20 +00006379 case ARM::Int_eh_sjlj_setjmp:
6380 case ARM::Int_eh_sjlj_setjmp_nofp:
6381 case ARM::tInt_eh_sjlj_setjmp:
6382 case ARM::t2Int_eh_sjlj_setjmp:
6383 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6384 EmitSjLjDispatchBlock(MI, BB);
6385 return BB;
6386
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006387 case ARM::ABS:
6388 case ARM::t2ABS: {
6389 // To insert an ABS instruction, we have to insert the
6390 // diamond control-flow pattern. The incoming instruction knows the
6391 // source vreg to test against 0, the destination vreg to set,
6392 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006393 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006394 // It transforms
6395 // V1 = ABS V0
6396 // into
6397 // V2 = MOVS V0
6398 // BCC (branch to SinkBB if V0 >= 0)
6399 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006400 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006401 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6402 MachineFunction::iterator BBI = BB;
6403 ++BBI;
6404 MachineFunction *Fn = BB->getParent();
6405 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6406 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6407 Fn->insert(BBI, RSBBB);
6408 Fn->insert(BBI, SinkBB);
6409
6410 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6411 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6412 bool isThumb2 = Subtarget->isThumb2();
6413 MachineRegisterInfo &MRI = Fn->getRegInfo();
6414 // In Thumb mode S must not be specified if source register is the SP or
6415 // PC and if destination register is the SP, so restrict register class
6416 unsigned NewMovDstReg = MRI.createVirtualRegister(
6417 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6418 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6419 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6420
6421 // Transfer the remainder of BB and its successor edges to sinkMBB.
6422 SinkBB->splice(SinkBB->begin(), BB,
6423 llvm::next(MachineBasicBlock::iterator(MI)),
6424 BB->end());
6425 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6426
6427 BB->addSuccessor(RSBBB);
6428 BB->addSuccessor(SinkBB);
6429
6430 // fall through to SinkMBB
6431 RSBBB->addSuccessor(SinkBB);
6432
6433 // insert a movs at the end of BB
6434 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6435 NewMovDstReg)
6436 .addReg(ABSSrcReg, RegState::Kill)
6437 .addImm((unsigned)ARMCC::AL).addReg(0)
6438 .addReg(ARM::CPSR, RegState::Define);
6439
6440 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006441 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006442 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6443 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6444
6445 // insert rsbri in RSBBB
6446 // Note: BCC and rsbri will be converted into predicated rsbmi
6447 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006448 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006449 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6450 .addReg(NewMovDstReg, RegState::Kill)
6451 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6452
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006453 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006454 // reuse ABSDstReg to not change uses of ABS instruction
6455 BuildMI(*SinkBB, SinkBB->begin(), dl,
6456 TII->get(ARM::PHI), ABSDstReg)
6457 .addReg(NewRsbDstReg).addMBB(RSBBB)
6458 .addReg(NewMovDstReg).addMBB(BB);
6459
6460 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006461 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006462
6463 // return last added BB
6464 return SinkBB;
6465 }
Evan Chenga8e29892007-01-19 07:51:42 +00006466 }
6467}
6468
Evan Cheng37fefc22011-08-30 19:09:48 +00006469void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6470 SDNode *Node) const {
Andrew Trick90b7b122011-10-18 19:18:52 +00006471 const MCInstrDesc *MCID = &MI->getDesc();
6472 if (!MCID->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006473 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6474 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6475 return;
6476 }
6477
Andrew Trick4815d562011-09-20 03:17:40 +00006478 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6479 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6480 // operand is still set to noreg. If needed, set the optional operand's
6481 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006482 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006483 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006484
Andrew Trick3be654f2011-09-21 02:20:46 +00006485 // Rename pseudo opcodes.
6486 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6487 if (NewOpc) {
6488 const ARMBaseInstrInfo *TII =
6489 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006490 MCID = &TII->get(NewOpc);
6491
6492 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6493 "converted opcode should be the same except for cc_out");
6494
6495 MI->setDesc(*MCID);
6496
6497 // Add the optional cc_out operand
6498 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006499 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006500 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006501
6502 // Any ARM instruction that sets the 's' bit should specify an optional
6503 // "cc_out" operand in the last operand position.
Andrew Trick90b7b122011-10-18 19:18:52 +00006504 if (!MCID->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006505 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006506 return;
6507 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006508 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6509 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006510 bool definesCPSR = false;
6511 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006512 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006513 i != e; ++i) {
6514 const MachineOperand &MO = MI->getOperand(i);
6515 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6516 definesCPSR = true;
6517 if (MO.isDead())
6518 deadCPSR = true;
6519 MI->RemoveOperand(i);
6520 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006521 }
6522 }
Andrew Trick4815d562011-09-20 03:17:40 +00006523 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006524 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006525 return;
6526 }
6527 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006528 if (deadCPSR) {
6529 assert(!MI->getOperand(ccOutIdx).getReg() &&
6530 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006531 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006532 }
Andrew Trick4815d562011-09-20 03:17:40 +00006533
Andrew Trick3be654f2011-09-21 02:20:46 +00006534 // If this instruction was defined with an optional CPSR def and its dag node
6535 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006536 MachineOperand &MO = MI->getOperand(ccOutIdx);
6537 MO.setReg(ARM::CPSR);
6538 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006539}
6540
Evan Chenga8e29892007-01-19 07:51:42 +00006541//===----------------------------------------------------------------------===//
6542// ARM Optimization Hooks
6543//===----------------------------------------------------------------------===//
6544
Chris Lattnerd1980a52009-03-12 06:52:53 +00006545static
6546SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6547 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006548 SelectionDAG &DAG = DCI.DAG;
6549 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006550 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006551 unsigned Opc = N->getOpcode();
6552 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6553 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6554 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6555 ISD::CondCode CC = ISD::SETCC_INVALID;
6556
6557 if (isSlctCC) {
6558 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6559 } else {
6560 SDValue CCOp = Slct.getOperand(0);
6561 if (CCOp.getOpcode() == ISD::SETCC)
6562 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6563 }
6564
6565 bool DoXform = false;
6566 bool InvCC = false;
6567 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6568 "Bad input!");
6569
6570 if (LHS.getOpcode() == ISD::Constant &&
6571 cast<ConstantSDNode>(LHS)->isNullValue()) {
6572 DoXform = true;
6573 } else if (CC != ISD::SETCC_INVALID &&
6574 RHS.getOpcode() == ISD::Constant &&
6575 cast<ConstantSDNode>(RHS)->isNullValue()) {
6576 std::swap(LHS, RHS);
6577 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006578 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006579 Op0.getOperand(0).getValueType();
6580 bool isInt = OpVT.isInteger();
6581 CC = ISD::getSetCCInverse(CC, isInt);
6582
6583 if (!TLI.isCondCodeLegal(CC, OpVT))
6584 return SDValue(); // Inverse operator isn't legal.
6585
6586 DoXform = true;
6587 InvCC = true;
6588 }
6589
6590 if (DoXform) {
6591 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6592 if (isSlctCC)
6593 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6594 Slct.getOperand(0), Slct.getOperand(1), CC);
6595 SDValue CCOp = Slct.getOperand(0);
6596 if (InvCC)
6597 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6598 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6599 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6600 CCOp, OtherOp, Result);
6601 }
6602 return SDValue();
6603}
6604
Eric Christopherfa6f5912011-06-29 21:10:36 +00006605// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006606// (only after legalization).
6607static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6608 TargetLowering::DAGCombinerInfo &DCI,
6609 const ARMSubtarget *Subtarget) {
6610
6611 // Only perform optimization if after legalize, and if NEON is available. We
6612 // also expected both operands to be BUILD_VECTORs.
6613 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6614 || N0.getOpcode() != ISD::BUILD_VECTOR
6615 || N1.getOpcode() != ISD::BUILD_VECTOR)
6616 return SDValue();
6617
6618 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6619 EVT VT = N->getValueType(0);
6620 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6621 return SDValue();
6622
6623 // Check that the vector operands are of the right form.
6624 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6625 // operands, where N is the size of the formed vector.
6626 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6627 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006628
6629 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006630 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006631 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006632 SDValue Vec = N0->getOperand(0)->getOperand(0);
6633 SDNode *V = Vec.getNode();
6634 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006635
Eric Christopherfa6f5912011-06-29 21:10:36 +00006636 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006637 // check to see if each of their operands are an EXTRACT_VECTOR with
6638 // the same vector and appropriate index.
6639 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6640 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6641 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006642
Tanya Lattner189531f2011-06-14 23:48:48 +00006643 SDValue ExtVec0 = N0->getOperand(i);
6644 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006645
Tanya Lattner189531f2011-06-14 23:48:48 +00006646 // First operand is the vector, verify its the same.
6647 if (V != ExtVec0->getOperand(0).getNode() ||
6648 V != ExtVec1->getOperand(0).getNode())
6649 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006650
Tanya Lattner189531f2011-06-14 23:48:48 +00006651 // Second is the constant, verify its correct.
6652 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6653 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006654
Tanya Lattner189531f2011-06-14 23:48:48 +00006655 // For the constant, we want to see all the even or all the odd.
6656 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6657 || C1->getZExtValue() != nextIndex+1)
6658 return SDValue();
6659
6660 // Increment index.
6661 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006662 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006663 return SDValue();
6664 }
6665
6666 // Create VPADDL node.
6667 SelectionDAG &DAG = DCI.DAG;
6668 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006669
6670 // Build operand list.
6671 SmallVector<SDValue, 8> Ops;
6672 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6673 TLI.getPointerTy()));
6674
6675 // Input is the vector.
6676 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006677
Tanya Lattner189531f2011-06-14 23:48:48 +00006678 // Get widened type and narrowed type.
6679 MVT widenType;
6680 unsigned numElem = VT.getVectorNumElements();
6681 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6682 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6683 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6684 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6685 default:
6686 assert(0 && "Invalid vector element type for padd optimization.");
6687 }
6688
6689 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6690 widenType, &Ops[0], Ops.size());
6691 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6692}
6693
Bob Wilson3d5792a2010-07-29 20:34:14 +00006694/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6695/// operands N0 and N1. This is a helper for PerformADDCombine that is
6696/// called with the default operands, and if that fails, with commuted
6697/// operands.
6698static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006699 TargetLowering::DAGCombinerInfo &DCI,
6700 const ARMSubtarget *Subtarget){
6701
6702 // Attempt to create vpaddl for this add.
6703 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6704 if (Result.getNode())
6705 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006706
Chris Lattnerd1980a52009-03-12 06:52:53 +00006707 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6708 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6709 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6710 if (Result.getNode()) return Result;
6711 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006712 return SDValue();
6713}
6714
Bob Wilson3d5792a2010-07-29 20:34:14 +00006715/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6716///
6717static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006718 TargetLowering::DAGCombinerInfo &DCI,
6719 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006720 SDValue N0 = N->getOperand(0);
6721 SDValue N1 = N->getOperand(1);
6722
6723 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006724 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006725 if (Result.getNode())
6726 return Result;
6727
6728 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006729 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006730}
6731
Chris Lattnerd1980a52009-03-12 06:52:53 +00006732/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006733///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006734static SDValue PerformSUBCombine(SDNode *N,
6735 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006736 SDValue N0 = N->getOperand(0);
6737 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006738
Chris Lattnerd1980a52009-03-12 06:52:53 +00006739 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6740 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6741 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6742 if (Result.getNode()) return Result;
6743 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006744
Chris Lattnerd1980a52009-03-12 06:52:53 +00006745 return SDValue();
6746}
6747
Evan Cheng463d3582011-03-31 19:38:48 +00006748/// PerformVMULCombine
6749/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6750/// special multiplier accumulator forwarding.
6751/// vmul d3, d0, d2
6752/// vmla d3, d1, d2
6753/// is faster than
6754/// vadd d3, d0, d1
6755/// vmul d3, d3, d2
6756static SDValue PerformVMULCombine(SDNode *N,
6757 TargetLowering::DAGCombinerInfo &DCI,
6758 const ARMSubtarget *Subtarget) {
6759 if (!Subtarget->hasVMLxForwarding())
6760 return SDValue();
6761
6762 SelectionDAG &DAG = DCI.DAG;
6763 SDValue N0 = N->getOperand(0);
6764 SDValue N1 = N->getOperand(1);
6765 unsigned Opcode = N0.getOpcode();
6766 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6767 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006768 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006769 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6770 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6771 return SDValue();
6772 std::swap(N0, N1);
6773 }
6774
6775 EVT VT = N->getValueType(0);
6776 DebugLoc DL = N->getDebugLoc();
6777 SDValue N00 = N0->getOperand(0);
6778 SDValue N01 = N0->getOperand(1);
6779 return DAG.getNode(Opcode, DL, VT,
6780 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6781 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6782}
6783
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006784static SDValue PerformMULCombine(SDNode *N,
6785 TargetLowering::DAGCombinerInfo &DCI,
6786 const ARMSubtarget *Subtarget) {
6787 SelectionDAG &DAG = DCI.DAG;
6788
6789 if (Subtarget->isThumb1Only())
6790 return SDValue();
6791
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006792 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6793 return SDValue();
6794
6795 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006796 if (VT.is64BitVector() || VT.is128BitVector())
6797 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006798 if (VT != MVT::i32)
6799 return SDValue();
6800
6801 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6802 if (!C)
6803 return SDValue();
6804
6805 uint64_t MulAmt = C->getZExtValue();
6806 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
6807 ShiftAmt = ShiftAmt & (32 - 1);
6808 SDValue V = N->getOperand(0);
6809 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006810
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006811 SDValue Res;
6812 MulAmt >>= ShiftAmt;
6813 if (isPowerOf2_32(MulAmt - 1)) {
6814 // (mul x, 2^N + 1) => (add (shl x, N), x)
6815 Res = DAG.getNode(ISD::ADD, DL, VT,
6816 V, DAG.getNode(ISD::SHL, DL, VT,
6817 V, DAG.getConstant(Log2_32(MulAmt-1),
6818 MVT::i32)));
6819 } else if (isPowerOf2_32(MulAmt + 1)) {
6820 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6821 Res = DAG.getNode(ISD::SUB, DL, VT,
6822 DAG.getNode(ISD::SHL, DL, VT,
6823 V, DAG.getConstant(Log2_32(MulAmt+1),
6824 MVT::i32)),
6825 V);
6826 } else
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006827 return SDValue();
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006828
6829 if (ShiftAmt != 0)
6830 Res = DAG.getNode(ISD::SHL, DL, VT, Res,
6831 DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006832
6833 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006834 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006835 return SDValue();
6836}
6837
Owen Anderson080c0922010-11-05 19:27:46 +00006838static SDValue PerformANDCombine(SDNode *N,
6839 TargetLowering::DAGCombinerInfo &DCI) {
Owen Anderson76706012011-04-05 21:48:57 +00006840
Owen Anderson080c0922010-11-05 19:27:46 +00006841 // Attempt to use immediate-form VBIC
6842 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6843 DebugLoc dl = N->getDebugLoc();
6844 EVT VT = N->getValueType(0);
6845 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846
Tanya Lattner0433b212011-04-07 15:24:20 +00006847 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6848 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006849
Owen Anderson080c0922010-11-05 19:27:46 +00006850 APInt SplatBits, SplatUndef;
6851 unsigned SplatBitSize;
6852 bool HasAnyUndefs;
6853 if (BVN &&
6854 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6855 if (SplatBitSize <= 64) {
6856 EVT VbicVT;
6857 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6858 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006859 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006860 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006861 if (Val.getNode()) {
6862 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006863 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00006864 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006865 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00006866 }
6867 }
6868 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006869
Owen Anderson080c0922010-11-05 19:27:46 +00006870 return SDValue();
6871}
6872
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006873/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
6874static SDValue PerformORCombine(SDNode *N,
6875 TargetLowering::DAGCombinerInfo &DCI,
6876 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00006877 // Attempt to use immediate-form VORR
6878 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6879 DebugLoc dl = N->getDebugLoc();
6880 EVT VT = N->getValueType(0);
6881 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006882
Tanya Lattner0433b212011-04-07 15:24:20 +00006883 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6884 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006885
Owen Anderson60f48702010-11-03 23:15:26 +00006886 APInt SplatBits, SplatUndef;
6887 unsigned SplatBitSize;
6888 bool HasAnyUndefs;
6889 if (BVN && Subtarget->hasNEON() &&
6890 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6891 if (SplatBitSize <= 64) {
6892 EVT VorrVT;
6893 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6894 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006895 DAG, VorrVT, VT.is128BitVector(),
6896 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00006897 if (Val.getNode()) {
6898 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006899 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00006900 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006901 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00006902 }
6903 }
6904 }
6905
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006906 SDValue N0 = N->getOperand(0);
6907 if (N0.getOpcode() != ISD::AND)
6908 return SDValue();
6909 SDValue N1 = N->getOperand(1);
6910
6911 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
6912 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
6913 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
6914 APInt SplatUndef;
6915 unsigned SplatBitSize;
6916 bool HasAnyUndefs;
6917
6918 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
6919 APInt SplatBits0;
6920 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
6921 HasAnyUndefs) && !HasAnyUndefs) {
6922 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
6923 APInt SplatBits1;
6924 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
6925 HasAnyUndefs) && !HasAnyUndefs &&
6926 SplatBits0 == ~SplatBits1) {
6927 // Canonicalize the vector type to make instruction selection simpler.
6928 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
6929 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
6930 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00006931 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00006932 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
6933 }
6934 }
6935 }
6936
Jim Grosbach54238562010-07-17 03:30:54 +00006937 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
6938 // reasonable.
6939
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006940 // BFI is only available on V6T2+
6941 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
6942 return SDValue();
6943
Jim Grosbach54238562010-07-17 03:30:54 +00006944 DebugLoc DL = N->getDebugLoc();
6945 // 1) or (and A, mask), val => ARMbfi A, val, mask
6946 // iff (val & mask) == val
6947 //
6948 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
6949 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006950 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006951 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00006952 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00006953 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006954
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006955 if (VT != MVT::i32)
6956 return SDValue();
6957
Evan Cheng30fb13f2010-12-13 20:32:54 +00006958 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00006959
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006960 // The value and the mask need to be constants so we can verify this is
6961 // actually a bitfield set. If the mask is 0xffff, we can do better
6962 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00006963 SDValue MaskOp = N0.getOperand(1);
6964 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
6965 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006966 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006967 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006968 if (Mask == 0xffff)
6969 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006970 SDValue Res;
6971 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006972 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
6973 if (N1C) {
6974 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006975 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00006976 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006977
Evan Chenga9688c42010-12-11 04:11:38 +00006978 if (ARM::isBitFieldInvertedMask(Mask)) {
6979 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00006980
Evan Cheng30fb13f2010-12-13 20:32:54 +00006981 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00006982 DAG.getConstant(Val, MVT::i32),
6983 DAG.getConstant(Mask, MVT::i32));
6984
6985 // Do not add new nodes to DAG combiner worklist.
6986 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00006987 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00006988 }
Jim Grosbach54238562010-07-17 03:30:54 +00006989 } else if (N1.getOpcode() == ISD::AND) {
6990 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00006991 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
6992 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00006993 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00006994 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00006995
Eric Christopher29aeed12011-03-26 01:21:03 +00006996 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
6997 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00006998 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00006999 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007000 // The pack halfword instruction works better for masks that fit it,
7001 // so use that when it's available.
7002 if (Subtarget->hasT2ExtractPack() &&
7003 (Mask == 0xffff || Mask == 0xffff0000))
7004 return SDValue();
7005 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007006 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007007 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007008 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007009 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007010 DAG.getConstant(Mask, MVT::i32));
7011 // Do not add new nodes to DAG combiner worklist.
7012 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007013 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007014 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007015 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007016 // The pack halfword instruction works better for masks that fit it,
7017 // so use that when it's available.
7018 if (Subtarget->hasT2ExtractPack() &&
7019 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7020 return SDValue();
7021 // 2b
7022 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007023 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007024 DAG.getConstant(lsb, MVT::i32));
7025 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007026 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007027 // Do not add new nodes to DAG combiner worklist.
7028 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007029 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007030 }
7031 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007032
Evan Cheng30fb13f2010-12-13 20:32:54 +00007033 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7034 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7035 ARM::isBitFieldInvertedMask(~Mask)) {
7036 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7037 // where lsb(mask) == #shamt and masked bits of B are known zero.
7038 SDValue ShAmt = N00.getOperand(1);
7039 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7040 unsigned LSB = CountTrailingZeros_32(Mask);
7041 if (ShAmtC != LSB)
7042 return SDValue();
7043
7044 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7045 DAG.getConstant(~Mask, MVT::i32));
7046
7047 // Do not add new nodes to DAG combiner worklist.
7048 DCI.CombineTo(N, Res, false);
7049 }
7050
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007051 return SDValue();
7052}
7053
Evan Chengbf188ae2011-06-15 01:12:31 +00007054/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7055/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007056static SDValue PerformBFICombine(SDNode *N,
7057 TargetLowering::DAGCombinerInfo &DCI) {
7058 SDValue N1 = N->getOperand(1);
7059 if (N1.getOpcode() == ISD::AND) {
7060 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7061 if (!N11C)
7062 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007063 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7064 unsigned LSB = CountTrailingZeros_32(~InvMask);
7065 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7066 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007067 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007068 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007069 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7070 N->getOperand(0), N1.getOperand(0),
7071 N->getOperand(2));
7072 }
7073 return SDValue();
7074}
7075
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007076/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7077/// ARMISD::VMOVRRD.
7078static SDValue PerformVMOVRRDCombine(SDNode *N,
7079 TargetLowering::DAGCombinerInfo &DCI) {
7080 // vmovrrd(vmovdrr x, y) -> x,y
7081 SDValue InDouble = N->getOperand(0);
7082 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7083 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007084
7085 // vmovrrd(load f64) -> (load i32), (load i32)
7086 SDNode *InNode = InDouble.getNode();
7087 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7088 InNode->getValueType(0) == MVT::f64 &&
7089 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7090 !cast<LoadSDNode>(InNode)->isVolatile()) {
7091 // TODO: Should this be done for non-FrameIndex operands?
7092 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7093
7094 SelectionDAG &DAG = DCI.DAG;
7095 DebugLoc DL = LD->getDebugLoc();
7096 SDValue BasePtr = LD->getBasePtr();
7097 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7098 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007099 LD->isNonTemporal(), LD->isInvariant(),
7100 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007101
7102 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7103 DAG.getConstant(4, MVT::i32));
7104 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7105 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007106 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007107 std::min(4U, LD->getAlignment() / 2));
7108
7109 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7110 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7111 DCI.RemoveFromWorklist(LD);
7112 DAG.DeleteNode(LD);
7113 return Result;
7114 }
7115
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007116 return SDValue();
7117}
7118
7119/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7120/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7121static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7122 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7123 SDValue Op0 = N->getOperand(0);
7124 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007125 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007126 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007127 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007128 Op1 = Op1.getOperand(0);
7129 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7130 Op0.getNode() == Op1.getNode() &&
7131 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007132 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007133 N->getValueType(0), Op0.getOperand(0));
7134 return SDValue();
7135}
7136
Bob Wilson31600902010-12-21 06:43:19 +00007137/// PerformSTORECombine - Target-specific dag combine xforms for
7138/// ISD::STORE.
7139static SDValue PerformSTORECombine(SDNode *N,
7140 TargetLowering::DAGCombinerInfo &DCI) {
7141 // Bitcast an i64 store extracted from a vector to f64.
7142 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7143 StoreSDNode *St = cast<StoreSDNode>(N);
7144 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007145 if (!ISD::isNormalStore(St) || St->isVolatile())
7146 return SDValue();
7147
7148 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7149 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7150 SelectionDAG &DAG = DCI.DAG;
7151 DebugLoc DL = St->getDebugLoc();
7152 SDValue BasePtr = St->getBasePtr();
7153 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7154 StVal.getNode()->getOperand(0), BasePtr,
7155 St->getPointerInfo(), St->isVolatile(),
7156 St->isNonTemporal(), St->getAlignment());
7157
7158 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7159 DAG.getConstant(4, MVT::i32));
7160 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7161 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7162 St->isNonTemporal(),
7163 std::min(4U, St->getAlignment() / 2));
7164 }
7165
7166 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007167 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7168 return SDValue();
7169
7170 SelectionDAG &DAG = DCI.DAG;
7171 DebugLoc dl = StVal.getDebugLoc();
7172 SDValue IntVec = StVal.getOperand(0);
7173 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7174 IntVec.getValueType().getVectorNumElements());
7175 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7176 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7177 Vec, StVal.getOperand(1));
7178 dl = N->getDebugLoc();
7179 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7180 // Make the DAGCombiner fold the bitcasts.
7181 DCI.AddToWorklist(Vec.getNode());
7182 DCI.AddToWorklist(ExtElt.getNode());
7183 DCI.AddToWorklist(V.getNode());
7184 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7185 St->getPointerInfo(), St->isVolatile(),
7186 St->isNonTemporal(), St->getAlignment(),
7187 St->getTBAAInfo());
7188}
7189
7190/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7191/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7192/// i64 vector to have f64 elements, since the value can then be loaded
7193/// directly into a VFP register.
7194static bool hasNormalLoadOperand(SDNode *N) {
7195 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7196 for (unsigned i = 0; i < NumElts; ++i) {
7197 SDNode *Elt = N->getOperand(i).getNode();
7198 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7199 return true;
7200 }
7201 return false;
7202}
7203
Bob Wilson75f02882010-09-17 22:59:05 +00007204/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7205/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007206static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7207 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007208 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7209 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7210 // into a pair of GPRs, which is fine when the value is used as a scalar,
7211 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007212 SelectionDAG &DAG = DCI.DAG;
7213 if (N->getNumOperands() == 2) {
7214 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7215 if (RV.getNode())
7216 return RV;
7217 }
Bob Wilson75f02882010-09-17 22:59:05 +00007218
Bob Wilson31600902010-12-21 06:43:19 +00007219 // Load i64 elements as f64 values so that type legalization does not split
7220 // them up into i32 values.
7221 EVT VT = N->getValueType(0);
7222 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7223 return SDValue();
7224 DebugLoc dl = N->getDebugLoc();
7225 SmallVector<SDValue, 8> Ops;
7226 unsigned NumElts = VT.getVectorNumElements();
7227 for (unsigned i = 0; i < NumElts; ++i) {
7228 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7229 Ops.push_back(V);
7230 // Make the DAGCombiner fold the bitcast.
7231 DCI.AddToWorklist(V.getNode());
7232 }
7233 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7234 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7235 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7236}
7237
7238/// PerformInsertEltCombine - Target-specific dag combine xforms for
7239/// ISD::INSERT_VECTOR_ELT.
7240static SDValue PerformInsertEltCombine(SDNode *N,
7241 TargetLowering::DAGCombinerInfo &DCI) {
7242 // Bitcast an i64 load inserted into a vector to f64.
7243 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7244 EVT VT = N->getValueType(0);
7245 SDNode *Elt = N->getOperand(1).getNode();
7246 if (VT.getVectorElementType() != MVT::i64 ||
7247 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7248 return SDValue();
7249
7250 SelectionDAG &DAG = DCI.DAG;
7251 DebugLoc dl = N->getDebugLoc();
7252 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7253 VT.getVectorNumElements());
7254 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7255 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7256 // Make the DAGCombiner fold the bitcasts.
7257 DCI.AddToWorklist(Vec.getNode());
7258 DCI.AddToWorklist(V.getNode());
7259 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7260 Vec, V, N->getOperand(2));
7261 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007262}
7263
Bob Wilsonf20700c2010-10-27 20:38:28 +00007264/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7265/// ISD::VECTOR_SHUFFLE.
7266static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7267 // The LLVM shufflevector instruction does not require the shuffle mask
7268 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7269 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7270 // operands do not match the mask length, they are extended by concatenating
7271 // them with undef vectors. That is probably the right thing for other
7272 // targets, but for NEON it is better to concatenate two double-register
7273 // size vector operands into a single quad-register size vector. Do that
7274 // transformation here:
7275 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7276 // shuffle(concat(v1, v2), undef)
7277 SDValue Op0 = N->getOperand(0);
7278 SDValue Op1 = N->getOperand(1);
7279 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7280 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7281 Op0.getNumOperands() != 2 ||
7282 Op1.getNumOperands() != 2)
7283 return SDValue();
7284 SDValue Concat0Op1 = Op0.getOperand(1);
7285 SDValue Concat1Op1 = Op1.getOperand(1);
7286 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7287 Concat1Op1.getOpcode() != ISD::UNDEF)
7288 return SDValue();
7289 // Skip the transformation if any of the types are illegal.
7290 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7291 EVT VT = N->getValueType(0);
7292 if (!TLI.isTypeLegal(VT) ||
7293 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7294 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7295 return SDValue();
7296
7297 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7298 Op0.getOperand(0), Op1.getOperand(0));
7299 // Translate the shuffle mask.
7300 SmallVector<int, 16> NewMask;
7301 unsigned NumElts = VT.getVectorNumElements();
7302 unsigned HalfElts = NumElts/2;
7303 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7304 for (unsigned n = 0; n < NumElts; ++n) {
7305 int MaskElt = SVN->getMaskElt(n);
7306 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007307 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007308 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007309 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007310 NewElt = HalfElts + MaskElt - NumElts;
7311 NewMask.push_back(NewElt);
7312 }
7313 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7314 DAG.getUNDEF(VT), NewMask.data());
7315}
7316
Bob Wilson1c3ef902011-02-07 17:43:21 +00007317/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7318/// NEON load/store intrinsics to merge base address updates.
7319static SDValue CombineBaseUpdate(SDNode *N,
7320 TargetLowering::DAGCombinerInfo &DCI) {
7321 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7322 return SDValue();
7323
7324 SelectionDAG &DAG = DCI.DAG;
7325 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7326 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7327 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7328 SDValue Addr = N->getOperand(AddrOpIdx);
7329
7330 // Search for a use of the address operand that is an increment.
7331 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7332 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7333 SDNode *User = *UI;
7334 if (User->getOpcode() != ISD::ADD ||
7335 UI.getUse().getResNo() != Addr.getResNo())
7336 continue;
7337
7338 // Check that the add is independent of the load/store. Otherwise, folding
7339 // it would create a cycle.
7340 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7341 continue;
7342
7343 // Find the new opcode for the updating load/store.
7344 bool isLoad = true;
7345 bool isLaneOp = false;
7346 unsigned NewOpc = 0;
7347 unsigned NumVecs = 0;
7348 if (isIntrinsic) {
7349 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7350 switch (IntNo) {
7351 default: assert(0 && "unexpected intrinsic for Neon base update");
7352 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7353 NumVecs = 1; break;
7354 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7355 NumVecs = 2; break;
7356 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7357 NumVecs = 3; break;
7358 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7359 NumVecs = 4; break;
7360 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7361 NumVecs = 2; isLaneOp = true; break;
7362 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7363 NumVecs = 3; isLaneOp = true; break;
7364 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7365 NumVecs = 4; isLaneOp = true; break;
7366 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7367 NumVecs = 1; isLoad = false; break;
7368 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7369 NumVecs = 2; isLoad = false; break;
7370 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7371 NumVecs = 3; isLoad = false; break;
7372 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7373 NumVecs = 4; isLoad = false; break;
7374 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7375 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7376 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7377 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7378 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7379 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7380 }
7381 } else {
7382 isLaneOp = true;
7383 switch (N->getOpcode()) {
7384 default: assert(0 && "unexpected opcode for Neon base update");
7385 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7386 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7387 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7388 }
7389 }
7390
7391 // Find the size of memory referenced by the load/store.
7392 EVT VecTy;
7393 if (isLoad)
7394 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007395 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007396 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7397 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7398 if (isLaneOp)
7399 NumBytes /= VecTy.getVectorNumElements();
7400
7401 // If the increment is a constant, it must match the memory ref size.
7402 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7403 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7404 uint64_t IncVal = CInc->getZExtValue();
7405 if (IncVal != NumBytes)
7406 continue;
7407 } else if (NumBytes >= 3 * 16) {
7408 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7409 // separate instructions that make it harder to use a non-constant update.
7410 continue;
7411 }
7412
7413 // Create the new updating load/store node.
7414 EVT Tys[6];
7415 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7416 unsigned n;
7417 for (n = 0; n < NumResultVecs; ++n)
7418 Tys[n] = VecTy;
7419 Tys[n++] = MVT::i32;
7420 Tys[n] = MVT::Other;
7421 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7422 SmallVector<SDValue, 8> Ops;
7423 Ops.push_back(N->getOperand(0)); // incoming chain
7424 Ops.push_back(N->getOperand(AddrOpIdx));
7425 Ops.push_back(Inc);
7426 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7427 Ops.push_back(N->getOperand(i));
7428 }
7429 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7430 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7431 Ops.data(), Ops.size(),
7432 MemInt->getMemoryVT(),
7433 MemInt->getMemOperand());
7434
7435 // Update the uses.
7436 std::vector<SDValue> NewResults;
7437 for (unsigned i = 0; i < NumResultVecs; ++i) {
7438 NewResults.push_back(SDValue(UpdN.getNode(), i));
7439 }
7440 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7441 DCI.CombineTo(N, NewResults);
7442 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7443
7444 break;
Owen Anderson76706012011-04-05 21:48:57 +00007445 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007446 return SDValue();
7447}
7448
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007449/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7450/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7451/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7452/// return true.
7453static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7454 SelectionDAG &DAG = DCI.DAG;
7455 EVT VT = N->getValueType(0);
7456 // vldN-dup instructions only support 64-bit vectors for N > 1.
7457 if (!VT.is64BitVector())
7458 return false;
7459
7460 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7461 SDNode *VLD = N->getOperand(0).getNode();
7462 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7463 return false;
7464 unsigned NumVecs = 0;
7465 unsigned NewOpc = 0;
7466 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7467 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7468 NumVecs = 2;
7469 NewOpc = ARMISD::VLD2DUP;
7470 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7471 NumVecs = 3;
7472 NewOpc = ARMISD::VLD3DUP;
7473 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7474 NumVecs = 4;
7475 NewOpc = ARMISD::VLD4DUP;
7476 } else {
7477 return false;
7478 }
7479
7480 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7481 // numbers match the load.
7482 unsigned VLDLaneNo =
7483 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7484 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7485 UI != UE; ++UI) {
7486 // Ignore uses of the chain result.
7487 if (UI.getUse().getResNo() == NumVecs)
7488 continue;
7489 SDNode *User = *UI;
7490 if (User->getOpcode() != ARMISD::VDUPLANE ||
7491 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7492 return false;
7493 }
7494
7495 // Create the vldN-dup node.
7496 EVT Tys[5];
7497 unsigned n;
7498 for (n = 0; n < NumVecs; ++n)
7499 Tys[n] = VT;
7500 Tys[n] = MVT::Other;
7501 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7502 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7503 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7504 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7505 Ops, 2, VLDMemInt->getMemoryVT(),
7506 VLDMemInt->getMemOperand());
7507
7508 // Update the uses.
7509 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7510 UI != UE; ++UI) {
7511 unsigned ResNo = UI.getUse().getResNo();
7512 // Ignore uses of the chain result.
7513 if (ResNo == NumVecs)
7514 continue;
7515 SDNode *User = *UI;
7516 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7517 }
7518
7519 // Now the vldN-lane intrinsic is dead except for its chain result.
7520 // Update uses of the chain.
7521 std::vector<SDValue> VLDDupResults;
7522 for (unsigned n = 0; n < NumVecs; ++n)
7523 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7524 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7525 DCI.CombineTo(VLD, VLDDupResults);
7526
7527 return true;
7528}
7529
Bob Wilson9e82bf12010-07-14 01:22:12 +00007530/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7531/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007532static SDValue PerformVDUPLANECombine(SDNode *N,
7533 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007534 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007535
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007536 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7537 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7538 if (CombineVLDDUP(N, DCI))
7539 return SDValue(N, 0);
7540
7541 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7542 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007543 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007544 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007545 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007546 return SDValue();
7547
7548 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7549 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7550 // The canonical VMOV for a zero vector uses a 32-bit element size.
7551 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7552 unsigned EltBits;
7553 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7554 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007555 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007556 if (EltSize > VT.getVectorElementType().getSizeInBits())
7557 return SDValue();
7558
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007559 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007560}
7561
Eric Christopherfa6f5912011-06-29 21:10:36 +00007562// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007563// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7564static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7565{
Chad Rosier118c9a02011-06-28 17:26:57 +00007566 integerPart cN;
7567 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007568 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7569 I != E; I++) {
7570 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7571 if (!C)
7572 return false;
7573
Eric Christopherfa6f5912011-06-29 21:10:36 +00007574 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007575 APFloat APF = C->getValueAPF();
7576 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7577 != APFloat::opOK || !isExact)
7578 return false;
7579
7580 c0 = (I == 0) ? cN : c0;
7581 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7582 return false;
7583 }
7584 C = c0;
7585 return true;
7586}
7587
7588/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7589/// can replace combinations of VMUL and VCVT (floating-point to integer)
7590/// when the VMUL has a constant operand that is a power of 2.
7591///
7592/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7593/// vmul.f32 d16, d17, d16
7594/// vcvt.s32.f32 d16, d16
7595/// becomes:
7596/// vcvt.s32.f32 d16, d16, #3
7597static SDValue PerformVCVTCombine(SDNode *N,
7598 TargetLowering::DAGCombinerInfo &DCI,
7599 const ARMSubtarget *Subtarget) {
7600 SelectionDAG &DAG = DCI.DAG;
7601 SDValue Op = N->getOperand(0);
7602
7603 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7604 Op.getOpcode() != ISD::FMUL)
7605 return SDValue();
7606
7607 uint64_t C;
7608 SDValue N0 = Op->getOperand(0);
7609 SDValue ConstVec = Op->getOperand(1);
7610 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7611
Eric Christopherfa6f5912011-06-29 21:10:36 +00007612 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007613 !isConstVecPow2(ConstVec, isSigned, C))
7614 return SDValue();
7615
7616 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7617 Intrinsic::arm_neon_vcvtfp2fxu;
7618 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7619 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007620 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007621 DAG.getConstant(Log2_64(C), MVT::i32));
7622}
7623
7624/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7625/// can replace combinations of VCVT (integer to floating-point) and VDIV
7626/// when the VDIV has a constant operand that is a power of 2.
7627///
7628/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7629/// vcvt.f32.s32 d16, d16
7630/// vdiv.f32 d16, d17, d16
7631/// becomes:
7632/// vcvt.f32.s32 d16, d16, #3
7633static SDValue PerformVDIVCombine(SDNode *N,
7634 TargetLowering::DAGCombinerInfo &DCI,
7635 const ARMSubtarget *Subtarget) {
7636 SelectionDAG &DAG = DCI.DAG;
7637 SDValue Op = N->getOperand(0);
7638 unsigned OpOpcode = Op.getNode()->getOpcode();
7639
7640 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7641 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7642 return SDValue();
7643
7644 uint64_t C;
7645 SDValue ConstVec = N->getOperand(1);
7646 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7647
7648 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7649 !isConstVecPow2(ConstVec, isSigned, C))
7650 return SDValue();
7651
Eric Christopherfa6f5912011-06-29 21:10:36 +00007652 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007653 Intrinsic::arm_neon_vcvtfxu2fp;
7654 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7655 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007656 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007657 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7658}
7659
7660/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007661/// operand of a vector shift operation, where all the elements of the
7662/// build_vector must have the same constant integer value.
7663static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7664 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007665 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007666 Op = Op.getOperand(0);
7667 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7668 APInt SplatBits, SplatUndef;
7669 unsigned SplatBitSize;
7670 bool HasAnyUndefs;
7671 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7672 HasAnyUndefs, ElementBits) ||
7673 SplatBitSize > ElementBits)
7674 return false;
7675 Cnt = SplatBits.getSExtValue();
7676 return true;
7677}
7678
7679/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7680/// operand of a vector shift left operation. That value must be in the range:
7681/// 0 <= Value < ElementBits for a left shift; or
7682/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007683static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007684 assert(VT.isVector() && "vector shift count is not a vector type");
7685 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7686 if (! getVShiftImm(Op, ElementBits, Cnt))
7687 return false;
7688 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7689}
7690
7691/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7692/// operand of a vector shift right operation. For a shift opcode, the value
7693/// is positive, but for an intrinsic the value count must be negative. The
7694/// absolute value must be in the range:
7695/// 1 <= |Value| <= ElementBits for a right shift; or
7696/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007697static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007698 int64_t &Cnt) {
7699 assert(VT.isVector() && "vector shift count is not a vector type");
7700 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7701 if (! getVShiftImm(Op, ElementBits, Cnt))
7702 return false;
7703 if (isIntrinsic)
7704 Cnt = -Cnt;
7705 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7706}
7707
7708/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7709static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7710 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7711 switch (IntNo) {
7712 default:
7713 // Don't do anything for most intrinsics.
7714 break;
7715
7716 // Vector shifts: check for immediate versions and lower them.
7717 // Note: This is done during DAG combining instead of DAG legalizing because
7718 // the build_vectors for 64-bit vector element shift counts are generally
7719 // not legal, and it is hard to see their values after they get legalized to
7720 // loads from a constant pool.
7721 case Intrinsic::arm_neon_vshifts:
7722 case Intrinsic::arm_neon_vshiftu:
7723 case Intrinsic::arm_neon_vshiftls:
7724 case Intrinsic::arm_neon_vshiftlu:
7725 case Intrinsic::arm_neon_vshiftn:
7726 case Intrinsic::arm_neon_vrshifts:
7727 case Intrinsic::arm_neon_vrshiftu:
7728 case Intrinsic::arm_neon_vrshiftn:
7729 case Intrinsic::arm_neon_vqshifts:
7730 case Intrinsic::arm_neon_vqshiftu:
7731 case Intrinsic::arm_neon_vqshiftsu:
7732 case Intrinsic::arm_neon_vqshiftns:
7733 case Intrinsic::arm_neon_vqshiftnu:
7734 case Intrinsic::arm_neon_vqshiftnsu:
7735 case Intrinsic::arm_neon_vqrshiftns:
7736 case Intrinsic::arm_neon_vqrshiftnu:
7737 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007738 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007739 int64_t Cnt;
7740 unsigned VShiftOpc = 0;
7741
7742 switch (IntNo) {
7743 case Intrinsic::arm_neon_vshifts:
7744 case Intrinsic::arm_neon_vshiftu:
7745 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7746 VShiftOpc = ARMISD::VSHL;
7747 break;
7748 }
7749 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7750 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7751 ARMISD::VSHRs : ARMISD::VSHRu);
7752 break;
7753 }
7754 return SDValue();
7755
7756 case Intrinsic::arm_neon_vshiftls:
7757 case Intrinsic::arm_neon_vshiftlu:
7758 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7759 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007760 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007761
7762 case Intrinsic::arm_neon_vrshifts:
7763 case Intrinsic::arm_neon_vrshiftu:
7764 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7765 break;
7766 return SDValue();
7767
7768 case Intrinsic::arm_neon_vqshifts:
7769 case Intrinsic::arm_neon_vqshiftu:
7770 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7771 break;
7772 return SDValue();
7773
7774 case Intrinsic::arm_neon_vqshiftsu:
7775 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7776 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007777 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007778
7779 case Intrinsic::arm_neon_vshiftn:
7780 case Intrinsic::arm_neon_vrshiftn:
7781 case Intrinsic::arm_neon_vqshiftns:
7782 case Intrinsic::arm_neon_vqshiftnu:
7783 case Intrinsic::arm_neon_vqshiftnsu:
7784 case Intrinsic::arm_neon_vqrshiftns:
7785 case Intrinsic::arm_neon_vqrshiftnu:
7786 case Intrinsic::arm_neon_vqrshiftnsu:
7787 // Narrowing shifts require an immediate right shift.
7788 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7789 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007790 llvm_unreachable("invalid shift count for narrowing vector shift "
7791 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007792
7793 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007794 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007795 }
7796
7797 switch (IntNo) {
7798 case Intrinsic::arm_neon_vshifts:
7799 case Intrinsic::arm_neon_vshiftu:
7800 // Opcode already set above.
7801 break;
7802 case Intrinsic::arm_neon_vshiftls:
7803 case Intrinsic::arm_neon_vshiftlu:
7804 if (Cnt == VT.getVectorElementType().getSizeInBits())
7805 VShiftOpc = ARMISD::VSHLLi;
7806 else
7807 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7808 ARMISD::VSHLLs : ARMISD::VSHLLu);
7809 break;
7810 case Intrinsic::arm_neon_vshiftn:
7811 VShiftOpc = ARMISD::VSHRN; break;
7812 case Intrinsic::arm_neon_vrshifts:
7813 VShiftOpc = ARMISD::VRSHRs; break;
7814 case Intrinsic::arm_neon_vrshiftu:
7815 VShiftOpc = ARMISD::VRSHRu; break;
7816 case Intrinsic::arm_neon_vrshiftn:
7817 VShiftOpc = ARMISD::VRSHRN; break;
7818 case Intrinsic::arm_neon_vqshifts:
7819 VShiftOpc = ARMISD::VQSHLs; break;
7820 case Intrinsic::arm_neon_vqshiftu:
7821 VShiftOpc = ARMISD::VQSHLu; break;
7822 case Intrinsic::arm_neon_vqshiftsu:
7823 VShiftOpc = ARMISD::VQSHLsu; break;
7824 case Intrinsic::arm_neon_vqshiftns:
7825 VShiftOpc = ARMISD::VQSHRNs; break;
7826 case Intrinsic::arm_neon_vqshiftnu:
7827 VShiftOpc = ARMISD::VQSHRNu; break;
7828 case Intrinsic::arm_neon_vqshiftnsu:
7829 VShiftOpc = ARMISD::VQSHRNsu; break;
7830 case Intrinsic::arm_neon_vqrshiftns:
7831 VShiftOpc = ARMISD::VQRSHRNs; break;
7832 case Intrinsic::arm_neon_vqrshiftnu:
7833 VShiftOpc = ARMISD::VQRSHRNu; break;
7834 case Intrinsic::arm_neon_vqrshiftnsu:
7835 VShiftOpc = ARMISD::VQRSHRNsu; break;
7836 }
7837
7838 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007839 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007840 }
7841
7842 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00007843 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007844 int64_t Cnt;
7845 unsigned VShiftOpc = 0;
7846
7847 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
7848 VShiftOpc = ARMISD::VSLI;
7849 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
7850 VShiftOpc = ARMISD::VSRI;
7851 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00007852 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007853 }
7854
7855 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
7856 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00007857 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007858 }
7859
7860 case Intrinsic::arm_neon_vqrshifts:
7861 case Intrinsic::arm_neon_vqrshiftu:
7862 // No immediate versions of these to check for.
7863 break;
7864 }
7865
7866 return SDValue();
7867}
7868
7869/// PerformShiftCombine - Checks for immediate versions of vector shifts and
7870/// lowers them. As with the vector shift intrinsics, this is done during DAG
7871/// combining instead of DAG legalizing because the build_vectors for 64-bit
7872/// vector element shift counts are generally not legal, and it is hard to see
7873/// their values after they get legalized to loads from a constant pool.
7874static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
7875 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00007876 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00007877
7878 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00007879 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7880 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00007881 return SDValue();
7882
7883 assert(ST->hasNEON() && "unexpected vector shift");
7884 int64_t Cnt;
7885
7886 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007887 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007888
7889 case ISD::SHL:
7890 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
7891 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007892 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007893 break;
7894
7895 case ISD::SRA:
7896 case ISD::SRL:
7897 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
7898 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
7899 ARMISD::VSHRs : ARMISD::VSHRu);
7900 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00007901 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00007902 }
7903 }
7904 return SDValue();
7905}
7906
7907/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
7908/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
7909static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
7910 const ARMSubtarget *ST) {
7911 SDValue N0 = N->getOperand(0);
7912
7913 // Check for sign- and zero-extensions of vector extract operations of 8-
7914 // and 16-bit vector elements. NEON supports these directly. They are
7915 // handled during DAG combining because type legalization will promote them
7916 // to 32-bit types and it is messy to recognize the operations after that.
7917 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7918 SDValue Vec = N0.getOperand(0);
7919 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00007920 EVT VT = N->getValueType(0);
7921 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7923
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 if (VT == MVT::i32 &&
7925 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00007926 TLI.isTypeLegal(Vec.getValueType()) &&
7927 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007928
7929 unsigned Opc = 0;
7930 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007931 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00007932 case ISD::SIGN_EXTEND:
7933 Opc = ARMISD::VGETLANEs;
7934 break;
7935 case ISD::ZERO_EXTEND:
7936 case ISD::ANY_EXTEND:
7937 Opc = ARMISD::VGETLANEu;
7938 break;
7939 }
7940 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
7941 }
7942 }
7943
7944 return SDValue();
7945}
7946
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007947/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
7948/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
7949static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
7950 const ARMSubtarget *ST) {
7951 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00007952 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007953 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
7954 // a NaN; only do the transformation when it matches that behavior.
7955
7956 // For now only do this when using NEON for FP operations; if using VFP, it
7957 // is not obvious that the benefit outweighs the cost of switching to the
7958 // NEON pipeline.
7959 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
7960 N->getValueType(0) != MVT::f32)
7961 return SDValue();
7962
7963 SDValue CondLHS = N->getOperand(0);
7964 SDValue CondRHS = N->getOperand(1);
7965 SDValue LHS = N->getOperand(2);
7966 SDValue RHS = N->getOperand(3);
7967 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
7968
7969 unsigned Opcode = 0;
7970 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00007971 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007972 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00007973 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007974 IsReversed = true ; // x CC y ? y : x
7975 } else {
7976 return SDValue();
7977 }
7978
Bob Wilsone742bb52010-02-24 22:15:53 +00007979 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007980 switch (CC) {
7981 default: break;
7982 case ISD::SETOLT:
7983 case ISD::SETOLE:
7984 case ISD::SETLT:
7985 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00007986 case ISD::SETULT:
7987 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00007988 // If LHS is NaN, an ordered comparison will be false and the result will
7989 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
7990 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
7991 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
7992 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
7993 break;
7994 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
7995 // will return -0, so vmin can only be used for unsafe math or if one of
7996 // the operands is known to be nonzero.
7997 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
7998 !UnsafeFPMath &&
7999 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8000 break;
8001 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008002 break;
8003
8004 case ISD::SETOGT:
8005 case ISD::SETOGE:
8006 case ISD::SETGT:
8007 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008008 case ISD::SETUGT:
8009 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008010 // If LHS is NaN, an ordered comparison will be false and the result will
8011 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8012 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8013 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8014 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8015 break;
8016 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8017 // will return +0, so vmax can only be used for unsafe math or if one of
8018 // the operands is known to be nonzero.
8019 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
8020 !UnsafeFPMath &&
8021 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8022 break;
8023 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008024 break;
8025 }
8026
8027 if (!Opcode)
8028 return SDValue();
8029 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8030}
8031
Evan Chenge721f5c2011-07-13 00:42:17 +00008032/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8033SDValue
8034ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8035 SDValue Cmp = N->getOperand(4);
8036 if (Cmp.getOpcode() != ARMISD::CMPZ)
8037 // Only looking at EQ and NE cases.
8038 return SDValue();
8039
8040 EVT VT = N->getValueType(0);
8041 DebugLoc dl = N->getDebugLoc();
8042 SDValue LHS = Cmp.getOperand(0);
8043 SDValue RHS = Cmp.getOperand(1);
8044 SDValue FalseVal = N->getOperand(0);
8045 SDValue TrueVal = N->getOperand(1);
8046 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008047 ARMCC::CondCodes CC =
8048 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008049
8050 // Simplify
8051 // mov r1, r0
8052 // cmp r1, x
8053 // mov r0, y
8054 // moveq r0, x
8055 // to
8056 // cmp r0, x
8057 // movne r0, y
8058 //
8059 // mov r1, r0
8060 // cmp r1, x
8061 // mov r0, x
8062 // movne r0, y
8063 // to
8064 // cmp r0, x
8065 // movne r0, y
8066 /// FIXME: Turn this into a target neutral optimization?
8067 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008068 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008069 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8070 N->getOperand(3), Cmp);
8071 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8072 SDValue ARMcc;
8073 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8074 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8075 N->getOperand(3), NewCmp);
8076 }
8077
8078 if (Res.getNode()) {
8079 APInt KnownZero, KnownOne;
8080 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8081 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8082 // Capture demanded bits information that would be otherwise lost.
8083 if (KnownZero == 0xfffffffe)
8084 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8085 DAG.getValueType(MVT::i1));
8086 else if (KnownZero == 0xffffff00)
8087 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8088 DAG.getValueType(MVT::i8));
8089 else if (KnownZero == 0xffff0000)
8090 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8091 DAG.getValueType(MVT::i16));
8092 }
8093
8094 return Res;
8095}
8096
Dan Gohman475871a2008-07-27 21:46:04 +00008097SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008098 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008099 switch (N->getOpcode()) {
8100 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008101 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008102 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008103 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008104 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Owen Anderson080c0922010-11-05 19:27:46 +00008105 case ISD::AND: return PerformANDCombine(N, DCI);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008106 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008107 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008108 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008109 case ISD::STORE: return PerformSTORECombine(N, DCI);
8110 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8111 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008112 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008113 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008114 case ISD::FP_TO_SINT:
8115 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8116 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008117 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008118 case ISD::SHL:
8119 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008120 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008121 case ISD::SIGN_EXTEND:
8122 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008123 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8124 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008125 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008126 case ARMISD::VLD2DUP:
8127 case ARMISD::VLD3DUP:
8128 case ARMISD::VLD4DUP:
8129 return CombineBaseUpdate(N, DCI);
8130 case ISD::INTRINSIC_VOID:
8131 case ISD::INTRINSIC_W_CHAIN:
8132 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8133 case Intrinsic::arm_neon_vld1:
8134 case Intrinsic::arm_neon_vld2:
8135 case Intrinsic::arm_neon_vld3:
8136 case Intrinsic::arm_neon_vld4:
8137 case Intrinsic::arm_neon_vld2lane:
8138 case Intrinsic::arm_neon_vld3lane:
8139 case Intrinsic::arm_neon_vld4lane:
8140 case Intrinsic::arm_neon_vst1:
8141 case Intrinsic::arm_neon_vst2:
8142 case Intrinsic::arm_neon_vst3:
8143 case Intrinsic::arm_neon_vst4:
8144 case Intrinsic::arm_neon_vst2lane:
8145 case Intrinsic::arm_neon_vst3lane:
8146 case Intrinsic::arm_neon_vst4lane:
8147 return CombineBaseUpdate(N, DCI);
8148 default: break;
8149 }
8150 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008151 }
Dan Gohman475871a2008-07-27 21:46:04 +00008152 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008153}
8154
Evan Cheng31959b12011-02-02 01:06:55 +00008155bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8156 EVT VT) const {
8157 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8158}
8159
Bill Wendlingaf566342009-08-15 21:21:19 +00008160bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008161 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008162 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008163
8164 switch (VT.getSimpleVT().SimpleTy) {
8165 default:
8166 return false;
8167 case MVT::i8:
8168 case MVT::i16:
8169 case MVT::i32:
8170 return true;
8171 // FIXME: VLD1 etc with standard alignment is legal.
8172 }
8173}
8174
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008175static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8176 unsigned AlignCheck) {
8177 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8178 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8179}
8180
8181EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8182 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008183 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008184 bool MemcpyStrSrc,
8185 MachineFunction &MF) const {
8186 const Function *F = MF.getFunction();
8187
8188 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008189 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008190 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8191 Subtarget->hasNEON()) {
8192 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8193 return MVT::v4i32;
8194 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8195 return MVT::v2i32;
8196 }
8197 }
8198
Lang Hames5207bf22011-11-08 18:56:23 +00008199 // Lowering to i32/i16 if the size permits.
8200 if (Size >= 4) {
8201 return MVT::i32;
8202 } else if (Size >= 2) {
8203 return MVT::i16;
8204 }
8205
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008206 // Let the target-independent logic figure it out.
8207 return MVT::Other;
8208}
8209
Evan Chenge6c835f2009-08-14 20:09:37 +00008210static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8211 if (V < 0)
8212 return false;
8213
8214 unsigned Scale = 1;
8215 switch (VT.getSimpleVT().SimpleTy) {
8216 default: return false;
8217 case MVT::i1:
8218 case MVT::i8:
8219 // Scale == 1;
8220 break;
8221 case MVT::i16:
8222 // Scale == 2;
8223 Scale = 2;
8224 break;
8225 case MVT::i32:
8226 // Scale == 4;
8227 Scale = 4;
8228 break;
8229 }
8230
8231 if ((V & (Scale - 1)) != 0)
8232 return false;
8233 V /= Scale;
8234 return V == (V & ((1LL << 5) - 1));
8235}
8236
8237static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8238 const ARMSubtarget *Subtarget) {
8239 bool isNeg = false;
8240 if (V < 0) {
8241 isNeg = true;
8242 V = - V;
8243 }
8244
8245 switch (VT.getSimpleVT().SimpleTy) {
8246 default: return false;
8247 case MVT::i1:
8248 case MVT::i8:
8249 case MVT::i16:
8250 case MVT::i32:
8251 // + imm12 or - imm8
8252 if (isNeg)
8253 return V == (V & ((1LL << 8) - 1));
8254 return V == (V & ((1LL << 12) - 1));
8255 case MVT::f32:
8256 case MVT::f64:
8257 // Same as ARM mode. FIXME: NEON?
8258 if (!Subtarget->hasVFP2())
8259 return false;
8260 if ((V & 3) != 0)
8261 return false;
8262 V >>= 2;
8263 return V == (V & ((1LL << 8) - 1));
8264 }
8265}
8266
Evan Chengb01fad62007-03-12 23:30:29 +00008267/// isLegalAddressImmediate - Return true if the integer value can be used
8268/// as the offset of the target addressing mode for load / store of the
8269/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008270static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008271 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008272 if (V == 0)
8273 return true;
8274
Evan Cheng65011532009-03-09 19:15:00 +00008275 if (!VT.isSimple())
8276 return false;
8277
Evan Chenge6c835f2009-08-14 20:09:37 +00008278 if (Subtarget->isThumb1Only())
8279 return isLegalT1AddressImmediate(V, VT);
8280 else if (Subtarget->isThumb2())
8281 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008282
Evan Chenge6c835f2009-08-14 20:09:37 +00008283 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008284 if (V < 0)
8285 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008287 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008288 case MVT::i1:
8289 case MVT::i8:
8290 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008291 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008292 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008293 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008294 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008295 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008296 case MVT::f32:
8297 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008298 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008299 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008300 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008301 return false;
8302 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008303 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008304 }
Evan Chenga8e29892007-01-19 07:51:42 +00008305}
8306
Evan Chenge6c835f2009-08-14 20:09:37 +00008307bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8308 EVT VT) const {
8309 int Scale = AM.Scale;
8310 if (Scale < 0)
8311 return false;
8312
8313 switch (VT.getSimpleVT().SimpleTy) {
8314 default: return false;
8315 case MVT::i1:
8316 case MVT::i8:
8317 case MVT::i16:
8318 case MVT::i32:
8319 if (Scale == 1)
8320 return true;
8321 // r + r << imm
8322 Scale = Scale & ~1;
8323 return Scale == 2 || Scale == 4 || Scale == 8;
8324 case MVT::i64:
8325 // r + r
8326 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8327 return true;
8328 return false;
8329 case MVT::isVoid:
8330 // Note, we allow "void" uses (basically, uses that aren't loads or
8331 // stores), because arm allows folding a scale into many arithmetic
8332 // operations. This should be made more precise and revisited later.
8333
8334 // Allow r << imm, but the imm has to be a multiple of two.
8335 if (Scale & 1) return false;
8336 return isPowerOf2_32(Scale);
8337 }
8338}
8339
Chris Lattner37caf8c2007-04-09 23:33:39 +00008340/// isLegalAddressingMode - Return true if the addressing mode represented
8341/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008342bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008343 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008344 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008345 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008346 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008347
Chris Lattner37caf8c2007-04-09 23:33:39 +00008348 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008349 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008350 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008351
Chris Lattner37caf8c2007-04-09 23:33:39 +00008352 switch (AM.Scale) {
8353 case 0: // no scale reg, must be "r+i" or "r", or "i".
8354 break;
8355 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008356 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008357 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008358 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008359 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008360 // ARM doesn't support any R+R*scale+imm addr modes.
8361 if (AM.BaseOffs)
8362 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008363
Bob Wilson2c7dab12009-04-08 17:55:28 +00008364 if (!VT.isSimple())
8365 return false;
8366
Evan Chenge6c835f2009-08-14 20:09:37 +00008367 if (Subtarget->isThumb2())
8368 return isLegalT2ScaledAddressingMode(AM, VT);
8369
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008370 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008371 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008372 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 case MVT::i1:
8374 case MVT::i8:
8375 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008376 if (Scale < 0) Scale = -Scale;
8377 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008378 return true;
8379 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008380 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008382 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008383 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008384 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008385 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008386 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008387
Owen Anderson825b72b2009-08-11 20:47:22 +00008388 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008389 // Note, we allow "void" uses (basically, uses that aren't loads or
8390 // stores), because arm allows folding a scale into many arithmetic
8391 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008392
Chris Lattner37caf8c2007-04-09 23:33:39 +00008393 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008394 if (Scale & 1) return false;
8395 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008396 }
8397 break;
Evan Chengb01fad62007-03-12 23:30:29 +00008398 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008399 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008400}
8401
Evan Cheng77e47512009-11-11 19:05:52 +00008402/// isLegalICmpImmediate - Return true if the specified immediate is legal
8403/// icmp immediate, that is the target has icmp instructions which can compare
8404/// a register against the immediate without having to materialize the
8405/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008406bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008407 if (!Subtarget->isThumb())
8408 return ARM_AM::getSOImmVal(Imm) != -1;
8409 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008410 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008411 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008412}
8413
Dan Gohmancca82142011-05-03 00:46:49 +00008414/// isLegalAddImmediate - Return true if the specified immediate is legal
8415/// add immediate, that is the target has add instructions which can add
8416/// a register with the immediate without having to materialize the
8417/// immediate into a register.
8418bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8419 return ARM_AM::getSOImmVal(Imm) != -1;
8420}
8421
Owen Andersone50ed302009-08-10 22:56:29 +00008422static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008423 bool isSEXTLoad, SDValue &Base,
8424 SDValue &Offset, bool &isInc,
8425 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008426 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8427 return false;
8428
Owen Anderson825b72b2009-08-11 20:47:22 +00008429 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008430 // AddressingMode 3
8431 Base = Ptr->getOperand(0);
8432 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008433 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008434 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008435 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008436 isInc = false;
8437 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8438 return true;
8439 }
8440 }
8441 isInc = (Ptr->getOpcode() == ISD::ADD);
8442 Offset = Ptr->getOperand(1);
8443 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008444 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008445 // AddressingMode 2
8446 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008447 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008448 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008449 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008450 isInc = false;
8451 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8452 Base = Ptr->getOperand(0);
8453 return true;
8454 }
8455 }
8456
8457 if (Ptr->getOpcode() == ISD::ADD) {
8458 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008459 ARM_AM::ShiftOpc ShOpcVal=
8460 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008461 if (ShOpcVal != ARM_AM::no_shift) {
8462 Base = Ptr->getOperand(1);
8463 Offset = Ptr->getOperand(0);
8464 } else {
8465 Base = Ptr->getOperand(0);
8466 Offset = Ptr->getOperand(1);
8467 }
8468 return true;
8469 }
8470
8471 isInc = (Ptr->getOpcode() == ISD::ADD);
8472 Base = Ptr->getOperand(0);
8473 Offset = Ptr->getOperand(1);
8474 return true;
8475 }
8476
Jim Grosbache5165492009-11-09 00:11:35 +00008477 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008478 return false;
8479}
8480
Owen Andersone50ed302009-08-10 22:56:29 +00008481static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008482 bool isSEXTLoad, SDValue &Base,
8483 SDValue &Offset, bool &isInc,
8484 SelectionDAG &DAG) {
8485 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8486 return false;
8487
8488 Base = Ptr->getOperand(0);
8489 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8490 int RHSC = (int)RHS->getZExtValue();
8491 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8492 assert(Ptr->getOpcode() == ISD::ADD);
8493 isInc = false;
8494 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8495 return true;
8496 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8497 isInc = Ptr->getOpcode() == ISD::ADD;
8498 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8499 return true;
8500 }
8501 }
8502
8503 return false;
8504}
8505
Evan Chenga8e29892007-01-19 07:51:42 +00008506/// getPreIndexedAddressParts - returns true by value, base pointer and
8507/// offset pointer and addressing mode by reference if the node's address
8508/// can be legally represented as pre-indexed load / store address.
8509bool
Dan Gohman475871a2008-07-27 21:46:04 +00008510ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8511 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008512 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008513 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008514 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008515 return false;
8516
Owen Andersone50ed302009-08-10 22:56:29 +00008517 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008518 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008519 bool isSEXTLoad = false;
8520 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8521 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008522 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008523 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8524 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8525 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008526 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008527 } else
8528 return false;
8529
8530 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008531 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008532 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008533 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8534 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008535 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008536 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008537 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008538 if (!isLegal)
8539 return false;
8540
8541 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8542 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008543}
8544
8545/// getPostIndexedAddressParts - returns true by value, base pointer and
8546/// offset pointer and addressing mode by reference if this node can be
8547/// combined with a load / store to form a post-indexed load / store.
8548bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008549 SDValue &Base,
8550 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008551 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008552 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008553 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008554 return false;
8555
Owen Andersone50ed302009-08-10 22:56:29 +00008556 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008557 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008558 bool isSEXTLoad = false;
8559 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008560 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008561 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008562 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8563 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008564 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008565 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008566 } else
8567 return false;
8568
8569 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008570 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008571 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008572 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008573 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008574 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008575 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8576 isInc, DAG);
8577 if (!isLegal)
8578 return false;
8579
Evan Cheng28dad2a2010-05-18 21:31:17 +00008580 if (Ptr != Base) {
8581 // Swap base ptr and offset to catch more post-index load / store when
8582 // it's legal. In Thumb2 mode, offset must be an immediate.
8583 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8584 !Subtarget->isThumb2())
8585 std::swap(Base, Offset);
8586
8587 // Post-indexed load / store update the base pointer.
8588 if (Ptr != Base)
8589 return false;
8590 }
8591
Evan Chenge88d5ce2009-07-02 07:28:31 +00008592 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8593 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008594}
8595
Dan Gohman475871a2008-07-27 21:46:04 +00008596void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008597 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008598 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008599 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008600 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008601 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008602 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008603 switch (Op.getOpcode()) {
8604 default: break;
8605 case ARMISD::CMOV: {
8606 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008607 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008608 if (KnownZero == 0 && KnownOne == 0) return;
8609
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008610 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008611 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8612 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008613 KnownZero &= KnownZeroRHS;
8614 KnownOne &= KnownOneRHS;
8615 return;
8616 }
8617 }
8618}
8619
8620//===----------------------------------------------------------------------===//
8621// ARM Inline Assembly Support
8622//===----------------------------------------------------------------------===//
8623
Evan Cheng55d42002011-01-08 01:24:27 +00008624bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8625 // Looking for "rev" which is V6+.
8626 if (!Subtarget->hasV6Ops())
8627 return false;
8628
8629 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8630 std::string AsmStr = IA->getAsmString();
8631 SmallVector<StringRef, 4> AsmPieces;
8632 SplitString(AsmStr, AsmPieces, ";\n");
8633
8634 switch (AsmPieces.size()) {
8635 default: return false;
8636 case 1:
8637 AsmStr = AsmPieces[0];
8638 AsmPieces.clear();
8639 SplitString(AsmStr, AsmPieces, " \t,");
8640
8641 // rev $0, $1
8642 if (AsmPieces.size() == 3 &&
8643 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8644 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008645 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008646 if (Ty && Ty->getBitWidth() == 32)
8647 return IntrinsicLowering::LowerToByteSwap(CI);
8648 }
8649 break;
8650 }
8651
8652 return false;
8653}
8654
Evan Chenga8e29892007-01-19 07:51:42 +00008655/// getConstraintType - Given a constraint letter, return the type of
8656/// constraint it is for this target.
8657ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008658ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8659 if (Constraint.size() == 1) {
8660 switch (Constraint[0]) {
8661 default: break;
8662 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008663 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008664 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008665 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008666 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008667 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008668 // An address with a single base register. Due to the way we
8669 // currently handle addresses it is the same as an 'r' memory constraint.
8670 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008671 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008672 } else if (Constraint.size() == 2) {
8673 switch (Constraint[0]) {
8674 default: break;
8675 // All 'U+' constraints are addresses.
8676 case 'U': return C_Memory;
8677 }
Evan Chenga8e29892007-01-19 07:51:42 +00008678 }
Chris Lattner4234f572007-03-25 02:14:49 +00008679 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008680}
8681
John Thompson44ab89e2010-10-29 17:29:13 +00008682/// Examine constraint type and operand type and determine a weight value.
8683/// This object must already have been set up with the operand type
8684/// and the current alternative constraint selected.
8685TargetLowering::ConstraintWeight
8686ARMTargetLowering::getSingleConstraintMatchWeight(
8687 AsmOperandInfo &info, const char *constraint) const {
8688 ConstraintWeight weight = CW_Invalid;
8689 Value *CallOperandVal = info.CallOperandVal;
8690 // If we don't have a value, we can't do a match,
8691 // but allow it at the lowest weight.
8692 if (CallOperandVal == NULL)
8693 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008694 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008695 // Look at the constraint type.
8696 switch (*constraint) {
8697 default:
8698 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8699 break;
8700 case 'l':
8701 if (type->isIntegerTy()) {
8702 if (Subtarget->isThumb())
8703 weight = CW_SpecificReg;
8704 else
8705 weight = CW_Register;
8706 }
8707 break;
8708 case 'w':
8709 if (type->isFloatingPointTy())
8710 weight = CW_Register;
8711 break;
8712 }
8713 return weight;
8714}
8715
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008716typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8717RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008718ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008719 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008720 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008721 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008722 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008723 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008724 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008725 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008726 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008727 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008728 case 'h': // High regs or no regs.
8729 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008730 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008731 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008732 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008733 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008734 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008735 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008736 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008737 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008738 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008739 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008740 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008741 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008742 case 'x':
8743 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008744 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008745 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008746 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008747 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008748 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008749 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008750 case 't':
8751 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008752 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008753 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008754 }
8755 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008756 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008757 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008758
Evan Chenga8e29892007-01-19 07:51:42 +00008759 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8760}
8761
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008762/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8763/// vector. If it is invalid, don't add anything to Ops.
8764void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008765 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008766 std::vector<SDValue>&Ops,
8767 SelectionDAG &DAG) const {
8768 SDValue Result(0, 0);
8769
Eric Christopher100c8332011-06-02 23:16:42 +00008770 // Currently only support length 1 constraints.
8771 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008772
Eric Christopher100c8332011-06-02 23:16:42 +00008773 char ConstraintLetter = Constraint[0];
8774 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008775 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008776 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008777 case 'I': case 'J': case 'K': case 'L':
8778 case 'M': case 'N': case 'O':
8779 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8780 if (!C)
8781 return;
8782
8783 int64_t CVal64 = C->getSExtValue();
8784 int CVal = (int) CVal64;
8785 // None of these constraints allow values larger than 32 bits. Check
8786 // that the value fits in an int.
8787 if (CVal != CVal64)
8788 return;
8789
Eric Christopher100c8332011-06-02 23:16:42 +00008790 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008791 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008792 // Constant suitable for movw, must be between 0 and
8793 // 65535.
8794 if (Subtarget->hasV6T2Ops())
8795 if (CVal >= 0 && CVal <= 65535)
8796 break;
8797 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008798 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008799 if (Subtarget->isThumb1Only()) {
8800 // This must be a constant between 0 and 255, for ADD
8801 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008802 if (CVal >= 0 && CVal <= 255)
8803 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008804 } else if (Subtarget->isThumb2()) {
8805 // A constant that can be used as an immediate value in a
8806 // data-processing instruction.
8807 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8808 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008809 } else {
8810 // A constant that can be used as an immediate value in a
8811 // data-processing instruction.
8812 if (ARM_AM::getSOImmVal(CVal) != -1)
8813 break;
8814 }
8815 return;
8816
8817 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008818 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008819 // This must be a constant between -255 and -1, for negated ADD
8820 // immediates. This can be used in GCC with an "n" modifier that
8821 // prints the negated value, for use with SUB instructions. It is
8822 // not useful otherwise but is implemented for compatibility.
8823 if (CVal >= -255 && CVal <= -1)
8824 break;
8825 } else {
8826 // This must be a constant between -4095 and 4095. It is not clear
8827 // what this constraint is intended for. Implemented for
8828 // compatibility with GCC.
8829 if (CVal >= -4095 && CVal <= 4095)
8830 break;
8831 }
8832 return;
8833
8834 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008835 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008836 // A 32-bit value where only one byte has a nonzero value. Exclude
8837 // zero to match GCC. This constraint is used by GCC internally for
8838 // constants that can be loaded with a move/shift combination.
8839 // It is not useful otherwise but is implemented for compatibility.
8840 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
8841 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008842 } else if (Subtarget->isThumb2()) {
8843 // A constant whose bitwise inverse can be used as an immediate
8844 // value in a data-processing instruction. This can be used in GCC
8845 // with a "B" modifier that prints the inverted value, for use with
8846 // BIC and MVN instructions. It is not useful otherwise but is
8847 // implemented for compatibility.
8848 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
8849 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008850 } else {
8851 // A constant whose bitwise inverse can be used as an immediate
8852 // value in a data-processing instruction. This can be used in GCC
8853 // with a "B" modifier that prints the inverted value, for use with
8854 // BIC and MVN instructions. It is not useful otherwise but is
8855 // implemented for compatibility.
8856 if (ARM_AM::getSOImmVal(~CVal) != -1)
8857 break;
8858 }
8859 return;
8860
8861 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008862 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008863 // This must be a constant between -7 and 7,
8864 // for 3-operand ADD/SUB immediate instructions.
8865 if (CVal >= -7 && CVal < 7)
8866 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008867 } else if (Subtarget->isThumb2()) {
8868 // A constant whose negation can be used as an immediate value in a
8869 // data-processing instruction. This can be used in GCC with an "n"
8870 // modifier that prints the negated value, for use with SUB
8871 // instructions. It is not useful otherwise but is implemented for
8872 // compatibility.
8873 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
8874 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008875 } else {
8876 // A constant whose negation can be used as an immediate value in a
8877 // data-processing instruction. This can be used in GCC with an "n"
8878 // modifier that prints the negated value, for use with SUB
8879 // instructions. It is not useful otherwise but is implemented for
8880 // compatibility.
8881 if (ARM_AM::getSOImmVal(-CVal) != -1)
8882 break;
8883 }
8884 return;
8885
8886 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008887 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008888 // This must be a multiple of 4 between 0 and 1020, for
8889 // ADD sp + immediate.
8890 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
8891 break;
8892 } else {
8893 // A power of two or a constant between 0 and 32. This is used in
8894 // GCC for the shift amount on shifted register operands, but it is
8895 // useful in general for any shift amounts.
8896 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
8897 break;
8898 }
8899 return;
8900
8901 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008902 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008903 // This must be a constant between 0 and 31, for shift amounts.
8904 if (CVal >= 0 && CVal <= 31)
8905 break;
8906 }
8907 return;
8908
8909 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008910 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008911 // This must be a multiple of 4 between -508 and 508, for
8912 // ADD/SUB sp = sp + immediate.
8913 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
8914 break;
8915 }
8916 return;
8917 }
8918 Result = DAG.getTargetConstant(CVal, Op.getValueType());
8919 break;
8920 }
8921
8922 if (Result.getNode()) {
8923 Ops.push_back(Result);
8924 return;
8925 }
Dale Johannesen1784d162010-06-25 21:55:36 +00008926 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008927}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00008928
8929bool
8930ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
8931 // The ARM target isn't yet aware of offsets.
8932 return false;
8933}
Evan Cheng39382422009-10-28 01:44:26 +00008934
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008935bool ARM::isBitFieldInvertedMask(unsigned v) {
8936 if (v == 0xffffffff)
8937 return 0;
8938 // there can be 1's on either or both "outsides", all the "inside"
8939 // bits must be 0's
8940 unsigned int lsb = 0, msb = 31;
8941 while (v & (1 << msb)) --msb;
8942 while (v & (1 << lsb)) ++lsb;
8943 for (unsigned int i = lsb; i <= msb; ++i) {
8944 if (v & (1 << i))
8945 return 0;
8946 }
8947 return 1;
8948}
8949
Evan Cheng39382422009-10-28 01:44:26 +00008950/// isFPImmLegal - Returns true if the target can instruction select the
8951/// specified FP immediate natively. If false, the legalizer will
8952/// materialize the FP immediate as a load from a constant pool.
8953bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
8954 if (!Subtarget->hasVFP3())
8955 return false;
8956 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008957 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008958 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00008959 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00008960 return false;
8961}
Bob Wilson65ffec42010-09-21 17:56:22 +00008962
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008963/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00008964/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
8965/// specified in the intrinsic calls.
8966bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
8967 const CallInst &I,
8968 unsigned Intrinsic) const {
8969 switch (Intrinsic) {
8970 case Intrinsic::arm_neon_vld1:
8971 case Intrinsic::arm_neon_vld2:
8972 case Intrinsic::arm_neon_vld3:
8973 case Intrinsic::arm_neon_vld4:
8974 case Intrinsic::arm_neon_vld2lane:
8975 case Intrinsic::arm_neon_vld3lane:
8976 case Intrinsic::arm_neon_vld4lane: {
8977 Info.opc = ISD::INTRINSIC_W_CHAIN;
8978 // Conservatively set memVT to the entire set of vectors loaded.
8979 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
8980 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
8981 Info.ptrVal = I.getArgOperand(0);
8982 Info.offset = 0;
8983 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
8984 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
8985 Info.vol = false; // volatile loads with NEON intrinsics not supported
8986 Info.readMem = true;
8987 Info.writeMem = false;
8988 return true;
8989 }
8990 case Intrinsic::arm_neon_vst1:
8991 case Intrinsic::arm_neon_vst2:
8992 case Intrinsic::arm_neon_vst3:
8993 case Intrinsic::arm_neon_vst4:
8994 case Intrinsic::arm_neon_vst2lane:
8995 case Intrinsic::arm_neon_vst3lane:
8996 case Intrinsic::arm_neon_vst4lane: {
8997 Info.opc = ISD::INTRINSIC_VOID;
8998 // Conservatively set memVT to the entire set of vectors stored.
8999 unsigned NumElts = 0;
9000 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009001 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009002 if (!ArgTy->isVectorTy())
9003 break;
9004 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9005 }
9006 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9007 Info.ptrVal = I.getArgOperand(0);
9008 Info.offset = 0;
9009 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9010 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9011 Info.vol = false; // volatile stores with NEON intrinsics not supported
9012 Info.readMem = false;
9013 Info.writeMem = true;
9014 return true;
9015 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009016 case Intrinsic::arm_strexd: {
9017 Info.opc = ISD::INTRINSIC_W_CHAIN;
9018 Info.memVT = MVT::i64;
9019 Info.ptrVal = I.getArgOperand(2);
9020 Info.offset = 0;
9021 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009022 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009023 Info.readMem = false;
9024 Info.writeMem = true;
9025 return true;
9026 }
9027 case Intrinsic::arm_ldrexd: {
9028 Info.opc = ISD::INTRINSIC_W_CHAIN;
9029 Info.memVT = MVT::i64;
9030 Info.ptrVal = I.getArgOperand(0);
9031 Info.offset = 0;
9032 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009033 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009034 Info.readMem = true;
9035 Info.writeMem = false;
9036 return true;
9037 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009038 default:
9039 break;
9040 }
9041
9042 return false;
9043}