blob: e26dd22c9294d989714096e8e31b3e6ecdf9ca87 [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"
Craig Topperc1f6f422012-03-17 07:33:42 +000016#include "ARMISelLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#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"
Evan Chenga8e29892007-01-19 07:51:42 +000043#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000045#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000046#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000047#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000049#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000050#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000051#include "llvm/Support/raw_ostream.h"
Evan Chenga8e29892007-01-19 07:51:42 +000052using namespace llvm;
53
Dale Johannesen51e28e62010-06-03 21:09:53 +000054STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000055STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000056
Bob Wilson703af3a2010-08-13 22:43:33 +000057// This option should go away when tail calls fully work.
58static cl::opt<bool>
59EnableARMTailCalls("arm-tail-calls", cl::Hidden,
60 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
61 cl::init(false));
62
Eric Christopher836c6242010-12-15 23:47:29 +000063cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000064EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000065 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000066 cl::init(false));
67
Evan Cheng46df4eb2010-06-16 07:35:02 +000068static cl::opt<bool>
69ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
71 cl::init(true));
72
Benjamin Kramer0861f572011-11-26 23:01:57 +000073namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000074 class ARMCCState : public CCState {
75 public:
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
77 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
78 LLVMContext &C, ParmContext PC)
79 : CCState(CC, isVarArg, MF, TM, locs, C) {
80 assert(((PC == Call) || (PC == Prologue)) &&
81 "ARMCCState users must specify whether their context is call"
82 "or prologue generation.");
83 CallOrPrologue = PC;
84 }
85 };
86}
87
Stuart Hastingsc7315872011-04-20 16:47:52 +000088// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000089static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000090 ARM::R0, ARM::R1, ARM::R2, ARM::R3
91};
92
Owen Andersone50ed302009-08-10 22:56:29 +000093void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
94 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000096 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000097 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
98 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000099
Owen Anderson70671842009-08-10 20:18:46 +0000100 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000101 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000102 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000103 }
104
Owen Andersone50ed302009-08-10 22:56:29 +0000105 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000107 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000108 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000109 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000110 if (ElemTy == MVT::i32) {
111 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
112 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
115 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000116 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
120 }
Owen Anderson70671842009-08-10 20:18:46 +0000121 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
122 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000123 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000124 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000125 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000128 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000132 }
133
134 // Promote all bit-wise operations.
135 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000136 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000137 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
138 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000139 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000140 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000141 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000142 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000143 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000144 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000145 }
Bob Wilson16330762009-09-16 00:17:28 +0000146
147 // Neon does not support vector divide/remainder operations.
148 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
149 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Owen Andersone50ed302009-08-10 22:56:29 +0000156void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000157 addRegisterClass(VT, ARM::DPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Owen Andersone50ed302009-08-10 22:56:29 +0000161void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Bob Wilson5bafff32009-06-22 23:27:02 +0000162 addRegisterClass(VT, ARM::QPRRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000164}
165
Chris Lattnerf0144122009-07-28 03:13:23 +0000166static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
167 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000168 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000169
Chris Lattner80ec2792009-08-02 00:34:36 +0000170 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000171}
172
Evan Chenga8e29892007-01-19 07:51:42 +0000173ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000174 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000175 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000176 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000177 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000178
Duncan Sands28b77e92011-09-06 19:07:46 +0000179 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Evan Cheng07043272012-02-21 20:46:00 +0000263 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000388 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
389 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
390 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
391 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
397
398 // Integer division functions
399 // RTABI chapter 4.3.1
400 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
401 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
402 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000403 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000404 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
405 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000407 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000411 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000412 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000414 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000415 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000422 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
423 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000425 }
426
Bob Wilson2fef4572011-10-07 16:59:21 +0000427 // Use divmod compiler-rt calls for iOS 5.0 and later.
428 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
429 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
430 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
431 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
432 }
433
David Goodwinf1daf7d2009-07-08 23:10:31 +0000434 if (Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addRegisterClass(MVT::i32, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000436 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 addRegisterClass(MVT::i32, ARM::GPRRegisterClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000438 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
439 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 addRegisterClass(MVT::f32, ARM::SPRRegisterClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000441 if (!Subtarget->isFPOnlySP())
442 addRegisterClass(MVT::f64, ARM::DPRRegisterClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000443
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000445 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000446
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000447 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
448 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
449 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
450 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
451 setTruncStoreAction((MVT::SimpleValueType)VT,
452 (MVT::SimpleValueType)InnerVT, Expand);
453 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
454 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
456 }
457
Lang Hames45b5f882012-03-15 18:49:02 +0000458 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
459
Bob Wilson5bafff32009-06-22 23:27:02 +0000460 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 addDRTypeForNEON(MVT::v2f32);
462 addDRTypeForNEON(MVT::v8i8);
463 addDRTypeForNEON(MVT::v4i16);
464 addDRTypeForNEON(MVT::v2i32);
465 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000466
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 addQRTypeForNEON(MVT::v4f32);
468 addQRTypeForNEON(MVT::v2f64);
469 addQRTypeForNEON(MVT::v16i8);
470 addQRTypeForNEON(MVT::v8i16);
471 addQRTypeForNEON(MVT::v4i32);
472 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000473
Bob Wilson74dc72e2009-09-15 23:55:57 +0000474 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
475 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
477 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
479 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
480 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000481 // FIXME: Code duplication: FDIV and FREM are expanded always, see
482 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
484 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000485 // FIXME: Create unittest.
486 // In another words, find a way when "copysign" appears in DAG with vector
487 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000489 // FIXME: Code duplication: SETCC has custom operation action, see
490 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000491 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000492 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000493 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
494 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
496 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
497 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
499 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
504 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000505 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000506 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
507 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
508 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
510 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000511
512 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
513 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
514 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
515 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
516 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
519 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
520 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
521 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000522
Bob Wilson642b3292009-09-16 00:32:15 +0000523 // Neon does not support some operations on v1i64 and v2i64 types.
524 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000525 // Custom handling for some quad-vector types to detect VMULL.
526 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
527 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
528 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000529 // Custom handling for some vector types to avoid expensive expansions
530 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
531 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
532 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
533 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000534 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
535 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000536 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000537 // a destination type that is wider than the source, and nor does
538 // it have a FP_TO_[SU]INT instruction with a narrower destination than
539 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000540 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
541 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000542 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
543 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000544
Bob Wilson1c3ef902011-02-07 17:43:21 +0000545 setTargetDAGCombine(ISD::INTRINSIC_VOID);
546 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000547 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
548 setTargetDAGCombine(ISD::SHL);
549 setTargetDAGCombine(ISD::SRL);
550 setTargetDAGCombine(ISD::SRA);
551 setTargetDAGCombine(ISD::SIGN_EXTEND);
552 setTargetDAGCombine(ISD::ZERO_EXTEND);
553 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000554 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000555 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000556 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000557 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
558 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000559 setTargetDAGCombine(ISD::FP_TO_SINT);
560 setTargetDAGCombine(ISD::FP_TO_UINT);
561 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000562
James Molloy873fd5f2012-02-20 09:24:05 +0000563 // It is legal to extload from v4i8 to v4i16 or v4i32.
564 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
565 MVT::v4i16, MVT::v2i16,
566 MVT::v2i32};
567 for (unsigned i = 0; i < 6; ++i) {
568 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
569 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
570 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
571 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000572 }
573
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000574 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000575
576 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000578
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000579 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000583 if (!Subtarget->isThumb1Only()) {
584 for (unsigned im = (unsigned)ISD::PRE_INC;
585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setIndexedLoadAction(im, MVT::i1, Legal);
587 setIndexedLoadAction(im, MVT::i8, Legal);
588 setIndexedLoadAction(im, MVT::i16, Legal);
589 setIndexedLoadAction(im, MVT::i32, Legal);
590 setIndexedStoreAction(im, MVT::i1, Legal);
591 setIndexedStoreAction(im, MVT::i8, Legal);
592 setIndexedStoreAction(im, MVT::i16, Legal);
593 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595 }
596
597 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000598 setOperationAction(ISD::MUL, MVT::i64, Expand);
599 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000600 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000606 setOperationAction(ISD::MULHS, MVT::i32, Expand);
607
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SRL, MVT::i64, Custom);
612 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Evan Cheng342e3162011-08-30 01:34:54 +0000614 if (!Subtarget->isThumb1Only()) {
615 // FIXME: We should do this for Thumb1 as well.
616 setOperationAction(ISD::ADDC, MVT::i32, Custom);
617 setOperationAction(ISD::ADDE, MVT::i32, Custom);
618 setOperationAction(ISD::SUBC, MVT::i32, Custom);
619 setOperationAction(ISD::SUBE, MVT::i32, Custom);
620 }
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000624 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Chandler Carruth63974b22011-12-13 01:56:10 +0000629 // These just redirect to CTTZ and CTLZ on ARM.
630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
632
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000633 // Only ARMv6 has BSWAP.
634 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000639 // v7M has a hardware divider
640 setOperationAction(ISD::SDIV, MVT::i32, Expand);
641 setOperationAction(ISD::UDIV, MVT::i32, Expand);
642 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::UREM, MVT::i32, Expand);
645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000654 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000655
Evan Chenga8e29892007-01-19 07:51:42 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VASTART, MVT::Other, Custom);
658 setOperationAction(ISD::VAARG, MVT::Other, Expand);
659 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
660 setOperationAction(ISD::VAEND, MVT::Other, Expand);
661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000663
664 if (!Subtarget->isTargetDarwin()) {
665 // Non-Darwin platforms may return values in these registers via the
666 // personality function.
667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
669 setExceptionPointerRegister(ARM::R0);
670 setExceptionSelectorRegister(ARM::R1);
671 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000672
Evan Cheng3a1588a2010-04-15 22:20:34 +0000673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
675 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000676 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000677 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 // membarrier needs custom lowering; the rest are legal and handled
680 // normally.
681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000683 // Custom lowering for 64-bit ops
684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
692 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 } else {
694 // Set them all for expansion, which will force libcalls.
695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
710 // Unordered/Monotonic case.
711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000713 // Since the libcalls include locking, fold in the fences
714 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 }
Evan Chenga8e29892007-01-19 07:51:42 +0000716
Evan Cheng416941d2010-11-04 05:19:35 +0000717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000718
Eli Friedmana2c6f452010-06-26 04:36:50 +0000719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
720 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000723 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
727 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
729 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
732 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000733
734 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000736 if (Subtarget->isTargetDarwin()) {
737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000740 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SETCC, MVT::i32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000745 setOperationAction(ISD::SELECT, MVT::i32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
753 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
756 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000757
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000758 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN, MVT::f64, Expand);
760 setOperationAction(ISD::FSIN, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000769 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::FPOW, MVT::f64, Expand);
771 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Cameron Zwarich33390842011-07-08 21:39:21 +0000773 setOperationAction(ISD::FMA, MVT::f64, Expand);
774 setOperationAction(ISD::FMA, MVT::f32, Expand);
775
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000776 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000777 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000778 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
779 if (Subtarget->hasVFP2()) {
780 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
781 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
782 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
783 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
784 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000785 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000786 if (!Subtarget->hasFP16()) {
787 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
788 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000789 }
Evan Cheng110cf482008-04-01 01:50:16 +0000790 }
Evan Chenga8e29892007-01-19 07:51:42 +0000791
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000792 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000793 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000794 setTargetDAGCombine(ISD::ADD);
795 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000796 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000797
Evan Chengc892aeb2012-02-23 01:19:06 +0000798 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000799 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000800 setTargetDAGCombine(ISD::OR);
801 setTargetDAGCombine(ISD::XOR);
802 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000803
Evan Cheng5fb468a2012-02-23 02:58:19 +0000804 if (Subtarget->hasV6Ops())
805 setTargetDAGCombine(ISD::SRL);
806
Evan Chenga8e29892007-01-19 07:51:42 +0000807 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000808
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000809 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
810 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000811 setSchedulingPreference(Sched::RegPressure);
812 else
813 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000814
Evan Cheng05219282011-01-06 06:52:41 +0000815 //// temporary - rewrite interface to use type
816 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000817 maxStoresPerMemset = 16;
818 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000819
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000820 // On ARM arguments smaller than 4 bytes are extended, so all arguments
821 // are at least 4 bytes aligned.
822 setMinStackArgumentAlignment(4);
823
Evan Chengfff606d2010-09-24 19:07:23 +0000824 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000825
826 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000827}
828
Andrew Trick32cec0a2011-01-19 02:35:27 +0000829// FIXME: It might make sense to define the representative register class as the
830// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
831// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
832// SPR's representative would be DPR_VFP2. This should work well if register
833// pressure tracking were modified such that a register use would increment the
834// pressure of the register class's representative and all of it's super
835// classes' representatives transitively. We have not implemented this because
836// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000837// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000838// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000839std::pair<const TargetRegisterClass*, uint8_t>
840ARMTargetLowering::findRepresentativeClass(EVT VT) const{
841 const TargetRegisterClass *RRC = 0;
842 uint8_t Cost = 1;
843 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000844 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000845 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000846 // Use DPR as representative register class for all floating point
847 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
848 // the cost is 1 for both f32 and f64.
849 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Evan Cheng4a863e22010-07-21 23:53:58 +0000851 RRC = ARM::DPRRegisterClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000852 // When NEON is used for SP, only half of the register file is available
853 // because operations that define both SP and DP results will be constrained
854 // to the VFP2 class (D0-D15). We currently model this constraint prior to
855 // coalescing by double-counting the SP regs. See the FIXME above.
856 if (Subtarget->useNEONForSinglePrecisionFP())
857 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000858 break;
859 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
860 case MVT::v4f32: case MVT::v2f64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000861 RRC = ARM::DPRRegisterClass;
862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v4i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000865 RRC = ARM::DPRRegisterClass;
866 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000867 break;
868 case MVT::v8i64:
Evan Cheng4a863e22010-07-21 23:53:58 +0000869 RRC = ARM::DPRRegisterClass;
870 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000871 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000872 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000874}
875
Evan Chenga8e29892007-01-19 07:51:42 +0000876const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
877 switch (Opcode) {
878 default: return 0;
879 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000880 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000881 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000882 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
883 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000884 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000885 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
886 case ARMISD::tCALL: return "ARMISD::tCALL";
887 case ARMISD::BRCOND: return "ARMISD::BRCOND";
888 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000889 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
891 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
892 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000893 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000894 case ARMISD::CMPFP: return "ARMISD::CMPFP";
895 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000896 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000897 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000898
Evan Chenga8e29892007-01-19 07:51:42 +0000899 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000900 case ARMISD::CAND: return "ARMISD::CAND";
901 case ARMISD::COR: return "ARMISD::COR";
902 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000903
Jim Grosbach3482c802010-01-18 19:58:49 +0000904 case ARMISD::RBIT: return "ARMISD::RBIT";
905
Bob Wilson76a312b2010-03-19 22:51:32 +0000906 case ARMISD::FTOSI: return "ARMISD::FTOSI";
907 case ARMISD::FTOUI: return "ARMISD::FTOUI";
908 case ARMISD::SITOF: return "ARMISD::SITOF";
909 case ARMISD::UITOF: return "ARMISD::UITOF";
910
Evan Chenga8e29892007-01-19 07:51:42 +0000911 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
912 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
913 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000914
Evan Cheng342e3162011-08-30 01:34:54 +0000915 case ARMISD::ADDC: return "ARMISD::ADDC";
916 case ARMISD::ADDE: return "ARMISD::ADDE";
917 case ARMISD::SUBC: return "ARMISD::SUBC";
918 case ARMISD::SUBE: return "ARMISD::SUBE";
919
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000920 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
921 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000922
Evan Chengc5942082009-10-28 06:55:03 +0000923 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
924 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
925
Dale Johannesen51e28e62010-06-03 21:09:53 +0000926 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000927
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000928 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000929
Evan Cheng86198642009-08-07 00:34:42 +0000930 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
931
Jim Grosbach3728e962009-12-10 00:11:09 +0000932 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000933 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000934
Evan Chengdfed19f2010-11-03 06:34:55 +0000935 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
936
Bob Wilson5bafff32009-06-22 23:27:02 +0000937 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000938 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000939 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000940 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
941 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 case ARMISD::VCGEU: return "ARMISD::VCGEU";
943 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000944 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
945 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000946 case ARMISD::VCGTU: return "ARMISD::VCGTU";
947 case ARMISD::VTST: return "ARMISD::VTST";
948
949 case ARMISD::VSHL: return "ARMISD::VSHL";
950 case ARMISD::VSHRs: return "ARMISD::VSHRs";
951 case ARMISD::VSHRu: return "ARMISD::VSHRu";
952 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
953 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
954 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
955 case ARMISD::VSHRN: return "ARMISD::VSHRN";
956 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
957 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
958 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
959 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
960 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
961 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
962 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
963 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
964 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
965 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
966 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
967 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
968 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
969 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000970 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000971 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000972 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000973 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000974 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000975 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000976 case ARMISD::VREV64: return "ARMISD::VREV64";
977 case ARMISD::VREV32: return "ARMISD::VREV32";
978 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000979 case ARMISD::VZIP: return "ARMISD::VZIP";
980 case ARMISD::VUZP: return "ARMISD::VUZP";
981 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000982 case ARMISD::VTBL1: return "ARMISD::VTBL1";
983 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000984 case ARMISD::VMULLs: return "ARMISD::VMULLs";
985 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000986 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000987 case ARMISD::FMAX: return "ARMISD::FMAX";
988 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000989 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000990 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
991 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000992 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000993 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
994 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
995 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000996 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
997 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
998 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
999 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1000 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1001 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1002 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1003 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1004 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1005 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1006 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1007 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1008 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1009 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1010 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1011 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1012 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001013 }
1014}
1015
Duncan Sands28b77e92011-09-06 19:07:46 +00001016EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1017 if (!VT.isVector()) return getPointerTy();
1018 return VT.changeVectorElementTypeToInteger();
1019}
1020
Evan Cheng06b666c2010-05-15 02:18:07 +00001021/// getRegClassFor - Return the register class that should be used for the
1022/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001023const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001024 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1025 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1026 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001027 if (Subtarget->hasNEON()) {
1028 if (VT == MVT::v4i64)
1029 return ARM::QQPRRegisterClass;
1030 else if (VT == MVT::v8i64)
1031 return ARM::QQQQPRRegisterClass;
1032 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001033 return TargetLowering::getRegClassFor(VT);
1034}
1035
Eric Christopherab695882010-07-21 22:26:11 +00001036// Create a fast isel object.
1037FastISel *
1038ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1039 return ARM::createFastISel(funcInfo);
1040}
1041
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001042/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1043/// be used for loads / stores from the global.
1044unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1045 return (Subtarget->isThumb1Only() ? 127 : 4095);
1046}
1047
Evan Cheng1cc39842010-05-20 23:26:43 +00001048Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001049 unsigned NumVals = N->getNumValues();
1050 if (!NumVals)
1051 return Sched::RegPressure;
1052
1053 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001054 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001055 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001056 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001057 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001058 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001059 }
Evan Chengc10f5432010-05-28 23:25:23 +00001060
1061 if (!N->isMachineOpcode())
1062 return Sched::RegPressure;
1063
1064 // Load are scheduled for latency even if there instruction itinerary
1065 // is not available.
1066 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001067 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001068
Evan Chenge837dea2011-06-28 19:10:37 +00001069 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001070 return Sched::RegPressure;
1071 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001072 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001073 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001074
Evan Cheng1cc39842010-05-20 23:26:43 +00001075 return Sched::RegPressure;
1076}
1077
Evan Chenga8e29892007-01-19 07:51:42 +00001078//===----------------------------------------------------------------------===//
1079// Lowering Code
1080//===----------------------------------------------------------------------===//
1081
Evan Chenga8e29892007-01-19 07:51:42 +00001082/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1083static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1084 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001085 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001086 case ISD::SETNE: return ARMCC::NE;
1087 case ISD::SETEQ: return ARMCC::EQ;
1088 case ISD::SETGT: return ARMCC::GT;
1089 case ISD::SETGE: return ARMCC::GE;
1090 case ISD::SETLT: return ARMCC::LT;
1091 case ISD::SETLE: return ARMCC::LE;
1092 case ISD::SETUGT: return ARMCC::HI;
1093 case ISD::SETUGE: return ARMCC::HS;
1094 case ISD::SETULT: return ARMCC::LO;
1095 case ISD::SETULE: return ARMCC::LS;
1096 }
1097}
1098
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001099/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1100static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001101 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001102 CondCode2 = ARMCC::AL;
1103 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001104 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001105 case ISD::SETEQ:
1106 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1107 case ISD::SETGT:
1108 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1109 case ISD::SETGE:
1110 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1111 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001112 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001113 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1114 case ISD::SETO: CondCode = ARMCC::VC; break;
1115 case ISD::SETUO: CondCode = ARMCC::VS; break;
1116 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1117 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1118 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1119 case ISD::SETLT:
1120 case ISD::SETULT: CondCode = ARMCC::LT; break;
1121 case ISD::SETLE:
1122 case ISD::SETULE: CondCode = ARMCC::LE; break;
1123 case ISD::SETNE:
1124 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1125 }
Evan Chenga8e29892007-01-19 07:51:42 +00001126}
1127
Bob Wilson1f595bb2009-04-17 19:07:39 +00001128//===----------------------------------------------------------------------===//
1129// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130//===----------------------------------------------------------------------===//
1131
1132#include "ARMGenCallingConv.inc"
1133
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001134/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1135/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001136CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001137 bool Return,
1138 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001139 switch (CC) {
1140 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001141 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001142 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001143 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001144 if (!Subtarget->isAAPCS_ABI())
1145 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1146 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1147 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1148 }
1149 // Fallthrough
1150 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001151 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001152 if (!Subtarget->isAAPCS_ABI())
1153 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1154 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001155 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1156 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001157 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1158 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1159 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001160 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001161 if (!isVarArg)
1162 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1163 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001164 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001165 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001166 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001167 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001168 }
1169}
1170
Dan Gohman98ca4f22009-08-05 01:29:28 +00001171/// LowerCallResult - Lower the result values of a call into the
1172/// appropriate copies out of appropriate physical registers.
1173SDValue
1174ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001175 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176 const SmallVectorImpl<ISD::InputArg> &Ins,
1177 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001178 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001179
Bob Wilson1f595bb2009-04-17 19:07:39 +00001180 // Assign locations to each value returned by this call.
1181 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001182 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1183 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001185 CCAssignFnForNode(CallConv, /* Return*/ true,
1186 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187
1188 // Copy all of the result registers out of their specified physreg.
1189 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1190 CCValAssign VA = RVLocs[i];
1191
Bob Wilson80915242009-04-25 00:33:20 +00001192 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001194 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001195 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001197 Chain = Lo.getValue(1);
1198 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001199 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001201 InFlag);
1202 Chain = Hi.getValue(1);
1203 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001204 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001205
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 if (VA.getLocVT() == MVT::v2f64) {
1207 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1208 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1209 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001210
1211 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001212 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001213 Chain = Lo.getValue(1);
1214 InFlag = Lo.getValue(2);
1215 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001217 Chain = Hi.getValue(1);
1218 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001219 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1221 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001222 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001223 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001224 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1225 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001226 Chain = Val.getValue(1);
1227 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 }
Bob Wilson80915242009-04-25 00:33:20 +00001229
1230 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001231 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001232 case CCValAssign::Full: break;
1233 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001234 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001235 break;
1236 }
1237
Dan Gohman98ca4f22009-08-05 01:29:28 +00001238 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 }
1240
Dan Gohman98ca4f22009-08-05 01:29:28 +00001241 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001242}
1243
Bob Wilsondee46d72009-04-17 20:35:10 +00001244/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1247 SDValue StackPtr, SDValue Arg,
1248 DebugLoc dl, SelectionDAG &DAG,
1249 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001250 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 unsigned LocMemOffset = VA.getLocMemOffset();
1252 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1253 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001255 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001256 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001257}
1258
Dan Gohman98ca4f22009-08-05 01:29:28 +00001259void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001260 SDValue Chain, SDValue &Arg,
1261 RegsToPassVector &RegsToPass,
1262 CCValAssign &VA, CCValAssign &NextVA,
1263 SDValue &StackPtr,
1264 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001265 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001266
Jim Grosbache5165492009-11-09 00:11:35 +00001267 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1270
1271 if (NextVA.isRegLoc())
1272 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1273 else {
1274 assert(NextVA.isMemLoc());
1275 if (StackPtr.getNode() == 0)
1276 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1279 dl, DAG, NextVA,
1280 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001281 }
1282}
1283
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001285/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1286/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001287SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001288ARMTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001289 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001290 bool doesNotRet, bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001292 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 const SmallVectorImpl<ISD::InputArg> &Ins,
1294 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001295 SmallVectorImpl<SDValue> &InVals) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001296 MachineFunction &MF = DAG.getMachineFunction();
1297 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1298 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001299 // Disable tail calls if they're not supported.
1300 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001301 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001302 if (isTailCall) {
1303 // Check if it's really possible to do a tail call.
1304 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1305 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001306 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001307 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1308 // detected sibcalls.
1309 if (isTailCall) {
1310 ++NumTailCalls;
1311 IsSibCall = true;
1312 }
1313 }
Evan Chenga8e29892007-01-19 07:51:42 +00001314
Bob Wilson1f595bb2009-04-17 19:07:39 +00001315 // Analyze operands of the call, assigning locations to each operand.
1316 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001317 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1318 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001319 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001320 CCAssignFnForNode(CallConv, /* Return*/ false,
1321 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001322
Bob Wilson1f595bb2009-04-17 19:07:39 +00001323 // Get a count of how many bytes are to be pushed on the stack.
1324 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001325
Dale Johannesen51e28e62010-06-03 21:09:53 +00001326 // For tail calls, memory operands are available in our caller's stack.
1327 if (IsSibCall)
1328 NumBytes = 0;
1329
Evan Chenga8e29892007-01-19 07:51:42 +00001330 // Adjust the stack pointer for the new arguments...
1331 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001332 if (!IsSibCall)
1333 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001334
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001335 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001336
Bob Wilson5bafff32009-06-22 23:27:02 +00001337 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001338 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001339
Bob Wilson1f595bb2009-04-17 19:07:39 +00001340 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001341 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001342 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1343 i != e;
1344 ++i, ++realArgIdx) {
1345 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001346 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001348 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001349
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350 // Promote the value if needed.
1351 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001352 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 case CCValAssign::Full: break;
1354 case CCValAssign::SExt:
1355 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1356 break;
1357 case CCValAssign::ZExt:
1358 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1359 break;
1360 case CCValAssign::AExt:
1361 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1362 break;
1363 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001364 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001365 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001366 }
1367
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001368 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001369 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001370 if (VA.getLocVT() == MVT::v2f64) {
1371 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1372 DAG.getConstant(0, MVT::i32));
1373 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1374 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375
Dan Gohman98ca4f22009-08-05 01:29:28 +00001376 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001377 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1378
1379 VA = ArgLocs[++i]; // skip ahead to next loc
1380 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001381 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001382 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1383 } else {
1384 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001385
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1387 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001388 }
1389 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001391 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001392 }
1393 } else if (VA.isRegLoc()) {
1394 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001395 } else if (isByVal) {
1396 assert(VA.isMemLoc());
1397 unsigned offset = 0;
1398
1399 // True if this byval aggregate will be split between registers
1400 // and memory.
1401 if (CCInfo.isFirstByValRegValid()) {
1402 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1403 unsigned int i, j;
1404 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1405 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1406 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1407 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1408 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001409 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001410 MemOpChains.push_back(Load.getValue(1));
1411 RegsToPass.push_back(std::make_pair(j, Load));
1412 }
1413 offset = ARM::R4 - CCInfo.getFirstByValReg();
1414 CCInfo.clearFirstByValReg();
1415 }
1416
1417 unsigned LocMemOffset = VA.getLocMemOffset();
1418 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1419 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1420 StkPtrOff);
1421 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1422 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1423 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1424 MVT::i32);
1425 MemOpChains.push_back(DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode,
1426 Flags.getByValAlign(),
1427 /*isVolatile=*/false,
Dan Gohman65fd6562011-11-03 21:49:52 +00001428 /*AlwaysInline=*/false,
Stuart Hastingsc7315872011-04-20 16:47:52 +00001429 MachinePointerInfo(0),
1430 MachinePointerInfo(0)));
1431
1432 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001433 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001434
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1436 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001437 }
Evan Chenga8e29892007-01-19 07:51:42 +00001438 }
1439
1440 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001442 &MemOpChains[0], MemOpChains.size());
1443
1444 // Build a sequence of copy-to-reg nodes chained together with token chain
1445 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001446 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001447 // Tail call byval lowering might overwrite argument registers so in case of
1448 // tail call optimization the copies to registers are lowered later.
1449 if (!isTailCall)
1450 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1451 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1452 RegsToPass[i].second, InFlag);
1453 InFlag = Chain.getValue(1);
1454 }
Evan Chenga8e29892007-01-19 07:51:42 +00001455
Dale Johannesen51e28e62010-06-03 21:09:53 +00001456 // For tail calls lower the arguments to the 'real' stack slot.
1457 if (isTailCall) {
1458 // Force all the incoming stack arguments to be loaded from the stack
1459 // before any new outgoing arguments are stored to the stack, because the
1460 // outgoing stack slots may alias the incoming argument stack slots, and
1461 // the alias isn't otherwise explicit. This is slightly more conservative
1462 // than necessary, because it means that each store effectively depends
1463 // on every argument instead of just those arguments it would clobber.
1464
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001465 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001466 InFlag = SDValue();
1467 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1468 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1469 RegsToPass[i].second, InFlag);
1470 InFlag = Chain.getValue(1);
1471 }
1472 InFlag =SDValue();
1473 }
1474
Bill Wendling056292f2008-09-16 21:48:12 +00001475 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1476 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1477 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001478 bool isDirect = false;
1479 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001480 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001481 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001482
1483 if (EnableARMLongCalls) {
1484 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1485 && "long-calls with non-static relocation model!");
1486 // Handle a global address or an external symbol. If it's not one of
1487 // those, the target's already in a register, so we don't need to do
1488 // anything extra.
1489 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001490 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001491 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001492 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001493 ARMConstantPoolValue *CPV =
1494 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1495
Jim Grosbache7b52522010-04-14 22:28:31 +00001496 // Get the address of the callee into a register
1497 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1498 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1499 Callee = DAG.getLoad(getPointerTy(), dl,
1500 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001501 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001502 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001503 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1504 const char *Sym = S->getSymbol();
1505
1506 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001507 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001508 ARMConstantPoolValue *CPV =
1509 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1510 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001511 // Get the address of the callee into a register
1512 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1513 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1514 Callee = DAG.getLoad(getPointerTy(), dl,
1515 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001516 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001517 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001518 }
1519 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001520 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001521 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001522 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001523 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001524 getTargetMachine().getRelocationModel() != Reloc::Static;
1525 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001526 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001527 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001528 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001529 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001530 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001531 ARMConstantPoolValue *CPV =
1532 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001533 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001535 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001536 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001537 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001538 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001539 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001540 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001541 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001542 } else {
1543 // On ELF targets for PIC code, direct calls should go through the PLT
1544 unsigned OpFlags = 0;
1545 if (Subtarget->isTargetELF() &&
1546 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1547 OpFlags = ARMII::MO_PLT;
1548 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1549 }
Bill Wendling056292f2008-09-16 21:48:12 +00001550 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001551 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001552 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001553 getTargetMachine().getRelocationModel() != Reloc::Static;
1554 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001555 // tBX takes a register source operand.
1556 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001557 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001558 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001559 ARMConstantPoolValue *CPV =
1560 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1561 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001562 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001564 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001565 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001566 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001567 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001568 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001569 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001570 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001571 } else {
1572 unsigned OpFlags = 0;
1573 // On ELF targets for PIC code, direct calls should go through the PLT
1574 if (Subtarget->isTargetELF() &&
1575 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1576 OpFlags = ARMII::MO_PLT;
1577 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1578 }
Evan Chenga8e29892007-01-19 07:51:42 +00001579 }
1580
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001581 // FIXME: handle tail calls differently.
1582 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001583 if (Subtarget->isThumb()) {
1584 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001585 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001586 else if (doesNotRet && isDirect && !isARMFunc &&
1587 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1588 // "mov lr, pc; b _foo" to avoid confusing the RSP
1589 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001590 else
1591 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1592 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001593 if (!isDirect && !Subtarget->hasV5TOps()) {
1594 CallOpc = ARMISD::CALL_NOLINK;
1595 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1596 // "mov lr, pc; b _foo" to avoid confusing the RSP
1597 CallOpc = ARMISD::CALL_NOLINK;
1598 else
1599 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001600 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001601
Dan Gohman475871a2008-07-27 21:46:04 +00001602 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001603 Ops.push_back(Chain);
1604 Ops.push_back(Callee);
1605
1606 // Add argument registers to the end of the list so that they are known live
1607 // into the call.
1608 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1609 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1610 RegsToPass[i].second.getValueType()));
1611
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001612 // Add a register mask operand representing the call-preserved registers.
1613 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1614 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1615 assert(Mask && "Missing call preserved mask for calling convention");
1616 Ops.push_back(DAG.getRegisterMask(Mask));
1617
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001619 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001620
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001621 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001622 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001623 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001624
Duncan Sands4bdcb612008-07-02 17:40:58 +00001625 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001626 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001627 InFlag = Chain.getValue(1);
1628
Chris Lattnere563bbc2008-10-11 22:08:30 +00001629 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1630 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001631 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001632 InFlag = Chain.getValue(1);
1633
Bob Wilson1f595bb2009-04-17 19:07:39 +00001634 // Handle result values, copying them out of physregs into vregs that we
1635 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001636 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1637 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001638}
1639
Stuart Hastingsf222e592011-02-28 17:17:53 +00001640/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001641/// on the stack. Remember the next parameter register to allocate,
1642/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001643/// this.
1644void
Stuart Hastingsc7315872011-04-20 16:47:52 +00001645llvm::ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
1646 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1647 assert((State->getCallOrPrologue() == Prologue ||
1648 State->getCallOrPrologue() == Call) &&
1649 "unhandled ParmContext");
1650 if ((!State->isFirstByValRegValid()) &&
1651 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1652 State->setFirstByValReg(reg);
1653 // At a call site, a byval parameter that is split between
1654 // registers and memory needs its size truncated here. In a
1655 // function prologue, such byval parameters are reassembled in
1656 // memory, and are not truncated.
1657 if (State->getCallOrPrologue() == Call) {
1658 unsigned excess = 4 * (ARM::R4 - reg);
1659 assert(size >= excess && "expected larger existing stack allocation");
1660 size -= excess;
1661 }
1662 }
1663 // Confiscate any remaining parameter registers to preclude their
1664 // assignment to subsequent parameters.
1665 while (State->AllocateReg(GPRArgRegs, 4))
1666 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001667}
1668
Dale Johannesen51e28e62010-06-03 21:09:53 +00001669/// MatchingStackOffset - Return true if the given stack call argument is
1670/// already available in the same position (relatively) of the caller's
1671/// incoming argument stack.
1672static
1673bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1674 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1675 const ARMInstrInfo *TII) {
1676 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1677 int FI = INT_MAX;
1678 if (Arg.getOpcode() == ISD::CopyFromReg) {
1679 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001680 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001681 return false;
1682 MachineInstr *Def = MRI->getVRegDef(VR);
1683 if (!Def)
1684 return false;
1685 if (!Flags.isByVal()) {
1686 if (!TII->isLoadFromStackSlot(Def, FI))
1687 return false;
1688 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001689 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001690 }
1691 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1692 if (Flags.isByVal())
1693 // ByVal argument is passed in as a pointer but it's now being
1694 // dereferenced. e.g.
1695 // define @foo(%struct.X* %A) {
1696 // tail call @bar(%struct.X* byval %A)
1697 // }
1698 return false;
1699 SDValue Ptr = Ld->getBasePtr();
1700 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1701 if (!FINode)
1702 return false;
1703 FI = FINode->getIndex();
1704 } else
1705 return false;
1706
1707 assert(FI != INT_MAX);
1708 if (!MFI->isFixedObjectIndex(FI))
1709 return false;
1710 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1711}
1712
1713/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1714/// for tail call optimization. Targets which want to do tail call
1715/// optimization should implement this function.
1716bool
1717ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1718 CallingConv::ID CalleeCC,
1719 bool isVarArg,
1720 bool isCalleeStructRet,
1721 bool isCallerStructRet,
1722 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001723 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001724 const SmallVectorImpl<ISD::InputArg> &Ins,
1725 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001726 const Function *CallerF = DAG.getMachineFunction().getFunction();
1727 CallingConv::ID CallerCC = CallerF->getCallingConv();
1728 bool CCMatch = CallerCC == CalleeCC;
1729
1730 // Look for obvious safe cases to perform tail call optimization that do not
1731 // require ABI changes. This is what gcc calls sibcall.
1732
Jim Grosbach7616b642010-06-16 23:45:49 +00001733 // Do not sibcall optimize vararg calls unless the call site is not passing
1734 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001735 if (isVarArg && !Outs.empty())
1736 return false;
1737
1738 // Also avoid sibcall optimization if either caller or callee uses struct
1739 // return semantics.
1740 if (isCalleeStructRet || isCallerStructRet)
1741 return false;
1742
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001743 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001744 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1745 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1746 // support in the assembler and linker to be used. This would need to be
1747 // fixed to fully support tail calls in Thumb1.
1748 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001749 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1750 // LR. This means if we need to reload LR, it takes an extra instructions,
1751 // which outweighs the value of the tail call; but here we don't know yet
1752 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001753 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001754 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001755
1756 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1757 // but we need to make sure there are enough registers; the only valid
1758 // registers are the 4 used for parameters. We don't currently do this
1759 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001760 if (Subtarget->isThumb1Only())
1761 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001762
Dale Johannesen51e28e62010-06-03 21:09:53 +00001763 // If the calling conventions do not match, then we'd better make sure the
1764 // results are returned in the same way as what the caller expects.
1765 if (!CCMatch) {
1766 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001767 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1768 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001769 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1770
1771 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001772 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1773 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001774 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1775
1776 if (RVLocs1.size() != RVLocs2.size())
1777 return false;
1778 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1779 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1780 return false;
1781 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1782 return false;
1783 if (RVLocs1[i].isRegLoc()) {
1784 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1785 return false;
1786 } else {
1787 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1788 return false;
1789 }
1790 }
1791 }
1792
1793 // If the callee takes no arguments then go on to check the results of the
1794 // call.
1795 if (!Outs.empty()) {
1796 // Check if stack adjustment is needed. For now, do not do this if any
1797 // argument is passed on the stack.
1798 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001799 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1800 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001801 CCInfo.AnalyzeCallOperands(Outs,
1802 CCAssignFnForNode(CalleeCC, false, isVarArg));
1803 if (CCInfo.getNextStackOffset()) {
1804 MachineFunction &MF = DAG.getMachineFunction();
1805
1806 // Check if the arguments are already laid out in the right way as
1807 // the caller's fixed stack objects.
1808 MachineFrameInfo *MFI = MF.getFrameInfo();
1809 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1810 const ARMInstrInfo *TII =
1811 ((ARMTargetMachine&)getTargetMachine()).getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001812 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1813 i != e;
1814 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001815 CCValAssign &VA = ArgLocs[i];
1816 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001817 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001818 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001819 if (VA.getLocInfo() == CCValAssign::Indirect)
1820 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001821 if (VA.needsCustom()) {
1822 // f64 and vector types are split into multiple registers or
1823 // register/stack-slot combinations. The types will not match
1824 // the registers; give up on memory f64 refs until we figure
1825 // out what to do about this.
1826 if (!VA.isRegLoc())
1827 return false;
1828 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001829 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001830 if (RegVT == MVT::v2f64) {
1831 if (!ArgLocs[++i].isRegLoc())
1832 return false;
1833 if (!ArgLocs[++i].isRegLoc())
1834 return false;
1835 }
1836 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001837 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1838 MFI, MRI, TII))
1839 return false;
1840 }
1841 }
1842 }
1843 }
1844
1845 return true;
1846}
1847
Dan Gohman98ca4f22009-08-05 01:29:28 +00001848SDValue
1849ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001850 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001852 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001853 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001854
Bob Wilsondee46d72009-04-17 20:35:10 +00001855 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001856 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001857
Bob Wilsondee46d72009-04-17 20:35:10 +00001858 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001859 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1860 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001861
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001863 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1864 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001865
1866 // If this is the first return lowered for this function, add
1867 // the regs to the liveout set for the function.
1868 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1869 for (unsigned i = 0; i != RVLocs.size(); ++i)
1870 if (RVLocs[i].isRegLoc())
1871 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001872 }
1873
Bob Wilson1f595bb2009-04-17 19:07:39 +00001874 SDValue Flag;
1875
1876 // Copy the result values into the output registers.
1877 for (unsigned i = 0, realRVLocIdx = 0;
1878 i != RVLocs.size();
1879 ++i, ++realRVLocIdx) {
1880 CCValAssign &VA = RVLocs[i];
1881 assert(VA.isRegLoc() && "Can only return in registers!");
1882
Dan Gohmanc9403652010-07-07 15:54:55 +00001883 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001884
1885 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001886 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001887 case CCValAssign::Full: break;
1888 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001889 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001890 break;
1891 }
1892
Bob Wilson1f595bb2009-04-17 19:07:39 +00001893 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001895 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1897 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001898 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001900
1901 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1902 Flag = Chain.getValue(1);
1903 VA = RVLocs[++i]; // skip ahead to next loc
1904 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1905 HalfGPRs.getValue(1), Flag);
1906 Flag = Chain.getValue(1);
1907 VA = RVLocs[++i]; // skip ahead to next loc
1908
1909 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1911 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001912 }
1913 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1914 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001915 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001917 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001918 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001919 VA = RVLocs[++i]; // skip ahead to next loc
1920 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1921 Flag);
1922 } else
1923 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1924
Bob Wilsondee46d72009-04-17 20:35:10 +00001925 // Guarantee that all emitted copies are
1926 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 Flag = Chain.getValue(1);
1928 }
1929
1930 SDValue result;
1931 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001933 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001934 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001935
1936 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001937}
1938
Evan Cheng3d2125c2010-11-30 23:55:39 +00001939bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N) const {
1940 if (N->getNumValues() != 1)
1941 return false;
1942 if (!N->hasNUsesOfValue(1, 0))
1943 return false;
1944
1945 unsigned NumCopies = 0;
Jason W Kim1de886c2012-02-10 16:07:59 +00001946 SDNode* Copies[2] = { 0, 0 };
Evan Cheng3d2125c2010-11-30 23:55:39 +00001947 SDNode *Use = *N->use_begin();
1948 if (Use->getOpcode() == ISD::CopyToReg) {
1949 Copies[NumCopies++] = Use;
1950 } else if (Use->getOpcode() == ARMISD::VMOVRRD) {
1951 // f64 returned in a pair of GPRs.
1952 for (SDNode::use_iterator UI = Use->use_begin(), UE = Use->use_end();
1953 UI != UE; ++UI) {
1954 if (UI->getOpcode() != ISD::CopyToReg)
1955 return false;
1956 Copies[UI.getUse().getResNo()] = *UI;
1957 ++NumCopies;
1958 }
1959 } else if (Use->getOpcode() == ISD::BITCAST) {
1960 // f32 returned in a single GPR.
1961 if (!Use->hasNUsesOfValue(1, 0))
1962 return false;
1963 Use = *Use->use_begin();
1964 if (Use->getOpcode() != ISD::CopyToReg || !Use->hasNUsesOfValue(1, 0))
1965 return false;
1966 Copies[NumCopies++] = Use;
1967 } else {
1968 return false;
1969 }
1970
1971 if (NumCopies != 1 && NumCopies != 2)
1972 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001973
1974 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001975 for (unsigned i = 0; i < NumCopies; ++i) {
1976 SDNode *Copy = Copies[i];
1977 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1978 UI != UE; ++UI) {
1979 if (UI->getOpcode() == ISD::CopyToReg) {
1980 SDNode *Use = *UI;
Jason W Kim1de886c2012-02-10 16:07:59 +00001981 if (Use == Copies[0] || ((NumCopies == 2) && (Use == Copies[1])))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001982 continue;
1983 return false;
1984 }
1985 if (UI->getOpcode() != ARMISD::RET_FLAG)
1986 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001987 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001988 }
1989 }
1990
Evan Cheng1bf891a2010-12-01 22:59:46 +00001991 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992}
1993
Evan Cheng485fafc2011-03-21 01:19:09 +00001994bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1995 if (!EnableARMTailCalls)
1996 return false;
1997
1998 if (!CI->isTailCall())
1999 return false;
2000
2001 return !Subtarget->isThumb1Only();
2002}
2003
Bob Wilsonb62d2572009-11-03 00:02:05 +00002004// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2005// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2006// one of the above mentioned nodes. It has to be wrapped because otherwise
2007// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2008// be used to form addressing mode. These wrapped nodes will be selected
2009// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002010static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002011 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002012 // FIXME there is no actual debug info here
2013 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002014 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002016 if (CP->isMachineConstantPoolEntry())
2017 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2018 CP->getAlignment());
2019 else
2020 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2021 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002022 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002023}
2024
Jim Grosbache1102ca2010-07-19 17:20:38 +00002025unsigned ARMTargetLowering::getJumpTableEncoding() const {
2026 return MachineJumpTableInfo::EK_Inline;
2027}
2028
Dan Gohmand858e902010-04-17 15:26:15 +00002029SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2030 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002031 MachineFunction &MF = DAG.getMachineFunction();
2032 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2033 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002034 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002035 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002036 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002037 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2038 SDValue CPAddr;
2039 if (RelocM == Reloc::Static) {
2040 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2041 } else {
2042 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002043 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002044 ARMConstantPoolValue *CPV =
2045 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2046 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002047 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2048 }
2049 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2050 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002051 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002052 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002053 if (RelocM == Reloc::Static)
2054 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002055 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002056 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002057}
2058
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002059// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002060SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002061ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002062 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002063 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002064 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002065 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002066 MachineFunction &MF = DAG.getMachineFunction();
2067 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002068 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002069 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002070 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2071 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002072 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002073 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002074 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002075 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002076 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002077 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002078
Evan Chenge7e0d622009-11-06 22:24:13 +00002079 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002080 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002081
2082 // call __tls_get_addr.
2083 ArgListTy Args;
2084 ArgListEntry Entry;
2085 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002086 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002087 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002088 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002089 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002090 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002091 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002092 0, CallingConv::C, /*isTailCall=*/false,
2093 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002094 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002095 return CallResult.first;
2096}
2097
2098// Lower ISD::GlobalTLSAddress using the "initial exec" or
2099// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002100SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002101ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002102 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002103 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002104 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002105 SDValue Offset;
2106 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002107 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002108 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002109 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002110
Chris Lattner4fb63d02009-07-15 04:12:33 +00002111 if (GV->isDeclaration()) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002112 MachineFunction &MF = DAG.getMachineFunction();
2113 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002114 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002115 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002116 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2117 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002118 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2119 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2120 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002121 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002123 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002124 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002125 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002126 Chain = Offset.getValue(1);
2127
Evan Chenge7e0d622009-11-06 22:24:13 +00002128 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002129 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002130
Evan Cheng9eda6892009-10-31 03:39:36 +00002131 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002132 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002133 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002134 } else {
2135 // local exec model
Bill Wendling5bb77992011-10-01 08:00:54 +00002136 ARMConstantPoolValue *CPV =
2137 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002138 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002139 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002140 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002141 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002142 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002143 }
2144
2145 // The address of the thread local variable is the add of the thread
2146 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002147 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002148}
2149
Dan Gohman475871a2008-07-27 21:46:04 +00002150SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002151ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002152 // TODO: implement the "local dynamic" model
2153 assert(Subtarget->isTargetELF() &&
2154 "TLS not implemented for non-ELF targets");
2155 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2156 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
2157 // otherwise use the "Local Exec" TLS Model
2158 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
2159 return LowerToTLSGeneralDynamicModel(GA, DAG);
2160 else
2161 return LowerToTLSExecModels(GA, DAG);
2162}
2163
Dan Gohman475871a2008-07-27 21:46:04 +00002164SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002165 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002166 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002167 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002168 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002169 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2170 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002171 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002172 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002173 ARMConstantPoolConstant::Create(GV,
2174 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002175 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002177 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002178 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002179 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002180 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002181 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002182 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002183 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002184 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002185 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002186 MachinePointerInfo::getGOT(),
2187 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002188 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002189 }
2190
2191 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002192 // pair. This is always cheaper.
2193 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002194 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002195 // FIXME: Once remat is capable of dealing with instructions with register
2196 // operands, expand this into two nodes.
2197 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2198 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002199 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002200 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2201 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2202 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2203 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002204 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002205 }
2206}
2207
Dan Gohman475871a2008-07-27 21:46:04 +00002208SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002209 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002210 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002212 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002213 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002214 MachineFunction &MF = DAG.getMachineFunction();
2215 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2216
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002217 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2218 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002219 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002220 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002221 // FIXME: Once remat is capable of dealing with instructions with register
2222 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002223 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002224 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2225 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2226
Evan Cheng53519f02011-01-21 18:55:51 +00002227 unsigned Wrapper = (RelocM == Reloc::PIC_)
2228 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2229 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002230 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002231 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2232 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002233 MachinePointerInfo::getGOT(),
2234 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002235 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002236 }
2237
2238 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002239 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002240 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002241 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002242 } else {
2243 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002244 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2245 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002246 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2247 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002248 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002249 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002250 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002251
Evan Cheng9eda6892009-10-31 03:39:36 +00002252 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002253 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002254 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002255 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002256
2257 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002258 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002259 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002260 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002261
Evan Cheng63476a82009-09-03 07:04:02 +00002262 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002263 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002264 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002265
2266 return Result;
2267}
2268
Dan Gohman475871a2008-07-27 21:46:04 +00002269SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002270 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002271 assert(Subtarget->isTargetELF() &&
2272 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002273 MachineFunction &MF = DAG.getMachineFunction();
2274 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002275 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002276 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002277 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002278 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002279 ARMConstantPoolValue *CPV =
2280 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2281 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002282 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002284 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002285 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002286 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002287 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002288 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002289}
2290
Jim Grosbach0e0da732009-05-12 23:59:14 +00002291SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002292ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2293 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002294 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002295 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2296 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002297 Op.getOperand(1), Val);
2298}
2299
2300SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002301ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2302 DebugLoc dl = Op.getDebugLoc();
2303 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2304 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2305}
2306
2307SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002308ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002309 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002310 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002311 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002312 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002313 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002314 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002315 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002316 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2317 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002318 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002319 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002320 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002321 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002322 EVT PtrVT = getPointerTy();
2323 DebugLoc dl = Op.getDebugLoc();
2324 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2325 SDValue CPAddr;
2326 unsigned PCAdj = (RelocM != Reloc::PIC_)
2327 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002328 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002329 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2330 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002331 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002333 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002334 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002335 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002336 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002337
2338 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002339 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002340 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2341 }
2342 return Result;
2343 }
Evan Cheng92e39162011-03-29 23:06:19 +00002344 case Intrinsic::arm_neon_vmulls:
2345 case Intrinsic::arm_neon_vmullu: {
2346 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2347 ? ARMISD::VMULLs : ARMISD::VMULLu;
2348 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2349 Op.getOperand(1), Op.getOperand(2));
2350 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002351 }
2352}
2353
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002354static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002355 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002356 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002357 if (!Subtarget->hasDataBarrier()) {
2358 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2359 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2360 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002361 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002362 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002363 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002364 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002365 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002366
2367 SDValue Op5 = Op.getOperand(5);
2368 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2369 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2370 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2371 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2372
2373 ARM_MB::MemBOpt DMBOpt;
2374 if (isDeviceBarrier)
2375 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2376 else
2377 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2378 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2379 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002380}
2381
Eli Friedman26689ac2011-08-03 21:06:02 +00002382
2383static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2384 const ARMSubtarget *Subtarget) {
2385 // FIXME: handle "fence singlethread" more efficiently.
2386 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002387 if (!Subtarget->hasDataBarrier()) {
2388 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2389 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2390 // here.
2391 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2392 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002393 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002394 DAG.getConstant(0, MVT::i32));
2395 }
2396
Eli Friedman26689ac2011-08-03 21:06:02 +00002397 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002398 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002399}
2400
Evan Chengdfed19f2010-11-03 06:34:55 +00002401static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2402 const ARMSubtarget *Subtarget) {
2403 // ARM pre v5TE and Thumb1 does not have preload instructions.
2404 if (!(Subtarget->isThumb2() ||
2405 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2406 // Just preserve the chain.
2407 return Op.getOperand(0);
2408
2409 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002410 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2411 if (!isRead &&
2412 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2413 // ARMv7 with MP extension has PLDW.
2414 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002415
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002416 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2417 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002418 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002419 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002420 isData = ~isData & 1;
2421 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002422
2423 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002424 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2425 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002426}
2427
Dan Gohman1e93df62010-04-17 14:41:14 +00002428static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2429 MachineFunction &MF = DAG.getMachineFunction();
2430 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2431
Evan Chenga8e29892007-01-19 07:51:42 +00002432 // vastart just stores the address of the VarArgsFrameIndex slot into the
2433 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002434 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002435 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002436 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002437 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002438 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2439 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002440}
2441
Dan Gohman475871a2008-07-27 21:46:04 +00002442SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002443ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2444 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002445 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002446 MachineFunction &MF = DAG.getMachineFunction();
2447 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2448
Craig Topper44d23822012-02-22 05:59:10 +00002449 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002450 if (AFI->isThumb1OnlyFunction())
Bob Wilson5bafff32009-06-22 23:27:02 +00002451 RC = ARM::tGPRRegisterClass;
2452 else
2453 RC = ARM::GPRRegisterClass;
2454
2455 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002456 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002457 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002458
2459 SDValue ArgValue2;
2460 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002461 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002462 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002463
2464 // Create load node to retrieve arguments from the stack.
2465 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002466 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002467 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002468 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002469 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002470 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002471 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002472 }
2473
Jim Grosbache5165492009-11-09 00:11:35 +00002474 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002475}
2476
Stuart Hastingsc7315872011-04-20 16:47:52 +00002477void
2478ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2479 unsigned &VARegSize, unsigned &VARegSaveSize)
2480 const {
2481 unsigned NumGPRs;
2482 if (CCInfo.isFirstByValRegValid())
2483 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2484 else {
2485 unsigned int firstUnalloced;
2486 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2487 sizeof(GPRArgRegs) /
2488 sizeof(GPRArgRegs[0]));
2489 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2490 }
2491
2492 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2493 VARegSize = NumGPRs * 4;
2494 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2495}
2496
2497// The remaining GPRs hold either the beginning of variable-argument
2498// data, or the beginning of an aggregate passed by value (usuall
2499// byval). Either way, we allocate stack slots adjacent to the data
2500// provided by our caller, and store the unallocated registers there.
2501// If this is a variadic function, the va_list pointer will begin with
2502// these values; otherwise, this reassembles a (byval) structure that
2503// was split between registers and memory.
2504void
2505ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2506 DebugLoc dl, SDValue &Chain,
2507 unsigned ArgOffset) const {
2508 MachineFunction &MF = DAG.getMachineFunction();
2509 MachineFrameInfo *MFI = MF.getFrameInfo();
2510 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2511 unsigned firstRegToSaveIndex;
2512 if (CCInfo.isFirstByValRegValid())
2513 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2514 else {
2515 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2516 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2517 }
2518
2519 unsigned VARegSize, VARegSaveSize;
2520 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2521 if (VARegSaveSize) {
2522 // If this function is vararg, store any remaining integer argument regs
2523 // to their spots on the stack so that they may be loaded by deferencing
2524 // the result of va_next.
2525 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002526 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2527 ArgOffset + VARegSaveSize
2528 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002529 false));
2530 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2531 getPointerTy());
2532
2533 SmallVector<SDValue, 4> MemOps;
2534 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002535 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002536 if (AFI->isThumb1OnlyFunction())
2537 RC = ARM::tGPRRegisterClass;
2538 else
2539 RC = ARM::GPRRegisterClass;
2540
2541 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2542 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2543 SDValue Store =
2544 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002545 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002546 false, false, 0);
2547 MemOps.push_back(Store);
2548 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2549 DAG.getConstant(4, getPointerTy()));
2550 }
2551 if (!MemOps.empty())
2552 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2553 &MemOps[0], MemOps.size());
2554 } else
2555 // This will point to the next argument passed via stack.
2556 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2557}
2558
Bob Wilson5bafff32009-06-22 23:27:02 +00002559SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002561 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002562 const SmallVectorImpl<ISD::InputArg>
2563 &Ins,
2564 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002565 SmallVectorImpl<SDValue> &InVals)
2566 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002567 MachineFunction &MF = DAG.getMachineFunction();
2568 MachineFrameInfo *MFI = MF.getFrameInfo();
2569
Bob Wilson1f595bb2009-04-17 19:07:39 +00002570 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2571
2572 // Assign locations to all of the incoming arguments.
2573 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002574 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2575 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002576 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002577 CCAssignFnForNode(CallConv, /* Return*/ false,
2578 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002579
2580 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002581 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002582
Stuart Hastingsf222e592011-02-28 17:17:53 +00002583 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002584 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2585 CCValAssign &VA = ArgLocs[i];
2586
Bob Wilsondee46d72009-04-17 20:35:10 +00002587 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002588 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002589 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002590
Bob Wilson1f595bb2009-04-17 19:07:39 +00002591 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002592 // f64 and vector types are split up into multiple registers or
2593 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002594 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002595 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002596 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002597 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002598 SDValue ArgValue2;
2599 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002600 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002601 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2602 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002603 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002604 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002605 } else {
2606 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2607 Chain, DAG, dl);
2608 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002609 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2610 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002611 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002612 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002613 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2614 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002615 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616
Bob Wilson5bafff32009-06-22 23:27:02 +00002617 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002618 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002619
Owen Anderson825b72b2009-08-11 20:47:22 +00002620 if (RegVT == MVT::f32)
Bob Wilson5bafff32009-06-22 23:27:02 +00002621 RC = ARM::SPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 else if (RegVT == MVT::f64)
Bob Wilson5bafff32009-06-22 23:27:02 +00002623 RC = ARM::DPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002624 else if (RegVT == MVT::v2f64)
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002625 RC = ARM::QPRRegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002626 else if (RegVT == MVT::i32)
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002627 RC = (AFI->isThumb1OnlyFunction() ?
2628 ARM::tGPRRegisterClass : ARM::GPRRegisterClass);
Bob Wilson5bafff32009-06-22 23:27:02 +00002629 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002630 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002631
2632 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002633 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002634 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002635 }
2636
2637 // If this is an 8 or 16-bit value, it is really passed promoted
2638 // to 32 bits. Insert an assert[sz]ext to capture this, then
2639 // truncate to the right size.
2640 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002641 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002642 case CCValAssign::Full: break;
2643 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002644 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002645 break;
2646 case CCValAssign::SExt:
2647 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2648 DAG.getValueType(VA.getValVT()));
2649 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2650 break;
2651 case CCValAssign::ZExt:
2652 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2653 DAG.getValueType(VA.getValVT()));
2654 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2655 break;
2656 }
2657
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002659
2660 } else { // VA.isRegLoc()
2661
2662 // sanity check
2663 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002664 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002665
Stuart Hastingsf222e592011-02-28 17:17:53 +00002666 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002667
Stuart Hastingsf222e592011-02-28 17:17:53 +00002668 // Some Ins[] entries become multiple ArgLoc[] entries.
2669 // Process them only once.
2670 if (index != lastInsIndex)
2671 {
2672 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002673 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002674 // This can be changed with more analysis.
2675 // In case of tail call optimization mark all arguments mutable.
2676 // Since they could be overwritten by lowering of arguments in case of
2677 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002678 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002679 unsigned VARegSize, VARegSaveSize;
2680 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2681 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2682 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002683 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002684 int FI = MFI->CreateFixedObject(Bytes,
2685 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002686 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2687 } else {
2688 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2689 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002690
Stuart Hastingsf222e592011-02-28 17:17:53 +00002691 // Create load nodes to retrieve arguments from the stack.
2692 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2693 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2694 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002695 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002696 }
2697 lastInsIndex = index;
2698 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002699 }
2700 }
2701
2702 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002703 if (isVarArg)
2704 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002705
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002707}
2708
2709/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002710static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002711 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002712 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002713 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002714 // Maybe this has already been legalized into the constant pool?
2715 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002716 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002717 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002718 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002719 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002720 }
2721 }
2722 return false;
2723}
2724
Evan Chenga8e29892007-01-19 07:51:42 +00002725/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2726/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002727SDValue
2728ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002729 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002730 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002731 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002732 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002733 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002734 // Constant does not fit, try adjusting it by one?
2735 switch (CC) {
2736 default: break;
2737 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002738 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002739 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002740 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002741 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002742 }
2743 break;
2744 case ISD::SETULT:
2745 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002746 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002747 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002748 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002749 }
2750 break;
2751 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002752 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002753 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002754 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002755 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002756 }
2757 break;
2758 case ISD::SETULE:
2759 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002760 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002761 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002762 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002763 }
2764 break;
2765 }
2766 }
2767 }
2768
2769 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002770 ARMISD::NodeType CompareType;
2771 switch (CondCode) {
2772 default:
2773 CompareType = ARMISD::CMP;
2774 break;
2775 case ARMCC::EQ:
2776 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002777 // Uses only Z Flag
2778 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002779 break;
2780 }
Evan Cheng218977b2010-07-13 19:27:42 +00002781 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002782 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002783}
2784
2785/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002786SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002787ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002788 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002789 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002790 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002791 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002792 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002793 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2794 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002795}
2796
Bob Wilson79f56c92011-03-08 01:17:20 +00002797/// duplicateCmp - Glue values can have only one use, so this function
2798/// duplicates a comparison node.
2799SDValue
2800ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2801 unsigned Opc = Cmp.getOpcode();
2802 DebugLoc DL = Cmp.getDebugLoc();
2803 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2804 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2805
2806 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2807 Cmp = Cmp.getOperand(0);
2808 Opc = Cmp.getOpcode();
2809 if (Opc == ARMISD::CMPFP)
2810 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2811 else {
2812 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2813 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2814 }
2815 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2816}
2817
Bill Wendlingde2b1512010-08-11 08:43:16 +00002818SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2819 SDValue Cond = Op.getOperand(0);
2820 SDValue SelectTrue = Op.getOperand(1);
2821 SDValue SelectFalse = Op.getOperand(2);
2822 DebugLoc dl = Op.getDebugLoc();
2823
2824 // Convert:
2825 //
2826 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2827 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2828 //
2829 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2830 const ConstantSDNode *CMOVTrue =
2831 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2832 const ConstantSDNode *CMOVFalse =
2833 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2834
2835 if (CMOVTrue && CMOVFalse) {
2836 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2837 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2838
2839 SDValue True;
2840 SDValue False;
2841 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2842 True = SelectTrue;
2843 False = SelectFalse;
2844 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2845 True = SelectFalse;
2846 False = SelectTrue;
2847 }
2848
2849 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002850 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002851 SDValue ARMcc = Cond.getOperand(2);
2852 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002853 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002854 assert(True.getValueType() == VT);
2855 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002856 }
2857 }
2858 }
2859
Dan Gohmandb953892012-02-24 00:09:36 +00002860 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2861 // undefined bits before doing a full-word comparison with zero.
2862 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2863 DAG.getConstant(1, Cond.getValueType()));
2864
Bill Wendlingde2b1512010-08-11 08:43:16 +00002865 return DAG.getSelectCC(dl, Cond,
2866 DAG.getConstant(0, Cond.getValueType()),
2867 SelectTrue, SelectFalse, ISD::SETNE);
2868}
2869
Dan Gohmand858e902010-04-17 15:26:15 +00002870SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002871 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002872 SDValue LHS = Op.getOperand(0);
2873 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002874 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002875 SDValue TrueVal = Op.getOperand(2);
2876 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002877 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002878
Owen Anderson825b72b2009-08-11 20:47:22 +00002879 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002880 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002881 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002882 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002883 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002884 }
2885
2886 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002887 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002888
Evan Cheng218977b2010-07-13 19:27:42 +00002889 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2890 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002892 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002893 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002894 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002895 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002896 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002897 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002898 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002899 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002900 }
2901 return Result;
2902}
2903
Evan Cheng218977b2010-07-13 19:27:42 +00002904/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2905/// to morph to an integer compare sequence.
2906static bool canChangeToInt(SDValue Op, bool &SeenZero,
2907 const ARMSubtarget *Subtarget) {
2908 SDNode *N = Op.getNode();
2909 if (!N->hasOneUse())
2910 // Otherwise it requires moving the value from fp to integer registers.
2911 return false;
2912 if (!N->getNumValues())
2913 return false;
2914 EVT VT = Op.getValueType();
2915 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2916 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2917 // vmrs are very slow, e.g. cortex-a8.
2918 return false;
2919
2920 if (isFloatingPointZero(Op)) {
2921 SeenZero = true;
2922 return true;
2923 }
2924 return ISD::isNormalLoad(N);
2925}
2926
2927static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2928 if (isFloatingPointZero(Op))
2929 return DAG.getConstant(0, MVT::i32);
2930
2931 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2932 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002933 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002934 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002935 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002936
2937 llvm_unreachable("Unknown VFP cmp argument!");
2938}
2939
2940static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2941 SDValue &RetVal1, SDValue &RetVal2) {
2942 if (isFloatingPointZero(Op)) {
2943 RetVal1 = DAG.getConstant(0, MVT::i32);
2944 RetVal2 = DAG.getConstant(0, MVT::i32);
2945 return;
2946 }
2947
2948 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2949 SDValue Ptr = Ld->getBasePtr();
2950 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2951 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002952 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002953 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002954 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002955
2956 EVT PtrType = Ptr.getValueType();
2957 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2958 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2959 PtrType, Ptr, DAG.getConstant(4, PtrType));
2960 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2961 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002962 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002963 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002964 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002965 return;
2966 }
2967
2968 llvm_unreachable("Unknown VFP cmp argument!");
2969}
2970
2971/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2972/// f32 and even f64 comparisons to integer ones.
2973SDValue
2974ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2975 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002976 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002977 SDValue LHS = Op.getOperand(2);
2978 SDValue RHS = Op.getOperand(3);
2979 SDValue Dest = Op.getOperand(4);
2980 DebugLoc dl = Op.getDebugLoc();
2981
Evan Chengfc501a32012-03-01 23:27:13 +00002982 bool LHSSeenZero = false;
2983 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
2984 bool RHSSeenZero = false;
2985 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
2986 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00002987 // If unsafe fp math optimization is enabled and there are no other uses of
2988 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00002989 // to an integer comparison.
2990 if (CC == ISD::SETOEQ)
2991 CC = ISD::SETEQ;
2992 else if (CC == ISD::SETUNE)
2993 CC = ISD::SETNE;
2994
Evan Chengfc501a32012-03-01 23:27:13 +00002995 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002996 SDValue ARMcc;
2997 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00002998 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
2999 bitcastf32Toi32(LHS, DAG), Mask);
3000 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3001 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003002 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3003 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3004 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3005 Chain, Dest, ARMcc, CCR, Cmp);
3006 }
3007
3008 SDValue LHS1, LHS2;
3009 SDValue RHS1, RHS2;
3010 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3011 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003012 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3013 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003014 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3015 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003016 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003017 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3018 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3019 }
3020
3021 return SDValue();
3022}
3023
3024SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3025 SDValue Chain = Op.getOperand(0);
3026 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3027 SDValue LHS = Op.getOperand(2);
3028 SDValue RHS = Op.getOperand(3);
3029 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003030 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003031
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003033 SDValue ARMcc;
3034 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003037 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003038 }
3039
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003041
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003042 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003043 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3044 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3045 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3046 if (Result.getNode())
3047 return Result;
3048 }
3049
Evan Chenga8e29892007-01-19 07:51:42 +00003050 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003051 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003052
Evan Cheng218977b2010-07-13 19:27:42 +00003053 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3054 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003055 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003056 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003057 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003058 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003059 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003060 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3061 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003062 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003063 }
3064 return Res;
3065}
3066
Dan Gohmand858e902010-04-17 15:26:15 +00003067SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003068 SDValue Chain = Op.getOperand(0);
3069 SDValue Table = Op.getOperand(1);
3070 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003071 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003072
Owen Andersone50ed302009-08-10 22:56:29 +00003073 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003074 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3075 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003076 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003079 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3080 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003081 if (Subtarget->isThumb2()) {
3082 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3083 // which does another jump to the destination. This also makes it easier
3084 // to translate it to TBB / TBH later.
3085 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003087 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003088 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003089 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003090 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003091 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003092 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003093 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003094 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003096 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003097 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003098 MachinePointerInfo::getJumpTable(),
3099 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003100 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003101 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003102 }
Evan Chenga8e29892007-01-19 07:51:42 +00003103}
3104
Eli Friedman14e809c2011-11-09 23:36:02 +00003105static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003106 EVT VT = Op.getValueType();
3107 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003108
James Molloy873fd5f2012-02-20 09:24:05 +00003109 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3110 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3111 return Op;
3112 return DAG.UnrollVectorOp(Op.getNode());
3113 }
3114
3115 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3116 "Invalid type for custom lowering!");
3117 if (VT != MVT::v4i16)
3118 return DAG.UnrollVectorOp(Op.getNode());
3119
3120 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3121 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003122}
3123
Bob Wilson76a312b2010-03-19 22:51:32 +00003124static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003125 EVT VT = Op.getValueType();
3126 if (VT.isVector())
3127 return LowerVectorFP_TO_INT(Op, DAG);
3128
Bob Wilson76a312b2010-03-19 22:51:32 +00003129 DebugLoc dl = Op.getDebugLoc();
3130 unsigned Opc;
3131
3132 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003133 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003134 case ISD::FP_TO_SINT:
3135 Opc = ARMISD::FTOSI;
3136 break;
3137 case ISD::FP_TO_UINT:
3138 Opc = ARMISD::FTOUI;
3139 break;
3140 }
3141 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003142 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003143}
3144
Cameron Zwarich3007d332011-03-29 21:41:55 +00003145static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3146 EVT VT = Op.getValueType();
3147 DebugLoc dl = Op.getDebugLoc();
3148
Eli Friedman14e809c2011-11-09 23:36:02 +00003149 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3150 if (VT.getVectorElementType() == MVT::f32)
3151 return Op;
3152 return DAG.UnrollVectorOp(Op.getNode());
3153 }
3154
Duncan Sands1f6a3292011-08-12 14:54:45 +00003155 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3156 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003157 if (VT != MVT::v4f32)
3158 return DAG.UnrollVectorOp(Op.getNode());
3159
3160 unsigned CastOpc;
3161 unsigned Opc;
3162 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003163 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003164 case ISD::SINT_TO_FP:
3165 CastOpc = ISD::SIGN_EXTEND;
3166 Opc = ISD::SINT_TO_FP;
3167 break;
3168 case ISD::UINT_TO_FP:
3169 CastOpc = ISD::ZERO_EXTEND;
3170 Opc = ISD::UINT_TO_FP;
3171 break;
3172 }
3173
3174 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3175 return DAG.getNode(Opc, dl, VT, Op);
3176}
3177
Bob Wilson76a312b2010-03-19 22:51:32 +00003178static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3179 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003180 if (VT.isVector())
3181 return LowerVectorINT_TO_FP(Op, DAG);
3182
Bob Wilson76a312b2010-03-19 22:51:32 +00003183 DebugLoc dl = Op.getDebugLoc();
3184 unsigned Opc;
3185
3186 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003187 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003188 case ISD::SINT_TO_FP:
3189 Opc = ARMISD::SITOF;
3190 break;
3191 case ISD::UINT_TO_FP:
3192 Opc = ARMISD::UITOF;
3193 break;
3194 }
3195
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003196 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003197 return DAG.getNode(Opc, dl, VT, Op);
3198}
3199
Evan Cheng515fe3a2010-07-08 02:08:50 +00003200SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003201 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SDValue Tmp0 = Op.getOperand(0);
3203 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003204 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003205 EVT VT = Op.getValueType();
3206 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003207 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3208 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3209 bool UseNEON = !InGPR && Subtarget->hasNEON();
3210
3211 if (UseNEON) {
3212 // Use VBSL to copy the sign bit.
3213 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3214 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3215 DAG.getTargetConstant(EncodedVal, MVT::i32));
3216 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3217 if (VT == MVT::f64)
3218 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3219 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3220 DAG.getConstant(32, MVT::i32));
3221 else /*if (VT == MVT::f32)*/
3222 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3223 if (SrcVT == MVT::f32) {
3224 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3225 if (VT == MVT::f64)
3226 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3227 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3228 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003229 } else if (VT == MVT::f32)
3230 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3231 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3232 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003233 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3234 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3235
3236 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3237 MVT::i32);
3238 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3239 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3240 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003241
Evan Chenge573fb32011-02-23 02:24:55 +00003242 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3243 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3244 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003245 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003246 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3247 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3248 DAG.getConstant(0, MVT::i32));
3249 } else {
3250 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3251 }
3252
3253 return Res;
3254 }
Evan Chengc143dd42011-02-11 02:28:55 +00003255
3256 // Bitcast operand 1 to i32.
3257 if (SrcVT == MVT::f64)
3258 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3259 &Tmp1, 1).getValue(1);
3260 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3261
Evan Chenge573fb32011-02-23 02:24:55 +00003262 // Or in the signbit with integer operations.
3263 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3264 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3265 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3266 if (VT == MVT::f32) {
3267 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3268 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3269 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3270 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003271 }
3272
Evan Chenge573fb32011-02-23 02:24:55 +00003273 // f64: Or the high part with signbit and then combine two parts.
3274 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3275 &Tmp0, 1);
3276 SDValue Lo = Tmp0.getValue(0);
3277 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3278 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3279 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003280}
3281
Evan Cheng2457f2c2010-05-22 01:47:14 +00003282SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3283 MachineFunction &MF = DAG.getMachineFunction();
3284 MachineFrameInfo *MFI = MF.getFrameInfo();
3285 MFI->setReturnAddressIsTaken(true);
3286
3287 EVT VT = Op.getValueType();
3288 DebugLoc dl = Op.getDebugLoc();
3289 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3290 if (Depth) {
3291 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3292 SDValue Offset = DAG.getConstant(4, MVT::i32);
3293 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3294 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003295 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003296 }
3297
3298 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003299 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003300 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3301}
3302
Dan Gohmand858e902010-04-17 15:26:15 +00003303SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003304 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3305 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003306
Owen Andersone50ed302009-08-10 22:56:29 +00003307 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003308 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3309 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003310 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003311 ? ARM::R7 : ARM::R11;
3312 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3313 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003314 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3315 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003316 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003317 return FrameAddr;
3318}
3319
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003320/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003321/// expand a bit convert where either the source or destination type is i64 to
3322/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3323/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3324/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003325static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003326 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3327 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003329
Bob Wilson9f3f0612010-04-17 05:30:19 +00003330 // This function is only supposed to be called for i64 types, either as the
3331 // source or destination of the bit convert.
3332 EVT SrcVT = Op.getValueType();
3333 EVT DstVT = N->getValueType(0);
3334 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003335 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003336
Bob Wilson9f3f0612010-04-17 05:30:19 +00003337 // Turn i64->f64 into VMOVDRR.
3338 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3340 DAG.getConstant(0, MVT::i32));
3341 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3342 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003343 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003344 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003345 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003346
Jim Grosbache5165492009-11-09 00:11:35 +00003347 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003348 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3349 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3350 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3351 // Merge the pieces into a single i64 value.
3352 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3353 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003354
Bob Wilson9f3f0612010-04-17 05:30:19 +00003355 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003356}
3357
Bob Wilson5bafff32009-06-22 23:27:02 +00003358/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003359/// Zero vectors are used to represent vector negation and in those cases
3360/// will be implemented with the NEON VNEG instruction. However, VNEG does
3361/// not support i64 elements, so sometimes the zero vectors will need to be
3362/// explicitly constructed. Regardless, use a canonical VMOV to create the
3363/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003364static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003365 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003366 // The canonical modified immediate encoding of a zero vector is....0!
3367 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3368 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3369 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003370 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003371}
3372
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003373/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3374/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003375SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3376 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003377 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3378 EVT VT = Op.getValueType();
3379 unsigned VTBits = VT.getSizeInBits();
3380 DebugLoc dl = Op.getDebugLoc();
3381 SDValue ShOpLo = Op.getOperand(0);
3382 SDValue ShOpHi = Op.getOperand(1);
3383 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003384 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003385 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003386
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003387 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3388
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003389 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3390 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3391 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3392 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3393 DAG.getConstant(VTBits, MVT::i32));
3394 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3395 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003396 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003397
3398 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3399 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003400 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003401 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003402 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003403 CCR, Cmp);
3404
3405 SDValue Ops[2] = { Lo, Hi };
3406 return DAG.getMergeValues(Ops, 2, dl);
3407}
3408
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003409/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3410/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003411SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3412 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003413 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3414 EVT VT = Op.getValueType();
3415 unsigned VTBits = VT.getSizeInBits();
3416 DebugLoc dl = Op.getDebugLoc();
3417 SDValue ShOpLo = Op.getOperand(0);
3418 SDValue ShOpHi = Op.getOperand(1);
3419 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003420 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003421
3422 assert(Op.getOpcode() == ISD::SHL_PARTS);
3423 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3424 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3425 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3426 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3427 DAG.getConstant(VTBits, MVT::i32));
3428 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3429 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3430
3431 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3432 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3433 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003434 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003435 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003436 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003437 CCR, Cmp);
3438
3439 SDValue Ops[2] = { Lo, Hi };
3440 return DAG.getMergeValues(Ops, 2, dl);
3441}
3442
Jim Grosbach4725ca72010-09-08 03:54:02 +00003443SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003444 SelectionDAG &DAG) const {
3445 // The rounding mode is in bits 23:22 of the FPSCR.
3446 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3447 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3448 // so that the shift + and get folded into a bitfield extract.
3449 DebugLoc dl = Op.getDebugLoc();
3450 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3451 DAG.getConstant(Intrinsic::arm_get_fpscr,
3452 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003453 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003454 DAG.getConstant(1U << 22, MVT::i32));
3455 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3456 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003457 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003458 DAG.getConstant(3, MVT::i32));
3459}
3460
Jim Grosbach3482c802010-01-18 19:58:49 +00003461static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3462 const ARMSubtarget *ST) {
3463 EVT VT = N->getValueType(0);
3464 DebugLoc dl = N->getDebugLoc();
3465
3466 if (!ST->hasV6T2Ops())
3467 return SDValue();
3468
3469 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3470 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3471}
3472
Bob Wilson5bafff32009-06-22 23:27:02 +00003473static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3474 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003475 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003476 DebugLoc dl = N->getDebugLoc();
3477
Bob Wilsond5448bb2010-11-18 21:16:28 +00003478 if (!VT.isVector())
3479 return SDValue();
3480
Bob Wilson5bafff32009-06-22 23:27:02 +00003481 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003482 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003483
Bob Wilsond5448bb2010-11-18 21:16:28 +00003484 // Left shifts translate directly to the vshiftu intrinsic.
3485 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003486 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003487 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3488 N->getOperand(0), N->getOperand(1));
3489
3490 assert((N->getOpcode() == ISD::SRA ||
3491 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3492
3493 // NEON uses the same intrinsics for both left and right shifts. For
3494 // right shifts, the shift amounts are negative, so negate the vector of
3495 // shift amounts.
3496 EVT ShiftVT = N->getOperand(1).getValueType();
3497 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3498 getZeroVector(ShiftVT, DAG, dl),
3499 N->getOperand(1));
3500 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3501 Intrinsic::arm_neon_vshifts :
3502 Intrinsic::arm_neon_vshiftu);
3503 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3504 DAG.getConstant(vshiftInt, MVT::i32),
3505 N->getOperand(0), NegatedCount);
3506}
3507
3508static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3509 const ARMSubtarget *ST) {
3510 EVT VT = N->getValueType(0);
3511 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003512
Eli Friedmance392eb2009-08-22 03:13:10 +00003513 // We can get here for a node like i32 = ISD::SHL i32, i64
3514 if (VT != MVT::i64)
3515 return SDValue();
3516
3517 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003518 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003519
Chris Lattner27a6c732007-11-24 07:07:01 +00003520 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3521 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003522 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003523 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003524
Chris Lattner27a6c732007-11-24 07:07:01 +00003525 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003526 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003527
Chris Lattner27a6c732007-11-24 07:07:01 +00003528 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003530 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003532 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003533
Chris Lattner27a6c732007-11-24 07:07:01 +00003534 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3535 // captures the result into a carry flag.
3536 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003537 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003538
Chris Lattner27a6c732007-11-24 07:07:01 +00003539 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003541
Chris Lattner27a6c732007-11-24 07:07:01 +00003542 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003544}
3545
Bob Wilson5bafff32009-06-22 23:27:02 +00003546static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3547 SDValue TmpOp0, TmpOp1;
3548 bool Invert = false;
3549 bool Swap = false;
3550 unsigned Opc = 0;
3551
3552 SDValue Op0 = Op.getOperand(0);
3553 SDValue Op1 = Op.getOperand(1);
3554 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003555 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003556 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3557 DebugLoc dl = Op.getDebugLoc();
3558
3559 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3560 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003561 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003562 case ISD::SETUNE:
3563 case ISD::SETNE: Invert = true; // Fallthrough
3564 case ISD::SETOEQ:
3565 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3566 case ISD::SETOLT:
3567 case ISD::SETLT: Swap = true; // Fallthrough
3568 case ISD::SETOGT:
3569 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3570 case ISD::SETOLE:
3571 case ISD::SETLE: Swap = true; // Fallthrough
3572 case ISD::SETOGE:
3573 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3574 case ISD::SETUGE: Swap = true; // Fallthrough
3575 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3576 case ISD::SETUGT: Swap = true; // Fallthrough
3577 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3578 case ISD::SETUEQ: Invert = true; // Fallthrough
3579 case ISD::SETONE:
3580 // Expand this to (OLT | OGT).
3581 TmpOp0 = Op0;
3582 TmpOp1 = Op1;
3583 Opc = ISD::OR;
3584 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3585 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3586 break;
3587 case ISD::SETUO: Invert = true; // Fallthrough
3588 case ISD::SETO:
3589 // Expand this to (OLT | OGE).
3590 TmpOp0 = Op0;
3591 TmpOp1 = Op1;
3592 Opc = ISD::OR;
3593 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3594 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3595 break;
3596 }
3597 } else {
3598 // Integer comparisons.
3599 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003600 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003601 case ISD::SETNE: Invert = true;
3602 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3603 case ISD::SETLT: Swap = true;
3604 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3605 case ISD::SETLE: Swap = true;
3606 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3607 case ISD::SETULT: Swap = true;
3608 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3609 case ISD::SETULE: Swap = true;
3610 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3611 }
3612
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003613 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003614 if (Opc == ARMISD::VCEQ) {
3615
3616 SDValue AndOp;
3617 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3618 AndOp = Op0;
3619 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3620 AndOp = Op1;
3621
3622 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003623 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003624 AndOp = AndOp.getOperand(0);
3625
3626 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3627 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003628 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3629 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003630 Invert = !Invert;
3631 }
3632 }
3633 }
3634
3635 if (Swap)
3636 std::swap(Op0, Op1);
3637
Owen Andersonc24cb352010-11-08 23:21:22 +00003638 // If one of the operands is a constant vector zero, attempt to fold the
3639 // comparison to a specialized compare-against-zero form.
3640 SDValue SingleOp;
3641 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3642 SingleOp = Op0;
3643 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3644 if (Opc == ARMISD::VCGE)
3645 Opc = ARMISD::VCLEZ;
3646 else if (Opc == ARMISD::VCGT)
3647 Opc = ARMISD::VCLTZ;
3648 SingleOp = Op1;
3649 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003650
Owen Andersonc24cb352010-11-08 23:21:22 +00003651 SDValue Result;
3652 if (SingleOp.getNode()) {
3653 switch (Opc) {
3654 case ARMISD::VCEQ:
3655 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3656 case ARMISD::VCGE:
3657 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3658 case ARMISD::VCLEZ:
3659 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3660 case ARMISD::VCGT:
3661 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3662 case ARMISD::VCLTZ:
3663 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3664 default:
3665 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3666 }
3667 } else {
3668 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3669 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003670
3671 if (Invert)
3672 Result = DAG.getNOT(dl, Result, VT);
3673
3674 return Result;
3675}
3676
Lang Hames45b5f882012-03-15 18:49:02 +00003677SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3678 const ARMSubtarget *ST) const {
3679 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3680 return SDValue();
3681
3682 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3683 assert(Op.getValueType() == MVT::f32 &&
3684 "ConstantFP custom lowering should only occur for f32.");
3685
3686 APFloat FPVal = CFP->getValueAPF();
3687 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3688 if (ImmVal == -1)
3689 return SDValue();
3690
3691 DebugLoc DL = Op.getDebugLoc();
3692 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3693 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32, NewVal);
3694 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3695 DAG.getConstant(0, MVT::i32));
3696}
3697
Bob Wilsond3c42842010-06-14 22:19:57 +00003698/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3699/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003700/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003701static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3702 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003703 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003704 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003705
Bob Wilson827b2102010-06-15 19:05:35 +00003706 // SplatBitSize is set to the smallest size that splats the vector, so a
3707 // zero vector will always have SplatBitSize == 8. However, NEON modified
3708 // immediate instructions others than VMOV do not support the 8-bit encoding
3709 // of a zero vector, and the default encoding of zero is supposed to be the
3710 // 32-bit version.
3711 if (SplatBits == 0)
3712 SplatBitSize = 32;
3713
Bob Wilson5bafff32009-06-22 23:27:02 +00003714 switch (SplatBitSize) {
3715 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003716 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003717 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003718 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003719 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003720 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003721 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003722 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003723 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003724
3725 case 16:
3726 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003727 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003728 if ((SplatBits & ~0xff) == 0) {
3729 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003730 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 Imm = SplatBits;
3732 break;
3733 }
3734 if ((SplatBits & ~0xff00) == 0) {
3735 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003736 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003737 Imm = SplatBits >> 8;
3738 break;
3739 }
3740 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003741
3742 case 32:
3743 // NEON's 32-bit VMOV supports splat values where:
3744 // * only one byte is nonzero, or
3745 // * the least significant byte is 0xff and the second byte is nonzero, or
3746 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003747 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003748 if ((SplatBits & ~0xff) == 0) {
3749 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003750 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003751 Imm = SplatBits;
3752 break;
3753 }
3754 if ((SplatBits & ~0xff00) == 0) {
3755 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003756 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003757 Imm = SplatBits >> 8;
3758 break;
3759 }
3760 if ((SplatBits & ~0xff0000) == 0) {
3761 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003762 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003763 Imm = SplatBits >> 16;
3764 break;
3765 }
3766 if ((SplatBits & ~0xff000000) == 0) {
3767 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003768 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003769 Imm = SplatBits >> 24;
3770 break;
3771 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003772
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003773 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3774 if (type == OtherModImm) return SDValue();
3775
Bob Wilson5bafff32009-06-22 23:27:02 +00003776 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003777 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3778 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003779 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003780 Imm = SplatBits >> 8;
3781 SplatBits |= 0xff;
3782 break;
3783 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003784
3785 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003786 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3787 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003788 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003789 Imm = SplatBits >> 16;
3790 SplatBits |= 0xffff;
3791 break;
3792 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003793
3794 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3795 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3796 // VMOV.I32. A (very) minor optimization would be to replicate the value
3797 // and fall through here to test for a valid 64-bit splat. But, then the
3798 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003799 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003800
3801 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003802 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003803 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003804 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003805 uint64_t BitMask = 0xff;
3806 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003807 unsigned ImmMask = 1;
3808 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003809 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003810 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003811 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003812 Imm |= ImmMask;
3813 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003814 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003815 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003816 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003817 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003818 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003819 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003820 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003821 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003822 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003823 break;
3824 }
3825
Bob Wilson1a913ed2010-06-11 21:34:50 +00003826 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003827 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003828 }
3829
Bob Wilsoncba270d2010-07-13 21:16:48 +00003830 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3831 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003832}
3833
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003834static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003835 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003836 unsigned NumElts = VT.getVectorNumElements();
3837 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003838
3839 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3840 if (M[0] < 0)
3841 return false;
3842
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003843 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003844
3845 // If this is a VEXT shuffle, the immediate value is the index of the first
3846 // element. The other shuffle indices must be the successive elements after
3847 // the first one.
3848 unsigned ExpectedElt = Imm;
3849 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003850 // Increment the expected index. If it wraps around, it may still be
3851 // a VEXT but the source vectors must be swapped.
3852 ExpectedElt += 1;
3853 if (ExpectedElt == NumElts * 2) {
3854 ExpectedElt = 0;
3855 ReverseVEXT = true;
3856 }
3857
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003858 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003859 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003860 return false;
3861 }
3862
3863 // Adjust the index value if the source operands will be swapped.
3864 if (ReverseVEXT)
3865 Imm -= NumElts;
3866
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003867 return true;
3868}
3869
Bob Wilson8bb9e482009-07-26 00:39:34 +00003870/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3871/// instruction with the specified blocksize. (The order of the elements
3872/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003873static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003874 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3875 "Only possible block sizes for VREV are: 16, 32, 64");
3876
Bob Wilson8bb9e482009-07-26 00:39:34 +00003877 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003878 if (EltSz == 64)
3879 return false;
3880
3881 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003882 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003883 // If the first shuffle index is UNDEF, be optimistic.
3884 if (M[0] < 0)
3885 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003886
3887 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3888 return false;
3889
3890 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003891 if (M[i] < 0) continue; // ignore UNDEF indices
3892 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003893 return false;
3894 }
3895
3896 return true;
3897}
3898
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003899static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003900 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3901 // range, then 0 is placed into the resulting vector. So pretty much any mask
3902 // of 8 elements can work here.
3903 return VT == MVT::v8i8 && M.size() == 8;
3904}
3905
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003906static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003907 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3908 if (EltSz == 64)
3909 return false;
3910
Bob Wilsonc692cb72009-08-21 20:54:19 +00003911 unsigned NumElts = VT.getVectorNumElements();
3912 WhichResult = (M[0] == 0 ? 0 : 1);
3913 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003914 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3915 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003916 return false;
3917 }
3918 return true;
3919}
3920
Bob Wilson324f4f12009-12-03 06:40:55 +00003921/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3922/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3923/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003924static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003925 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3926 if (EltSz == 64)
3927 return false;
3928
3929 unsigned NumElts = VT.getVectorNumElements();
3930 WhichResult = (M[0] == 0 ? 0 : 1);
3931 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003932 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3933 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003934 return false;
3935 }
3936 return true;
3937}
3938
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003939static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003940 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3941 if (EltSz == 64)
3942 return false;
3943
Bob Wilsonc692cb72009-08-21 20:54:19 +00003944 unsigned NumElts = VT.getVectorNumElements();
3945 WhichResult = (M[0] == 0 ? 0 : 1);
3946 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003947 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003948 if ((unsigned) M[i] != 2 * i + WhichResult)
3949 return false;
3950 }
3951
3952 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00003953 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00003954 return false;
3955
3956 return true;
3957}
3958
Bob Wilson324f4f12009-12-03 06:40:55 +00003959/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
3960/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3961/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003962static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003963 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3964 if (EltSz == 64)
3965 return false;
3966
3967 unsigned Half = VT.getVectorNumElements() / 2;
3968 WhichResult = (M[0] == 0 ? 0 : 1);
3969 for (unsigned j = 0; j != 2; ++j) {
3970 unsigned Idx = WhichResult;
3971 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003972 int MIdx = M[i + j * Half];
3973 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00003974 return false;
3975 Idx += 2;
3976 }
3977 }
3978
3979 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
3980 if (VT.is64BitVector() && EltSz == 32)
3981 return false;
3982
3983 return true;
3984}
3985
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003986static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003987 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3988 if (EltSz == 64)
3989 return false;
3990
Bob Wilsonc692cb72009-08-21 20:54:19 +00003991 unsigned NumElts = VT.getVectorNumElements();
3992 WhichResult = (M[0] == 0 ? 0 : 1);
3993 unsigned Idx = WhichResult * NumElts / 2;
3994 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003995 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
3996 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003997 return false;
3998 Idx += 1;
3999 }
4000
4001 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004002 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004003 return false;
4004
4005 return true;
4006}
4007
Bob Wilson324f4f12009-12-03 06:40:55 +00004008/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4009/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4010/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004011static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004012 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4013 if (EltSz == 64)
4014 return false;
4015
4016 unsigned NumElts = VT.getVectorNumElements();
4017 WhichResult = (M[0] == 0 ? 0 : 1);
4018 unsigned Idx = WhichResult * NumElts / 2;
4019 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004020 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4021 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004022 return false;
4023 Idx += 1;
4024 }
4025
4026 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4027 if (VT.is64BitVector() && EltSz == 32)
4028 return false;
4029
4030 return true;
4031}
4032
Dale Johannesenf630c712010-07-29 20:10:08 +00004033// If N is an integer constant that can be moved into a register in one
4034// instruction, return an SDValue of such a constant (will become a MOV
4035// instruction). Otherwise return null.
4036static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4037 const ARMSubtarget *ST, DebugLoc dl) {
4038 uint64_t Val;
4039 if (!isa<ConstantSDNode>(N))
4040 return SDValue();
4041 Val = cast<ConstantSDNode>(N)->getZExtValue();
4042
4043 if (ST->isThumb1Only()) {
4044 if (Val <= 255 || ~Val <= 255)
4045 return DAG.getConstant(Val, MVT::i32);
4046 } else {
4047 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4048 return DAG.getConstant(Val, MVT::i32);
4049 }
4050 return SDValue();
4051}
4052
Bob Wilson5bafff32009-06-22 23:27:02 +00004053// If this is a case we can't handle, return null and let the default
4054// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004055SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4056 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004057 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004058 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004059 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004060
4061 APInt SplatBits, SplatUndef;
4062 unsigned SplatBitSize;
4063 bool HasAnyUndefs;
4064 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004065 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004066 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004067 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004068 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004069 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004070 DAG, VmovVT, VT.is128BitVector(),
4071 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004072 if (Val.getNode()) {
4073 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004074 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004075 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004076
4077 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004078 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004079 Val = isNEONModifiedImm(NegatedImm,
4080 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004081 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004082 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004083 if (Val.getNode()) {
4084 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004085 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004086 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004087
4088 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004089 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004090 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004091 if (ImmVal != -1) {
4092 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4093 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4094 }
4095 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004096 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004097 }
4098
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004099 // Scan through the operands to see if only one value is used.
4100 unsigned NumElts = VT.getVectorNumElements();
4101 bool isOnlyLowElement = true;
4102 bool usesOnlyOneValue = true;
4103 bool isConstant = true;
4104 SDValue Value;
4105 for (unsigned i = 0; i < NumElts; ++i) {
4106 SDValue V = Op.getOperand(i);
4107 if (V.getOpcode() == ISD::UNDEF)
4108 continue;
4109 if (i > 0)
4110 isOnlyLowElement = false;
4111 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4112 isConstant = false;
4113
4114 if (!Value.getNode())
4115 Value = V;
4116 else if (V != Value)
4117 usesOnlyOneValue = false;
4118 }
4119
4120 if (!Value.getNode())
4121 return DAG.getUNDEF(VT);
4122
4123 if (isOnlyLowElement)
4124 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4125
Dale Johannesenf630c712010-07-29 20:10:08 +00004126 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4127
Dale Johannesen575cd142010-10-19 20:00:17 +00004128 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4129 // i32 and try again.
4130 if (usesOnlyOneValue && EltSize <= 32) {
4131 if (!isConstant)
4132 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4133 if (VT.getVectorElementType().isFloatingPoint()) {
4134 SmallVector<SDValue, 8> Ops;
4135 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004136 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004137 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004138 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4139 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004140 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4141 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004142 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004143 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004144 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4145 if (Val.getNode())
4146 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004147 }
4148
4149 // If all elements are constants and the case above didn't get hit, fall back
4150 // to the default expansion, which will generate a load from the constant
4151 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004152 if (isConstant)
4153 return SDValue();
4154
Bob Wilson11a1dff2011-01-07 21:37:30 +00004155 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4156 if (NumElts >= 4) {
4157 SDValue shuffle = ReconstructShuffle(Op, DAG);
4158 if (shuffle != SDValue())
4159 return shuffle;
4160 }
4161
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004162 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004163 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4164 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004165 if (EltSize >= 32) {
4166 // Do the expansion with floating-point types, since that is what the VFP
4167 // registers are defined to use, and since i64 is not legal.
4168 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4169 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004170 SmallVector<SDValue, 8> Ops;
4171 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004172 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004173 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004174 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004175 }
4176
4177 return SDValue();
4178}
4179
Bob Wilson11a1dff2011-01-07 21:37:30 +00004180// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004181// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004182SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4183 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004184 DebugLoc dl = Op.getDebugLoc();
4185 EVT VT = Op.getValueType();
4186 unsigned NumElts = VT.getVectorNumElements();
4187
4188 SmallVector<SDValue, 2> SourceVecs;
4189 SmallVector<unsigned, 2> MinElts;
4190 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004191
Bob Wilson11a1dff2011-01-07 21:37:30 +00004192 for (unsigned i = 0; i < NumElts; ++i) {
4193 SDValue V = Op.getOperand(i);
4194 if (V.getOpcode() == ISD::UNDEF)
4195 continue;
4196 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4197 // A shuffle can only come from building a vector from various
4198 // elements of other vectors.
4199 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004200 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4201 VT.getVectorElementType()) {
4202 // This code doesn't know how to handle shuffles where the vector
4203 // element types do not match (this happens because type legalization
4204 // promotes the return type of EXTRACT_VECTOR_ELT).
4205 // FIXME: It might be appropriate to extend this code to handle
4206 // mismatched types.
4207 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004208 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004209
Bob Wilson11a1dff2011-01-07 21:37:30 +00004210 // Record this extraction against the appropriate vector if possible...
4211 SDValue SourceVec = V.getOperand(0);
4212 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4213 bool FoundSource = false;
4214 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4215 if (SourceVecs[j] == SourceVec) {
4216 if (MinElts[j] > EltNo)
4217 MinElts[j] = EltNo;
4218 if (MaxElts[j] < EltNo)
4219 MaxElts[j] = EltNo;
4220 FoundSource = true;
4221 break;
4222 }
4223 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004224
Bob Wilson11a1dff2011-01-07 21:37:30 +00004225 // Or record a new source if not...
4226 if (!FoundSource) {
4227 SourceVecs.push_back(SourceVec);
4228 MinElts.push_back(EltNo);
4229 MaxElts.push_back(EltNo);
4230 }
4231 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004232
Bob Wilson11a1dff2011-01-07 21:37:30 +00004233 // Currently only do something sane when at most two source vectors
4234 // involved.
4235 if (SourceVecs.size() > 2)
4236 return SDValue();
4237
4238 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4239 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004240
Bob Wilson11a1dff2011-01-07 21:37:30 +00004241 // This loop extracts the usage patterns of the source vectors
4242 // and prepares appropriate SDValues for a shuffle if possible.
4243 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4244 if (SourceVecs[i].getValueType() == VT) {
4245 // No VEXT necessary
4246 ShuffleSrcs[i] = SourceVecs[i];
4247 VEXTOffsets[i] = 0;
4248 continue;
4249 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4250 // It probably isn't worth padding out a smaller vector just to
4251 // break it down again in a shuffle.
4252 return SDValue();
4253 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004254
Bob Wilson11a1dff2011-01-07 21:37:30 +00004255 // Since only 64-bit and 128-bit vectors are legal on ARM and
4256 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004257 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4258 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004259
Bob Wilson11a1dff2011-01-07 21:37:30 +00004260 if (MaxElts[i] - MinElts[i] >= NumElts) {
4261 // Span too large for a VEXT to cope
4262 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004263 }
4264
Bob Wilson11a1dff2011-01-07 21:37:30 +00004265 if (MinElts[i] >= NumElts) {
4266 // The extraction can just take the second half
4267 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004268 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4269 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004270 DAG.getIntPtrConstant(NumElts));
4271 } else if (MaxElts[i] < NumElts) {
4272 // The extraction can just take the first half
4273 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004274 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4275 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004276 DAG.getIntPtrConstant(0));
4277 } else {
4278 // An actual VEXT is needed
4279 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004280 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4281 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004282 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004283 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4284 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004285 DAG.getIntPtrConstant(NumElts));
4286 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4287 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4288 }
4289 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004290
Bob Wilson11a1dff2011-01-07 21:37:30 +00004291 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004292
Bob Wilson11a1dff2011-01-07 21:37:30 +00004293 for (unsigned i = 0; i < NumElts; ++i) {
4294 SDValue Entry = Op.getOperand(i);
4295 if (Entry.getOpcode() == ISD::UNDEF) {
4296 Mask.push_back(-1);
4297 continue;
4298 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004299
Bob Wilson11a1dff2011-01-07 21:37:30 +00004300 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004301 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4302 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004303 if (ExtractVec == SourceVecs[0]) {
4304 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4305 } else {
4306 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4307 }
4308 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004309
Bob Wilson11a1dff2011-01-07 21:37:30 +00004310 // Final check before we try to produce nonsense...
4311 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004312 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4313 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004314
Bob Wilson11a1dff2011-01-07 21:37:30 +00004315 return SDValue();
4316}
4317
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004318/// isShuffleMaskLegal - Targets can use this to indicate that they only
4319/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4320/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4321/// are assumed to be legal.
4322bool
4323ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4324 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004325 if (VT.getVectorNumElements() == 4 &&
4326 (VT.is128BitVector() || VT.is64BitVector())) {
4327 unsigned PFIndexes[4];
4328 for (unsigned i = 0; i != 4; ++i) {
4329 if (M[i] < 0)
4330 PFIndexes[i] = 8;
4331 else
4332 PFIndexes[i] = M[i];
4333 }
4334
4335 // Compute the index in the perfect shuffle table.
4336 unsigned PFTableIndex =
4337 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4338 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4339 unsigned Cost = (PFEntry >> 30);
4340
4341 if (Cost <= 4)
4342 return true;
4343 }
4344
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004345 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004346 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004347
Bob Wilson53dd2452010-06-07 23:53:38 +00004348 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4349 return (EltSize >= 32 ||
4350 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004351 isVREVMask(M, VT, 64) ||
4352 isVREVMask(M, VT, 32) ||
4353 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004354 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004355 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004356 isVTRNMask(M, VT, WhichResult) ||
4357 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004358 isVZIPMask(M, VT, WhichResult) ||
4359 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4360 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4361 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004362}
4363
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004364/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4365/// the specified operations to build the shuffle.
4366static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4367 SDValue RHS, SelectionDAG &DAG,
4368 DebugLoc dl) {
4369 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4370 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4371 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4372
4373 enum {
4374 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4375 OP_VREV,
4376 OP_VDUP0,
4377 OP_VDUP1,
4378 OP_VDUP2,
4379 OP_VDUP3,
4380 OP_VEXT1,
4381 OP_VEXT2,
4382 OP_VEXT3,
4383 OP_VUZPL, // VUZP, left result
4384 OP_VUZPR, // VUZP, right result
4385 OP_VZIPL, // VZIP, left result
4386 OP_VZIPR, // VZIP, right result
4387 OP_VTRNL, // VTRN, left result
4388 OP_VTRNR // VTRN, right result
4389 };
4390
4391 if (OpNum == OP_COPY) {
4392 if (LHSID == (1*9+2)*9+3) return LHS;
4393 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4394 return RHS;
4395 }
4396
4397 SDValue OpLHS, OpRHS;
4398 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4399 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4400 EVT VT = OpLHS.getValueType();
4401
4402 switch (OpNum) {
4403 default: llvm_unreachable("Unknown shuffle opcode!");
4404 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004405 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004406 if (VT.getVectorElementType() == MVT::i32 ||
4407 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004408 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4409 // vrev <4 x i16> -> VREV32
4410 if (VT.getVectorElementType() == MVT::i16)
4411 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4412 // vrev <4 x i8> -> VREV16
4413 assert(VT.getVectorElementType() == MVT::i8);
4414 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004415 case OP_VDUP0:
4416 case OP_VDUP1:
4417 case OP_VDUP2:
4418 case OP_VDUP3:
4419 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004420 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004421 case OP_VEXT1:
4422 case OP_VEXT2:
4423 case OP_VEXT3:
4424 return DAG.getNode(ARMISD::VEXT, dl, VT,
4425 OpLHS, OpRHS,
4426 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4427 case OP_VUZPL:
4428 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004429 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004430 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4431 case OP_VZIPL:
4432 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004433 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004434 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4435 case OP_VTRNL:
4436 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004437 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4438 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004439 }
4440}
4441
Bill Wendling69a05a72011-03-14 23:02:38 +00004442static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004443 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004444 SelectionDAG &DAG) {
4445 // Check to see if we can use the VTBL instruction.
4446 SDValue V1 = Op.getOperand(0);
4447 SDValue V2 = Op.getOperand(1);
4448 DebugLoc DL = Op.getDebugLoc();
4449
4450 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004451 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004452 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4453 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4454
4455 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4456 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4457 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4458 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004459
Owen Anderson76706012011-04-05 21:48:57 +00004460 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004461 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4462 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004463}
4464
Bob Wilson5bafff32009-06-22 23:27:02 +00004465static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004466 SDValue V1 = Op.getOperand(0);
4467 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004468 DebugLoc dl = Op.getDebugLoc();
4469 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004470 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004471
Bob Wilson28865062009-08-13 02:13:04 +00004472 // Convert shuffles that are directly supported on NEON to target-specific
4473 // DAG nodes, instead of keeping them as shuffles and matching them again
4474 // during code selection. This is more efficient and avoids the possibility
4475 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004476 // FIXME: floating-point vectors should be canonicalized to integer vectors
4477 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004478 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004479
Bob Wilson53dd2452010-06-07 23:53:38 +00004480 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4481 if (EltSize <= 32) {
4482 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4483 int Lane = SVN->getSplatIndex();
4484 // If this is undef splat, generate it via "just" vdup, if possible.
4485 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004486
Dan Gohman65fd6562011-11-03 21:49:52 +00004487 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004488 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4489 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4490 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004491 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4492 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4493 // reaches it).
4494 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4495 !isa<ConstantSDNode>(V1.getOperand(0))) {
4496 bool IsScalarToVector = true;
4497 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4498 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4499 IsScalarToVector = false;
4500 break;
4501 }
4502 if (IsScalarToVector)
4503 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4504 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004505 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4506 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004507 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004508
4509 bool ReverseVEXT;
4510 unsigned Imm;
4511 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4512 if (ReverseVEXT)
4513 std::swap(V1, V2);
4514 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4515 DAG.getConstant(Imm, MVT::i32));
4516 }
4517
4518 if (isVREVMask(ShuffleMask, VT, 64))
4519 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4520 if (isVREVMask(ShuffleMask, VT, 32))
4521 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4522 if (isVREVMask(ShuffleMask, VT, 16))
4523 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4524
4525 // Check for Neon shuffles that modify both input vectors in place.
4526 // If both results are used, i.e., if there are two shuffles with the same
4527 // source operands and with masks corresponding to both results of one of
4528 // these operations, DAG memoization will ensure that a single node is
4529 // used for both shuffles.
4530 unsigned WhichResult;
4531 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4532 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4533 V1, V2).getValue(WhichResult);
4534 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4535 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4536 V1, V2).getValue(WhichResult);
4537 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4538 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4539 V1, V2).getValue(WhichResult);
4540
4541 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4542 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4543 V1, V1).getValue(WhichResult);
4544 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4545 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4546 V1, V1).getValue(WhichResult);
4547 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4548 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4549 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004550 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004551
Bob Wilsonc692cb72009-08-21 20:54:19 +00004552 // If the shuffle is not directly supported and it has 4 elements, use
4553 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004554 unsigned NumElts = VT.getVectorNumElements();
4555 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004556 unsigned PFIndexes[4];
4557 for (unsigned i = 0; i != 4; ++i) {
4558 if (ShuffleMask[i] < 0)
4559 PFIndexes[i] = 8;
4560 else
4561 PFIndexes[i] = ShuffleMask[i];
4562 }
4563
4564 // Compute the index in the perfect shuffle table.
4565 unsigned PFTableIndex =
4566 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004567 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4568 unsigned Cost = (PFEntry >> 30);
4569
4570 if (Cost <= 4)
4571 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4572 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004573
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004574 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004575 if (EltSize >= 32) {
4576 // Do the expansion with floating-point types, since that is what the VFP
4577 // registers are defined to use, and since i64 is not legal.
4578 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4579 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004580 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4581 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004582 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004583 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004584 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004585 Ops.push_back(DAG.getUNDEF(EltVT));
4586 else
4587 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4588 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4589 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4590 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004591 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004592 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004593 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004594 }
4595
Bill Wendling69a05a72011-03-14 23:02:38 +00004596 if (VT == MVT::v8i8) {
4597 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4598 if (NewOp.getNode())
4599 return NewOp;
4600 }
4601
Bob Wilson22cac0d2009-08-14 05:16:33 +00004602 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004603}
4604
Eli Friedman5c89cb82011-10-24 23:08:52 +00004605static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4606 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4607 SDValue Lane = Op.getOperand(2);
4608 if (!isa<ConstantSDNode>(Lane))
4609 return SDValue();
4610
4611 return Op;
4612}
4613
Bob Wilson5bafff32009-06-22 23:27:02 +00004614static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004615 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004616 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004617 if (!isa<ConstantSDNode>(Lane))
4618 return SDValue();
4619
4620 SDValue Vec = Op.getOperand(0);
4621 if (Op.getValueType() == MVT::i32 &&
4622 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4623 DebugLoc dl = Op.getDebugLoc();
4624 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4625 }
4626
4627 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004628}
4629
Bob Wilsona6d65862009-08-03 20:36:38 +00004630static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4631 // The only time a CONCAT_VECTORS operation can have legal types is when
4632 // two 64-bit vectors are concatenated to a 128-bit vector.
4633 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4634 "unexpected CONCAT_VECTORS");
4635 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004636 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004637 SDValue Op0 = Op.getOperand(0);
4638 SDValue Op1 = Op.getOperand(1);
4639 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004640 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004641 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004642 DAG.getIntPtrConstant(0));
4643 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004644 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004645 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004646 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004647 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004648}
4649
Bob Wilson626613d2010-11-23 19:38:38 +00004650/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4651/// element has been zero/sign-extended, depending on the isSigned parameter,
4652/// from an integer type half its size.
4653static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4654 bool isSigned) {
4655 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4656 EVT VT = N->getValueType(0);
4657 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4658 SDNode *BVN = N->getOperand(0).getNode();
4659 if (BVN->getValueType(0) != MVT::v4i32 ||
4660 BVN->getOpcode() != ISD::BUILD_VECTOR)
4661 return false;
4662 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4663 unsigned HiElt = 1 - LoElt;
4664 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4665 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4666 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4667 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4668 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4669 return false;
4670 if (isSigned) {
4671 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4672 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4673 return true;
4674 } else {
4675 if (Hi0->isNullValue() && Hi1->isNullValue())
4676 return true;
4677 }
4678 return false;
4679 }
4680
4681 if (N->getOpcode() != ISD::BUILD_VECTOR)
4682 return false;
4683
4684 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4685 SDNode *Elt = N->getOperand(i).getNode();
4686 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4687 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4688 unsigned HalfSize = EltSize / 2;
4689 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004690 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004691 return false;
4692 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004693 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004694 return false;
4695 }
4696 continue;
4697 }
4698 return false;
4699 }
4700
4701 return true;
4702}
4703
4704/// isSignExtended - Check if a node is a vector value that is sign-extended
4705/// or a constant BUILD_VECTOR with sign-extended elements.
4706static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4707 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4708 return true;
4709 if (isExtendedBUILD_VECTOR(N, DAG, true))
4710 return true;
4711 return false;
4712}
4713
4714/// isZeroExtended - Check if a node is a vector value that is zero-extended
4715/// or a constant BUILD_VECTOR with zero-extended elements.
4716static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4717 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4718 return true;
4719 if (isExtendedBUILD_VECTOR(N, DAG, false))
4720 return true;
4721 return false;
4722}
4723
4724/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4725/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004726static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4727 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4728 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004729 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4730 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4731 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004732 LD->isNonTemporal(), LD->isInvariant(),
4733 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004734 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4735 // have been legalized as a BITCAST from v4i32.
4736 if (N->getOpcode() == ISD::BITCAST) {
4737 SDNode *BVN = N->getOperand(0).getNode();
4738 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4739 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4740 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4741 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4742 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4743 }
4744 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4745 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4746 EVT VT = N->getValueType(0);
4747 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4748 unsigned NumElts = VT.getVectorNumElements();
4749 MVT TruncVT = MVT::getIntegerVT(EltSize);
4750 SmallVector<SDValue, 8> Ops;
4751 for (unsigned i = 0; i != NumElts; ++i) {
4752 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4753 const APInt &CInt = C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004754 Ops.push_back(DAG.getConstant(CInt.trunc(EltSize), TruncVT));
Bob Wilson626613d2010-11-23 19:38:38 +00004755 }
4756 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4757 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004758}
4759
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004760static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4761 unsigned Opcode = N->getOpcode();
4762 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4763 SDNode *N0 = N->getOperand(0).getNode();
4764 SDNode *N1 = N->getOperand(1).getNode();
4765 return N0->hasOneUse() && N1->hasOneUse() &&
4766 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4767 }
4768 return false;
4769}
4770
4771static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4772 unsigned Opcode = N->getOpcode();
4773 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4774 SDNode *N0 = N->getOperand(0).getNode();
4775 SDNode *N1 = N->getOperand(1).getNode();
4776 return N0->hasOneUse() && N1->hasOneUse() &&
4777 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4778 }
4779 return false;
4780}
4781
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004782static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4783 // Multiplications are only custom-lowered for 128-bit vectors so that
4784 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4785 EVT VT = Op.getValueType();
4786 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4787 SDNode *N0 = Op.getOperand(0).getNode();
4788 SDNode *N1 = Op.getOperand(1).getNode();
4789 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004790 bool isMLA = false;
4791 bool isN0SExt = isSignExtended(N0, DAG);
4792 bool isN1SExt = isSignExtended(N1, DAG);
4793 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004794 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004795 else {
4796 bool isN0ZExt = isZeroExtended(N0, DAG);
4797 bool isN1ZExt = isZeroExtended(N1, DAG);
4798 if (isN0ZExt && isN1ZExt)
4799 NewOpc = ARMISD::VMULLu;
4800 else if (isN1SExt || isN1ZExt) {
4801 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4802 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4803 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4804 NewOpc = ARMISD::VMULLs;
4805 isMLA = true;
4806 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4807 NewOpc = ARMISD::VMULLu;
4808 isMLA = true;
4809 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4810 std::swap(N0, N1);
4811 NewOpc = ARMISD::VMULLu;
4812 isMLA = true;
4813 }
4814 }
4815
4816 if (!NewOpc) {
4817 if (VT == MVT::v2i64)
4818 // Fall through to expand this. It is not legal.
4819 return SDValue();
4820 else
4821 // Other vector multiplications are legal.
4822 return Op;
4823 }
4824 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004825
4826 // Legalize to a VMULL instruction.
4827 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004828 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004829 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004830 if (!isMLA) {
4831 Op0 = SkipExtension(N0, DAG);
4832 assert(Op0.getValueType().is64BitVector() &&
4833 Op1.getValueType().is64BitVector() &&
4834 "unexpected types for extended operands to VMULL");
4835 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4836 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004837
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004838 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4839 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4840 // vmull q0, d4, d6
4841 // vmlal q0, d5, d6
4842 // is faster than
4843 // vaddl q0, d4, d5
4844 // vmovl q1, d6
4845 // vmul q0, q0, q1
4846 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4847 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4848 EVT Op1VT = Op1.getValueType();
4849 return DAG.getNode(N0->getOpcode(), DL, VT,
4850 DAG.getNode(NewOpc, DL, VT,
4851 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4852 DAG.getNode(NewOpc, DL, VT,
4853 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004854}
4855
Owen Anderson76706012011-04-05 21:48:57 +00004856static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004857LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4858 // Convert to float
4859 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4860 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4861 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4862 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4863 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4864 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4865 // Get reciprocal estimate.
4866 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004867 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004868 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4869 // Because char has a smaller range than uchar, we can actually get away
4870 // without any newton steps. This requires that we use a weird bias
4871 // of 0xb000, however (again, this has been exhaustively tested).
4872 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4873 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4874 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4875 Y = DAG.getConstant(0xb000, MVT::i32);
4876 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4877 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4878 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4879 // Convert back to short.
4880 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4881 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4882 return X;
4883}
4884
Owen Anderson76706012011-04-05 21:48:57 +00004885static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004886LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4887 SDValue N2;
4888 // Convert to float.
4889 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4890 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4891 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4892 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4893 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4894 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004895
Nate Begeman7973f352011-02-11 20:53:29 +00004896 // Use reciprocal estimate and one refinement step.
4897 // float4 recip = vrecpeq_f32(yf);
4898 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004899 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004900 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004901 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004902 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4903 N1, N2);
4904 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4905 // Because short has a smaller range than ushort, we can actually get away
4906 // with only a single newton step. This requires that we use a weird bias
4907 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004908 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004909 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4910 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004911 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004912 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4913 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4914 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4915 // Convert back to integer and return.
4916 // return vmovn_s32(vcvt_s32_f32(result));
4917 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4918 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4919 return N0;
4920}
4921
4922static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4923 EVT VT = Op.getValueType();
4924 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4925 "unexpected type for custom-lowering ISD::SDIV");
4926
4927 DebugLoc dl = Op.getDebugLoc();
4928 SDValue N0 = Op.getOperand(0);
4929 SDValue N1 = Op.getOperand(1);
4930 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004931
Nate Begeman7973f352011-02-11 20:53:29 +00004932 if (VT == MVT::v8i8) {
4933 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4934 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004935
Nate Begeman7973f352011-02-11 20:53:29 +00004936 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4937 DAG.getIntPtrConstant(4));
4938 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004939 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004940 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4941 DAG.getIntPtrConstant(0));
4942 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4943 DAG.getIntPtrConstant(0));
4944
4945 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4946 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4947
4948 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4949 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004950
Nate Begeman7973f352011-02-11 20:53:29 +00004951 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
4952 return N0;
4953 }
4954 return LowerSDIV_v4i16(N0, N1, dl, DAG);
4955}
4956
4957static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
4958 EVT VT = Op.getValueType();
4959 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4960 "unexpected type for custom-lowering ISD::UDIV");
4961
4962 DebugLoc dl = Op.getDebugLoc();
4963 SDValue N0 = Op.getOperand(0);
4964 SDValue N1 = Op.getOperand(1);
4965 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004966
Nate Begeman7973f352011-02-11 20:53:29 +00004967 if (VT == MVT::v8i8) {
4968 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
4969 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004970
Nate Begeman7973f352011-02-11 20:53:29 +00004971 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4972 DAG.getIntPtrConstant(4));
4973 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004974 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004975 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4976 DAG.getIntPtrConstant(0));
4977 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4978 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00004979
Nate Begeman7973f352011-02-11 20:53:29 +00004980 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
4981 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00004982
Nate Begeman7973f352011-02-11 20:53:29 +00004983 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4984 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00004985
4986 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00004987 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
4988 N0);
4989 return N0;
4990 }
Owen Anderson76706012011-04-05 21:48:57 +00004991
Nate Begeman7973f352011-02-11 20:53:29 +00004992 // v4i16 sdiv ... Convert to float.
4993 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
4994 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
4995 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
4996 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
4997 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004998 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00004999
5000 // Use reciprocal estimate and two refinement steps.
5001 // float4 recip = vrecpeq_f32(yf);
5002 // recip *= vrecpsq_f32(yf, recip);
5003 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005004 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005005 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005006 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005007 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005008 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005009 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005010 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005011 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005012 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005013 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5014 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5015 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5016 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005017 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005018 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5019 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5020 N1 = DAG.getConstant(2, MVT::i32);
5021 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5022 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5023 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5024 // Convert back to integer and return.
5025 // return vmovn_u32(vcvt_s32_f32(result));
5026 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5027 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5028 return N0;
5029}
5030
Evan Cheng342e3162011-08-30 01:34:54 +00005031static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5032 EVT VT = Op.getNode()->getValueType(0);
5033 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5034
5035 unsigned Opc;
5036 bool ExtraOp = false;
5037 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005038 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005039 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5040 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5041 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5042 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5043 }
5044
5045 if (!ExtraOp)
5046 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5047 Op.getOperand(1));
5048 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5049 Op.getOperand(1), Op.getOperand(2));
5050}
5051
Eli Friedman74bf18c2011-09-15 22:26:18 +00005052static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005053 // Monotonic load/store is legal for all targets
5054 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5055 return Op;
5056
5057 // Aquire/Release load/store is not legal for targets without a
5058 // dmb or equivalent available.
5059 return SDValue();
5060}
5061
5062
Eli Friedman2bdffe42011-08-31 00:31:29 +00005063static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005064ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5065 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005066 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005067 assert (Node->getValueType(0) == MVT::i64 &&
5068 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005069
Eli Friedman4d3f3292011-08-31 17:52:22 +00005070 SmallVector<SDValue, 6> Ops;
5071 Ops.push_back(Node->getOperand(0)); // Chain
5072 Ops.push_back(Node->getOperand(1)); // Ptr
5073 // Low part of Val1
5074 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5075 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5076 // High part of Val1
5077 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5078 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005079 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005080 // High part of Val1
5081 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5082 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5083 // High part of Val2
5084 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5085 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5086 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005087 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5088 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005089 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005090 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005091 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005092 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5093 Results.push_back(Result.getValue(2));
5094}
5095
Dan Gohmand858e902010-04-17 15:26:15 +00005096SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005097 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005098 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005099 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005100 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005101 case ISD::GlobalAddress:
5102 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5103 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005104 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005105 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005106 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5107 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005108 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005109 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005110 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005111 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005112 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005113 case ISD::SINT_TO_FP:
5114 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5115 case ISD::FP_TO_SINT:
5116 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005117 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005118 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005119 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005120 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005121 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005122 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005123 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5124 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005125 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005126 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005127 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005128 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005129 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005130 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005131 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005132 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005133 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005134 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005135 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005136 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005137 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005138 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005139 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005140 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005141 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005142 case ISD::SDIV: return LowerSDIV(Op, DAG);
5143 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005144 case ISD::ADDC:
5145 case ISD::ADDE:
5146 case ISD::SUBC:
5147 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005148 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005149 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005150 }
Evan Chenga8e29892007-01-19 07:51:42 +00005151}
5152
Duncan Sands1607f052008-12-01 11:39:25 +00005153/// ReplaceNodeResults - Replace the results of node with an illegal result
5154/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005155void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5156 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005157 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005158 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005159 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005160 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005161 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005162 case ISD::BITCAST:
5163 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005164 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005165 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005166 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005167 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005168 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005169 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005170 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005171 return;
5172 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005173 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005174 return;
5175 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005176 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005177 return;
5178 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005179 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005180 return;
5181 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005182 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005183 return;
5184 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005185 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005186 return;
5187 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005188 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005189 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005190 case ISD::ATOMIC_CMP_SWAP:
5191 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5192 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005193 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005194 if (Res.getNode())
5195 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005196}
Chris Lattner27a6c732007-11-24 07:07:01 +00005197
Evan Chenga8e29892007-01-19 07:51:42 +00005198//===----------------------------------------------------------------------===//
5199// ARM Scheduler Hooks
5200//===----------------------------------------------------------------------===//
5201
5202MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005203ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5204 MachineBasicBlock *BB,
5205 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005206 unsigned dest = MI->getOperand(0).getReg();
5207 unsigned ptr = MI->getOperand(1).getReg();
5208 unsigned oldval = MI->getOperand(2).getReg();
5209 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005210 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5211 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005212 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005213
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005214 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5215 unsigned scratch =
Cameron Zwarich141ec632011-05-18 02:29:50 +00005216 MRI.createVirtualRegister(isThumb2 ? ARM::rGPRRegisterClass
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005217 : ARM::GPRRegisterClass);
5218
5219 if (isThumb2) {
Cameron Zwarich141ec632011-05-18 02:29:50 +00005220 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5221 MRI.constrainRegClass(oldval, ARM::rGPRRegisterClass);
5222 MRI.constrainRegClass(newval, ARM::rGPRRegisterClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005223 }
5224
Jim Grosbach5278eb82009-12-11 01:42: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;
Evan Chengaa261022011-02-07 18:50:47 +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 Grosbach5278eb82009-12-11 01:42:04 +00005240 }
5241
5242 MachineFunction *MF = BB->getParent();
5243 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5244 MachineFunction::iterator It = BB;
5245 ++It; // insert the new blocks after the current block
5246
5247 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5248 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5249 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5250 MF->insert(It, loop1MBB);
5251 MF->insert(It, loop2MBB);
5252 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005253
5254 // Transfer the remainder of BB and its successor edges to exitMBB.
5255 exitMBB->splice(exitMBB->begin(), BB,
5256 llvm::next(MachineBasicBlock::iterator(MI)),
5257 BB->end());
5258 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005259
5260 // thisMBB:
5261 // ...
5262 // fallthrough --> loop1MBB
5263 BB->addSuccessor(loop1MBB);
5264
5265 // loop1MBB:
5266 // ldrex dest, [ptr]
5267 // cmp dest, oldval
5268 // bne exitMBB
5269 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005270 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5271 if (ldrOpc == ARM::t2LDREX)
5272 MIB.addImm(0);
5273 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005274 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005275 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005276 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5277 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005278 BB->addSuccessor(loop2MBB);
5279 BB->addSuccessor(exitMBB);
5280
5281 // loop2MBB:
5282 // strex scratch, newval, [ptr]
5283 // cmp scratch, #0
5284 // bne loop1MBB
5285 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005286 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5287 if (strOpc == ARM::t2STREX)
5288 MIB.addImm(0);
5289 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005291 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005292 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5293 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005294 BB->addSuccessor(loop1MBB);
5295 BB->addSuccessor(exitMBB);
5296
5297 // exitMBB:
5298 // ...
5299 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005300
Dan Gohman14152b42010-07-06 20:24:04 +00005301 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005302
Jim Grosbach5278eb82009-12-11 01:42:04 +00005303 return BB;
5304}
5305
5306MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005307ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5308 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005309 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5310 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5311
5312 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005313 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005314 MachineFunction::iterator It = BB;
5315 ++It;
5316
5317 unsigned dest = MI->getOperand(0).getReg();
5318 unsigned ptr = MI->getOperand(1).getReg();
5319 unsigned incr = MI->getOperand(2).getReg();
5320 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005321 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005322
5323 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5324 if (isThumb2) {
5325 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5326 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5327 }
5328
Jim Grosbachc3c23542009-12-14 04:22:04 +00005329 unsigned ldrOpc, strOpc;
5330 switch (Size) {
5331 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005332 case 1:
5333 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005334 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005335 break;
5336 case 2:
5337 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5338 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5339 break;
5340 case 4:
5341 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5342 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5343 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005344 }
5345
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005346 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5347 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5348 MF->insert(It, loopMBB);
5349 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005350
5351 // Transfer the remainder of BB and its successor edges to exitMBB.
5352 exitMBB->splice(exitMBB->begin(), BB,
5353 llvm::next(MachineBasicBlock::iterator(MI)),
5354 BB->end());
5355 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005356
Craig Topper44d23822012-02-22 05:59:10 +00005357 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005358 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5359 unsigned scratch = MRI.createVirtualRegister(TRC);
5360 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005361
5362 // thisMBB:
5363 // ...
5364 // fallthrough --> loopMBB
5365 BB->addSuccessor(loopMBB);
5366
5367 // loopMBB:
5368 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005369 // <binop> scratch2, dest, incr
5370 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005371 // cmp scratch, #0
5372 // bne- loopMBB
5373 // fallthrough --> exitMBB
5374 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005375 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5376 if (ldrOpc == ARM::t2LDREX)
5377 MIB.addImm(0);
5378 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005379 if (BinOpcode) {
5380 // operand order needs to go the other way for NAND
5381 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5382 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5383 addReg(incr).addReg(dest)).addReg(0);
5384 else
5385 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5386 addReg(dest).addReg(incr)).addReg(0);
5387 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005388
Jim Grosbachb6aed502011-09-09 18:37:27 +00005389 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5390 if (strOpc == ARM::t2STREX)
5391 MIB.addImm(0);
5392 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005393 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005394 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005395 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5396 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005397
5398 BB->addSuccessor(loopMBB);
5399 BB->addSuccessor(exitMBB);
5400
5401 // exitMBB:
5402 // ...
5403 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005404
Dan Gohman14152b42010-07-06 20:24:04 +00005405 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005406
Jim Grosbachc3c23542009-12-14 04:22:04 +00005407 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005408}
5409
Jim Grosbachf7da8822011-04-26 19:44:18 +00005410MachineBasicBlock *
5411ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5412 MachineBasicBlock *BB,
5413 unsigned Size,
5414 bool signExtend,
5415 ARMCC::CondCodes Cond) const {
5416 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5417
5418 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5419 MachineFunction *MF = BB->getParent();
5420 MachineFunction::iterator It = BB;
5421 ++It;
5422
5423 unsigned dest = MI->getOperand(0).getReg();
5424 unsigned ptr = MI->getOperand(1).getReg();
5425 unsigned incr = MI->getOperand(2).getReg();
5426 unsigned oldval = dest;
5427 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005428 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005429
5430 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5431 if (isThumb2) {
5432 MRI.constrainRegClass(dest, ARM::rGPRRegisterClass);
5433 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5434 }
5435
Jim Grosbachf7da8822011-04-26 19:44:18 +00005436 unsigned ldrOpc, strOpc, extendOpc;
5437 switch (Size) {
5438 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5439 case 1:
5440 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5441 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005442 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005443 break;
5444 case 2:
5445 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5446 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005447 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005448 break;
5449 case 4:
5450 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5451 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5452 extendOpc = 0;
5453 break;
5454 }
5455
5456 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5457 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5458 MF->insert(It, loopMBB);
5459 MF->insert(It, exitMBB);
5460
5461 // Transfer the remainder of BB and its successor edges to exitMBB.
5462 exitMBB->splice(exitMBB->begin(), BB,
5463 llvm::next(MachineBasicBlock::iterator(MI)),
5464 BB->end());
5465 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5466
Craig Topper44d23822012-02-22 05:59:10 +00005467 const TargetRegisterClass *TRC =
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005468 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5469 unsigned scratch = MRI.createVirtualRegister(TRC);
5470 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005471
5472 // thisMBB:
5473 // ...
5474 // fallthrough --> loopMBB
5475 BB->addSuccessor(loopMBB);
5476
5477 // loopMBB:
5478 // ldrex dest, ptr
5479 // (sign extend dest, if required)
5480 // cmp dest, incr
5481 // cmov.cond scratch2, dest, incr
5482 // strex scratch, scratch2, ptr
5483 // cmp scratch, #0
5484 // bne- loopMBB
5485 // fallthrough --> exitMBB
5486 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005487 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5488 if (ldrOpc == ARM::t2LDREX)
5489 MIB.addImm(0);
5490 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005491
5492 // Sign extend the value, if necessary.
5493 if (signExtend && extendOpc) {
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005494 oldval = MRI.createVirtualRegister(ARM::GPRRegisterClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005495 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5496 .addReg(dest)
5497 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005498 }
5499
5500 // Build compare and cmov instructions.
5501 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5502 .addReg(oldval).addReg(incr));
5503 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5504 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5505
Jim Grosbachb6aed502011-09-09 18:37:27 +00005506 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5507 if (strOpc == ARM::t2STREX)
5508 MIB.addImm(0);
5509 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005510 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5511 .addReg(scratch).addImm(0));
5512 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5513 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5514
5515 BB->addSuccessor(loopMBB);
5516 BB->addSuccessor(exitMBB);
5517
5518 // exitMBB:
5519 // ...
5520 BB = exitMBB;
5521
5522 MI->eraseFromParent(); // The instruction is gone now.
5523
5524 return BB;
5525}
5526
Eli Friedman2bdffe42011-08-31 00:31:29 +00005527MachineBasicBlock *
5528ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5529 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005530 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005531 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5532 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5533
5534 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5535 MachineFunction *MF = BB->getParent();
5536 MachineFunction::iterator It = BB;
5537 ++It;
5538
5539 unsigned destlo = MI->getOperand(0).getReg();
5540 unsigned desthi = MI->getOperand(1).getReg();
5541 unsigned ptr = MI->getOperand(2).getReg();
5542 unsigned vallo = MI->getOperand(3).getReg();
5543 unsigned valhi = MI->getOperand(4).getReg();
5544 DebugLoc dl = MI->getDebugLoc();
5545 bool isThumb2 = Subtarget->isThumb2();
5546
5547 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5548 if (isThumb2) {
5549 MRI.constrainRegClass(destlo, ARM::rGPRRegisterClass);
5550 MRI.constrainRegClass(desthi, ARM::rGPRRegisterClass);
5551 MRI.constrainRegClass(ptr, ARM::rGPRRegisterClass);
5552 }
5553
5554 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5555 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5556
5557 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005558 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005559 if (IsCmpxchg) {
5560 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5561 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5562 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005563 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5564 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005565 if (IsCmpxchg) {
5566 MF->insert(It, contBB);
5567 MF->insert(It, cont2BB);
5568 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005569 MF->insert(It, exitMBB);
5570
5571 // Transfer the remainder of BB and its successor edges to exitMBB.
5572 exitMBB->splice(exitMBB->begin(), BB,
5573 llvm::next(MachineBasicBlock::iterator(MI)),
5574 BB->end());
5575 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5576
Craig Topper44d23822012-02-22 05:59:10 +00005577 const TargetRegisterClass *TRC =
Eli Friedman2bdffe42011-08-31 00:31:29 +00005578 isThumb2 ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5579 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5580
5581 // thisMBB:
5582 // ...
5583 // fallthrough --> loopMBB
5584 BB->addSuccessor(loopMBB);
5585
5586 // loopMBB:
5587 // ldrexd r2, r3, ptr
5588 // <binopa> r0, r2, incr
5589 // <binopb> r1, r3, incr
5590 // strexd storesuccess, r0, r1, ptr
5591 // cmp storesuccess, #0
5592 // bne- loopMBB
5593 // fallthrough --> exitMBB
5594 //
5595 // Note that the registers are explicitly specified because there is not any
5596 // way to force the register allocator to allocate a register pair.
5597 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005598 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005599 // need to properly enforce the restriction that the two output registers
5600 // for ldrexd must be different.
5601 BB = loopMBB;
5602 // Load
5603 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5604 .addReg(ARM::R2, RegState::Define)
5605 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5606 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5607 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5608 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005609
5610 if (IsCmpxchg) {
5611 // Add early exit
5612 for (unsigned i = 0; i < 2; i++) {
5613 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5614 ARM::CMPrr))
5615 .addReg(i == 0 ? destlo : desthi)
5616 .addReg(i == 0 ? vallo : valhi));
5617 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5618 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5619 BB->addSuccessor(exitMBB);
5620 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5621 BB = (i == 0 ? contBB : cont2BB);
5622 }
5623
5624 // Copy to physregs for strexd
5625 unsigned setlo = MI->getOperand(5).getReg();
5626 unsigned sethi = MI->getOperand(6).getReg();
5627 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5628 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5629 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005630 // Perform binary operation
5631 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5632 .addReg(destlo).addReg(vallo))
5633 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5634 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5635 .addReg(desthi).addReg(valhi)).addReg(0);
5636 } else {
5637 // Copy to physregs for strexd
5638 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5639 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5640 }
5641
5642 // Store
5643 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5644 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5645 // Cmp+jump
5646 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5647 .addReg(storesuccess).addImm(0));
5648 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5649 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5650
5651 BB->addSuccessor(loopMBB);
5652 BB->addSuccessor(exitMBB);
5653
5654 // exitMBB:
5655 // ...
5656 BB = exitMBB;
5657
5658 MI->eraseFromParent(); // The instruction is gone now.
5659
5660 return BB;
5661}
5662
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005663/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5664/// registers the function context.
5665void ARMTargetLowering::
5666SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5667 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005668 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5669 DebugLoc dl = MI->getDebugLoc();
5670 MachineFunction *MF = MBB->getParent();
5671 MachineRegisterInfo *MRI = &MF->getRegInfo();
5672 MachineConstantPool *MCP = MF->getConstantPool();
5673 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5674 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005675
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005676 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005677 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005678
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005679 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005680 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005681 ARMConstantPoolValue *CPV =
5682 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5683 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5684
5685 const TargetRegisterClass *TRC =
5686 isThumb ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5687
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005688 // Grab constant pool and fixed stack memory operands.
5689 MachineMemOperand *CPMMO =
5690 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5691 MachineMemOperand::MOLoad, 4, 4);
5692
5693 MachineMemOperand *FIMMOSt =
5694 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5695 MachineMemOperand::MOStore, 4, 4);
5696
5697 // Load the address of the dispatch MBB into the jump buffer.
5698 if (isThumb2) {
5699 // Incoming value: jbuf
5700 // ldr.n r5, LCPI1_1
5701 // orr r5, r5, #1
5702 // add r5, pc
5703 // str r5, [$jbuf, #+4] ; &jbuf[1]
5704 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5705 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5706 .addConstantPoolIndex(CPI)
5707 .addMemOperand(CPMMO));
5708 // Set the low bit because of thumb mode.
5709 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5710 AddDefaultCC(
5711 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5712 .addReg(NewVReg1, RegState::Kill)
5713 .addImm(0x01)));
5714 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5715 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5716 .addReg(NewVReg2, RegState::Kill)
5717 .addImm(PCLabelId);
5718 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5719 .addReg(NewVReg3, RegState::Kill)
5720 .addFrameIndex(FI)
5721 .addImm(36) // &jbuf[1] :: pc
5722 .addMemOperand(FIMMOSt));
5723 } else if (isThumb) {
5724 // Incoming value: jbuf
5725 // ldr.n r1, LCPI1_4
5726 // add r1, pc
5727 // mov r2, #1
5728 // orrs r1, r2
5729 // add r2, $jbuf, #+4 ; &jbuf[1]
5730 // str r1, [r2]
5731 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5732 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5733 .addConstantPoolIndex(CPI)
5734 .addMemOperand(CPMMO));
5735 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5736 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5737 .addReg(NewVReg1, RegState::Kill)
5738 .addImm(PCLabelId);
5739 // Set the low bit because of thumb mode.
5740 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5741 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5742 .addReg(ARM::CPSR, RegState::Define)
5743 .addImm(1));
5744 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5745 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5746 .addReg(ARM::CPSR, RegState::Define)
5747 .addReg(NewVReg2, RegState::Kill)
5748 .addReg(NewVReg3, RegState::Kill));
5749 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5750 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5751 .addFrameIndex(FI)
5752 .addImm(36)); // &jbuf[1] :: pc
5753 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5754 .addReg(NewVReg4, RegState::Kill)
5755 .addReg(NewVReg5, RegState::Kill)
5756 .addImm(0)
5757 .addMemOperand(FIMMOSt));
5758 } else {
5759 // Incoming value: jbuf
5760 // ldr r1, LCPI1_1
5761 // add r1, pc, r1
5762 // str r1, [$jbuf, #+4] ; &jbuf[1]
5763 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5764 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5765 .addConstantPoolIndex(CPI)
5766 .addImm(0)
5767 .addMemOperand(CPMMO));
5768 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5769 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5770 .addReg(NewVReg1, RegState::Kill)
5771 .addImm(PCLabelId));
5772 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5773 .addReg(NewVReg2, RegState::Kill)
5774 .addFrameIndex(FI)
5775 .addImm(36) // &jbuf[1] :: pc
5776 .addMemOperand(FIMMOSt));
5777 }
5778}
5779
5780MachineBasicBlock *ARMTargetLowering::
5781EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5782 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5783 DebugLoc dl = MI->getDebugLoc();
5784 MachineFunction *MF = MBB->getParent();
5785 MachineRegisterInfo *MRI = &MF->getRegInfo();
5786 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5787 MachineFrameInfo *MFI = MF->getFrameInfo();
5788 int FI = MFI->getFunctionContextIndex();
5789
5790 const TargetRegisterClass *TRC =
5791 Subtarget->isThumb() ? ARM::tGPRRegisterClass : ARM::GPRRegisterClass;
5792
Bill Wendling04f15b42011-10-06 21:29:56 +00005793 // Get a mapping of the call site numbers to all of the landing pads they're
5794 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005795 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5796 unsigned MaxCSNum = 0;
5797 MachineModuleInfo &MMI = MF->getMMI();
5798 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E; ++BB) {
5799 if (!BB->isLandingPad()) continue;
5800
5801 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5802 // pad.
5803 for (MachineBasicBlock::iterator
5804 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5805 if (!II->isEHLabel()) continue;
5806
5807 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005808 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005809
Bill Wendling5cbef192011-10-05 23:28:57 +00005810 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5811 for (SmallVectorImpl<unsigned>::iterator
5812 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5813 CSI != CSE; ++CSI) {
5814 CallSiteNumToLPad[*CSI].push_back(BB);
5815 MaxCSNum = std::max(MaxCSNum, *CSI);
5816 }
Bill Wendling2a850152011-10-05 00:02:33 +00005817 break;
5818 }
5819 }
5820
5821 // Get an ordered list of the machine basic blocks for the jump table.
5822 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005823 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005824 LPadList.reserve(CallSiteNumToLPad.size());
5825 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5826 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5827 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005828 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005829 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005830 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5831 }
Bill Wendling2a850152011-10-05 00:02:33 +00005832 }
5833
Bill Wendling5cbef192011-10-05 23:28:57 +00005834 assert(!LPadList.empty() &&
5835 "No landing pad destinations for the dispatch jump table!");
5836
Bill Wendling04f15b42011-10-06 21:29:56 +00005837 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005838 MachineJumpTableInfo *JTI =
5839 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5840 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5841 unsigned UId = AFI->createJumpTableUId();
5842
Bill Wendling04f15b42011-10-06 21:29:56 +00005843 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005844
5845 // Shove the dispatch's address into the return slot in the function context.
5846 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5847 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005848
Bill Wendlingbb734682011-10-05 00:39:32 +00005849 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005850 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005851 DispatchBB->addSuccessor(TrapBB);
5852
5853 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5854 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005855
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005856 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005857 MF->insert(MF->end(), DispatchBB);
5858 MF->insert(MF->end(), DispContBB);
5859 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005860
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005861 // Insert code into the entry block that creates and registers the function
5862 // context.
5863 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5864
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005865 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005866 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005867 MachineMemOperand::MOLoad |
5868 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005869
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005870 if (AFI->isThumb1OnlyFunction())
5871 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5872 else if (!Subtarget->hasVFP2())
5873 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
5874 else
5875 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005876
Bill Wendling952cb502011-10-18 22:49:07 +00005877 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005878 if (Subtarget->isThumb2()) {
5879 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5880 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5881 .addFrameIndex(FI)
5882 .addImm(4)
5883 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005884
Bill Wendling952cb502011-10-18 22:49:07 +00005885 if (NumLPads < 256) {
5886 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5887 .addReg(NewVReg1)
5888 .addImm(LPadList.size()));
5889 } else {
5890 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5891 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005892 .addImm(NumLPads & 0xFFFF));
5893
5894 unsigned VReg2 = VReg1;
5895 if ((NumLPads & 0xFFFF0000) != 0) {
5896 VReg2 = MRI->createVirtualRegister(TRC);
5897 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5898 .addReg(VReg1)
5899 .addImm(NumLPads >> 16));
5900 }
5901
Bill Wendling952cb502011-10-18 22:49:07 +00005902 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5903 .addReg(NewVReg1)
5904 .addReg(VReg2));
5905 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005906
Bill Wendling95ce2e92011-10-06 22:53:00 +00005907 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5908 .addMBB(TrapBB)
5909 .addImm(ARMCC::HI)
5910 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005911
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005912 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5913 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005914 .addJumpTableIndex(MJTI)
5915 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005916
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005917 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005918 AddDefaultCC(
5919 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005920 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5921 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005922 .addReg(NewVReg1)
5923 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5924
5925 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005926 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005927 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005928 .addJumpTableIndex(MJTI)
5929 .addImm(UId);
5930 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005931 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5932 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5933 .addFrameIndex(FI)
5934 .addImm(1)
5935 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005936
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005937 if (NumLPads < 256) {
5938 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5939 .addReg(NewVReg1)
5940 .addImm(NumLPads));
5941 } else {
5942 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005943 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
5944 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
5945
5946 // MachineConstantPool wants an explicit alignment.
5947 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
5948 if (Align == 0)
5949 Align = getTargetData()->getTypeAllocSize(C->getType());
5950 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005951
5952 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5953 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
5954 .addReg(VReg1, RegState::Define)
5955 .addConstantPoolIndex(Idx));
5956 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
5957 .addReg(NewVReg1)
5958 .addReg(VReg1));
5959 }
5960
Bill Wendling083a8eb2011-10-06 23:37:36 +00005961 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
5962 .addMBB(TrapBB)
5963 .addImm(ARMCC::HI)
5964 .addReg(ARM::CPSR);
5965
5966 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5967 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
5968 .addReg(ARM::CPSR, RegState::Define)
5969 .addReg(NewVReg1)
5970 .addImm(2));
5971
5972 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00005973 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00005974 .addJumpTableIndex(MJTI)
5975 .addImm(UId));
5976
5977 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5978 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
5979 .addReg(ARM::CPSR, RegState::Define)
5980 .addReg(NewVReg2, RegState::Kill)
5981 .addReg(NewVReg3));
5982
5983 MachineMemOperand *JTMMOLd =
5984 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
5985 MachineMemOperand::MOLoad, 4, 4);
5986
5987 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5988 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
5989 .addReg(NewVReg4, RegState::Kill)
5990 .addImm(0)
5991 .addMemOperand(JTMMOLd));
5992
5993 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
5994 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
5995 .addReg(ARM::CPSR, RegState::Define)
5996 .addReg(NewVReg5, RegState::Kill)
5997 .addReg(NewVReg3));
5998
5999 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6000 .addReg(NewVReg6, RegState::Kill)
6001 .addJumpTableIndex(MJTI)
6002 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006003 } else {
6004 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6005 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6006 .addFrameIndex(FI)
6007 .addImm(4)
6008 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006009
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006010 if (NumLPads < 256) {
6011 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6012 .addReg(NewVReg1)
6013 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006014 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006015 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6016 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006017 .addImm(NumLPads & 0xFFFF));
6018
6019 unsigned VReg2 = VReg1;
6020 if ((NumLPads & 0xFFFF0000) != 0) {
6021 VReg2 = MRI->createVirtualRegister(TRC);
6022 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6023 .addReg(VReg1)
6024 .addImm(NumLPads >> 16));
6025 }
6026
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006027 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6028 .addReg(NewVReg1)
6029 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006030 } else {
6031 MachineConstantPool *ConstantPool = MF->getConstantPool();
6032 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6033 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6034
6035 // MachineConstantPool wants an explicit alignment.
6036 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6037 if (Align == 0)
6038 Align = getTargetData()->getTypeAllocSize(C->getType());
6039 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6040
6041 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6042 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6043 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006044 .addConstantPoolIndex(Idx)
6045 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006046 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6047 .addReg(NewVReg1)
6048 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006049 }
6050
Bill Wendling95ce2e92011-10-06 22:53:00 +00006051 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6052 .addMBB(TrapBB)
6053 .addImm(ARMCC::HI)
6054 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006055
Bill Wendling564392b2011-10-18 22:11:18 +00006056 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006057 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006058 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006059 .addReg(NewVReg1)
6060 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006061 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6062 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006063 .addJumpTableIndex(MJTI)
6064 .addImm(UId));
6065
6066 MachineMemOperand *JTMMOLd =
6067 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6068 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006069 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006070 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006071 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6072 .addReg(NewVReg3, RegState::Kill)
6073 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006074 .addImm(0)
6075 .addMemOperand(JTMMOLd));
6076
6077 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006078 .addReg(NewVReg5, RegState::Kill)
6079 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006080 .addJumpTableIndex(MJTI)
6081 .addImm(UId);
6082 }
Bill Wendling2a850152011-10-05 00:02:33 +00006083
Bill Wendlingbb734682011-10-05 00:39:32 +00006084 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006085 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006086 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006087 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6088 MachineBasicBlock *CurMBB = *I;
6089 if (PrevMBB != CurMBB)
6090 DispContBB->addSuccessor(CurMBB);
6091 PrevMBB = CurMBB;
6092 }
6093
Bill Wendling24bb9252011-10-17 05:25:09 +00006094 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006095 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6096 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006097 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006098 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006099 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6100 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6101 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006102
6103 // Remove the landing pad successor from the invoke block and replace it
6104 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006105 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6106 BB->succ_end());
6107 while (!Successors.empty()) {
6108 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006109 if (SMBB->isLandingPad()) {
6110 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006111 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006112 }
6113 }
6114
6115 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006116
6117 // Find the invoke call and mark all of the callee-saved registers as
6118 // 'implicit defined' so that they're spilled. This prevents code from
6119 // moving instructions to before the EH block, where they will never be
6120 // executed.
6121 for (MachineBasicBlock::reverse_iterator
6122 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006123 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006124
6125 DenseMap<unsigned, bool> DefRegs;
6126 for (MachineInstr::mop_iterator
6127 OI = II->operands_begin(), OE = II->operands_end();
6128 OI != OE; ++OI) {
6129 if (!OI->isReg()) continue;
6130 DefRegs[OI->getReg()] = true;
6131 }
6132
6133 MachineInstrBuilder MIB(&*II);
6134
Bill Wendling5d798592011-10-14 23:55:44 +00006135 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006136 unsigned Reg = SavedRegs[i];
6137 if (Subtarget->isThumb2() &&
6138 !ARM::tGPRRegisterClass->contains(Reg) &&
6139 !ARM::hGPRRegisterClass->contains(Reg))
6140 continue;
6141 else if (Subtarget->isThumb1Only() &&
6142 !ARM::tGPRRegisterClass->contains(Reg))
6143 continue;
6144 else if (!Subtarget->isThumb() &&
6145 !ARM::GPRRegisterClass->contains(Reg))
6146 continue;
6147 if (!DefRegs[Reg])
6148 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006149 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006150
6151 break;
6152 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006153 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006154
Bill Wendlingf7b02072011-10-18 18:30:49 +00006155 // Mark all former landing pads as non-landing pads. The dispatch is the only
6156 // landing pad now.
6157 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6158 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6159 (*I)->setIsLandingPad(false);
6160
Bill Wendlingbb734682011-10-05 00:39:32 +00006161 // The instruction is gone now.
6162 MI->eraseFromParent();
6163
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006164 return MBB;
6165}
6166
Evan Cheng218977b2010-07-13 19:27:42 +00006167static
6168MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6169 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6170 E = MBB->succ_end(); I != E; ++I)
6171 if (*I != Succ)
6172 return *I;
6173 llvm_unreachable("Expecting a BB with two successors!");
6174}
6175
Jim Grosbache801dc42009-12-12 01:40:06 +00006176MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006177ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006178 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006179 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006180 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006181 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006182 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006183 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006184 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006185 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006186 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006187 // The Thumb2 pre-indexed stores have the same MI operands, they just
6188 // define them differently in the .td files from the isel patterns, so
6189 // they need pseudos.
6190 case ARM::t2STR_preidx:
6191 MI->setDesc(TII->get(ARM::t2STR_PRE));
6192 return BB;
6193 case ARM::t2STRB_preidx:
6194 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6195 return BB;
6196 case ARM::t2STRH_preidx:
6197 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6198 return BB;
6199
Jim Grosbach19dec202011-08-05 20:35:44 +00006200 case ARM::STRi_preidx:
6201 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006202 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006203 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6204 // Decode the offset.
6205 unsigned Offset = MI->getOperand(4).getImm();
6206 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6207 Offset = ARM_AM::getAM2Offset(Offset);
6208 if (isSub)
6209 Offset = -Offset;
6210
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006211 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006212 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006213 .addOperand(MI->getOperand(0)) // Rn_wb
6214 .addOperand(MI->getOperand(1)) // Rt
6215 .addOperand(MI->getOperand(2)) // Rn
6216 .addImm(Offset) // offset (skip GPR==zero_reg)
6217 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006218 .addOperand(MI->getOperand(6))
6219 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006220 MI->eraseFromParent();
6221 return BB;
6222 }
6223 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006224 case ARM::STRBr_preidx:
6225 case ARM::STRH_preidx: {
6226 unsigned NewOpc;
6227 switch (MI->getOpcode()) {
6228 default: llvm_unreachable("unexpected opcode!");
6229 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6230 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6231 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6232 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006233 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6234 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6235 MIB.addOperand(MI->getOperand(i));
6236 MI->eraseFromParent();
6237 return BB;
6238 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006239 case ARM::ATOMIC_LOAD_ADD_I8:
6240 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6241 case ARM::ATOMIC_LOAD_ADD_I16:
6242 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6243 case ARM::ATOMIC_LOAD_ADD_I32:
6244 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006245
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006246 case ARM::ATOMIC_LOAD_AND_I8:
6247 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6248 case ARM::ATOMIC_LOAD_AND_I16:
6249 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6250 case ARM::ATOMIC_LOAD_AND_I32:
6251 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006252
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006253 case ARM::ATOMIC_LOAD_OR_I8:
6254 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6255 case ARM::ATOMIC_LOAD_OR_I16:
6256 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6257 case ARM::ATOMIC_LOAD_OR_I32:
6258 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006259
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006260 case ARM::ATOMIC_LOAD_XOR_I8:
6261 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6262 case ARM::ATOMIC_LOAD_XOR_I16:
6263 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6264 case ARM::ATOMIC_LOAD_XOR_I32:
6265 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006266
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006267 case ARM::ATOMIC_LOAD_NAND_I8:
6268 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6269 case ARM::ATOMIC_LOAD_NAND_I16:
6270 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6271 case ARM::ATOMIC_LOAD_NAND_I32:
6272 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006273
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006274 case ARM::ATOMIC_LOAD_SUB_I8:
6275 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6276 case ARM::ATOMIC_LOAD_SUB_I16:
6277 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6278 case ARM::ATOMIC_LOAD_SUB_I32:
6279 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006280
Jim Grosbachf7da8822011-04-26 19:44:18 +00006281 case ARM::ATOMIC_LOAD_MIN_I8:
6282 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6283 case ARM::ATOMIC_LOAD_MIN_I16:
6284 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6285 case ARM::ATOMIC_LOAD_MIN_I32:
6286 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6287
6288 case ARM::ATOMIC_LOAD_MAX_I8:
6289 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6290 case ARM::ATOMIC_LOAD_MAX_I16:
6291 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6292 case ARM::ATOMIC_LOAD_MAX_I32:
6293 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6294
6295 case ARM::ATOMIC_LOAD_UMIN_I8:
6296 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6297 case ARM::ATOMIC_LOAD_UMIN_I16:
6298 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6299 case ARM::ATOMIC_LOAD_UMIN_I32:
6300 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6301
6302 case ARM::ATOMIC_LOAD_UMAX_I8:
6303 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6304 case ARM::ATOMIC_LOAD_UMAX_I16:
6305 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6306 case ARM::ATOMIC_LOAD_UMAX_I32:
6307 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6308
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006309 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6310 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6311 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006312
6313 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6314 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6315 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006316
Eli Friedman2bdffe42011-08-31 00:31:29 +00006317
6318 case ARM::ATOMADD6432:
6319 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006320 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6321 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006322 case ARM::ATOMSUB6432:
6323 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006324 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6325 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006326 case ARM::ATOMOR6432:
6327 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006328 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006329 case ARM::ATOMXOR6432:
6330 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006331 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006332 case ARM::ATOMAND6432:
6333 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006334 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006335 case ARM::ATOMSWAP6432:
6336 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006337 case ARM::ATOMCMPXCHG6432:
6338 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6339 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6340 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006341
Evan Cheng007ea272009-08-12 05:17:19 +00006342 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006343 // To "insert" a SELECT_CC instruction, we actually have to insert the
6344 // diamond control-flow pattern. The incoming instruction knows the
6345 // destination vreg to set, the condition code register to branch on, the
6346 // true/false values to select between, and a branch opcode to use.
6347 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006348 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006349 ++It;
6350
6351 // thisMBB:
6352 // ...
6353 // TrueVal = ...
6354 // cmpTY ccX, r1, r2
6355 // bCC copy1MBB
6356 // fallthrough --> copy0MBB
6357 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006358 MachineFunction *F = BB->getParent();
6359 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6360 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006361 F->insert(It, copy0MBB);
6362 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006363
6364 // Transfer the remainder of BB and its successor edges to sinkMBB.
6365 sinkMBB->splice(sinkMBB->begin(), BB,
6366 llvm::next(MachineBasicBlock::iterator(MI)),
6367 BB->end());
6368 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6369
Dan Gohman258c58c2010-07-06 15:49:48 +00006370 BB->addSuccessor(copy0MBB);
6371 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006372
Dan Gohman14152b42010-07-06 20:24:04 +00006373 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6374 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6375
Evan Chenga8e29892007-01-19 07:51:42 +00006376 // copy0MBB:
6377 // %FalseValue = ...
6378 // # fallthrough to sinkMBB
6379 BB = copy0MBB;
6380
6381 // Update machine-CFG edges
6382 BB->addSuccessor(sinkMBB);
6383
6384 // sinkMBB:
6385 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6386 // ...
6387 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006388 BuildMI(*BB, BB->begin(), dl,
6389 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006390 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6391 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6392
Dan Gohman14152b42010-07-06 20:24:04 +00006393 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006394 return BB;
6395 }
Evan Cheng86198642009-08-07 00:34:42 +00006396
Evan Cheng218977b2010-07-13 19:27:42 +00006397 case ARM::BCCi64:
6398 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006399 // If there is an unconditional branch to the other successor, remove it.
6400 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006401
Evan Cheng218977b2010-07-13 19:27:42 +00006402 // Compare both parts that make up the double comparison separately for
6403 // equality.
6404 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6405
6406 unsigned LHS1 = MI->getOperand(1).getReg();
6407 unsigned LHS2 = MI->getOperand(2).getReg();
6408 if (RHSisZero) {
6409 AddDefaultPred(BuildMI(BB, dl,
6410 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6411 .addReg(LHS1).addImm(0));
6412 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6413 .addReg(LHS2).addImm(0)
6414 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6415 } else {
6416 unsigned RHS1 = MI->getOperand(3).getReg();
6417 unsigned RHS2 = MI->getOperand(4).getReg();
6418 AddDefaultPred(BuildMI(BB, dl,
6419 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6420 .addReg(LHS1).addReg(RHS1));
6421 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6422 .addReg(LHS2).addReg(RHS2)
6423 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6424 }
6425
6426 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6427 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6428 if (MI->getOperand(0).getImm() == ARMCC::NE)
6429 std::swap(destMBB, exitMBB);
6430
6431 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6432 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006433 if (isThumb2)
6434 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6435 else
6436 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006437
6438 MI->eraseFromParent(); // The pseudo instruction is gone now.
6439 return BB;
6440 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006441
Bill Wendling5bc85282011-10-17 20:37:20 +00006442 case ARM::Int_eh_sjlj_setjmp:
6443 case ARM::Int_eh_sjlj_setjmp_nofp:
6444 case ARM::tInt_eh_sjlj_setjmp:
6445 case ARM::t2Int_eh_sjlj_setjmp:
6446 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6447 EmitSjLjDispatchBlock(MI, BB);
6448 return BB;
6449
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006450 case ARM::ABS:
6451 case ARM::t2ABS: {
6452 // To insert an ABS instruction, we have to insert the
6453 // diamond control-flow pattern. The incoming instruction knows the
6454 // source vreg to test against 0, the destination vreg to set,
6455 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006456 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006457 // It transforms
6458 // V1 = ABS V0
6459 // into
6460 // V2 = MOVS V0
6461 // BCC (branch to SinkBB if V0 >= 0)
6462 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006463 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006464 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6465 MachineFunction::iterator BBI = BB;
6466 ++BBI;
6467 MachineFunction *Fn = BB->getParent();
6468 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6469 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6470 Fn->insert(BBI, RSBBB);
6471 Fn->insert(BBI, SinkBB);
6472
6473 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6474 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6475 bool isThumb2 = Subtarget->isThumb2();
6476 MachineRegisterInfo &MRI = Fn->getRegInfo();
6477 // In Thumb mode S must not be specified if source register is the SP or
6478 // PC and if destination register is the SP, so restrict register class
6479 unsigned NewMovDstReg = MRI.createVirtualRegister(
6480 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6481 unsigned NewRsbDstReg = MRI.createVirtualRegister(
6482 isThumb2 ? ARM::rGPRRegisterClass : ARM::GPRRegisterClass);
6483
6484 // Transfer the remainder of BB and its successor edges to sinkMBB.
6485 SinkBB->splice(SinkBB->begin(), BB,
6486 llvm::next(MachineBasicBlock::iterator(MI)),
6487 BB->end());
6488 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6489
6490 BB->addSuccessor(RSBBB);
6491 BB->addSuccessor(SinkBB);
6492
6493 // fall through to SinkMBB
6494 RSBBB->addSuccessor(SinkBB);
6495
6496 // insert a movs at the end of BB
6497 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6498 NewMovDstReg)
6499 .addReg(ABSSrcReg, RegState::Kill)
6500 .addImm((unsigned)ARMCC::AL).addReg(0)
6501 .addReg(ARM::CPSR, RegState::Define);
6502
6503 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006504 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006505 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6506 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6507
6508 // insert rsbri in RSBBB
6509 // Note: BCC and rsbri will be converted into predicated rsbmi
6510 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006511 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006512 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6513 .addReg(NewMovDstReg, RegState::Kill)
6514 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6515
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006516 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006517 // reuse ABSDstReg to not change uses of ABS instruction
6518 BuildMI(*SinkBB, SinkBB->begin(), dl,
6519 TII->get(ARM::PHI), ABSDstReg)
6520 .addReg(NewRsbDstReg).addMBB(RSBBB)
6521 .addReg(NewMovDstReg).addMBB(BB);
6522
6523 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006524 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006525
6526 // return last added BB
6527 return SinkBB;
6528 }
Evan Chenga8e29892007-01-19 07:51:42 +00006529 }
6530}
6531
Evan Cheng37fefc22011-08-30 19:09:48 +00006532void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6533 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006534 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006535 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6536 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6537 return;
6538 }
6539
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006540 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006541 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6542 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6543 // operand is still set to noreg. If needed, set the optional operand's
6544 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006545 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006546 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006547
Andrew Trick3be654f2011-09-21 02:20:46 +00006548 // Rename pseudo opcodes.
6549 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6550 if (NewOpc) {
6551 const ARMBaseInstrInfo *TII =
6552 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006553 MCID = &TII->get(NewOpc);
6554
6555 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6556 "converted opcode should be the same except for cc_out");
6557
6558 MI->setDesc(*MCID);
6559
6560 // Add the optional cc_out operand
6561 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006562 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006563 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006564
6565 // Any ARM instruction that sets the 's' bit should specify an optional
6566 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006567 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006568 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006569 return;
6570 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006571 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6572 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006573 bool definesCPSR = false;
6574 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006575 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006576 i != e; ++i) {
6577 const MachineOperand &MO = MI->getOperand(i);
6578 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6579 definesCPSR = true;
6580 if (MO.isDead())
6581 deadCPSR = true;
6582 MI->RemoveOperand(i);
6583 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006584 }
6585 }
Andrew Trick4815d562011-09-20 03:17:40 +00006586 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006587 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006588 return;
6589 }
6590 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006591 if (deadCPSR) {
6592 assert(!MI->getOperand(ccOutIdx).getReg() &&
6593 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006594 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006595 }
Andrew Trick4815d562011-09-20 03:17:40 +00006596
Andrew Trick3be654f2011-09-21 02:20:46 +00006597 // If this instruction was defined with an optional CPSR def and its dag node
6598 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006599 MachineOperand &MO = MI->getOperand(ccOutIdx);
6600 MO.setReg(ARM::CPSR);
6601 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006602}
6603
Evan Chenga8e29892007-01-19 07:51:42 +00006604//===----------------------------------------------------------------------===//
6605// ARM Optimization Hooks
6606//===----------------------------------------------------------------------===//
6607
Chris Lattnerd1980a52009-03-12 06:52:53 +00006608static
6609SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6610 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006611 SelectionDAG &DAG = DCI.DAG;
6612 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006613 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006614 unsigned Opc = N->getOpcode();
6615 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6616 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6617 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6618 ISD::CondCode CC = ISD::SETCC_INVALID;
6619
6620 if (isSlctCC) {
6621 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6622 } else {
6623 SDValue CCOp = Slct.getOperand(0);
6624 if (CCOp.getOpcode() == ISD::SETCC)
6625 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6626 }
6627
6628 bool DoXform = false;
6629 bool InvCC = false;
6630 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6631 "Bad input!");
6632
6633 if (LHS.getOpcode() == ISD::Constant &&
6634 cast<ConstantSDNode>(LHS)->isNullValue()) {
6635 DoXform = true;
6636 } else if (CC != ISD::SETCC_INVALID &&
6637 RHS.getOpcode() == ISD::Constant &&
6638 cast<ConstantSDNode>(RHS)->isNullValue()) {
6639 std::swap(LHS, RHS);
6640 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006641 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006642 Op0.getOperand(0).getValueType();
6643 bool isInt = OpVT.isInteger();
6644 CC = ISD::getSetCCInverse(CC, isInt);
6645
6646 if (!TLI.isCondCodeLegal(CC, OpVT))
6647 return SDValue(); // Inverse operator isn't legal.
6648
6649 DoXform = true;
6650 InvCC = true;
6651 }
6652
6653 if (DoXform) {
6654 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6655 if (isSlctCC)
6656 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6657 Slct.getOperand(0), Slct.getOperand(1), CC);
6658 SDValue CCOp = Slct.getOperand(0);
6659 if (InvCC)
6660 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6661 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6662 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6663 CCOp, OtherOp, Result);
6664 }
6665 return SDValue();
6666}
6667
Eric Christopherfa6f5912011-06-29 21:10:36 +00006668// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006669// (only after legalization).
6670static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6671 TargetLowering::DAGCombinerInfo &DCI,
6672 const ARMSubtarget *Subtarget) {
6673
6674 // Only perform optimization if after legalize, and if NEON is available. We
6675 // also expected both operands to be BUILD_VECTORs.
6676 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6677 || N0.getOpcode() != ISD::BUILD_VECTOR
6678 || N1.getOpcode() != ISD::BUILD_VECTOR)
6679 return SDValue();
6680
6681 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6682 EVT VT = N->getValueType(0);
6683 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6684 return SDValue();
6685
6686 // Check that the vector operands are of the right form.
6687 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6688 // operands, where N is the size of the formed vector.
6689 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6690 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006691
6692 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006693 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006694 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006695 SDValue Vec = N0->getOperand(0)->getOperand(0);
6696 SDNode *V = Vec.getNode();
6697 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006698
Eric Christopherfa6f5912011-06-29 21:10:36 +00006699 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006700 // check to see if each of their operands are an EXTRACT_VECTOR with
6701 // the same vector and appropriate index.
6702 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6703 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6704 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006705
Tanya Lattner189531f2011-06-14 23:48:48 +00006706 SDValue ExtVec0 = N0->getOperand(i);
6707 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006708
Tanya Lattner189531f2011-06-14 23:48:48 +00006709 // First operand is the vector, verify its the same.
6710 if (V != ExtVec0->getOperand(0).getNode() ||
6711 V != ExtVec1->getOperand(0).getNode())
6712 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006713
Tanya Lattner189531f2011-06-14 23:48:48 +00006714 // Second is the constant, verify its correct.
6715 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6716 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006717
Tanya Lattner189531f2011-06-14 23:48:48 +00006718 // For the constant, we want to see all the even or all the odd.
6719 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6720 || C1->getZExtValue() != nextIndex+1)
6721 return SDValue();
6722
6723 // Increment index.
6724 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006725 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006726 return SDValue();
6727 }
6728
6729 // Create VPADDL node.
6730 SelectionDAG &DAG = DCI.DAG;
6731 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006732
6733 // Build operand list.
6734 SmallVector<SDValue, 8> Ops;
6735 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6736 TLI.getPointerTy()));
6737
6738 // Input is the vector.
6739 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006740
Tanya Lattner189531f2011-06-14 23:48:48 +00006741 // Get widened type and narrowed type.
6742 MVT widenType;
6743 unsigned numElem = VT.getVectorNumElements();
6744 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6745 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6746 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6747 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6748 default:
Craig Topperbc219812012-02-07 02:50:20 +00006749 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006750 }
6751
6752 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6753 widenType, &Ops[0], Ops.size());
6754 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6755}
6756
Bob Wilson3d5792a2010-07-29 20:34:14 +00006757/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6758/// operands N0 and N1. This is a helper for PerformADDCombine that is
6759/// called with the default operands, and if that fails, with commuted
6760/// operands.
6761static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006762 TargetLowering::DAGCombinerInfo &DCI,
6763 const ARMSubtarget *Subtarget){
6764
6765 // Attempt to create vpaddl for this add.
6766 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6767 if (Result.getNode())
6768 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006769
Chris Lattnerd1980a52009-03-12 06:52:53 +00006770 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6771 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6772 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6773 if (Result.getNode()) return Result;
6774 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006775 return SDValue();
6776}
6777
Bob Wilson3d5792a2010-07-29 20:34:14 +00006778/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6779///
6780static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006781 TargetLowering::DAGCombinerInfo &DCI,
6782 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006783 SDValue N0 = N->getOperand(0);
6784 SDValue N1 = N->getOperand(1);
6785
6786 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006787 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006788 if (Result.getNode())
6789 return Result;
6790
6791 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006792 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006793}
6794
Chris Lattnerd1980a52009-03-12 06:52:53 +00006795/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006796///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006797static SDValue PerformSUBCombine(SDNode *N,
6798 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006799 SDValue N0 = N->getOperand(0);
6800 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006801
Chris Lattnerd1980a52009-03-12 06:52:53 +00006802 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6803 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6804 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6805 if (Result.getNode()) return Result;
6806 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006807
Chris Lattnerd1980a52009-03-12 06:52:53 +00006808 return SDValue();
6809}
6810
Evan Cheng463d3582011-03-31 19:38:48 +00006811/// PerformVMULCombine
6812/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6813/// special multiplier accumulator forwarding.
6814/// vmul d3, d0, d2
6815/// vmla d3, d1, d2
6816/// is faster than
6817/// vadd d3, d0, d1
6818/// vmul d3, d3, d2
6819static SDValue PerformVMULCombine(SDNode *N,
6820 TargetLowering::DAGCombinerInfo &DCI,
6821 const ARMSubtarget *Subtarget) {
6822 if (!Subtarget->hasVMLxForwarding())
6823 return SDValue();
6824
6825 SelectionDAG &DAG = DCI.DAG;
6826 SDValue N0 = N->getOperand(0);
6827 SDValue N1 = N->getOperand(1);
6828 unsigned Opcode = N0.getOpcode();
6829 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6830 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006831 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006832 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6833 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6834 return SDValue();
6835 std::swap(N0, N1);
6836 }
6837
6838 EVT VT = N->getValueType(0);
6839 DebugLoc DL = N->getDebugLoc();
6840 SDValue N00 = N0->getOperand(0);
6841 SDValue N01 = N0->getOperand(1);
6842 return DAG.getNode(Opcode, DL, VT,
6843 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6844 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6845}
6846
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006847static SDValue PerformMULCombine(SDNode *N,
6848 TargetLowering::DAGCombinerInfo &DCI,
6849 const ARMSubtarget *Subtarget) {
6850 SelectionDAG &DAG = DCI.DAG;
6851
6852 if (Subtarget->isThumb1Only())
6853 return SDValue();
6854
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006855 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6856 return SDValue();
6857
6858 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006859 if (VT.is64BitVector() || VT.is128BitVector())
6860 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006861 if (VT != MVT::i32)
6862 return SDValue();
6863
6864 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6865 if (!C)
6866 return SDValue();
6867
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006868 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006869 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006870
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006871 ShiftAmt = ShiftAmt & (32 - 1);
6872 SDValue V = N->getOperand(0);
6873 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006874
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006875 SDValue Res;
6876 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006877
6878 if (MulAmt >= 0) {
6879 if (isPowerOf2_32(MulAmt - 1)) {
6880 // (mul x, 2^N + 1) => (add (shl x, N), x)
6881 Res = DAG.getNode(ISD::ADD, DL, VT,
6882 V,
6883 DAG.getNode(ISD::SHL, DL, VT,
6884 V,
6885 DAG.getConstant(Log2_32(MulAmt - 1),
6886 MVT::i32)));
6887 } else if (isPowerOf2_32(MulAmt + 1)) {
6888 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6889 Res = DAG.getNode(ISD::SUB, DL, VT,
6890 DAG.getNode(ISD::SHL, DL, VT,
6891 V,
6892 DAG.getConstant(Log2_32(MulAmt + 1),
6893 MVT::i32)),
6894 V);
6895 } else
6896 return SDValue();
6897 } else {
6898 uint64_t MulAmtAbs = -MulAmt;
6899 if (isPowerOf2_32(MulAmtAbs + 1)) {
6900 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
6901 Res = DAG.getNode(ISD::SUB, DL, VT,
6902 V,
6903 DAG.getNode(ISD::SHL, DL, VT,
6904 V,
6905 DAG.getConstant(Log2_32(MulAmtAbs + 1),
6906 MVT::i32)));
6907 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
6908 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
6909 Res = DAG.getNode(ISD::ADD, DL, VT,
6910 V,
6911 DAG.getNode(ISD::SHL, DL, VT,
6912 V,
6913 DAG.getConstant(Log2_32(MulAmtAbs-1),
6914 MVT::i32)));
6915 Res = DAG.getNode(ISD::SUB, DL, VT,
6916 DAG.getConstant(0, MVT::i32),Res);
6917
6918 } else
6919 return SDValue();
6920 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006921
6922 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006923 Res = DAG.getNode(ISD::SHL, DL, VT,
6924 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006925
6926 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006927 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006928 return SDValue();
6929}
6930
Evan Chengc892aeb2012-02-23 01:19:06 +00006931static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
6932 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
6933 return false;
6934
6935 SDValue FalseVal = N.getOperand(0);
6936 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
6937 if (!C)
6938 return false;
6939 if (AllOnes)
6940 return C->isAllOnesValue();
6941 return C->isNullValue();
6942}
6943
6944/// formConditionalOp - Combine an operation with a conditional move operand
6945/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
6946/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
6947static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
6948 bool Commutable) {
6949 SDValue N0 = N->getOperand(0);
6950 SDValue N1 = N->getOperand(1);
6951
6952 bool isAND = N->getOpcode() == ISD::AND;
6953 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
6954 if (!isCand && Commutable) {
6955 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
6956 if (isCand)
6957 std::swap(N0, N1);
6958 }
6959 if (!isCand)
6960 return SDValue();
6961
6962 unsigned Opc = 0;
6963 switch (N->getOpcode()) {
6964 default: llvm_unreachable("Unexpected node");
6965 case ISD::AND: Opc = ARMISD::CAND; break;
6966 case ISD::OR: Opc = ARMISD::COR; break;
6967 case ISD::XOR: Opc = ARMISD::CXOR; break;
6968 }
6969 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
6970 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
6971 N1.getOperand(4));
6972}
6973
Owen Anderson080c0922010-11-05 19:27:46 +00006974static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00006975 TargetLowering::DAGCombinerInfo &DCI,
6976 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00006977
Owen Anderson080c0922010-11-05 19:27:46 +00006978 // Attempt to use immediate-form VBIC
6979 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
6980 DebugLoc dl = N->getDebugLoc();
6981 EVT VT = N->getValueType(0);
6982 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006983
Tanya Lattner0433b212011-04-07 15:24:20 +00006984 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
6985 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00006986
Owen Anderson080c0922010-11-05 19:27:46 +00006987 APInt SplatBits, SplatUndef;
6988 unsigned SplatBitSize;
6989 bool HasAnyUndefs;
6990 if (BVN &&
6991 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6992 if (SplatBitSize <= 64) {
6993 EVT VbicVT;
6994 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
6995 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006996 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00006997 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00006998 if (Val.getNode()) {
6999 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007000 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007001 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007002 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007003 }
7004 }
7005 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007006
Evan Chengc892aeb2012-02-23 01:19:06 +00007007 if (!Subtarget->isThumb1Only()) {
7008 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7009 SDValue CAND = formConditionalOp(N, DAG, true);
7010 if (CAND.getNode())
7011 return CAND;
7012 }
7013
Owen Anderson080c0922010-11-05 19:27:46 +00007014 return SDValue();
7015}
7016
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007017/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7018static SDValue PerformORCombine(SDNode *N,
7019 TargetLowering::DAGCombinerInfo &DCI,
7020 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007021 // Attempt to use immediate-form VORR
7022 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7023 DebugLoc dl = N->getDebugLoc();
7024 EVT VT = N->getValueType(0);
7025 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007026
Tanya Lattner0433b212011-04-07 15:24:20 +00007027 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7028 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007029
Owen Anderson60f48702010-11-03 23:15:26 +00007030 APInt SplatBits, SplatUndef;
7031 unsigned SplatBitSize;
7032 bool HasAnyUndefs;
7033 if (BVN && Subtarget->hasNEON() &&
7034 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7035 if (SplatBitSize <= 64) {
7036 EVT VorrVT;
7037 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7038 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007039 DAG, VorrVT, VT.is128BitVector(),
7040 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007041 if (Val.getNode()) {
7042 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007043 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007044 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007045 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007046 }
7047 }
7048 }
7049
Evan Chengc892aeb2012-02-23 01:19:06 +00007050 if (!Subtarget->isThumb1Only()) {
7051 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7052 SDValue COR = formConditionalOp(N, DAG, true);
7053 if (COR.getNode())
7054 return COR;
7055 }
7056
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007057 SDValue N0 = N->getOperand(0);
7058 if (N0.getOpcode() != ISD::AND)
7059 return SDValue();
7060 SDValue N1 = N->getOperand(1);
7061
7062 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7063 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7064 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7065 APInt SplatUndef;
7066 unsigned SplatBitSize;
7067 bool HasAnyUndefs;
7068
7069 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7070 APInt SplatBits0;
7071 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7072 HasAnyUndefs) && !HasAnyUndefs) {
7073 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7074 APInt SplatBits1;
7075 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7076 HasAnyUndefs) && !HasAnyUndefs &&
7077 SplatBits0 == ~SplatBits1) {
7078 // Canonicalize the vector type to make instruction selection simpler.
7079 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7080 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7081 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007082 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007083 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7084 }
7085 }
7086 }
7087
Jim Grosbach54238562010-07-17 03:30:54 +00007088 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7089 // reasonable.
7090
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007091 // BFI is only available on V6T2+
7092 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7093 return SDValue();
7094
Jim Grosbach54238562010-07-17 03:30:54 +00007095 DebugLoc DL = N->getDebugLoc();
7096 // 1) or (and A, mask), val => ARMbfi A, val, mask
7097 // iff (val & mask) == val
7098 //
7099 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7100 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007101 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007102 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007103 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007104 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007105
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007106 if (VT != MVT::i32)
7107 return SDValue();
7108
Evan Cheng30fb13f2010-12-13 20:32:54 +00007109 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007110
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007111 // The value and the mask need to be constants so we can verify this is
7112 // actually a bitfield set. If the mask is 0xffff, we can do better
7113 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007114 SDValue MaskOp = N0.getOperand(1);
7115 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7116 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007117 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007118 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007119 if (Mask == 0xffff)
7120 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007121 SDValue Res;
7122 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007123 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7124 if (N1C) {
7125 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007126 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007127 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007128
Evan Chenga9688c42010-12-11 04:11:38 +00007129 if (ARM::isBitFieldInvertedMask(Mask)) {
7130 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007131
Evan Cheng30fb13f2010-12-13 20:32:54 +00007132 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007133 DAG.getConstant(Val, MVT::i32),
7134 DAG.getConstant(Mask, MVT::i32));
7135
7136 // Do not add new nodes to DAG combiner worklist.
7137 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007138 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007139 }
Jim Grosbach54238562010-07-17 03:30:54 +00007140 } else if (N1.getOpcode() == ISD::AND) {
7141 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007142 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7143 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007144 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007145 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007146
Eric Christopher29aeed12011-03-26 01:21:03 +00007147 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7148 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007149 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007150 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007151 // The pack halfword instruction works better for masks that fit it,
7152 // so use that when it's available.
7153 if (Subtarget->hasT2ExtractPack() &&
7154 (Mask == 0xffff || Mask == 0xffff0000))
7155 return SDValue();
7156 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007157 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007158 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007159 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007160 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007161 DAG.getConstant(Mask, MVT::i32));
7162 // Do not add new nodes to DAG combiner worklist.
7163 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007164 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007165 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007166 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007167 // The pack halfword instruction works better for masks that fit it,
7168 // so use that when it's available.
7169 if (Subtarget->hasT2ExtractPack() &&
7170 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7171 return SDValue();
7172 // 2b
7173 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007174 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007175 DAG.getConstant(lsb, MVT::i32));
7176 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007177 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007178 // Do not add new nodes to DAG combiner worklist.
7179 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007180 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007181 }
7182 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007183
Evan Cheng30fb13f2010-12-13 20:32:54 +00007184 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7185 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7186 ARM::isBitFieldInvertedMask(~Mask)) {
7187 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7188 // where lsb(mask) == #shamt and masked bits of B are known zero.
7189 SDValue ShAmt = N00.getOperand(1);
7190 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7191 unsigned LSB = CountTrailingZeros_32(Mask);
7192 if (ShAmtC != LSB)
7193 return SDValue();
7194
7195 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7196 DAG.getConstant(~Mask, MVT::i32));
7197
7198 // Do not add new nodes to DAG combiner worklist.
7199 DCI.CombineTo(N, Res, false);
7200 }
7201
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007202 return SDValue();
7203}
7204
Evan Chengc892aeb2012-02-23 01:19:06 +00007205static SDValue PerformXORCombine(SDNode *N,
7206 TargetLowering::DAGCombinerInfo &DCI,
7207 const ARMSubtarget *Subtarget) {
7208 EVT VT = N->getValueType(0);
7209 SelectionDAG &DAG = DCI.DAG;
7210
7211 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7212 return SDValue();
7213
7214 if (!Subtarget->isThumb1Only()) {
7215 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7216 SDValue CXOR = formConditionalOp(N, DAG, true);
7217 if (CXOR.getNode())
7218 return CXOR;
7219 }
7220
7221 return SDValue();
7222}
7223
Evan Chengbf188ae2011-06-15 01:12:31 +00007224/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7225/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007226static SDValue PerformBFICombine(SDNode *N,
7227 TargetLowering::DAGCombinerInfo &DCI) {
7228 SDValue N1 = N->getOperand(1);
7229 if (N1.getOpcode() == ISD::AND) {
7230 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7231 if (!N11C)
7232 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007233 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7234 unsigned LSB = CountTrailingZeros_32(~InvMask);
7235 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7236 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007237 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007238 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007239 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7240 N->getOperand(0), N1.getOperand(0),
7241 N->getOperand(2));
7242 }
7243 return SDValue();
7244}
7245
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007246/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7247/// ARMISD::VMOVRRD.
7248static SDValue PerformVMOVRRDCombine(SDNode *N,
7249 TargetLowering::DAGCombinerInfo &DCI) {
7250 // vmovrrd(vmovdrr x, y) -> x,y
7251 SDValue InDouble = N->getOperand(0);
7252 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7253 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007254
7255 // vmovrrd(load f64) -> (load i32), (load i32)
7256 SDNode *InNode = InDouble.getNode();
7257 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7258 InNode->getValueType(0) == MVT::f64 &&
7259 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7260 !cast<LoadSDNode>(InNode)->isVolatile()) {
7261 // TODO: Should this be done for non-FrameIndex operands?
7262 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7263
7264 SelectionDAG &DAG = DCI.DAG;
7265 DebugLoc DL = LD->getDebugLoc();
7266 SDValue BasePtr = LD->getBasePtr();
7267 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7268 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007269 LD->isNonTemporal(), LD->isInvariant(),
7270 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007271
7272 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7273 DAG.getConstant(4, MVT::i32));
7274 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7275 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007276 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007277 std::min(4U, LD->getAlignment() / 2));
7278
7279 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7280 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7281 DCI.RemoveFromWorklist(LD);
7282 DAG.DeleteNode(LD);
7283 return Result;
7284 }
7285
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007286 return SDValue();
7287}
7288
7289/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7290/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7291static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7292 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7293 SDValue Op0 = N->getOperand(0);
7294 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007295 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007296 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007297 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007298 Op1 = Op1.getOperand(0);
7299 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7300 Op0.getNode() == Op1.getNode() &&
7301 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007302 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007303 N->getValueType(0), Op0.getOperand(0));
7304 return SDValue();
7305}
7306
Bob Wilson31600902010-12-21 06:43:19 +00007307/// PerformSTORECombine - Target-specific dag combine xforms for
7308/// ISD::STORE.
7309static SDValue PerformSTORECombine(SDNode *N,
7310 TargetLowering::DAGCombinerInfo &DCI) {
7311 // Bitcast an i64 store extracted from a vector to f64.
7312 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7313 StoreSDNode *St = cast<StoreSDNode>(N);
7314 SDValue StVal = St->getValue();
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007315 if (!ISD::isNormalStore(St) || St->isVolatile())
7316 return SDValue();
7317
7318 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
7319 StVal.getNode()->hasOneUse() && !St->isVolatile()) {
7320 SelectionDAG &DAG = DCI.DAG;
7321 DebugLoc DL = St->getDebugLoc();
7322 SDValue BasePtr = St->getBasePtr();
7323 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7324 StVal.getNode()->getOperand(0), BasePtr,
7325 St->getPointerInfo(), St->isVolatile(),
7326 St->isNonTemporal(), St->getAlignment());
7327
7328 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7329 DAG.getConstant(4, MVT::i32));
7330 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7331 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7332 St->isNonTemporal(),
7333 std::min(4U, St->getAlignment() / 2));
7334 }
7335
7336 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007337 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7338 return SDValue();
7339
7340 SelectionDAG &DAG = DCI.DAG;
7341 DebugLoc dl = StVal.getDebugLoc();
7342 SDValue IntVec = StVal.getOperand(0);
7343 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7344 IntVec.getValueType().getVectorNumElements());
7345 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7346 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7347 Vec, StVal.getOperand(1));
7348 dl = N->getDebugLoc();
7349 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7350 // Make the DAGCombiner fold the bitcasts.
7351 DCI.AddToWorklist(Vec.getNode());
7352 DCI.AddToWorklist(ExtElt.getNode());
7353 DCI.AddToWorklist(V.getNode());
7354 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7355 St->getPointerInfo(), St->isVolatile(),
7356 St->isNonTemporal(), St->getAlignment(),
7357 St->getTBAAInfo());
7358}
7359
7360/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7361/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7362/// i64 vector to have f64 elements, since the value can then be loaded
7363/// directly into a VFP register.
7364static bool hasNormalLoadOperand(SDNode *N) {
7365 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7366 for (unsigned i = 0; i < NumElts; ++i) {
7367 SDNode *Elt = N->getOperand(i).getNode();
7368 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7369 return true;
7370 }
7371 return false;
7372}
7373
Bob Wilson75f02882010-09-17 22:59:05 +00007374/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7375/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007376static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7377 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007378 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7379 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7380 // into a pair of GPRs, which is fine when the value is used as a scalar,
7381 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007382 SelectionDAG &DAG = DCI.DAG;
7383 if (N->getNumOperands() == 2) {
7384 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7385 if (RV.getNode())
7386 return RV;
7387 }
Bob Wilson75f02882010-09-17 22:59:05 +00007388
Bob Wilson31600902010-12-21 06:43:19 +00007389 // Load i64 elements as f64 values so that type legalization does not split
7390 // them up into i32 values.
7391 EVT VT = N->getValueType(0);
7392 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7393 return SDValue();
7394 DebugLoc dl = N->getDebugLoc();
7395 SmallVector<SDValue, 8> Ops;
7396 unsigned NumElts = VT.getVectorNumElements();
7397 for (unsigned i = 0; i < NumElts; ++i) {
7398 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7399 Ops.push_back(V);
7400 // Make the DAGCombiner fold the bitcast.
7401 DCI.AddToWorklist(V.getNode());
7402 }
7403 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7404 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7405 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7406}
7407
7408/// PerformInsertEltCombine - Target-specific dag combine xforms for
7409/// ISD::INSERT_VECTOR_ELT.
7410static SDValue PerformInsertEltCombine(SDNode *N,
7411 TargetLowering::DAGCombinerInfo &DCI) {
7412 // Bitcast an i64 load inserted into a vector to f64.
7413 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7414 EVT VT = N->getValueType(0);
7415 SDNode *Elt = N->getOperand(1).getNode();
7416 if (VT.getVectorElementType() != MVT::i64 ||
7417 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7418 return SDValue();
7419
7420 SelectionDAG &DAG = DCI.DAG;
7421 DebugLoc dl = N->getDebugLoc();
7422 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7423 VT.getVectorNumElements());
7424 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7425 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7426 // Make the DAGCombiner fold the bitcasts.
7427 DCI.AddToWorklist(Vec.getNode());
7428 DCI.AddToWorklist(V.getNode());
7429 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7430 Vec, V, N->getOperand(2));
7431 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007432}
7433
Bob Wilsonf20700c2010-10-27 20:38:28 +00007434/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7435/// ISD::VECTOR_SHUFFLE.
7436static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7437 // The LLVM shufflevector instruction does not require the shuffle mask
7438 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7439 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7440 // operands do not match the mask length, they are extended by concatenating
7441 // them with undef vectors. That is probably the right thing for other
7442 // targets, but for NEON it is better to concatenate two double-register
7443 // size vector operands into a single quad-register size vector. Do that
7444 // transformation here:
7445 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7446 // shuffle(concat(v1, v2), undef)
7447 SDValue Op0 = N->getOperand(0);
7448 SDValue Op1 = N->getOperand(1);
7449 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7450 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7451 Op0.getNumOperands() != 2 ||
7452 Op1.getNumOperands() != 2)
7453 return SDValue();
7454 SDValue Concat0Op1 = Op0.getOperand(1);
7455 SDValue Concat1Op1 = Op1.getOperand(1);
7456 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7457 Concat1Op1.getOpcode() != ISD::UNDEF)
7458 return SDValue();
7459 // Skip the transformation if any of the types are illegal.
7460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7461 EVT VT = N->getValueType(0);
7462 if (!TLI.isTypeLegal(VT) ||
7463 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7464 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7465 return SDValue();
7466
7467 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7468 Op0.getOperand(0), Op1.getOperand(0));
7469 // Translate the shuffle mask.
7470 SmallVector<int, 16> NewMask;
7471 unsigned NumElts = VT.getVectorNumElements();
7472 unsigned HalfElts = NumElts/2;
7473 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7474 for (unsigned n = 0; n < NumElts; ++n) {
7475 int MaskElt = SVN->getMaskElt(n);
7476 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007477 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007478 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007479 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007480 NewElt = HalfElts + MaskElt - NumElts;
7481 NewMask.push_back(NewElt);
7482 }
7483 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7484 DAG.getUNDEF(VT), NewMask.data());
7485}
7486
Bob Wilson1c3ef902011-02-07 17:43:21 +00007487/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7488/// NEON load/store intrinsics to merge base address updates.
7489static SDValue CombineBaseUpdate(SDNode *N,
7490 TargetLowering::DAGCombinerInfo &DCI) {
7491 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7492 return SDValue();
7493
7494 SelectionDAG &DAG = DCI.DAG;
7495 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7496 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7497 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7498 SDValue Addr = N->getOperand(AddrOpIdx);
7499
7500 // Search for a use of the address operand that is an increment.
7501 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7502 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7503 SDNode *User = *UI;
7504 if (User->getOpcode() != ISD::ADD ||
7505 UI.getUse().getResNo() != Addr.getResNo())
7506 continue;
7507
7508 // Check that the add is independent of the load/store. Otherwise, folding
7509 // it would create a cycle.
7510 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7511 continue;
7512
7513 // Find the new opcode for the updating load/store.
7514 bool isLoad = true;
7515 bool isLaneOp = false;
7516 unsigned NewOpc = 0;
7517 unsigned NumVecs = 0;
7518 if (isIntrinsic) {
7519 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7520 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007521 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007522 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7523 NumVecs = 1; break;
7524 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7525 NumVecs = 2; break;
7526 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7527 NumVecs = 3; break;
7528 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7529 NumVecs = 4; break;
7530 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7531 NumVecs = 2; isLaneOp = true; break;
7532 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7533 NumVecs = 3; isLaneOp = true; break;
7534 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7535 NumVecs = 4; isLaneOp = true; break;
7536 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7537 NumVecs = 1; isLoad = false; break;
7538 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7539 NumVecs = 2; isLoad = false; break;
7540 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7541 NumVecs = 3; isLoad = false; break;
7542 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7543 NumVecs = 4; isLoad = false; break;
7544 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7545 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7546 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7547 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7548 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7549 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7550 }
7551 } else {
7552 isLaneOp = true;
7553 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007554 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007555 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7556 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7557 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7558 }
7559 }
7560
7561 // Find the size of memory referenced by the load/store.
7562 EVT VecTy;
7563 if (isLoad)
7564 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007565 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007566 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7567 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7568 if (isLaneOp)
7569 NumBytes /= VecTy.getVectorNumElements();
7570
7571 // If the increment is a constant, it must match the memory ref size.
7572 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7573 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7574 uint64_t IncVal = CInc->getZExtValue();
7575 if (IncVal != NumBytes)
7576 continue;
7577 } else if (NumBytes >= 3 * 16) {
7578 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7579 // separate instructions that make it harder to use a non-constant update.
7580 continue;
7581 }
7582
7583 // Create the new updating load/store node.
7584 EVT Tys[6];
7585 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7586 unsigned n;
7587 for (n = 0; n < NumResultVecs; ++n)
7588 Tys[n] = VecTy;
7589 Tys[n++] = MVT::i32;
7590 Tys[n] = MVT::Other;
7591 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7592 SmallVector<SDValue, 8> Ops;
7593 Ops.push_back(N->getOperand(0)); // incoming chain
7594 Ops.push_back(N->getOperand(AddrOpIdx));
7595 Ops.push_back(Inc);
7596 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7597 Ops.push_back(N->getOperand(i));
7598 }
7599 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7600 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7601 Ops.data(), Ops.size(),
7602 MemInt->getMemoryVT(),
7603 MemInt->getMemOperand());
7604
7605 // Update the uses.
7606 std::vector<SDValue> NewResults;
7607 for (unsigned i = 0; i < NumResultVecs; ++i) {
7608 NewResults.push_back(SDValue(UpdN.getNode(), i));
7609 }
7610 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7611 DCI.CombineTo(N, NewResults);
7612 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7613
7614 break;
Owen Anderson76706012011-04-05 21:48:57 +00007615 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007616 return SDValue();
7617}
7618
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007619/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7620/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7621/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7622/// return true.
7623static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7624 SelectionDAG &DAG = DCI.DAG;
7625 EVT VT = N->getValueType(0);
7626 // vldN-dup instructions only support 64-bit vectors for N > 1.
7627 if (!VT.is64BitVector())
7628 return false;
7629
7630 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7631 SDNode *VLD = N->getOperand(0).getNode();
7632 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7633 return false;
7634 unsigned NumVecs = 0;
7635 unsigned NewOpc = 0;
7636 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7637 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7638 NumVecs = 2;
7639 NewOpc = ARMISD::VLD2DUP;
7640 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7641 NumVecs = 3;
7642 NewOpc = ARMISD::VLD3DUP;
7643 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7644 NumVecs = 4;
7645 NewOpc = ARMISD::VLD4DUP;
7646 } else {
7647 return false;
7648 }
7649
7650 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7651 // numbers match the load.
7652 unsigned VLDLaneNo =
7653 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7654 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7655 UI != UE; ++UI) {
7656 // Ignore uses of the chain result.
7657 if (UI.getUse().getResNo() == NumVecs)
7658 continue;
7659 SDNode *User = *UI;
7660 if (User->getOpcode() != ARMISD::VDUPLANE ||
7661 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7662 return false;
7663 }
7664
7665 // Create the vldN-dup node.
7666 EVT Tys[5];
7667 unsigned n;
7668 for (n = 0; n < NumVecs; ++n)
7669 Tys[n] = VT;
7670 Tys[n] = MVT::Other;
7671 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7672 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7673 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7674 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7675 Ops, 2, VLDMemInt->getMemoryVT(),
7676 VLDMemInt->getMemOperand());
7677
7678 // Update the uses.
7679 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7680 UI != UE; ++UI) {
7681 unsigned ResNo = UI.getUse().getResNo();
7682 // Ignore uses of the chain result.
7683 if (ResNo == NumVecs)
7684 continue;
7685 SDNode *User = *UI;
7686 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7687 }
7688
7689 // Now the vldN-lane intrinsic is dead except for its chain result.
7690 // Update uses of the chain.
7691 std::vector<SDValue> VLDDupResults;
7692 for (unsigned n = 0; n < NumVecs; ++n)
7693 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7694 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7695 DCI.CombineTo(VLD, VLDDupResults);
7696
7697 return true;
7698}
7699
Bob Wilson9e82bf12010-07-14 01:22:12 +00007700/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7701/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007702static SDValue PerformVDUPLANECombine(SDNode *N,
7703 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007704 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007705
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007706 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7707 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7708 if (CombineVLDDUP(N, DCI))
7709 return SDValue(N, 0);
7710
7711 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7712 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007713 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007714 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007715 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007716 return SDValue();
7717
7718 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7719 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7720 // The canonical VMOV for a zero vector uses a 32-bit element size.
7721 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7722 unsigned EltBits;
7723 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7724 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007725 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007726 if (EltSize > VT.getVectorElementType().getSizeInBits())
7727 return SDValue();
7728
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007729 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007730}
7731
Eric Christopherfa6f5912011-06-29 21:10:36 +00007732// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007733// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7734static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7735{
Chad Rosier118c9a02011-06-28 17:26:57 +00007736 integerPart cN;
7737 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007738 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7739 I != E; I++) {
7740 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7741 if (!C)
7742 return false;
7743
Eric Christopherfa6f5912011-06-29 21:10:36 +00007744 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007745 APFloat APF = C->getValueAPF();
7746 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7747 != APFloat::opOK || !isExact)
7748 return false;
7749
7750 c0 = (I == 0) ? cN : c0;
7751 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7752 return false;
7753 }
7754 C = c0;
7755 return true;
7756}
7757
7758/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7759/// can replace combinations of VMUL and VCVT (floating-point to integer)
7760/// when the VMUL has a constant operand that is a power of 2.
7761///
7762/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7763/// vmul.f32 d16, d17, d16
7764/// vcvt.s32.f32 d16, d16
7765/// becomes:
7766/// vcvt.s32.f32 d16, d16, #3
7767static SDValue PerformVCVTCombine(SDNode *N,
7768 TargetLowering::DAGCombinerInfo &DCI,
7769 const ARMSubtarget *Subtarget) {
7770 SelectionDAG &DAG = DCI.DAG;
7771 SDValue Op = N->getOperand(0);
7772
7773 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7774 Op.getOpcode() != ISD::FMUL)
7775 return SDValue();
7776
7777 uint64_t C;
7778 SDValue N0 = Op->getOperand(0);
7779 SDValue ConstVec = Op->getOperand(1);
7780 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7781
Eric Christopherfa6f5912011-06-29 21:10:36 +00007782 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007783 !isConstVecPow2(ConstVec, isSigned, C))
7784 return SDValue();
7785
7786 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7787 Intrinsic::arm_neon_vcvtfp2fxu;
7788 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7789 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007790 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007791 DAG.getConstant(Log2_64(C), MVT::i32));
7792}
7793
7794/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7795/// can replace combinations of VCVT (integer to floating-point) and VDIV
7796/// when the VDIV has a constant operand that is a power of 2.
7797///
7798/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7799/// vcvt.f32.s32 d16, d16
7800/// vdiv.f32 d16, d17, d16
7801/// becomes:
7802/// vcvt.f32.s32 d16, d16, #3
7803static SDValue PerformVDIVCombine(SDNode *N,
7804 TargetLowering::DAGCombinerInfo &DCI,
7805 const ARMSubtarget *Subtarget) {
7806 SelectionDAG &DAG = DCI.DAG;
7807 SDValue Op = N->getOperand(0);
7808 unsigned OpOpcode = Op.getNode()->getOpcode();
7809
7810 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7811 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7812 return SDValue();
7813
7814 uint64_t C;
7815 SDValue ConstVec = N->getOperand(1);
7816 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7817
7818 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7819 !isConstVecPow2(ConstVec, isSigned, C))
7820 return SDValue();
7821
Eric Christopherfa6f5912011-06-29 21:10:36 +00007822 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007823 Intrinsic::arm_neon_vcvtfxu2fp;
7824 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7825 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007826 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007827 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7828}
7829
7830/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007831/// operand of a vector shift operation, where all the elements of the
7832/// build_vector must have the same constant integer value.
7833static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7834 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007835 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007836 Op = Op.getOperand(0);
7837 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7838 APInt SplatBits, SplatUndef;
7839 unsigned SplatBitSize;
7840 bool HasAnyUndefs;
7841 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7842 HasAnyUndefs, ElementBits) ||
7843 SplatBitSize > ElementBits)
7844 return false;
7845 Cnt = SplatBits.getSExtValue();
7846 return true;
7847}
7848
7849/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7850/// operand of a vector shift left operation. That value must be in the range:
7851/// 0 <= Value < ElementBits for a left shift; or
7852/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007853static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007854 assert(VT.isVector() && "vector shift count is not a vector type");
7855 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7856 if (! getVShiftImm(Op, ElementBits, Cnt))
7857 return false;
7858 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
7859}
7860
7861/// isVShiftRImm - Check if this is a valid build_vector for the immediate
7862/// operand of a vector shift right operation. For a shift opcode, the value
7863/// is positive, but for an intrinsic the value count must be negative. The
7864/// absolute value must be in the range:
7865/// 1 <= |Value| <= ElementBits for a right shift; or
7866/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007867static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00007868 int64_t &Cnt) {
7869 assert(VT.isVector() && "vector shift count is not a vector type");
7870 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7871 if (! getVShiftImm(Op, ElementBits, Cnt))
7872 return false;
7873 if (isIntrinsic)
7874 Cnt = -Cnt;
7875 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
7876}
7877
7878/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
7879static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
7880 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
7881 switch (IntNo) {
7882 default:
7883 // Don't do anything for most intrinsics.
7884 break;
7885
7886 // Vector shifts: check for immediate versions and lower them.
7887 // Note: This is done during DAG combining instead of DAG legalizing because
7888 // the build_vectors for 64-bit vector element shift counts are generally
7889 // not legal, and it is hard to see their values after they get legalized to
7890 // loads from a constant pool.
7891 case Intrinsic::arm_neon_vshifts:
7892 case Intrinsic::arm_neon_vshiftu:
7893 case Intrinsic::arm_neon_vshiftls:
7894 case Intrinsic::arm_neon_vshiftlu:
7895 case Intrinsic::arm_neon_vshiftn:
7896 case Intrinsic::arm_neon_vrshifts:
7897 case Intrinsic::arm_neon_vrshiftu:
7898 case Intrinsic::arm_neon_vrshiftn:
7899 case Intrinsic::arm_neon_vqshifts:
7900 case Intrinsic::arm_neon_vqshiftu:
7901 case Intrinsic::arm_neon_vqshiftsu:
7902 case Intrinsic::arm_neon_vqshiftns:
7903 case Intrinsic::arm_neon_vqshiftnu:
7904 case Intrinsic::arm_neon_vqshiftnsu:
7905 case Intrinsic::arm_neon_vqrshiftns:
7906 case Intrinsic::arm_neon_vqrshiftnu:
7907 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00007908 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00007909 int64_t Cnt;
7910 unsigned VShiftOpc = 0;
7911
7912 switch (IntNo) {
7913 case Intrinsic::arm_neon_vshifts:
7914 case Intrinsic::arm_neon_vshiftu:
7915 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
7916 VShiftOpc = ARMISD::VSHL;
7917 break;
7918 }
7919 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
7920 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
7921 ARMISD::VSHRs : ARMISD::VSHRu);
7922 break;
7923 }
7924 return SDValue();
7925
7926 case Intrinsic::arm_neon_vshiftls:
7927 case Intrinsic::arm_neon_vshiftlu:
7928 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
7929 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007930 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007931
7932 case Intrinsic::arm_neon_vrshifts:
7933 case Intrinsic::arm_neon_vrshiftu:
7934 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
7935 break;
7936 return SDValue();
7937
7938 case Intrinsic::arm_neon_vqshifts:
7939 case Intrinsic::arm_neon_vqshiftu:
7940 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7941 break;
7942 return SDValue();
7943
7944 case Intrinsic::arm_neon_vqshiftsu:
7945 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
7946 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007947 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007948
7949 case Intrinsic::arm_neon_vshiftn:
7950 case Intrinsic::arm_neon_vrshiftn:
7951 case Intrinsic::arm_neon_vqshiftns:
7952 case Intrinsic::arm_neon_vqshiftnu:
7953 case Intrinsic::arm_neon_vqshiftnsu:
7954 case Intrinsic::arm_neon_vqrshiftns:
7955 case Intrinsic::arm_neon_vqrshiftnu:
7956 case Intrinsic::arm_neon_vqrshiftnsu:
7957 // Narrowing shifts require an immediate right shift.
7958 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
7959 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00007960 llvm_unreachable("invalid shift count for narrowing vector shift "
7961 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00007962
7963 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007964 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00007965 }
7966
7967 switch (IntNo) {
7968 case Intrinsic::arm_neon_vshifts:
7969 case Intrinsic::arm_neon_vshiftu:
7970 // Opcode already set above.
7971 break;
7972 case Intrinsic::arm_neon_vshiftls:
7973 case Intrinsic::arm_neon_vshiftlu:
7974 if (Cnt == VT.getVectorElementType().getSizeInBits())
7975 VShiftOpc = ARMISD::VSHLLi;
7976 else
7977 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
7978 ARMISD::VSHLLs : ARMISD::VSHLLu);
7979 break;
7980 case Intrinsic::arm_neon_vshiftn:
7981 VShiftOpc = ARMISD::VSHRN; break;
7982 case Intrinsic::arm_neon_vrshifts:
7983 VShiftOpc = ARMISD::VRSHRs; break;
7984 case Intrinsic::arm_neon_vrshiftu:
7985 VShiftOpc = ARMISD::VRSHRu; break;
7986 case Intrinsic::arm_neon_vrshiftn:
7987 VShiftOpc = ARMISD::VRSHRN; break;
7988 case Intrinsic::arm_neon_vqshifts:
7989 VShiftOpc = ARMISD::VQSHLs; break;
7990 case Intrinsic::arm_neon_vqshiftu:
7991 VShiftOpc = ARMISD::VQSHLu; break;
7992 case Intrinsic::arm_neon_vqshiftsu:
7993 VShiftOpc = ARMISD::VQSHLsu; break;
7994 case Intrinsic::arm_neon_vqshiftns:
7995 VShiftOpc = ARMISD::VQSHRNs; break;
7996 case Intrinsic::arm_neon_vqshiftnu:
7997 VShiftOpc = ARMISD::VQSHRNu; break;
7998 case Intrinsic::arm_neon_vqshiftnsu:
7999 VShiftOpc = ARMISD::VQSHRNsu; break;
8000 case Intrinsic::arm_neon_vqrshiftns:
8001 VShiftOpc = ARMISD::VQRSHRNs; break;
8002 case Intrinsic::arm_neon_vqrshiftnu:
8003 VShiftOpc = ARMISD::VQRSHRNu; break;
8004 case Intrinsic::arm_neon_vqrshiftnsu:
8005 VShiftOpc = ARMISD::VQRSHRNsu; break;
8006 }
8007
8008 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008009 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008010 }
8011
8012 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008013 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008014 int64_t Cnt;
8015 unsigned VShiftOpc = 0;
8016
8017 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8018 VShiftOpc = ARMISD::VSLI;
8019 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8020 VShiftOpc = ARMISD::VSRI;
8021 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008022 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008023 }
8024
8025 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8026 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008027 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008028 }
8029
8030 case Intrinsic::arm_neon_vqrshifts:
8031 case Intrinsic::arm_neon_vqrshiftu:
8032 // No immediate versions of these to check for.
8033 break;
8034 }
8035
8036 return SDValue();
8037}
8038
8039/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8040/// lowers them. As with the vector shift intrinsics, this is done during DAG
8041/// combining instead of DAG legalizing because the build_vectors for 64-bit
8042/// vector element shift counts are generally not legal, and it is hard to see
8043/// their values after they get legalized to loads from a constant pool.
8044static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8045 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008046 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008047 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8048 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8049 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8050 SDValue N1 = N->getOperand(1);
8051 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8052 SDValue N0 = N->getOperand(0);
8053 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8054 DAG.MaskedValueIsZero(N0.getOperand(0),
8055 APInt::getHighBitsSet(32, 16)))
8056 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8057 }
8058 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008059
8060 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008061 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8062 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008063 return SDValue();
8064
8065 assert(ST->hasNEON() && "unexpected vector shift");
8066 int64_t Cnt;
8067
8068 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008069 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008070
8071 case ISD::SHL:
8072 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8073 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008075 break;
8076
8077 case ISD::SRA:
8078 case ISD::SRL:
8079 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8080 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8081 ARMISD::VSHRs : ARMISD::VSHRu);
8082 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008084 }
8085 }
8086 return SDValue();
8087}
8088
8089/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8090/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8091static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8092 const ARMSubtarget *ST) {
8093 SDValue N0 = N->getOperand(0);
8094
8095 // Check for sign- and zero-extensions of vector extract operations of 8-
8096 // and 16-bit vector elements. NEON supports these directly. They are
8097 // handled during DAG combining because type legalization will promote them
8098 // to 32-bit types and it is messy to recognize the operations after that.
8099 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8100 SDValue Vec = N0.getOperand(0);
8101 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008102 EVT VT = N->getValueType(0);
8103 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008104 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8105
Owen Anderson825b72b2009-08-11 20:47:22 +00008106 if (VT == MVT::i32 &&
8107 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008108 TLI.isTypeLegal(Vec.getValueType()) &&
8109 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008110
8111 unsigned Opc = 0;
8112 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008113 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008114 case ISD::SIGN_EXTEND:
8115 Opc = ARMISD::VGETLANEs;
8116 break;
8117 case ISD::ZERO_EXTEND:
8118 case ISD::ANY_EXTEND:
8119 Opc = ARMISD::VGETLANEu;
8120 break;
8121 }
8122 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8123 }
8124 }
8125
8126 return SDValue();
8127}
8128
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008129/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8130/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8131static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8132 const ARMSubtarget *ST) {
8133 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008134 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008135 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8136 // a NaN; only do the transformation when it matches that behavior.
8137
8138 // For now only do this when using NEON for FP operations; if using VFP, it
8139 // is not obvious that the benefit outweighs the cost of switching to the
8140 // NEON pipeline.
8141 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8142 N->getValueType(0) != MVT::f32)
8143 return SDValue();
8144
8145 SDValue CondLHS = N->getOperand(0);
8146 SDValue CondRHS = N->getOperand(1);
8147 SDValue LHS = N->getOperand(2);
8148 SDValue RHS = N->getOperand(3);
8149 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8150
8151 unsigned Opcode = 0;
8152 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008153 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008154 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008155 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008156 IsReversed = true ; // x CC y ? y : x
8157 } else {
8158 return SDValue();
8159 }
8160
Bob Wilsone742bb52010-02-24 22:15:53 +00008161 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008162 switch (CC) {
8163 default: break;
8164 case ISD::SETOLT:
8165 case ISD::SETOLE:
8166 case ISD::SETLT:
8167 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008168 case ISD::SETULT:
8169 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008170 // If LHS is NaN, an ordered comparison will be false and the result will
8171 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8172 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8173 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8174 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8175 break;
8176 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8177 // will return -0, so vmin can only be used for unsafe math or if one of
8178 // the operands is known to be nonzero.
8179 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008180 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008181 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8182 break;
8183 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008184 break;
8185
8186 case ISD::SETOGT:
8187 case ISD::SETOGE:
8188 case ISD::SETGT:
8189 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008190 case ISD::SETUGT:
8191 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008192 // If LHS is NaN, an ordered comparison will be false and the result will
8193 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8194 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8195 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8196 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8197 break;
8198 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8199 // will return +0, so vmax can only be used for unsafe math or if one of
8200 // the operands is known to be nonzero.
8201 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008202 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008203 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8204 break;
8205 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008206 break;
8207 }
8208
8209 if (!Opcode)
8210 return SDValue();
8211 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8212}
8213
Evan Chenge721f5c2011-07-13 00:42:17 +00008214/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8215SDValue
8216ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8217 SDValue Cmp = N->getOperand(4);
8218 if (Cmp.getOpcode() != ARMISD::CMPZ)
8219 // Only looking at EQ and NE cases.
8220 return SDValue();
8221
8222 EVT VT = N->getValueType(0);
8223 DebugLoc dl = N->getDebugLoc();
8224 SDValue LHS = Cmp.getOperand(0);
8225 SDValue RHS = Cmp.getOperand(1);
8226 SDValue FalseVal = N->getOperand(0);
8227 SDValue TrueVal = N->getOperand(1);
8228 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008229 ARMCC::CondCodes CC =
8230 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008231
8232 // Simplify
8233 // mov r1, r0
8234 // cmp r1, x
8235 // mov r0, y
8236 // moveq r0, x
8237 // to
8238 // cmp r0, x
8239 // movne r0, y
8240 //
8241 // mov r1, r0
8242 // cmp r1, x
8243 // mov r0, x
8244 // movne r0, y
8245 // to
8246 // cmp r0, x
8247 // movne r0, y
8248 /// FIXME: Turn this into a target neutral optimization?
8249 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008250 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008251 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8252 N->getOperand(3), Cmp);
8253 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8254 SDValue ARMcc;
8255 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8256 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8257 N->getOperand(3), NewCmp);
8258 }
8259
8260 if (Res.getNode()) {
8261 APInt KnownZero, KnownOne;
8262 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
8263 DAG.ComputeMaskedBits(SDValue(N,0), Mask, KnownZero, KnownOne);
8264 // Capture demanded bits information that would be otherwise lost.
8265 if (KnownZero == 0xfffffffe)
8266 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8267 DAG.getValueType(MVT::i1));
8268 else if (KnownZero == 0xffffff00)
8269 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8270 DAG.getValueType(MVT::i8));
8271 else if (KnownZero == 0xffff0000)
8272 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8273 DAG.getValueType(MVT::i16));
8274 }
8275
8276 return Res;
8277}
8278
Dan Gohman475871a2008-07-27 21:46:04 +00008279SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008280 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008281 switch (N->getOpcode()) {
8282 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008283 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008284 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008285 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008286 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008287 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8288 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008289 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008290 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008291 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008292 case ISD::STORE: return PerformSTORECombine(N, DCI);
8293 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8294 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008295 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008296 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008297 case ISD::FP_TO_SINT:
8298 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8299 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008300 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008301 case ISD::SHL:
8302 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008303 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008304 case ISD::SIGN_EXTEND:
8305 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008306 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8307 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008308 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008309 case ARMISD::VLD2DUP:
8310 case ARMISD::VLD3DUP:
8311 case ARMISD::VLD4DUP:
8312 return CombineBaseUpdate(N, DCI);
8313 case ISD::INTRINSIC_VOID:
8314 case ISD::INTRINSIC_W_CHAIN:
8315 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8316 case Intrinsic::arm_neon_vld1:
8317 case Intrinsic::arm_neon_vld2:
8318 case Intrinsic::arm_neon_vld3:
8319 case Intrinsic::arm_neon_vld4:
8320 case Intrinsic::arm_neon_vld2lane:
8321 case Intrinsic::arm_neon_vld3lane:
8322 case Intrinsic::arm_neon_vld4lane:
8323 case Intrinsic::arm_neon_vst1:
8324 case Intrinsic::arm_neon_vst2:
8325 case Intrinsic::arm_neon_vst3:
8326 case Intrinsic::arm_neon_vst4:
8327 case Intrinsic::arm_neon_vst2lane:
8328 case Intrinsic::arm_neon_vst3lane:
8329 case Intrinsic::arm_neon_vst4lane:
8330 return CombineBaseUpdate(N, DCI);
8331 default: break;
8332 }
8333 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008334 }
Dan Gohman475871a2008-07-27 21:46:04 +00008335 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008336}
8337
Evan Cheng31959b12011-02-02 01:06:55 +00008338bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8339 EVT VT) const {
8340 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8341}
8342
Bill Wendlingaf566342009-08-15 21:21:19 +00008343bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008344 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008345 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008346
8347 switch (VT.getSimpleVT().SimpleTy) {
8348 default:
8349 return false;
8350 case MVT::i8:
8351 case MVT::i16:
8352 case MVT::i32:
8353 return true;
8354 // FIXME: VLD1 etc with standard alignment is legal.
8355 }
8356}
8357
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008358static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8359 unsigned AlignCheck) {
8360 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8361 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8362}
8363
8364EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8365 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008366 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008367 bool MemcpyStrSrc,
8368 MachineFunction &MF) const {
8369 const Function *F = MF.getFunction();
8370
8371 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008372 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008373 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8374 Subtarget->hasNEON()) {
8375 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8376 return MVT::v4i32;
8377 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8378 return MVT::v2i32;
8379 }
8380 }
8381
Lang Hames5207bf22011-11-08 18:56:23 +00008382 // Lowering to i32/i16 if the size permits.
8383 if (Size >= 4) {
8384 return MVT::i32;
8385 } else if (Size >= 2) {
8386 return MVT::i16;
8387 }
8388
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008389 // Let the target-independent logic figure it out.
8390 return MVT::Other;
8391}
8392
Evan Chenge6c835f2009-08-14 20:09:37 +00008393static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8394 if (V < 0)
8395 return false;
8396
8397 unsigned Scale = 1;
8398 switch (VT.getSimpleVT().SimpleTy) {
8399 default: return false;
8400 case MVT::i1:
8401 case MVT::i8:
8402 // Scale == 1;
8403 break;
8404 case MVT::i16:
8405 // Scale == 2;
8406 Scale = 2;
8407 break;
8408 case MVT::i32:
8409 // Scale == 4;
8410 Scale = 4;
8411 break;
8412 }
8413
8414 if ((V & (Scale - 1)) != 0)
8415 return false;
8416 V /= Scale;
8417 return V == (V & ((1LL << 5) - 1));
8418}
8419
8420static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8421 const ARMSubtarget *Subtarget) {
8422 bool isNeg = false;
8423 if (V < 0) {
8424 isNeg = true;
8425 V = - V;
8426 }
8427
8428 switch (VT.getSimpleVT().SimpleTy) {
8429 default: return false;
8430 case MVT::i1:
8431 case MVT::i8:
8432 case MVT::i16:
8433 case MVT::i32:
8434 // + imm12 or - imm8
8435 if (isNeg)
8436 return V == (V & ((1LL << 8) - 1));
8437 return V == (V & ((1LL << 12) - 1));
8438 case MVT::f32:
8439 case MVT::f64:
8440 // Same as ARM mode. FIXME: NEON?
8441 if (!Subtarget->hasVFP2())
8442 return false;
8443 if ((V & 3) != 0)
8444 return false;
8445 V >>= 2;
8446 return V == (V & ((1LL << 8) - 1));
8447 }
8448}
8449
Evan Chengb01fad62007-03-12 23:30:29 +00008450/// isLegalAddressImmediate - Return true if the integer value can be used
8451/// as the offset of the target addressing mode for load / store of the
8452/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008453static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008454 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008455 if (V == 0)
8456 return true;
8457
Evan Cheng65011532009-03-09 19:15:00 +00008458 if (!VT.isSimple())
8459 return false;
8460
Evan Chenge6c835f2009-08-14 20:09:37 +00008461 if (Subtarget->isThumb1Only())
8462 return isLegalT1AddressImmediate(V, VT);
8463 else if (Subtarget->isThumb2())
8464 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008465
Evan Chenge6c835f2009-08-14 20:09:37 +00008466 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008467 if (V < 0)
8468 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008469 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008470 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008471 case MVT::i1:
8472 case MVT::i8:
8473 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008474 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008475 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008476 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008477 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008478 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 case MVT::f32:
8480 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008481 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008482 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008483 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008484 return false;
8485 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008486 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008487 }
Evan Chenga8e29892007-01-19 07:51:42 +00008488}
8489
Evan Chenge6c835f2009-08-14 20:09:37 +00008490bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8491 EVT VT) const {
8492 int Scale = AM.Scale;
8493 if (Scale < 0)
8494 return false;
8495
8496 switch (VT.getSimpleVT().SimpleTy) {
8497 default: return false;
8498 case MVT::i1:
8499 case MVT::i8:
8500 case MVT::i16:
8501 case MVT::i32:
8502 if (Scale == 1)
8503 return true;
8504 // r + r << imm
8505 Scale = Scale & ~1;
8506 return Scale == 2 || Scale == 4 || Scale == 8;
8507 case MVT::i64:
8508 // r + r
8509 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8510 return true;
8511 return false;
8512 case MVT::isVoid:
8513 // Note, we allow "void" uses (basically, uses that aren't loads or
8514 // stores), because arm allows folding a scale into many arithmetic
8515 // operations. This should be made more precise and revisited later.
8516
8517 // Allow r << imm, but the imm has to be a multiple of two.
8518 if (Scale & 1) return false;
8519 return isPowerOf2_32(Scale);
8520 }
8521}
8522
Chris Lattner37caf8c2007-04-09 23:33:39 +00008523/// isLegalAddressingMode - Return true if the addressing mode represented
8524/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008525bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008526 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008527 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008528 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008529 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008530
Chris Lattner37caf8c2007-04-09 23:33:39 +00008531 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008532 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008533 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008534
Chris Lattner37caf8c2007-04-09 23:33:39 +00008535 switch (AM.Scale) {
8536 case 0: // no scale reg, must be "r+i" or "r", or "i".
8537 break;
8538 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008539 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008540 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008541 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008542 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008543 // ARM doesn't support any R+R*scale+imm addr modes.
8544 if (AM.BaseOffs)
8545 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008546
Bob Wilson2c7dab12009-04-08 17:55:28 +00008547 if (!VT.isSimple())
8548 return false;
8549
Evan Chenge6c835f2009-08-14 20:09:37 +00008550 if (Subtarget->isThumb2())
8551 return isLegalT2ScaledAddressingMode(AM, VT);
8552
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008553 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008554 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008555 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008556 case MVT::i1:
8557 case MVT::i8:
8558 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008559 if (Scale < 0) Scale = -Scale;
8560 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008561 return true;
8562 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008563 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008564 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008565 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008566 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008567 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008568 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008569 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008570
Owen Anderson825b72b2009-08-11 20:47:22 +00008571 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008572 // Note, we allow "void" uses (basically, uses that aren't loads or
8573 // stores), because arm allows folding a scale into many arithmetic
8574 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008575
Chris Lattner37caf8c2007-04-09 23:33:39 +00008576 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008577 if (Scale & 1) return false;
8578 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008579 }
Evan Chengb01fad62007-03-12 23:30:29 +00008580 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008581 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008582}
8583
Evan Cheng77e47512009-11-11 19:05:52 +00008584/// isLegalICmpImmediate - Return true if the specified immediate is legal
8585/// icmp immediate, that is the target has icmp instructions which can compare
8586/// a register against the immediate without having to materialize the
8587/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008588bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Evan Cheng77e47512009-11-11 19:05:52 +00008589 if (!Subtarget->isThumb())
8590 return ARM_AM::getSOImmVal(Imm) != -1;
8591 if (Subtarget->isThumb2())
Jim Grosbach4725ca72010-09-08 03:54:02 +00008592 return ARM_AM::getT2SOImmVal(Imm) != -1;
Evan Cheng06b53c02009-11-12 07:13:11 +00008593 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008594}
8595
Dan Gohmancca82142011-05-03 00:46:49 +00008596/// isLegalAddImmediate - Return true if the specified immediate is legal
8597/// add immediate, that is the target has add instructions which can add
8598/// a register with the immediate without having to materialize the
8599/// immediate into a register.
8600bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8601 return ARM_AM::getSOImmVal(Imm) != -1;
8602}
8603
Owen Andersone50ed302009-08-10 22:56:29 +00008604static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008605 bool isSEXTLoad, SDValue &Base,
8606 SDValue &Offset, bool &isInc,
8607 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008608 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8609 return false;
8610
Owen Anderson825b72b2009-08-11 20:47:22 +00008611 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008612 // AddressingMode 3
8613 Base = Ptr->getOperand(0);
8614 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008615 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008616 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008617 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008618 isInc = false;
8619 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8620 return true;
8621 }
8622 }
8623 isInc = (Ptr->getOpcode() == ISD::ADD);
8624 Offset = Ptr->getOperand(1);
8625 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008626 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008627 // AddressingMode 2
8628 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008629 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008630 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008631 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008632 isInc = false;
8633 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8634 Base = Ptr->getOperand(0);
8635 return true;
8636 }
8637 }
8638
8639 if (Ptr->getOpcode() == ISD::ADD) {
8640 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008641 ARM_AM::ShiftOpc ShOpcVal=
8642 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008643 if (ShOpcVal != ARM_AM::no_shift) {
8644 Base = Ptr->getOperand(1);
8645 Offset = Ptr->getOperand(0);
8646 } else {
8647 Base = Ptr->getOperand(0);
8648 Offset = Ptr->getOperand(1);
8649 }
8650 return true;
8651 }
8652
8653 isInc = (Ptr->getOpcode() == ISD::ADD);
8654 Base = Ptr->getOperand(0);
8655 Offset = Ptr->getOperand(1);
8656 return true;
8657 }
8658
Jim Grosbache5165492009-11-09 00:11:35 +00008659 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008660 return false;
8661}
8662
Owen Andersone50ed302009-08-10 22:56:29 +00008663static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008664 bool isSEXTLoad, SDValue &Base,
8665 SDValue &Offset, bool &isInc,
8666 SelectionDAG &DAG) {
8667 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8668 return false;
8669
8670 Base = Ptr->getOperand(0);
8671 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8672 int RHSC = (int)RHS->getZExtValue();
8673 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8674 assert(Ptr->getOpcode() == ISD::ADD);
8675 isInc = false;
8676 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8677 return true;
8678 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8679 isInc = Ptr->getOpcode() == ISD::ADD;
8680 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8681 return true;
8682 }
8683 }
8684
8685 return false;
8686}
8687
Evan Chenga8e29892007-01-19 07:51:42 +00008688/// getPreIndexedAddressParts - returns true by value, base pointer and
8689/// offset pointer and addressing mode by reference if the node's address
8690/// can be legally represented as pre-indexed load / store address.
8691bool
Dan Gohman475871a2008-07-27 21:46:04 +00008692ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8693 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008694 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008695 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008696 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008697 return false;
8698
Owen Andersone50ed302009-08-10 22:56:29 +00008699 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008700 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008701 bool isSEXTLoad = false;
8702 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8703 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008704 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008705 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8706 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8707 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008708 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008709 } else
8710 return false;
8711
8712 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008713 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008714 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008715 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8716 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008717 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008718 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008719 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008720 if (!isLegal)
8721 return false;
8722
8723 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8724 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008725}
8726
8727/// getPostIndexedAddressParts - returns true by value, base pointer and
8728/// offset pointer and addressing mode by reference if this node can be
8729/// combined with a load / store to form a post-indexed load / store.
8730bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008731 SDValue &Base,
8732 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008733 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008734 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008735 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008736 return false;
8737
Owen Andersone50ed302009-08-10 22:56:29 +00008738 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008739 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008740 bool isSEXTLoad = false;
8741 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008742 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008743 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008744 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8745 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008746 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008747 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008748 } else
8749 return false;
8750
8751 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008752 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008753 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008754 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008755 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008756 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008757 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8758 isInc, DAG);
8759 if (!isLegal)
8760 return false;
8761
Evan Cheng28dad2a2010-05-18 21:31:17 +00008762 if (Ptr != Base) {
8763 // Swap base ptr and offset to catch more post-index load / store when
8764 // it's legal. In Thumb2 mode, offset must be an immediate.
8765 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8766 !Subtarget->isThumb2())
8767 std::swap(Base, Offset);
8768
8769 // Post-indexed load / store update the base pointer.
8770 if (Ptr != Base)
8771 return false;
8772 }
8773
Evan Chenge88d5ce2009-07-02 07:28:31 +00008774 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8775 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008776}
8777
Dan Gohman475871a2008-07-27 21:46:04 +00008778void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008779 const APInt &Mask,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008780 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008781 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008782 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008783 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008784 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008785 switch (Op.getOpcode()) {
8786 default: break;
8787 case ARMISD::CMOV: {
8788 // Bits are known zero/one if known on the LHS and RHS.
Dan Gohmanea859be2007-06-22 14:59:07 +00008789 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008790 if (KnownZero == 0 && KnownOne == 0) return;
8791
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008792 APInt KnownZeroRHS, KnownOneRHS;
Dan Gohmanea859be2007-06-22 14:59:07 +00008793 DAG.ComputeMaskedBits(Op.getOperand(1), Mask,
8794 KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008795 KnownZero &= KnownZeroRHS;
8796 KnownOne &= KnownOneRHS;
8797 return;
8798 }
8799 }
8800}
8801
8802//===----------------------------------------------------------------------===//
8803// ARM Inline Assembly Support
8804//===----------------------------------------------------------------------===//
8805
Evan Cheng55d42002011-01-08 01:24:27 +00008806bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8807 // Looking for "rev" which is V6+.
8808 if (!Subtarget->hasV6Ops())
8809 return false;
8810
8811 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8812 std::string AsmStr = IA->getAsmString();
8813 SmallVector<StringRef, 4> AsmPieces;
8814 SplitString(AsmStr, AsmPieces, ";\n");
8815
8816 switch (AsmPieces.size()) {
8817 default: return false;
8818 case 1:
8819 AsmStr = AsmPieces[0];
8820 AsmPieces.clear();
8821 SplitString(AsmStr, AsmPieces, " \t,");
8822
8823 // rev $0, $1
8824 if (AsmPieces.size() == 3 &&
8825 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8826 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008827 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008828 if (Ty && Ty->getBitWidth() == 32)
8829 return IntrinsicLowering::LowerToByteSwap(CI);
8830 }
8831 break;
8832 }
8833
8834 return false;
8835}
8836
Evan Chenga8e29892007-01-19 07:51:42 +00008837/// getConstraintType - Given a constraint letter, return the type of
8838/// constraint it is for this target.
8839ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008840ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8841 if (Constraint.size() == 1) {
8842 switch (Constraint[0]) {
8843 default: break;
8844 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008845 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008846 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008847 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008848 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008849 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008850 // An address with a single base register. Due to the way we
8851 // currently handle addresses it is the same as an 'r' memory constraint.
8852 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008853 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008854 } else if (Constraint.size() == 2) {
8855 switch (Constraint[0]) {
8856 default: break;
8857 // All 'U+' constraints are addresses.
8858 case 'U': return C_Memory;
8859 }
Evan Chenga8e29892007-01-19 07:51:42 +00008860 }
Chris Lattner4234f572007-03-25 02:14:49 +00008861 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00008862}
8863
John Thompson44ab89e2010-10-29 17:29:13 +00008864/// Examine constraint type and operand type and determine a weight value.
8865/// This object must already have been set up with the operand type
8866/// and the current alternative constraint selected.
8867TargetLowering::ConstraintWeight
8868ARMTargetLowering::getSingleConstraintMatchWeight(
8869 AsmOperandInfo &info, const char *constraint) const {
8870 ConstraintWeight weight = CW_Invalid;
8871 Value *CallOperandVal = info.CallOperandVal;
8872 // If we don't have a value, we can't do a match,
8873 // but allow it at the lowest weight.
8874 if (CallOperandVal == NULL)
8875 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008876 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00008877 // Look at the constraint type.
8878 switch (*constraint) {
8879 default:
8880 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
8881 break;
8882 case 'l':
8883 if (type->isIntegerTy()) {
8884 if (Subtarget->isThumb())
8885 weight = CW_SpecificReg;
8886 else
8887 weight = CW_Register;
8888 }
8889 break;
8890 case 'w':
8891 if (type->isFloatingPointTy())
8892 weight = CW_Register;
8893 break;
8894 }
8895 return weight;
8896}
8897
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008898typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
8899RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00008900ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008901 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00008902 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008903 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00008904 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00008905 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00008906 if (Subtarget->isThumb())
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008907 return RCPair(0U, ARM::tGPRRegisterClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +00008908 else
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008909 return RCPair(0U, ARM::GPRRegisterClass);
Eric Christopher73744df2011-06-30 23:23:01 +00008910 case 'h': // High regs or no regs.
8911 if (Subtarget->isThumb())
Andrew Trick3af7a672011-09-20 03:06:13 +00008912 return RCPair(0U, ARM::hGPRRegisterClass);
Eric Christopher1070f822011-07-01 00:19:27 +00008913 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008914 case 'r':
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008915 return RCPair(0U, ARM::GPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008916 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00008917 if (VT == MVT::f32)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008918 return RCPair(0U, ARM::SPRRegisterClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00008919 if (VT.getSizeInBits() == 64)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008920 return RCPair(0U, ARM::DPRRegisterClass);
Evan Chengd831cda2009-12-08 23:06:22 +00008921 if (VT.getSizeInBits() == 128)
Eric Christopher35e6d4d2011-06-30 23:50:52 +00008922 return RCPair(0U, ARM::QPRRegisterClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008923 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008924 case 'x':
8925 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008926 return RCPair(0U, ARM::SPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008927 if (VT.getSizeInBits() == 64)
Andrew Trick3af7a672011-09-20 03:06:13 +00008928 return RCPair(0U, ARM::DPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008929 if (VT.getSizeInBits() == 128)
Andrew Trick3af7a672011-09-20 03:06:13 +00008930 return RCPair(0U, ARM::QPR_8RegisterClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00008931 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008932 case 't':
8933 if (VT == MVT::f32)
Andrew Trick3af7a672011-09-20 03:06:13 +00008934 return RCPair(0U, ARM::SPRRegisterClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008935 break;
Evan Chenga8e29892007-01-19 07:51:42 +00008936 }
8937 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008938 if (StringRef("{cc}").equals_lower(Constraint))
Jakob Stoklund Olesen0d8ba332010-06-18 16:49:33 +00008939 return std::make_pair(unsigned(ARM::CPSR), ARM::CCRRegisterClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00008940
Evan Chenga8e29892007-01-19 07:51:42 +00008941 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8942}
8943
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008944/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8945/// vector. If it is invalid, don't add anything to Ops.
8946void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00008947 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008948 std::vector<SDValue>&Ops,
8949 SelectionDAG &DAG) const {
8950 SDValue Result(0, 0);
8951
Eric Christopher100c8332011-06-02 23:16:42 +00008952 // Currently only support length 1 constraints.
8953 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00008954
Eric Christopher100c8332011-06-02 23:16:42 +00008955 char ConstraintLetter = Constraint[0];
8956 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008957 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00008958 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008959 case 'I': case 'J': case 'K': case 'L':
8960 case 'M': case 'N': case 'O':
8961 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
8962 if (!C)
8963 return;
8964
8965 int64_t CVal64 = C->getSExtValue();
8966 int CVal = (int) CVal64;
8967 // None of these constraints allow values larger than 32 bits. Check
8968 // that the value fits in an int.
8969 if (CVal != CVal64)
8970 return;
8971
Eric Christopher100c8332011-06-02 23:16:42 +00008972 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00008973 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00008974 // Constant suitable for movw, must be between 0 and
8975 // 65535.
8976 if (Subtarget->hasV6T2Ops())
8977 if (CVal >= 0 && CVal <= 65535)
8978 break;
8979 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008980 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00008981 if (Subtarget->isThumb1Only()) {
8982 // This must be a constant between 0 and 255, for ADD
8983 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008984 if (CVal >= 0 && CVal <= 255)
8985 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00008986 } else if (Subtarget->isThumb2()) {
8987 // A constant that can be used as an immediate value in a
8988 // data-processing instruction.
8989 if (ARM_AM::getT2SOImmVal(CVal) != -1)
8990 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00008991 } else {
8992 // A constant that can be used as an immediate value in a
8993 // data-processing instruction.
8994 if (ARM_AM::getSOImmVal(CVal) != -1)
8995 break;
8996 }
8997 return;
8998
8999 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009000 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009001 // This must be a constant between -255 and -1, for negated ADD
9002 // immediates. This can be used in GCC with an "n" modifier that
9003 // prints the negated value, for use with SUB instructions. It is
9004 // not useful otherwise but is implemented for compatibility.
9005 if (CVal >= -255 && CVal <= -1)
9006 break;
9007 } else {
9008 // This must be a constant between -4095 and 4095. It is not clear
9009 // what this constraint is intended for. Implemented for
9010 // compatibility with GCC.
9011 if (CVal >= -4095 && CVal <= 4095)
9012 break;
9013 }
9014 return;
9015
9016 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009017 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009018 // A 32-bit value where only one byte has a nonzero value. Exclude
9019 // zero to match GCC. This constraint is used by GCC internally for
9020 // constants that can be loaded with a move/shift combination.
9021 // It is not useful otherwise but is implemented for compatibility.
9022 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9023 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009024 } else if (Subtarget->isThumb2()) {
9025 // A constant whose bitwise inverse can be used as an immediate
9026 // value in a data-processing instruction. This can be used in GCC
9027 // with a "B" modifier that prints the inverted value, for use with
9028 // BIC and MVN instructions. It is not useful otherwise but is
9029 // implemented for compatibility.
9030 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9031 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009032 } else {
9033 // A constant whose bitwise inverse can be used as an immediate
9034 // value in a data-processing instruction. This can be used in GCC
9035 // with a "B" modifier that prints the inverted value, for use with
9036 // BIC and MVN instructions. It is not useful otherwise but is
9037 // implemented for compatibility.
9038 if (ARM_AM::getSOImmVal(~CVal) != -1)
9039 break;
9040 }
9041 return;
9042
9043 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009044 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009045 // This must be a constant between -7 and 7,
9046 // for 3-operand ADD/SUB immediate instructions.
9047 if (CVal >= -7 && CVal < 7)
9048 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009049 } else if (Subtarget->isThumb2()) {
9050 // A constant whose negation can be used as an immediate value in a
9051 // data-processing instruction. This can be used in GCC with an "n"
9052 // modifier that prints the negated value, for use with SUB
9053 // instructions. It is not useful otherwise but is implemented for
9054 // compatibility.
9055 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9056 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009057 } else {
9058 // A constant whose negation can be used as an immediate value in a
9059 // data-processing instruction. This can be used in GCC with an "n"
9060 // modifier that prints the negated value, for use with SUB
9061 // instructions. It is not useful otherwise but is implemented for
9062 // compatibility.
9063 if (ARM_AM::getSOImmVal(-CVal) != -1)
9064 break;
9065 }
9066 return;
9067
9068 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009069 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009070 // This must be a multiple of 4 between 0 and 1020, for
9071 // ADD sp + immediate.
9072 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9073 break;
9074 } else {
9075 // A power of two or a constant between 0 and 32. This is used in
9076 // GCC for the shift amount on shifted register operands, but it is
9077 // useful in general for any shift amounts.
9078 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9079 break;
9080 }
9081 return;
9082
9083 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009084 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009085 // This must be a constant between 0 and 31, for shift amounts.
9086 if (CVal >= 0 && CVal <= 31)
9087 break;
9088 }
9089 return;
9090
9091 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009092 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009093 // This must be a multiple of 4 between -508 and 508, for
9094 // ADD/SUB sp = sp + immediate.
9095 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9096 break;
9097 }
9098 return;
9099 }
9100 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9101 break;
9102 }
9103
9104 if (Result.getNode()) {
9105 Ops.push_back(Result);
9106 return;
9107 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009108 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009109}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009110
9111bool
9112ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9113 // The ARM target isn't yet aware of offsets.
9114 return false;
9115}
Evan Cheng39382422009-10-28 01:44:26 +00009116
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009117bool ARM::isBitFieldInvertedMask(unsigned v) {
9118 if (v == 0xffffffff)
9119 return 0;
9120 // there can be 1's on either or both "outsides", all the "inside"
9121 // bits must be 0's
9122 unsigned int lsb = 0, msb = 31;
9123 while (v & (1 << msb)) --msb;
9124 while (v & (1 << lsb)) ++lsb;
9125 for (unsigned int i = lsb; i <= msb; ++i) {
9126 if (v & (1 << i))
9127 return 0;
9128 }
9129 return 1;
9130}
9131
Evan Cheng39382422009-10-28 01:44:26 +00009132/// isFPImmLegal - Returns true if the target can instruction select the
9133/// specified FP immediate natively. If false, the legalizer will
9134/// materialize the FP immediate as a load from a constant pool.
9135bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9136 if (!Subtarget->hasVFP3())
9137 return false;
9138 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009139 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009140 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009141 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009142 return false;
9143}
Bob Wilson65ffec42010-09-21 17:56:22 +00009144
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009145/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009146/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9147/// specified in the intrinsic calls.
9148bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9149 const CallInst &I,
9150 unsigned Intrinsic) const {
9151 switch (Intrinsic) {
9152 case Intrinsic::arm_neon_vld1:
9153 case Intrinsic::arm_neon_vld2:
9154 case Intrinsic::arm_neon_vld3:
9155 case Intrinsic::arm_neon_vld4:
9156 case Intrinsic::arm_neon_vld2lane:
9157 case Intrinsic::arm_neon_vld3lane:
9158 case Intrinsic::arm_neon_vld4lane: {
9159 Info.opc = ISD::INTRINSIC_W_CHAIN;
9160 // Conservatively set memVT to the entire set of vectors loaded.
9161 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9162 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9163 Info.ptrVal = I.getArgOperand(0);
9164 Info.offset = 0;
9165 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9166 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9167 Info.vol = false; // volatile loads with NEON intrinsics not supported
9168 Info.readMem = true;
9169 Info.writeMem = false;
9170 return true;
9171 }
9172 case Intrinsic::arm_neon_vst1:
9173 case Intrinsic::arm_neon_vst2:
9174 case Intrinsic::arm_neon_vst3:
9175 case Intrinsic::arm_neon_vst4:
9176 case Intrinsic::arm_neon_vst2lane:
9177 case Intrinsic::arm_neon_vst3lane:
9178 case Intrinsic::arm_neon_vst4lane: {
9179 Info.opc = ISD::INTRINSIC_VOID;
9180 // Conservatively set memVT to the entire set of vectors stored.
9181 unsigned NumElts = 0;
9182 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009183 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009184 if (!ArgTy->isVectorTy())
9185 break;
9186 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9187 }
9188 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9189 Info.ptrVal = I.getArgOperand(0);
9190 Info.offset = 0;
9191 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9192 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9193 Info.vol = false; // volatile stores with NEON intrinsics not supported
9194 Info.readMem = false;
9195 Info.writeMem = true;
9196 return true;
9197 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009198 case Intrinsic::arm_strexd: {
9199 Info.opc = ISD::INTRINSIC_W_CHAIN;
9200 Info.memVT = MVT::i64;
9201 Info.ptrVal = I.getArgOperand(2);
9202 Info.offset = 0;
9203 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009204 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009205 Info.readMem = false;
9206 Info.writeMem = true;
9207 return true;
9208 }
9209 case Intrinsic::arm_ldrexd: {
9210 Info.opc = ISD::INTRINSIC_W_CHAIN;
9211 Info.memVT = MVT::i64;
9212 Info.ptrVal = I.getArgOperand(0);
9213 Info.offset = 0;
9214 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009215 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009216 Info.readMem = true;
9217 Info.writeMem = false;
9218 return true;
9219 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009220 default:
9221 break;
9222 }
9223
9224 return false;
9225}