blob: bb2116f774318595900c4b1053b464d239e78555 [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 "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000031#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000032#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000033#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000034#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000035#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/MachineBasicBlock.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000043#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000045#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000046#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000047#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000050#include "llvm/Support/raw_ostream.h"
Evan Chenga8e29892007-01-19 07:51:42 +000051using namespace llvm;
52
Dale Johannesen51e28e62010-06-03 21:09:53 +000053STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000054STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren763a75d2012-06-01 02:44:42 +000055STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
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) {
Craig Topper420761a2012-04-20 07:30:17 +0000157 addRegisterClass(VT, &ARM::DPRRegClass);
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) {
Craig Topper420761a2012-04-20 07:30:17 +0000162 addRegisterClass(VT, &ARM::QPRRegClass);
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())
Craig Topper420761a2012-04-20 07:30:17 +0000435 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000436 else
Craig Topper420761a2012-04-20 07:30:17 +0000437 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000438 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
439 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000440 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000441 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000442 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
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);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000511
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000512 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
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +0000574 // ARM and Thumb2 support UMLAL/SMLAL.
575 if (!Subtarget->isThumb1Only())
576 setTargetDAGCombine(ISD::ADDC);
577
578
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000579 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000580
581 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000582 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000583
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000584 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000586
Evan Chenga8e29892007-01-19 07:51:42 +0000587 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000588 if (!Subtarget->isThumb1Only()) {
589 for (unsigned im = (unsigned)ISD::PRE_INC;
590 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setIndexedLoadAction(im, MVT::i1, Legal);
592 setIndexedLoadAction(im, MVT::i8, Legal);
593 setIndexedLoadAction(im, MVT::i16, Legal);
594 setIndexedLoadAction(im, MVT::i32, Legal);
595 setIndexedStoreAction(im, MVT::i1, Legal);
596 setIndexedStoreAction(im, MVT::i8, Legal);
597 setIndexedStoreAction(im, MVT::i16, Legal);
598 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000599 }
Evan Chenga8e29892007-01-19 07:51:42 +0000600 }
601
602 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000603 setOperationAction(ISD::MUL, MVT::i64, Expand);
604 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000605 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
607 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000608 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000609 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
610 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000611 setOperationAction(ISD::MULHS, MVT::i32, Expand);
612
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000613 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000614 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000615 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 setOperationAction(ISD::SRL, MVT::i64, Custom);
617 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000618
Evan Cheng342e3162011-08-30 01:34:54 +0000619 if (!Subtarget->isThumb1Only()) {
620 // FIXME: We should do this for Thumb1 as well.
621 setOperationAction(ISD::ADDC, MVT::i32, Custom);
622 setOperationAction(ISD::ADDE, MVT::i32, Custom);
623 setOperationAction(ISD::SUBC, MVT::i32, Custom);
624 setOperationAction(ISD::SUBE, MVT::i32, Custom);
625 }
626
Evan Chenga8e29892007-01-19 07:51:42 +0000627 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000629 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000631 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000633
Chandler Carruth63974b22011-12-13 01:56:10 +0000634 // These just redirect to CTTZ and CTLZ on ARM.
635 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
636 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
637
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000638 // Only ARMv6 has BSWAP.
639 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000641
Evan Chenga8e29892007-01-19 07:51:42 +0000642 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000643 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000644 // v7M has a hardware divider
645 setOperationAction(ISD::SDIV, MVT::i32, Expand);
646 setOperationAction(ISD::UDIV, MVT::i32, Expand);
647 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::SREM, MVT::i32, Expand);
649 setOperationAction(ISD::UREM, MVT::i32, Expand);
650 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
651 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
654 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
655 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
656 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000657 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000658
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000659 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000660
Evan Chenga8e29892007-01-19 07:51:42 +0000661 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::VASTART, MVT::Other, Custom);
663 setOperationAction(ISD::VAARG, MVT::Other, Expand);
664 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
665 setOperationAction(ISD::VAEND, MVT::Other, Expand);
666 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
667 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000668
669 if (!Subtarget->isTargetDarwin()) {
670 // Non-Darwin platforms may return values in these registers via the
671 // personality function.
672 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
673 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
674 setExceptionPointerRegister(ARM::R0);
675 setExceptionSelectorRegister(ARM::R1);
676 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000677
Evan Cheng3a1588a2010-04-15 22:20:34 +0000678 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000679 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
680 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000681 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000682 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000683 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000684 // membarrier needs custom lowering; the rest are legal and handled
685 // normally.
686 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000687 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000688 // Custom lowering for 64-bit ops
689 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
690 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
691 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
692 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
693 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
694 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000695 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000696 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
697 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000698 } else {
699 // Set them all for expansion, which will force libcalls.
700 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000701 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000703 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000705 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000706 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000707 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000708 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000709 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000710 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000711 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000712 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000713 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000714 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
715 // Unordered/Monotonic case.
716 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
717 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000718 // Since the libcalls include locking, fold in the fences
719 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000720 }
Evan Chenga8e29892007-01-19 07:51:42 +0000721
Evan Cheng416941d2010-11-04 05:19:35 +0000722 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000723
Eli Friedmana2c6f452010-06-26 04:36:50 +0000724 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
725 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
727 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000728 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000730
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000731 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
732 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000733 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
734 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000736 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
737 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000738
739 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000741 if (Subtarget->isTargetDarwin()) {
742 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
743 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000744 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000745 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000746
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::SETCC, MVT::i32, Expand);
748 setOperationAction(ISD::SETCC, MVT::f32, Expand);
749 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000750 setOperationAction(ISD::SELECT, MVT::i32, Custom);
751 setOperationAction(ISD::SELECT, MVT::f32, Custom);
752 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
754 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
755 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000756
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
758 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
759 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
760 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
761 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000762
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000763 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setOperationAction(ISD::FSIN, MVT::f64, Expand);
765 setOperationAction(ISD::FSIN, MVT::f32, Expand);
766 setOperationAction(ISD::FCOS, MVT::f32, Expand);
767 setOperationAction(ISD::FCOS, MVT::f64, Expand);
768 setOperationAction(ISD::FREM, MVT::f64, Expand);
769 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000770 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
771 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
773 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000774 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::FPOW, MVT::f64, Expand);
776 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000777
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000778 if (!Subtarget->hasVFP4()) {
779 setOperationAction(ISD::FMA, MVT::f64, Expand);
780 setOperationAction(ISD::FMA, MVT::f32, Expand);
781 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000782
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000783 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000784 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000785 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
786 if (Subtarget->hasVFP2()) {
787 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
788 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
789 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
790 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
791 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000792 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000793 if (!Subtarget->hasFP16()) {
794 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
795 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000796 }
Evan Cheng110cf482008-04-01 01:50:16 +0000797 }
Evan Chenga8e29892007-01-19 07:51:42 +0000798
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000799 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000800 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000801 setTargetDAGCombine(ISD::ADD);
802 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000803 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000804
Evan Chengc892aeb2012-02-23 01:19:06 +0000805 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000806 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000807 setTargetDAGCombine(ISD::OR);
808 setTargetDAGCombine(ISD::XOR);
809 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000810
Evan Cheng5fb468a2012-02-23 02:58:19 +0000811 if (Subtarget->hasV6Ops())
812 setTargetDAGCombine(ISD::SRL);
813
Evan Chenga8e29892007-01-19 07:51:42 +0000814 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000815
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000816 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
817 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000818 setSchedulingPreference(Sched::RegPressure);
819 else
820 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000821
Evan Cheng05219282011-01-06 06:52:41 +0000822 //// temporary - rewrite interface to use type
823 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000824 maxStoresPerMemset = 16;
825 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000826
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000827 // On ARM arguments smaller than 4 bytes are extended, so all arguments
828 // are at least 4 bytes aligned.
829 setMinStackArgumentAlignment(4);
830
Evan Chengfff606d2010-09-24 19:07:23 +0000831 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000832
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000833 // Prefer likely predicted branches to selects on out-of-order cores.
834 predictableSelectIsExpensive = Subtarget->isCortexA9();
835
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000836 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000837}
838
Andrew Trick32cec0a2011-01-19 02:35:27 +0000839// FIXME: It might make sense to define the representative register class as the
840// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
841// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
842// SPR's representative would be DPR_VFP2. This should work well if register
843// pressure tracking were modified such that a register use would increment the
844// pressure of the register class's representative and all of it's super
845// classes' representatives transitively. We have not implemented this because
846// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000847// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000848// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000849std::pair<const TargetRegisterClass*, uint8_t>
850ARMTargetLowering::findRepresentativeClass(EVT VT) const{
851 const TargetRegisterClass *RRC = 0;
852 uint8_t Cost = 1;
853 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000854 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000855 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000856 // Use DPR as representative register class for all floating point
857 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
858 // the cost is 1 for both f32 and f64.
859 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000860 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000861 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000862 // When NEON is used for SP, only half of the register file is available
863 // because operations that define both SP and DP results will be constrained
864 // to the VFP2 class (D0-D15). We currently model this constraint prior to
865 // coalescing by double-counting the SP regs. See the FIXME above.
866 if (Subtarget->useNEONForSinglePrecisionFP())
867 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
870 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000871 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000872 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873 break;
874 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000875 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000876 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000877 break;
878 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000879 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000880 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000881 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000882 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000883 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000884}
885
Evan Chenga8e29892007-01-19 07:51:42 +0000886const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
887 switch (Opcode) {
888 default: return 0;
889 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000890 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000891 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000892 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
893 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000894 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
896 case ARMISD::tCALL: return "ARMISD::tCALL";
897 case ARMISD::BRCOND: return "ARMISD::BRCOND";
898 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000899 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
901 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
902 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000903 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000904 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ARMISD::CMPFP: return "ARMISD::CMPFP";
906 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000907 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000908 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000909
Evan Chenga8e29892007-01-19 07:51:42 +0000910 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000911 case ARMISD::CAND: return "ARMISD::CAND";
912 case ARMISD::COR: return "ARMISD::COR";
913 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000914
Jim Grosbach3482c802010-01-18 19:58:49 +0000915 case ARMISD::RBIT: return "ARMISD::RBIT";
916
Bob Wilson76a312b2010-03-19 22:51:32 +0000917 case ARMISD::FTOSI: return "ARMISD::FTOSI";
918 case ARMISD::FTOUI: return "ARMISD::FTOUI";
919 case ARMISD::SITOF: return "ARMISD::SITOF";
920 case ARMISD::UITOF: return "ARMISD::UITOF";
921
Evan Chenga8e29892007-01-19 07:51:42 +0000922 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
923 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
924 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000925
Evan Cheng342e3162011-08-30 01:34:54 +0000926 case ARMISD::ADDC: return "ARMISD::ADDC";
927 case ARMISD::ADDE: return "ARMISD::ADDE";
928 case ARMISD::SUBC: return "ARMISD::SUBC";
929 case ARMISD::SUBE: return "ARMISD::SUBE";
930
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000931 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
932 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000933
Evan Chengc5942082009-10-28 06:55:03 +0000934 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
935 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
936
Dale Johannesen51e28e62010-06-03 21:09:53 +0000937 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000938
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000939 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000940
Evan Cheng86198642009-08-07 00:34:42 +0000941 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
942
Jim Grosbach3728e962009-12-10 00:11:09 +0000943 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000944 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000945
Evan Chengdfed19f2010-11-03 06:34:55 +0000946 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
947
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000949 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000950 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000951 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
952 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000953 case ARMISD::VCGEU: return "ARMISD::VCGEU";
954 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000955 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
956 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000957 case ARMISD::VCGTU: return "ARMISD::VCGTU";
958 case ARMISD::VTST: return "ARMISD::VTST";
959
960 case ARMISD::VSHL: return "ARMISD::VSHL";
961 case ARMISD::VSHRs: return "ARMISD::VSHRs";
962 case ARMISD::VSHRu: return "ARMISD::VSHRu";
963 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
964 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
965 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
966 case ARMISD::VSHRN: return "ARMISD::VSHRN";
967 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
968 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
969 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
970 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
971 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
972 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
973 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
974 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
975 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
976 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
977 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
978 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
979 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
980 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000981 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000982 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000983 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000984 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000985 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000986 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000987 case ARMISD::VREV64: return "ARMISD::VREV64";
988 case ARMISD::VREV32: return "ARMISD::VREV32";
989 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000990 case ARMISD::VZIP: return "ARMISD::VZIP";
991 case ARMISD::VUZP: return "ARMISD::VUZP";
992 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000993 case ARMISD::VTBL1: return "ARMISD::VTBL1";
994 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000995 case ARMISD::VMULLs: return "ARMISD::VMULLs";
996 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +0000997 case ARMISD::UMLAL: return "ARMISD::UMLAL";
998 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000999 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001000 case ARMISD::FMAX: return "ARMISD::FMAX";
1001 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001002 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001003 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1004 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001005 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001006 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1007 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1008 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001009 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1010 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1011 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1012 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1013 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1014 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1015 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1016 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1017 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1018 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1019 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1020 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1021 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1022 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1023 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1024 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1025 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001026 }
1027}
1028
Duncan Sands28b77e92011-09-06 19:07:46 +00001029EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1030 if (!VT.isVector()) return getPointerTy();
1031 return VT.changeVectorElementTypeToInteger();
1032}
1033
Evan Cheng06b666c2010-05-15 02:18:07 +00001034/// getRegClassFor - Return the register class that should be used for the
1035/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001036const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001037 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1038 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1039 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001040 if (Subtarget->hasNEON()) {
1041 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001042 return &ARM::QQPRRegClass;
1043 if (VT == MVT::v8i64)
1044 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001045 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001046 return TargetLowering::getRegClassFor(VT);
1047}
1048
Eric Christopherab695882010-07-21 22:26:11 +00001049// Create a fast isel object.
1050FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001051ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1052 const TargetLibraryInfo *libInfo) const {
1053 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001054}
1055
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001056/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1057/// be used for loads / stores from the global.
1058unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1059 return (Subtarget->isThumb1Only() ? 127 : 4095);
1060}
1061
Evan Cheng1cc39842010-05-20 23:26:43 +00001062Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001063 unsigned NumVals = N->getNumValues();
1064 if (!NumVals)
1065 return Sched::RegPressure;
1066
1067 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001068 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001069 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001070 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001071 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001072 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001073 }
Evan Chengc10f5432010-05-28 23:25:23 +00001074
1075 if (!N->isMachineOpcode())
1076 return Sched::RegPressure;
1077
1078 // Load are scheduled for latency even if there instruction itinerary
1079 // is not available.
1080 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001081 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001082
Evan Chenge837dea2011-06-28 19:10:37 +00001083 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001084 return Sched::RegPressure;
1085 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001086 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001087 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001088
Evan Cheng1cc39842010-05-20 23:26:43 +00001089 return Sched::RegPressure;
1090}
1091
Evan Chenga8e29892007-01-19 07:51:42 +00001092//===----------------------------------------------------------------------===//
1093// Lowering Code
1094//===----------------------------------------------------------------------===//
1095
Evan Chenga8e29892007-01-19 07:51:42 +00001096/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1097static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1098 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001099 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001100 case ISD::SETNE: return ARMCC::NE;
1101 case ISD::SETEQ: return ARMCC::EQ;
1102 case ISD::SETGT: return ARMCC::GT;
1103 case ISD::SETGE: return ARMCC::GE;
1104 case ISD::SETLT: return ARMCC::LT;
1105 case ISD::SETLE: return ARMCC::LE;
1106 case ISD::SETUGT: return ARMCC::HI;
1107 case ISD::SETUGE: return ARMCC::HS;
1108 case ISD::SETULT: return ARMCC::LO;
1109 case ISD::SETULE: return ARMCC::LS;
1110 }
1111}
1112
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001113/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1114static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001115 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001116 CondCode2 = ARMCC::AL;
1117 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001118 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001119 case ISD::SETEQ:
1120 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1121 case ISD::SETGT:
1122 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1123 case ISD::SETGE:
1124 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1125 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001126 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001127 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1128 case ISD::SETO: CondCode = ARMCC::VC; break;
1129 case ISD::SETUO: CondCode = ARMCC::VS; break;
1130 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1131 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1132 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1133 case ISD::SETLT:
1134 case ISD::SETULT: CondCode = ARMCC::LT; break;
1135 case ISD::SETLE:
1136 case ISD::SETULE: CondCode = ARMCC::LE; break;
1137 case ISD::SETNE:
1138 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1139 }
Evan Chenga8e29892007-01-19 07:51:42 +00001140}
1141
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142//===----------------------------------------------------------------------===//
1143// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144//===----------------------------------------------------------------------===//
1145
1146#include "ARMGenCallingConv.inc"
1147
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001148/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1149/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001150CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001151 bool Return,
1152 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001153 switch (CC) {
1154 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001155 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001156 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001157 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001158 if (!Subtarget->isAAPCS_ABI())
1159 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1160 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1161 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1162 }
1163 // Fallthrough
1164 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001165 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001166 if (!Subtarget->isAAPCS_ABI())
1167 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1168 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001169 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1170 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001171 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1172 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1173 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001174 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001175 if (!isVarArg)
1176 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1177 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001178 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001179 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001180 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001181 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001182 case CallingConv::GHC:
1183 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001184 }
1185}
1186
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187/// LowerCallResult - Lower the result values of a call into the
1188/// appropriate copies out of appropriate physical registers.
1189SDValue
1190ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001191 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 const SmallVectorImpl<ISD::InputArg> &Ins,
1193 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001194 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001195
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196 // Assign locations to each value returned by this call.
1197 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001198 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1199 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001201 CCAssignFnForNode(CallConv, /* Return*/ true,
1202 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001203
1204 // Copy all of the result registers out of their specified physreg.
1205 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1206 CCValAssign VA = RVLocs[i];
1207
Bob Wilson80915242009-04-25 00:33:20 +00001208 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001210 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001212 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001213 Chain = Lo.getValue(1);
1214 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001215 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001217 InFlag);
1218 Chain = Hi.getValue(1);
1219 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001220 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001221
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 if (VA.getLocVT() == MVT::v2f64) {
1223 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1224 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1225 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001226
1227 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001229 Chain = Lo.getValue(1);
1230 InFlag = Lo.getValue(2);
1231 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001232 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001233 Chain = Hi.getValue(1);
1234 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001235 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1237 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001238 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001240 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1241 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001242 Chain = Val.getValue(1);
1243 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244 }
Bob Wilson80915242009-04-25 00:33:20 +00001245
1246 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001247 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001248 case CCValAssign::Full: break;
1249 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001250 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001251 break;
1252 }
1253
Dan Gohman98ca4f22009-08-05 01:29:28 +00001254 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001255 }
1256
Dan Gohman98ca4f22009-08-05 01:29:28 +00001257 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258}
1259
Bob Wilsondee46d72009-04-17 20:35:10 +00001260/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001261SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001262ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1263 SDValue StackPtr, SDValue Arg,
1264 DebugLoc dl, SelectionDAG &DAG,
1265 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001266 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001267 unsigned LocMemOffset = VA.getLocMemOffset();
1268 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1269 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001270 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001271 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001272 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001273}
1274
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001276 SDValue Chain, SDValue &Arg,
1277 RegsToPassVector &RegsToPass,
1278 CCValAssign &VA, CCValAssign &NextVA,
1279 SDValue &StackPtr,
1280 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001281 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001282
Jim Grosbache5165492009-11-09 00:11:35 +00001283 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001284 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001285 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1286
1287 if (NextVA.isRegLoc())
1288 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1289 else {
1290 assert(NextVA.isMemLoc());
1291 if (StackPtr.getNode() == 0)
1292 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1293
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1295 dl, DAG, NextVA,
1296 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001297 }
1298}
1299
Dan Gohman98ca4f22009-08-05 01:29:28 +00001300/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001301/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1302/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001303SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001304ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001305 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001306 SelectionDAG &DAG = CLI.DAG;
1307 DebugLoc &dl = CLI.DL;
1308 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1309 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1310 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1311 SDValue Chain = CLI.Chain;
1312 SDValue Callee = CLI.Callee;
1313 bool &isTailCall = CLI.IsTailCall;
1314 CallingConv::ID CallConv = CLI.CallConv;
1315 bool doesNotRet = CLI.DoesNotReturn;
1316 bool isVarArg = CLI.IsVarArg;
1317
Dale Johannesen51e28e62010-06-03 21:09:53 +00001318 MachineFunction &MF = DAG.getMachineFunction();
1319 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1320 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001321 // Disable tail calls if they're not supported.
1322 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001323 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001324 if (isTailCall) {
1325 // Check if it's really possible to do a tail call.
1326 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1327 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001328 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001329 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1330 // detected sibcalls.
1331 if (isTailCall) {
1332 ++NumTailCalls;
1333 IsSibCall = true;
1334 }
1335 }
Evan Chenga8e29892007-01-19 07:51:42 +00001336
Bob Wilson1f595bb2009-04-17 19:07:39 +00001337 // Analyze operands of the call, assigning locations to each operand.
1338 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001339 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1340 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001342 CCAssignFnForNode(CallConv, /* Return*/ false,
1343 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001344
Bob Wilson1f595bb2009-04-17 19:07:39 +00001345 // Get a count of how many bytes are to be pushed on the stack.
1346 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001347
Dale Johannesen51e28e62010-06-03 21:09:53 +00001348 // For tail calls, memory operands are available in our caller's stack.
1349 if (IsSibCall)
1350 NumBytes = 0;
1351
Evan Chenga8e29892007-01-19 07:51:42 +00001352 // Adjust the stack pointer for the new arguments...
1353 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001354 if (!IsSibCall)
1355 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001356
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001357 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001358
Bob Wilson5bafff32009-06-22 23:27:02 +00001359 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001360 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001361
Bob Wilson1f595bb2009-04-17 19:07:39 +00001362 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001363 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1365 i != e;
1366 ++i, ++realArgIdx) {
1367 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001368 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001369 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001370 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001371
Bob Wilson1f595bb2009-04-17 19:07:39 +00001372 // Promote the value if needed.
1373 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001374 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 case CCValAssign::Full: break;
1376 case CCValAssign::SExt:
1377 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1378 break;
1379 case CCValAssign::ZExt:
1380 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1381 break;
1382 case CCValAssign::AExt:
1383 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1384 break;
1385 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001386 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001387 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001388 }
1389
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001390 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001391 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 if (VA.getLocVT() == MVT::v2f64) {
1393 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1394 DAG.getConstant(0, MVT::i32));
1395 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1396 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001397
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001399 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1400
1401 VA = ArgLocs[++i]; // skip ahead to next loc
1402 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001403 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001404 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1405 } else {
1406 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001407
Dan Gohman98ca4f22009-08-05 01:29:28 +00001408 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1409 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001410 }
1411 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001412 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001413 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001414 }
1415 } else if (VA.isRegLoc()) {
1416 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001417 } else if (isByVal) {
1418 assert(VA.isMemLoc());
1419 unsigned offset = 0;
1420
1421 // True if this byval aggregate will be split between registers
1422 // and memory.
1423 if (CCInfo.isFirstByValRegValid()) {
1424 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1425 unsigned int i, j;
1426 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1427 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1428 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1429 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1430 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001431 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001432 MemOpChains.push_back(Load.getValue(1));
1433 RegsToPass.push_back(std::make_pair(j, Load));
1434 }
1435 offset = ARM::R4 - CCInfo.getFirstByValReg();
1436 CCInfo.clearFirstByValReg();
1437 }
1438
Manman Ren763a75d2012-06-01 02:44:42 +00001439 if (Flags.getByValSize() - 4*offset > 0) {
1440 unsigned LocMemOffset = VA.getLocMemOffset();
1441 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1442 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1443 StkPtrOff);
1444 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1445 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1446 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1447 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001448 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001449
Manman Ren763a75d2012-06-01 02:44:42 +00001450 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001451 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001452 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1453 Ops, array_lengthof(Ops)));
1454 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001455 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001456 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001457
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1459 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001460 }
Evan Chenga8e29892007-01-19 07:51:42 +00001461 }
1462
1463 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001464 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001465 &MemOpChains[0], MemOpChains.size());
1466
1467 // Build a sequence of copy-to-reg nodes chained together with token chain
1468 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001469 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001470 // Tail call byval lowering might overwrite argument registers so in case of
1471 // tail call optimization the copies to registers are lowered later.
1472 if (!isTailCall)
1473 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1474 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1475 RegsToPass[i].second, InFlag);
1476 InFlag = Chain.getValue(1);
1477 }
Evan Chenga8e29892007-01-19 07:51:42 +00001478
Dale Johannesen51e28e62010-06-03 21:09:53 +00001479 // For tail calls lower the arguments to the 'real' stack slot.
1480 if (isTailCall) {
1481 // Force all the incoming stack arguments to be loaded from the stack
1482 // before any new outgoing arguments are stored to the stack, because the
1483 // outgoing stack slots may alias the incoming argument stack slots, and
1484 // the alias isn't otherwise explicit. This is slightly more conservative
1485 // than necessary, because it means that each store effectively depends
1486 // on every argument instead of just those arguments it would clobber.
1487
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001488 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001489 InFlag = SDValue();
1490 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1491 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1492 RegsToPass[i].second, InFlag);
1493 InFlag = Chain.getValue(1);
1494 }
1495 InFlag =SDValue();
1496 }
1497
Bill Wendling056292f2008-09-16 21:48:12 +00001498 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1499 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1500 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001501 bool isDirect = false;
1502 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001503 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001504 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001505
1506 if (EnableARMLongCalls) {
1507 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1508 && "long-calls with non-static relocation model!");
1509 // Handle a global address or an external symbol. If it's not one of
1510 // those, the target's already in a register, so we don't need to do
1511 // anything extra.
1512 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001513 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001514 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001515 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001516 ARMConstantPoolValue *CPV =
1517 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1518
Jim Grosbache7b52522010-04-14 22:28:31 +00001519 // Get the address of the callee into a register
1520 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1521 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1522 Callee = DAG.getLoad(getPointerTy(), dl,
1523 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001524 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001525 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001526 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1527 const char *Sym = S->getSymbol();
1528
1529 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001530 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001531 ARMConstantPoolValue *CPV =
1532 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1533 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001534 // Get the address of the callee into a register
1535 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1536 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1537 Callee = DAG.getLoad(getPointerTy(), dl,
1538 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001539 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001540 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001541 }
1542 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001543 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001544 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001545 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001546 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001547 getTargetMachine().getRelocationModel() != Reloc::Static;
1548 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001549 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001550 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001551 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001552 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001553 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001554 ARMConstantPoolValue *CPV =
1555 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001556 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001557 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001558 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001559 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001560 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001561 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001562 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001563 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001564 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001565 } else {
1566 // On ELF targets for PIC code, direct calls should go through the PLT
1567 unsigned OpFlags = 0;
1568 if (Subtarget->isTargetELF() &&
1569 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1570 OpFlags = ARMII::MO_PLT;
1571 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1572 }
Bill Wendling056292f2008-09-16 21:48:12 +00001573 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001574 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001575 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001576 getTargetMachine().getRelocationModel() != Reloc::Static;
1577 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001578 // tBX takes a register source operand.
1579 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001580 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001581 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001582 ARMConstantPoolValue *CPV =
1583 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1584 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001585 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001587 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001588 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001589 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001590 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001591 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001592 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001593 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001594 } else {
1595 unsigned OpFlags = 0;
1596 // On ELF targets for PIC code, direct calls should go through the PLT
1597 if (Subtarget->isTargetELF() &&
1598 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1599 OpFlags = ARMII::MO_PLT;
1600 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1601 }
Evan Chenga8e29892007-01-19 07:51:42 +00001602 }
1603
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001604 // FIXME: handle tail calls differently.
1605 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001606 if (Subtarget->isThumb()) {
1607 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001608 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001609 else if (doesNotRet && isDirect && !isARMFunc &&
1610 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1611 // "mov lr, pc; b _foo" to avoid confusing the RSP
1612 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001613 else
1614 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1615 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001616 if (!isDirect && !Subtarget->hasV5TOps()) {
1617 CallOpc = ARMISD::CALL_NOLINK;
1618 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1619 // "mov lr, pc; b _foo" to avoid confusing the RSP
1620 CallOpc = ARMISD::CALL_NOLINK;
1621 else
1622 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001623 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001624
Dan Gohman475871a2008-07-27 21:46:04 +00001625 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001626 Ops.push_back(Chain);
1627 Ops.push_back(Callee);
1628
1629 // Add argument registers to the end of the list so that they are known live
1630 // into the call.
1631 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1632 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1633 RegsToPass[i].second.getValueType()));
1634
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001635 // Add a register mask operand representing the call-preserved registers.
1636 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1637 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1638 assert(Mask && "Missing call preserved mask for calling convention");
1639 Ops.push_back(DAG.getRegisterMask(Mask));
1640
Gabor Greifba36cb52008-08-28 21:40:38 +00001641 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001642 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001643
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001644 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001645 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001646 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001647
Duncan Sands4bdcb612008-07-02 17:40:58 +00001648 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001649 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001650 InFlag = Chain.getValue(1);
1651
Chris Lattnere563bbc2008-10-11 22:08:30 +00001652 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1653 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001655 InFlag = Chain.getValue(1);
1656
Bob Wilson1f595bb2009-04-17 19:07:39 +00001657 // Handle result values, copying them out of physregs into vregs that we
1658 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1660 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001661}
1662
Stuart Hastingsf222e592011-02-28 17:17:53 +00001663/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001664/// on the stack. Remember the next parameter register to allocate,
1665/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001666/// this.
1667void
Craig Topperc89c7442012-03-27 07:21:54 +00001668ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001669 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1670 assert((State->getCallOrPrologue() == Prologue ||
1671 State->getCallOrPrologue() == Call) &&
1672 "unhandled ParmContext");
1673 if ((!State->isFirstByValRegValid()) &&
1674 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1675 State->setFirstByValReg(reg);
1676 // At a call site, a byval parameter that is split between
1677 // registers and memory needs its size truncated here. In a
1678 // function prologue, such byval parameters are reassembled in
1679 // memory, and are not truncated.
1680 if (State->getCallOrPrologue() == Call) {
1681 unsigned excess = 4 * (ARM::R4 - reg);
1682 assert(size >= excess && "expected larger existing stack allocation");
1683 size -= excess;
1684 }
1685 }
1686 // Confiscate any remaining parameter registers to preclude their
1687 // assignment to subsequent parameters.
1688 while (State->AllocateReg(GPRArgRegs, 4))
1689 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001690}
1691
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692/// MatchingStackOffset - Return true if the given stack call argument is
1693/// already available in the same position (relatively) of the caller's
1694/// incoming argument stack.
1695static
1696bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1697 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001698 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001699 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1700 int FI = INT_MAX;
1701 if (Arg.getOpcode() == ISD::CopyFromReg) {
1702 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001703 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001704 return false;
1705 MachineInstr *Def = MRI->getVRegDef(VR);
1706 if (!Def)
1707 return false;
1708 if (!Flags.isByVal()) {
1709 if (!TII->isLoadFromStackSlot(Def, FI))
1710 return false;
1711 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001712 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001713 }
1714 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1715 if (Flags.isByVal())
1716 // ByVal argument is passed in as a pointer but it's now being
1717 // dereferenced. e.g.
1718 // define @foo(%struct.X* %A) {
1719 // tail call @bar(%struct.X* byval %A)
1720 // }
1721 return false;
1722 SDValue Ptr = Ld->getBasePtr();
1723 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1724 if (!FINode)
1725 return false;
1726 FI = FINode->getIndex();
1727 } else
1728 return false;
1729
1730 assert(FI != INT_MAX);
1731 if (!MFI->isFixedObjectIndex(FI))
1732 return false;
1733 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1734}
1735
1736/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1737/// for tail call optimization. Targets which want to do tail call
1738/// optimization should implement this function.
1739bool
1740ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1741 CallingConv::ID CalleeCC,
1742 bool isVarArg,
1743 bool isCalleeStructRet,
1744 bool isCallerStructRet,
1745 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001746 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001747 const SmallVectorImpl<ISD::InputArg> &Ins,
1748 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001749 const Function *CallerF = DAG.getMachineFunction().getFunction();
1750 CallingConv::ID CallerCC = CallerF->getCallingConv();
1751 bool CCMatch = CallerCC == CalleeCC;
1752
1753 // Look for obvious safe cases to perform tail call optimization that do not
1754 // require ABI changes. This is what gcc calls sibcall.
1755
Jim Grosbach7616b642010-06-16 23:45:49 +00001756 // Do not sibcall optimize vararg calls unless the call site is not passing
1757 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001758 if (isVarArg && !Outs.empty())
1759 return false;
1760
1761 // Also avoid sibcall optimization if either caller or callee uses struct
1762 // return semantics.
1763 if (isCalleeStructRet || isCallerStructRet)
1764 return false;
1765
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001766 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001767 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1768 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1769 // support in the assembler and linker to be used. This would need to be
1770 // fixed to fully support tail calls in Thumb1.
1771 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001772 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1773 // LR. This means if we need to reload LR, it takes an extra instructions,
1774 // which outweighs the value of the tail call; but here we don't know yet
1775 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001776 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001777 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001778
1779 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1780 // but we need to make sure there are enough registers; the only valid
1781 // registers are the 4 used for parameters. We don't currently do this
1782 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001783 if (Subtarget->isThumb1Only())
1784 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001785
Dale Johannesen51e28e62010-06-03 21:09:53 +00001786 // If the calling conventions do not match, then we'd better make sure the
1787 // results are returned in the same way as what the caller expects.
1788 if (!CCMatch) {
1789 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001790 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1791 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001792 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1793
1794 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001795 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1796 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001797 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1798
1799 if (RVLocs1.size() != RVLocs2.size())
1800 return false;
1801 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1802 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1803 return false;
1804 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1805 return false;
1806 if (RVLocs1[i].isRegLoc()) {
1807 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1808 return false;
1809 } else {
1810 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1811 return false;
1812 }
1813 }
1814 }
1815
1816 // If the callee takes no arguments then go on to check the results of the
1817 // call.
1818 if (!Outs.empty()) {
1819 // Check if stack adjustment is needed. For now, do not do this if any
1820 // argument is passed on the stack.
1821 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001822 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1823 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001824 CCInfo.AnalyzeCallOperands(Outs,
1825 CCAssignFnForNode(CalleeCC, false, isVarArg));
1826 if (CCInfo.getNextStackOffset()) {
1827 MachineFunction &MF = DAG.getMachineFunction();
1828
1829 // Check if the arguments are already laid out in the right way as
1830 // the caller's fixed stack objects.
1831 MachineFrameInfo *MFI = MF.getFrameInfo();
1832 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001833 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001834 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1835 i != e;
1836 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001837 CCValAssign &VA = ArgLocs[i];
1838 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001839 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001840 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001841 if (VA.getLocInfo() == CCValAssign::Indirect)
1842 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001843 if (VA.needsCustom()) {
1844 // f64 and vector types are split into multiple registers or
1845 // register/stack-slot combinations. The types will not match
1846 // the registers; give up on memory f64 refs until we figure
1847 // out what to do about this.
1848 if (!VA.isRegLoc())
1849 return false;
1850 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001851 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001852 if (RegVT == MVT::v2f64) {
1853 if (!ArgLocs[++i].isRegLoc())
1854 return false;
1855 if (!ArgLocs[++i].isRegLoc())
1856 return false;
1857 }
1858 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001859 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1860 MFI, MRI, TII))
1861 return false;
1862 }
1863 }
1864 }
1865 }
1866
1867 return true;
1868}
1869
Dan Gohman98ca4f22009-08-05 01:29:28 +00001870SDValue
1871ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001872 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001874 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001875 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001876
Bob Wilsondee46d72009-04-17 20:35:10 +00001877 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001878 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001879
Bob Wilsondee46d72009-04-17 20:35:10 +00001880 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001881 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1882 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001883
Dan Gohman98ca4f22009-08-05 01:29:28 +00001884 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001885 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1886 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001887
1888 // If this is the first return lowered for this function, add
1889 // the regs to the liveout set for the function.
1890 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1891 for (unsigned i = 0; i != RVLocs.size(); ++i)
1892 if (RVLocs[i].isRegLoc())
1893 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001894 }
1895
Bob Wilson1f595bb2009-04-17 19:07:39 +00001896 SDValue Flag;
1897
1898 // Copy the result values into the output registers.
1899 for (unsigned i = 0, realRVLocIdx = 0;
1900 i != RVLocs.size();
1901 ++i, ++realRVLocIdx) {
1902 CCValAssign &VA = RVLocs[i];
1903 assert(VA.isRegLoc() && "Can only return in registers!");
1904
Dan Gohmanc9403652010-07-07 15:54:55 +00001905 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001906
1907 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001908 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001909 case CCValAssign::Full: break;
1910 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001911 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001912 break;
1913 }
1914
Bob Wilson1f595bb2009-04-17 19:07:39 +00001915 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001917 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001918 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1919 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001920 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001922
1923 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1924 Flag = Chain.getValue(1);
1925 VA = RVLocs[++i]; // skip ahead to next loc
1926 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1927 HalfGPRs.getValue(1), Flag);
1928 Flag = Chain.getValue(1);
1929 VA = RVLocs[++i]; // skip ahead to next loc
1930
1931 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1933 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001934 }
1935 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1936 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001937 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001938 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001939 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001940 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001941 VA = RVLocs[++i]; // skip ahead to next loc
1942 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1943 Flag);
1944 } else
1945 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1946
Bob Wilsondee46d72009-04-17 20:35:10 +00001947 // Guarantee that all emitted copies are
1948 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001949 Flag = Chain.getValue(1);
1950 }
1951
1952 SDValue result;
1953 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001955 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001957
1958 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001959}
1960
Evan Chengbf010eb2012-04-10 01:51:00 +00001961bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001962 if (N->getNumValues() != 1)
1963 return false;
1964 if (!N->hasNUsesOfValue(1, 0))
1965 return false;
1966
Evan Chengbf010eb2012-04-10 01:51:00 +00001967 SDValue TCChain = Chain;
1968 SDNode *Copy = *N->use_begin();
1969 if (Copy->getOpcode() == ISD::CopyToReg) {
1970 // If the copy has a glue operand, we conservatively assume it isn't safe to
1971 // perform a tail call.
1972 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1973 return false;
1974 TCChain = Copy->getOperand(0);
1975 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1976 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001978 SmallPtrSet<SDNode*, 2> Copies;
1979 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001980 UI != UE; ++UI) {
1981 if (UI->getOpcode() != ISD::CopyToReg)
1982 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001983 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001984 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001985 if (Copies.size() > 2)
1986 return false;
1987
1988 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1989 UI != UE; ++UI) {
1990 SDValue UseChain = UI->getOperand(0);
1991 if (Copies.count(UseChain.getNode()))
1992 // Second CopyToReg
1993 Copy = *UI;
1994 else
1995 // First CopyToReg
1996 TCChain = UseChain;
1997 }
1998 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001999 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002000 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002001 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002002 Copy = *Copy->use_begin();
2003 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002004 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002005 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002006 } else {
2007 return false;
2008 }
2009
Evan Cheng1bf891a2010-12-01 22:59:46 +00002010 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002011 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2012 UI != UE; ++UI) {
2013 if (UI->getOpcode() != ARMISD::RET_FLAG)
2014 return false;
2015 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002016 }
2017
Evan Chengbf010eb2012-04-10 01:51:00 +00002018 if (!HasRet)
2019 return false;
2020
2021 Chain = TCChain;
2022 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002023}
2024
Evan Cheng485fafc2011-03-21 01:19:09 +00002025bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002026 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002027 return false;
2028
2029 if (!CI->isTailCall())
2030 return false;
2031
2032 return !Subtarget->isThumb1Only();
2033}
2034
Bob Wilsonb62d2572009-11-03 00:02:05 +00002035// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2036// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2037// one of the above mentioned nodes. It has to be wrapped because otherwise
2038// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2039// be used to form addressing mode. These wrapped nodes will be selected
2040// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002041static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002042 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002043 // FIXME there is no actual debug info here
2044 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002045 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002046 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002047 if (CP->isMachineConstantPoolEntry())
2048 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2049 CP->getAlignment());
2050 else
2051 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2052 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002054}
2055
Jim Grosbache1102ca2010-07-19 17:20:38 +00002056unsigned ARMTargetLowering::getJumpTableEncoding() const {
2057 return MachineJumpTableInfo::EK_Inline;
2058}
2059
Dan Gohmand858e902010-04-17 15:26:15 +00002060SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2061 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002062 MachineFunction &MF = DAG.getMachineFunction();
2063 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2064 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002065 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002066 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002067 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002068 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2069 SDValue CPAddr;
2070 if (RelocM == Reloc::Static) {
2071 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2072 } else {
2073 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002074 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002075 ARMConstantPoolValue *CPV =
2076 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2077 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002078 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2079 }
2080 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2081 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002082 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002083 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002084 if (RelocM == Reloc::Static)
2085 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002086 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002087 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002088}
2089
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002090// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002091SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002092ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002093 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002094 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002095 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002096 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002097 MachineFunction &MF = DAG.getMachineFunction();
2098 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002099 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002100 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002101 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2102 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002103 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002105 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002106 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002107 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002108 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002109
Evan Chenge7e0d622009-11-06 22:24:13 +00002110 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002111 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002112
2113 // call __tls_get_addr.
2114 ArgListTy Args;
2115 ArgListEntry Entry;
2116 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002117 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002118 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002119 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002120 TargetLowering::CallLoweringInfo CLI(Chain,
2121 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002122 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002123 0, CallingConv::C, /*isTailCall=*/false,
2124 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002125 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002126 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002127 return CallResult.first;
2128}
2129
2130// Lower ISD::GlobalTLSAddress using the "initial exec" or
2131// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002132SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002134 SelectionDAG &DAG,
2135 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002136 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002137 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002138 SDValue Offset;
2139 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002140 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002141 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002142 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002143
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002144 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002145 MachineFunction &MF = DAG.getMachineFunction();
2146 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002147 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002148 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002149 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2150 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002151 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2152 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2153 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002154 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002155 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002156 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002157 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002158 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159 Chain = Offset.getValue(1);
2160
Evan Chenge7e0d622009-11-06 22:24:13 +00002161 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002162 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002163
Evan Cheng9eda6892009-10-31 03:39:36 +00002164 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002165 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002166 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002167 } else {
2168 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002169 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002170 ARMConstantPoolValue *CPV =
2171 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002172 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002174 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002175 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002176 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002177 }
2178
2179 // The address of the thread local variable is the add of the thread
2180 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002181 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002182}
2183
Dan Gohman475871a2008-07-27 21:46:04 +00002184SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002185ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002186 // TODO: implement the "local dynamic" model
2187 assert(Subtarget->isTargetELF() &&
2188 "TLS not implemented for non-ELF targets");
2189 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002190
2191 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2192
2193 switch (model) {
2194 case TLSModel::GeneralDynamic:
2195 case TLSModel::LocalDynamic:
2196 return LowerToTLSGeneralDynamicModel(GA, DAG);
2197 case TLSModel::InitialExec:
2198 case TLSModel::LocalExec:
2199 return LowerToTLSExecModels(GA, DAG, model);
2200 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002201 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002202}
2203
Dan Gohman475871a2008-07-27 21:46:04 +00002204SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002205 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002207 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002208 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002209 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2210 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002211 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002212 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002213 ARMConstantPoolConstant::Create(GV,
2214 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002215 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002217 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002218 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002219 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002220 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002222 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002223 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002224 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002225 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002226 MachinePointerInfo::getGOT(),
2227 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002228 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002229 }
2230
2231 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002232 // pair. This is always cheaper.
2233 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002234 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002235 // FIXME: Once remat is capable of dealing with instructions with register
2236 // operands, expand this into two nodes.
2237 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2238 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002239 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002240 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2241 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2242 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2243 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002244 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002245 }
2246}
2247
Dan Gohman475871a2008-07-27 21:46:04 +00002248SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002249 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002250 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002251 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002252 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002253 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002254 MachineFunction &MF = DAG.getMachineFunction();
2255 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2256
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002257 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2258 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002259 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002260 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002261 // FIXME: Once remat is capable of dealing with instructions with register
2262 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002263 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002264 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2265 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2266
Evan Cheng53519f02011-01-21 18:55:51 +00002267 unsigned Wrapper = (RelocM == Reloc::PIC_)
2268 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2269 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002270 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002271 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2272 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002273 MachinePointerInfo::getGOT(),
2274 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002275 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002276 }
2277
2278 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002279 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002280 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002281 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002282 } else {
2283 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002284 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2285 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002286 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2287 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002288 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002289 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002291
Evan Cheng9eda6892009-10-31 03:39:36 +00002292 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002293 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002294 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002295 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002296
2297 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002298 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002299 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002300 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002301
Evan Cheng63476a82009-09-03 07:04:02 +00002302 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002303 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002304 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002305
2306 return Result;
2307}
2308
Dan Gohman475871a2008-07-27 21:46:04 +00002309SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002310 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002311 assert(Subtarget->isTargetELF() &&
2312 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002313 MachineFunction &MF = DAG.getMachineFunction();
2314 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002315 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002316 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002317 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002318 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002319 ARMConstantPoolValue *CPV =
2320 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2321 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002322 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002323 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002324 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002325 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002326 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002327 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002328 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002329}
2330
Jim Grosbach0e0da732009-05-12 23:59:14 +00002331SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002332ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2333 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002334 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002335 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2336 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002337 Op.getOperand(1), Val);
2338}
2339
2340SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002341ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2342 DebugLoc dl = Op.getDebugLoc();
2343 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2344 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2345}
2346
2347SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002348ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002349 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002350 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002351 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002352 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002353 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002354 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002355 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002356 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2357 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002358 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002359 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002360 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002361 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002362 EVT PtrVT = getPointerTy();
2363 DebugLoc dl = Op.getDebugLoc();
2364 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2365 SDValue CPAddr;
2366 unsigned PCAdj = (RelocM != Reloc::PIC_)
2367 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002368 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002369 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2370 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002371 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002373 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002374 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002375 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002376 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002377
2378 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002379 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002380 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2381 }
2382 return Result;
2383 }
Evan Cheng92e39162011-03-29 23:06:19 +00002384 case Intrinsic::arm_neon_vmulls:
2385 case Intrinsic::arm_neon_vmullu: {
2386 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2387 ? ARMISD::VMULLs : ARMISD::VMULLu;
2388 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2389 Op.getOperand(1), Op.getOperand(2));
2390 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002391 }
2392}
2393
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002394static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002395 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002396 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002397 if (!Subtarget->hasDataBarrier()) {
2398 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2399 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2400 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002401 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002402 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002403 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002404 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002405 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002406
2407 SDValue Op5 = Op.getOperand(5);
2408 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2409 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2410 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2411 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2412
2413 ARM_MB::MemBOpt DMBOpt;
2414 if (isDeviceBarrier)
2415 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2416 else
2417 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2418 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2419 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002420}
2421
Eli Friedman26689ac2011-08-03 21:06:02 +00002422
2423static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2424 const ARMSubtarget *Subtarget) {
2425 // FIXME: handle "fence singlethread" more efficiently.
2426 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002427 if (!Subtarget->hasDataBarrier()) {
2428 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2429 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2430 // here.
2431 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2432 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002433 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002434 DAG.getConstant(0, MVT::i32));
2435 }
2436
Eli Friedman26689ac2011-08-03 21:06:02 +00002437 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002438 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002439}
2440
Evan Chengdfed19f2010-11-03 06:34:55 +00002441static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2442 const ARMSubtarget *Subtarget) {
2443 // ARM pre v5TE and Thumb1 does not have preload instructions.
2444 if (!(Subtarget->isThumb2() ||
2445 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2446 // Just preserve the chain.
2447 return Op.getOperand(0);
2448
2449 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002450 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2451 if (!isRead &&
2452 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2453 // ARMv7 with MP extension has PLDW.
2454 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002455
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002456 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2457 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002458 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002459 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002460 isData = ~isData & 1;
2461 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002462
2463 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002464 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2465 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002466}
2467
Dan Gohman1e93df62010-04-17 14:41:14 +00002468static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2469 MachineFunction &MF = DAG.getMachineFunction();
2470 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2471
Evan Chenga8e29892007-01-19 07:51:42 +00002472 // vastart just stores the address of the VarArgsFrameIndex slot into the
2473 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002474 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002475 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002476 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002477 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002478 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2479 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002480}
2481
Dan Gohman475871a2008-07-27 21:46:04 +00002482SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002483ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2484 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002485 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002486 MachineFunction &MF = DAG.getMachineFunction();
2487 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2488
Craig Topper44d23822012-02-22 05:59:10 +00002489 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002490 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002491 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002492 else
Craig Topper420761a2012-04-20 07:30:17 +00002493 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002494
2495 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002496 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002497 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002498
2499 SDValue ArgValue2;
2500 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002502 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002503
2504 // Create load node to retrieve arguments from the stack.
2505 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002506 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002507 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002508 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002509 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002510 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002512 }
2513
Jim Grosbache5165492009-11-09 00:11:35 +00002514 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002515}
2516
Stuart Hastingsc7315872011-04-20 16:47:52 +00002517void
2518ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2519 unsigned &VARegSize, unsigned &VARegSaveSize)
2520 const {
2521 unsigned NumGPRs;
2522 if (CCInfo.isFirstByValRegValid())
2523 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2524 else {
2525 unsigned int firstUnalloced;
2526 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2527 sizeof(GPRArgRegs) /
2528 sizeof(GPRArgRegs[0]));
2529 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2530 }
2531
2532 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2533 VARegSize = NumGPRs * 4;
2534 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2535}
2536
2537// The remaining GPRs hold either the beginning of variable-argument
2538// data, or the beginning of an aggregate passed by value (usuall
2539// byval). Either way, we allocate stack slots adjacent to the data
2540// provided by our caller, and store the unallocated registers there.
2541// If this is a variadic function, the va_list pointer will begin with
2542// these values; otherwise, this reassembles a (byval) structure that
2543// was split between registers and memory.
2544void
2545ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2546 DebugLoc dl, SDValue &Chain,
2547 unsigned ArgOffset) const {
2548 MachineFunction &MF = DAG.getMachineFunction();
2549 MachineFrameInfo *MFI = MF.getFrameInfo();
2550 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2551 unsigned firstRegToSaveIndex;
2552 if (CCInfo.isFirstByValRegValid())
2553 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2554 else {
2555 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2556 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2557 }
2558
2559 unsigned VARegSize, VARegSaveSize;
2560 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2561 if (VARegSaveSize) {
2562 // If this function is vararg, store any remaining integer argument regs
2563 // to their spots on the stack so that they may be loaded by deferencing
2564 // the result of va_next.
2565 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002566 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2567 ArgOffset + VARegSaveSize
2568 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002569 false));
2570 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2571 getPointerTy());
2572
2573 SmallVector<SDValue, 4> MemOps;
2574 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002575 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002576 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002577 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002578 else
Craig Topper420761a2012-04-20 07:30:17 +00002579 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002580
2581 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2582 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2583 SDValue Store =
2584 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002585 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002586 false, false, 0);
2587 MemOps.push_back(Store);
2588 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2589 DAG.getConstant(4, getPointerTy()));
2590 }
2591 if (!MemOps.empty())
2592 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2593 &MemOps[0], MemOps.size());
2594 } else
2595 // This will point to the next argument passed via stack.
2596 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2597}
2598
Bob Wilson5bafff32009-06-22 23:27:02 +00002599SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002600ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002601 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002602 const SmallVectorImpl<ISD::InputArg>
2603 &Ins,
2604 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002605 SmallVectorImpl<SDValue> &InVals)
2606 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002607 MachineFunction &MF = DAG.getMachineFunction();
2608 MachineFrameInfo *MFI = MF.getFrameInfo();
2609
Bob Wilson1f595bb2009-04-17 19:07:39 +00002610 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2611
2612 // Assign locations to all of the incoming arguments.
2613 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002614 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2615 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002616 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002617 CCAssignFnForNode(CallConv, /* Return*/ false,
2618 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619
2620 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002621 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002622
Stuart Hastingsf222e592011-02-28 17:17:53 +00002623 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002624 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2625 CCValAssign &VA = ArgLocs[i];
2626
Bob Wilsondee46d72009-04-17 20:35:10 +00002627 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002628 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002629 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002630
Bob Wilson1f595bb2009-04-17 19:07:39 +00002631 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002632 // f64 and vector types are split up into multiple registers or
2633 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002635 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002636 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002637 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002638 SDValue ArgValue2;
2639 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002640 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002641 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2642 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002643 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002644 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002645 } else {
2646 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2647 Chain, DAG, dl);
2648 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002649 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2650 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002651 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002653 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2654 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002655 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002656
Bob Wilson5bafff32009-06-22 23:27:02 +00002657 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002658 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002659
Owen Anderson825b72b2009-08-11 20:47:22 +00002660 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002661 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002663 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002664 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002665 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002667 RC = AFI->isThumb1OnlyFunction() ?
2668 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2669 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002670 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002671 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002672
2673 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002674 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002675 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002676 }
2677
2678 // If this is an 8 or 16-bit value, it is really passed promoted
2679 // to 32 bits. Insert an assert[sz]ext to capture this, then
2680 // truncate to the right size.
2681 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002682 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002683 case CCValAssign::Full: break;
2684 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002685 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002686 break;
2687 case CCValAssign::SExt:
2688 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2689 DAG.getValueType(VA.getValVT()));
2690 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2691 break;
2692 case CCValAssign::ZExt:
2693 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2694 DAG.getValueType(VA.getValVT()));
2695 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2696 break;
2697 }
2698
Dan Gohman98ca4f22009-08-05 01:29:28 +00002699 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002700
2701 } else { // VA.isRegLoc()
2702
2703 // sanity check
2704 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002705 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002706
Stuart Hastingsf222e592011-02-28 17:17:53 +00002707 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002708
Stuart Hastingsf222e592011-02-28 17:17:53 +00002709 // Some Ins[] entries become multiple ArgLoc[] entries.
2710 // Process them only once.
2711 if (index != lastInsIndex)
2712 {
2713 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002714 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002715 // This can be changed with more analysis.
2716 // In case of tail call optimization mark all arguments mutable.
2717 // Since they could be overwritten by lowering of arguments in case of
2718 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002719 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002720 unsigned VARegSize, VARegSaveSize;
2721 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2722 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2723 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002724 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002725 int FI = MFI->CreateFixedObject(Bytes,
2726 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002727 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2728 } else {
2729 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2730 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002731
Stuart Hastingsf222e592011-02-28 17:17:53 +00002732 // Create load nodes to retrieve arguments from the stack.
2733 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2734 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2735 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002736 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002737 }
2738 lastInsIndex = index;
2739 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002740 }
2741 }
2742
2743 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002744 if (isVarArg)
2745 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002746
Dan Gohman98ca4f22009-08-05 01:29:28 +00002747 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002748}
2749
2750/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002751static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002752 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002753 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002754 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002755 // Maybe this has already been legalized into the constant pool?
2756 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002757 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002758 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002759 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002760 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002761 }
2762 }
2763 return false;
2764}
2765
Evan Chenga8e29892007-01-19 07:51:42 +00002766/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2767/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002768SDValue
2769ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002770 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002771 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002772 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002773 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002774 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002775 // Constant does not fit, try adjusting it by one?
2776 switch (CC) {
2777 default: break;
2778 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002779 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002780 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002781 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002783 }
2784 break;
2785 case ISD::SETULT:
2786 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002787 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002788 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002789 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002790 }
2791 break;
2792 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002793 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002794 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002795 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002796 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002797 }
2798 break;
2799 case ISD::SETULE:
2800 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002801 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002802 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002803 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002804 }
2805 break;
2806 }
2807 }
2808 }
2809
2810 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002811 ARMISD::NodeType CompareType;
2812 switch (CondCode) {
2813 default:
2814 CompareType = ARMISD::CMP;
2815 break;
2816 case ARMCC::EQ:
2817 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002818 // Uses only Z Flag
2819 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002820 break;
2821 }
Evan Cheng218977b2010-07-13 19:27:42 +00002822 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002823 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002824}
2825
2826/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002827SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002828ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002829 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002830 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002831 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002832 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002833 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002834 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2835 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002836}
2837
Bob Wilson79f56c92011-03-08 01:17:20 +00002838/// duplicateCmp - Glue values can have only one use, so this function
2839/// duplicates a comparison node.
2840SDValue
2841ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2842 unsigned Opc = Cmp.getOpcode();
2843 DebugLoc DL = Cmp.getDebugLoc();
2844 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2845 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2846
2847 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2848 Cmp = Cmp.getOperand(0);
2849 Opc = Cmp.getOpcode();
2850 if (Opc == ARMISD::CMPFP)
2851 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2852 else {
2853 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2854 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2855 }
2856 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2857}
2858
Bill Wendlingde2b1512010-08-11 08:43:16 +00002859SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2860 SDValue Cond = Op.getOperand(0);
2861 SDValue SelectTrue = Op.getOperand(1);
2862 SDValue SelectFalse = Op.getOperand(2);
2863 DebugLoc dl = Op.getDebugLoc();
2864
2865 // Convert:
2866 //
2867 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2868 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2869 //
2870 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2871 const ConstantSDNode *CMOVTrue =
2872 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2873 const ConstantSDNode *CMOVFalse =
2874 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2875
2876 if (CMOVTrue && CMOVFalse) {
2877 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2878 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2879
2880 SDValue True;
2881 SDValue False;
2882 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2883 True = SelectTrue;
2884 False = SelectFalse;
2885 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2886 True = SelectFalse;
2887 False = SelectTrue;
2888 }
2889
2890 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002891 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002892 SDValue ARMcc = Cond.getOperand(2);
2893 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002894 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002895 assert(True.getValueType() == VT);
2896 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002897 }
2898 }
2899 }
2900
Dan Gohmandb953892012-02-24 00:09:36 +00002901 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2902 // undefined bits before doing a full-word comparison with zero.
2903 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2904 DAG.getConstant(1, Cond.getValueType()));
2905
Bill Wendlingde2b1512010-08-11 08:43:16 +00002906 return DAG.getSelectCC(dl, Cond,
2907 DAG.getConstant(0, Cond.getValueType()),
2908 SelectTrue, SelectFalse, ISD::SETNE);
2909}
2910
Dan Gohmand858e902010-04-17 15:26:15 +00002911SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002912 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002913 SDValue LHS = Op.getOperand(0);
2914 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002915 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002916 SDValue TrueVal = Op.getOperand(2);
2917 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002918 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002919
Owen Anderson825b72b2009-08-11 20:47:22 +00002920 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002921 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002923 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002924 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 }
2926
2927 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002928 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002929
Evan Cheng218977b2010-07-13 19:27:42 +00002930 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2931 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002932 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002933 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002934 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002935 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002936 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002937 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002938 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002939 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002940 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002941 }
2942 return Result;
2943}
2944
Evan Cheng218977b2010-07-13 19:27:42 +00002945/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2946/// to morph to an integer compare sequence.
2947static bool canChangeToInt(SDValue Op, bool &SeenZero,
2948 const ARMSubtarget *Subtarget) {
2949 SDNode *N = Op.getNode();
2950 if (!N->hasOneUse())
2951 // Otherwise it requires moving the value from fp to integer registers.
2952 return false;
2953 if (!N->getNumValues())
2954 return false;
2955 EVT VT = Op.getValueType();
2956 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2957 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2958 // vmrs are very slow, e.g. cortex-a8.
2959 return false;
2960
2961 if (isFloatingPointZero(Op)) {
2962 SeenZero = true;
2963 return true;
2964 }
2965 return ISD::isNormalLoad(N);
2966}
2967
2968static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2969 if (isFloatingPointZero(Op))
2970 return DAG.getConstant(0, MVT::i32);
2971
2972 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2973 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002974 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002975 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002976 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002977
2978 llvm_unreachable("Unknown VFP cmp argument!");
2979}
2980
2981static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2982 SDValue &RetVal1, SDValue &RetVal2) {
2983 if (isFloatingPointZero(Op)) {
2984 RetVal1 = DAG.getConstant(0, MVT::i32);
2985 RetVal2 = DAG.getConstant(0, MVT::i32);
2986 return;
2987 }
2988
2989 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2990 SDValue Ptr = Ld->getBasePtr();
2991 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2992 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002993 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002994 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002995 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002996
2997 EVT PtrType = Ptr.getValueType();
2998 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2999 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3000 PtrType, Ptr, DAG.getConstant(4, PtrType));
3001 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3002 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003003 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003004 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003005 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003006 return;
3007 }
3008
3009 llvm_unreachable("Unknown VFP cmp argument!");
3010}
3011
3012/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3013/// f32 and even f64 comparisons to integer ones.
3014SDValue
3015ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3016 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003017 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003018 SDValue LHS = Op.getOperand(2);
3019 SDValue RHS = Op.getOperand(3);
3020 SDValue Dest = Op.getOperand(4);
3021 DebugLoc dl = Op.getDebugLoc();
3022
Evan Chengfc501a32012-03-01 23:27:13 +00003023 bool LHSSeenZero = false;
3024 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3025 bool RHSSeenZero = false;
3026 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3027 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003028 // If unsafe fp math optimization is enabled and there are no other uses of
3029 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003030 // to an integer comparison.
3031 if (CC == ISD::SETOEQ)
3032 CC = ISD::SETEQ;
3033 else if (CC == ISD::SETUNE)
3034 CC = ISD::SETNE;
3035
Evan Chengfc501a32012-03-01 23:27:13 +00003036 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003037 SDValue ARMcc;
3038 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003039 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3040 bitcastf32Toi32(LHS, DAG), Mask);
3041 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3042 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003043 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3044 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3045 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3046 Chain, Dest, ARMcc, CCR, Cmp);
3047 }
3048
3049 SDValue LHS1, LHS2;
3050 SDValue RHS1, RHS2;
3051 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3052 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003053 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3054 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003055 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3056 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003057 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003058 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3059 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3060 }
3061
3062 return SDValue();
3063}
3064
3065SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3066 SDValue Chain = Op.getOperand(0);
3067 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3068 SDValue LHS = Op.getOperand(2);
3069 SDValue RHS = Op.getOperand(3);
3070 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003071 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003072
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003074 SDValue ARMcc;
3075 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003077 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003078 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003079 }
3080
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003082
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003083 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003084 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3085 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3086 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3087 if (Result.getNode())
3088 return Result;
3089 }
3090
Evan Chenga8e29892007-01-19 07:51:42 +00003091 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003092 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003093
Evan Cheng218977b2010-07-13 19:27:42 +00003094 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3095 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003096 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003097 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003098 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003099 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003100 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003101 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3102 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003103 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003104 }
3105 return Res;
3106}
3107
Dan Gohmand858e902010-04-17 15:26:15 +00003108SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003109 SDValue Chain = Op.getOperand(0);
3110 SDValue Table = Op.getOperand(1);
3111 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003112 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003113
Owen Andersone50ed302009-08-10 22:56:29 +00003114 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003115 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3116 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003117 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003118 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003120 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3121 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003122 if (Subtarget->isThumb2()) {
3123 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3124 // which does another jump to the destination. This also makes it easier
3125 // to translate it to TBB / TBH later.
3126 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003128 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003129 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003130 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003131 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003132 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003133 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003134 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003135 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003137 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003138 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003139 MachinePointerInfo::getJumpTable(),
3140 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003141 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003142 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003143 }
Evan Chenga8e29892007-01-19 07:51:42 +00003144}
3145
Eli Friedman14e809c2011-11-09 23:36:02 +00003146static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003147 EVT VT = Op.getValueType();
3148 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003149
James Molloy873fd5f2012-02-20 09:24:05 +00003150 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3151 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3152 return Op;
3153 return DAG.UnrollVectorOp(Op.getNode());
3154 }
3155
3156 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3157 "Invalid type for custom lowering!");
3158 if (VT != MVT::v4i16)
3159 return DAG.UnrollVectorOp(Op.getNode());
3160
3161 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3162 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003163}
3164
Bob Wilson76a312b2010-03-19 22:51:32 +00003165static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003166 EVT VT = Op.getValueType();
3167 if (VT.isVector())
3168 return LowerVectorFP_TO_INT(Op, DAG);
3169
Bob Wilson76a312b2010-03-19 22:51:32 +00003170 DebugLoc dl = Op.getDebugLoc();
3171 unsigned Opc;
3172
3173 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003174 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003175 case ISD::FP_TO_SINT:
3176 Opc = ARMISD::FTOSI;
3177 break;
3178 case ISD::FP_TO_UINT:
3179 Opc = ARMISD::FTOUI;
3180 break;
3181 }
3182 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003183 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003184}
3185
Cameron Zwarich3007d332011-03-29 21:41:55 +00003186static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3187 EVT VT = Op.getValueType();
3188 DebugLoc dl = Op.getDebugLoc();
3189
Eli Friedman14e809c2011-11-09 23:36:02 +00003190 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3191 if (VT.getVectorElementType() == MVT::f32)
3192 return Op;
3193 return DAG.UnrollVectorOp(Op.getNode());
3194 }
3195
Duncan Sands1f6a3292011-08-12 14:54:45 +00003196 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3197 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003198 if (VT != MVT::v4f32)
3199 return DAG.UnrollVectorOp(Op.getNode());
3200
3201 unsigned CastOpc;
3202 unsigned Opc;
3203 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003204 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003205 case ISD::SINT_TO_FP:
3206 CastOpc = ISD::SIGN_EXTEND;
3207 Opc = ISD::SINT_TO_FP;
3208 break;
3209 case ISD::UINT_TO_FP:
3210 CastOpc = ISD::ZERO_EXTEND;
3211 Opc = ISD::UINT_TO_FP;
3212 break;
3213 }
3214
3215 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3216 return DAG.getNode(Opc, dl, VT, Op);
3217}
3218
Bob Wilson76a312b2010-03-19 22:51:32 +00003219static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3220 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003221 if (VT.isVector())
3222 return LowerVectorINT_TO_FP(Op, DAG);
3223
Bob Wilson76a312b2010-03-19 22:51:32 +00003224 DebugLoc dl = Op.getDebugLoc();
3225 unsigned Opc;
3226
3227 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003228 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003229 case ISD::SINT_TO_FP:
3230 Opc = ARMISD::SITOF;
3231 break;
3232 case ISD::UINT_TO_FP:
3233 Opc = ARMISD::UITOF;
3234 break;
3235 }
3236
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003237 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003238 return DAG.getNode(Opc, dl, VT, Op);
3239}
3240
Evan Cheng515fe3a2010-07-08 02:08:50 +00003241SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003242 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SDValue Tmp0 = Op.getOperand(0);
3244 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003245 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003246 EVT VT = Op.getValueType();
3247 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003248 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3249 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3250 bool UseNEON = !InGPR && Subtarget->hasNEON();
3251
3252 if (UseNEON) {
3253 // Use VBSL to copy the sign bit.
3254 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3255 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3256 DAG.getTargetConstant(EncodedVal, MVT::i32));
3257 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3258 if (VT == MVT::f64)
3259 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3260 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3261 DAG.getConstant(32, MVT::i32));
3262 else /*if (VT == MVT::f32)*/
3263 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3264 if (SrcVT == MVT::f32) {
3265 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3266 if (VT == MVT::f64)
3267 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3268 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3269 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003270 } else if (VT == MVT::f32)
3271 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3272 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3273 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003274 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3275 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3276
3277 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3278 MVT::i32);
3279 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3280 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3281 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003282
Evan Chenge573fb32011-02-23 02:24:55 +00003283 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3284 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3285 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003286 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003287 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3288 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3289 DAG.getConstant(0, MVT::i32));
3290 } else {
3291 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3292 }
3293
3294 return Res;
3295 }
Evan Chengc143dd42011-02-11 02:28:55 +00003296
3297 // Bitcast operand 1 to i32.
3298 if (SrcVT == MVT::f64)
3299 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3300 &Tmp1, 1).getValue(1);
3301 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3302
Evan Chenge573fb32011-02-23 02:24:55 +00003303 // Or in the signbit with integer operations.
3304 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3305 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3306 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3307 if (VT == MVT::f32) {
3308 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3309 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3310 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3311 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003312 }
3313
Evan Chenge573fb32011-02-23 02:24:55 +00003314 // f64: Or the high part with signbit and then combine two parts.
3315 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3316 &Tmp0, 1);
3317 SDValue Lo = Tmp0.getValue(0);
3318 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3319 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3320 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003321}
3322
Evan Cheng2457f2c2010-05-22 01:47:14 +00003323SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3324 MachineFunction &MF = DAG.getMachineFunction();
3325 MachineFrameInfo *MFI = MF.getFrameInfo();
3326 MFI->setReturnAddressIsTaken(true);
3327
3328 EVT VT = Op.getValueType();
3329 DebugLoc dl = Op.getDebugLoc();
3330 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3331 if (Depth) {
3332 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3333 SDValue Offset = DAG.getConstant(4, MVT::i32);
3334 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3335 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003336 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003337 }
3338
3339 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003340 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003341 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3342}
3343
Dan Gohmand858e902010-04-17 15:26:15 +00003344SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003345 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3346 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003347
Owen Andersone50ed302009-08-10 22:56:29 +00003348 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003349 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3350 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003351 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003352 ? ARM::R7 : ARM::R11;
3353 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3354 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003355 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3356 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003357 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003358 return FrameAddr;
3359}
3360
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003361/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003362/// expand a bit convert where either the source or destination type is i64 to
3363/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3364/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3365/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003366static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003367 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3368 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003369 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003370
Bob Wilson9f3f0612010-04-17 05:30:19 +00003371 // This function is only supposed to be called for i64 types, either as the
3372 // source or destination of the bit convert.
3373 EVT SrcVT = Op.getValueType();
3374 EVT DstVT = N->getValueType(0);
3375 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003376 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003377
Bob Wilson9f3f0612010-04-17 05:30:19 +00003378 // Turn i64->f64 into VMOVDRR.
3379 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3381 DAG.getConstant(0, MVT::i32));
3382 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3383 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003384 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003385 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003386 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003387
Jim Grosbache5165492009-11-09 00:11:35 +00003388 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003389 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3390 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3391 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3392 // Merge the pieces into a single i64 value.
3393 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3394 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003395
Bob Wilson9f3f0612010-04-17 05:30:19 +00003396 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003397}
3398
Bob Wilson5bafff32009-06-22 23:27:02 +00003399/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003400/// Zero vectors are used to represent vector negation and in those cases
3401/// will be implemented with the NEON VNEG instruction. However, VNEG does
3402/// not support i64 elements, so sometimes the zero vectors will need to be
3403/// explicitly constructed. Regardless, use a canonical VMOV to create the
3404/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003405static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003406 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003407 // The canonical modified immediate encoding of a zero vector is....0!
3408 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3409 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3410 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003411 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003412}
3413
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003414/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3415/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003416SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3417 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003418 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3419 EVT VT = Op.getValueType();
3420 unsigned VTBits = VT.getSizeInBits();
3421 DebugLoc dl = Op.getDebugLoc();
3422 SDValue ShOpLo = Op.getOperand(0);
3423 SDValue ShOpHi = Op.getOperand(1);
3424 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003425 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003426 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003427
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003428 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3429
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003430 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3431 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3432 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3433 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3434 DAG.getConstant(VTBits, MVT::i32));
3435 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3436 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003437 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003438
3439 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3440 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003441 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003442 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003443 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003444 CCR, Cmp);
3445
3446 SDValue Ops[2] = { Lo, Hi };
3447 return DAG.getMergeValues(Ops, 2, dl);
3448}
3449
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003450/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3451/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003452SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3453 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003454 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3455 EVT VT = Op.getValueType();
3456 unsigned VTBits = VT.getSizeInBits();
3457 DebugLoc dl = Op.getDebugLoc();
3458 SDValue ShOpLo = Op.getOperand(0);
3459 SDValue ShOpHi = Op.getOperand(1);
3460 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003461 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003462
3463 assert(Op.getOpcode() == ISD::SHL_PARTS);
3464 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3465 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3466 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3467 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3468 DAG.getConstant(VTBits, MVT::i32));
3469 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3470 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3471
3472 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3473 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3474 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003475 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003476 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003477 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003478 CCR, Cmp);
3479
3480 SDValue Ops[2] = { Lo, Hi };
3481 return DAG.getMergeValues(Ops, 2, dl);
3482}
3483
Jim Grosbach4725ca72010-09-08 03:54:02 +00003484SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003485 SelectionDAG &DAG) const {
3486 // The rounding mode is in bits 23:22 of the FPSCR.
3487 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3488 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3489 // so that the shift + and get folded into a bitfield extract.
3490 DebugLoc dl = Op.getDebugLoc();
3491 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3492 DAG.getConstant(Intrinsic::arm_get_fpscr,
3493 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003494 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003495 DAG.getConstant(1U << 22, MVT::i32));
3496 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3497 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003498 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003499 DAG.getConstant(3, MVT::i32));
3500}
3501
Jim Grosbach3482c802010-01-18 19:58:49 +00003502static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3503 const ARMSubtarget *ST) {
3504 EVT VT = N->getValueType(0);
3505 DebugLoc dl = N->getDebugLoc();
3506
3507 if (!ST->hasV6T2Ops())
3508 return SDValue();
3509
3510 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3511 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3512}
3513
Bob Wilson5bafff32009-06-22 23:27:02 +00003514static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3515 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003516 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003517 DebugLoc dl = N->getDebugLoc();
3518
Bob Wilsond5448bb2010-11-18 21:16:28 +00003519 if (!VT.isVector())
3520 return SDValue();
3521
Bob Wilson5bafff32009-06-22 23:27:02 +00003522 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003523 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003524
Bob Wilsond5448bb2010-11-18 21:16:28 +00003525 // Left shifts translate directly to the vshiftu intrinsic.
3526 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003527 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003528 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3529 N->getOperand(0), N->getOperand(1));
3530
3531 assert((N->getOpcode() == ISD::SRA ||
3532 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3533
3534 // NEON uses the same intrinsics for both left and right shifts. For
3535 // right shifts, the shift amounts are negative, so negate the vector of
3536 // shift amounts.
3537 EVT ShiftVT = N->getOperand(1).getValueType();
3538 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3539 getZeroVector(ShiftVT, DAG, dl),
3540 N->getOperand(1));
3541 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3542 Intrinsic::arm_neon_vshifts :
3543 Intrinsic::arm_neon_vshiftu);
3544 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3545 DAG.getConstant(vshiftInt, MVT::i32),
3546 N->getOperand(0), NegatedCount);
3547}
3548
3549static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3550 const ARMSubtarget *ST) {
3551 EVT VT = N->getValueType(0);
3552 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003553
Eli Friedmance392eb2009-08-22 03:13:10 +00003554 // We can get here for a node like i32 = ISD::SHL i32, i64
3555 if (VT != MVT::i64)
3556 return SDValue();
3557
3558 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003559 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003560
Chris Lattner27a6c732007-11-24 07:07:01 +00003561 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3562 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003563 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003564 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003565
Chris Lattner27a6c732007-11-24 07:07:01 +00003566 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003567 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003568
Chris Lattner27a6c732007-11-24 07:07:01 +00003569 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003571 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003573 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003574
Chris Lattner27a6c732007-11-24 07:07:01 +00003575 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3576 // captures the result into a carry flag.
3577 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003578 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003579
Chris Lattner27a6c732007-11-24 07:07:01 +00003580 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003581 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003582
Chris Lattner27a6c732007-11-24 07:07:01 +00003583 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003584 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003585}
3586
Bob Wilson5bafff32009-06-22 23:27:02 +00003587static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3588 SDValue TmpOp0, TmpOp1;
3589 bool Invert = false;
3590 bool Swap = false;
3591 unsigned Opc = 0;
3592
3593 SDValue Op0 = Op.getOperand(0);
3594 SDValue Op1 = Op.getOperand(1);
3595 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003596 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003597 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3598 DebugLoc dl = Op.getDebugLoc();
3599
3600 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3601 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003602 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003603 case ISD::SETUNE:
3604 case ISD::SETNE: Invert = true; // Fallthrough
3605 case ISD::SETOEQ:
3606 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3607 case ISD::SETOLT:
3608 case ISD::SETLT: Swap = true; // Fallthrough
3609 case ISD::SETOGT:
3610 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3611 case ISD::SETOLE:
3612 case ISD::SETLE: Swap = true; // Fallthrough
3613 case ISD::SETOGE:
3614 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3615 case ISD::SETUGE: Swap = true; // Fallthrough
3616 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3617 case ISD::SETUGT: Swap = true; // Fallthrough
3618 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3619 case ISD::SETUEQ: Invert = true; // Fallthrough
3620 case ISD::SETONE:
3621 // Expand this to (OLT | OGT).
3622 TmpOp0 = Op0;
3623 TmpOp1 = Op1;
3624 Opc = ISD::OR;
3625 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3626 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3627 break;
3628 case ISD::SETUO: Invert = true; // Fallthrough
3629 case ISD::SETO:
3630 // Expand this to (OLT | OGE).
3631 TmpOp0 = Op0;
3632 TmpOp1 = Op1;
3633 Opc = ISD::OR;
3634 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3635 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3636 break;
3637 }
3638 } else {
3639 // Integer comparisons.
3640 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003641 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003642 case ISD::SETNE: Invert = true;
3643 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3644 case ISD::SETLT: Swap = true;
3645 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3646 case ISD::SETLE: Swap = true;
3647 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3648 case ISD::SETULT: Swap = true;
3649 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3650 case ISD::SETULE: Swap = true;
3651 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3652 }
3653
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003654 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003655 if (Opc == ARMISD::VCEQ) {
3656
3657 SDValue AndOp;
3658 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3659 AndOp = Op0;
3660 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3661 AndOp = Op1;
3662
3663 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003664 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003665 AndOp = AndOp.getOperand(0);
3666
3667 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3668 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003669 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3670 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003671 Invert = !Invert;
3672 }
3673 }
3674 }
3675
3676 if (Swap)
3677 std::swap(Op0, Op1);
3678
Owen Andersonc24cb352010-11-08 23:21:22 +00003679 // If one of the operands is a constant vector zero, attempt to fold the
3680 // comparison to a specialized compare-against-zero form.
3681 SDValue SingleOp;
3682 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3683 SingleOp = Op0;
3684 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3685 if (Opc == ARMISD::VCGE)
3686 Opc = ARMISD::VCLEZ;
3687 else if (Opc == ARMISD::VCGT)
3688 Opc = ARMISD::VCLTZ;
3689 SingleOp = Op1;
3690 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003691
Owen Andersonc24cb352010-11-08 23:21:22 +00003692 SDValue Result;
3693 if (SingleOp.getNode()) {
3694 switch (Opc) {
3695 case ARMISD::VCEQ:
3696 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3697 case ARMISD::VCGE:
3698 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3699 case ARMISD::VCLEZ:
3700 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3701 case ARMISD::VCGT:
3702 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3703 case ARMISD::VCLTZ:
3704 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3705 default:
3706 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3707 }
3708 } else {
3709 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3710 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003711
3712 if (Invert)
3713 Result = DAG.getNOT(dl, Result, VT);
3714
3715 return Result;
3716}
3717
Bob Wilsond3c42842010-06-14 22:19:57 +00003718/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3719/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003720/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003721static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3722 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003723 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003724 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003725
Bob Wilson827b2102010-06-15 19:05:35 +00003726 // SplatBitSize is set to the smallest size that splats the vector, so a
3727 // zero vector will always have SplatBitSize == 8. However, NEON modified
3728 // immediate instructions others than VMOV do not support the 8-bit encoding
3729 // of a zero vector, and the default encoding of zero is supposed to be the
3730 // 32-bit version.
3731 if (SplatBits == 0)
3732 SplatBitSize = 32;
3733
Bob Wilson5bafff32009-06-22 23:27:02 +00003734 switch (SplatBitSize) {
3735 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003736 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003737 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003738 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003739 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003740 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003741 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003742 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003743 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003744
3745 case 16:
3746 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003747 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003748 if ((SplatBits & ~0xff) == 0) {
3749 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003750 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003751 Imm = SplatBits;
3752 break;
3753 }
3754 if ((SplatBits & ~0xff00) == 0) {
3755 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003756 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003757 Imm = SplatBits >> 8;
3758 break;
3759 }
3760 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003761
3762 case 32:
3763 // NEON's 32-bit VMOV supports splat values where:
3764 // * only one byte is nonzero, or
3765 // * the least significant byte is 0xff and the second byte is nonzero, or
3766 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003767 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003768 if ((SplatBits & ~0xff) == 0) {
3769 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003770 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003771 Imm = SplatBits;
3772 break;
3773 }
3774 if ((SplatBits & ~0xff00) == 0) {
3775 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003776 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003777 Imm = SplatBits >> 8;
3778 break;
3779 }
3780 if ((SplatBits & ~0xff0000) == 0) {
3781 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003782 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003783 Imm = SplatBits >> 16;
3784 break;
3785 }
3786 if ((SplatBits & ~0xff000000) == 0) {
3787 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003788 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003789 Imm = SplatBits >> 24;
3790 break;
3791 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003792
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003793 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3794 if (type == OtherModImm) return SDValue();
3795
Bob Wilson5bafff32009-06-22 23:27:02 +00003796 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003797 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3798 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003799 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003800 Imm = SplatBits >> 8;
3801 SplatBits |= 0xff;
3802 break;
3803 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003804
3805 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003806 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3807 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003808 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003809 Imm = SplatBits >> 16;
3810 SplatBits |= 0xffff;
3811 break;
3812 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003813
3814 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3815 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3816 // VMOV.I32. A (very) minor optimization would be to replicate the value
3817 // and fall through here to test for a valid 64-bit splat. But, then the
3818 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003819 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003820
3821 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003822 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003823 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003824 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003825 uint64_t BitMask = 0xff;
3826 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 unsigned ImmMask = 1;
3828 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003829 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003830 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003831 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003832 Imm |= ImmMask;
3833 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003834 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003835 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003836 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003837 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003838 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003839 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003840 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003841 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003842 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003843 break;
3844 }
3845
Bob Wilson1a913ed2010-06-11 21:34:50 +00003846 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003847 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003848 }
3849
Bob Wilsoncba270d2010-07-13 21:16:48 +00003850 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3851 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003852}
3853
Lang Hamesc0a9f822012-03-29 21:56:11 +00003854SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3855 const ARMSubtarget *ST) const {
3856 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3857 return SDValue();
3858
3859 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3860 assert(Op.getValueType() == MVT::f32 &&
3861 "ConstantFP custom lowering should only occur for f32.");
3862
3863 // Try splatting with a VMOV.f32...
3864 APFloat FPVal = CFP->getValueAPF();
3865 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3866 if (ImmVal != -1) {
3867 DebugLoc DL = Op.getDebugLoc();
3868 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3869 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3870 NewVal);
3871 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3872 DAG.getConstant(0, MVT::i32));
3873 }
3874
3875 // If that fails, try a VMOV.i32
3876 EVT VMovVT;
3877 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3878 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3879 VMOVModImm);
3880 if (NewVal != SDValue()) {
3881 DebugLoc DL = Op.getDebugLoc();
3882 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3883 NewVal);
3884 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3885 VecConstant);
3886 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3887 DAG.getConstant(0, MVT::i32));
3888 }
3889
3890 // Finally, try a VMVN.i32
3891 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3892 VMVNModImm);
3893 if (NewVal != SDValue()) {
3894 DebugLoc DL = Op.getDebugLoc();
3895 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3896 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3897 VecConstant);
3898 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3899 DAG.getConstant(0, MVT::i32));
3900 }
3901
3902 return SDValue();
3903}
3904
3905
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003906static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003907 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003908 unsigned NumElts = VT.getVectorNumElements();
3909 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003910
3911 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3912 if (M[0] < 0)
3913 return false;
3914
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003915 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003916
3917 // If this is a VEXT shuffle, the immediate value is the index of the first
3918 // element. The other shuffle indices must be the successive elements after
3919 // the first one.
3920 unsigned ExpectedElt = Imm;
3921 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003922 // Increment the expected index. If it wraps around, it may still be
3923 // a VEXT but the source vectors must be swapped.
3924 ExpectedElt += 1;
3925 if (ExpectedElt == NumElts * 2) {
3926 ExpectedElt = 0;
3927 ReverseVEXT = true;
3928 }
3929
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003930 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003931 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003932 return false;
3933 }
3934
3935 // Adjust the index value if the source operands will be swapped.
3936 if (ReverseVEXT)
3937 Imm -= NumElts;
3938
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003939 return true;
3940}
3941
Bob Wilson8bb9e482009-07-26 00:39:34 +00003942/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3943/// instruction with the specified blocksize. (The order of the elements
3944/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003945static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003946 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3947 "Only possible block sizes for VREV are: 16, 32, 64");
3948
Bob Wilson8bb9e482009-07-26 00:39:34 +00003949 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003950 if (EltSz == 64)
3951 return false;
3952
3953 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003954 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003955 // If the first shuffle index is UNDEF, be optimistic.
3956 if (M[0] < 0)
3957 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003958
3959 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3960 return false;
3961
3962 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003963 if (M[i] < 0) continue; // ignore UNDEF indices
3964 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003965 return false;
3966 }
3967
3968 return true;
3969}
3970
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003971static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003972 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3973 // range, then 0 is placed into the resulting vector. So pretty much any mask
3974 // of 8 elements can work here.
3975 return VT == MVT::v8i8 && M.size() == 8;
3976}
3977
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003978static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003979 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3980 if (EltSz == 64)
3981 return false;
3982
Bob Wilsonc692cb72009-08-21 20:54:19 +00003983 unsigned NumElts = VT.getVectorNumElements();
3984 WhichResult = (M[0] == 0 ? 0 : 1);
3985 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003986 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3987 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003988 return false;
3989 }
3990 return true;
3991}
3992
Bob Wilson324f4f12009-12-03 06:40:55 +00003993/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3994/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3995/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003996static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003997 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3998 if (EltSz == 64)
3999 return false;
4000
4001 unsigned NumElts = VT.getVectorNumElements();
4002 WhichResult = (M[0] == 0 ? 0 : 1);
4003 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004004 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4005 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004006 return false;
4007 }
4008 return true;
4009}
4010
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004011static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004012 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4013 if (EltSz == 64)
4014 return false;
4015
Bob Wilsonc692cb72009-08-21 20:54:19 +00004016 unsigned NumElts = VT.getVectorNumElements();
4017 WhichResult = (M[0] == 0 ? 0 : 1);
4018 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004019 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004020 if ((unsigned) M[i] != 2 * i + WhichResult)
4021 return false;
4022 }
4023
4024 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004025 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004026 return false;
4027
4028 return true;
4029}
4030
Bob Wilson324f4f12009-12-03 06:40:55 +00004031/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4032/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4033/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004034static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004035 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4036 if (EltSz == 64)
4037 return false;
4038
4039 unsigned Half = VT.getVectorNumElements() / 2;
4040 WhichResult = (M[0] == 0 ? 0 : 1);
4041 for (unsigned j = 0; j != 2; ++j) {
4042 unsigned Idx = WhichResult;
4043 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004044 int MIdx = M[i + j * Half];
4045 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004046 return false;
4047 Idx += 2;
4048 }
4049 }
4050
4051 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4052 if (VT.is64BitVector() && EltSz == 32)
4053 return false;
4054
4055 return true;
4056}
4057
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004058static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004059 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4060 if (EltSz == 64)
4061 return false;
4062
Bob Wilsonc692cb72009-08-21 20:54:19 +00004063 unsigned NumElts = VT.getVectorNumElements();
4064 WhichResult = (M[0] == 0 ? 0 : 1);
4065 unsigned Idx = WhichResult * NumElts / 2;
4066 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004067 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4068 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004069 return false;
4070 Idx += 1;
4071 }
4072
4073 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004074 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004075 return false;
4076
4077 return true;
4078}
4079
Bob Wilson324f4f12009-12-03 06:40:55 +00004080/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4081/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4082/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004083static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004084 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4085 if (EltSz == 64)
4086 return false;
4087
4088 unsigned NumElts = VT.getVectorNumElements();
4089 WhichResult = (M[0] == 0 ? 0 : 1);
4090 unsigned Idx = WhichResult * NumElts / 2;
4091 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004092 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4093 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004094 return false;
4095 Idx += 1;
4096 }
4097
4098 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4099 if (VT.is64BitVector() && EltSz == 32)
4100 return false;
4101
4102 return true;
4103}
4104
Dale Johannesenf630c712010-07-29 20:10:08 +00004105// If N is an integer constant that can be moved into a register in one
4106// instruction, return an SDValue of such a constant (will become a MOV
4107// instruction). Otherwise return null.
4108static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4109 const ARMSubtarget *ST, DebugLoc dl) {
4110 uint64_t Val;
4111 if (!isa<ConstantSDNode>(N))
4112 return SDValue();
4113 Val = cast<ConstantSDNode>(N)->getZExtValue();
4114
4115 if (ST->isThumb1Only()) {
4116 if (Val <= 255 || ~Val <= 255)
4117 return DAG.getConstant(Val, MVT::i32);
4118 } else {
4119 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4120 return DAG.getConstant(Val, MVT::i32);
4121 }
4122 return SDValue();
4123}
4124
Bob Wilson5bafff32009-06-22 23:27:02 +00004125// If this is a case we can't handle, return null and let the default
4126// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004127SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4128 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004129 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004130 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004131 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004132
4133 APInt SplatBits, SplatUndef;
4134 unsigned SplatBitSize;
4135 bool HasAnyUndefs;
4136 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004137 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004138 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004139 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004140 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004141 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004142 DAG, VmovVT, VT.is128BitVector(),
4143 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004144 if (Val.getNode()) {
4145 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004146 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004147 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004148
4149 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004150 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004151 Val = isNEONModifiedImm(NegatedImm,
4152 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004153 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004154 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004155 if (Val.getNode()) {
4156 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004157 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004158 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004159
4160 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004161 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004162 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004163 if (ImmVal != -1) {
4164 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4165 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4166 }
4167 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004168 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004169 }
4170
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004171 // Scan through the operands to see if only one value is used.
4172 unsigned NumElts = VT.getVectorNumElements();
4173 bool isOnlyLowElement = true;
4174 bool usesOnlyOneValue = true;
4175 bool isConstant = true;
4176 SDValue Value;
4177 for (unsigned i = 0; i < NumElts; ++i) {
4178 SDValue V = Op.getOperand(i);
4179 if (V.getOpcode() == ISD::UNDEF)
4180 continue;
4181 if (i > 0)
4182 isOnlyLowElement = false;
4183 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4184 isConstant = false;
4185
4186 if (!Value.getNode())
4187 Value = V;
4188 else if (V != Value)
4189 usesOnlyOneValue = false;
4190 }
4191
4192 if (!Value.getNode())
4193 return DAG.getUNDEF(VT);
4194
4195 if (isOnlyLowElement)
4196 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4197
Dale Johannesenf630c712010-07-29 20:10:08 +00004198 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4199
Dale Johannesen575cd142010-10-19 20:00:17 +00004200 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4201 // i32 and try again.
4202 if (usesOnlyOneValue && EltSize <= 32) {
4203 if (!isConstant)
4204 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4205 if (VT.getVectorElementType().isFloatingPoint()) {
4206 SmallVector<SDValue, 8> Ops;
4207 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004208 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004209 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004210 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4211 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004212 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4213 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004214 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004215 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004216 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4217 if (Val.getNode())
4218 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004219 }
4220
4221 // If all elements are constants and the case above didn't get hit, fall back
4222 // to the default expansion, which will generate a load from the constant
4223 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004224 if (isConstant)
4225 return SDValue();
4226
Bob Wilson11a1dff2011-01-07 21:37:30 +00004227 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4228 if (NumElts >= 4) {
4229 SDValue shuffle = ReconstructShuffle(Op, DAG);
4230 if (shuffle != SDValue())
4231 return shuffle;
4232 }
4233
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004234 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004235 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4236 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004237 if (EltSize >= 32) {
4238 // Do the expansion with floating-point types, since that is what the VFP
4239 // registers are defined to use, and since i64 is not legal.
4240 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4241 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004242 SmallVector<SDValue, 8> Ops;
4243 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004244 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004245 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004246 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004247 }
4248
4249 return SDValue();
4250}
4251
Bob Wilson11a1dff2011-01-07 21:37:30 +00004252// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004253// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004254SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4255 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004256 DebugLoc dl = Op.getDebugLoc();
4257 EVT VT = Op.getValueType();
4258 unsigned NumElts = VT.getVectorNumElements();
4259
4260 SmallVector<SDValue, 2> SourceVecs;
4261 SmallVector<unsigned, 2> MinElts;
4262 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004263
Bob Wilson11a1dff2011-01-07 21:37:30 +00004264 for (unsigned i = 0; i < NumElts; ++i) {
4265 SDValue V = Op.getOperand(i);
4266 if (V.getOpcode() == ISD::UNDEF)
4267 continue;
4268 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4269 // A shuffle can only come from building a vector from various
4270 // elements of other vectors.
4271 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004272 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4273 VT.getVectorElementType()) {
4274 // This code doesn't know how to handle shuffles where the vector
4275 // element types do not match (this happens because type legalization
4276 // promotes the return type of EXTRACT_VECTOR_ELT).
4277 // FIXME: It might be appropriate to extend this code to handle
4278 // mismatched types.
4279 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004280 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004281
Bob Wilson11a1dff2011-01-07 21:37:30 +00004282 // Record this extraction against the appropriate vector if possible...
4283 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004284 // If the element number isn't a constant, we can't effectively
4285 // analyze what's going on.
4286 if (!isa<ConstantSDNode>(V.getOperand(1)))
4287 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004288 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4289 bool FoundSource = false;
4290 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4291 if (SourceVecs[j] == SourceVec) {
4292 if (MinElts[j] > EltNo)
4293 MinElts[j] = EltNo;
4294 if (MaxElts[j] < EltNo)
4295 MaxElts[j] = EltNo;
4296 FoundSource = true;
4297 break;
4298 }
4299 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004300
Bob Wilson11a1dff2011-01-07 21:37:30 +00004301 // Or record a new source if not...
4302 if (!FoundSource) {
4303 SourceVecs.push_back(SourceVec);
4304 MinElts.push_back(EltNo);
4305 MaxElts.push_back(EltNo);
4306 }
4307 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004308
Bob Wilson11a1dff2011-01-07 21:37:30 +00004309 // Currently only do something sane when at most two source vectors
4310 // involved.
4311 if (SourceVecs.size() > 2)
4312 return SDValue();
4313
4314 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4315 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004316
Bob Wilson11a1dff2011-01-07 21:37:30 +00004317 // This loop extracts the usage patterns of the source vectors
4318 // and prepares appropriate SDValues for a shuffle if possible.
4319 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4320 if (SourceVecs[i].getValueType() == VT) {
4321 // No VEXT necessary
4322 ShuffleSrcs[i] = SourceVecs[i];
4323 VEXTOffsets[i] = 0;
4324 continue;
4325 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4326 // It probably isn't worth padding out a smaller vector just to
4327 // break it down again in a shuffle.
4328 return SDValue();
4329 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004330
Bob Wilson11a1dff2011-01-07 21:37:30 +00004331 // Since only 64-bit and 128-bit vectors are legal on ARM and
4332 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004333 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4334 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004335
Bob Wilson11a1dff2011-01-07 21:37:30 +00004336 if (MaxElts[i] - MinElts[i] >= NumElts) {
4337 // Span too large for a VEXT to cope
4338 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004339 }
4340
Bob Wilson11a1dff2011-01-07 21:37:30 +00004341 if (MinElts[i] >= NumElts) {
4342 // The extraction can just take the second half
4343 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004344 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4345 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004346 DAG.getIntPtrConstant(NumElts));
4347 } else if (MaxElts[i] < NumElts) {
4348 // The extraction can just take the first half
4349 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004350 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4351 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004352 DAG.getIntPtrConstant(0));
4353 } else {
4354 // An actual VEXT is needed
4355 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004356 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4357 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004358 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004359 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4360 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004361 DAG.getIntPtrConstant(NumElts));
4362 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4363 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4364 }
4365 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004366
Bob Wilson11a1dff2011-01-07 21:37:30 +00004367 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004368
Bob Wilson11a1dff2011-01-07 21:37:30 +00004369 for (unsigned i = 0; i < NumElts; ++i) {
4370 SDValue Entry = Op.getOperand(i);
4371 if (Entry.getOpcode() == ISD::UNDEF) {
4372 Mask.push_back(-1);
4373 continue;
4374 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004375
Bob Wilson11a1dff2011-01-07 21:37:30 +00004376 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004377 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4378 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004379 if (ExtractVec == SourceVecs[0]) {
4380 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4381 } else {
4382 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4383 }
4384 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004385
Bob Wilson11a1dff2011-01-07 21:37:30 +00004386 // Final check before we try to produce nonsense...
4387 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004388 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4389 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004390
Bob Wilson11a1dff2011-01-07 21:37:30 +00004391 return SDValue();
4392}
4393
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004394/// isShuffleMaskLegal - Targets can use this to indicate that they only
4395/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4396/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4397/// are assumed to be legal.
4398bool
4399ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4400 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004401 if (VT.getVectorNumElements() == 4 &&
4402 (VT.is128BitVector() || VT.is64BitVector())) {
4403 unsigned PFIndexes[4];
4404 for (unsigned i = 0; i != 4; ++i) {
4405 if (M[i] < 0)
4406 PFIndexes[i] = 8;
4407 else
4408 PFIndexes[i] = M[i];
4409 }
4410
4411 // Compute the index in the perfect shuffle table.
4412 unsigned PFTableIndex =
4413 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4414 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4415 unsigned Cost = (PFEntry >> 30);
4416
4417 if (Cost <= 4)
4418 return true;
4419 }
4420
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004421 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004422 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004423
Bob Wilson53dd2452010-06-07 23:53:38 +00004424 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4425 return (EltSize >= 32 ||
4426 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004427 isVREVMask(M, VT, 64) ||
4428 isVREVMask(M, VT, 32) ||
4429 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004430 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004431 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004432 isVTRNMask(M, VT, WhichResult) ||
4433 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004434 isVZIPMask(M, VT, WhichResult) ||
4435 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4436 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4437 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004438}
4439
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004440/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4441/// the specified operations to build the shuffle.
4442static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4443 SDValue RHS, SelectionDAG &DAG,
4444 DebugLoc dl) {
4445 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4446 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4447 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4448
4449 enum {
4450 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4451 OP_VREV,
4452 OP_VDUP0,
4453 OP_VDUP1,
4454 OP_VDUP2,
4455 OP_VDUP3,
4456 OP_VEXT1,
4457 OP_VEXT2,
4458 OP_VEXT3,
4459 OP_VUZPL, // VUZP, left result
4460 OP_VUZPR, // VUZP, right result
4461 OP_VZIPL, // VZIP, left result
4462 OP_VZIPR, // VZIP, right result
4463 OP_VTRNL, // VTRN, left result
4464 OP_VTRNR // VTRN, right result
4465 };
4466
4467 if (OpNum == OP_COPY) {
4468 if (LHSID == (1*9+2)*9+3) return LHS;
4469 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4470 return RHS;
4471 }
4472
4473 SDValue OpLHS, OpRHS;
4474 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4475 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4476 EVT VT = OpLHS.getValueType();
4477
4478 switch (OpNum) {
4479 default: llvm_unreachable("Unknown shuffle opcode!");
4480 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004481 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004482 if (VT.getVectorElementType() == MVT::i32 ||
4483 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004484 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4485 // vrev <4 x i16> -> VREV32
4486 if (VT.getVectorElementType() == MVT::i16)
4487 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4488 // vrev <4 x i8> -> VREV16
4489 assert(VT.getVectorElementType() == MVT::i8);
4490 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004491 case OP_VDUP0:
4492 case OP_VDUP1:
4493 case OP_VDUP2:
4494 case OP_VDUP3:
4495 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004496 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004497 case OP_VEXT1:
4498 case OP_VEXT2:
4499 case OP_VEXT3:
4500 return DAG.getNode(ARMISD::VEXT, dl, VT,
4501 OpLHS, OpRHS,
4502 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4503 case OP_VUZPL:
4504 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004505 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004506 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4507 case OP_VZIPL:
4508 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004509 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004510 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4511 case OP_VTRNL:
4512 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004513 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4514 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004515 }
4516}
4517
Bill Wendling69a05a72011-03-14 23:02:38 +00004518static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004519 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004520 SelectionDAG &DAG) {
4521 // Check to see if we can use the VTBL instruction.
4522 SDValue V1 = Op.getOperand(0);
4523 SDValue V2 = Op.getOperand(1);
4524 DebugLoc DL = Op.getDebugLoc();
4525
4526 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004527 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004528 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4529 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4530
4531 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4532 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4533 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4534 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004535
Owen Anderson76706012011-04-05 21:48:57 +00004536 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004537 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4538 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004539}
4540
Bob Wilson5bafff32009-06-22 23:27:02 +00004541static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004542 SDValue V1 = Op.getOperand(0);
4543 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004544 DebugLoc dl = Op.getDebugLoc();
4545 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004546 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004547
Bob Wilson28865062009-08-13 02:13:04 +00004548 // Convert shuffles that are directly supported on NEON to target-specific
4549 // DAG nodes, instead of keeping them as shuffles and matching them again
4550 // during code selection. This is more efficient and avoids the possibility
4551 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004552 // FIXME: floating-point vectors should be canonicalized to integer vectors
4553 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004554 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004555
Bob Wilson53dd2452010-06-07 23:53:38 +00004556 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4557 if (EltSize <= 32) {
4558 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4559 int Lane = SVN->getSplatIndex();
4560 // If this is undef splat, generate it via "just" vdup, if possible.
4561 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004562
Dan Gohman65fd6562011-11-03 21:49:52 +00004563 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004564 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4565 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4566 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004567 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4568 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4569 // reaches it).
4570 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4571 !isa<ConstantSDNode>(V1.getOperand(0))) {
4572 bool IsScalarToVector = true;
4573 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4574 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4575 IsScalarToVector = false;
4576 break;
4577 }
4578 if (IsScalarToVector)
4579 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4580 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004581 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4582 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004583 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004584
4585 bool ReverseVEXT;
4586 unsigned Imm;
4587 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4588 if (ReverseVEXT)
4589 std::swap(V1, V2);
4590 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4591 DAG.getConstant(Imm, MVT::i32));
4592 }
4593
4594 if (isVREVMask(ShuffleMask, VT, 64))
4595 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4596 if (isVREVMask(ShuffleMask, VT, 32))
4597 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4598 if (isVREVMask(ShuffleMask, VT, 16))
4599 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4600
4601 // Check for Neon shuffles that modify both input vectors in place.
4602 // If both results are used, i.e., if there are two shuffles with the same
4603 // source operands and with masks corresponding to both results of one of
4604 // these operations, DAG memoization will ensure that a single node is
4605 // used for both shuffles.
4606 unsigned WhichResult;
4607 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4608 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4609 V1, V2).getValue(WhichResult);
4610 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4611 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4612 V1, V2).getValue(WhichResult);
4613 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4614 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4615 V1, V2).getValue(WhichResult);
4616
4617 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4618 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4619 V1, V1).getValue(WhichResult);
4620 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4621 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4622 V1, V1).getValue(WhichResult);
4623 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4624 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4625 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004626 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004627
Bob Wilsonc692cb72009-08-21 20:54:19 +00004628 // If the shuffle is not directly supported and it has 4 elements, use
4629 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004630 unsigned NumElts = VT.getVectorNumElements();
4631 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004632 unsigned PFIndexes[4];
4633 for (unsigned i = 0; i != 4; ++i) {
4634 if (ShuffleMask[i] < 0)
4635 PFIndexes[i] = 8;
4636 else
4637 PFIndexes[i] = ShuffleMask[i];
4638 }
4639
4640 // Compute the index in the perfect shuffle table.
4641 unsigned PFTableIndex =
4642 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004643 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4644 unsigned Cost = (PFEntry >> 30);
4645
4646 if (Cost <= 4)
4647 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4648 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004649
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004650 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004651 if (EltSize >= 32) {
4652 // Do the expansion with floating-point types, since that is what the VFP
4653 // registers are defined to use, and since i64 is not legal.
4654 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4655 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004656 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4657 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004658 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004659 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004660 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004661 Ops.push_back(DAG.getUNDEF(EltVT));
4662 else
4663 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4664 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4665 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4666 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004667 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004668 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004669 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004670 }
4671
Bill Wendling69a05a72011-03-14 23:02:38 +00004672 if (VT == MVT::v8i8) {
4673 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4674 if (NewOp.getNode())
4675 return NewOp;
4676 }
4677
Bob Wilson22cac0d2009-08-14 05:16:33 +00004678 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004679}
4680
Eli Friedman5c89cb82011-10-24 23:08:52 +00004681static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4682 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4683 SDValue Lane = Op.getOperand(2);
4684 if (!isa<ConstantSDNode>(Lane))
4685 return SDValue();
4686
4687 return Op;
4688}
4689
Bob Wilson5bafff32009-06-22 23:27:02 +00004690static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004691 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004692 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004693 if (!isa<ConstantSDNode>(Lane))
4694 return SDValue();
4695
4696 SDValue Vec = Op.getOperand(0);
4697 if (Op.getValueType() == MVT::i32 &&
4698 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4699 DebugLoc dl = Op.getDebugLoc();
4700 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4701 }
4702
4703 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004704}
4705
Bob Wilsona6d65862009-08-03 20:36:38 +00004706static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4707 // The only time a CONCAT_VECTORS operation can have legal types is when
4708 // two 64-bit vectors are concatenated to a 128-bit vector.
4709 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4710 "unexpected CONCAT_VECTORS");
4711 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004713 SDValue Op0 = Op.getOperand(0);
4714 SDValue Op1 = Op.getOperand(1);
4715 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004716 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004717 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004718 DAG.getIntPtrConstant(0));
4719 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004721 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004722 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004723 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004724}
4725
Bob Wilson626613d2010-11-23 19:38:38 +00004726/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4727/// element has been zero/sign-extended, depending on the isSigned parameter,
4728/// from an integer type half its size.
4729static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4730 bool isSigned) {
4731 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4732 EVT VT = N->getValueType(0);
4733 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4734 SDNode *BVN = N->getOperand(0).getNode();
4735 if (BVN->getValueType(0) != MVT::v4i32 ||
4736 BVN->getOpcode() != ISD::BUILD_VECTOR)
4737 return false;
4738 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4739 unsigned HiElt = 1 - LoElt;
4740 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4741 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4742 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4743 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4744 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4745 return false;
4746 if (isSigned) {
4747 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4748 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4749 return true;
4750 } else {
4751 if (Hi0->isNullValue() && Hi1->isNullValue())
4752 return true;
4753 }
4754 return false;
4755 }
4756
4757 if (N->getOpcode() != ISD::BUILD_VECTOR)
4758 return false;
4759
4760 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4761 SDNode *Elt = N->getOperand(i).getNode();
4762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4763 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4764 unsigned HalfSize = EltSize / 2;
4765 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004766 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004767 return false;
4768 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004769 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004770 return false;
4771 }
4772 continue;
4773 }
4774 return false;
4775 }
4776
4777 return true;
4778}
4779
4780/// isSignExtended - Check if a node is a vector value that is sign-extended
4781/// or a constant BUILD_VECTOR with sign-extended elements.
4782static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4783 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4784 return true;
4785 if (isExtendedBUILD_VECTOR(N, DAG, true))
4786 return true;
4787 return false;
4788}
4789
4790/// isZeroExtended - Check if a node is a vector value that is zero-extended
4791/// or a constant BUILD_VECTOR with zero-extended elements.
4792static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4793 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4794 return true;
4795 if (isExtendedBUILD_VECTOR(N, DAG, false))
4796 return true;
4797 return false;
4798}
4799
4800/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4801/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004802static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4803 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4804 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004805 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4806 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4807 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004808 LD->isNonTemporal(), LD->isInvariant(),
4809 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004810 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4811 // have been legalized as a BITCAST from v4i32.
4812 if (N->getOpcode() == ISD::BITCAST) {
4813 SDNode *BVN = N->getOperand(0).getNode();
4814 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4815 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4816 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4817 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4818 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4819 }
4820 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4821 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4822 EVT VT = N->getValueType(0);
4823 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4824 unsigned NumElts = VT.getVectorNumElements();
4825 MVT TruncVT = MVT::getIntegerVT(EltSize);
4826 SmallVector<SDValue, 8> Ops;
4827 for (unsigned i = 0; i != NumElts; ++i) {
4828 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4829 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004830 // Element types smaller than 32 bits are not legal, so use i32 elements.
4831 // The values are implicitly truncated so sext vs. zext doesn't matter.
4832 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004833 }
4834 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4835 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004836}
4837
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004838static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4839 unsigned Opcode = N->getOpcode();
4840 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4841 SDNode *N0 = N->getOperand(0).getNode();
4842 SDNode *N1 = N->getOperand(1).getNode();
4843 return N0->hasOneUse() && N1->hasOneUse() &&
4844 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4845 }
4846 return false;
4847}
4848
4849static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4850 unsigned Opcode = N->getOpcode();
4851 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4852 SDNode *N0 = N->getOperand(0).getNode();
4853 SDNode *N1 = N->getOperand(1).getNode();
4854 return N0->hasOneUse() && N1->hasOneUse() &&
4855 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4856 }
4857 return false;
4858}
4859
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004860static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4861 // Multiplications are only custom-lowered for 128-bit vectors so that
4862 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4863 EVT VT = Op.getValueType();
4864 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4865 SDNode *N0 = Op.getOperand(0).getNode();
4866 SDNode *N1 = Op.getOperand(1).getNode();
4867 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004868 bool isMLA = false;
4869 bool isN0SExt = isSignExtended(N0, DAG);
4870 bool isN1SExt = isSignExtended(N1, DAG);
4871 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004872 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004873 else {
4874 bool isN0ZExt = isZeroExtended(N0, DAG);
4875 bool isN1ZExt = isZeroExtended(N1, DAG);
4876 if (isN0ZExt && isN1ZExt)
4877 NewOpc = ARMISD::VMULLu;
4878 else if (isN1SExt || isN1ZExt) {
4879 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4880 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4881 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4882 NewOpc = ARMISD::VMULLs;
4883 isMLA = true;
4884 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4885 NewOpc = ARMISD::VMULLu;
4886 isMLA = true;
4887 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4888 std::swap(N0, N1);
4889 NewOpc = ARMISD::VMULLu;
4890 isMLA = true;
4891 }
4892 }
4893
4894 if (!NewOpc) {
4895 if (VT == MVT::v2i64)
4896 // Fall through to expand this. It is not legal.
4897 return SDValue();
4898 else
4899 // Other vector multiplications are legal.
4900 return Op;
4901 }
4902 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004903
4904 // Legalize to a VMULL instruction.
4905 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004906 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004907 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004908 if (!isMLA) {
4909 Op0 = SkipExtension(N0, DAG);
4910 assert(Op0.getValueType().is64BitVector() &&
4911 Op1.getValueType().is64BitVector() &&
4912 "unexpected types for extended operands to VMULL");
4913 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4914 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004915
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004916 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4917 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4918 // vmull q0, d4, d6
4919 // vmlal q0, d5, d6
4920 // is faster than
4921 // vaddl q0, d4, d5
4922 // vmovl q1, d6
4923 // vmul q0, q0, q1
4924 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4925 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4926 EVT Op1VT = Op1.getValueType();
4927 return DAG.getNode(N0->getOpcode(), DL, VT,
4928 DAG.getNode(NewOpc, DL, VT,
4929 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4930 DAG.getNode(NewOpc, DL, VT,
4931 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004932}
4933
Owen Anderson76706012011-04-05 21:48:57 +00004934static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004935LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4936 // Convert to float
4937 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4938 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4939 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4940 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4941 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4942 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4943 // Get reciprocal estimate.
4944 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004945 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004946 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4947 // Because char has a smaller range than uchar, we can actually get away
4948 // without any newton steps. This requires that we use a weird bias
4949 // of 0xb000, however (again, this has been exhaustively tested).
4950 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4951 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4952 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4953 Y = DAG.getConstant(0xb000, MVT::i32);
4954 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4955 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4956 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4957 // Convert back to short.
4958 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4959 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4960 return X;
4961}
4962
Owen Anderson76706012011-04-05 21:48:57 +00004963static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004964LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4965 SDValue N2;
4966 // Convert to float.
4967 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4968 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4969 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4970 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4971 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4972 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004973
Nate Begeman7973f352011-02-11 20:53:29 +00004974 // Use reciprocal estimate and one refinement step.
4975 // float4 recip = vrecpeq_f32(yf);
4976 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004977 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004978 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004979 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004980 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4981 N1, N2);
4982 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4983 // Because short has a smaller range than ushort, we can actually get away
4984 // with only a single newton step. This requires that we use a weird bias
4985 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004986 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004987 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4988 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004989 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004990 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4991 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4992 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4993 // Convert back to integer and return.
4994 // return vmovn_s32(vcvt_s32_f32(result));
4995 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4996 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4997 return N0;
4998}
4999
5000static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5001 EVT VT = Op.getValueType();
5002 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5003 "unexpected type for custom-lowering ISD::SDIV");
5004
5005 DebugLoc dl = Op.getDebugLoc();
5006 SDValue N0 = Op.getOperand(0);
5007 SDValue N1 = Op.getOperand(1);
5008 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005009
Nate Begeman7973f352011-02-11 20:53:29 +00005010 if (VT == MVT::v8i8) {
5011 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5012 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005013
Nate Begeman7973f352011-02-11 20:53:29 +00005014 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5015 DAG.getIntPtrConstant(4));
5016 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005017 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005018 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5019 DAG.getIntPtrConstant(0));
5020 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5021 DAG.getIntPtrConstant(0));
5022
5023 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5024 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5025
5026 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5027 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005028
Nate Begeman7973f352011-02-11 20:53:29 +00005029 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5030 return N0;
5031 }
5032 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5033}
5034
5035static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5036 EVT VT = Op.getValueType();
5037 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5038 "unexpected type for custom-lowering ISD::UDIV");
5039
5040 DebugLoc dl = Op.getDebugLoc();
5041 SDValue N0 = Op.getOperand(0);
5042 SDValue N1 = Op.getOperand(1);
5043 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005044
Nate Begeman7973f352011-02-11 20:53:29 +00005045 if (VT == MVT::v8i8) {
5046 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5047 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005048
Nate Begeman7973f352011-02-11 20:53:29 +00005049 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5050 DAG.getIntPtrConstant(4));
5051 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005052 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005053 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5054 DAG.getIntPtrConstant(0));
5055 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5056 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005057
Nate Begeman7973f352011-02-11 20:53:29 +00005058 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5059 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005060
Nate Begeman7973f352011-02-11 20:53:29 +00005061 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5062 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005063
5064 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005065 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5066 N0);
5067 return N0;
5068 }
Owen Anderson76706012011-04-05 21:48:57 +00005069
Nate Begeman7973f352011-02-11 20:53:29 +00005070 // v4i16 sdiv ... Convert to float.
5071 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5072 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5073 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5074 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5075 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005076 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005077
5078 // Use reciprocal estimate and two refinement steps.
5079 // float4 recip = vrecpeq_f32(yf);
5080 // recip *= vrecpsq_f32(yf, recip);
5081 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005082 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005083 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005084 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005085 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005086 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005087 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005088 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005089 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005090 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005091 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5092 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5093 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5094 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005095 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005096 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5097 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5098 N1 = DAG.getConstant(2, MVT::i32);
5099 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5100 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5101 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5102 // Convert back to integer and return.
5103 // return vmovn_u32(vcvt_s32_f32(result));
5104 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5105 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5106 return N0;
5107}
5108
Evan Cheng342e3162011-08-30 01:34:54 +00005109static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5110 EVT VT = Op.getNode()->getValueType(0);
5111 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5112
5113 unsigned Opc;
5114 bool ExtraOp = false;
5115 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005116 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005117 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5118 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5119 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5120 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5121 }
5122
5123 if (!ExtraOp)
5124 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5125 Op.getOperand(1));
5126 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5127 Op.getOperand(1), Op.getOperand(2));
5128}
5129
Eli Friedman74bf18c2011-09-15 22:26:18 +00005130static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005131 // Monotonic load/store is legal for all targets
5132 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5133 return Op;
5134
5135 // Aquire/Release load/store is not legal for targets without a
5136 // dmb or equivalent available.
5137 return SDValue();
5138}
5139
5140
Eli Friedman2bdffe42011-08-31 00:31:29 +00005141static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005142ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5143 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005144 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005145 assert (Node->getValueType(0) == MVT::i64 &&
5146 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005147
Eli Friedman4d3f3292011-08-31 17:52:22 +00005148 SmallVector<SDValue, 6> Ops;
5149 Ops.push_back(Node->getOperand(0)); // Chain
5150 Ops.push_back(Node->getOperand(1)); // Ptr
5151 // Low part of Val1
5152 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5153 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5154 // High part of Val1
5155 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5156 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005157 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005158 // High part of Val1
5159 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5160 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5161 // High part of Val2
5162 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5163 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5164 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005165 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5166 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005167 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005168 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005169 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005170 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5171 Results.push_back(Result.getValue(2));
5172}
5173
Dan Gohmand858e902010-04-17 15:26:15 +00005174SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005175 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005176 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005177 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005178 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005179 case ISD::GlobalAddress:
5180 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5181 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005182 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005183 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005184 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5185 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005186 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005187 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005188 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005189 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005190 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005191 case ISD::SINT_TO_FP:
5192 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5193 case ISD::FP_TO_SINT:
5194 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005195 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005196 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005197 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005198 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005199 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005200 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005201 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5202 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005203 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005204 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005205 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005206 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005207 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005208 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005209 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005210 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005211 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005212 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005213 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005214 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005215 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005216 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005217 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005218 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005219 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005220 case ISD::SDIV: return LowerSDIV(Op, DAG);
5221 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005222 case ISD::ADDC:
5223 case ISD::ADDE:
5224 case ISD::SUBC:
5225 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005226 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005227 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005228 }
Evan Chenga8e29892007-01-19 07:51:42 +00005229}
5230
Duncan Sands1607f052008-12-01 11:39:25 +00005231/// ReplaceNodeResults - Replace the results of node with an illegal result
5232/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005233void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5234 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005235 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005236 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005237 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005238 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005239 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005240 case ISD::BITCAST:
5241 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005242 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005243 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005244 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005245 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005246 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005247 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005248 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005249 return;
5250 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005251 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005252 return;
5253 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005254 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005255 return;
5256 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005257 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005258 return;
5259 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005260 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005261 return;
5262 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005263 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005264 return;
5265 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005266 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005267 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005268 case ISD::ATOMIC_CMP_SWAP:
5269 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5270 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005271 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005272 if (Res.getNode())
5273 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005274}
Chris Lattner27a6c732007-11-24 07:07:01 +00005275
Evan Chenga8e29892007-01-19 07:51:42 +00005276//===----------------------------------------------------------------------===//
5277// ARM Scheduler Hooks
5278//===----------------------------------------------------------------------===//
5279
5280MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005281ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5282 MachineBasicBlock *BB,
5283 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005284 unsigned dest = MI->getOperand(0).getReg();
5285 unsigned ptr = MI->getOperand(1).getReg();
5286 unsigned oldval = MI->getOperand(2).getReg();
5287 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5289 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005291
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005292 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005293 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5294 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5295 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005296
5297 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005298 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5299 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5300 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005301 }
5302
Jim Grosbach5278eb82009-12-11 01:42:04 +00005303 unsigned ldrOpc, strOpc;
5304 switch (Size) {
5305 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005306 case 1:
5307 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005308 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005309 break;
5310 case 2:
5311 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5312 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5313 break;
5314 case 4:
5315 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5316 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5317 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005318 }
5319
5320 MachineFunction *MF = BB->getParent();
5321 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5322 MachineFunction::iterator It = BB;
5323 ++It; // insert the new blocks after the current block
5324
5325 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5326 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5327 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5328 MF->insert(It, loop1MBB);
5329 MF->insert(It, loop2MBB);
5330 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005331
5332 // Transfer the remainder of BB and its successor edges to exitMBB.
5333 exitMBB->splice(exitMBB->begin(), BB,
5334 llvm::next(MachineBasicBlock::iterator(MI)),
5335 BB->end());
5336 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005337
5338 // thisMBB:
5339 // ...
5340 // fallthrough --> loop1MBB
5341 BB->addSuccessor(loop1MBB);
5342
5343 // loop1MBB:
5344 // ldrex dest, [ptr]
5345 // cmp dest, oldval
5346 // bne exitMBB
5347 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005348 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5349 if (ldrOpc == ARM::t2LDREX)
5350 MIB.addImm(0);
5351 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005352 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005353 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005354 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5355 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005356 BB->addSuccessor(loop2MBB);
5357 BB->addSuccessor(exitMBB);
5358
5359 // loop2MBB:
5360 // strex scratch, newval, [ptr]
5361 // cmp scratch, #0
5362 // bne loop1MBB
5363 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005364 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5365 if (strOpc == ARM::t2STREX)
5366 MIB.addImm(0);
5367 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005368 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005369 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005370 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5371 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005372 BB->addSuccessor(loop1MBB);
5373 BB->addSuccessor(exitMBB);
5374
5375 // exitMBB:
5376 // ...
5377 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005378
Dan Gohman14152b42010-07-06 20:24:04 +00005379 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005380
Jim Grosbach5278eb82009-12-11 01:42:04 +00005381 return BB;
5382}
5383
5384MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005385ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5386 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005387 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5388 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5389
5390 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005391 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005392 MachineFunction::iterator It = BB;
5393 ++It;
5394
5395 unsigned dest = MI->getOperand(0).getReg();
5396 unsigned ptr = MI->getOperand(1).getReg();
5397 unsigned incr = MI->getOperand(2).getReg();
5398 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005399 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005400
5401 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5402 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005403 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5404 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005405 }
5406
Jim Grosbachc3c23542009-12-14 04:22:04 +00005407 unsigned ldrOpc, strOpc;
5408 switch (Size) {
5409 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005410 case 1:
5411 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005412 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005413 break;
5414 case 2:
5415 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5416 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5417 break;
5418 case 4:
5419 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5420 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5421 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005422 }
5423
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005424 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5425 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5426 MF->insert(It, loopMBB);
5427 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005428
5429 // Transfer the remainder of BB and its successor edges to exitMBB.
5430 exitMBB->splice(exitMBB->begin(), BB,
5431 llvm::next(MachineBasicBlock::iterator(MI)),
5432 BB->end());
5433 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005434
Craig Topper420761a2012-04-20 07:30:17 +00005435 const TargetRegisterClass *TRC = isThumb2 ?
5436 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5437 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005438 unsigned scratch = MRI.createVirtualRegister(TRC);
5439 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005440
5441 // thisMBB:
5442 // ...
5443 // fallthrough --> loopMBB
5444 BB->addSuccessor(loopMBB);
5445
5446 // loopMBB:
5447 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005448 // <binop> scratch2, dest, incr
5449 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005450 // cmp scratch, #0
5451 // bne- loopMBB
5452 // fallthrough --> exitMBB
5453 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005454 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5455 if (ldrOpc == ARM::t2LDREX)
5456 MIB.addImm(0);
5457 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005458 if (BinOpcode) {
5459 // operand order needs to go the other way for NAND
5460 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5461 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5462 addReg(incr).addReg(dest)).addReg(0);
5463 else
5464 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5465 addReg(dest).addReg(incr)).addReg(0);
5466 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005467
Jim Grosbachb6aed502011-09-09 18:37:27 +00005468 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5469 if (strOpc == ARM::t2STREX)
5470 MIB.addImm(0);
5471 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005472 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005473 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005474 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5475 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005476
5477 BB->addSuccessor(loopMBB);
5478 BB->addSuccessor(exitMBB);
5479
5480 // exitMBB:
5481 // ...
5482 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005483
Dan Gohman14152b42010-07-06 20:24:04 +00005484 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005485
Jim Grosbachc3c23542009-12-14 04:22:04 +00005486 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005487}
5488
Jim Grosbachf7da8822011-04-26 19:44:18 +00005489MachineBasicBlock *
5490ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5491 MachineBasicBlock *BB,
5492 unsigned Size,
5493 bool signExtend,
5494 ARMCC::CondCodes Cond) const {
5495 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5496
5497 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5498 MachineFunction *MF = BB->getParent();
5499 MachineFunction::iterator It = BB;
5500 ++It;
5501
5502 unsigned dest = MI->getOperand(0).getReg();
5503 unsigned ptr = MI->getOperand(1).getReg();
5504 unsigned incr = MI->getOperand(2).getReg();
5505 unsigned oldval = dest;
5506 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005507 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005508
5509 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5510 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005511 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5512 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005513 }
5514
Jim Grosbachf7da8822011-04-26 19:44:18 +00005515 unsigned ldrOpc, strOpc, extendOpc;
5516 switch (Size) {
5517 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5518 case 1:
5519 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5520 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005521 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005522 break;
5523 case 2:
5524 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5525 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005526 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005527 break;
5528 case 4:
5529 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5530 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5531 extendOpc = 0;
5532 break;
5533 }
5534
5535 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5536 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5537 MF->insert(It, loopMBB);
5538 MF->insert(It, exitMBB);
5539
5540 // Transfer the remainder of BB and its successor edges to exitMBB.
5541 exitMBB->splice(exitMBB->begin(), BB,
5542 llvm::next(MachineBasicBlock::iterator(MI)),
5543 BB->end());
5544 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5545
Craig Topper420761a2012-04-20 07:30:17 +00005546 const TargetRegisterClass *TRC = isThumb2 ?
5547 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5548 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005549 unsigned scratch = MRI.createVirtualRegister(TRC);
5550 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005551
5552 // thisMBB:
5553 // ...
5554 // fallthrough --> loopMBB
5555 BB->addSuccessor(loopMBB);
5556
5557 // loopMBB:
5558 // ldrex dest, ptr
5559 // (sign extend dest, if required)
5560 // cmp dest, incr
5561 // cmov.cond scratch2, dest, incr
5562 // strex scratch, scratch2, ptr
5563 // cmp scratch, #0
5564 // bne- loopMBB
5565 // fallthrough --> exitMBB
5566 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005567 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5568 if (ldrOpc == ARM::t2LDREX)
5569 MIB.addImm(0);
5570 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005571
5572 // Sign extend the value, if necessary.
5573 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005574 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005575 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5576 .addReg(dest)
5577 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005578 }
5579
5580 // Build compare and cmov instructions.
5581 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5582 .addReg(oldval).addReg(incr));
5583 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5584 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5585
Jim Grosbachb6aed502011-09-09 18:37:27 +00005586 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5587 if (strOpc == ARM::t2STREX)
5588 MIB.addImm(0);
5589 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005590 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5591 .addReg(scratch).addImm(0));
5592 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5593 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5594
5595 BB->addSuccessor(loopMBB);
5596 BB->addSuccessor(exitMBB);
5597
5598 // exitMBB:
5599 // ...
5600 BB = exitMBB;
5601
5602 MI->eraseFromParent(); // The instruction is gone now.
5603
5604 return BB;
5605}
5606
Eli Friedman2bdffe42011-08-31 00:31:29 +00005607MachineBasicBlock *
5608ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5609 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005610 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005611 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5612 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5613
5614 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5615 MachineFunction *MF = BB->getParent();
5616 MachineFunction::iterator It = BB;
5617 ++It;
5618
5619 unsigned destlo = MI->getOperand(0).getReg();
5620 unsigned desthi = MI->getOperand(1).getReg();
5621 unsigned ptr = MI->getOperand(2).getReg();
5622 unsigned vallo = MI->getOperand(3).getReg();
5623 unsigned valhi = MI->getOperand(4).getReg();
5624 DebugLoc dl = MI->getDebugLoc();
5625 bool isThumb2 = Subtarget->isThumb2();
5626
5627 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5628 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005629 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5630 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5631 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005632 }
5633
5634 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5635 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5636
5637 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005638 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005639 if (IsCmpxchg) {
5640 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5641 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5642 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005643 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5644 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005645 if (IsCmpxchg) {
5646 MF->insert(It, contBB);
5647 MF->insert(It, cont2BB);
5648 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005649 MF->insert(It, exitMBB);
5650
5651 // Transfer the remainder of BB and its successor edges to exitMBB.
5652 exitMBB->splice(exitMBB->begin(), BB,
5653 llvm::next(MachineBasicBlock::iterator(MI)),
5654 BB->end());
5655 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5656
Craig Topper420761a2012-04-20 07:30:17 +00005657 const TargetRegisterClass *TRC = isThumb2 ?
5658 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5659 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005660 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5661
5662 // thisMBB:
5663 // ...
5664 // fallthrough --> loopMBB
5665 BB->addSuccessor(loopMBB);
5666
5667 // loopMBB:
5668 // ldrexd r2, r3, ptr
5669 // <binopa> r0, r2, incr
5670 // <binopb> r1, r3, incr
5671 // strexd storesuccess, r0, r1, ptr
5672 // cmp storesuccess, #0
5673 // bne- loopMBB
5674 // fallthrough --> exitMBB
5675 //
5676 // Note that the registers are explicitly specified because there is not any
5677 // way to force the register allocator to allocate a register pair.
5678 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005679 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005680 // need to properly enforce the restriction that the two output registers
5681 // for ldrexd must be different.
5682 BB = loopMBB;
5683 // Load
5684 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5685 .addReg(ARM::R2, RegState::Define)
5686 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5687 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5688 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5689 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005690
5691 if (IsCmpxchg) {
5692 // Add early exit
5693 for (unsigned i = 0; i < 2; i++) {
5694 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5695 ARM::CMPrr))
5696 .addReg(i == 0 ? destlo : desthi)
5697 .addReg(i == 0 ? vallo : valhi));
5698 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5699 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5700 BB->addSuccessor(exitMBB);
5701 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5702 BB = (i == 0 ? contBB : cont2BB);
5703 }
5704
5705 // Copy to physregs for strexd
5706 unsigned setlo = MI->getOperand(5).getReg();
5707 unsigned sethi = MI->getOperand(6).getReg();
5708 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5709 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5710 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005711 // Perform binary operation
5712 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5713 .addReg(destlo).addReg(vallo))
5714 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5715 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5716 .addReg(desthi).addReg(valhi)).addReg(0);
5717 } else {
5718 // Copy to physregs for strexd
5719 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5720 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5721 }
5722
5723 // Store
5724 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5725 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5726 // Cmp+jump
5727 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5728 .addReg(storesuccess).addImm(0));
5729 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5730 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5731
5732 BB->addSuccessor(loopMBB);
5733 BB->addSuccessor(exitMBB);
5734
5735 // exitMBB:
5736 // ...
5737 BB = exitMBB;
5738
5739 MI->eraseFromParent(); // The instruction is gone now.
5740
5741 return BB;
5742}
5743
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005744/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5745/// registers the function context.
5746void ARMTargetLowering::
5747SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5748 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005749 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5750 DebugLoc dl = MI->getDebugLoc();
5751 MachineFunction *MF = MBB->getParent();
5752 MachineRegisterInfo *MRI = &MF->getRegInfo();
5753 MachineConstantPool *MCP = MF->getConstantPool();
5754 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5755 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005756
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005757 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005758 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005759
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005760 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005761 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005762 ARMConstantPoolValue *CPV =
5763 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5764 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5765
Craig Topper420761a2012-04-20 07:30:17 +00005766 const TargetRegisterClass *TRC = isThumb ?
5767 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5768 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005769
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005770 // Grab constant pool and fixed stack memory operands.
5771 MachineMemOperand *CPMMO =
5772 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5773 MachineMemOperand::MOLoad, 4, 4);
5774
5775 MachineMemOperand *FIMMOSt =
5776 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5777 MachineMemOperand::MOStore, 4, 4);
5778
5779 // Load the address of the dispatch MBB into the jump buffer.
5780 if (isThumb2) {
5781 // Incoming value: jbuf
5782 // ldr.n r5, LCPI1_1
5783 // orr r5, r5, #1
5784 // add r5, pc
5785 // str r5, [$jbuf, #+4] ; &jbuf[1]
5786 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5787 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5788 .addConstantPoolIndex(CPI)
5789 .addMemOperand(CPMMO));
5790 // Set the low bit because of thumb mode.
5791 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5792 AddDefaultCC(
5793 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5794 .addReg(NewVReg1, RegState::Kill)
5795 .addImm(0x01)));
5796 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5797 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5798 .addReg(NewVReg2, RegState::Kill)
5799 .addImm(PCLabelId);
5800 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5801 .addReg(NewVReg3, RegState::Kill)
5802 .addFrameIndex(FI)
5803 .addImm(36) // &jbuf[1] :: pc
5804 .addMemOperand(FIMMOSt));
5805 } else if (isThumb) {
5806 // Incoming value: jbuf
5807 // ldr.n r1, LCPI1_4
5808 // add r1, pc
5809 // mov r2, #1
5810 // orrs r1, r2
5811 // add r2, $jbuf, #+4 ; &jbuf[1]
5812 // str r1, [r2]
5813 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5814 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5815 .addConstantPoolIndex(CPI)
5816 .addMemOperand(CPMMO));
5817 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5818 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5819 .addReg(NewVReg1, RegState::Kill)
5820 .addImm(PCLabelId);
5821 // Set the low bit because of thumb mode.
5822 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5823 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5824 .addReg(ARM::CPSR, RegState::Define)
5825 .addImm(1));
5826 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5827 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5828 .addReg(ARM::CPSR, RegState::Define)
5829 .addReg(NewVReg2, RegState::Kill)
5830 .addReg(NewVReg3, RegState::Kill));
5831 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5832 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5833 .addFrameIndex(FI)
5834 .addImm(36)); // &jbuf[1] :: pc
5835 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5836 .addReg(NewVReg4, RegState::Kill)
5837 .addReg(NewVReg5, RegState::Kill)
5838 .addImm(0)
5839 .addMemOperand(FIMMOSt));
5840 } else {
5841 // Incoming value: jbuf
5842 // ldr r1, LCPI1_1
5843 // add r1, pc, r1
5844 // str r1, [$jbuf, #+4] ; &jbuf[1]
5845 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5846 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5847 .addConstantPoolIndex(CPI)
5848 .addImm(0)
5849 .addMemOperand(CPMMO));
5850 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5851 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5852 .addReg(NewVReg1, RegState::Kill)
5853 .addImm(PCLabelId));
5854 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5855 .addReg(NewVReg2, RegState::Kill)
5856 .addFrameIndex(FI)
5857 .addImm(36) // &jbuf[1] :: pc
5858 .addMemOperand(FIMMOSt));
5859 }
5860}
5861
5862MachineBasicBlock *ARMTargetLowering::
5863EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5864 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5865 DebugLoc dl = MI->getDebugLoc();
5866 MachineFunction *MF = MBB->getParent();
5867 MachineRegisterInfo *MRI = &MF->getRegInfo();
5868 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5869 MachineFrameInfo *MFI = MF->getFrameInfo();
5870 int FI = MFI->getFunctionContextIndex();
5871
Craig Topper420761a2012-04-20 07:30:17 +00005872 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5873 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005874 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005875
Bill Wendling04f15b42011-10-06 21:29:56 +00005876 // Get a mapping of the call site numbers to all of the landing pads they're
5877 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005878 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5879 unsigned MaxCSNum = 0;
5880 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005881 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5882 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005883 if (!BB->isLandingPad()) continue;
5884
5885 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5886 // pad.
5887 for (MachineBasicBlock::iterator
5888 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5889 if (!II->isEHLabel()) continue;
5890
5891 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005892 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005893
Bill Wendling5cbef192011-10-05 23:28:57 +00005894 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5895 for (SmallVectorImpl<unsigned>::iterator
5896 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5897 CSI != CSE; ++CSI) {
5898 CallSiteNumToLPad[*CSI].push_back(BB);
5899 MaxCSNum = std::max(MaxCSNum, *CSI);
5900 }
Bill Wendling2a850152011-10-05 00:02:33 +00005901 break;
5902 }
5903 }
5904
5905 // Get an ordered list of the machine basic blocks for the jump table.
5906 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005907 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005908 LPadList.reserve(CallSiteNumToLPad.size());
5909 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5910 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5911 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005912 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005913 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005914 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5915 }
Bill Wendling2a850152011-10-05 00:02:33 +00005916 }
5917
Bill Wendling5cbef192011-10-05 23:28:57 +00005918 assert(!LPadList.empty() &&
5919 "No landing pad destinations for the dispatch jump table!");
5920
Bill Wendling04f15b42011-10-06 21:29:56 +00005921 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005922 MachineJumpTableInfo *JTI =
5923 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5924 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5925 unsigned UId = AFI->createJumpTableUId();
5926
Bill Wendling04f15b42011-10-06 21:29:56 +00005927 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005928
5929 // Shove the dispatch's address into the return slot in the function context.
5930 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5931 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005932
Bill Wendlingbb734682011-10-05 00:39:32 +00005933 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005934 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005935 DispatchBB->addSuccessor(TrapBB);
5936
5937 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5938 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005939
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005940 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005941 MF->insert(MF->end(), DispatchBB);
5942 MF->insert(MF->end(), DispContBB);
5943 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005944
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005945 // Insert code into the entry block that creates and registers the function
5946 // context.
5947 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5948
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005949 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005950 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005951 MachineMemOperand::MOLoad |
5952 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005953
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005954 if (AFI->isThumb1OnlyFunction())
5955 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5956 else if (!Subtarget->hasVFP2())
5957 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005958 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005959 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005960
Bill Wendling952cb502011-10-18 22:49:07 +00005961 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005962 if (Subtarget->isThumb2()) {
5963 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5964 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5965 .addFrameIndex(FI)
5966 .addImm(4)
5967 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005968
Bill Wendling952cb502011-10-18 22:49:07 +00005969 if (NumLPads < 256) {
5970 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5971 .addReg(NewVReg1)
5972 .addImm(LPadList.size()));
5973 } else {
5974 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5975 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005976 .addImm(NumLPads & 0xFFFF));
5977
5978 unsigned VReg2 = VReg1;
5979 if ((NumLPads & 0xFFFF0000) != 0) {
5980 VReg2 = MRI->createVirtualRegister(TRC);
5981 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5982 .addReg(VReg1)
5983 .addImm(NumLPads >> 16));
5984 }
5985
Bill Wendling952cb502011-10-18 22:49:07 +00005986 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5987 .addReg(NewVReg1)
5988 .addReg(VReg2));
5989 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005990
Bill Wendling95ce2e92011-10-06 22:53:00 +00005991 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5992 .addMBB(TrapBB)
5993 .addImm(ARMCC::HI)
5994 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005995
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005996 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5997 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005998 .addJumpTableIndex(MJTI)
5999 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006000
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006001 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006002 AddDefaultCC(
6003 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006004 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6005 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006006 .addReg(NewVReg1)
6007 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6008
6009 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006010 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006011 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006012 .addJumpTableIndex(MJTI)
6013 .addImm(UId);
6014 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006015 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6016 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6017 .addFrameIndex(FI)
6018 .addImm(1)
6019 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006020
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006021 if (NumLPads < 256) {
6022 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6023 .addReg(NewVReg1)
6024 .addImm(NumLPads));
6025 } else {
6026 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006027 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6028 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6029
6030 // MachineConstantPool wants an explicit alignment.
6031 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6032 if (Align == 0)
6033 Align = getTargetData()->getTypeAllocSize(C->getType());
6034 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006035
6036 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6037 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6038 .addReg(VReg1, RegState::Define)
6039 .addConstantPoolIndex(Idx));
6040 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6041 .addReg(NewVReg1)
6042 .addReg(VReg1));
6043 }
6044
Bill Wendling083a8eb2011-10-06 23:37:36 +00006045 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6046 .addMBB(TrapBB)
6047 .addImm(ARMCC::HI)
6048 .addReg(ARM::CPSR);
6049
6050 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6051 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6052 .addReg(ARM::CPSR, RegState::Define)
6053 .addReg(NewVReg1)
6054 .addImm(2));
6055
6056 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006057 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006058 .addJumpTableIndex(MJTI)
6059 .addImm(UId));
6060
6061 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6062 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6063 .addReg(ARM::CPSR, RegState::Define)
6064 .addReg(NewVReg2, RegState::Kill)
6065 .addReg(NewVReg3));
6066
6067 MachineMemOperand *JTMMOLd =
6068 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6069 MachineMemOperand::MOLoad, 4, 4);
6070
6071 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6072 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6073 .addReg(NewVReg4, RegState::Kill)
6074 .addImm(0)
6075 .addMemOperand(JTMMOLd));
6076
6077 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6078 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6079 .addReg(ARM::CPSR, RegState::Define)
6080 .addReg(NewVReg5, RegState::Kill)
6081 .addReg(NewVReg3));
6082
6083 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6084 .addReg(NewVReg6, RegState::Kill)
6085 .addJumpTableIndex(MJTI)
6086 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006087 } else {
6088 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6089 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6090 .addFrameIndex(FI)
6091 .addImm(4)
6092 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006093
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006094 if (NumLPads < 256) {
6095 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6096 .addReg(NewVReg1)
6097 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006098 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006099 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6100 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006101 .addImm(NumLPads & 0xFFFF));
6102
6103 unsigned VReg2 = VReg1;
6104 if ((NumLPads & 0xFFFF0000) != 0) {
6105 VReg2 = MRI->createVirtualRegister(TRC);
6106 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6107 .addReg(VReg1)
6108 .addImm(NumLPads >> 16));
6109 }
6110
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006111 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6112 .addReg(NewVReg1)
6113 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006114 } else {
6115 MachineConstantPool *ConstantPool = MF->getConstantPool();
6116 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6117 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6118
6119 // MachineConstantPool wants an explicit alignment.
6120 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6121 if (Align == 0)
6122 Align = getTargetData()->getTypeAllocSize(C->getType());
6123 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6124
6125 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6126 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6127 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006128 .addConstantPoolIndex(Idx)
6129 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006130 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6131 .addReg(NewVReg1)
6132 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006133 }
6134
Bill Wendling95ce2e92011-10-06 22:53:00 +00006135 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6136 .addMBB(TrapBB)
6137 .addImm(ARMCC::HI)
6138 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006139
Bill Wendling564392b2011-10-18 22:11:18 +00006140 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006141 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006142 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006143 .addReg(NewVReg1)
6144 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006145 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6146 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006147 .addJumpTableIndex(MJTI)
6148 .addImm(UId));
6149
6150 MachineMemOperand *JTMMOLd =
6151 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6152 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006153 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006154 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006155 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6156 .addReg(NewVReg3, RegState::Kill)
6157 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006158 .addImm(0)
6159 .addMemOperand(JTMMOLd));
6160
6161 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006162 .addReg(NewVReg5, RegState::Kill)
6163 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006164 .addJumpTableIndex(MJTI)
6165 .addImm(UId);
6166 }
Bill Wendling2a850152011-10-05 00:02:33 +00006167
Bill Wendlingbb734682011-10-05 00:39:32 +00006168 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006169 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006170 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006171 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6172 MachineBasicBlock *CurMBB = *I;
6173 if (PrevMBB != CurMBB)
6174 DispContBB->addSuccessor(CurMBB);
6175 PrevMBB = CurMBB;
6176 }
6177
Bill Wendling24bb9252011-10-17 05:25:09 +00006178 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006179 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6180 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006181 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006182 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006183 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6184 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6185 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006186
6187 // Remove the landing pad successor from the invoke block and replace it
6188 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006189 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6190 BB->succ_end());
6191 while (!Successors.empty()) {
6192 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006193 if (SMBB->isLandingPad()) {
6194 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006195 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006196 }
6197 }
6198
6199 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006200
6201 // Find the invoke call and mark all of the callee-saved registers as
6202 // 'implicit defined' so that they're spilled. This prevents code from
6203 // moving instructions to before the EH block, where they will never be
6204 // executed.
6205 for (MachineBasicBlock::reverse_iterator
6206 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006207 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006208
6209 DenseMap<unsigned, bool> DefRegs;
6210 for (MachineInstr::mop_iterator
6211 OI = II->operands_begin(), OE = II->operands_end();
6212 OI != OE; ++OI) {
6213 if (!OI->isReg()) continue;
6214 DefRegs[OI->getReg()] = true;
6215 }
6216
6217 MachineInstrBuilder MIB(&*II);
6218
Bill Wendling5d798592011-10-14 23:55:44 +00006219 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006220 unsigned Reg = SavedRegs[i];
6221 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006222 !ARM::tGPRRegClass.contains(Reg) &&
6223 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006224 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006225 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006226 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006227 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006228 continue;
6229 if (!DefRegs[Reg])
6230 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006231 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006232
6233 break;
6234 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006235 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006236
Bill Wendlingf7b02072011-10-18 18:30:49 +00006237 // Mark all former landing pads as non-landing pads. The dispatch is the only
6238 // landing pad now.
6239 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6240 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6241 (*I)->setIsLandingPad(false);
6242
Bill Wendlingbb734682011-10-05 00:39:32 +00006243 // The instruction is gone now.
6244 MI->eraseFromParent();
6245
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006246 return MBB;
6247}
6248
Evan Cheng218977b2010-07-13 19:27:42 +00006249static
6250MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6251 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6252 E = MBB->succ_end(); I != E; ++I)
6253 if (*I != Succ)
6254 return *I;
6255 llvm_unreachable("Expecting a BB with two successors!");
6256}
6257
Manman Ren68f25572012-06-01 19:33:18 +00006258MachineBasicBlock *ARMTargetLowering::
6259EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6260 // This pseudo instruction has 3 operands: dst, src, size
6261 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6262 // Otherwise, we will generate unrolled scalar copies.
6263 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6264 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6265 MachineFunction::iterator It = BB;
6266 ++It;
6267
6268 unsigned dest = MI->getOperand(0).getReg();
6269 unsigned src = MI->getOperand(1).getReg();
6270 unsigned SizeVal = MI->getOperand(2).getImm();
6271 unsigned Align = MI->getOperand(3).getImm();
6272 DebugLoc dl = MI->getDebugLoc();
6273
6274 bool isThumb2 = Subtarget->isThumb2();
6275 MachineFunction *MF = BB->getParent();
6276 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006277 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006278
6279 const TargetRegisterClass *TRC = isThumb2 ?
6280 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6281 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006282 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006283
6284 if (Align & 1) {
6285 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6286 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6287 UnitSize = 1;
6288 } else if (Align & 2) {
6289 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6290 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6291 UnitSize = 2;
6292 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006293 // Check whether we can use NEON instructions.
6294 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6295 Subtarget->hasNEON()) {
6296 if ((Align % 16 == 0) && SizeVal >= 16) {
6297 ldrOpc = ARM::VLD1q32wb_fixed;
6298 strOpc = ARM::VST1q32wb_fixed;
6299 UnitSize = 16;
6300 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6301 }
6302 else if ((Align % 8 == 0) && SizeVal >= 8) {
6303 ldrOpc = ARM::VLD1d32wb_fixed;
6304 strOpc = ARM::VST1d32wb_fixed;
6305 UnitSize = 8;
6306 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6307 }
6308 }
6309 // Can't use NEON instructions.
6310 if (UnitSize == 0) {
6311 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6312 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6313 UnitSize = 4;
6314 }
Manman Ren68f25572012-06-01 19:33:18 +00006315 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006316
Manman Ren68f25572012-06-01 19:33:18 +00006317 unsigned BytesLeft = SizeVal % UnitSize;
6318 unsigned LoopSize = SizeVal - BytesLeft;
6319
6320 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6321 // Use LDR and STR to copy.
6322 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6323 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6324 unsigned srcIn = src;
6325 unsigned destIn = dest;
6326 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006327 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006328 unsigned srcOut = MRI.createVirtualRegister(TRC);
6329 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006330 if (UnitSize >= 8) {
6331 AddDefaultPred(BuildMI(*BB, MI, dl,
6332 TII->get(ldrOpc), scratch)
6333 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6334
6335 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6336 .addReg(destIn).addImm(0).addReg(scratch));
6337 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006338 AddDefaultPred(BuildMI(*BB, MI, dl,
6339 TII->get(ldrOpc), scratch)
6340 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6341
6342 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6343 .addReg(scratch).addReg(destIn)
6344 .addImm(UnitSize));
6345 } else {
6346 AddDefaultPred(BuildMI(*BB, MI, dl,
6347 TII->get(ldrOpc), scratch)
6348 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6349 .addImm(UnitSize));
6350
6351 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6352 .addReg(scratch).addReg(destIn)
6353 .addReg(0).addImm(UnitSize));
6354 }
6355 srcIn = srcOut;
6356 destIn = destOut;
6357 }
6358
6359 // Handle the leftover bytes with LDRB and STRB.
6360 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6361 // [destOut] = STRB_POST(scratch, destIn, 1)
6362 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6363 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6364 for (unsigned i = 0; i < BytesLeft; i++) {
6365 unsigned scratch = MRI.createVirtualRegister(TRC);
6366 unsigned srcOut = MRI.createVirtualRegister(TRC);
6367 unsigned destOut = MRI.createVirtualRegister(TRC);
6368 if (isThumb2) {
6369 AddDefaultPred(BuildMI(*BB, MI, dl,
6370 TII->get(ldrOpc),scratch)
6371 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6372
6373 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6374 .addReg(scratch).addReg(destIn)
6375 .addReg(0).addImm(1));
6376 } else {
6377 AddDefaultPred(BuildMI(*BB, MI, dl,
6378 TII->get(ldrOpc),scratch)
6379 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6380
6381 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6382 .addReg(scratch).addReg(destIn)
6383 .addReg(0).addImm(1));
6384 }
6385 srcIn = srcOut;
6386 destIn = destOut;
6387 }
6388 MI->eraseFromParent(); // The instruction is gone now.
6389 return BB;
6390 }
6391
6392 // Expand the pseudo op to a loop.
6393 // thisMBB:
6394 // ...
6395 // movw varEnd, # --> with thumb2
6396 // movt varEnd, #
6397 // ldrcp varEnd, idx --> without thumb2
6398 // fallthrough --> loopMBB
6399 // loopMBB:
6400 // PHI varPhi, varEnd, varLoop
6401 // PHI srcPhi, src, srcLoop
6402 // PHI destPhi, dst, destLoop
6403 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6404 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6405 // subs varLoop, varPhi, #UnitSize
6406 // bne loopMBB
6407 // fallthrough --> exitMBB
6408 // exitMBB:
6409 // epilogue to handle left-over bytes
6410 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6411 // [destOut] = STRB_POST(scratch, destLoop, 1)
6412 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6413 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6414 MF->insert(It, loopMBB);
6415 MF->insert(It, exitMBB);
6416
6417 // Transfer the remainder of BB and its successor edges to exitMBB.
6418 exitMBB->splice(exitMBB->begin(), BB,
6419 llvm::next(MachineBasicBlock::iterator(MI)),
6420 BB->end());
6421 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6422
6423 // Load an immediate to varEnd.
6424 unsigned varEnd = MRI.createVirtualRegister(TRC);
6425 if (isThumb2) {
6426 unsigned VReg1 = varEnd;
6427 if ((LoopSize & 0xFFFF0000) != 0)
6428 VReg1 = MRI.createVirtualRegister(TRC);
6429 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6430 .addImm(LoopSize & 0xFFFF));
6431
6432 if ((LoopSize & 0xFFFF0000) != 0)
6433 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6434 .addReg(VReg1)
6435 .addImm(LoopSize >> 16));
6436 } else {
6437 MachineConstantPool *ConstantPool = MF->getConstantPool();
6438 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6439 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6440
6441 // MachineConstantPool wants an explicit alignment.
6442 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6443 if (Align == 0)
6444 Align = getTargetData()->getTypeAllocSize(C->getType());
6445 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6446
6447 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6448 .addReg(varEnd, RegState::Define)
6449 .addConstantPoolIndex(Idx)
6450 .addImm(0));
6451 }
6452 BB->addSuccessor(loopMBB);
6453
6454 // Generate the loop body:
6455 // varPhi = PHI(varLoop, varEnd)
6456 // srcPhi = PHI(srcLoop, src)
6457 // destPhi = PHI(destLoop, dst)
6458 MachineBasicBlock *entryBB = BB;
6459 BB = loopMBB;
6460 unsigned varLoop = MRI.createVirtualRegister(TRC);
6461 unsigned varPhi = MRI.createVirtualRegister(TRC);
6462 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6463 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6464 unsigned destLoop = MRI.createVirtualRegister(TRC);
6465 unsigned destPhi = MRI.createVirtualRegister(TRC);
6466
6467 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6468 .addReg(varLoop).addMBB(loopMBB)
6469 .addReg(varEnd).addMBB(entryBB);
6470 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6471 .addReg(srcLoop).addMBB(loopMBB)
6472 .addReg(src).addMBB(entryBB);
6473 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6474 .addReg(destLoop).addMBB(loopMBB)
6475 .addReg(dest).addMBB(entryBB);
6476
6477 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6478 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006479 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6480 if (UnitSize >= 8) {
6481 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6482 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6483
6484 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6485 .addReg(destPhi).addImm(0).addReg(scratch));
6486 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006487 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6488 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6489
6490 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6491 .addReg(scratch).addReg(destPhi)
6492 .addImm(UnitSize));
6493 } else {
6494 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6495 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6496 .addImm(UnitSize));
6497
6498 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6499 .addReg(scratch).addReg(destPhi)
6500 .addReg(0).addImm(UnitSize));
6501 }
6502
6503 // Decrement loop variable by UnitSize.
6504 MachineInstrBuilder MIB = BuildMI(BB, dl,
6505 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6506 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6507 MIB->getOperand(5).setReg(ARM::CPSR);
6508 MIB->getOperand(5).setIsDef(true);
6509
6510 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6511 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6512
6513 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6514 BB->addSuccessor(loopMBB);
6515 BB->addSuccessor(exitMBB);
6516
6517 // Add epilogue to handle BytesLeft.
6518 BB = exitMBB;
6519 MachineInstr *StartOfExit = exitMBB->begin();
6520 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6521 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6522
6523 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6524 // [destOut] = STRB_POST(scratch, destLoop, 1)
6525 unsigned srcIn = srcLoop;
6526 unsigned destIn = destLoop;
6527 for (unsigned i = 0; i < BytesLeft; i++) {
6528 unsigned scratch = MRI.createVirtualRegister(TRC);
6529 unsigned srcOut = MRI.createVirtualRegister(TRC);
6530 unsigned destOut = MRI.createVirtualRegister(TRC);
6531 if (isThumb2) {
6532 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6533 TII->get(ldrOpc),scratch)
6534 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6535
6536 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6537 .addReg(scratch).addReg(destIn)
6538 .addImm(1));
6539 } else {
6540 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6541 TII->get(ldrOpc),scratch)
6542 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6543
6544 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6545 .addReg(scratch).addReg(destIn)
6546 .addReg(0).addImm(1));
6547 }
6548 srcIn = srcOut;
6549 destIn = destOut;
6550 }
6551
6552 MI->eraseFromParent(); // The instruction is gone now.
6553 return BB;
6554}
6555
Jim Grosbache801dc42009-12-12 01:40:06 +00006556MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006557ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006558 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006559 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006560 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006561 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006562 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006563 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006564 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006565 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006566 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006567 // The Thumb2 pre-indexed stores have the same MI operands, they just
6568 // define them differently in the .td files from the isel patterns, so
6569 // they need pseudos.
6570 case ARM::t2STR_preidx:
6571 MI->setDesc(TII->get(ARM::t2STR_PRE));
6572 return BB;
6573 case ARM::t2STRB_preidx:
6574 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6575 return BB;
6576 case ARM::t2STRH_preidx:
6577 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6578 return BB;
6579
Jim Grosbach19dec202011-08-05 20:35:44 +00006580 case ARM::STRi_preidx:
6581 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006582 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006583 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6584 // Decode the offset.
6585 unsigned Offset = MI->getOperand(4).getImm();
6586 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6587 Offset = ARM_AM::getAM2Offset(Offset);
6588 if (isSub)
6589 Offset = -Offset;
6590
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006591 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006592 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006593 .addOperand(MI->getOperand(0)) // Rn_wb
6594 .addOperand(MI->getOperand(1)) // Rt
6595 .addOperand(MI->getOperand(2)) // Rn
6596 .addImm(Offset) // offset (skip GPR==zero_reg)
6597 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006598 .addOperand(MI->getOperand(6))
6599 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006600 MI->eraseFromParent();
6601 return BB;
6602 }
6603 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006604 case ARM::STRBr_preidx:
6605 case ARM::STRH_preidx: {
6606 unsigned NewOpc;
6607 switch (MI->getOpcode()) {
6608 default: llvm_unreachable("unexpected opcode!");
6609 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6610 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6611 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6612 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006613 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6614 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6615 MIB.addOperand(MI->getOperand(i));
6616 MI->eraseFromParent();
6617 return BB;
6618 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006619 case ARM::ATOMIC_LOAD_ADD_I8:
6620 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6621 case ARM::ATOMIC_LOAD_ADD_I16:
6622 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6623 case ARM::ATOMIC_LOAD_ADD_I32:
6624 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006625
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006626 case ARM::ATOMIC_LOAD_AND_I8:
6627 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6628 case ARM::ATOMIC_LOAD_AND_I16:
6629 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6630 case ARM::ATOMIC_LOAD_AND_I32:
6631 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006632
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006633 case ARM::ATOMIC_LOAD_OR_I8:
6634 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6635 case ARM::ATOMIC_LOAD_OR_I16:
6636 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6637 case ARM::ATOMIC_LOAD_OR_I32:
6638 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006639
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006640 case ARM::ATOMIC_LOAD_XOR_I8:
6641 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6642 case ARM::ATOMIC_LOAD_XOR_I16:
6643 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6644 case ARM::ATOMIC_LOAD_XOR_I32:
6645 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006646
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006647 case ARM::ATOMIC_LOAD_NAND_I8:
6648 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6649 case ARM::ATOMIC_LOAD_NAND_I16:
6650 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6651 case ARM::ATOMIC_LOAD_NAND_I32:
6652 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006653
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006654 case ARM::ATOMIC_LOAD_SUB_I8:
6655 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6656 case ARM::ATOMIC_LOAD_SUB_I16:
6657 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6658 case ARM::ATOMIC_LOAD_SUB_I32:
6659 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006660
Jim Grosbachf7da8822011-04-26 19:44:18 +00006661 case ARM::ATOMIC_LOAD_MIN_I8:
6662 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6663 case ARM::ATOMIC_LOAD_MIN_I16:
6664 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6665 case ARM::ATOMIC_LOAD_MIN_I32:
6666 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6667
6668 case ARM::ATOMIC_LOAD_MAX_I8:
6669 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6670 case ARM::ATOMIC_LOAD_MAX_I16:
6671 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6672 case ARM::ATOMIC_LOAD_MAX_I32:
6673 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6674
6675 case ARM::ATOMIC_LOAD_UMIN_I8:
6676 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6677 case ARM::ATOMIC_LOAD_UMIN_I16:
6678 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6679 case ARM::ATOMIC_LOAD_UMIN_I32:
6680 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6681
6682 case ARM::ATOMIC_LOAD_UMAX_I8:
6683 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6684 case ARM::ATOMIC_LOAD_UMAX_I16:
6685 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6686 case ARM::ATOMIC_LOAD_UMAX_I32:
6687 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6688
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006689 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6690 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6691 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006692
6693 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6694 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6695 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006696
Eli Friedman2bdffe42011-08-31 00:31:29 +00006697
6698 case ARM::ATOMADD6432:
6699 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006700 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6701 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006702 case ARM::ATOMSUB6432:
6703 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006704 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6705 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006706 case ARM::ATOMOR6432:
6707 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006708 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006709 case ARM::ATOMXOR6432:
6710 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006711 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006712 case ARM::ATOMAND6432:
6713 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006714 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006715 case ARM::ATOMSWAP6432:
6716 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006717 case ARM::ATOMCMPXCHG6432:
6718 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6719 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6720 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006721
Evan Cheng007ea272009-08-12 05:17:19 +00006722 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006723 // To "insert" a SELECT_CC instruction, we actually have to insert the
6724 // diamond control-flow pattern. The incoming instruction knows the
6725 // destination vreg to set, the condition code register to branch on, the
6726 // true/false values to select between, and a branch opcode to use.
6727 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006728 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006729 ++It;
6730
6731 // thisMBB:
6732 // ...
6733 // TrueVal = ...
6734 // cmpTY ccX, r1, r2
6735 // bCC copy1MBB
6736 // fallthrough --> copy0MBB
6737 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006738 MachineFunction *F = BB->getParent();
6739 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6740 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006741 F->insert(It, copy0MBB);
6742 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006743
6744 // Transfer the remainder of BB and its successor edges to sinkMBB.
6745 sinkMBB->splice(sinkMBB->begin(), BB,
6746 llvm::next(MachineBasicBlock::iterator(MI)),
6747 BB->end());
6748 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6749
Dan Gohman258c58c2010-07-06 15:49:48 +00006750 BB->addSuccessor(copy0MBB);
6751 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006752
Dan Gohman14152b42010-07-06 20:24:04 +00006753 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6754 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6755
Evan Chenga8e29892007-01-19 07:51:42 +00006756 // copy0MBB:
6757 // %FalseValue = ...
6758 // # fallthrough to sinkMBB
6759 BB = copy0MBB;
6760
6761 // Update machine-CFG edges
6762 BB->addSuccessor(sinkMBB);
6763
6764 // sinkMBB:
6765 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6766 // ...
6767 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006768 BuildMI(*BB, BB->begin(), dl,
6769 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006770 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6771 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6772
Dan Gohman14152b42010-07-06 20:24:04 +00006773 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006774 return BB;
6775 }
Evan Cheng86198642009-08-07 00:34:42 +00006776
Evan Cheng218977b2010-07-13 19:27:42 +00006777 case ARM::BCCi64:
6778 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006779 // If there is an unconditional branch to the other successor, remove it.
6780 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006781
Evan Cheng218977b2010-07-13 19:27:42 +00006782 // Compare both parts that make up the double comparison separately for
6783 // equality.
6784 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6785
6786 unsigned LHS1 = MI->getOperand(1).getReg();
6787 unsigned LHS2 = MI->getOperand(2).getReg();
6788 if (RHSisZero) {
6789 AddDefaultPred(BuildMI(BB, dl,
6790 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6791 .addReg(LHS1).addImm(0));
6792 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6793 .addReg(LHS2).addImm(0)
6794 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6795 } else {
6796 unsigned RHS1 = MI->getOperand(3).getReg();
6797 unsigned RHS2 = MI->getOperand(4).getReg();
6798 AddDefaultPred(BuildMI(BB, dl,
6799 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6800 .addReg(LHS1).addReg(RHS1));
6801 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6802 .addReg(LHS2).addReg(RHS2)
6803 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6804 }
6805
6806 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6807 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6808 if (MI->getOperand(0).getImm() == ARMCC::NE)
6809 std::swap(destMBB, exitMBB);
6810
6811 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6812 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006813 if (isThumb2)
6814 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6815 else
6816 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006817
6818 MI->eraseFromParent(); // The pseudo instruction is gone now.
6819 return BB;
6820 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006821
Bill Wendling5bc85282011-10-17 20:37:20 +00006822 case ARM::Int_eh_sjlj_setjmp:
6823 case ARM::Int_eh_sjlj_setjmp_nofp:
6824 case ARM::tInt_eh_sjlj_setjmp:
6825 case ARM::t2Int_eh_sjlj_setjmp:
6826 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6827 EmitSjLjDispatchBlock(MI, BB);
6828 return BB;
6829
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006830 case ARM::ABS:
6831 case ARM::t2ABS: {
6832 // To insert an ABS instruction, we have to insert the
6833 // diamond control-flow pattern. The incoming instruction knows the
6834 // source vreg to test against 0, the destination vreg to set,
6835 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006836 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006837 // It transforms
6838 // V1 = ABS V0
6839 // into
6840 // V2 = MOVS V0
6841 // BCC (branch to SinkBB if V0 >= 0)
6842 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006843 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006844 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6845 MachineFunction::iterator BBI = BB;
6846 ++BBI;
6847 MachineFunction *Fn = BB->getParent();
6848 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6849 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6850 Fn->insert(BBI, RSBBB);
6851 Fn->insert(BBI, SinkBB);
6852
6853 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6854 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6855 bool isThumb2 = Subtarget->isThumb2();
6856 MachineRegisterInfo &MRI = Fn->getRegInfo();
6857 // In Thumb mode S must not be specified if source register is the SP or
6858 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006859 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6860 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6861 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006862
6863 // Transfer the remainder of BB and its successor edges to sinkMBB.
6864 SinkBB->splice(SinkBB->begin(), BB,
6865 llvm::next(MachineBasicBlock::iterator(MI)),
6866 BB->end());
6867 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6868
6869 BB->addSuccessor(RSBBB);
6870 BB->addSuccessor(SinkBB);
6871
6872 // fall through to SinkMBB
6873 RSBBB->addSuccessor(SinkBB);
6874
Manman Ren307473d2012-06-15 21:32:12 +00006875 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006876 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006877 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6878 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006879
6880 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006881 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006882 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6883 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6884
6885 // insert rsbri in RSBBB
6886 // Note: BCC and rsbri will be converted into predicated rsbmi
6887 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006888 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006889 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006890 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006891 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6892
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006893 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006894 // reuse ABSDstReg to not change uses of ABS instruction
6895 BuildMI(*SinkBB, SinkBB->begin(), dl,
6896 TII->get(ARM::PHI), ABSDstReg)
6897 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006898 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006899
6900 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006901 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006902
6903 // return last added BB
6904 return SinkBB;
6905 }
Manman Ren68f25572012-06-01 19:33:18 +00006906 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006907 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006908 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006909 }
6910}
6911
Evan Cheng37fefc22011-08-30 19:09:48 +00006912void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6913 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006914 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006915 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6916 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6917 return;
6918 }
6919
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006920 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006921 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6922 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6923 // operand is still set to noreg. If needed, set the optional operand's
6924 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006925 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006926 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006927
Andrew Trick3be654f2011-09-21 02:20:46 +00006928 // Rename pseudo opcodes.
6929 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6930 if (NewOpc) {
6931 const ARMBaseInstrInfo *TII =
6932 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006933 MCID = &TII->get(NewOpc);
6934
6935 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6936 "converted opcode should be the same except for cc_out");
6937
6938 MI->setDesc(*MCID);
6939
6940 // Add the optional cc_out operand
6941 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006942 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006943 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006944
6945 // Any ARM instruction that sets the 's' bit should specify an optional
6946 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006947 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006948 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006949 return;
6950 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006951 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6952 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006953 bool definesCPSR = false;
6954 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006955 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006956 i != e; ++i) {
6957 const MachineOperand &MO = MI->getOperand(i);
6958 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6959 definesCPSR = true;
6960 if (MO.isDead())
6961 deadCPSR = true;
6962 MI->RemoveOperand(i);
6963 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006964 }
6965 }
Andrew Trick4815d562011-09-20 03:17:40 +00006966 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006967 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006968 return;
6969 }
6970 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006971 if (deadCPSR) {
6972 assert(!MI->getOperand(ccOutIdx).getReg() &&
6973 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006974 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006975 }
Andrew Trick4815d562011-09-20 03:17:40 +00006976
Andrew Trick3be654f2011-09-21 02:20:46 +00006977 // If this instruction was defined with an optional CPSR def and its dag node
6978 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006979 MachineOperand &MO = MI->getOperand(ccOutIdx);
6980 MO.setReg(ARM::CPSR);
6981 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006982}
6983
Evan Chenga8e29892007-01-19 07:51:42 +00006984//===----------------------------------------------------------------------===//
6985// ARM Optimization Hooks
6986//===----------------------------------------------------------------------===//
6987
Chris Lattnerd1980a52009-03-12 06:52:53 +00006988static
6989SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6990 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006991 SelectionDAG &DAG = DCI.DAG;
6992 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006993 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006994 unsigned Opc = N->getOpcode();
6995 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6996 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6997 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6998 ISD::CondCode CC = ISD::SETCC_INVALID;
6999
7000 if (isSlctCC) {
7001 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
7002 } else {
7003 SDValue CCOp = Slct.getOperand(0);
7004 if (CCOp.getOpcode() == ISD::SETCC)
7005 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
7006 }
7007
7008 bool DoXform = false;
7009 bool InvCC = false;
7010 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
7011 "Bad input!");
7012
7013 if (LHS.getOpcode() == ISD::Constant &&
7014 cast<ConstantSDNode>(LHS)->isNullValue()) {
7015 DoXform = true;
7016 } else if (CC != ISD::SETCC_INVALID &&
7017 RHS.getOpcode() == ISD::Constant &&
7018 cast<ConstantSDNode>(RHS)->isNullValue()) {
7019 std::swap(LHS, RHS);
7020 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007021 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00007022 Op0.getOperand(0).getValueType();
7023 bool isInt = OpVT.isInteger();
7024 CC = ISD::getSetCCInverse(CC, isInt);
7025
7026 if (!TLI.isCondCodeLegal(CC, OpVT))
7027 return SDValue(); // Inverse operator isn't legal.
7028
7029 DoXform = true;
7030 InvCC = true;
7031 }
7032
7033 if (DoXform) {
7034 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
7035 if (isSlctCC)
7036 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
7037 Slct.getOperand(0), Slct.getOperand(1), CC);
7038 SDValue CCOp = Slct.getOperand(0);
7039 if (InvCC)
7040 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
7041 CCOp.getOperand(0), CCOp.getOperand(1), CC);
7042 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
7043 CCOp, OtherOp, Result);
7044 }
7045 return SDValue();
7046}
7047
Eric Christopherfa6f5912011-06-29 21:10:36 +00007048// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007049// (only after legalization).
7050static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7051 TargetLowering::DAGCombinerInfo &DCI,
7052 const ARMSubtarget *Subtarget) {
7053
7054 // Only perform optimization if after legalize, and if NEON is available. We
7055 // also expected both operands to be BUILD_VECTORs.
7056 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7057 || N0.getOpcode() != ISD::BUILD_VECTOR
7058 || N1.getOpcode() != ISD::BUILD_VECTOR)
7059 return SDValue();
7060
7061 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7062 EVT VT = N->getValueType(0);
7063 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7064 return SDValue();
7065
7066 // Check that the vector operands are of the right form.
7067 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7068 // operands, where N is the size of the formed vector.
7069 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7070 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007071
7072 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007073 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007074 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007075 SDValue Vec = N0->getOperand(0)->getOperand(0);
7076 SDNode *V = Vec.getNode();
7077 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007078
Eric Christopherfa6f5912011-06-29 21:10:36 +00007079 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007080 // check to see if each of their operands are an EXTRACT_VECTOR with
7081 // the same vector and appropriate index.
7082 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7083 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7084 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007085
Tanya Lattner189531f2011-06-14 23:48:48 +00007086 SDValue ExtVec0 = N0->getOperand(i);
7087 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007088
Tanya Lattner189531f2011-06-14 23:48:48 +00007089 // First operand is the vector, verify its the same.
7090 if (V != ExtVec0->getOperand(0).getNode() ||
7091 V != ExtVec1->getOperand(0).getNode())
7092 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007093
Tanya Lattner189531f2011-06-14 23:48:48 +00007094 // Second is the constant, verify its correct.
7095 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7096 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007097
Tanya Lattner189531f2011-06-14 23:48:48 +00007098 // For the constant, we want to see all the even or all the odd.
7099 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7100 || C1->getZExtValue() != nextIndex+1)
7101 return SDValue();
7102
7103 // Increment index.
7104 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007105 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007106 return SDValue();
7107 }
7108
7109 // Create VPADDL node.
7110 SelectionDAG &DAG = DCI.DAG;
7111 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007112
7113 // Build operand list.
7114 SmallVector<SDValue, 8> Ops;
7115 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7116 TLI.getPointerTy()));
7117
7118 // Input is the vector.
7119 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007120
Tanya Lattner189531f2011-06-14 23:48:48 +00007121 // Get widened type and narrowed type.
7122 MVT widenType;
7123 unsigned numElem = VT.getVectorNumElements();
7124 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7125 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7126 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7127 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7128 default:
Craig Topperbc219812012-02-07 02:50:20 +00007129 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007130 }
7131
7132 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7133 widenType, &Ops[0], Ops.size());
7134 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7135}
7136
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +00007137static SDValue findMUL_LOHI(SDValue V) {
7138 if (V->getOpcode() == ISD::UMUL_LOHI ||
7139 V->getOpcode() == ISD::SMUL_LOHI)
7140 return V;
7141 return SDValue();
7142}
7143
7144static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7145 TargetLowering::DAGCombinerInfo &DCI,
7146 const ARMSubtarget *Subtarget) {
7147
7148 if (Subtarget->isThumb1Only()) return SDValue();
7149
7150 // Only perform the checks after legalize when the pattern is available.
7151 if (DCI.isBeforeLegalize()) return SDValue();
7152
7153 // Look for multiply add opportunities.
7154 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7155 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7156 // a glue link from the first add to the second add.
7157 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7158 // a S/UMLAL instruction.
7159 // loAdd UMUL_LOHI
7160 // \ / :lo \ :hi
7161 // \ / \ [no multiline comment]
7162 // ADDC | hiAdd
7163 // \ :glue / /
7164 // \ / /
7165 // ADDE
7166 //
7167 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7168 SDValue AddcOp0 = AddcNode->getOperand(0);
7169 SDValue AddcOp1 = AddcNode->getOperand(1);
7170
7171 // Check if the two operands are from the same mul_lohi node.
7172 if (AddcOp0.getNode() == AddcOp1.getNode())
7173 return SDValue();
7174
7175 assert(AddcNode->getNumValues() == 2 &&
7176 AddcNode->getValueType(0) == MVT::i32 &&
7177 AddcNode->getValueType(1) == MVT::Glue &&
7178 "Expect ADDC with two result values: i32, glue");
7179
7180 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7181 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7182 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7183 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7184 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7185 return SDValue();
7186
7187 // Look for the glued ADDE.
7188 SDNode* AddeNode = AddcNode->getGluedUser();
7189 if (AddeNode == NULL)
7190 return SDValue();
7191
7192 // Make sure it is really an ADDE.
7193 if (AddeNode->getOpcode() != ISD::ADDE)
7194 return SDValue();
7195
7196 assert(AddeNode->getNumOperands() == 3 &&
7197 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7198 "ADDE node has the wrong inputs");
7199
7200 // Check for the triangle shape.
7201 SDValue AddeOp0 = AddeNode->getOperand(0);
7202 SDValue AddeOp1 = AddeNode->getOperand(1);
7203
7204 // Make sure that the ADDE operands are not coming from the same node.
7205 if (AddeOp0.getNode() == AddeOp1.getNode())
7206 return SDValue();
7207
7208 // Find the MUL_LOHI node walking up ADDE's operands.
7209 bool IsLeftOperandMUL = false;
7210 SDValue MULOp = findMUL_LOHI(AddeOp0);
7211 if (MULOp == SDValue())
7212 MULOp = findMUL_LOHI(AddeOp1);
7213 else
7214 IsLeftOperandMUL = true;
7215 if (MULOp == SDValue())
7216 return SDValue();
7217
7218 // Figure out the right opcode.
7219 unsigned Opc = MULOp->getOpcode();
7220 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7221
7222 // Figure out the high and low input values to the MLAL node.
7223 SDValue* HiMul = &MULOp;
7224 SDValue* HiAdd = NULL;
7225 SDValue* LoMul = NULL;
7226 SDValue* LowAdd = NULL;
7227
7228 if (IsLeftOperandMUL)
7229 HiAdd = &AddeOp1;
7230 else
7231 HiAdd = &AddeOp0;
7232
7233
7234 if (AddcOp0->getOpcode() == Opc) {
7235 LoMul = &AddcOp0;
7236 LowAdd = &AddcOp1;
7237 }
7238 if (AddcOp1->getOpcode() == Opc) {
7239 LoMul = &AddcOp1;
7240 LowAdd = &AddcOp0;
7241 }
7242
7243 if (LoMul == NULL)
7244 return SDValue();
7245
7246 if (LoMul->getNode() != HiMul->getNode())
7247 return SDValue();
7248
7249 // Create the merged node.
7250 SelectionDAG &DAG = DCI.DAG;
7251
7252 // Build operand list.
7253 SmallVector<SDValue, 8> Ops;
7254 Ops.push_back(LoMul->getOperand(0));
7255 Ops.push_back(LoMul->getOperand(1));
7256 Ops.push_back(*LowAdd);
7257 Ops.push_back(*HiAdd);
7258
7259 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7260 DAG.getVTList(MVT::i32, MVT::i32),
7261 &Ops[0], Ops.size());
7262
7263 // Replace the ADDs' nodes uses by the MLA node's values.
7264 SDValue HiMLALResult(MLALNode.getNode(), 1);
7265 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7266
7267 SDValue LoMLALResult(MLALNode.getNode(), 0);
7268 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7269
7270 // Return original node to notify the driver to stop replacing.
7271 SDValue resNode(AddcNode, 0);
7272 return resNode;
7273}
7274
7275/// PerformADDCCombine - Target-specific dag combine transform from
7276/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7277static SDValue PerformADDCCombine(SDNode *N,
7278 TargetLowering::DAGCombinerInfo &DCI,
7279 const ARMSubtarget *Subtarget) {
7280
7281 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7282
7283}
7284
Bob Wilson3d5792a2010-07-29 20:34:14 +00007285/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7286/// operands N0 and N1. This is a helper for PerformADDCombine that is
7287/// called with the default operands, and if that fails, with commuted
7288/// operands.
7289static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007290 TargetLowering::DAGCombinerInfo &DCI,
7291 const ARMSubtarget *Subtarget){
7292
7293 // Attempt to create vpaddl for this add.
7294 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7295 if (Result.getNode())
7296 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007297
Chris Lattnerd1980a52009-03-12 06:52:53 +00007298 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7299 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7300 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7301 if (Result.getNode()) return Result;
7302 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007303 return SDValue();
7304}
7305
Bob Wilson3d5792a2010-07-29 20:34:14 +00007306/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7307///
7308static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007309 TargetLowering::DAGCombinerInfo &DCI,
7310 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007311 SDValue N0 = N->getOperand(0);
7312 SDValue N1 = N->getOperand(1);
7313
7314 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007315 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007316 if (Result.getNode())
7317 return Result;
7318
7319 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007320 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007321}
7322
Chris Lattnerd1980a52009-03-12 06:52:53 +00007323/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007324///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007325static SDValue PerformSUBCombine(SDNode *N,
7326 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007327 SDValue N0 = N->getOperand(0);
7328 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007329
Chris Lattnerd1980a52009-03-12 06:52:53 +00007330 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7331 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7332 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7333 if (Result.getNode()) return Result;
7334 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007335
Chris Lattnerd1980a52009-03-12 06:52:53 +00007336 return SDValue();
7337}
7338
Evan Cheng463d3582011-03-31 19:38:48 +00007339/// PerformVMULCombine
7340/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7341/// special multiplier accumulator forwarding.
7342/// vmul d3, d0, d2
7343/// vmla d3, d1, d2
7344/// is faster than
7345/// vadd d3, d0, d1
7346/// vmul d3, d3, d2
7347static SDValue PerformVMULCombine(SDNode *N,
7348 TargetLowering::DAGCombinerInfo &DCI,
7349 const ARMSubtarget *Subtarget) {
7350 if (!Subtarget->hasVMLxForwarding())
7351 return SDValue();
7352
7353 SelectionDAG &DAG = DCI.DAG;
7354 SDValue N0 = N->getOperand(0);
7355 SDValue N1 = N->getOperand(1);
7356 unsigned Opcode = N0.getOpcode();
7357 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7358 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007359 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007360 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7361 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7362 return SDValue();
7363 std::swap(N0, N1);
7364 }
7365
7366 EVT VT = N->getValueType(0);
7367 DebugLoc DL = N->getDebugLoc();
7368 SDValue N00 = N0->getOperand(0);
7369 SDValue N01 = N0->getOperand(1);
7370 return DAG.getNode(Opcode, DL, VT,
7371 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7372 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7373}
7374
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007375static SDValue PerformMULCombine(SDNode *N,
7376 TargetLowering::DAGCombinerInfo &DCI,
7377 const ARMSubtarget *Subtarget) {
7378 SelectionDAG &DAG = DCI.DAG;
7379
7380 if (Subtarget->isThumb1Only())
7381 return SDValue();
7382
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007383 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7384 return SDValue();
7385
7386 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007387 if (VT.is64BitVector() || VT.is128BitVector())
7388 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007389 if (VT != MVT::i32)
7390 return SDValue();
7391
7392 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7393 if (!C)
7394 return SDValue();
7395
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007396 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007397 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007398
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007399 ShiftAmt = ShiftAmt & (32 - 1);
7400 SDValue V = N->getOperand(0);
7401 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007402
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007403 SDValue Res;
7404 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007405
7406 if (MulAmt >= 0) {
7407 if (isPowerOf2_32(MulAmt - 1)) {
7408 // (mul x, 2^N + 1) => (add (shl x, N), x)
7409 Res = DAG.getNode(ISD::ADD, DL, VT,
7410 V,
7411 DAG.getNode(ISD::SHL, DL, VT,
7412 V,
7413 DAG.getConstant(Log2_32(MulAmt - 1),
7414 MVT::i32)));
7415 } else if (isPowerOf2_32(MulAmt + 1)) {
7416 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7417 Res = DAG.getNode(ISD::SUB, DL, VT,
7418 DAG.getNode(ISD::SHL, DL, VT,
7419 V,
7420 DAG.getConstant(Log2_32(MulAmt + 1),
7421 MVT::i32)),
7422 V);
7423 } else
7424 return SDValue();
7425 } else {
7426 uint64_t MulAmtAbs = -MulAmt;
7427 if (isPowerOf2_32(MulAmtAbs + 1)) {
7428 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7429 Res = DAG.getNode(ISD::SUB, DL, VT,
7430 V,
7431 DAG.getNode(ISD::SHL, DL, VT,
7432 V,
7433 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7434 MVT::i32)));
7435 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7436 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7437 Res = DAG.getNode(ISD::ADD, DL, VT,
7438 V,
7439 DAG.getNode(ISD::SHL, DL, VT,
7440 V,
7441 DAG.getConstant(Log2_32(MulAmtAbs-1),
7442 MVT::i32)));
7443 Res = DAG.getNode(ISD::SUB, DL, VT,
7444 DAG.getConstant(0, MVT::i32),Res);
7445
7446 } else
7447 return SDValue();
7448 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007449
7450 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007451 Res = DAG.getNode(ISD::SHL, DL, VT,
7452 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007453
7454 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007455 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007456 return SDValue();
7457}
7458
Evan Chengc892aeb2012-02-23 01:19:06 +00007459static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7460 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7461 return false;
7462
7463 SDValue FalseVal = N.getOperand(0);
7464 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7465 if (!C)
7466 return false;
7467 if (AllOnes)
7468 return C->isAllOnesValue();
7469 return C->isNullValue();
7470}
7471
7472/// formConditionalOp - Combine an operation with a conditional move operand
7473/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7474/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7475static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7476 bool Commutable) {
7477 SDValue N0 = N->getOperand(0);
7478 SDValue N1 = N->getOperand(1);
7479
7480 bool isAND = N->getOpcode() == ISD::AND;
7481 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7482 if (!isCand && Commutable) {
7483 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7484 if (isCand)
7485 std::swap(N0, N1);
7486 }
7487 if (!isCand)
7488 return SDValue();
7489
7490 unsigned Opc = 0;
7491 switch (N->getOpcode()) {
7492 default: llvm_unreachable("Unexpected node");
7493 case ISD::AND: Opc = ARMISD::CAND; break;
7494 case ISD::OR: Opc = ARMISD::COR; break;
7495 case ISD::XOR: Opc = ARMISD::CXOR; break;
7496 }
7497 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7498 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7499 N1.getOperand(4));
7500}
7501
Owen Anderson080c0922010-11-05 19:27:46 +00007502static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007503 TargetLowering::DAGCombinerInfo &DCI,
7504 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007505
Owen Anderson080c0922010-11-05 19:27:46 +00007506 // Attempt to use immediate-form VBIC
7507 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7508 DebugLoc dl = N->getDebugLoc();
7509 EVT VT = N->getValueType(0);
7510 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007511
Tanya Lattner0433b212011-04-07 15:24:20 +00007512 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7513 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007514
Owen Anderson080c0922010-11-05 19:27:46 +00007515 APInt SplatBits, SplatUndef;
7516 unsigned SplatBitSize;
7517 bool HasAnyUndefs;
7518 if (BVN &&
7519 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7520 if (SplatBitSize <= 64) {
7521 EVT VbicVT;
7522 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7523 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007524 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007525 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007526 if (Val.getNode()) {
7527 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007528 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007529 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007530 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007531 }
7532 }
7533 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007534
Evan Chengc892aeb2012-02-23 01:19:06 +00007535 if (!Subtarget->isThumb1Only()) {
7536 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7537 SDValue CAND = formConditionalOp(N, DAG, true);
7538 if (CAND.getNode())
7539 return CAND;
7540 }
7541
Owen Anderson080c0922010-11-05 19:27:46 +00007542 return SDValue();
7543}
7544
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007545/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7546static SDValue PerformORCombine(SDNode *N,
7547 TargetLowering::DAGCombinerInfo &DCI,
7548 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007549 // Attempt to use immediate-form VORR
7550 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7551 DebugLoc dl = N->getDebugLoc();
7552 EVT VT = N->getValueType(0);
7553 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007554
Tanya Lattner0433b212011-04-07 15:24:20 +00007555 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7556 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007557
Owen Anderson60f48702010-11-03 23:15:26 +00007558 APInt SplatBits, SplatUndef;
7559 unsigned SplatBitSize;
7560 bool HasAnyUndefs;
7561 if (BVN && Subtarget->hasNEON() &&
7562 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7563 if (SplatBitSize <= 64) {
7564 EVT VorrVT;
7565 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7566 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007567 DAG, VorrVT, VT.is128BitVector(),
7568 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007569 if (Val.getNode()) {
7570 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007571 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007572 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007573 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007574 }
7575 }
7576 }
7577
Evan Chengc892aeb2012-02-23 01:19:06 +00007578 if (!Subtarget->isThumb1Only()) {
7579 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7580 SDValue COR = formConditionalOp(N, DAG, true);
7581 if (COR.getNode())
7582 return COR;
7583 }
7584
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007585 SDValue N0 = N->getOperand(0);
7586 if (N0.getOpcode() != ISD::AND)
7587 return SDValue();
7588 SDValue N1 = N->getOperand(1);
7589
7590 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7591 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7592 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7593 APInt SplatUndef;
7594 unsigned SplatBitSize;
7595 bool HasAnyUndefs;
7596
7597 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7598 APInt SplatBits0;
7599 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7600 HasAnyUndefs) && !HasAnyUndefs) {
7601 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7602 APInt SplatBits1;
7603 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7604 HasAnyUndefs) && !HasAnyUndefs &&
7605 SplatBits0 == ~SplatBits1) {
7606 // Canonicalize the vector type to make instruction selection simpler.
7607 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7608 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7609 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007610 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007611 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7612 }
7613 }
7614 }
7615
Jim Grosbach54238562010-07-17 03:30:54 +00007616 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7617 // reasonable.
7618
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007619 // BFI is only available on V6T2+
7620 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7621 return SDValue();
7622
Jim Grosbach54238562010-07-17 03:30:54 +00007623 DebugLoc DL = N->getDebugLoc();
7624 // 1) or (and A, mask), val => ARMbfi A, val, mask
7625 // iff (val & mask) == val
7626 //
7627 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7628 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007629 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007630 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007631 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007632 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007633
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007634 if (VT != MVT::i32)
7635 return SDValue();
7636
Evan Cheng30fb13f2010-12-13 20:32:54 +00007637 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007638
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007639 // The value and the mask need to be constants so we can verify this is
7640 // actually a bitfield set. If the mask is 0xffff, we can do better
7641 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007642 SDValue MaskOp = N0.getOperand(1);
7643 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7644 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007645 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007646 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007647 if (Mask == 0xffff)
7648 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007649 SDValue Res;
7650 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007651 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7652 if (N1C) {
7653 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007654 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007655 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007656
Evan Chenga9688c42010-12-11 04:11:38 +00007657 if (ARM::isBitFieldInvertedMask(Mask)) {
7658 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007659
Evan Cheng30fb13f2010-12-13 20:32:54 +00007660 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007661 DAG.getConstant(Val, MVT::i32),
7662 DAG.getConstant(Mask, MVT::i32));
7663
7664 // Do not add new nodes to DAG combiner worklist.
7665 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007666 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007667 }
Jim Grosbach54238562010-07-17 03:30:54 +00007668 } else if (N1.getOpcode() == ISD::AND) {
7669 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007670 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7671 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007672 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007673 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007674
Eric Christopher29aeed12011-03-26 01:21:03 +00007675 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7676 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007677 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007678 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007679 // The pack halfword instruction works better for masks that fit it,
7680 // so use that when it's available.
7681 if (Subtarget->hasT2ExtractPack() &&
7682 (Mask == 0xffff || Mask == 0xffff0000))
7683 return SDValue();
7684 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007685 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007686 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007687 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007688 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007689 DAG.getConstant(Mask, MVT::i32));
7690 // Do not add new nodes to DAG combiner worklist.
7691 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007692 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007693 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007694 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007695 // The pack halfword instruction works better for masks that fit it,
7696 // so use that when it's available.
7697 if (Subtarget->hasT2ExtractPack() &&
7698 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7699 return SDValue();
7700 // 2b
7701 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007702 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007703 DAG.getConstant(lsb, MVT::i32));
7704 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007705 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007706 // Do not add new nodes to DAG combiner worklist.
7707 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007708 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007709 }
7710 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007711
Evan Cheng30fb13f2010-12-13 20:32:54 +00007712 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7713 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7714 ARM::isBitFieldInvertedMask(~Mask)) {
7715 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7716 // where lsb(mask) == #shamt and masked bits of B are known zero.
7717 SDValue ShAmt = N00.getOperand(1);
7718 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7719 unsigned LSB = CountTrailingZeros_32(Mask);
7720 if (ShAmtC != LSB)
7721 return SDValue();
7722
7723 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7724 DAG.getConstant(~Mask, MVT::i32));
7725
7726 // Do not add new nodes to DAG combiner worklist.
7727 DCI.CombineTo(N, Res, false);
7728 }
7729
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007730 return SDValue();
7731}
7732
Evan Chengc892aeb2012-02-23 01:19:06 +00007733static SDValue PerformXORCombine(SDNode *N,
7734 TargetLowering::DAGCombinerInfo &DCI,
7735 const ARMSubtarget *Subtarget) {
7736 EVT VT = N->getValueType(0);
7737 SelectionDAG &DAG = DCI.DAG;
7738
7739 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7740 return SDValue();
7741
7742 if (!Subtarget->isThumb1Only()) {
7743 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7744 SDValue CXOR = formConditionalOp(N, DAG, true);
7745 if (CXOR.getNode())
7746 return CXOR;
7747 }
7748
7749 return SDValue();
7750}
7751
Evan Chengbf188ae2011-06-15 01:12:31 +00007752/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7753/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007754static SDValue PerformBFICombine(SDNode *N,
7755 TargetLowering::DAGCombinerInfo &DCI) {
7756 SDValue N1 = N->getOperand(1);
7757 if (N1.getOpcode() == ISD::AND) {
7758 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7759 if (!N11C)
7760 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007761 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7762 unsigned LSB = CountTrailingZeros_32(~InvMask);
7763 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7764 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007765 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007766 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007767 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7768 N->getOperand(0), N1.getOperand(0),
7769 N->getOperand(2));
7770 }
7771 return SDValue();
7772}
7773
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007774/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7775/// ARMISD::VMOVRRD.
7776static SDValue PerformVMOVRRDCombine(SDNode *N,
7777 TargetLowering::DAGCombinerInfo &DCI) {
7778 // vmovrrd(vmovdrr x, y) -> x,y
7779 SDValue InDouble = N->getOperand(0);
7780 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7781 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007782
7783 // vmovrrd(load f64) -> (load i32), (load i32)
7784 SDNode *InNode = InDouble.getNode();
7785 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7786 InNode->getValueType(0) == MVT::f64 &&
7787 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7788 !cast<LoadSDNode>(InNode)->isVolatile()) {
7789 // TODO: Should this be done for non-FrameIndex operands?
7790 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7791
7792 SelectionDAG &DAG = DCI.DAG;
7793 DebugLoc DL = LD->getDebugLoc();
7794 SDValue BasePtr = LD->getBasePtr();
7795 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7796 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007797 LD->isNonTemporal(), LD->isInvariant(),
7798 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007799
7800 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7801 DAG.getConstant(4, MVT::i32));
7802 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7803 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007804 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007805 std::min(4U, LD->getAlignment() / 2));
7806
7807 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7808 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7809 DCI.RemoveFromWorklist(LD);
7810 DAG.DeleteNode(LD);
7811 return Result;
7812 }
7813
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007814 return SDValue();
7815}
7816
7817/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7818/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7819static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7820 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7821 SDValue Op0 = N->getOperand(0);
7822 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007823 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007824 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007825 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007826 Op1 = Op1.getOperand(0);
7827 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7828 Op0.getNode() == Op1.getNode() &&
7829 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007830 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007831 N->getValueType(0), Op0.getOperand(0));
7832 return SDValue();
7833}
7834
Bob Wilson31600902010-12-21 06:43:19 +00007835/// PerformSTORECombine - Target-specific dag combine xforms for
7836/// ISD::STORE.
7837static SDValue PerformSTORECombine(SDNode *N,
7838 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007839 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007840 if (St->isVolatile())
7841 return SDValue();
7842
Andrew Trick49b446f2012-07-18 18:34:24 +00007843 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007844 // pack all of the elements in one place. Next, store to memory in fewer
7845 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007846 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007847 EVT VT = StVal.getValueType();
7848 if (St->isTruncatingStore() && VT.isVector()) {
7849 SelectionDAG &DAG = DCI.DAG;
7850 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7851 EVT StVT = St->getMemoryVT();
7852 unsigned NumElems = VT.getVectorNumElements();
7853 assert(StVT != VT && "Cannot truncate to the same type");
7854 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7855 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7856
7857 // From, To sizes and ElemCount must be pow of two
7858 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7859
7860 // We are going to use the original vector elt for storing.
7861 // Accumulated smaller vector elements must be a multiple of the store size.
7862 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7863
7864 unsigned SizeRatio = FromEltSz / ToEltSz;
7865 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7866
7867 // Create a type on which we perform the shuffle.
7868 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7869 NumElems*SizeRatio);
7870 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7871
7872 DebugLoc DL = St->getDebugLoc();
7873 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7874 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7875 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7876
7877 // Can't shuffle using an illegal type.
7878 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7879
7880 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7881 DAG.getUNDEF(WideVec.getValueType()),
7882 ShuffleVec.data());
7883 // At this point all of the data is stored at the bottom of the
7884 // register. We now need to save it to mem.
7885
7886 // Find the largest store unit
7887 MVT StoreType = MVT::i8;
7888 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7889 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7890 MVT Tp = (MVT::SimpleValueType)tp;
7891 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7892 StoreType = Tp;
7893 }
7894 // Didn't find a legal store type.
7895 if (!TLI.isTypeLegal(StoreType))
7896 return SDValue();
7897
7898 // Bitcast the original vector into a vector of store-size units
7899 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7900 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7901 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7902 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7903 SmallVector<SDValue, 8> Chains;
7904 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7905 TLI.getPointerTy());
7906 SDValue BasePtr = St->getBasePtr();
7907
7908 // Perform one or more big stores into memory.
7909 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7910 for (unsigned I = 0; I < E; I++) {
7911 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7912 StoreType, ShuffWide,
7913 DAG.getIntPtrConstant(I));
7914 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7915 St->getPointerInfo(), St->isVolatile(),
7916 St->isNonTemporal(), St->getAlignment());
7917 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7918 Increment);
7919 Chains.push_back(Ch);
7920 }
7921 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7922 Chains.size());
7923 }
7924
7925 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007926 return SDValue();
7927
Chad Rosier96b66d62012-04-09 19:38:15 +00007928 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7929 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007930 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007931 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007932 SelectionDAG &DAG = DCI.DAG;
7933 DebugLoc DL = St->getDebugLoc();
7934 SDValue BasePtr = St->getBasePtr();
7935 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7936 StVal.getNode()->getOperand(0), BasePtr,
7937 St->getPointerInfo(), St->isVolatile(),
7938 St->isNonTemporal(), St->getAlignment());
7939
7940 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7941 DAG.getConstant(4, MVT::i32));
7942 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7943 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7944 St->isNonTemporal(),
7945 std::min(4U, St->getAlignment() / 2));
7946 }
7947
7948 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007949 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7950 return SDValue();
7951
Chad Rosier96b66d62012-04-09 19:38:15 +00007952 // Bitcast an i64 store extracted from a vector to f64.
7953 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007954 SelectionDAG &DAG = DCI.DAG;
7955 DebugLoc dl = StVal.getDebugLoc();
7956 SDValue IntVec = StVal.getOperand(0);
7957 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7958 IntVec.getValueType().getVectorNumElements());
7959 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7960 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7961 Vec, StVal.getOperand(1));
7962 dl = N->getDebugLoc();
7963 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7964 // Make the DAGCombiner fold the bitcasts.
7965 DCI.AddToWorklist(Vec.getNode());
7966 DCI.AddToWorklist(ExtElt.getNode());
7967 DCI.AddToWorklist(V.getNode());
7968 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7969 St->getPointerInfo(), St->isVolatile(),
7970 St->isNonTemporal(), St->getAlignment(),
7971 St->getTBAAInfo());
7972}
7973
7974/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7975/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7976/// i64 vector to have f64 elements, since the value can then be loaded
7977/// directly into a VFP register.
7978static bool hasNormalLoadOperand(SDNode *N) {
7979 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7980 for (unsigned i = 0; i < NumElts; ++i) {
7981 SDNode *Elt = N->getOperand(i).getNode();
7982 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7983 return true;
7984 }
7985 return false;
7986}
7987
Bob Wilson75f02882010-09-17 22:59:05 +00007988/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7989/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007990static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7991 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007992 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7993 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7994 // into a pair of GPRs, which is fine when the value is used as a scalar,
7995 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007996 SelectionDAG &DAG = DCI.DAG;
7997 if (N->getNumOperands() == 2) {
7998 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7999 if (RV.getNode())
8000 return RV;
8001 }
Bob Wilson75f02882010-09-17 22:59:05 +00008002
Bob Wilson31600902010-12-21 06:43:19 +00008003 // Load i64 elements as f64 values so that type legalization does not split
8004 // them up into i32 values.
8005 EVT VT = N->getValueType(0);
8006 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8007 return SDValue();
8008 DebugLoc dl = N->getDebugLoc();
8009 SmallVector<SDValue, 8> Ops;
8010 unsigned NumElts = VT.getVectorNumElements();
8011 for (unsigned i = 0; i < NumElts; ++i) {
8012 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8013 Ops.push_back(V);
8014 // Make the DAGCombiner fold the bitcast.
8015 DCI.AddToWorklist(V.getNode());
8016 }
8017 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8018 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8019 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8020}
8021
8022/// PerformInsertEltCombine - Target-specific dag combine xforms for
8023/// ISD::INSERT_VECTOR_ELT.
8024static SDValue PerformInsertEltCombine(SDNode *N,
8025 TargetLowering::DAGCombinerInfo &DCI) {
8026 // Bitcast an i64 load inserted into a vector to f64.
8027 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8028 EVT VT = N->getValueType(0);
8029 SDNode *Elt = N->getOperand(1).getNode();
8030 if (VT.getVectorElementType() != MVT::i64 ||
8031 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8032 return SDValue();
8033
8034 SelectionDAG &DAG = DCI.DAG;
8035 DebugLoc dl = N->getDebugLoc();
8036 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8037 VT.getVectorNumElements());
8038 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8039 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8040 // Make the DAGCombiner fold the bitcasts.
8041 DCI.AddToWorklist(Vec.getNode());
8042 DCI.AddToWorklist(V.getNode());
8043 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8044 Vec, V, N->getOperand(2));
8045 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008046}
8047
Bob Wilsonf20700c2010-10-27 20:38:28 +00008048/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8049/// ISD::VECTOR_SHUFFLE.
8050static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8051 // The LLVM shufflevector instruction does not require the shuffle mask
8052 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8053 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8054 // operands do not match the mask length, they are extended by concatenating
8055 // them with undef vectors. That is probably the right thing for other
8056 // targets, but for NEON it is better to concatenate two double-register
8057 // size vector operands into a single quad-register size vector. Do that
8058 // transformation here:
8059 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8060 // shuffle(concat(v1, v2), undef)
8061 SDValue Op0 = N->getOperand(0);
8062 SDValue Op1 = N->getOperand(1);
8063 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8064 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8065 Op0.getNumOperands() != 2 ||
8066 Op1.getNumOperands() != 2)
8067 return SDValue();
8068 SDValue Concat0Op1 = Op0.getOperand(1);
8069 SDValue Concat1Op1 = Op1.getOperand(1);
8070 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8071 Concat1Op1.getOpcode() != ISD::UNDEF)
8072 return SDValue();
8073 // Skip the transformation if any of the types are illegal.
8074 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8075 EVT VT = N->getValueType(0);
8076 if (!TLI.isTypeLegal(VT) ||
8077 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8078 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8079 return SDValue();
8080
8081 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8082 Op0.getOperand(0), Op1.getOperand(0));
8083 // Translate the shuffle mask.
8084 SmallVector<int, 16> NewMask;
8085 unsigned NumElts = VT.getVectorNumElements();
8086 unsigned HalfElts = NumElts/2;
8087 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8088 for (unsigned n = 0; n < NumElts; ++n) {
8089 int MaskElt = SVN->getMaskElt(n);
8090 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008091 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008092 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008093 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008094 NewElt = HalfElts + MaskElt - NumElts;
8095 NewMask.push_back(NewElt);
8096 }
8097 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8098 DAG.getUNDEF(VT), NewMask.data());
8099}
8100
Bob Wilson1c3ef902011-02-07 17:43:21 +00008101/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8102/// NEON load/store intrinsics to merge base address updates.
8103static SDValue CombineBaseUpdate(SDNode *N,
8104 TargetLowering::DAGCombinerInfo &DCI) {
8105 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8106 return SDValue();
8107
8108 SelectionDAG &DAG = DCI.DAG;
8109 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8110 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8111 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8112 SDValue Addr = N->getOperand(AddrOpIdx);
8113
8114 // Search for a use of the address operand that is an increment.
8115 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8116 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8117 SDNode *User = *UI;
8118 if (User->getOpcode() != ISD::ADD ||
8119 UI.getUse().getResNo() != Addr.getResNo())
8120 continue;
8121
8122 // Check that the add is independent of the load/store. Otherwise, folding
8123 // it would create a cycle.
8124 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8125 continue;
8126
8127 // Find the new opcode for the updating load/store.
8128 bool isLoad = true;
8129 bool isLaneOp = false;
8130 unsigned NewOpc = 0;
8131 unsigned NumVecs = 0;
8132 if (isIntrinsic) {
8133 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8134 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008135 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008136 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8137 NumVecs = 1; break;
8138 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8139 NumVecs = 2; break;
8140 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8141 NumVecs = 3; break;
8142 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8143 NumVecs = 4; break;
8144 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8145 NumVecs = 2; isLaneOp = true; break;
8146 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8147 NumVecs = 3; isLaneOp = true; break;
8148 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8149 NumVecs = 4; isLaneOp = true; break;
8150 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8151 NumVecs = 1; isLoad = false; break;
8152 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8153 NumVecs = 2; isLoad = false; break;
8154 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8155 NumVecs = 3; isLoad = false; break;
8156 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8157 NumVecs = 4; isLoad = false; break;
8158 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8159 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8160 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8161 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8162 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8163 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8164 }
8165 } else {
8166 isLaneOp = true;
8167 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008168 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008169 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8170 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8171 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8172 }
8173 }
8174
8175 // Find the size of memory referenced by the load/store.
8176 EVT VecTy;
8177 if (isLoad)
8178 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008179 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008180 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8181 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8182 if (isLaneOp)
8183 NumBytes /= VecTy.getVectorNumElements();
8184
8185 // If the increment is a constant, it must match the memory ref size.
8186 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8187 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8188 uint64_t IncVal = CInc->getZExtValue();
8189 if (IncVal != NumBytes)
8190 continue;
8191 } else if (NumBytes >= 3 * 16) {
8192 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8193 // separate instructions that make it harder to use a non-constant update.
8194 continue;
8195 }
8196
8197 // Create the new updating load/store node.
8198 EVT Tys[6];
8199 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8200 unsigned n;
8201 for (n = 0; n < NumResultVecs; ++n)
8202 Tys[n] = VecTy;
8203 Tys[n++] = MVT::i32;
8204 Tys[n] = MVT::Other;
8205 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8206 SmallVector<SDValue, 8> Ops;
8207 Ops.push_back(N->getOperand(0)); // incoming chain
8208 Ops.push_back(N->getOperand(AddrOpIdx));
8209 Ops.push_back(Inc);
8210 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8211 Ops.push_back(N->getOperand(i));
8212 }
8213 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8214 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8215 Ops.data(), Ops.size(),
8216 MemInt->getMemoryVT(),
8217 MemInt->getMemOperand());
8218
8219 // Update the uses.
8220 std::vector<SDValue> NewResults;
8221 for (unsigned i = 0; i < NumResultVecs; ++i) {
8222 NewResults.push_back(SDValue(UpdN.getNode(), i));
8223 }
8224 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8225 DCI.CombineTo(N, NewResults);
8226 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8227
8228 break;
Owen Anderson76706012011-04-05 21:48:57 +00008229 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008230 return SDValue();
8231}
8232
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008233/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8234/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8235/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8236/// return true.
8237static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8238 SelectionDAG &DAG = DCI.DAG;
8239 EVT VT = N->getValueType(0);
8240 // vldN-dup instructions only support 64-bit vectors for N > 1.
8241 if (!VT.is64BitVector())
8242 return false;
8243
8244 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8245 SDNode *VLD = N->getOperand(0).getNode();
8246 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8247 return false;
8248 unsigned NumVecs = 0;
8249 unsigned NewOpc = 0;
8250 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8251 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8252 NumVecs = 2;
8253 NewOpc = ARMISD::VLD2DUP;
8254 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8255 NumVecs = 3;
8256 NewOpc = ARMISD::VLD3DUP;
8257 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8258 NumVecs = 4;
8259 NewOpc = ARMISD::VLD4DUP;
8260 } else {
8261 return false;
8262 }
8263
8264 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8265 // numbers match the load.
8266 unsigned VLDLaneNo =
8267 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8268 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8269 UI != UE; ++UI) {
8270 // Ignore uses of the chain result.
8271 if (UI.getUse().getResNo() == NumVecs)
8272 continue;
8273 SDNode *User = *UI;
8274 if (User->getOpcode() != ARMISD::VDUPLANE ||
8275 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8276 return false;
8277 }
8278
8279 // Create the vldN-dup node.
8280 EVT Tys[5];
8281 unsigned n;
8282 for (n = 0; n < NumVecs; ++n)
8283 Tys[n] = VT;
8284 Tys[n] = MVT::Other;
8285 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8286 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8287 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8288 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8289 Ops, 2, VLDMemInt->getMemoryVT(),
8290 VLDMemInt->getMemOperand());
8291
8292 // Update the uses.
8293 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8294 UI != UE; ++UI) {
8295 unsigned ResNo = UI.getUse().getResNo();
8296 // Ignore uses of the chain result.
8297 if (ResNo == NumVecs)
8298 continue;
8299 SDNode *User = *UI;
8300 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8301 }
8302
8303 // Now the vldN-lane intrinsic is dead except for its chain result.
8304 // Update uses of the chain.
8305 std::vector<SDValue> VLDDupResults;
8306 for (unsigned n = 0; n < NumVecs; ++n)
8307 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8308 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8309 DCI.CombineTo(VLD, VLDDupResults);
8310
8311 return true;
8312}
8313
Bob Wilson9e82bf12010-07-14 01:22:12 +00008314/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8315/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008316static SDValue PerformVDUPLANECombine(SDNode *N,
8317 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008318 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008319
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008320 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8321 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8322 if (CombineVLDDUP(N, DCI))
8323 return SDValue(N, 0);
8324
8325 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8326 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008327 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008328 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008329 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008330 return SDValue();
8331
8332 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8333 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8334 // The canonical VMOV for a zero vector uses a 32-bit element size.
8335 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8336 unsigned EltBits;
8337 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8338 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008339 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008340 if (EltSize > VT.getVectorElementType().getSizeInBits())
8341 return SDValue();
8342
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008343 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008344}
8345
Eric Christopherfa6f5912011-06-29 21:10:36 +00008346// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008347// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8348static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8349{
Chad Rosier118c9a02011-06-28 17:26:57 +00008350 integerPart cN;
8351 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008352 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8353 I != E; I++) {
8354 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8355 if (!C)
8356 return false;
8357
Eric Christopherfa6f5912011-06-29 21:10:36 +00008358 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008359 APFloat APF = C->getValueAPF();
8360 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8361 != APFloat::opOK || !isExact)
8362 return false;
8363
8364 c0 = (I == 0) ? cN : c0;
8365 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8366 return false;
8367 }
8368 C = c0;
8369 return true;
8370}
8371
8372/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8373/// can replace combinations of VMUL and VCVT (floating-point to integer)
8374/// when the VMUL has a constant operand that is a power of 2.
8375///
8376/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8377/// vmul.f32 d16, d17, d16
8378/// vcvt.s32.f32 d16, d16
8379/// becomes:
8380/// vcvt.s32.f32 d16, d16, #3
8381static SDValue PerformVCVTCombine(SDNode *N,
8382 TargetLowering::DAGCombinerInfo &DCI,
8383 const ARMSubtarget *Subtarget) {
8384 SelectionDAG &DAG = DCI.DAG;
8385 SDValue Op = N->getOperand(0);
8386
8387 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8388 Op.getOpcode() != ISD::FMUL)
8389 return SDValue();
8390
8391 uint64_t C;
8392 SDValue N0 = Op->getOperand(0);
8393 SDValue ConstVec = Op->getOperand(1);
8394 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8395
Eric Christopherfa6f5912011-06-29 21:10:36 +00008396 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008397 !isConstVecPow2(ConstVec, isSigned, C))
8398 return SDValue();
8399
8400 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8401 Intrinsic::arm_neon_vcvtfp2fxu;
8402 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8403 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008404 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008405 DAG.getConstant(Log2_64(C), MVT::i32));
8406}
8407
8408/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8409/// can replace combinations of VCVT (integer to floating-point) and VDIV
8410/// when the VDIV has a constant operand that is a power of 2.
8411///
8412/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8413/// vcvt.f32.s32 d16, d16
8414/// vdiv.f32 d16, d17, d16
8415/// becomes:
8416/// vcvt.f32.s32 d16, d16, #3
8417static SDValue PerformVDIVCombine(SDNode *N,
8418 TargetLowering::DAGCombinerInfo &DCI,
8419 const ARMSubtarget *Subtarget) {
8420 SelectionDAG &DAG = DCI.DAG;
8421 SDValue Op = N->getOperand(0);
8422 unsigned OpOpcode = Op.getNode()->getOpcode();
8423
8424 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8425 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8426 return SDValue();
8427
8428 uint64_t C;
8429 SDValue ConstVec = N->getOperand(1);
8430 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8431
8432 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8433 !isConstVecPow2(ConstVec, isSigned, C))
8434 return SDValue();
8435
Eric Christopherfa6f5912011-06-29 21:10:36 +00008436 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008437 Intrinsic::arm_neon_vcvtfxu2fp;
8438 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8439 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008440 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008441 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8442}
8443
8444/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008445/// operand of a vector shift operation, where all the elements of the
8446/// build_vector must have the same constant integer value.
8447static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8448 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008449 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008450 Op = Op.getOperand(0);
8451 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8452 APInt SplatBits, SplatUndef;
8453 unsigned SplatBitSize;
8454 bool HasAnyUndefs;
8455 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8456 HasAnyUndefs, ElementBits) ||
8457 SplatBitSize > ElementBits)
8458 return false;
8459 Cnt = SplatBits.getSExtValue();
8460 return true;
8461}
8462
8463/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8464/// operand of a vector shift left operation. That value must be in the range:
8465/// 0 <= Value < ElementBits for a left shift; or
8466/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008467static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008468 assert(VT.isVector() && "vector shift count is not a vector type");
8469 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8470 if (! getVShiftImm(Op, ElementBits, Cnt))
8471 return false;
8472 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8473}
8474
8475/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8476/// operand of a vector shift right operation. For a shift opcode, the value
8477/// is positive, but for an intrinsic the value count must be negative. The
8478/// absolute value must be in the range:
8479/// 1 <= |Value| <= ElementBits for a right shift; or
8480/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008481static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008482 int64_t &Cnt) {
8483 assert(VT.isVector() && "vector shift count is not a vector type");
8484 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8485 if (! getVShiftImm(Op, ElementBits, Cnt))
8486 return false;
8487 if (isIntrinsic)
8488 Cnt = -Cnt;
8489 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8490}
8491
8492/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8493static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8494 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8495 switch (IntNo) {
8496 default:
8497 // Don't do anything for most intrinsics.
8498 break;
8499
8500 // Vector shifts: check for immediate versions and lower them.
8501 // Note: This is done during DAG combining instead of DAG legalizing because
8502 // the build_vectors for 64-bit vector element shift counts are generally
8503 // not legal, and it is hard to see their values after they get legalized to
8504 // loads from a constant pool.
8505 case Intrinsic::arm_neon_vshifts:
8506 case Intrinsic::arm_neon_vshiftu:
8507 case Intrinsic::arm_neon_vshiftls:
8508 case Intrinsic::arm_neon_vshiftlu:
8509 case Intrinsic::arm_neon_vshiftn:
8510 case Intrinsic::arm_neon_vrshifts:
8511 case Intrinsic::arm_neon_vrshiftu:
8512 case Intrinsic::arm_neon_vrshiftn:
8513 case Intrinsic::arm_neon_vqshifts:
8514 case Intrinsic::arm_neon_vqshiftu:
8515 case Intrinsic::arm_neon_vqshiftsu:
8516 case Intrinsic::arm_neon_vqshiftns:
8517 case Intrinsic::arm_neon_vqshiftnu:
8518 case Intrinsic::arm_neon_vqshiftnsu:
8519 case Intrinsic::arm_neon_vqrshiftns:
8520 case Intrinsic::arm_neon_vqrshiftnu:
8521 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008522 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008523 int64_t Cnt;
8524 unsigned VShiftOpc = 0;
8525
8526 switch (IntNo) {
8527 case Intrinsic::arm_neon_vshifts:
8528 case Intrinsic::arm_neon_vshiftu:
8529 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8530 VShiftOpc = ARMISD::VSHL;
8531 break;
8532 }
8533 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8534 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8535 ARMISD::VSHRs : ARMISD::VSHRu);
8536 break;
8537 }
8538 return SDValue();
8539
8540 case Intrinsic::arm_neon_vshiftls:
8541 case Intrinsic::arm_neon_vshiftlu:
8542 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8543 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008544 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008545
8546 case Intrinsic::arm_neon_vrshifts:
8547 case Intrinsic::arm_neon_vrshiftu:
8548 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8549 break;
8550 return SDValue();
8551
8552 case Intrinsic::arm_neon_vqshifts:
8553 case Intrinsic::arm_neon_vqshiftu:
8554 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8555 break;
8556 return SDValue();
8557
8558 case Intrinsic::arm_neon_vqshiftsu:
8559 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8560 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008561 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008562
8563 case Intrinsic::arm_neon_vshiftn:
8564 case Intrinsic::arm_neon_vrshiftn:
8565 case Intrinsic::arm_neon_vqshiftns:
8566 case Intrinsic::arm_neon_vqshiftnu:
8567 case Intrinsic::arm_neon_vqshiftnsu:
8568 case Intrinsic::arm_neon_vqrshiftns:
8569 case Intrinsic::arm_neon_vqrshiftnu:
8570 case Intrinsic::arm_neon_vqrshiftnsu:
8571 // Narrowing shifts require an immediate right shift.
8572 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8573 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008574 llvm_unreachable("invalid shift count for narrowing vector shift "
8575 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008576
8577 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008578 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008579 }
8580
8581 switch (IntNo) {
8582 case Intrinsic::arm_neon_vshifts:
8583 case Intrinsic::arm_neon_vshiftu:
8584 // Opcode already set above.
8585 break;
8586 case Intrinsic::arm_neon_vshiftls:
8587 case Intrinsic::arm_neon_vshiftlu:
8588 if (Cnt == VT.getVectorElementType().getSizeInBits())
8589 VShiftOpc = ARMISD::VSHLLi;
8590 else
8591 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8592 ARMISD::VSHLLs : ARMISD::VSHLLu);
8593 break;
8594 case Intrinsic::arm_neon_vshiftn:
8595 VShiftOpc = ARMISD::VSHRN; break;
8596 case Intrinsic::arm_neon_vrshifts:
8597 VShiftOpc = ARMISD::VRSHRs; break;
8598 case Intrinsic::arm_neon_vrshiftu:
8599 VShiftOpc = ARMISD::VRSHRu; break;
8600 case Intrinsic::arm_neon_vrshiftn:
8601 VShiftOpc = ARMISD::VRSHRN; break;
8602 case Intrinsic::arm_neon_vqshifts:
8603 VShiftOpc = ARMISD::VQSHLs; break;
8604 case Intrinsic::arm_neon_vqshiftu:
8605 VShiftOpc = ARMISD::VQSHLu; break;
8606 case Intrinsic::arm_neon_vqshiftsu:
8607 VShiftOpc = ARMISD::VQSHLsu; break;
8608 case Intrinsic::arm_neon_vqshiftns:
8609 VShiftOpc = ARMISD::VQSHRNs; break;
8610 case Intrinsic::arm_neon_vqshiftnu:
8611 VShiftOpc = ARMISD::VQSHRNu; break;
8612 case Intrinsic::arm_neon_vqshiftnsu:
8613 VShiftOpc = ARMISD::VQSHRNsu; break;
8614 case Intrinsic::arm_neon_vqrshiftns:
8615 VShiftOpc = ARMISD::VQRSHRNs; break;
8616 case Intrinsic::arm_neon_vqrshiftnu:
8617 VShiftOpc = ARMISD::VQRSHRNu; break;
8618 case Intrinsic::arm_neon_vqrshiftnsu:
8619 VShiftOpc = ARMISD::VQRSHRNsu; break;
8620 }
8621
8622 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008623 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008624 }
8625
8626 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008627 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008628 int64_t Cnt;
8629 unsigned VShiftOpc = 0;
8630
8631 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8632 VShiftOpc = ARMISD::VSLI;
8633 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8634 VShiftOpc = ARMISD::VSRI;
8635 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008636 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008637 }
8638
8639 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8640 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008641 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008642 }
8643
8644 case Intrinsic::arm_neon_vqrshifts:
8645 case Intrinsic::arm_neon_vqrshiftu:
8646 // No immediate versions of these to check for.
8647 break;
8648 }
8649
8650 return SDValue();
8651}
8652
8653/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8654/// lowers them. As with the vector shift intrinsics, this is done during DAG
8655/// combining instead of DAG legalizing because the build_vectors for 64-bit
8656/// vector element shift counts are generally not legal, and it is hard to see
8657/// their values after they get legalized to loads from a constant pool.
8658static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8659 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008660 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008661 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8662 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8663 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8664 SDValue N1 = N->getOperand(1);
8665 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8666 SDValue N0 = N->getOperand(0);
8667 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8668 DAG.MaskedValueIsZero(N0.getOperand(0),
8669 APInt::getHighBitsSet(32, 16)))
8670 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8671 }
8672 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008673
8674 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008675 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8676 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008677 return SDValue();
8678
8679 assert(ST->hasNEON() && "unexpected vector shift");
8680 int64_t Cnt;
8681
8682 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008683 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008684
8685 case ISD::SHL:
8686 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8687 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008688 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008689 break;
8690
8691 case ISD::SRA:
8692 case ISD::SRL:
8693 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8694 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8695 ARMISD::VSHRs : ARMISD::VSHRu);
8696 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008698 }
8699 }
8700 return SDValue();
8701}
8702
8703/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8704/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8705static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8706 const ARMSubtarget *ST) {
8707 SDValue N0 = N->getOperand(0);
8708
8709 // Check for sign- and zero-extensions of vector extract operations of 8-
8710 // and 16-bit vector elements. NEON supports these directly. They are
8711 // handled during DAG combining because type legalization will promote them
8712 // to 32-bit types and it is messy to recognize the operations after that.
8713 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8714 SDValue Vec = N0.getOperand(0);
8715 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008716 EVT VT = N->getValueType(0);
8717 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008718 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8719
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 if (VT == MVT::i32 &&
8721 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008722 TLI.isTypeLegal(Vec.getValueType()) &&
8723 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008724
8725 unsigned Opc = 0;
8726 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008727 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008728 case ISD::SIGN_EXTEND:
8729 Opc = ARMISD::VGETLANEs;
8730 break;
8731 case ISD::ZERO_EXTEND:
8732 case ISD::ANY_EXTEND:
8733 Opc = ARMISD::VGETLANEu;
8734 break;
8735 }
8736 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8737 }
8738 }
8739
8740 return SDValue();
8741}
8742
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008743/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8744/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8745static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8746 const ARMSubtarget *ST) {
8747 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008748 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008749 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8750 // a NaN; only do the transformation when it matches that behavior.
8751
8752 // For now only do this when using NEON for FP operations; if using VFP, it
8753 // is not obvious that the benefit outweighs the cost of switching to the
8754 // NEON pipeline.
8755 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8756 N->getValueType(0) != MVT::f32)
8757 return SDValue();
8758
8759 SDValue CondLHS = N->getOperand(0);
8760 SDValue CondRHS = N->getOperand(1);
8761 SDValue LHS = N->getOperand(2);
8762 SDValue RHS = N->getOperand(3);
8763 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8764
8765 unsigned Opcode = 0;
8766 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008767 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008768 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008769 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008770 IsReversed = true ; // x CC y ? y : x
8771 } else {
8772 return SDValue();
8773 }
8774
Bob Wilsone742bb52010-02-24 22:15:53 +00008775 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008776 switch (CC) {
8777 default: break;
8778 case ISD::SETOLT:
8779 case ISD::SETOLE:
8780 case ISD::SETLT:
8781 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008782 case ISD::SETULT:
8783 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008784 // If LHS is NaN, an ordered comparison will be false and the result will
8785 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8786 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8787 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8788 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8789 break;
8790 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8791 // will return -0, so vmin can only be used for unsafe math or if one of
8792 // the operands is known to be nonzero.
8793 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008794 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008795 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8796 break;
8797 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008798 break;
8799
8800 case ISD::SETOGT:
8801 case ISD::SETOGE:
8802 case ISD::SETGT:
8803 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008804 case ISD::SETUGT:
8805 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008806 // If LHS is NaN, an ordered comparison will be false and the result will
8807 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8808 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8809 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8810 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8811 break;
8812 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8813 // will return +0, so vmax can only be used for unsafe math or if one of
8814 // the operands is known to be nonzero.
8815 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008816 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008817 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8818 break;
8819 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008820 break;
8821 }
8822
8823 if (!Opcode)
8824 return SDValue();
8825 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8826}
8827
Evan Chenge721f5c2011-07-13 00:42:17 +00008828/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8829SDValue
8830ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8831 SDValue Cmp = N->getOperand(4);
8832 if (Cmp.getOpcode() != ARMISD::CMPZ)
8833 // Only looking at EQ and NE cases.
8834 return SDValue();
8835
8836 EVT VT = N->getValueType(0);
8837 DebugLoc dl = N->getDebugLoc();
8838 SDValue LHS = Cmp.getOperand(0);
8839 SDValue RHS = Cmp.getOperand(1);
8840 SDValue FalseVal = N->getOperand(0);
8841 SDValue TrueVal = N->getOperand(1);
8842 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008843 ARMCC::CondCodes CC =
8844 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008845
8846 // Simplify
8847 // mov r1, r0
8848 // cmp r1, x
8849 // mov r0, y
8850 // moveq r0, x
8851 // to
8852 // cmp r0, x
8853 // movne r0, y
8854 //
8855 // mov r1, r0
8856 // cmp r1, x
8857 // mov r0, x
8858 // movne r0, y
8859 // to
8860 // cmp r0, x
8861 // movne r0, y
8862 /// FIXME: Turn this into a target neutral optimization?
8863 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008864 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008865 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8866 N->getOperand(3), Cmp);
8867 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8868 SDValue ARMcc;
8869 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8870 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8871 N->getOperand(3), NewCmp);
8872 }
8873
8874 if (Res.getNode()) {
8875 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008876 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008877 // Capture demanded bits information that would be otherwise lost.
8878 if (KnownZero == 0xfffffffe)
8879 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8880 DAG.getValueType(MVT::i1));
8881 else if (KnownZero == 0xffffff00)
8882 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8883 DAG.getValueType(MVT::i8));
8884 else if (KnownZero == 0xffff0000)
8885 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8886 DAG.getValueType(MVT::i16));
8887 }
8888
8889 return Res;
8890}
8891
Dan Gohman475871a2008-07-27 21:46:04 +00008892SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008893 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008894 switch (N->getOpcode()) {
8895 default: break;
Arnold Schwaighoferbcc4c1d2012-08-09 15:25:52 +00008896 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00008897 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008898 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008899 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008900 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008901 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8902 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008903 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008904 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008905 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008906 case ISD::STORE: return PerformSTORECombine(N, DCI);
8907 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8908 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008909 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008910 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008911 case ISD::FP_TO_SINT:
8912 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8913 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008914 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008915 case ISD::SHL:
8916 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008917 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008918 case ISD::SIGN_EXTEND:
8919 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008920 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8921 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008922 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008923 case ARMISD::VLD2DUP:
8924 case ARMISD::VLD3DUP:
8925 case ARMISD::VLD4DUP:
8926 return CombineBaseUpdate(N, DCI);
8927 case ISD::INTRINSIC_VOID:
8928 case ISD::INTRINSIC_W_CHAIN:
8929 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8930 case Intrinsic::arm_neon_vld1:
8931 case Intrinsic::arm_neon_vld2:
8932 case Intrinsic::arm_neon_vld3:
8933 case Intrinsic::arm_neon_vld4:
8934 case Intrinsic::arm_neon_vld2lane:
8935 case Intrinsic::arm_neon_vld3lane:
8936 case Intrinsic::arm_neon_vld4lane:
8937 case Intrinsic::arm_neon_vst1:
8938 case Intrinsic::arm_neon_vst2:
8939 case Intrinsic::arm_neon_vst3:
8940 case Intrinsic::arm_neon_vst4:
8941 case Intrinsic::arm_neon_vst2lane:
8942 case Intrinsic::arm_neon_vst3lane:
8943 case Intrinsic::arm_neon_vst4lane:
8944 return CombineBaseUpdate(N, DCI);
8945 default: break;
8946 }
8947 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008948 }
Dan Gohman475871a2008-07-27 21:46:04 +00008949 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008950}
8951
Evan Cheng31959b12011-02-02 01:06:55 +00008952bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8953 EVT VT) const {
8954 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8955}
8956
Bill Wendlingaf566342009-08-15 21:21:19 +00008957bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008958 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008959 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008960
8961 switch (VT.getSimpleVT().SimpleTy) {
8962 default:
8963 return false;
8964 case MVT::i8:
8965 case MVT::i16:
8966 case MVT::i32:
8967 return true;
8968 // FIXME: VLD1 etc with standard alignment is legal.
8969 }
8970}
8971
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008972static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8973 unsigned AlignCheck) {
8974 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8975 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8976}
8977
8978EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8979 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008980 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008981 bool MemcpyStrSrc,
8982 MachineFunction &MF) const {
8983 const Function *F = MF.getFunction();
8984
8985 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008986 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008987 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8988 Subtarget->hasNEON()) {
8989 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8990 return MVT::v4i32;
8991 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8992 return MVT::v2i32;
8993 }
8994 }
8995
Lang Hames5207bf22011-11-08 18:56:23 +00008996 // Lowering to i32/i16 if the size permits.
8997 if (Size >= 4) {
8998 return MVT::i32;
8999 } else if (Size >= 2) {
9000 return MVT::i16;
9001 }
9002
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009003 // Let the target-independent logic figure it out.
9004 return MVT::Other;
9005}
9006
Evan Chenge6c835f2009-08-14 20:09:37 +00009007static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9008 if (V < 0)
9009 return false;
9010
9011 unsigned Scale = 1;
9012 switch (VT.getSimpleVT().SimpleTy) {
9013 default: return false;
9014 case MVT::i1:
9015 case MVT::i8:
9016 // Scale == 1;
9017 break;
9018 case MVT::i16:
9019 // Scale == 2;
9020 Scale = 2;
9021 break;
9022 case MVT::i32:
9023 // Scale == 4;
9024 Scale = 4;
9025 break;
9026 }
9027
9028 if ((V & (Scale - 1)) != 0)
9029 return false;
9030 V /= Scale;
9031 return V == (V & ((1LL << 5) - 1));
9032}
9033
9034static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9035 const ARMSubtarget *Subtarget) {
9036 bool isNeg = false;
9037 if (V < 0) {
9038 isNeg = true;
9039 V = - V;
9040 }
9041
9042 switch (VT.getSimpleVT().SimpleTy) {
9043 default: return false;
9044 case MVT::i1:
9045 case MVT::i8:
9046 case MVT::i16:
9047 case MVT::i32:
9048 // + imm12 or - imm8
9049 if (isNeg)
9050 return V == (V & ((1LL << 8) - 1));
9051 return V == (V & ((1LL << 12) - 1));
9052 case MVT::f32:
9053 case MVT::f64:
9054 // Same as ARM mode. FIXME: NEON?
9055 if (!Subtarget->hasVFP2())
9056 return false;
9057 if ((V & 3) != 0)
9058 return false;
9059 V >>= 2;
9060 return V == (V & ((1LL << 8) - 1));
9061 }
9062}
9063
Evan Chengb01fad62007-03-12 23:30:29 +00009064/// isLegalAddressImmediate - Return true if the integer value can be used
9065/// as the offset of the target addressing mode for load / store of the
9066/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009067static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009068 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009069 if (V == 0)
9070 return true;
9071
Evan Cheng65011532009-03-09 19:15:00 +00009072 if (!VT.isSimple())
9073 return false;
9074
Evan Chenge6c835f2009-08-14 20:09:37 +00009075 if (Subtarget->isThumb1Only())
9076 return isLegalT1AddressImmediate(V, VT);
9077 else if (Subtarget->isThumb2())
9078 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009079
Evan Chenge6c835f2009-08-14 20:09:37 +00009080 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009081 if (V < 0)
9082 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009083 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009084 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009085 case MVT::i1:
9086 case MVT::i8:
9087 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009088 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009089 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009090 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009091 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009092 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009093 case MVT::f32:
9094 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009095 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009096 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009097 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009098 return false;
9099 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009100 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009101 }
Evan Chenga8e29892007-01-19 07:51:42 +00009102}
9103
Evan Chenge6c835f2009-08-14 20:09:37 +00009104bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9105 EVT VT) const {
9106 int Scale = AM.Scale;
9107 if (Scale < 0)
9108 return false;
9109
9110 switch (VT.getSimpleVT().SimpleTy) {
9111 default: return false;
9112 case MVT::i1:
9113 case MVT::i8:
9114 case MVT::i16:
9115 case MVT::i32:
9116 if (Scale == 1)
9117 return true;
9118 // r + r << imm
9119 Scale = Scale & ~1;
9120 return Scale == 2 || Scale == 4 || Scale == 8;
9121 case MVT::i64:
9122 // r + r
9123 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9124 return true;
9125 return false;
9126 case MVT::isVoid:
9127 // Note, we allow "void" uses (basically, uses that aren't loads or
9128 // stores), because arm allows folding a scale into many arithmetic
9129 // operations. This should be made more precise and revisited later.
9130
9131 // Allow r << imm, but the imm has to be a multiple of two.
9132 if (Scale & 1) return false;
9133 return isPowerOf2_32(Scale);
9134 }
9135}
9136
Chris Lattner37caf8c2007-04-09 23:33:39 +00009137/// isLegalAddressingMode - Return true if the addressing mode represented
9138/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009139bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009140 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009141 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009142 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009143 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009144
Chris Lattner37caf8c2007-04-09 23:33:39 +00009145 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009146 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009147 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009148
Chris Lattner37caf8c2007-04-09 23:33:39 +00009149 switch (AM.Scale) {
9150 case 0: // no scale reg, must be "r+i" or "r", or "i".
9151 break;
9152 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009153 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009154 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009155 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009156 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009157 // ARM doesn't support any R+R*scale+imm addr modes.
9158 if (AM.BaseOffs)
9159 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009160
Bob Wilson2c7dab12009-04-08 17:55:28 +00009161 if (!VT.isSimple())
9162 return false;
9163
Evan Chenge6c835f2009-08-14 20:09:37 +00009164 if (Subtarget->isThumb2())
9165 return isLegalT2ScaledAddressingMode(AM, VT);
9166
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009167 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009168 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009169 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009170 case MVT::i1:
9171 case MVT::i8:
9172 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009173 if (Scale < 0) Scale = -Scale;
9174 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009175 return true;
9176 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009177 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009178 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009179 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009180 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009181 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009182 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009183 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009184
Owen Anderson825b72b2009-08-11 20:47:22 +00009185 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009186 // Note, we allow "void" uses (basically, uses that aren't loads or
9187 // stores), because arm allows folding a scale into many arithmetic
9188 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009189
Chris Lattner37caf8c2007-04-09 23:33:39 +00009190 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009191 if (Scale & 1) return false;
9192 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009193 }
Evan Chengb01fad62007-03-12 23:30:29 +00009194 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009195 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009196}
9197
Evan Cheng77e47512009-11-11 19:05:52 +00009198/// isLegalICmpImmediate - Return true if the specified immediate is legal
9199/// icmp immediate, that is the target has icmp instructions which can compare
9200/// a register against the immediate without having to materialize the
9201/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009202bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009203 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009204 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009205 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009206 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009207 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009208 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009209 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009210}
9211
Andrew Trick8d8d9612012-07-18 18:34:27 +00009212/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9213/// *or sub* immediate, that is the target has add or sub instructions which can
9214/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009215/// immediate into a register.
9216bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009217 // Same encoding for add/sub, just flip the sign.
9218 int64_t AbsImm = llvm::abs64(Imm);
9219 if (!Subtarget->isThumb())
9220 return ARM_AM::getSOImmVal(AbsImm) != -1;
9221 if (Subtarget->isThumb2())
9222 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9223 // Thumb1 only has 8-bit unsigned immediate.
9224 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009225}
9226
Owen Andersone50ed302009-08-10 22:56:29 +00009227static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009228 bool isSEXTLoad, SDValue &Base,
9229 SDValue &Offset, bool &isInc,
9230 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009231 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9232 return false;
9233
Owen Anderson825b72b2009-08-11 20:47:22 +00009234 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009235 // AddressingMode 3
9236 Base = Ptr->getOperand(0);
9237 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009238 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009239 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009240 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009241 isInc = false;
9242 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9243 return true;
9244 }
9245 }
9246 isInc = (Ptr->getOpcode() == ISD::ADD);
9247 Offset = Ptr->getOperand(1);
9248 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009249 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009250 // AddressingMode 2
9251 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009252 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009253 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009254 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009255 isInc = false;
9256 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9257 Base = Ptr->getOperand(0);
9258 return true;
9259 }
9260 }
9261
9262 if (Ptr->getOpcode() == ISD::ADD) {
9263 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009264 ARM_AM::ShiftOpc ShOpcVal=
9265 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009266 if (ShOpcVal != ARM_AM::no_shift) {
9267 Base = Ptr->getOperand(1);
9268 Offset = Ptr->getOperand(0);
9269 } else {
9270 Base = Ptr->getOperand(0);
9271 Offset = Ptr->getOperand(1);
9272 }
9273 return true;
9274 }
9275
9276 isInc = (Ptr->getOpcode() == ISD::ADD);
9277 Base = Ptr->getOperand(0);
9278 Offset = Ptr->getOperand(1);
9279 return true;
9280 }
9281
Jim Grosbache5165492009-11-09 00:11:35 +00009282 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009283 return false;
9284}
9285
Owen Andersone50ed302009-08-10 22:56:29 +00009286static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009287 bool isSEXTLoad, SDValue &Base,
9288 SDValue &Offset, bool &isInc,
9289 SelectionDAG &DAG) {
9290 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9291 return false;
9292
9293 Base = Ptr->getOperand(0);
9294 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9295 int RHSC = (int)RHS->getZExtValue();
9296 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9297 assert(Ptr->getOpcode() == ISD::ADD);
9298 isInc = false;
9299 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9300 return true;
9301 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9302 isInc = Ptr->getOpcode() == ISD::ADD;
9303 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9304 return true;
9305 }
9306 }
9307
9308 return false;
9309}
9310
Evan Chenga8e29892007-01-19 07:51:42 +00009311/// getPreIndexedAddressParts - returns true by value, base pointer and
9312/// offset pointer and addressing mode by reference if the node's address
9313/// can be legally represented as pre-indexed load / store address.
9314bool
Dan Gohman475871a2008-07-27 21:46:04 +00009315ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9316 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009317 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009318 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009319 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009320 return false;
9321
Owen Andersone50ed302009-08-10 22:56:29 +00009322 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009323 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009324 bool isSEXTLoad = false;
9325 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9326 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009327 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009328 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9329 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9330 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009331 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009332 } else
9333 return false;
9334
9335 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009336 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009337 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009338 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9339 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009340 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009341 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009342 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009343 if (!isLegal)
9344 return false;
9345
9346 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9347 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009348}
9349
9350/// getPostIndexedAddressParts - returns true by value, base pointer and
9351/// offset pointer and addressing mode by reference if this node can be
9352/// combined with a load / store to form a post-indexed load / store.
9353bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009354 SDValue &Base,
9355 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009356 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009357 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009358 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009359 return false;
9360
Owen Andersone50ed302009-08-10 22:56:29 +00009361 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009362 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009363 bool isSEXTLoad = false;
9364 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009365 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009366 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009367 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9368 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009369 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009370 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009371 } else
9372 return false;
9373
9374 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009375 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009376 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009377 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009378 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009379 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009380 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9381 isInc, DAG);
9382 if (!isLegal)
9383 return false;
9384
Evan Cheng28dad2a2010-05-18 21:31:17 +00009385 if (Ptr != Base) {
9386 // Swap base ptr and offset to catch more post-index load / store when
9387 // it's legal. In Thumb2 mode, offset must be an immediate.
9388 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9389 !Subtarget->isThumb2())
9390 std::swap(Base, Offset);
9391
9392 // Post-indexed load / store update the base pointer.
9393 if (Ptr != Base)
9394 return false;
9395 }
9396
Evan Chenge88d5ce2009-07-02 07:28:31 +00009397 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9398 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009399}
9400
Dan Gohman475871a2008-07-27 21:46:04 +00009401void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009402 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009403 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009404 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009405 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009406 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009407 switch (Op.getOpcode()) {
9408 default: break;
9409 case ARMISD::CMOV: {
9410 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009411 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009412 if (KnownZero == 0 && KnownOne == 0) return;
9413
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009414 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009415 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009416 KnownZero &= KnownZeroRHS;
9417 KnownOne &= KnownOneRHS;
9418 return;
9419 }
9420 }
9421}
9422
9423//===----------------------------------------------------------------------===//
9424// ARM Inline Assembly Support
9425//===----------------------------------------------------------------------===//
9426
Evan Cheng55d42002011-01-08 01:24:27 +00009427bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9428 // Looking for "rev" which is V6+.
9429 if (!Subtarget->hasV6Ops())
9430 return false;
9431
9432 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9433 std::string AsmStr = IA->getAsmString();
9434 SmallVector<StringRef, 4> AsmPieces;
9435 SplitString(AsmStr, AsmPieces, ";\n");
9436
9437 switch (AsmPieces.size()) {
9438 default: return false;
9439 case 1:
9440 AsmStr = AsmPieces[0];
9441 AsmPieces.clear();
9442 SplitString(AsmStr, AsmPieces, " \t,");
9443
9444 // rev $0, $1
9445 if (AsmPieces.size() == 3 &&
9446 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9447 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009448 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009449 if (Ty && Ty->getBitWidth() == 32)
9450 return IntrinsicLowering::LowerToByteSwap(CI);
9451 }
9452 break;
9453 }
9454
9455 return false;
9456}
9457
Evan Chenga8e29892007-01-19 07:51:42 +00009458/// getConstraintType - Given a constraint letter, return the type of
9459/// constraint it is for this target.
9460ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009461ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9462 if (Constraint.size() == 1) {
9463 switch (Constraint[0]) {
9464 default: break;
9465 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009466 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009467 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009468 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009469 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009470 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009471 // An address with a single base register. Due to the way we
9472 // currently handle addresses it is the same as an 'r' memory constraint.
9473 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009474 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009475 } else if (Constraint.size() == 2) {
9476 switch (Constraint[0]) {
9477 default: break;
9478 // All 'U+' constraints are addresses.
9479 case 'U': return C_Memory;
9480 }
Evan Chenga8e29892007-01-19 07:51:42 +00009481 }
Chris Lattner4234f572007-03-25 02:14:49 +00009482 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009483}
9484
John Thompson44ab89e2010-10-29 17:29:13 +00009485/// Examine constraint type and operand type and determine a weight value.
9486/// This object must already have been set up with the operand type
9487/// and the current alternative constraint selected.
9488TargetLowering::ConstraintWeight
9489ARMTargetLowering::getSingleConstraintMatchWeight(
9490 AsmOperandInfo &info, const char *constraint) const {
9491 ConstraintWeight weight = CW_Invalid;
9492 Value *CallOperandVal = info.CallOperandVal;
9493 // If we don't have a value, we can't do a match,
9494 // but allow it at the lowest weight.
9495 if (CallOperandVal == NULL)
9496 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009497 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009498 // Look at the constraint type.
9499 switch (*constraint) {
9500 default:
9501 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9502 break;
9503 case 'l':
9504 if (type->isIntegerTy()) {
9505 if (Subtarget->isThumb())
9506 weight = CW_SpecificReg;
9507 else
9508 weight = CW_Register;
9509 }
9510 break;
9511 case 'w':
9512 if (type->isFloatingPointTy())
9513 weight = CW_Register;
9514 break;
9515 }
9516 return weight;
9517}
9518
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009519typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9520RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009521ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009522 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009523 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009524 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009525 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009526 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009527 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009528 return RCPair(0U, &ARM::tGPRRegClass);
9529 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009530 case 'h': // High regs or no regs.
9531 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009532 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009533 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009534 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009535 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009536 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009537 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009538 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009539 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009540 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009541 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009542 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009543 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009544 case 'x':
9545 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009546 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009547 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009548 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009549 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009550 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009551 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009552 case 't':
9553 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009554 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009555 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009556 }
9557 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009558 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009559 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009560
Evan Chenga8e29892007-01-19 07:51:42 +00009561 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9562}
9563
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009564/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9565/// vector. If it is invalid, don't add anything to Ops.
9566void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009567 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009568 std::vector<SDValue>&Ops,
9569 SelectionDAG &DAG) const {
9570 SDValue Result(0, 0);
9571
Eric Christopher100c8332011-06-02 23:16:42 +00009572 // Currently only support length 1 constraints.
9573 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009574
Eric Christopher100c8332011-06-02 23:16:42 +00009575 char ConstraintLetter = Constraint[0];
9576 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009577 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009578 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009579 case 'I': case 'J': case 'K': case 'L':
9580 case 'M': case 'N': case 'O':
9581 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9582 if (!C)
9583 return;
9584
9585 int64_t CVal64 = C->getSExtValue();
9586 int CVal = (int) CVal64;
9587 // None of these constraints allow values larger than 32 bits. Check
9588 // that the value fits in an int.
9589 if (CVal != CVal64)
9590 return;
9591
Eric Christopher100c8332011-06-02 23:16:42 +00009592 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009593 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009594 // Constant suitable for movw, must be between 0 and
9595 // 65535.
9596 if (Subtarget->hasV6T2Ops())
9597 if (CVal >= 0 && CVal <= 65535)
9598 break;
9599 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009600 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009601 if (Subtarget->isThumb1Only()) {
9602 // This must be a constant between 0 and 255, for ADD
9603 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009604 if (CVal >= 0 && CVal <= 255)
9605 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009606 } else if (Subtarget->isThumb2()) {
9607 // A constant that can be used as an immediate value in a
9608 // data-processing instruction.
9609 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9610 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009611 } else {
9612 // A constant that can be used as an immediate value in a
9613 // data-processing instruction.
9614 if (ARM_AM::getSOImmVal(CVal) != -1)
9615 break;
9616 }
9617 return;
9618
9619 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009620 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009621 // This must be a constant between -255 and -1, for negated ADD
9622 // immediates. This can be used in GCC with an "n" modifier that
9623 // prints the negated value, for use with SUB instructions. It is
9624 // not useful otherwise but is implemented for compatibility.
9625 if (CVal >= -255 && CVal <= -1)
9626 break;
9627 } else {
9628 // This must be a constant between -4095 and 4095. It is not clear
9629 // what this constraint is intended for. Implemented for
9630 // compatibility with GCC.
9631 if (CVal >= -4095 && CVal <= 4095)
9632 break;
9633 }
9634 return;
9635
9636 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009637 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009638 // A 32-bit value where only one byte has a nonzero value. Exclude
9639 // zero to match GCC. This constraint is used by GCC internally for
9640 // constants that can be loaded with a move/shift combination.
9641 // It is not useful otherwise but is implemented for compatibility.
9642 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9643 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009644 } else if (Subtarget->isThumb2()) {
9645 // A constant whose bitwise inverse can be used as an immediate
9646 // value in a data-processing instruction. This can be used in GCC
9647 // with a "B" modifier that prints the inverted value, for use with
9648 // BIC and MVN instructions. It is not useful otherwise but is
9649 // implemented for compatibility.
9650 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9651 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009652 } else {
9653 // A constant whose bitwise inverse can be used as an immediate
9654 // value in a data-processing instruction. This can be used in GCC
9655 // with a "B" modifier that prints the inverted value, for use with
9656 // BIC and MVN instructions. It is not useful otherwise but is
9657 // implemented for compatibility.
9658 if (ARM_AM::getSOImmVal(~CVal) != -1)
9659 break;
9660 }
9661 return;
9662
9663 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009664 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009665 // This must be a constant between -7 and 7,
9666 // for 3-operand ADD/SUB immediate instructions.
9667 if (CVal >= -7 && CVal < 7)
9668 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009669 } else if (Subtarget->isThumb2()) {
9670 // A constant whose negation can be used as an immediate value in a
9671 // data-processing instruction. This can be used in GCC with an "n"
9672 // modifier that prints the negated value, for use with SUB
9673 // instructions. It is not useful otherwise but is implemented for
9674 // compatibility.
9675 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9676 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009677 } else {
9678 // A constant whose negation can be used as an immediate value in a
9679 // data-processing instruction. This can be used in GCC with an "n"
9680 // modifier that prints the negated value, for use with SUB
9681 // instructions. It is not useful otherwise but is implemented for
9682 // compatibility.
9683 if (ARM_AM::getSOImmVal(-CVal) != -1)
9684 break;
9685 }
9686 return;
9687
9688 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009689 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009690 // This must be a multiple of 4 between 0 and 1020, for
9691 // ADD sp + immediate.
9692 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9693 break;
9694 } else {
9695 // A power of two or a constant between 0 and 32. This is used in
9696 // GCC for the shift amount on shifted register operands, but it is
9697 // useful in general for any shift amounts.
9698 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9699 break;
9700 }
9701 return;
9702
9703 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009704 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009705 // This must be a constant between 0 and 31, for shift amounts.
9706 if (CVal >= 0 && CVal <= 31)
9707 break;
9708 }
9709 return;
9710
9711 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009712 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009713 // This must be a multiple of 4 between -508 and 508, for
9714 // ADD/SUB sp = sp + immediate.
9715 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9716 break;
9717 }
9718 return;
9719 }
9720 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9721 break;
9722 }
9723
9724 if (Result.getNode()) {
9725 Ops.push_back(Result);
9726 return;
9727 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009728 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009729}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009730
9731bool
9732ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9733 // The ARM target isn't yet aware of offsets.
9734 return false;
9735}
Evan Cheng39382422009-10-28 01:44:26 +00009736
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009737bool ARM::isBitFieldInvertedMask(unsigned v) {
9738 if (v == 0xffffffff)
9739 return 0;
9740 // there can be 1's on either or both "outsides", all the "inside"
9741 // bits must be 0's
9742 unsigned int lsb = 0, msb = 31;
9743 while (v & (1 << msb)) --msb;
9744 while (v & (1 << lsb)) ++lsb;
9745 for (unsigned int i = lsb; i <= msb; ++i) {
9746 if (v & (1 << i))
9747 return 0;
9748 }
9749 return 1;
9750}
9751
Evan Cheng39382422009-10-28 01:44:26 +00009752/// isFPImmLegal - Returns true if the target can instruction select the
9753/// specified FP immediate natively. If false, the legalizer will
9754/// materialize the FP immediate as a load from a constant pool.
9755bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9756 if (!Subtarget->hasVFP3())
9757 return false;
9758 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009759 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009760 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009761 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009762 return false;
9763}
Bob Wilson65ffec42010-09-21 17:56:22 +00009764
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009765/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009766/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9767/// specified in the intrinsic calls.
9768bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9769 const CallInst &I,
9770 unsigned Intrinsic) const {
9771 switch (Intrinsic) {
9772 case Intrinsic::arm_neon_vld1:
9773 case Intrinsic::arm_neon_vld2:
9774 case Intrinsic::arm_neon_vld3:
9775 case Intrinsic::arm_neon_vld4:
9776 case Intrinsic::arm_neon_vld2lane:
9777 case Intrinsic::arm_neon_vld3lane:
9778 case Intrinsic::arm_neon_vld4lane: {
9779 Info.opc = ISD::INTRINSIC_W_CHAIN;
9780 // Conservatively set memVT to the entire set of vectors loaded.
9781 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9782 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9783 Info.ptrVal = I.getArgOperand(0);
9784 Info.offset = 0;
9785 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9786 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9787 Info.vol = false; // volatile loads with NEON intrinsics not supported
9788 Info.readMem = true;
9789 Info.writeMem = false;
9790 return true;
9791 }
9792 case Intrinsic::arm_neon_vst1:
9793 case Intrinsic::arm_neon_vst2:
9794 case Intrinsic::arm_neon_vst3:
9795 case Intrinsic::arm_neon_vst4:
9796 case Intrinsic::arm_neon_vst2lane:
9797 case Intrinsic::arm_neon_vst3lane:
9798 case Intrinsic::arm_neon_vst4lane: {
9799 Info.opc = ISD::INTRINSIC_VOID;
9800 // Conservatively set memVT to the entire set of vectors stored.
9801 unsigned NumElts = 0;
9802 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009803 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009804 if (!ArgTy->isVectorTy())
9805 break;
9806 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9807 }
9808 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9809 Info.ptrVal = I.getArgOperand(0);
9810 Info.offset = 0;
9811 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9812 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9813 Info.vol = false; // volatile stores with NEON intrinsics not supported
9814 Info.readMem = false;
9815 Info.writeMem = true;
9816 return true;
9817 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009818 case Intrinsic::arm_strexd: {
9819 Info.opc = ISD::INTRINSIC_W_CHAIN;
9820 Info.memVT = MVT::i64;
9821 Info.ptrVal = I.getArgOperand(2);
9822 Info.offset = 0;
9823 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009824 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009825 Info.readMem = false;
9826 Info.writeMem = true;
9827 return true;
9828 }
9829 case Intrinsic::arm_ldrexd: {
9830 Info.opc = ISD::INTRINSIC_W_CHAIN;
9831 Info.memVT = MVT::i64;
9832 Info.ptrVal = I.getArgOperand(0);
9833 Info.offset = 0;
9834 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009835 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009836 Info.readMem = true;
9837 Info.writeMem = false;
9838 return true;
9839 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009840 default:
9841 break;
9842 }
9843
9844 return false;
9845}