blob: 7d8222927bbeb311fa5883121c504cf23a00462f [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
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000574 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000575
576 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000578
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000579 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000583 if (!Subtarget->isThumb1Only()) {
584 for (unsigned im = (unsigned)ISD::PRE_INC;
585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setIndexedLoadAction(im, MVT::i1, Legal);
587 setIndexedLoadAction(im, MVT::i8, Legal);
588 setIndexedLoadAction(im, MVT::i16, Legal);
589 setIndexedLoadAction(im, MVT::i32, Legal);
590 setIndexedStoreAction(im, MVT::i1, Legal);
591 setIndexedStoreAction(im, MVT::i8, Legal);
592 setIndexedStoreAction(im, MVT::i16, Legal);
593 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595 }
596
597 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000598 setOperationAction(ISD::MUL, MVT::i64, Expand);
599 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000600 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000606 setOperationAction(ISD::MULHS, MVT::i32, Expand);
607
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SRL, MVT::i64, Custom);
612 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Evan Cheng342e3162011-08-30 01:34:54 +0000614 if (!Subtarget->isThumb1Only()) {
615 // FIXME: We should do this for Thumb1 as well.
616 setOperationAction(ISD::ADDC, MVT::i32, Custom);
617 setOperationAction(ISD::ADDE, MVT::i32, Custom);
618 setOperationAction(ISD::SUBC, MVT::i32, Custom);
619 setOperationAction(ISD::SUBE, MVT::i32, Custom);
620 }
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000624 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Chandler Carruth63974b22011-12-13 01:56:10 +0000629 // These just redirect to CTTZ and CTLZ on ARM.
630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
632
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000633 // Only ARMv6 has BSWAP.
634 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000639 // v7M has a hardware divider
640 setOperationAction(ISD::SDIV, MVT::i32, Expand);
641 setOperationAction(ISD::UDIV, MVT::i32, Expand);
642 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::UREM, MVT::i32, Expand);
645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000654 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000655
Evan Chenga8e29892007-01-19 07:51:42 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VASTART, MVT::Other, Custom);
658 setOperationAction(ISD::VAARG, MVT::Other, Expand);
659 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
660 setOperationAction(ISD::VAEND, MVT::Other, Expand);
661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000663
664 if (!Subtarget->isTargetDarwin()) {
665 // Non-Darwin platforms may return values in these registers via the
666 // personality function.
667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
669 setExceptionPointerRegister(ARM::R0);
670 setExceptionSelectorRegister(ARM::R1);
671 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000672
Evan Cheng3a1588a2010-04-15 22:20:34 +0000673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
675 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000676 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000677 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 // membarrier needs custom lowering; the rest are legal and handled
680 // normally.
681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000683 // Custom lowering for 64-bit ops
684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
692 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 } else {
694 // Set them all for expansion, which will force libcalls.
695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
710 // Unordered/Monotonic case.
711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000713 // Since the libcalls include locking, fold in the fences
714 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 }
Evan Chenga8e29892007-01-19 07:51:42 +0000716
Evan Cheng416941d2010-11-04 05:19:35 +0000717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000718
Eli Friedmana2c6f452010-06-26 04:36:50 +0000719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
720 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000723 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
727 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
729 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
732 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000733
734 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000736 if (Subtarget->isTargetDarwin()) {
737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000740 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SETCC, MVT::i32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000745 setOperationAction(ISD::SELECT, MVT::i32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
753 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
756 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000757
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000758 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN, MVT::f64, Expand);
760 setOperationAction(ISD::FSIN, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000769 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::FPOW, MVT::f64, Expand);
771 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000773 if (!Subtarget->hasVFP4()) {
774 setOperationAction(ISD::FMA, MVT::f64, Expand);
775 setOperationAction(ISD::FMA, MVT::f32, Expand);
776 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000777
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000778 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000779 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000780 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
781 if (Subtarget->hasVFP2()) {
782 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
783 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
784 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
785 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
786 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000787 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000788 if (!Subtarget->hasFP16()) {
789 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
790 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000791 }
Evan Cheng110cf482008-04-01 01:50:16 +0000792 }
Evan Chenga8e29892007-01-19 07:51:42 +0000793
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000794 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000795 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000796 setTargetDAGCombine(ISD::ADD);
797 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000798 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000799
Evan Chengc892aeb2012-02-23 01:19:06 +0000800 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000801 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000802 setTargetDAGCombine(ISD::OR);
803 setTargetDAGCombine(ISD::XOR);
804 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000805
Evan Cheng5fb468a2012-02-23 02:58:19 +0000806 if (Subtarget->hasV6Ops())
807 setTargetDAGCombine(ISD::SRL);
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000810
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000811 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
812 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000813 setSchedulingPreference(Sched::RegPressure);
814 else
815 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000816
Evan Cheng05219282011-01-06 06:52:41 +0000817 //// temporary - rewrite interface to use type
818 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000819 maxStoresPerMemset = 16;
820 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000821
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000822 // On ARM arguments smaller than 4 bytes are extended, so all arguments
823 // are at least 4 bytes aligned.
824 setMinStackArgumentAlignment(4);
825
Evan Chengfff606d2010-09-24 19:07:23 +0000826 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000827
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000828 // Prefer likely predicted branches to selects on out-of-order cores.
829 predictableSelectIsExpensive = Subtarget->isCortexA9();
830
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000831 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000832}
833
Andrew Trick32cec0a2011-01-19 02:35:27 +0000834// FIXME: It might make sense to define the representative register class as the
835// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
836// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
837// SPR's representative would be DPR_VFP2. This should work well if register
838// pressure tracking were modified such that a register use would increment the
839// pressure of the register class's representative and all of it's super
840// classes' representatives transitively. We have not implemented this because
841// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000842// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000843// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844std::pair<const TargetRegisterClass*, uint8_t>
845ARMTargetLowering::findRepresentativeClass(EVT VT) const{
846 const TargetRegisterClass *RRC = 0;
847 uint8_t Cost = 1;
848 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000849 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000851 // Use DPR as representative register class for all floating point
852 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
853 // the cost is 1 for both f32 and f64.
854 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000855 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000856 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000857 // When NEON is used for SP, only half of the register file is available
858 // because operations that define both SP and DP results will be constrained
859 // to the VFP2 class (D0-D15). We currently model this constraint prior to
860 // coalescing by double-counting the SP regs. See the FIXME above.
861 if (Subtarget->useNEONForSinglePrecisionFP())
862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
865 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000867 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000870 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000871 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 break;
873 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000874 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000875 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000876 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000877 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000879}
880
Evan Chenga8e29892007-01-19 07:51:42 +0000881const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
882 switch (Opcode) {
883 default: return 0;
884 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000885 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000886 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000887 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
888 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000889 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
891 case ARMISD::tCALL: return "ARMISD::tCALL";
892 case ARMISD::BRCOND: return "ARMISD::BRCOND";
893 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000894 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
896 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
897 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000898 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000899 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::CMPFP: return "ARMISD::CMPFP";
901 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000902 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000903 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000904
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000906 case ARMISD::CAND: return "ARMISD::CAND";
907 case ARMISD::COR: return "ARMISD::COR";
908 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000909
Jim Grosbach3482c802010-01-18 19:58:49 +0000910 case ARMISD::RBIT: return "ARMISD::RBIT";
911
Bob Wilson76a312b2010-03-19 22:51:32 +0000912 case ARMISD::FTOSI: return "ARMISD::FTOSI";
913 case ARMISD::FTOUI: return "ARMISD::FTOUI";
914 case ARMISD::SITOF: return "ARMISD::SITOF";
915 case ARMISD::UITOF: return "ARMISD::UITOF";
916
Evan Chenga8e29892007-01-19 07:51:42 +0000917 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
918 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
919 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000920
Evan Cheng342e3162011-08-30 01:34:54 +0000921 case ARMISD::ADDC: return "ARMISD::ADDC";
922 case ARMISD::ADDE: return "ARMISD::ADDE";
923 case ARMISD::SUBC: return "ARMISD::SUBC";
924 case ARMISD::SUBE: return "ARMISD::SUBE";
925
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000926 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
927 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000928
Evan Chengc5942082009-10-28 06:55:03 +0000929 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
930 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
931
Dale Johannesen51e28e62010-06-03 21:09:53 +0000932 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000933
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000934 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000935
Evan Cheng86198642009-08-07 00:34:42 +0000936 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
937
Jim Grosbach3728e962009-12-10 00:11:09 +0000938 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000939 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000940
Evan Chengdfed19f2010-11-03 06:34:55 +0000941 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
942
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000944 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000946 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
947 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 case ARMISD::VCGEU: return "ARMISD::VCGEU";
949 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000950 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
951 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000952 case ARMISD::VCGTU: return "ARMISD::VCGTU";
953 case ARMISD::VTST: return "ARMISD::VTST";
954
955 case ARMISD::VSHL: return "ARMISD::VSHL";
956 case ARMISD::VSHRs: return "ARMISD::VSHRs";
957 case ARMISD::VSHRu: return "ARMISD::VSHRu";
958 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
959 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
960 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
961 case ARMISD::VSHRN: return "ARMISD::VSHRN";
962 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
963 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
964 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
965 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
966 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
967 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
968 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
969 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
970 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
971 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
972 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
973 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
974 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
975 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000976 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000977 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000978 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000979 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000980 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000981 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000982 case ARMISD::VREV64: return "ARMISD::VREV64";
983 case ARMISD::VREV32: return "ARMISD::VREV32";
984 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000985 case ARMISD::VZIP: return "ARMISD::VZIP";
986 case ARMISD::VUZP: return "ARMISD::VUZP";
987 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000988 case ARMISD::VTBL1: return "ARMISD::VTBL1";
989 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000990 case ARMISD::VMULLs: return "ARMISD::VMULLs";
991 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000992 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000993 case ARMISD::FMAX: return "ARMISD::FMAX";
994 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000995 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000996 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
997 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000998 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000999 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1000 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1001 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001002 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1003 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1004 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1005 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1006 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1007 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1008 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1009 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1010 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1011 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1012 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1013 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1014 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1015 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1016 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1017 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1018 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001019 }
1020}
1021
Duncan Sands28b77e92011-09-06 19:07:46 +00001022EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1023 if (!VT.isVector()) return getPointerTy();
1024 return VT.changeVectorElementTypeToInteger();
1025}
1026
Evan Cheng06b666c2010-05-15 02:18:07 +00001027/// getRegClassFor - Return the register class that should be used for the
1028/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001029const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001030 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1031 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1032 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001033 if (Subtarget->hasNEON()) {
1034 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001035 return &ARM::QQPRRegClass;
1036 if (VT == MVT::v8i64)
1037 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001038 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001039 return TargetLowering::getRegClassFor(VT);
1040}
1041
Eric Christopherab695882010-07-21 22:26:11 +00001042// Create a fast isel object.
1043FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001044ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1045 const TargetLibraryInfo *libInfo) const {
1046 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001047}
1048
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001049/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1050/// be used for loads / stores from the global.
1051unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1052 return (Subtarget->isThumb1Only() ? 127 : 4095);
1053}
1054
Evan Cheng1cc39842010-05-20 23:26:43 +00001055Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001056 unsigned NumVals = N->getNumValues();
1057 if (!NumVals)
1058 return Sched::RegPressure;
1059
1060 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001061 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001062 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001063 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001064 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001065 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001066 }
Evan Chengc10f5432010-05-28 23:25:23 +00001067
1068 if (!N->isMachineOpcode())
1069 return Sched::RegPressure;
1070
1071 // Load are scheduled for latency even if there instruction itinerary
1072 // is not available.
1073 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001074 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001075
Evan Chenge837dea2011-06-28 19:10:37 +00001076 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001077 return Sched::RegPressure;
1078 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001079 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001080 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001081
Evan Cheng1cc39842010-05-20 23:26:43 +00001082 return Sched::RegPressure;
1083}
1084
Evan Chenga8e29892007-01-19 07:51:42 +00001085//===----------------------------------------------------------------------===//
1086// Lowering Code
1087//===----------------------------------------------------------------------===//
1088
Evan Chenga8e29892007-01-19 07:51:42 +00001089/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1090static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1091 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001092 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001093 case ISD::SETNE: return ARMCC::NE;
1094 case ISD::SETEQ: return ARMCC::EQ;
1095 case ISD::SETGT: return ARMCC::GT;
1096 case ISD::SETGE: return ARMCC::GE;
1097 case ISD::SETLT: return ARMCC::LT;
1098 case ISD::SETLE: return ARMCC::LE;
1099 case ISD::SETUGT: return ARMCC::HI;
1100 case ISD::SETUGE: return ARMCC::HS;
1101 case ISD::SETULT: return ARMCC::LO;
1102 case ISD::SETULE: return ARMCC::LS;
1103 }
1104}
1105
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001106/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1107static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001108 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001109 CondCode2 = ARMCC::AL;
1110 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001111 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001112 case ISD::SETEQ:
1113 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1114 case ISD::SETGT:
1115 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1116 case ISD::SETGE:
1117 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1118 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001119 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001120 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1121 case ISD::SETO: CondCode = ARMCC::VC; break;
1122 case ISD::SETUO: CondCode = ARMCC::VS; break;
1123 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1124 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1125 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1126 case ISD::SETLT:
1127 case ISD::SETULT: CondCode = ARMCC::LT; break;
1128 case ISD::SETLE:
1129 case ISD::SETULE: CondCode = ARMCC::LE; break;
1130 case ISD::SETNE:
1131 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1132 }
Evan Chenga8e29892007-01-19 07:51:42 +00001133}
1134
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135//===----------------------------------------------------------------------===//
1136// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001137//===----------------------------------------------------------------------===//
1138
1139#include "ARMGenCallingConv.inc"
1140
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001141/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1142/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001143CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001144 bool Return,
1145 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001146 switch (CC) {
1147 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001148 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001149 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001150 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001151 if (!Subtarget->isAAPCS_ABI())
1152 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1153 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1154 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1155 }
1156 // Fallthrough
1157 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001158 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001159 if (!Subtarget->isAAPCS_ABI())
1160 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1161 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001162 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1163 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001164 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1165 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1166 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001167 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001168 if (!isVarArg)
1169 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1170 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001171 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001172 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001173 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001174 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001175 case CallingConv::GHC:
1176 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001177 }
1178}
1179
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180/// LowerCallResult - Lower the result values of a call into the
1181/// appropriate copies out of appropriate physical registers.
1182SDValue
1183ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001184 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185 const SmallVectorImpl<ISD::InputArg> &Ins,
1186 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001187 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001188
Bob Wilson1f595bb2009-04-17 19:07:39 +00001189 // Assign locations to each value returned by this call.
1190 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001191 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1192 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001193 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001194 CCAssignFnForNode(CallConv, /* Return*/ true,
1195 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001196
1197 // Copy all of the result registers out of their specified physreg.
1198 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1199 CCValAssign VA = RVLocs[i];
1200
Bob Wilson80915242009-04-25 00:33:20 +00001201 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001203 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001204 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001206 Chain = Lo.getValue(1);
1207 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001208 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001209 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001210 InFlag);
1211 Chain = Hi.getValue(1);
1212 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001213 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001214
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 if (VA.getLocVT() == MVT::v2f64) {
1216 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1217 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1218 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001219
1220 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001221 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001222 Chain = Lo.getValue(1);
1223 InFlag = Lo.getValue(2);
1224 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001226 Chain = Hi.getValue(1);
1227 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001228 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1230 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001231 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001233 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1234 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001235 Chain = Val.getValue(1);
1236 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237 }
Bob Wilson80915242009-04-25 00:33:20 +00001238
1239 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001240 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001241 case CCValAssign::Full: break;
1242 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001243 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001244 break;
1245 }
1246
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248 }
1249
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251}
1252
Bob Wilsondee46d72009-04-17 20:35:10 +00001253/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001255ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1256 SDValue StackPtr, SDValue Arg,
1257 DebugLoc dl, SelectionDAG &DAG,
1258 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001259 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 unsigned LocMemOffset = VA.getLocMemOffset();
1261 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1262 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001263 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001264 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001265 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001266}
1267
Dan Gohman98ca4f22009-08-05 01:29:28 +00001268void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001269 SDValue Chain, SDValue &Arg,
1270 RegsToPassVector &RegsToPass,
1271 CCValAssign &VA, CCValAssign &NextVA,
1272 SDValue &StackPtr,
1273 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001274 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001275
Jim Grosbache5165492009-11-09 00:11:35 +00001276 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001277 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001278 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1279
1280 if (NextVA.isRegLoc())
1281 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1282 else {
1283 assert(NextVA.isMemLoc());
1284 if (StackPtr.getNode() == 0)
1285 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1286
Dan Gohman98ca4f22009-08-05 01:29:28 +00001287 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1288 dl, DAG, NextVA,
1289 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001290 }
1291}
1292
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001294/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1295/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001297ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001298 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001299 SelectionDAG &DAG = CLI.DAG;
1300 DebugLoc &dl = CLI.DL;
1301 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1302 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1303 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1304 SDValue Chain = CLI.Chain;
1305 SDValue Callee = CLI.Callee;
1306 bool &isTailCall = CLI.IsTailCall;
1307 CallingConv::ID CallConv = CLI.CallConv;
1308 bool doesNotRet = CLI.DoesNotReturn;
1309 bool isVarArg = CLI.IsVarArg;
1310
Dale Johannesen51e28e62010-06-03 21:09:53 +00001311 MachineFunction &MF = DAG.getMachineFunction();
1312 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1313 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001314 // Disable tail calls if they're not supported.
1315 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001316 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001317 if (isTailCall) {
1318 // Check if it's really possible to do a tail call.
1319 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1320 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001321 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001322 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1323 // detected sibcalls.
1324 if (isTailCall) {
1325 ++NumTailCalls;
1326 IsSibCall = true;
1327 }
1328 }
Evan Chenga8e29892007-01-19 07:51:42 +00001329
Bob Wilson1f595bb2009-04-17 19:07:39 +00001330 // Analyze operands of the call, assigning locations to each operand.
1331 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001332 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1333 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001335 CCAssignFnForNode(CallConv, /* Return*/ false,
1336 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001337
Bob Wilson1f595bb2009-04-17 19:07:39 +00001338 // Get a count of how many bytes are to be pushed on the stack.
1339 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001340
Dale Johannesen51e28e62010-06-03 21:09:53 +00001341 // For tail calls, memory operands are available in our caller's stack.
1342 if (IsSibCall)
1343 NumBytes = 0;
1344
Evan Chenga8e29892007-01-19 07:51:42 +00001345 // Adjust the stack pointer for the new arguments...
1346 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001347 if (!IsSibCall)
1348 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001349
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001350 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001351
Bob Wilson5bafff32009-06-22 23:27:02 +00001352 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001354
Bob Wilson1f595bb2009-04-17 19:07:39 +00001355 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001356 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001357 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1358 i != e;
1359 ++i, ++realArgIdx) {
1360 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001361 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001363 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001364
Bob Wilson1f595bb2009-04-17 19:07:39 +00001365 // Promote the value if needed.
1366 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001367 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001368 case CCValAssign::Full: break;
1369 case CCValAssign::SExt:
1370 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1371 break;
1372 case CCValAssign::ZExt:
1373 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1374 break;
1375 case CCValAssign::AExt:
1376 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1377 break;
1378 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001379 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001380 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001381 }
1382
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001383 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001384 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001385 if (VA.getLocVT() == MVT::v2f64) {
1386 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1387 DAG.getConstant(0, MVT::i32));
1388 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1389 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001390
Dan Gohman98ca4f22009-08-05 01:29:28 +00001391 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001392 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1393
1394 VA = ArgLocs[++i]; // skip ahead to next loc
1395 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001396 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001397 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1398 } else {
1399 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001400
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1402 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001403 }
1404 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001405 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001406 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001407 }
1408 } else if (VA.isRegLoc()) {
1409 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001410 } else if (isByVal) {
1411 assert(VA.isMemLoc());
1412 unsigned offset = 0;
1413
1414 // True if this byval aggregate will be split between registers
1415 // and memory.
1416 if (CCInfo.isFirstByValRegValid()) {
1417 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1418 unsigned int i, j;
1419 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1420 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1421 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1422 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1423 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001424 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001425 MemOpChains.push_back(Load.getValue(1));
1426 RegsToPass.push_back(std::make_pair(j, Load));
1427 }
1428 offset = ARM::R4 - CCInfo.getFirstByValReg();
1429 CCInfo.clearFirstByValReg();
1430 }
1431
Manman Ren763a75d2012-06-01 02:44:42 +00001432 if (Flags.getByValSize() - 4*offset > 0) {
1433 unsigned LocMemOffset = VA.getLocMemOffset();
1434 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1435 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1436 StkPtrOff);
1437 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1438 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1439 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1440 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001441 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001442
Manman Ren763a75d2012-06-01 02:44:42 +00001443 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001444 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001445 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1446 Ops, array_lengthof(Ops)));
1447 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001448 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001449 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001450
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1452 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001453 }
Evan Chenga8e29892007-01-19 07:51:42 +00001454 }
1455
1456 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001457 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001458 &MemOpChains[0], MemOpChains.size());
1459
1460 // Build a sequence of copy-to-reg nodes chained together with token chain
1461 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001463 // Tail call byval lowering might overwrite argument registers so in case of
1464 // tail call optimization the copies to registers are lowered later.
1465 if (!isTailCall)
1466 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1467 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1468 RegsToPass[i].second, InFlag);
1469 InFlag = Chain.getValue(1);
1470 }
Evan Chenga8e29892007-01-19 07:51:42 +00001471
Dale Johannesen51e28e62010-06-03 21:09:53 +00001472 // For tail calls lower the arguments to the 'real' stack slot.
1473 if (isTailCall) {
1474 // Force all the incoming stack arguments to be loaded from the stack
1475 // before any new outgoing arguments are stored to the stack, because the
1476 // outgoing stack slots may alias the incoming argument stack slots, and
1477 // the alias isn't otherwise explicit. This is slightly more conservative
1478 // than necessary, because it means that each store effectively depends
1479 // on every argument instead of just those arguments it would clobber.
1480
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001481 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001482 InFlag = SDValue();
1483 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1484 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1485 RegsToPass[i].second, InFlag);
1486 InFlag = Chain.getValue(1);
1487 }
1488 InFlag =SDValue();
1489 }
1490
Bill Wendling056292f2008-09-16 21:48:12 +00001491 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1492 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1493 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001494 bool isDirect = false;
1495 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001496 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001497 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001498
1499 if (EnableARMLongCalls) {
1500 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1501 && "long-calls with non-static relocation model!");
1502 // Handle a global address or an external symbol. If it's not one of
1503 // those, the target's already in a register, so we don't need to do
1504 // anything extra.
1505 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001506 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001507 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001508 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001509 ARMConstantPoolValue *CPV =
1510 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1511
Jim Grosbache7b52522010-04-14 22:28:31 +00001512 // Get the address of the callee into a register
1513 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1514 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1515 Callee = DAG.getLoad(getPointerTy(), dl,
1516 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001517 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001518 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001519 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1520 const char *Sym = S->getSymbol();
1521
1522 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001523 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001524 ARMConstantPoolValue *CPV =
1525 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1526 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001527 // Get the address of the callee into a register
1528 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1529 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1530 Callee = DAG.getLoad(getPointerTy(), dl,
1531 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001532 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001533 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001534 }
1535 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001536 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001537 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001538 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001539 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001540 getTargetMachine().getRelocationModel() != Reloc::Static;
1541 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001542 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001543 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001544 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001545 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001546 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001547 ARMConstantPoolValue *CPV =
1548 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001549 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001551 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001552 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001553 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001554 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001555 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001556 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001557 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001558 } else {
1559 // On ELF targets for PIC code, direct calls should go through the PLT
1560 unsigned OpFlags = 0;
1561 if (Subtarget->isTargetELF() &&
1562 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1563 OpFlags = ARMII::MO_PLT;
1564 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1565 }
Bill Wendling056292f2008-09-16 21:48:12 +00001566 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001567 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001568 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001569 getTargetMachine().getRelocationModel() != Reloc::Static;
1570 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001571 // tBX takes a register source operand.
1572 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001573 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001574 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001575 ARMConstantPoolValue *CPV =
1576 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1577 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001578 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001580 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001581 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001582 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001583 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001584 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001585 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001586 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001587 } else {
1588 unsigned OpFlags = 0;
1589 // On ELF targets for PIC code, direct calls should go through the PLT
1590 if (Subtarget->isTargetELF() &&
1591 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1592 OpFlags = ARMII::MO_PLT;
1593 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1594 }
Evan Chenga8e29892007-01-19 07:51:42 +00001595 }
1596
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001597 // FIXME: handle tail calls differently.
1598 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001599 if (Subtarget->isThumb()) {
1600 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001601 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001602 else if (doesNotRet && isDirect && !isARMFunc &&
1603 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1604 // "mov lr, pc; b _foo" to avoid confusing the RSP
1605 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001606 else
1607 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1608 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001609 if (!isDirect && !Subtarget->hasV5TOps()) {
1610 CallOpc = ARMISD::CALL_NOLINK;
1611 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1612 // "mov lr, pc; b _foo" to avoid confusing the RSP
1613 CallOpc = ARMISD::CALL_NOLINK;
1614 else
1615 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001616 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001617
Dan Gohman475871a2008-07-27 21:46:04 +00001618 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001619 Ops.push_back(Chain);
1620 Ops.push_back(Callee);
1621
1622 // Add argument registers to the end of the list so that they are known live
1623 // into the call.
1624 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1625 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1626 RegsToPass[i].second.getValueType()));
1627
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001628 // Add a register mask operand representing the call-preserved registers.
1629 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1630 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1631 assert(Mask && "Missing call preserved mask for calling convention");
1632 Ops.push_back(DAG.getRegisterMask(Mask));
1633
Gabor Greifba36cb52008-08-28 21:40:38 +00001634 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001635 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001637 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001638 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001639 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001640
Duncan Sands4bdcb612008-07-02 17:40:58 +00001641 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001642 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001643 InFlag = Chain.getValue(1);
1644
Chris Lattnere563bbc2008-10-11 22:08:30 +00001645 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1646 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001648 InFlag = Chain.getValue(1);
1649
Bob Wilson1f595bb2009-04-17 19:07:39 +00001650 // Handle result values, copying them out of physregs into vregs that we
1651 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1653 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001654}
1655
Stuart Hastingsf222e592011-02-28 17:17:53 +00001656/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001657/// on the stack. Remember the next parameter register to allocate,
1658/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001659/// this.
1660void
Craig Topperc89c7442012-03-27 07:21:54 +00001661ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001662 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1663 assert((State->getCallOrPrologue() == Prologue ||
1664 State->getCallOrPrologue() == Call) &&
1665 "unhandled ParmContext");
1666 if ((!State->isFirstByValRegValid()) &&
1667 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1668 State->setFirstByValReg(reg);
1669 // At a call site, a byval parameter that is split between
1670 // registers and memory needs its size truncated here. In a
1671 // function prologue, such byval parameters are reassembled in
1672 // memory, and are not truncated.
1673 if (State->getCallOrPrologue() == Call) {
1674 unsigned excess = 4 * (ARM::R4 - reg);
1675 assert(size >= excess && "expected larger existing stack allocation");
1676 size -= excess;
1677 }
1678 }
1679 // Confiscate any remaining parameter registers to preclude their
1680 // assignment to subsequent parameters.
1681 while (State->AllocateReg(GPRArgRegs, 4))
1682 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001683}
1684
Dale Johannesen51e28e62010-06-03 21:09:53 +00001685/// MatchingStackOffset - Return true if the given stack call argument is
1686/// already available in the same position (relatively) of the caller's
1687/// incoming argument stack.
1688static
1689bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1690 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001691 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1693 int FI = INT_MAX;
1694 if (Arg.getOpcode() == ISD::CopyFromReg) {
1695 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001696 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001697 return false;
1698 MachineInstr *Def = MRI->getVRegDef(VR);
1699 if (!Def)
1700 return false;
1701 if (!Flags.isByVal()) {
1702 if (!TII->isLoadFromStackSlot(Def, FI))
1703 return false;
1704 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001705 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001706 }
1707 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1708 if (Flags.isByVal())
1709 // ByVal argument is passed in as a pointer but it's now being
1710 // dereferenced. e.g.
1711 // define @foo(%struct.X* %A) {
1712 // tail call @bar(%struct.X* byval %A)
1713 // }
1714 return false;
1715 SDValue Ptr = Ld->getBasePtr();
1716 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1717 if (!FINode)
1718 return false;
1719 FI = FINode->getIndex();
1720 } else
1721 return false;
1722
1723 assert(FI != INT_MAX);
1724 if (!MFI->isFixedObjectIndex(FI))
1725 return false;
1726 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1727}
1728
1729/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1730/// for tail call optimization. Targets which want to do tail call
1731/// optimization should implement this function.
1732bool
1733ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1734 CallingConv::ID CalleeCC,
1735 bool isVarArg,
1736 bool isCalleeStructRet,
1737 bool isCallerStructRet,
1738 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001739 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001740 const SmallVectorImpl<ISD::InputArg> &Ins,
1741 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001742 const Function *CallerF = DAG.getMachineFunction().getFunction();
1743 CallingConv::ID CallerCC = CallerF->getCallingConv();
1744 bool CCMatch = CallerCC == CalleeCC;
1745
1746 // Look for obvious safe cases to perform tail call optimization that do not
1747 // require ABI changes. This is what gcc calls sibcall.
1748
Jim Grosbach7616b642010-06-16 23:45:49 +00001749 // Do not sibcall optimize vararg calls unless the call site is not passing
1750 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001751 if (isVarArg && !Outs.empty())
1752 return false;
1753
1754 // Also avoid sibcall optimization if either caller or callee uses struct
1755 // return semantics.
1756 if (isCalleeStructRet || isCallerStructRet)
1757 return false;
1758
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001759 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001760 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1761 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1762 // support in the assembler and linker to be used. This would need to be
1763 // fixed to fully support tail calls in Thumb1.
1764 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001765 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1766 // LR. This means if we need to reload LR, it takes an extra instructions,
1767 // which outweighs the value of the tail call; but here we don't know yet
1768 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001769 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001770 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001771
1772 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1773 // but we need to make sure there are enough registers; the only valid
1774 // registers are the 4 used for parameters. We don't currently do this
1775 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001776 if (Subtarget->isThumb1Only())
1777 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001778
Dale Johannesen51e28e62010-06-03 21:09:53 +00001779 // If the calling conventions do not match, then we'd better make sure the
1780 // results are returned in the same way as what the caller expects.
1781 if (!CCMatch) {
1782 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001783 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1784 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001785 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1786
1787 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001788 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1789 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001790 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1791
1792 if (RVLocs1.size() != RVLocs2.size())
1793 return false;
1794 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1795 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1796 return false;
1797 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1798 return false;
1799 if (RVLocs1[i].isRegLoc()) {
1800 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1801 return false;
1802 } else {
1803 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1804 return false;
1805 }
1806 }
1807 }
1808
1809 // If the callee takes no arguments then go on to check the results of the
1810 // call.
1811 if (!Outs.empty()) {
1812 // Check if stack adjustment is needed. For now, do not do this if any
1813 // argument is passed on the stack.
1814 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001815 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1816 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001817 CCInfo.AnalyzeCallOperands(Outs,
1818 CCAssignFnForNode(CalleeCC, false, isVarArg));
1819 if (CCInfo.getNextStackOffset()) {
1820 MachineFunction &MF = DAG.getMachineFunction();
1821
1822 // Check if the arguments are already laid out in the right way as
1823 // the caller's fixed stack objects.
1824 MachineFrameInfo *MFI = MF.getFrameInfo();
1825 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001826 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001827 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1828 i != e;
1829 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001830 CCValAssign &VA = ArgLocs[i];
1831 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001832 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001833 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001834 if (VA.getLocInfo() == CCValAssign::Indirect)
1835 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001836 if (VA.needsCustom()) {
1837 // f64 and vector types are split into multiple registers or
1838 // register/stack-slot combinations. The types will not match
1839 // the registers; give up on memory f64 refs until we figure
1840 // out what to do about this.
1841 if (!VA.isRegLoc())
1842 return false;
1843 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001844 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001845 if (RegVT == MVT::v2f64) {
1846 if (!ArgLocs[++i].isRegLoc())
1847 return false;
1848 if (!ArgLocs[++i].isRegLoc())
1849 return false;
1850 }
1851 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001852 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1853 MFI, MRI, TII))
1854 return false;
1855 }
1856 }
1857 }
1858 }
1859
1860 return true;
1861}
1862
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863SDValue
1864ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001865 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001867 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001868 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001869
Bob Wilsondee46d72009-04-17 20:35:10 +00001870 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001871 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872
Bob Wilsondee46d72009-04-17 20:35:10 +00001873 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001874 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1875 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001876
Dan Gohman98ca4f22009-08-05 01:29:28 +00001877 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001878 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1879 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001880
1881 // If this is the first return lowered for this function, add
1882 // the regs to the liveout set for the function.
1883 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1884 for (unsigned i = 0; i != RVLocs.size(); ++i)
1885 if (RVLocs[i].isRegLoc())
1886 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001887 }
1888
Bob Wilson1f595bb2009-04-17 19:07:39 +00001889 SDValue Flag;
1890
1891 // Copy the result values into the output registers.
1892 for (unsigned i = 0, realRVLocIdx = 0;
1893 i != RVLocs.size();
1894 ++i, ++realRVLocIdx) {
1895 CCValAssign &VA = RVLocs[i];
1896 assert(VA.isRegLoc() && "Can only return in registers!");
1897
Dan Gohmanc9403652010-07-07 15:54:55 +00001898 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001899
1900 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001901 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001902 case CCValAssign::Full: break;
1903 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001904 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001905 break;
1906 }
1907
Bob Wilson1f595bb2009-04-17 19:07:39 +00001908 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001910 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1912 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001913 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001914 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001915
1916 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1917 Flag = Chain.getValue(1);
1918 VA = RVLocs[++i]; // skip ahead to next loc
1919 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1920 HalfGPRs.getValue(1), Flag);
1921 Flag = Chain.getValue(1);
1922 VA = RVLocs[++i]; // skip ahead to next loc
1923
1924 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001925 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1926 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001927 }
1928 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1929 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001930 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001932 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001933 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001934 VA = RVLocs[++i]; // skip ahead to next loc
1935 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1936 Flag);
1937 } else
1938 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1939
Bob Wilsondee46d72009-04-17 20:35:10 +00001940 // Guarantee that all emitted copies are
1941 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001942 Flag = Chain.getValue(1);
1943 }
1944
1945 SDValue result;
1946 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001948 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001950
1951 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001952}
1953
Evan Chengbf010eb2012-04-10 01:51:00 +00001954bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001955 if (N->getNumValues() != 1)
1956 return false;
1957 if (!N->hasNUsesOfValue(1, 0))
1958 return false;
1959
Evan Chengbf010eb2012-04-10 01:51:00 +00001960 SDValue TCChain = Chain;
1961 SDNode *Copy = *N->use_begin();
1962 if (Copy->getOpcode() == ISD::CopyToReg) {
1963 // If the copy has a glue operand, we conservatively assume it isn't safe to
1964 // perform a tail call.
1965 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1966 return false;
1967 TCChain = Copy->getOperand(0);
1968 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1969 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001970 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001971 SmallPtrSet<SDNode*, 2> Copies;
1972 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001973 UI != UE; ++UI) {
1974 if (UI->getOpcode() != ISD::CopyToReg)
1975 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001976 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001978 if (Copies.size() > 2)
1979 return false;
1980
1981 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1982 UI != UE; ++UI) {
1983 SDValue UseChain = UI->getOperand(0);
1984 if (Copies.count(UseChain.getNode()))
1985 // Second CopyToReg
1986 Copy = *UI;
1987 else
1988 // First CopyToReg
1989 TCChain = UseChain;
1990 }
1991 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001993 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001995 Copy = *Copy->use_begin();
1996 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001997 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001998 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001999 } else {
2000 return false;
2001 }
2002
Evan Cheng1bf891a2010-12-01 22:59:46 +00002003 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002004 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2005 UI != UE; ++UI) {
2006 if (UI->getOpcode() != ARMISD::RET_FLAG)
2007 return false;
2008 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002009 }
2010
Evan Chengbf010eb2012-04-10 01:51:00 +00002011 if (!HasRet)
2012 return false;
2013
2014 Chain = TCChain;
2015 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002016}
2017
Evan Cheng485fafc2011-03-21 01:19:09 +00002018bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002019 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002020 return false;
2021
2022 if (!CI->isTailCall())
2023 return false;
2024
2025 return !Subtarget->isThumb1Only();
2026}
2027
Bob Wilsonb62d2572009-11-03 00:02:05 +00002028// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2029// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2030// one of the above mentioned nodes. It has to be wrapped because otherwise
2031// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2032// be used to form addressing mode. These wrapped nodes will be selected
2033// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002034static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002035 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002036 // FIXME there is no actual debug info here
2037 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002038 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002039 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002040 if (CP->isMachineConstantPoolEntry())
2041 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2042 CP->getAlignment());
2043 else
2044 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2045 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002046 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002047}
2048
Jim Grosbache1102ca2010-07-19 17:20:38 +00002049unsigned ARMTargetLowering::getJumpTableEncoding() const {
2050 return MachineJumpTableInfo::EK_Inline;
2051}
2052
Dan Gohmand858e902010-04-17 15:26:15 +00002053SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2054 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002055 MachineFunction &MF = DAG.getMachineFunction();
2056 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2057 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002058 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002059 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002060 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002061 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2062 SDValue CPAddr;
2063 if (RelocM == Reloc::Static) {
2064 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2065 } else {
2066 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002067 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002068 ARMConstantPoolValue *CPV =
2069 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2070 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002071 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2072 }
2073 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2074 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002075 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002076 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002077 if (RelocM == Reloc::Static)
2078 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002079 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002080 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002081}
2082
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002083// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002084SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002086 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002087 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002088 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002089 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002090 MachineFunction &MF = DAG.getMachineFunction();
2091 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002092 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002093 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002094 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2095 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002096 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002098 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002099 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002100 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002101 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002102
Evan Chenge7e0d622009-11-06 22:24:13 +00002103 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002104 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002105
2106 // call __tls_get_addr.
2107 ArgListTy Args;
2108 ArgListEntry Entry;
2109 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002110 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002111 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002112 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002113 TargetLowering::CallLoweringInfo CLI(Chain,
2114 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002115 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002116 0, CallingConv::C, /*isTailCall=*/false,
2117 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002118 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002119 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002120 return CallResult.first;
2121}
2122
2123// Lower ISD::GlobalTLSAddress using the "initial exec" or
2124// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002125SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002126ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002127 SelectionDAG &DAG,
2128 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002129 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002130 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002131 SDValue Offset;
2132 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002134 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002135 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002136
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002137 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 MachineFunction &MF = DAG.getMachineFunction();
2139 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002140 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002141 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002142 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2143 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002144 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2145 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2146 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002147 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002149 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002150 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002151 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002152 Chain = Offset.getValue(1);
2153
Evan Chenge7e0d622009-11-06 22:24:13 +00002154 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002155 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002156
Evan Cheng9eda6892009-10-31 03:39:36 +00002157 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002158 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002159 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002160 } else {
2161 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002162 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002163 ARMConstantPoolValue *CPV =
2164 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002165 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002166 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002167 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002168 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002169 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002170 }
2171
2172 // The address of the thread local variable is the add of the thread
2173 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002174 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002175}
2176
Dan Gohman475871a2008-07-27 21:46:04 +00002177SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002178ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002179 // TODO: implement the "local dynamic" model
2180 assert(Subtarget->isTargetELF() &&
2181 "TLS not implemented for non-ELF targets");
2182 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002183
2184 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2185
2186 switch (model) {
2187 case TLSModel::GeneralDynamic:
2188 case TLSModel::LocalDynamic:
2189 return LowerToTLSGeneralDynamicModel(GA, DAG);
2190 case TLSModel::InitialExec:
2191 case TLSModel::LocalExec:
2192 return LowerToTLSExecModels(GA, DAG, model);
2193 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002194 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002195}
2196
Dan Gohman475871a2008-07-27 21:46:04 +00002197SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002198 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002200 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002201 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002202 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2203 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002204 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002205 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002206 ARMConstantPoolConstant::Create(GV,
2207 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002208 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002209 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002210 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002211 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002212 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002213 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002214 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002215 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002216 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002217 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002218 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002219 MachinePointerInfo::getGOT(),
2220 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002221 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002222 }
2223
2224 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002225 // pair. This is always cheaper.
2226 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002227 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002228 // FIXME: Once remat is capable of dealing with instructions with register
2229 // operands, expand this into two nodes.
2230 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2231 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002232 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002233 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2234 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2235 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2236 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002237 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002238 }
2239}
2240
Dan Gohman475871a2008-07-27 21:46:04 +00002241SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002242 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002243 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002244 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002245 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002246 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002247 MachineFunction &MF = DAG.getMachineFunction();
2248 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2249
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002250 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2251 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002252 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002253 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002254 // FIXME: Once remat is capable of dealing with instructions with register
2255 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002256 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002257 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2258 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2259
Evan Cheng53519f02011-01-21 18:55:51 +00002260 unsigned Wrapper = (RelocM == Reloc::PIC_)
2261 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2262 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002263 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002264 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2265 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002266 MachinePointerInfo::getGOT(),
2267 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002268 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002269 }
2270
2271 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002272 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002273 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002274 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002275 } else {
2276 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002277 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2278 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002279 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2280 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002281 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002282 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002283 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002284
Evan Cheng9eda6892009-10-31 03:39:36 +00002285 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002286 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002287 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002288 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002289
2290 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002291 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002292 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002293 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002294
Evan Cheng63476a82009-09-03 07:04:02 +00002295 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002296 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002297 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002298
2299 return Result;
2300}
2301
Dan Gohman475871a2008-07-27 21:46:04 +00002302SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002303 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002304 assert(Subtarget->isTargetELF() &&
2305 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002306 MachineFunction &MF = DAG.getMachineFunction();
2307 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002308 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002309 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002310 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002311 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002312 ARMConstantPoolValue *CPV =
2313 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2314 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002315 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002317 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002318 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002319 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002320 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002321 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002322}
2323
Jim Grosbach0e0da732009-05-12 23:59:14 +00002324SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002325ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2326 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002327 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002328 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2329 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002330 Op.getOperand(1), Val);
2331}
2332
2333SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002334ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2335 DebugLoc dl = Op.getDebugLoc();
2336 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2337 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2338}
2339
2340SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002341ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002342 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002343 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002344 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002345 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002346 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002347 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002348 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002349 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2350 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002351 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002352 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002353 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002354 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002355 EVT PtrVT = getPointerTy();
2356 DebugLoc dl = Op.getDebugLoc();
2357 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2358 SDValue CPAddr;
2359 unsigned PCAdj = (RelocM != Reloc::PIC_)
2360 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002361 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002362 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2363 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002364 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002365 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002366 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002367 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002368 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002369 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002370
2371 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002372 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002373 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2374 }
2375 return Result;
2376 }
Evan Cheng92e39162011-03-29 23:06:19 +00002377 case Intrinsic::arm_neon_vmulls:
2378 case Intrinsic::arm_neon_vmullu: {
2379 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2380 ? ARMISD::VMULLs : ARMISD::VMULLu;
2381 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2382 Op.getOperand(1), Op.getOperand(2));
2383 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002384 }
2385}
2386
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002387static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002388 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002389 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002390 if (!Subtarget->hasDataBarrier()) {
2391 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2392 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2393 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002394 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002395 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002396 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002397 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002398 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002399
2400 SDValue Op5 = Op.getOperand(5);
2401 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2402 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2403 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2404 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2405
2406 ARM_MB::MemBOpt DMBOpt;
2407 if (isDeviceBarrier)
2408 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2409 else
2410 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2411 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2412 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002413}
2414
Eli Friedman26689ac2011-08-03 21:06:02 +00002415
2416static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2417 const ARMSubtarget *Subtarget) {
2418 // FIXME: handle "fence singlethread" more efficiently.
2419 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002420 if (!Subtarget->hasDataBarrier()) {
2421 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2422 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2423 // here.
2424 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2425 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002426 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002427 DAG.getConstant(0, MVT::i32));
2428 }
2429
Eli Friedman26689ac2011-08-03 21:06:02 +00002430 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002431 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002432}
2433
Evan Chengdfed19f2010-11-03 06:34:55 +00002434static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2435 const ARMSubtarget *Subtarget) {
2436 // ARM pre v5TE and Thumb1 does not have preload instructions.
2437 if (!(Subtarget->isThumb2() ||
2438 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2439 // Just preserve the chain.
2440 return Op.getOperand(0);
2441
2442 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002443 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2444 if (!isRead &&
2445 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2446 // ARMv7 with MP extension has PLDW.
2447 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002448
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002449 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2450 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002451 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002452 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002453 isData = ~isData & 1;
2454 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002455
2456 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002457 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2458 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002459}
2460
Dan Gohman1e93df62010-04-17 14:41:14 +00002461static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2462 MachineFunction &MF = DAG.getMachineFunction();
2463 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2464
Evan Chenga8e29892007-01-19 07:51:42 +00002465 // vastart just stores the address of the VarArgsFrameIndex slot into the
2466 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002467 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002468 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002469 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002470 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002471 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2472 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002473}
2474
Dan Gohman475871a2008-07-27 21:46:04 +00002475SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002476ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2477 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002478 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002479 MachineFunction &MF = DAG.getMachineFunction();
2480 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2481
Craig Topper44d23822012-02-22 05:59:10 +00002482 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002483 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002484 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 else
Craig Topper420761a2012-04-20 07:30:17 +00002486 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002487
2488 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002489 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002490 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002491
2492 SDValue ArgValue2;
2493 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002494 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002495 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002496
2497 // Create load node to retrieve arguments from the stack.
2498 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002499 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002500 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002501 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002502 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002503 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002504 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002505 }
2506
Jim Grosbache5165492009-11-09 00:11:35 +00002507 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002508}
2509
Stuart Hastingsc7315872011-04-20 16:47:52 +00002510void
2511ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2512 unsigned &VARegSize, unsigned &VARegSaveSize)
2513 const {
2514 unsigned NumGPRs;
2515 if (CCInfo.isFirstByValRegValid())
2516 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2517 else {
2518 unsigned int firstUnalloced;
2519 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2520 sizeof(GPRArgRegs) /
2521 sizeof(GPRArgRegs[0]));
2522 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2523 }
2524
2525 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2526 VARegSize = NumGPRs * 4;
2527 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2528}
2529
2530// The remaining GPRs hold either the beginning of variable-argument
2531// data, or the beginning of an aggregate passed by value (usuall
2532// byval). Either way, we allocate stack slots adjacent to the data
2533// provided by our caller, and store the unallocated registers there.
2534// If this is a variadic function, the va_list pointer will begin with
2535// these values; otherwise, this reassembles a (byval) structure that
2536// was split between registers and memory.
2537void
2538ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2539 DebugLoc dl, SDValue &Chain,
2540 unsigned ArgOffset) const {
2541 MachineFunction &MF = DAG.getMachineFunction();
2542 MachineFrameInfo *MFI = MF.getFrameInfo();
2543 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2544 unsigned firstRegToSaveIndex;
2545 if (CCInfo.isFirstByValRegValid())
2546 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2547 else {
2548 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2549 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2550 }
2551
2552 unsigned VARegSize, VARegSaveSize;
2553 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2554 if (VARegSaveSize) {
2555 // If this function is vararg, store any remaining integer argument regs
2556 // to their spots on the stack so that they may be loaded by deferencing
2557 // the result of va_next.
2558 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002559 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2560 ArgOffset + VARegSaveSize
2561 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002562 false));
2563 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2564 getPointerTy());
2565
2566 SmallVector<SDValue, 4> MemOps;
2567 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002568 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002569 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002570 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002571 else
Craig Topper420761a2012-04-20 07:30:17 +00002572 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002573
2574 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2575 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2576 SDValue Store =
2577 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002578 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002579 false, false, 0);
2580 MemOps.push_back(Store);
2581 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2582 DAG.getConstant(4, getPointerTy()));
2583 }
2584 if (!MemOps.empty())
2585 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2586 &MemOps[0], MemOps.size());
2587 } else
2588 // This will point to the next argument passed via stack.
2589 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2590}
2591
Bob Wilson5bafff32009-06-22 23:27:02 +00002592SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002593ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002594 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002595 const SmallVectorImpl<ISD::InputArg>
2596 &Ins,
2597 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002598 SmallVectorImpl<SDValue> &InVals)
2599 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002600 MachineFunction &MF = DAG.getMachineFunction();
2601 MachineFrameInfo *MFI = MF.getFrameInfo();
2602
Bob Wilson1f595bb2009-04-17 19:07:39 +00002603 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2604
2605 // Assign locations to all of the incoming arguments.
2606 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002607 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2608 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002609 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002610 CCAssignFnForNode(CallConv, /* Return*/ false,
2611 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002612
2613 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002614 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002615
Stuart Hastingsf222e592011-02-28 17:17:53 +00002616 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002617 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2618 CCValAssign &VA = ArgLocs[i];
2619
Bob Wilsondee46d72009-04-17 20:35:10 +00002620 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002621 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002622 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002623
Bob Wilson1f595bb2009-04-17 19:07:39 +00002624 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002625 // f64 and vector types are split up into multiple registers or
2626 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002627 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002628 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002629 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002630 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002631 SDValue ArgValue2;
2632 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002633 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002634 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2635 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002636 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002637 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002638 } else {
2639 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2640 Chain, DAG, dl);
2641 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2643 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002644 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002645 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002646 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2647 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002648 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002649
Bob Wilson5bafff32009-06-22 23:27:02 +00002650 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002651 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002652
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002654 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002656 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002658 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002659 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002660 RC = AFI->isThumb1OnlyFunction() ?
2661 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2662 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002663 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002664 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002665
2666 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002667 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002668 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002669 }
2670
2671 // If this is an 8 or 16-bit value, it is really passed promoted
2672 // to 32 bits. Insert an assert[sz]ext to capture this, then
2673 // truncate to the right size.
2674 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002675 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002676 case CCValAssign::Full: break;
2677 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002678 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002679 break;
2680 case CCValAssign::SExt:
2681 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2682 DAG.getValueType(VA.getValVT()));
2683 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2684 break;
2685 case CCValAssign::ZExt:
2686 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2687 DAG.getValueType(VA.getValVT()));
2688 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2689 break;
2690 }
2691
Dan Gohman98ca4f22009-08-05 01:29:28 +00002692 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002693
2694 } else { // VA.isRegLoc()
2695
2696 // sanity check
2697 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002698 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002699
Stuart Hastingsf222e592011-02-28 17:17:53 +00002700 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002701
Stuart Hastingsf222e592011-02-28 17:17:53 +00002702 // Some Ins[] entries become multiple ArgLoc[] entries.
2703 // Process them only once.
2704 if (index != lastInsIndex)
2705 {
2706 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002707 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002708 // This can be changed with more analysis.
2709 // In case of tail call optimization mark all arguments mutable.
2710 // Since they could be overwritten by lowering of arguments in case of
2711 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002712 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002713 unsigned VARegSize, VARegSaveSize;
2714 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2715 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2716 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002717 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002718 int FI = MFI->CreateFixedObject(Bytes,
2719 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002720 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2721 } else {
2722 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2723 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002724
Stuart Hastingsf222e592011-02-28 17:17:53 +00002725 // Create load nodes to retrieve arguments from the stack.
2726 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2727 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2728 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002729 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002730 }
2731 lastInsIndex = index;
2732 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002733 }
2734 }
2735
2736 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002737 if (isVarArg)
2738 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002739
Dan Gohman98ca4f22009-08-05 01:29:28 +00002740 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002741}
2742
2743/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002744static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002745 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002746 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002747 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002748 // Maybe this has already been legalized into the constant pool?
2749 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002750 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002751 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002752 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002753 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002754 }
2755 }
2756 return false;
2757}
2758
Evan Chenga8e29892007-01-19 07:51:42 +00002759/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2760/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002761SDValue
2762ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002763 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002764 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002765 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002766 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002767 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002768 // Constant does not fit, try adjusting it by one?
2769 switch (CC) {
2770 default: break;
2771 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002772 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002773 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002774 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002775 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002776 }
2777 break;
2778 case ISD::SETULT:
2779 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002780 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002781 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002783 }
2784 break;
2785 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002786 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002787 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002788 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002789 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002790 }
2791 break;
2792 case ISD::SETULE:
2793 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002794 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002795 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002796 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002797 }
2798 break;
2799 }
2800 }
2801 }
2802
2803 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002804 ARMISD::NodeType CompareType;
2805 switch (CondCode) {
2806 default:
2807 CompareType = ARMISD::CMP;
2808 break;
2809 case ARMCC::EQ:
2810 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002811 // Uses only Z Flag
2812 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002813 break;
2814 }
Evan Cheng218977b2010-07-13 19:27:42 +00002815 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002816 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002817}
2818
2819/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002820SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002821ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002822 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002823 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002824 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002825 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002826 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002827 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2828 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002829}
2830
Bob Wilson79f56c92011-03-08 01:17:20 +00002831/// duplicateCmp - Glue values can have only one use, so this function
2832/// duplicates a comparison node.
2833SDValue
2834ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2835 unsigned Opc = Cmp.getOpcode();
2836 DebugLoc DL = Cmp.getDebugLoc();
2837 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2838 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2839
2840 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2841 Cmp = Cmp.getOperand(0);
2842 Opc = Cmp.getOpcode();
2843 if (Opc == ARMISD::CMPFP)
2844 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2845 else {
2846 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2847 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2848 }
2849 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2850}
2851
Bill Wendlingde2b1512010-08-11 08:43:16 +00002852SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2853 SDValue Cond = Op.getOperand(0);
2854 SDValue SelectTrue = Op.getOperand(1);
2855 SDValue SelectFalse = Op.getOperand(2);
2856 DebugLoc dl = Op.getDebugLoc();
2857
2858 // Convert:
2859 //
2860 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2861 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2862 //
2863 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2864 const ConstantSDNode *CMOVTrue =
2865 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2866 const ConstantSDNode *CMOVFalse =
2867 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2868
2869 if (CMOVTrue && CMOVFalse) {
2870 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2871 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2872
2873 SDValue True;
2874 SDValue False;
2875 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2876 True = SelectTrue;
2877 False = SelectFalse;
2878 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2879 True = SelectFalse;
2880 False = SelectTrue;
2881 }
2882
2883 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002884 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002885 SDValue ARMcc = Cond.getOperand(2);
2886 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002887 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002888 assert(True.getValueType() == VT);
2889 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002890 }
2891 }
2892 }
2893
Dan Gohmandb953892012-02-24 00:09:36 +00002894 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2895 // undefined bits before doing a full-word comparison with zero.
2896 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2897 DAG.getConstant(1, Cond.getValueType()));
2898
Bill Wendlingde2b1512010-08-11 08:43:16 +00002899 return DAG.getSelectCC(dl, Cond,
2900 DAG.getConstant(0, Cond.getValueType()),
2901 SelectTrue, SelectFalse, ISD::SETNE);
2902}
2903
Dan Gohmand858e902010-04-17 15:26:15 +00002904SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002905 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue LHS = Op.getOperand(0);
2907 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002908 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002909 SDValue TrueVal = Op.getOperand(2);
2910 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002911 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002912
Owen Anderson825b72b2009-08-11 20:47:22 +00002913 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002914 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002915 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002916 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002917 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002918 }
2919
2920 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002921 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002922
Evan Cheng218977b2010-07-13 19:27:42 +00002923 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2924 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002925 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002926 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002927 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002928 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002929 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002930 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002931 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002932 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002933 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002934 }
2935 return Result;
2936}
2937
Evan Cheng218977b2010-07-13 19:27:42 +00002938/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2939/// to morph to an integer compare sequence.
2940static bool canChangeToInt(SDValue Op, bool &SeenZero,
2941 const ARMSubtarget *Subtarget) {
2942 SDNode *N = Op.getNode();
2943 if (!N->hasOneUse())
2944 // Otherwise it requires moving the value from fp to integer registers.
2945 return false;
2946 if (!N->getNumValues())
2947 return false;
2948 EVT VT = Op.getValueType();
2949 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2950 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2951 // vmrs are very slow, e.g. cortex-a8.
2952 return false;
2953
2954 if (isFloatingPointZero(Op)) {
2955 SeenZero = true;
2956 return true;
2957 }
2958 return ISD::isNormalLoad(N);
2959}
2960
2961static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2962 if (isFloatingPointZero(Op))
2963 return DAG.getConstant(0, MVT::i32);
2964
2965 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2966 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002967 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002968 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002969 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002970
2971 llvm_unreachable("Unknown VFP cmp argument!");
2972}
2973
2974static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2975 SDValue &RetVal1, SDValue &RetVal2) {
2976 if (isFloatingPointZero(Op)) {
2977 RetVal1 = DAG.getConstant(0, MVT::i32);
2978 RetVal2 = DAG.getConstant(0, MVT::i32);
2979 return;
2980 }
2981
2982 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2983 SDValue Ptr = Ld->getBasePtr();
2984 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2985 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002986 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002987 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002988 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002989
2990 EVT PtrType = Ptr.getValueType();
2991 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2992 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2993 PtrType, Ptr, DAG.getConstant(4, PtrType));
2994 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2995 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002996 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002997 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002998 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002999 return;
3000 }
3001
3002 llvm_unreachable("Unknown VFP cmp argument!");
3003}
3004
3005/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3006/// f32 and even f64 comparisons to integer ones.
3007SDValue
3008ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3009 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003010 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003011 SDValue LHS = Op.getOperand(2);
3012 SDValue RHS = Op.getOperand(3);
3013 SDValue Dest = Op.getOperand(4);
3014 DebugLoc dl = Op.getDebugLoc();
3015
Evan Chengfc501a32012-03-01 23:27:13 +00003016 bool LHSSeenZero = false;
3017 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3018 bool RHSSeenZero = false;
3019 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3020 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003021 // If unsafe fp math optimization is enabled and there are no other uses of
3022 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003023 // to an integer comparison.
3024 if (CC == ISD::SETOEQ)
3025 CC = ISD::SETEQ;
3026 else if (CC == ISD::SETUNE)
3027 CC = ISD::SETNE;
3028
Evan Chengfc501a32012-03-01 23:27:13 +00003029 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003030 SDValue ARMcc;
3031 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003032 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3033 bitcastf32Toi32(LHS, DAG), Mask);
3034 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3035 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003036 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3037 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3038 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3039 Chain, Dest, ARMcc, CCR, Cmp);
3040 }
3041
3042 SDValue LHS1, LHS2;
3043 SDValue RHS1, RHS2;
3044 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3045 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003046 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3047 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003048 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3049 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003050 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003051 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3052 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3053 }
3054
3055 return SDValue();
3056}
3057
3058SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3059 SDValue Chain = Op.getOperand(0);
3060 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3061 SDValue LHS = Op.getOperand(2);
3062 SDValue RHS = Op.getOperand(3);
3063 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003064 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003065
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003067 SDValue ARMcc;
3068 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003069 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003070 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003071 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003072 }
3073
Owen Anderson825b72b2009-08-11 20:47:22 +00003074 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003075
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003076 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003077 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3078 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3079 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3080 if (Result.getNode())
3081 return Result;
3082 }
3083
Evan Chenga8e29892007-01-19 07:51:42 +00003084 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003085 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003086
Evan Cheng218977b2010-07-13 19:27:42 +00003087 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3088 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003090 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003091 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003092 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003093 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003094 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3095 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003096 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003097 }
3098 return Res;
3099}
3100
Dan Gohmand858e902010-04-17 15:26:15 +00003101SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003102 SDValue Chain = Op.getOperand(0);
3103 SDValue Table = Op.getOperand(1);
3104 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003105 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003106
Owen Andersone50ed302009-08-10 22:56:29 +00003107 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003108 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3109 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003110 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003111 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003112 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003113 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3114 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003115 if (Subtarget->isThumb2()) {
3116 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3117 // which does another jump to the destination. This also makes it easier
3118 // to translate it to TBB / TBH later.
3119 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003120 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003121 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003122 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003123 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003124 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003125 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003126 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003127 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003128 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003130 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003131 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003132 MachinePointerInfo::getJumpTable(),
3133 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003134 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003136 }
Evan Chenga8e29892007-01-19 07:51:42 +00003137}
3138
Eli Friedman14e809c2011-11-09 23:36:02 +00003139static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003140 EVT VT = Op.getValueType();
3141 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003142
James Molloy873fd5f2012-02-20 09:24:05 +00003143 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3144 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3145 return Op;
3146 return DAG.UnrollVectorOp(Op.getNode());
3147 }
3148
3149 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3150 "Invalid type for custom lowering!");
3151 if (VT != MVT::v4i16)
3152 return DAG.UnrollVectorOp(Op.getNode());
3153
3154 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3155 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003156}
3157
Bob Wilson76a312b2010-03-19 22:51:32 +00003158static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003159 EVT VT = Op.getValueType();
3160 if (VT.isVector())
3161 return LowerVectorFP_TO_INT(Op, DAG);
3162
Bob Wilson76a312b2010-03-19 22:51:32 +00003163 DebugLoc dl = Op.getDebugLoc();
3164 unsigned Opc;
3165
3166 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003167 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003168 case ISD::FP_TO_SINT:
3169 Opc = ARMISD::FTOSI;
3170 break;
3171 case ISD::FP_TO_UINT:
3172 Opc = ARMISD::FTOUI;
3173 break;
3174 }
3175 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003176 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003177}
3178
Cameron Zwarich3007d332011-03-29 21:41:55 +00003179static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3180 EVT VT = Op.getValueType();
3181 DebugLoc dl = Op.getDebugLoc();
3182
Eli Friedman14e809c2011-11-09 23:36:02 +00003183 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3184 if (VT.getVectorElementType() == MVT::f32)
3185 return Op;
3186 return DAG.UnrollVectorOp(Op.getNode());
3187 }
3188
Duncan Sands1f6a3292011-08-12 14:54:45 +00003189 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3190 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003191 if (VT != MVT::v4f32)
3192 return DAG.UnrollVectorOp(Op.getNode());
3193
3194 unsigned CastOpc;
3195 unsigned Opc;
3196 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003197 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003198 case ISD::SINT_TO_FP:
3199 CastOpc = ISD::SIGN_EXTEND;
3200 Opc = ISD::SINT_TO_FP;
3201 break;
3202 case ISD::UINT_TO_FP:
3203 CastOpc = ISD::ZERO_EXTEND;
3204 Opc = ISD::UINT_TO_FP;
3205 break;
3206 }
3207
3208 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3209 return DAG.getNode(Opc, dl, VT, Op);
3210}
3211
Bob Wilson76a312b2010-03-19 22:51:32 +00003212static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3213 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003214 if (VT.isVector())
3215 return LowerVectorINT_TO_FP(Op, DAG);
3216
Bob Wilson76a312b2010-03-19 22:51:32 +00003217 DebugLoc dl = Op.getDebugLoc();
3218 unsigned Opc;
3219
3220 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003221 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003222 case ISD::SINT_TO_FP:
3223 Opc = ARMISD::SITOF;
3224 break;
3225 case ISD::UINT_TO_FP:
3226 Opc = ARMISD::UITOF;
3227 break;
3228 }
3229
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003230 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003231 return DAG.getNode(Opc, dl, VT, Op);
3232}
3233
Evan Cheng515fe3a2010-07-08 02:08:50 +00003234SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003235 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003236 SDValue Tmp0 = Op.getOperand(0);
3237 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003238 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003239 EVT VT = Op.getValueType();
3240 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003241 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3242 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3243 bool UseNEON = !InGPR && Subtarget->hasNEON();
3244
3245 if (UseNEON) {
3246 // Use VBSL to copy the sign bit.
3247 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3248 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3249 DAG.getTargetConstant(EncodedVal, MVT::i32));
3250 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3251 if (VT == MVT::f64)
3252 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3253 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3254 DAG.getConstant(32, MVT::i32));
3255 else /*if (VT == MVT::f32)*/
3256 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3257 if (SrcVT == MVT::f32) {
3258 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3259 if (VT == MVT::f64)
3260 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3261 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3262 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003263 } else if (VT == MVT::f32)
3264 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3265 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3266 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003267 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3268 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3269
3270 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3271 MVT::i32);
3272 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3273 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3274 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003275
Evan Chenge573fb32011-02-23 02:24:55 +00003276 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3277 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3278 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003279 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003280 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3281 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3282 DAG.getConstant(0, MVT::i32));
3283 } else {
3284 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3285 }
3286
3287 return Res;
3288 }
Evan Chengc143dd42011-02-11 02:28:55 +00003289
3290 // Bitcast operand 1 to i32.
3291 if (SrcVT == MVT::f64)
3292 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3293 &Tmp1, 1).getValue(1);
3294 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3295
Evan Chenge573fb32011-02-23 02:24:55 +00003296 // Or in the signbit with integer operations.
3297 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3298 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3299 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3300 if (VT == MVT::f32) {
3301 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3302 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3303 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3304 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003305 }
3306
Evan Chenge573fb32011-02-23 02:24:55 +00003307 // f64: Or the high part with signbit and then combine two parts.
3308 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3309 &Tmp0, 1);
3310 SDValue Lo = Tmp0.getValue(0);
3311 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3312 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3313 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003314}
3315
Evan Cheng2457f2c2010-05-22 01:47:14 +00003316SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3317 MachineFunction &MF = DAG.getMachineFunction();
3318 MachineFrameInfo *MFI = MF.getFrameInfo();
3319 MFI->setReturnAddressIsTaken(true);
3320
3321 EVT VT = Op.getValueType();
3322 DebugLoc dl = Op.getDebugLoc();
3323 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3324 if (Depth) {
3325 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3326 SDValue Offset = DAG.getConstant(4, MVT::i32);
3327 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3328 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003329 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003330 }
3331
3332 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003333 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003334 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3335}
3336
Dan Gohmand858e902010-04-17 15:26:15 +00003337SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003338 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3339 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003340
Owen Andersone50ed302009-08-10 22:56:29 +00003341 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003342 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3343 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003344 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003345 ? ARM::R7 : ARM::R11;
3346 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3347 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003348 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3349 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003350 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003351 return FrameAddr;
3352}
3353
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003354/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003355/// expand a bit convert where either the source or destination type is i64 to
3356/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3357/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3358/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003359static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003360 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3361 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003362 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003363
Bob Wilson9f3f0612010-04-17 05:30:19 +00003364 // This function is only supposed to be called for i64 types, either as the
3365 // source or destination of the bit convert.
3366 EVT SrcVT = Op.getValueType();
3367 EVT DstVT = N->getValueType(0);
3368 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003369 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003370
Bob Wilson9f3f0612010-04-17 05:30:19 +00003371 // Turn i64->f64 into VMOVDRR.
3372 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3374 DAG.getConstant(0, MVT::i32));
3375 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3376 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003377 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003378 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003379 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003380
Jim Grosbache5165492009-11-09 00:11:35 +00003381 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003382 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3383 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3384 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3385 // Merge the pieces into a single i64 value.
3386 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3387 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003388
Bob Wilson9f3f0612010-04-17 05:30:19 +00003389 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003390}
3391
Bob Wilson5bafff32009-06-22 23:27:02 +00003392/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003393/// Zero vectors are used to represent vector negation and in those cases
3394/// will be implemented with the NEON VNEG instruction. However, VNEG does
3395/// not support i64 elements, so sometimes the zero vectors will need to be
3396/// explicitly constructed. Regardless, use a canonical VMOV to create the
3397/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003398static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003399 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003400 // The canonical modified immediate encoding of a zero vector is....0!
3401 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3402 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3403 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003404 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003405}
3406
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003407/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3408/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003409SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3410 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003411 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3412 EVT VT = Op.getValueType();
3413 unsigned VTBits = VT.getSizeInBits();
3414 DebugLoc dl = Op.getDebugLoc();
3415 SDValue ShOpLo = Op.getOperand(0);
3416 SDValue ShOpHi = Op.getOperand(1);
3417 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003418 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003419 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003420
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003421 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3422
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003423 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3424 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3425 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3426 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3427 DAG.getConstant(VTBits, MVT::i32));
3428 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3429 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003430 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003431
3432 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3433 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003434 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003435 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003436 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003437 CCR, Cmp);
3438
3439 SDValue Ops[2] = { Lo, Hi };
3440 return DAG.getMergeValues(Ops, 2, dl);
3441}
3442
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003443/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3444/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003445SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3446 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003447 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3448 EVT VT = Op.getValueType();
3449 unsigned VTBits = VT.getSizeInBits();
3450 DebugLoc dl = Op.getDebugLoc();
3451 SDValue ShOpLo = Op.getOperand(0);
3452 SDValue ShOpHi = Op.getOperand(1);
3453 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003454 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003455
3456 assert(Op.getOpcode() == ISD::SHL_PARTS);
3457 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3458 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3459 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3460 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3461 DAG.getConstant(VTBits, MVT::i32));
3462 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3463 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3464
3465 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3466 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3467 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003468 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003469 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003470 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003471 CCR, Cmp);
3472
3473 SDValue Ops[2] = { Lo, Hi };
3474 return DAG.getMergeValues(Ops, 2, dl);
3475}
3476
Jim Grosbach4725ca72010-09-08 03:54:02 +00003477SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003478 SelectionDAG &DAG) const {
3479 // The rounding mode is in bits 23:22 of the FPSCR.
3480 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3481 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3482 // so that the shift + and get folded into a bitfield extract.
3483 DebugLoc dl = Op.getDebugLoc();
3484 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3485 DAG.getConstant(Intrinsic::arm_get_fpscr,
3486 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003487 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003488 DAG.getConstant(1U << 22, MVT::i32));
3489 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3490 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003491 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003492 DAG.getConstant(3, MVT::i32));
3493}
3494
Jim Grosbach3482c802010-01-18 19:58:49 +00003495static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3496 const ARMSubtarget *ST) {
3497 EVT VT = N->getValueType(0);
3498 DebugLoc dl = N->getDebugLoc();
3499
3500 if (!ST->hasV6T2Ops())
3501 return SDValue();
3502
3503 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3504 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3505}
3506
Bob Wilson5bafff32009-06-22 23:27:02 +00003507static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3508 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003509 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003510 DebugLoc dl = N->getDebugLoc();
3511
Bob Wilsond5448bb2010-11-18 21:16:28 +00003512 if (!VT.isVector())
3513 return SDValue();
3514
Bob Wilson5bafff32009-06-22 23:27:02 +00003515 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003516 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003517
Bob Wilsond5448bb2010-11-18 21:16:28 +00003518 // Left shifts translate directly to the vshiftu intrinsic.
3519 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003520 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003521 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3522 N->getOperand(0), N->getOperand(1));
3523
3524 assert((N->getOpcode() == ISD::SRA ||
3525 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3526
3527 // NEON uses the same intrinsics for both left and right shifts. For
3528 // right shifts, the shift amounts are negative, so negate the vector of
3529 // shift amounts.
3530 EVT ShiftVT = N->getOperand(1).getValueType();
3531 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3532 getZeroVector(ShiftVT, DAG, dl),
3533 N->getOperand(1));
3534 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3535 Intrinsic::arm_neon_vshifts :
3536 Intrinsic::arm_neon_vshiftu);
3537 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3538 DAG.getConstant(vshiftInt, MVT::i32),
3539 N->getOperand(0), NegatedCount);
3540}
3541
3542static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3543 const ARMSubtarget *ST) {
3544 EVT VT = N->getValueType(0);
3545 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003546
Eli Friedmance392eb2009-08-22 03:13:10 +00003547 // We can get here for a node like i32 = ISD::SHL i32, i64
3548 if (VT != MVT::i64)
3549 return SDValue();
3550
3551 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003552 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003553
Chris Lattner27a6c732007-11-24 07:07:01 +00003554 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3555 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003556 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003557 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattner27a6c732007-11-24 07:07:01 +00003559 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003560 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003561
Chris Lattner27a6c732007-11-24 07:07:01 +00003562 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003564 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003566 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003567
Chris Lattner27a6c732007-11-24 07:07:01 +00003568 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3569 // captures the result into a carry flag.
3570 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003571 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003572
Chris Lattner27a6c732007-11-24 07:07:01 +00003573 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003575
Chris Lattner27a6c732007-11-24 07:07:01 +00003576 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003578}
3579
Bob Wilson5bafff32009-06-22 23:27:02 +00003580static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3581 SDValue TmpOp0, TmpOp1;
3582 bool Invert = false;
3583 bool Swap = false;
3584 unsigned Opc = 0;
3585
3586 SDValue Op0 = Op.getOperand(0);
3587 SDValue Op1 = Op.getOperand(1);
3588 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003589 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003590 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3591 DebugLoc dl = Op.getDebugLoc();
3592
3593 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3594 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003595 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003596 case ISD::SETUNE:
3597 case ISD::SETNE: Invert = true; // Fallthrough
3598 case ISD::SETOEQ:
3599 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3600 case ISD::SETOLT:
3601 case ISD::SETLT: Swap = true; // Fallthrough
3602 case ISD::SETOGT:
3603 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3604 case ISD::SETOLE:
3605 case ISD::SETLE: Swap = true; // Fallthrough
3606 case ISD::SETOGE:
3607 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3608 case ISD::SETUGE: Swap = true; // Fallthrough
3609 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3610 case ISD::SETUGT: Swap = true; // Fallthrough
3611 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3612 case ISD::SETUEQ: Invert = true; // Fallthrough
3613 case ISD::SETONE:
3614 // Expand this to (OLT | OGT).
3615 TmpOp0 = Op0;
3616 TmpOp1 = Op1;
3617 Opc = ISD::OR;
3618 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3619 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3620 break;
3621 case ISD::SETUO: Invert = true; // Fallthrough
3622 case ISD::SETO:
3623 // Expand this to (OLT | OGE).
3624 TmpOp0 = Op0;
3625 TmpOp1 = Op1;
3626 Opc = ISD::OR;
3627 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3628 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3629 break;
3630 }
3631 } else {
3632 // Integer comparisons.
3633 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003634 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003635 case ISD::SETNE: Invert = true;
3636 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3637 case ISD::SETLT: Swap = true;
3638 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3639 case ISD::SETLE: Swap = true;
3640 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3641 case ISD::SETULT: Swap = true;
3642 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3643 case ISD::SETULE: Swap = true;
3644 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3645 }
3646
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003647 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003648 if (Opc == ARMISD::VCEQ) {
3649
3650 SDValue AndOp;
3651 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3652 AndOp = Op0;
3653 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3654 AndOp = Op1;
3655
3656 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003658 AndOp = AndOp.getOperand(0);
3659
3660 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3661 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003662 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3663 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003664 Invert = !Invert;
3665 }
3666 }
3667 }
3668
3669 if (Swap)
3670 std::swap(Op0, Op1);
3671
Owen Andersonc24cb352010-11-08 23:21:22 +00003672 // If one of the operands is a constant vector zero, attempt to fold the
3673 // comparison to a specialized compare-against-zero form.
3674 SDValue SingleOp;
3675 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3676 SingleOp = Op0;
3677 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3678 if (Opc == ARMISD::VCGE)
3679 Opc = ARMISD::VCLEZ;
3680 else if (Opc == ARMISD::VCGT)
3681 Opc = ARMISD::VCLTZ;
3682 SingleOp = Op1;
3683 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003684
Owen Andersonc24cb352010-11-08 23:21:22 +00003685 SDValue Result;
3686 if (SingleOp.getNode()) {
3687 switch (Opc) {
3688 case ARMISD::VCEQ:
3689 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3690 case ARMISD::VCGE:
3691 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3692 case ARMISD::VCLEZ:
3693 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3694 case ARMISD::VCGT:
3695 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3696 case ARMISD::VCLTZ:
3697 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3698 default:
3699 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3700 }
3701 } else {
3702 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3703 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003704
3705 if (Invert)
3706 Result = DAG.getNOT(dl, Result, VT);
3707
3708 return Result;
3709}
3710
Bob Wilsond3c42842010-06-14 22:19:57 +00003711/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3712/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003713/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003714static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3715 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003716 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003717 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003718
Bob Wilson827b2102010-06-15 19:05:35 +00003719 // SplatBitSize is set to the smallest size that splats the vector, so a
3720 // zero vector will always have SplatBitSize == 8. However, NEON modified
3721 // immediate instructions others than VMOV do not support the 8-bit encoding
3722 // of a zero vector, and the default encoding of zero is supposed to be the
3723 // 32-bit version.
3724 if (SplatBits == 0)
3725 SplatBitSize = 32;
3726
Bob Wilson5bafff32009-06-22 23:27:02 +00003727 switch (SplatBitSize) {
3728 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003729 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003730 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003732 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003733 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003734 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003735 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003737
3738 case 16:
3739 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003740 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003741 if ((SplatBits & ~0xff) == 0) {
3742 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003743 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003744 Imm = SplatBits;
3745 break;
3746 }
3747 if ((SplatBits & ~0xff00) == 0) {
3748 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003749 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003750 Imm = SplatBits >> 8;
3751 break;
3752 }
3753 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003754
3755 case 32:
3756 // NEON's 32-bit VMOV supports splat values where:
3757 // * only one byte is nonzero, or
3758 // * the least significant byte is 0xff and the second byte is nonzero, or
3759 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003760 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003761 if ((SplatBits & ~0xff) == 0) {
3762 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003763 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003764 Imm = SplatBits;
3765 break;
3766 }
3767 if ((SplatBits & ~0xff00) == 0) {
3768 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003769 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003770 Imm = SplatBits >> 8;
3771 break;
3772 }
3773 if ((SplatBits & ~0xff0000) == 0) {
3774 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003775 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003776 Imm = SplatBits >> 16;
3777 break;
3778 }
3779 if ((SplatBits & ~0xff000000) == 0) {
3780 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003781 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003782 Imm = SplatBits >> 24;
3783 break;
3784 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003785
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003786 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3787 if (type == OtherModImm) return SDValue();
3788
Bob Wilson5bafff32009-06-22 23:27:02 +00003789 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003790 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3791 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003792 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003793 Imm = SplatBits >> 8;
3794 SplatBits |= 0xff;
3795 break;
3796 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003797
3798 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003799 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3800 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003801 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003802 Imm = SplatBits >> 16;
3803 SplatBits |= 0xffff;
3804 break;
3805 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003806
3807 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3808 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3809 // VMOV.I32. A (very) minor optimization would be to replicate the value
3810 // and fall through here to test for a valid 64-bit splat. But, then the
3811 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003812 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003813
3814 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003815 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003816 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003817 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003818 uint64_t BitMask = 0xff;
3819 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 unsigned ImmMask = 1;
3821 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003822 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003823 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 Imm |= ImmMask;
3826 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003827 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003828 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003829 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003830 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003831 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003832 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003833 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003834 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003835 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003836 break;
3837 }
3838
Bob Wilson1a913ed2010-06-11 21:34:50 +00003839 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003840 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003841 }
3842
Bob Wilsoncba270d2010-07-13 21:16:48 +00003843 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3844 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003845}
3846
Lang Hamesc0a9f822012-03-29 21:56:11 +00003847SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3848 const ARMSubtarget *ST) const {
3849 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3850 return SDValue();
3851
3852 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3853 assert(Op.getValueType() == MVT::f32 &&
3854 "ConstantFP custom lowering should only occur for f32.");
3855
3856 // Try splatting with a VMOV.f32...
3857 APFloat FPVal = CFP->getValueAPF();
3858 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3859 if (ImmVal != -1) {
3860 DebugLoc DL = Op.getDebugLoc();
3861 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3862 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3863 NewVal);
3864 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3865 DAG.getConstant(0, MVT::i32));
3866 }
3867
3868 // If that fails, try a VMOV.i32
3869 EVT VMovVT;
3870 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3871 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3872 VMOVModImm);
3873 if (NewVal != SDValue()) {
3874 DebugLoc DL = Op.getDebugLoc();
3875 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3876 NewVal);
3877 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3878 VecConstant);
3879 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3880 DAG.getConstant(0, MVT::i32));
3881 }
3882
3883 // Finally, try a VMVN.i32
3884 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3885 VMVNModImm);
3886 if (NewVal != SDValue()) {
3887 DebugLoc DL = Op.getDebugLoc();
3888 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3889 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3890 VecConstant);
3891 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3892 DAG.getConstant(0, MVT::i32));
3893 }
3894
3895 return SDValue();
3896}
3897
3898
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003899static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003900 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003901 unsigned NumElts = VT.getVectorNumElements();
3902 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003903
3904 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3905 if (M[0] < 0)
3906 return false;
3907
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003908 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003909
3910 // If this is a VEXT shuffle, the immediate value is the index of the first
3911 // element. The other shuffle indices must be the successive elements after
3912 // the first one.
3913 unsigned ExpectedElt = Imm;
3914 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003915 // Increment the expected index. If it wraps around, it may still be
3916 // a VEXT but the source vectors must be swapped.
3917 ExpectedElt += 1;
3918 if (ExpectedElt == NumElts * 2) {
3919 ExpectedElt = 0;
3920 ReverseVEXT = true;
3921 }
3922
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003923 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003924 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003925 return false;
3926 }
3927
3928 // Adjust the index value if the source operands will be swapped.
3929 if (ReverseVEXT)
3930 Imm -= NumElts;
3931
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003932 return true;
3933}
3934
Bob Wilson8bb9e482009-07-26 00:39:34 +00003935/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3936/// instruction with the specified blocksize. (The order of the elements
3937/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003938static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003939 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3940 "Only possible block sizes for VREV are: 16, 32, 64");
3941
Bob Wilson8bb9e482009-07-26 00:39:34 +00003942 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003943 if (EltSz == 64)
3944 return false;
3945
3946 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003947 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003948 // If the first shuffle index is UNDEF, be optimistic.
3949 if (M[0] < 0)
3950 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003951
3952 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3953 return false;
3954
3955 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003956 if (M[i] < 0) continue; // ignore UNDEF indices
3957 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003958 return false;
3959 }
3960
3961 return true;
3962}
3963
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003964static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003965 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3966 // range, then 0 is placed into the resulting vector. So pretty much any mask
3967 // of 8 elements can work here.
3968 return VT == MVT::v8i8 && M.size() == 8;
3969}
3970
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003971static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003972 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3973 if (EltSz == 64)
3974 return false;
3975
Bob Wilsonc692cb72009-08-21 20:54:19 +00003976 unsigned NumElts = VT.getVectorNumElements();
3977 WhichResult = (M[0] == 0 ? 0 : 1);
3978 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003979 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3980 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003981 return false;
3982 }
3983 return true;
3984}
3985
Bob Wilson324f4f12009-12-03 06:40:55 +00003986/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3987/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3988/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003989static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003990 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3991 if (EltSz == 64)
3992 return false;
3993
3994 unsigned NumElts = VT.getVectorNumElements();
3995 WhichResult = (M[0] == 0 ? 0 : 1);
3996 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003997 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3998 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003999 return false;
4000 }
4001 return true;
4002}
4003
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004004static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004005 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4006 if (EltSz == 64)
4007 return false;
4008
Bob Wilsonc692cb72009-08-21 20:54:19 +00004009 unsigned NumElts = VT.getVectorNumElements();
4010 WhichResult = (M[0] == 0 ? 0 : 1);
4011 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004012 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004013 if ((unsigned) M[i] != 2 * i + WhichResult)
4014 return false;
4015 }
4016
4017 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004018 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004019 return false;
4020
4021 return true;
4022}
4023
Bob Wilson324f4f12009-12-03 06:40:55 +00004024/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4025/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4026/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004027static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004028 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4029 if (EltSz == 64)
4030 return false;
4031
4032 unsigned Half = VT.getVectorNumElements() / 2;
4033 WhichResult = (M[0] == 0 ? 0 : 1);
4034 for (unsigned j = 0; j != 2; ++j) {
4035 unsigned Idx = WhichResult;
4036 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004037 int MIdx = M[i + j * Half];
4038 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004039 return false;
4040 Idx += 2;
4041 }
4042 }
4043
4044 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4045 if (VT.is64BitVector() && EltSz == 32)
4046 return false;
4047
4048 return true;
4049}
4050
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004051static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004052 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4053 if (EltSz == 64)
4054 return false;
4055
Bob Wilsonc692cb72009-08-21 20:54:19 +00004056 unsigned NumElts = VT.getVectorNumElements();
4057 WhichResult = (M[0] == 0 ? 0 : 1);
4058 unsigned Idx = WhichResult * NumElts / 2;
4059 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004060 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4061 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004062 return false;
4063 Idx += 1;
4064 }
4065
4066 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004067 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004068 return false;
4069
4070 return true;
4071}
4072
Bob Wilson324f4f12009-12-03 06:40:55 +00004073/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4074/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4075/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004076static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004077 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4078 if (EltSz == 64)
4079 return false;
4080
4081 unsigned NumElts = VT.getVectorNumElements();
4082 WhichResult = (M[0] == 0 ? 0 : 1);
4083 unsigned Idx = WhichResult * NumElts / 2;
4084 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004085 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4086 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004087 return false;
4088 Idx += 1;
4089 }
4090
4091 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4092 if (VT.is64BitVector() && EltSz == 32)
4093 return false;
4094
4095 return true;
4096}
4097
Dale Johannesenf630c712010-07-29 20:10:08 +00004098// If N is an integer constant that can be moved into a register in one
4099// instruction, return an SDValue of such a constant (will become a MOV
4100// instruction). Otherwise return null.
4101static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4102 const ARMSubtarget *ST, DebugLoc dl) {
4103 uint64_t Val;
4104 if (!isa<ConstantSDNode>(N))
4105 return SDValue();
4106 Val = cast<ConstantSDNode>(N)->getZExtValue();
4107
4108 if (ST->isThumb1Only()) {
4109 if (Val <= 255 || ~Val <= 255)
4110 return DAG.getConstant(Val, MVT::i32);
4111 } else {
4112 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4113 return DAG.getConstant(Val, MVT::i32);
4114 }
4115 return SDValue();
4116}
4117
Bob Wilson5bafff32009-06-22 23:27:02 +00004118// If this is a case we can't handle, return null and let the default
4119// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004120SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4121 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004122 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004123 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004124 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004125
4126 APInt SplatBits, SplatUndef;
4127 unsigned SplatBitSize;
4128 bool HasAnyUndefs;
4129 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004130 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004131 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004132 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004133 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004134 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004135 DAG, VmovVT, VT.is128BitVector(),
4136 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004137 if (Val.getNode()) {
4138 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004139 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004140 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004141
4142 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004143 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004144 Val = isNEONModifiedImm(NegatedImm,
4145 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004146 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004147 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004148 if (Val.getNode()) {
4149 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004150 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004151 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004152
4153 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004154 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004155 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004156 if (ImmVal != -1) {
4157 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4158 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4159 }
4160 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004161 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004162 }
4163
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004164 // Scan through the operands to see if only one value is used.
4165 unsigned NumElts = VT.getVectorNumElements();
4166 bool isOnlyLowElement = true;
4167 bool usesOnlyOneValue = true;
4168 bool isConstant = true;
4169 SDValue Value;
4170 for (unsigned i = 0; i < NumElts; ++i) {
4171 SDValue V = Op.getOperand(i);
4172 if (V.getOpcode() == ISD::UNDEF)
4173 continue;
4174 if (i > 0)
4175 isOnlyLowElement = false;
4176 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4177 isConstant = false;
4178
4179 if (!Value.getNode())
4180 Value = V;
4181 else if (V != Value)
4182 usesOnlyOneValue = false;
4183 }
4184
4185 if (!Value.getNode())
4186 return DAG.getUNDEF(VT);
4187
4188 if (isOnlyLowElement)
4189 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4190
Dale Johannesenf630c712010-07-29 20:10:08 +00004191 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4192
Dale Johannesen575cd142010-10-19 20:00:17 +00004193 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4194 // i32 and try again.
4195 if (usesOnlyOneValue && EltSize <= 32) {
4196 if (!isConstant)
4197 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4198 if (VT.getVectorElementType().isFloatingPoint()) {
4199 SmallVector<SDValue, 8> Ops;
4200 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004201 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004202 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004203 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4204 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004205 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4206 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004207 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004208 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004209 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4210 if (Val.getNode())
4211 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004212 }
4213
4214 // If all elements are constants and the case above didn't get hit, fall back
4215 // to the default expansion, which will generate a load from the constant
4216 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004217 if (isConstant)
4218 return SDValue();
4219
Bob Wilson11a1dff2011-01-07 21:37:30 +00004220 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4221 if (NumElts >= 4) {
4222 SDValue shuffle = ReconstructShuffle(Op, DAG);
4223 if (shuffle != SDValue())
4224 return shuffle;
4225 }
4226
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004227 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004228 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4229 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004230 if (EltSize >= 32) {
4231 // Do the expansion with floating-point types, since that is what the VFP
4232 // registers are defined to use, and since i64 is not legal.
4233 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4234 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004235 SmallVector<SDValue, 8> Ops;
4236 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004237 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004238 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004239 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004240 }
4241
4242 return SDValue();
4243}
4244
Bob Wilson11a1dff2011-01-07 21:37:30 +00004245// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004246// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004247SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4248 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004249 DebugLoc dl = Op.getDebugLoc();
4250 EVT VT = Op.getValueType();
4251 unsigned NumElts = VT.getVectorNumElements();
4252
4253 SmallVector<SDValue, 2> SourceVecs;
4254 SmallVector<unsigned, 2> MinElts;
4255 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004256
Bob Wilson11a1dff2011-01-07 21:37:30 +00004257 for (unsigned i = 0; i < NumElts; ++i) {
4258 SDValue V = Op.getOperand(i);
4259 if (V.getOpcode() == ISD::UNDEF)
4260 continue;
4261 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4262 // A shuffle can only come from building a vector from various
4263 // elements of other vectors.
4264 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004265 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4266 VT.getVectorElementType()) {
4267 // This code doesn't know how to handle shuffles where the vector
4268 // element types do not match (this happens because type legalization
4269 // promotes the return type of EXTRACT_VECTOR_ELT).
4270 // FIXME: It might be appropriate to extend this code to handle
4271 // mismatched types.
4272 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004273 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004274
Bob Wilson11a1dff2011-01-07 21:37:30 +00004275 // Record this extraction against the appropriate vector if possible...
4276 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004277 // If the element number isn't a constant, we can't effectively
4278 // analyze what's going on.
4279 if (!isa<ConstantSDNode>(V.getOperand(1)))
4280 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004281 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4282 bool FoundSource = false;
4283 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4284 if (SourceVecs[j] == SourceVec) {
4285 if (MinElts[j] > EltNo)
4286 MinElts[j] = EltNo;
4287 if (MaxElts[j] < EltNo)
4288 MaxElts[j] = EltNo;
4289 FoundSource = true;
4290 break;
4291 }
4292 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004293
Bob Wilson11a1dff2011-01-07 21:37:30 +00004294 // Or record a new source if not...
4295 if (!FoundSource) {
4296 SourceVecs.push_back(SourceVec);
4297 MinElts.push_back(EltNo);
4298 MaxElts.push_back(EltNo);
4299 }
4300 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004301
Bob Wilson11a1dff2011-01-07 21:37:30 +00004302 // Currently only do something sane when at most two source vectors
4303 // involved.
4304 if (SourceVecs.size() > 2)
4305 return SDValue();
4306
4307 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4308 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004309
Bob Wilson11a1dff2011-01-07 21:37:30 +00004310 // This loop extracts the usage patterns of the source vectors
4311 // and prepares appropriate SDValues for a shuffle if possible.
4312 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4313 if (SourceVecs[i].getValueType() == VT) {
4314 // No VEXT necessary
4315 ShuffleSrcs[i] = SourceVecs[i];
4316 VEXTOffsets[i] = 0;
4317 continue;
4318 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4319 // It probably isn't worth padding out a smaller vector just to
4320 // break it down again in a shuffle.
4321 return SDValue();
4322 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004323
Bob Wilson11a1dff2011-01-07 21:37:30 +00004324 // Since only 64-bit and 128-bit vectors are legal on ARM and
4325 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004326 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4327 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004328
Bob Wilson11a1dff2011-01-07 21:37:30 +00004329 if (MaxElts[i] - MinElts[i] >= NumElts) {
4330 // Span too large for a VEXT to cope
4331 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004332 }
4333
Bob Wilson11a1dff2011-01-07 21:37:30 +00004334 if (MinElts[i] >= NumElts) {
4335 // The extraction can just take the second half
4336 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004337 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4338 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004339 DAG.getIntPtrConstant(NumElts));
4340 } else if (MaxElts[i] < NumElts) {
4341 // The extraction can just take the first half
4342 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004343 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4344 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004345 DAG.getIntPtrConstant(0));
4346 } else {
4347 // An actual VEXT is needed
4348 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004349 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4350 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004351 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004352 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4353 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004354 DAG.getIntPtrConstant(NumElts));
4355 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4356 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4357 }
4358 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004359
Bob Wilson11a1dff2011-01-07 21:37:30 +00004360 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004361
Bob Wilson11a1dff2011-01-07 21:37:30 +00004362 for (unsigned i = 0; i < NumElts; ++i) {
4363 SDValue Entry = Op.getOperand(i);
4364 if (Entry.getOpcode() == ISD::UNDEF) {
4365 Mask.push_back(-1);
4366 continue;
4367 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004368
Bob Wilson11a1dff2011-01-07 21:37:30 +00004369 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004370 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4371 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004372 if (ExtractVec == SourceVecs[0]) {
4373 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4374 } else {
4375 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4376 }
4377 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004378
Bob Wilson11a1dff2011-01-07 21:37:30 +00004379 // Final check before we try to produce nonsense...
4380 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004381 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4382 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004383
Bob Wilson11a1dff2011-01-07 21:37:30 +00004384 return SDValue();
4385}
4386
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004387/// isShuffleMaskLegal - Targets can use this to indicate that they only
4388/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4389/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4390/// are assumed to be legal.
4391bool
4392ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4393 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004394 if (VT.getVectorNumElements() == 4 &&
4395 (VT.is128BitVector() || VT.is64BitVector())) {
4396 unsigned PFIndexes[4];
4397 for (unsigned i = 0; i != 4; ++i) {
4398 if (M[i] < 0)
4399 PFIndexes[i] = 8;
4400 else
4401 PFIndexes[i] = M[i];
4402 }
4403
4404 // Compute the index in the perfect shuffle table.
4405 unsigned PFTableIndex =
4406 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4407 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4408 unsigned Cost = (PFEntry >> 30);
4409
4410 if (Cost <= 4)
4411 return true;
4412 }
4413
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004414 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004415 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004416
Bob Wilson53dd2452010-06-07 23:53:38 +00004417 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4418 return (EltSize >= 32 ||
4419 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004420 isVREVMask(M, VT, 64) ||
4421 isVREVMask(M, VT, 32) ||
4422 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004423 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004424 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004425 isVTRNMask(M, VT, WhichResult) ||
4426 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004427 isVZIPMask(M, VT, WhichResult) ||
4428 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4429 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4430 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004431}
4432
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004433/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4434/// the specified operations to build the shuffle.
4435static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4436 SDValue RHS, SelectionDAG &DAG,
4437 DebugLoc dl) {
4438 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4439 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4440 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4441
4442 enum {
4443 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4444 OP_VREV,
4445 OP_VDUP0,
4446 OP_VDUP1,
4447 OP_VDUP2,
4448 OP_VDUP3,
4449 OP_VEXT1,
4450 OP_VEXT2,
4451 OP_VEXT3,
4452 OP_VUZPL, // VUZP, left result
4453 OP_VUZPR, // VUZP, right result
4454 OP_VZIPL, // VZIP, left result
4455 OP_VZIPR, // VZIP, right result
4456 OP_VTRNL, // VTRN, left result
4457 OP_VTRNR // VTRN, right result
4458 };
4459
4460 if (OpNum == OP_COPY) {
4461 if (LHSID == (1*9+2)*9+3) return LHS;
4462 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4463 return RHS;
4464 }
4465
4466 SDValue OpLHS, OpRHS;
4467 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4468 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4469 EVT VT = OpLHS.getValueType();
4470
4471 switch (OpNum) {
4472 default: llvm_unreachable("Unknown shuffle opcode!");
4473 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004474 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004475 if (VT.getVectorElementType() == MVT::i32 ||
4476 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004477 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4478 // vrev <4 x i16> -> VREV32
4479 if (VT.getVectorElementType() == MVT::i16)
4480 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4481 // vrev <4 x i8> -> VREV16
4482 assert(VT.getVectorElementType() == MVT::i8);
4483 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004484 case OP_VDUP0:
4485 case OP_VDUP1:
4486 case OP_VDUP2:
4487 case OP_VDUP3:
4488 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004489 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004490 case OP_VEXT1:
4491 case OP_VEXT2:
4492 case OP_VEXT3:
4493 return DAG.getNode(ARMISD::VEXT, dl, VT,
4494 OpLHS, OpRHS,
4495 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4496 case OP_VUZPL:
4497 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004498 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004499 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4500 case OP_VZIPL:
4501 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004502 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004503 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4504 case OP_VTRNL:
4505 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004506 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4507 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004508 }
4509}
4510
Bill Wendling69a05a72011-03-14 23:02:38 +00004511static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004512 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004513 SelectionDAG &DAG) {
4514 // Check to see if we can use the VTBL instruction.
4515 SDValue V1 = Op.getOperand(0);
4516 SDValue V2 = Op.getOperand(1);
4517 DebugLoc DL = Op.getDebugLoc();
4518
4519 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004520 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004521 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4522 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4523
4524 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4525 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4526 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4527 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004528
Owen Anderson76706012011-04-05 21:48:57 +00004529 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004530 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4531 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004532}
4533
Bob Wilson5bafff32009-06-22 23:27:02 +00004534static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004535 SDValue V1 = Op.getOperand(0);
4536 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004537 DebugLoc dl = Op.getDebugLoc();
4538 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004539 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004540
Bob Wilson28865062009-08-13 02:13:04 +00004541 // Convert shuffles that are directly supported on NEON to target-specific
4542 // DAG nodes, instead of keeping them as shuffles and matching them again
4543 // during code selection. This is more efficient and avoids the possibility
4544 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004545 // FIXME: floating-point vectors should be canonicalized to integer vectors
4546 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004547 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004548
Bob Wilson53dd2452010-06-07 23:53:38 +00004549 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4550 if (EltSize <= 32) {
4551 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4552 int Lane = SVN->getSplatIndex();
4553 // If this is undef splat, generate it via "just" vdup, if possible.
4554 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004555
Dan Gohman65fd6562011-11-03 21:49:52 +00004556 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004557 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4558 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4559 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004560 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4561 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4562 // reaches it).
4563 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4564 !isa<ConstantSDNode>(V1.getOperand(0))) {
4565 bool IsScalarToVector = true;
4566 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4567 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4568 IsScalarToVector = false;
4569 break;
4570 }
4571 if (IsScalarToVector)
4572 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4573 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004574 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4575 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004576 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004577
4578 bool ReverseVEXT;
4579 unsigned Imm;
4580 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4581 if (ReverseVEXT)
4582 std::swap(V1, V2);
4583 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4584 DAG.getConstant(Imm, MVT::i32));
4585 }
4586
4587 if (isVREVMask(ShuffleMask, VT, 64))
4588 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4589 if (isVREVMask(ShuffleMask, VT, 32))
4590 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4591 if (isVREVMask(ShuffleMask, VT, 16))
4592 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4593
4594 // Check for Neon shuffles that modify both input vectors in place.
4595 // If both results are used, i.e., if there are two shuffles with the same
4596 // source operands and with masks corresponding to both results of one of
4597 // these operations, DAG memoization will ensure that a single node is
4598 // used for both shuffles.
4599 unsigned WhichResult;
4600 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4601 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4602 V1, V2).getValue(WhichResult);
4603 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4604 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4605 V1, V2).getValue(WhichResult);
4606 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4607 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4608 V1, V2).getValue(WhichResult);
4609
4610 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4611 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4612 V1, V1).getValue(WhichResult);
4613 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4614 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4615 V1, V1).getValue(WhichResult);
4616 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4617 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4618 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004619 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004620
Bob Wilsonc692cb72009-08-21 20:54:19 +00004621 // If the shuffle is not directly supported and it has 4 elements, use
4622 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004623 unsigned NumElts = VT.getVectorNumElements();
4624 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004625 unsigned PFIndexes[4];
4626 for (unsigned i = 0; i != 4; ++i) {
4627 if (ShuffleMask[i] < 0)
4628 PFIndexes[i] = 8;
4629 else
4630 PFIndexes[i] = ShuffleMask[i];
4631 }
4632
4633 // Compute the index in the perfect shuffle table.
4634 unsigned PFTableIndex =
4635 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004636 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4637 unsigned Cost = (PFEntry >> 30);
4638
4639 if (Cost <= 4)
4640 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4641 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004642
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004643 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004644 if (EltSize >= 32) {
4645 // Do the expansion with floating-point types, since that is what the VFP
4646 // registers are defined to use, and since i64 is not legal.
4647 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4648 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004649 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4650 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004651 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004652 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004653 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004654 Ops.push_back(DAG.getUNDEF(EltVT));
4655 else
4656 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4657 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4658 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4659 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004660 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004661 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004662 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004663 }
4664
Bill Wendling69a05a72011-03-14 23:02:38 +00004665 if (VT == MVT::v8i8) {
4666 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4667 if (NewOp.getNode())
4668 return NewOp;
4669 }
4670
Bob Wilson22cac0d2009-08-14 05:16:33 +00004671 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004672}
4673
Eli Friedman5c89cb82011-10-24 23:08:52 +00004674static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4675 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4676 SDValue Lane = Op.getOperand(2);
4677 if (!isa<ConstantSDNode>(Lane))
4678 return SDValue();
4679
4680 return Op;
4681}
4682
Bob Wilson5bafff32009-06-22 23:27:02 +00004683static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004684 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004685 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004686 if (!isa<ConstantSDNode>(Lane))
4687 return SDValue();
4688
4689 SDValue Vec = Op.getOperand(0);
4690 if (Op.getValueType() == MVT::i32 &&
4691 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4692 DebugLoc dl = Op.getDebugLoc();
4693 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4694 }
4695
4696 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004697}
4698
Bob Wilsona6d65862009-08-03 20:36:38 +00004699static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4700 // The only time a CONCAT_VECTORS operation can have legal types is when
4701 // two 64-bit vectors are concatenated to a 128-bit vector.
4702 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4703 "unexpected CONCAT_VECTORS");
4704 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004705 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004706 SDValue Op0 = Op.getOperand(0);
4707 SDValue Op1 = Op.getOperand(1);
4708 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004709 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004710 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004711 DAG.getIntPtrConstant(0));
4712 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004713 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004714 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004715 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004716 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004717}
4718
Bob Wilson626613d2010-11-23 19:38:38 +00004719/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4720/// element has been zero/sign-extended, depending on the isSigned parameter,
4721/// from an integer type half its size.
4722static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4723 bool isSigned) {
4724 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4725 EVT VT = N->getValueType(0);
4726 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4727 SDNode *BVN = N->getOperand(0).getNode();
4728 if (BVN->getValueType(0) != MVT::v4i32 ||
4729 BVN->getOpcode() != ISD::BUILD_VECTOR)
4730 return false;
4731 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4732 unsigned HiElt = 1 - LoElt;
4733 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4734 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4735 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4736 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4737 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4738 return false;
4739 if (isSigned) {
4740 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4741 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4742 return true;
4743 } else {
4744 if (Hi0->isNullValue() && Hi1->isNullValue())
4745 return true;
4746 }
4747 return false;
4748 }
4749
4750 if (N->getOpcode() != ISD::BUILD_VECTOR)
4751 return false;
4752
4753 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4754 SDNode *Elt = N->getOperand(i).getNode();
4755 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4756 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4757 unsigned HalfSize = EltSize / 2;
4758 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004759 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004760 return false;
4761 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004762 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004763 return false;
4764 }
4765 continue;
4766 }
4767 return false;
4768 }
4769
4770 return true;
4771}
4772
4773/// isSignExtended - Check if a node is a vector value that is sign-extended
4774/// or a constant BUILD_VECTOR with sign-extended elements.
4775static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4776 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4777 return true;
4778 if (isExtendedBUILD_VECTOR(N, DAG, true))
4779 return true;
4780 return false;
4781}
4782
4783/// isZeroExtended - Check if a node is a vector value that is zero-extended
4784/// or a constant BUILD_VECTOR with zero-extended elements.
4785static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4786 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4787 return true;
4788 if (isExtendedBUILD_VECTOR(N, DAG, false))
4789 return true;
4790 return false;
4791}
4792
4793/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4794/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004795static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4796 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4797 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004798 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4799 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4800 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004801 LD->isNonTemporal(), LD->isInvariant(),
4802 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004803 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4804 // have been legalized as a BITCAST from v4i32.
4805 if (N->getOpcode() == ISD::BITCAST) {
4806 SDNode *BVN = N->getOperand(0).getNode();
4807 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4808 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4809 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4810 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4811 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4812 }
4813 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4814 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4815 EVT VT = N->getValueType(0);
4816 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4817 unsigned NumElts = VT.getVectorNumElements();
4818 MVT TruncVT = MVT::getIntegerVT(EltSize);
4819 SmallVector<SDValue, 8> Ops;
4820 for (unsigned i = 0; i != NumElts; ++i) {
4821 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4822 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004823 // Element types smaller than 32 bits are not legal, so use i32 elements.
4824 // The values are implicitly truncated so sext vs. zext doesn't matter.
4825 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004826 }
4827 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4828 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004829}
4830
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004831static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4832 unsigned Opcode = N->getOpcode();
4833 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4834 SDNode *N0 = N->getOperand(0).getNode();
4835 SDNode *N1 = N->getOperand(1).getNode();
4836 return N0->hasOneUse() && N1->hasOneUse() &&
4837 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4838 }
4839 return false;
4840}
4841
4842static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4843 unsigned Opcode = N->getOpcode();
4844 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4845 SDNode *N0 = N->getOperand(0).getNode();
4846 SDNode *N1 = N->getOperand(1).getNode();
4847 return N0->hasOneUse() && N1->hasOneUse() &&
4848 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4849 }
4850 return false;
4851}
4852
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004853static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4854 // Multiplications are only custom-lowered for 128-bit vectors so that
4855 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4856 EVT VT = Op.getValueType();
4857 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4858 SDNode *N0 = Op.getOperand(0).getNode();
4859 SDNode *N1 = Op.getOperand(1).getNode();
4860 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004861 bool isMLA = false;
4862 bool isN0SExt = isSignExtended(N0, DAG);
4863 bool isN1SExt = isSignExtended(N1, DAG);
4864 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004865 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004866 else {
4867 bool isN0ZExt = isZeroExtended(N0, DAG);
4868 bool isN1ZExt = isZeroExtended(N1, DAG);
4869 if (isN0ZExt && isN1ZExt)
4870 NewOpc = ARMISD::VMULLu;
4871 else if (isN1SExt || isN1ZExt) {
4872 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4873 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4874 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4875 NewOpc = ARMISD::VMULLs;
4876 isMLA = true;
4877 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4878 NewOpc = ARMISD::VMULLu;
4879 isMLA = true;
4880 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4881 std::swap(N0, N1);
4882 NewOpc = ARMISD::VMULLu;
4883 isMLA = true;
4884 }
4885 }
4886
4887 if (!NewOpc) {
4888 if (VT == MVT::v2i64)
4889 // Fall through to expand this. It is not legal.
4890 return SDValue();
4891 else
4892 // Other vector multiplications are legal.
4893 return Op;
4894 }
4895 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004896
4897 // Legalize to a VMULL instruction.
4898 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004899 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004900 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004901 if (!isMLA) {
4902 Op0 = SkipExtension(N0, DAG);
4903 assert(Op0.getValueType().is64BitVector() &&
4904 Op1.getValueType().is64BitVector() &&
4905 "unexpected types for extended operands to VMULL");
4906 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4907 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004908
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004909 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4910 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4911 // vmull q0, d4, d6
4912 // vmlal q0, d5, d6
4913 // is faster than
4914 // vaddl q0, d4, d5
4915 // vmovl q1, d6
4916 // vmul q0, q0, q1
4917 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4918 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4919 EVT Op1VT = Op1.getValueType();
4920 return DAG.getNode(N0->getOpcode(), DL, VT,
4921 DAG.getNode(NewOpc, DL, VT,
4922 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4923 DAG.getNode(NewOpc, DL, VT,
4924 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004925}
4926
Owen Anderson76706012011-04-05 21:48:57 +00004927static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004928LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4929 // Convert to float
4930 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4931 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4932 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4933 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4934 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4935 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4936 // Get reciprocal estimate.
4937 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004938 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004939 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4940 // Because char has a smaller range than uchar, we can actually get away
4941 // without any newton steps. This requires that we use a weird bias
4942 // of 0xb000, however (again, this has been exhaustively tested).
4943 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4944 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4945 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4946 Y = DAG.getConstant(0xb000, MVT::i32);
4947 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4948 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4949 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4950 // Convert back to short.
4951 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4952 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4953 return X;
4954}
4955
Owen Anderson76706012011-04-05 21:48:57 +00004956static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004957LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4958 SDValue N2;
4959 // Convert to float.
4960 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4961 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4962 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4963 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4964 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4965 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004966
Nate Begeman7973f352011-02-11 20:53:29 +00004967 // Use reciprocal estimate and one refinement step.
4968 // float4 recip = vrecpeq_f32(yf);
4969 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004970 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004971 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004972 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004973 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4974 N1, N2);
4975 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4976 // Because short has a smaller range than ushort, we can actually get away
4977 // with only a single newton step. This requires that we use a weird bias
4978 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004979 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004980 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4981 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004982 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004983 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4984 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4985 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4986 // Convert back to integer and return.
4987 // return vmovn_s32(vcvt_s32_f32(result));
4988 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4989 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4990 return N0;
4991}
4992
4993static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4994 EVT VT = Op.getValueType();
4995 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4996 "unexpected type for custom-lowering ISD::SDIV");
4997
4998 DebugLoc dl = Op.getDebugLoc();
4999 SDValue N0 = Op.getOperand(0);
5000 SDValue N1 = Op.getOperand(1);
5001 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005002
Nate Begeman7973f352011-02-11 20:53:29 +00005003 if (VT == MVT::v8i8) {
5004 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5005 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005006
Nate Begeman7973f352011-02-11 20:53:29 +00005007 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5008 DAG.getIntPtrConstant(4));
5009 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005010 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005011 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5012 DAG.getIntPtrConstant(0));
5013 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5014 DAG.getIntPtrConstant(0));
5015
5016 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5017 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5018
5019 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5020 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005021
Nate Begeman7973f352011-02-11 20:53:29 +00005022 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5023 return N0;
5024 }
5025 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5026}
5027
5028static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5029 EVT VT = Op.getValueType();
5030 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5031 "unexpected type for custom-lowering ISD::UDIV");
5032
5033 DebugLoc dl = Op.getDebugLoc();
5034 SDValue N0 = Op.getOperand(0);
5035 SDValue N1 = Op.getOperand(1);
5036 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005037
Nate Begeman7973f352011-02-11 20:53:29 +00005038 if (VT == MVT::v8i8) {
5039 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5040 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005041
Nate Begeman7973f352011-02-11 20:53:29 +00005042 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5043 DAG.getIntPtrConstant(4));
5044 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005045 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005046 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5047 DAG.getIntPtrConstant(0));
5048 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5049 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005050
Nate Begeman7973f352011-02-11 20:53:29 +00005051 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5052 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005053
Nate Begeman7973f352011-02-11 20:53:29 +00005054 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5055 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005056
5057 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005058 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5059 N0);
5060 return N0;
5061 }
Owen Anderson76706012011-04-05 21:48:57 +00005062
Nate Begeman7973f352011-02-11 20:53:29 +00005063 // v4i16 sdiv ... Convert to float.
5064 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5065 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5066 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5067 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5068 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005069 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005070
5071 // Use reciprocal estimate and two refinement steps.
5072 // float4 recip = vrecpeq_f32(yf);
5073 // recip *= vrecpsq_f32(yf, recip);
5074 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005075 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005076 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005077 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005078 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005079 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005080 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005081 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005082 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005083 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005084 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5085 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5086 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5087 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005088 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005089 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5090 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5091 N1 = DAG.getConstant(2, MVT::i32);
5092 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5093 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5094 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5095 // Convert back to integer and return.
5096 // return vmovn_u32(vcvt_s32_f32(result));
5097 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5098 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5099 return N0;
5100}
5101
Evan Cheng342e3162011-08-30 01:34:54 +00005102static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5103 EVT VT = Op.getNode()->getValueType(0);
5104 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5105
5106 unsigned Opc;
5107 bool ExtraOp = false;
5108 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005109 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005110 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5111 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5112 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5113 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5114 }
5115
5116 if (!ExtraOp)
5117 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5118 Op.getOperand(1));
5119 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5120 Op.getOperand(1), Op.getOperand(2));
5121}
5122
Eli Friedman74bf18c2011-09-15 22:26:18 +00005123static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005124 // Monotonic load/store is legal for all targets
5125 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5126 return Op;
5127
5128 // Aquire/Release load/store is not legal for targets without a
5129 // dmb or equivalent available.
5130 return SDValue();
5131}
5132
5133
Eli Friedman2bdffe42011-08-31 00:31:29 +00005134static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005135ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5136 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005137 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005138 assert (Node->getValueType(0) == MVT::i64 &&
5139 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005140
Eli Friedman4d3f3292011-08-31 17:52:22 +00005141 SmallVector<SDValue, 6> Ops;
5142 Ops.push_back(Node->getOperand(0)); // Chain
5143 Ops.push_back(Node->getOperand(1)); // Ptr
5144 // Low part of Val1
5145 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5146 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5147 // High part of Val1
5148 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5149 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005150 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005151 // High part of Val1
5152 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5153 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5154 // High part of Val2
5155 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5156 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5157 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005158 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5159 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005160 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005161 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005162 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005163 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5164 Results.push_back(Result.getValue(2));
5165}
5166
Dan Gohmand858e902010-04-17 15:26:15 +00005167SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005168 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005169 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005170 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005171 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005172 case ISD::GlobalAddress:
5173 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5174 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005175 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005176 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005177 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5178 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005179 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005180 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005181 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005182 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005183 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005184 case ISD::SINT_TO_FP:
5185 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5186 case ISD::FP_TO_SINT:
5187 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005188 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005189 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005190 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005191 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005192 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005193 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005194 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5195 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005196 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005197 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005198 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005199 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005200 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005201 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005202 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005203 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005204 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005205 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005206 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005207 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005208 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005209 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005210 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005211 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005212 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005213 case ISD::SDIV: return LowerSDIV(Op, DAG);
5214 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005215 case ISD::ADDC:
5216 case ISD::ADDE:
5217 case ISD::SUBC:
5218 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005219 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005220 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005221 }
Evan Chenga8e29892007-01-19 07:51:42 +00005222}
5223
Duncan Sands1607f052008-12-01 11:39:25 +00005224/// ReplaceNodeResults - Replace the results of node with an illegal result
5225/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005226void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5227 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005228 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005229 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005230 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005231 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005232 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005233 case ISD::BITCAST:
5234 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005235 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005236 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005237 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005238 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005239 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005240 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005241 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005242 return;
5243 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005244 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005245 return;
5246 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005247 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005248 return;
5249 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005250 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005251 return;
5252 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005253 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005254 return;
5255 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005256 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005257 return;
5258 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005259 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005260 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005261 case ISD::ATOMIC_CMP_SWAP:
5262 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5263 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005264 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005265 if (Res.getNode())
5266 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005267}
Chris Lattner27a6c732007-11-24 07:07:01 +00005268
Evan Chenga8e29892007-01-19 07:51:42 +00005269//===----------------------------------------------------------------------===//
5270// ARM Scheduler Hooks
5271//===----------------------------------------------------------------------===//
5272
5273MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005274ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5275 MachineBasicBlock *BB,
5276 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005277 unsigned dest = MI->getOperand(0).getReg();
5278 unsigned ptr = MI->getOperand(1).getReg();
5279 unsigned oldval = MI->getOperand(2).getReg();
5280 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5282 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005283 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005284
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005285 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005286 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5287 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5288 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005289
5290 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005291 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5292 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5293 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005294 }
5295
Jim Grosbach5278eb82009-12-11 01:42:04 +00005296 unsigned ldrOpc, strOpc;
5297 switch (Size) {
5298 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005299 case 1:
5300 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005301 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005302 break;
5303 case 2:
5304 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5305 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5306 break;
5307 case 4:
5308 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5309 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5310 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005311 }
5312
5313 MachineFunction *MF = BB->getParent();
5314 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5315 MachineFunction::iterator It = BB;
5316 ++It; // insert the new blocks after the current block
5317
5318 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5319 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5320 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5321 MF->insert(It, loop1MBB);
5322 MF->insert(It, loop2MBB);
5323 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005324
5325 // Transfer the remainder of BB and its successor edges to exitMBB.
5326 exitMBB->splice(exitMBB->begin(), BB,
5327 llvm::next(MachineBasicBlock::iterator(MI)),
5328 BB->end());
5329 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005330
5331 // thisMBB:
5332 // ...
5333 // fallthrough --> loop1MBB
5334 BB->addSuccessor(loop1MBB);
5335
5336 // loop1MBB:
5337 // ldrex dest, [ptr]
5338 // cmp dest, oldval
5339 // bne exitMBB
5340 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005341 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5342 if (ldrOpc == ARM::t2LDREX)
5343 MIB.addImm(0);
5344 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005345 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005346 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005347 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5348 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005349 BB->addSuccessor(loop2MBB);
5350 BB->addSuccessor(exitMBB);
5351
5352 // loop2MBB:
5353 // strex scratch, newval, [ptr]
5354 // cmp scratch, #0
5355 // bne loop1MBB
5356 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005357 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5358 if (strOpc == ARM::t2STREX)
5359 MIB.addImm(0);
5360 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005361 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005362 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005363 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5364 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005365 BB->addSuccessor(loop1MBB);
5366 BB->addSuccessor(exitMBB);
5367
5368 // exitMBB:
5369 // ...
5370 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005371
Dan Gohman14152b42010-07-06 20:24:04 +00005372 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005373
Jim Grosbach5278eb82009-12-11 01:42:04 +00005374 return BB;
5375}
5376
5377MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005378ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5379 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005380 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5381 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5382
5383 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005384 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005385 MachineFunction::iterator It = BB;
5386 ++It;
5387
5388 unsigned dest = MI->getOperand(0).getReg();
5389 unsigned ptr = MI->getOperand(1).getReg();
5390 unsigned incr = MI->getOperand(2).getReg();
5391 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005392 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005393
5394 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5395 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005396 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5397 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005398 }
5399
Jim Grosbachc3c23542009-12-14 04:22:04 +00005400 unsigned ldrOpc, strOpc;
5401 switch (Size) {
5402 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005403 case 1:
5404 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005405 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005406 break;
5407 case 2:
5408 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5409 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5410 break;
5411 case 4:
5412 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5413 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5414 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005415 }
5416
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005417 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5418 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5419 MF->insert(It, loopMBB);
5420 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005421
5422 // Transfer the remainder of BB and its successor edges to exitMBB.
5423 exitMBB->splice(exitMBB->begin(), BB,
5424 llvm::next(MachineBasicBlock::iterator(MI)),
5425 BB->end());
5426 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005427
Craig Topper420761a2012-04-20 07:30:17 +00005428 const TargetRegisterClass *TRC = isThumb2 ?
5429 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5430 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005431 unsigned scratch = MRI.createVirtualRegister(TRC);
5432 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005433
5434 // thisMBB:
5435 // ...
5436 // fallthrough --> loopMBB
5437 BB->addSuccessor(loopMBB);
5438
5439 // loopMBB:
5440 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005441 // <binop> scratch2, dest, incr
5442 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005443 // cmp scratch, #0
5444 // bne- loopMBB
5445 // fallthrough --> exitMBB
5446 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005447 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5448 if (ldrOpc == ARM::t2LDREX)
5449 MIB.addImm(0);
5450 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005451 if (BinOpcode) {
5452 // operand order needs to go the other way for NAND
5453 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5454 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5455 addReg(incr).addReg(dest)).addReg(0);
5456 else
5457 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5458 addReg(dest).addReg(incr)).addReg(0);
5459 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005460
Jim Grosbachb6aed502011-09-09 18:37:27 +00005461 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5462 if (strOpc == ARM::t2STREX)
5463 MIB.addImm(0);
5464 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005465 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005466 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005467 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5468 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005469
5470 BB->addSuccessor(loopMBB);
5471 BB->addSuccessor(exitMBB);
5472
5473 // exitMBB:
5474 // ...
5475 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005476
Dan Gohman14152b42010-07-06 20:24:04 +00005477 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005478
Jim Grosbachc3c23542009-12-14 04:22:04 +00005479 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005480}
5481
Jim Grosbachf7da8822011-04-26 19:44:18 +00005482MachineBasicBlock *
5483ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5484 MachineBasicBlock *BB,
5485 unsigned Size,
5486 bool signExtend,
5487 ARMCC::CondCodes Cond) const {
5488 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5489
5490 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5491 MachineFunction *MF = BB->getParent();
5492 MachineFunction::iterator It = BB;
5493 ++It;
5494
5495 unsigned dest = MI->getOperand(0).getReg();
5496 unsigned ptr = MI->getOperand(1).getReg();
5497 unsigned incr = MI->getOperand(2).getReg();
5498 unsigned oldval = dest;
5499 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005500 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005501
5502 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5503 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005504 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5505 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005506 }
5507
Jim Grosbachf7da8822011-04-26 19:44:18 +00005508 unsigned ldrOpc, strOpc, extendOpc;
5509 switch (Size) {
5510 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5511 case 1:
5512 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5513 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005514 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005515 break;
5516 case 2:
5517 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5518 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005519 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005520 break;
5521 case 4:
5522 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5523 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5524 extendOpc = 0;
5525 break;
5526 }
5527
5528 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5529 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5530 MF->insert(It, loopMBB);
5531 MF->insert(It, exitMBB);
5532
5533 // Transfer the remainder of BB and its successor edges to exitMBB.
5534 exitMBB->splice(exitMBB->begin(), BB,
5535 llvm::next(MachineBasicBlock::iterator(MI)),
5536 BB->end());
5537 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5538
Craig Topper420761a2012-04-20 07:30:17 +00005539 const TargetRegisterClass *TRC = isThumb2 ?
5540 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5541 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005542 unsigned scratch = MRI.createVirtualRegister(TRC);
5543 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005544
5545 // thisMBB:
5546 // ...
5547 // fallthrough --> loopMBB
5548 BB->addSuccessor(loopMBB);
5549
5550 // loopMBB:
5551 // ldrex dest, ptr
5552 // (sign extend dest, if required)
5553 // cmp dest, incr
5554 // cmov.cond scratch2, dest, incr
5555 // strex scratch, scratch2, ptr
5556 // cmp scratch, #0
5557 // bne- loopMBB
5558 // fallthrough --> exitMBB
5559 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005560 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5561 if (ldrOpc == ARM::t2LDREX)
5562 MIB.addImm(0);
5563 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005564
5565 // Sign extend the value, if necessary.
5566 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005567 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005568 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5569 .addReg(dest)
5570 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005571 }
5572
5573 // Build compare and cmov instructions.
5574 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5575 .addReg(oldval).addReg(incr));
5576 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5577 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5578
Jim Grosbachb6aed502011-09-09 18:37:27 +00005579 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5580 if (strOpc == ARM::t2STREX)
5581 MIB.addImm(0);
5582 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005583 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5584 .addReg(scratch).addImm(0));
5585 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5586 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5587
5588 BB->addSuccessor(loopMBB);
5589 BB->addSuccessor(exitMBB);
5590
5591 // exitMBB:
5592 // ...
5593 BB = exitMBB;
5594
5595 MI->eraseFromParent(); // The instruction is gone now.
5596
5597 return BB;
5598}
5599
Eli Friedman2bdffe42011-08-31 00:31:29 +00005600MachineBasicBlock *
5601ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5602 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005603 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005604 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5606
5607 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5608 MachineFunction *MF = BB->getParent();
5609 MachineFunction::iterator It = BB;
5610 ++It;
5611
5612 unsigned destlo = MI->getOperand(0).getReg();
5613 unsigned desthi = MI->getOperand(1).getReg();
5614 unsigned ptr = MI->getOperand(2).getReg();
5615 unsigned vallo = MI->getOperand(3).getReg();
5616 unsigned valhi = MI->getOperand(4).getReg();
5617 DebugLoc dl = MI->getDebugLoc();
5618 bool isThumb2 = Subtarget->isThumb2();
5619
5620 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5621 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005622 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5623 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5624 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005625 }
5626
5627 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5628 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5629
5630 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005631 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005632 if (IsCmpxchg) {
5633 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5634 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5635 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005636 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5637 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005638 if (IsCmpxchg) {
5639 MF->insert(It, contBB);
5640 MF->insert(It, cont2BB);
5641 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005642 MF->insert(It, exitMBB);
5643
5644 // Transfer the remainder of BB and its successor edges to exitMBB.
5645 exitMBB->splice(exitMBB->begin(), BB,
5646 llvm::next(MachineBasicBlock::iterator(MI)),
5647 BB->end());
5648 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5649
Craig Topper420761a2012-04-20 07:30:17 +00005650 const TargetRegisterClass *TRC = isThumb2 ?
5651 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5652 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005653 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5654
5655 // thisMBB:
5656 // ...
5657 // fallthrough --> loopMBB
5658 BB->addSuccessor(loopMBB);
5659
5660 // loopMBB:
5661 // ldrexd r2, r3, ptr
5662 // <binopa> r0, r2, incr
5663 // <binopb> r1, r3, incr
5664 // strexd storesuccess, r0, r1, ptr
5665 // cmp storesuccess, #0
5666 // bne- loopMBB
5667 // fallthrough --> exitMBB
5668 //
5669 // Note that the registers are explicitly specified because there is not any
5670 // way to force the register allocator to allocate a register pair.
5671 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005672 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005673 // need to properly enforce the restriction that the two output registers
5674 // for ldrexd must be different.
5675 BB = loopMBB;
5676 // Load
5677 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5678 .addReg(ARM::R2, RegState::Define)
5679 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5680 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5681 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5682 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005683
5684 if (IsCmpxchg) {
5685 // Add early exit
5686 for (unsigned i = 0; i < 2; i++) {
5687 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5688 ARM::CMPrr))
5689 .addReg(i == 0 ? destlo : desthi)
5690 .addReg(i == 0 ? vallo : valhi));
5691 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5692 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5693 BB->addSuccessor(exitMBB);
5694 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5695 BB = (i == 0 ? contBB : cont2BB);
5696 }
5697
5698 // Copy to physregs for strexd
5699 unsigned setlo = MI->getOperand(5).getReg();
5700 unsigned sethi = MI->getOperand(6).getReg();
5701 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5702 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5703 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005704 // Perform binary operation
5705 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5706 .addReg(destlo).addReg(vallo))
5707 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5708 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5709 .addReg(desthi).addReg(valhi)).addReg(0);
5710 } else {
5711 // Copy to physregs for strexd
5712 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5713 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5714 }
5715
5716 // Store
5717 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5718 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5719 // Cmp+jump
5720 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5721 .addReg(storesuccess).addImm(0));
5722 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5723 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5724
5725 BB->addSuccessor(loopMBB);
5726 BB->addSuccessor(exitMBB);
5727
5728 // exitMBB:
5729 // ...
5730 BB = exitMBB;
5731
5732 MI->eraseFromParent(); // The instruction is gone now.
5733
5734 return BB;
5735}
5736
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005737/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5738/// registers the function context.
5739void ARMTargetLowering::
5740SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5741 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005742 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5743 DebugLoc dl = MI->getDebugLoc();
5744 MachineFunction *MF = MBB->getParent();
5745 MachineRegisterInfo *MRI = &MF->getRegInfo();
5746 MachineConstantPool *MCP = MF->getConstantPool();
5747 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5748 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005749
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005750 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005751 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005752
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005753 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005754 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005755 ARMConstantPoolValue *CPV =
5756 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5757 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5758
Craig Topper420761a2012-04-20 07:30:17 +00005759 const TargetRegisterClass *TRC = isThumb ?
5760 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5761 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005762
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005763 // Grab constant pool and fixed stack memory operands.
5764 MachineMemOperand *CPMMO =
5765 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5766 MachineMemOperand::MOLoad, 4, 4);
5767
5768 MachineMemOperand *FIMMOSt =
5769 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5770 MachineMemOperand::MOStore, 4, 4);
5771
5772 // Load the address of the dispatch MBB into the jump buffer.
5773 if (isThumb2) {
5774 // Incoming value: jbuf
5775 // ldr.n r5, LCPI1_1
5776 // orr r5, r5, #1
5777 // add r5, pc
5778 // str r5, [$jbuf, #+4] ; &jbuf[1]
5779 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5780 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5781 .addConstantPoolIndex(CPI)
5782 .addMemOperand(CPMMO));
5783 // Set the low bit because of thumb mode.
5784 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5785 AddDefaultCC(
5786 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5787 .addReg(NewVReg1, RegState::Kill)
5788 .addImm(0x01)));
5789 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5790 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5791 .addReg(NewVReg2, RegState::Kill)
5792 .addImm(PCLabelId);
5793 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5794 .addReg(NewVReg3, RegState::Kill)
5795 .addFrameIndex(FI)
5796 .addImm(36) // &jbuf[1] :: pc
5797 .addMemOperand(FIMMOSt));
5798 } else if (isThumb) {
5799 // Incoming value: jbuf
5800 // ldr.n r1, LCPI1_4
5801 // add r1, pc
5802 // mov r2, #1
5803 // orrs r1, r2
5804 // add r2, $jbuf, #+4 ; &jbuf[1]
5805 // str r1, [r2]
5806 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5807 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5808 .addConstantPoolIndex(CPI)
5809 .addMemOperand(CPMMO));
5810 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5811 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5812 .addReg(NewVReg1, RegState::Kill)
5813 .addImm(PCLabelId);
5814 // Set the low bit because of thumb mode.
5815 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5816 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5817 .addReg(ARM::CPSR, RegState::Define)
5818 .addImm(1));
5819 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5820 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5821 .addReg(ARM::CPSR, RegState::Define)
5822 .addReg(NewVReg2, RegState::Kill)
5823 .addReg(NewVReg3, RegState::Kill));
5824 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5825 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5826 .addFrameIndex(FI)
5827 .addImm(36)); // &jbuf[1] :: pc
5828 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5829 .addReg(NewVReg4, RegState::Kill)
5830 .addReg(NewVReg5, RegState::Kill)
5831 .addImm(0)
5832 .addMemOperand(FIMMOSt));
5833 } else {
5834 // Incoming value: jbuf
5835 // ldr r1, LCPI1_1
5836 // add r1, pc, r1
5837 // str r1, [$jbuf, #+4] ; &jbuf[1]
5838 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5839 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5840 .addConstantPoolIndex(CPI)
5841 .addImm(0)
5842 .addMemOperand(CPMMO));
5843 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5844 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5845 .addReg(NewVReg1, RegState::Kill)
5846 .addImm(PCLabelId));
5847 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5848 .addReg(NewVReg2, RegState::Kill)
5849 .addFrameIndex(FI)
5850 .addImm(36) // &jbuf[1] :: pc
5851 .addMemOperand(FIMMOSt));
5852 }
5853}
5854
5855MachineBasicBlock *ARMTargetLowering::
5856EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5857 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5858 DebugLoc dl = MI->getDebugLoc();
5859 MachineFunction *MF = MBB->getParent();
5860 MachineRegisterInfo *MRI = &MF->getRegInfo();
5861 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5862 MachineFrameInfo *MFI = MF->getFrameInfo();
5863 int FI = MFI->getFunctionContextIndex();
5864
Craig Topper420761a2012-04-20 07:30:17 +00005865 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5866 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005867 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005868
Bill Wendling04f15b42011-10-06 21:29:56 +00005869 // Get a mapping of the call site numbers to all of the landing pads they're
5870 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005871 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5872 unsigned MaxCSNum = 0;
5873 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005874 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5875 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005876 if (!BB->isLandingPad()) continue;
5877
5878 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5879 // pad.
5880 for (MachineBasicBlock::iterator
5881 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5882 if (!II->isEHLabel()) continue;
5883
5884 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005885 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005886
Bill Wendling5cbef192011-10-05 23:28:57 +00005887 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5888 for (SmallVectorImpl<unsigned>::iterator
5889 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5890 CSI != CSE; ++CSI) {
5891 CallSiteNumToLPad[*CSI].push_back(BB);
5892 MaxCSNum = std::max(MaxCSNum, *CSI);
5893 }
Bill Wendling2a850152011-10-05 00:02:33 +00005894 break;
5895 }
5896 }
5897
5898 // Get an ordered list of the machine basic blocks for the jump table.
5899 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005900 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005901 LPadList.reserve(CallSiteNumToLPad.size());
5902 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5903 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5904 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005905 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005906 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005907 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5908 }
Bill Wendling2a850152011-10-05 00:02:33 +00005909 }
5910
Bill Wendling5cbef192011-10-05 23:28:57 +00005911 assert(!LPadList.empty() &&
5912 "No landing pad destinations for the dispatch jump table!");
5913
Bill Wendling04f15b42011-10-06 21:29:56 +00005914 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005915 MachineJumpTableInfo *JTI =
5916 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5917 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5918 unsigned UId = AFI->createJumpTableUId();
5919
Bill Wendling04f15b42011-10-06 21:29:56 +00005920 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005921
5922 // Shove the dispatch's address into the return slot in the function context.
5923 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5924 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005925
Bill Wendlingbb734682011-10-05 00:39:32 +00005926 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005927 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005928 DispatchBB->addSuccessor(TrapBB);
5929
5930 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5931 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005932
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005933 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005934 MF->insert(MF->end(), DispatchBB);
5935 MF->insert(MF->end(), DispContBB);
5936 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005937
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005938 // Insert code into the entry block that creates and registers the function
5939 // context.
5940 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5941
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005942 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005943 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005944 MachineMemOperand::MOLoad |
5945 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005946
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005947 if (AFI->isThumb1OnlyFunction())
5948 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5949 else if (!Subtarget->hasVFP2())
5950 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005951 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005952 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005953
Bill Wendling952cb502011-10-18 22:49:07 +00005954 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005955 if (Subtarget->isThumb2()) {
5956 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5957 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5958 .addFrameIndex(FI)
5959 .addImm(4)
5960 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005961
Bill Wendling952cb502011-10-18 22:49:07 +00005962 if (NumLPads < 256) {
5963 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5964 .addReg(NewVReg1)
5965 .addImm(LPadList.size()));
5966 } else {
5967 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5968 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005969 .addImm(NumLPads & 0xFFFF));
5970
5971 unsigned VReg2 = VReg1;
5972 if ((NumLPads & 0xFFFF0000) != 0) {
5973 VReg2 = MRI->createVirtualRegister(TRC);
5974 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5975 .addReg(VReg1)
5976 .addImm(NumLPads >> 16));
5977 }
5978
Bill Wendling952cb502011-10-18 22:49:07 +00005979 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5980 .addReg(NewVReg1)
5981 .addReg(VReg2));
5982 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005983
Bill Wendling95ce2e92011-10-06 22:53:00 +00005984 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5985 .addMBB(TrapBB)
5986 .addImm(ARMCC::HI)
5987 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005988
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005989 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5990 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005991 .addJumpTableIndex(MJTI)
5992 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005993
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005994 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005995 AddDefaultCC(
5996 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005997 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5998 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005999 .addReg(NewVReg1)
6000 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6001
6002 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006003 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006004 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006005 .addJumpTableIndex(MJTI)
6006 .addImm(UId);
6007 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006008 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6009 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6010 .addFrameIndex(FI)
6011 .addImm(1)
6012 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006013
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006014 if (NumLPads < 256) {
6015 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6016 .addReg(NewVReg1)
6017 .addImm(NumLPads));
6018 } else {
6019 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006020 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6021 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6022
6023 // MachineConstantPool wants an explicit alignment.
6024 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6025 if (Align == 0)
6026 Align = getTargetData()->getTypeAllocSize(C->getType());
6027 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006028
6029 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6030 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6031 .addReg(VReg1, RegState::Define)
6032 .addConstantPoolIndex(Idx));
6033 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6034 .addReg(NewVReg1)
6035 .addReg(VReg1));
6036 }
6037
Bill Wendling083a8eb2011-10-06 23:37:36 +00006038 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6039 .addMBB(TrapBB)
6040 .addImm(ARMCC::HI)
6041 .addReg(ARM::CPSR);
6042
6043 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6044 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6045 .addReg(ARM::CPSR, RegState::Define)
6046 .addReg(NewVReg1)
6047 .addImm(2));
6048
6049 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006050 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006051 .addJumpTableIndex(MJTI)
6052 .addImm(UId));
6053
6054 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6055 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6056 .addReg(ARM::CPSR, RegState::Define)
6057 .addReg(NewVReg2, RegState::Kill)
6058 .addReg(NewVReg3));
6059
6060 MachineMemOperand *JTMMOLd =
6061 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6062 MachineMemOperand::MOLoad, 4, 4);
6063
6064 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6065 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6066 .addReg(NewVReg4, RegState::Kill)
6067 .addImm(0)
6068 .addMemOperand(JTMMOLd));
6069
6070 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6071 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6072 .addReg(ARM::CPSR, RegState::Define)
6073 .addReg(NewVReg5, RegState::Kill)
6074 .addReg(NewVReg3));
6075
6076 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6077 .addReg(NewVReg6, RegState::Kill)
6078 .addJumpTableIndex(MJTI)
6079 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006080 } else {
6081 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6082 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6083 .addFrameIndex(FI)
6084 .addImm(4)
6085 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006086
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006087 if (NumLPads < 256) {
6088 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6089 .addReg(NewVReg1)
6090 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006091 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006092 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6093 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006094 .addImm(NumLPads & 0xFFFF));
6095
6096 unsigned VReg2 = VReg1;
6097 if ((NumLPads & 0xFFFF0000) != 0) {
6098 VReg2 = MRI->createVirtualRegister(TRC);
6099 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6100 .addReg(VReg1)
6101 .addImm(NumLPads >> 16));
6102 }
6103
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006104 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6105 .addReg(NewVReg1)
6106 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006107 } else {
6108 MachineConstantPool *ConstantPool = MF->getConstantPool();
6109 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6110 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6111
6112 // MachineConstantPool wants an explicit alignment.
6113 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6114 if (Align == 0)
6115 Align = getTargetData()->getTypeAllocSize(C->getType());
6116 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6117
6118 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6119 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6120 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006121 .addConstantPoolIndex(Idx)
6122 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006123 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6124 .addReg(NewVReg1)
6125 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006126 }
6127
Bill Wendling95ce2e92011-10-06 22:53:00 +00006128 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6129 .addMBB(TrapBB)
6130 .addImm(ARMCC::HI)
6131 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006132
Bill Wendling564392b2011-10-18 22:11:18 +00006133 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006134 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006135 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006136 .addReg(NewVReg1)
6137 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006138 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6139 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006140 .addJumpTableIndex(MJTI)
6141 .addImm(UId));
6142
6143 MachineMemOperand *JTMMOLd =
6144 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6145 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006146 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006147 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006148 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6149 .addReg(NewVReg3, RegState::Kill)
6150 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006151 .addImm(0)
6152 .addMemOperand(JTMMOLd));
6153
6154 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006155 .addReg(NewVReg5, RegState::Kill)
6156 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006157 .addJumpTableIndex(MJTI)
6158 .addImm(UId);
6159 }
Bill Wendling2a850152011-10-05 00:02:33 +00006160
Bill Wendlingbb734682011-10-05 00:39:32 +00006161 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006162 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006163 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006164 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6165 MachineBasicBlock *CurMBB = *I;
6166 if (PrevMBB != CurMBB)
6167 DispContBB->addSuccessor(CurMBB);
6168 PrevMBB = CurMBB;
6169 }
6170
Bill Wendling24bb9252011-10-17 05:25:09 +00006171 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006172 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6173 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006174 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006175 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006176 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6177 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6178 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006179
6180 // Remove the landing pad successor from the invoke block and replace it
6181 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006182 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6183 BB->succ_end());
6184 while (!Successors.empty()) {
6185 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006186 if (SMBB->isLandingPad()) {
6187 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006188 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006189 }
6190 }
6191
6192 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006193
6194 // Find the invoke call and mark all of the callee-saved registers as
6195 // 'implicit defined' so that they're spilled. This prevents code from
6196 // moving instructions to before the EH block, where they will never be
6197 // executed.
6198 for (MachineBasicBlock::reverse_iterator
6199 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006200 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006201
6202 DenseMap<unsigned, bool> DefRegs;
6203 for (MachineInstr::mop_iterator
6204 OI = II->operands_begin(), OE = II->operands_end();
6205 OI != OE; ++OI) {
6206 if (!OI->isReg()) continue;
6207 DefRegs[OI->getReg()] = true;
6208 }
6209
6210 MachineInstrBuilder MIB(&*II);
6211
Bill Wendling5d798592011-10-14 23:55:44 +00006212 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006213 unsigned Reg = SavedRegs[i];
6214 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006215 !ARM::tGPRRegClass.contains(Reg) &&
6216 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006217 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006218 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006219 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006220 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006221 continue;
6222 if (!DefRegs[Reg])
6223 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006224 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006225
6226 break;
6227 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006228 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006229
Bill Wendlingf7b02072011-10-18 18:30:49 +00006230 // Mark all former landing pads as non-landing pads. The dispatch is the only
6231 // landing pad now.
6232 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6233 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6234 (*I)->setIsLandingPad(false);
6235
Bill Wendlingbb734682011-10-05 00:39:32 +00006236 // The instruction is gone now.
6237 MI->eraseFromParent();
6238
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006239 return MBB;
6240}
6241
Evan Cheng218977b2010-07-13 19:27:42 +00006242static
6243MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6244 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6245 E = MBB->succ_end(); I != E; ++I)
6246 if (*I != Succ)
6247 return *I;
6248 llvm_unreachable("Expecting a BB with two successors!");
6249}
6250
Manman Ren68f25572012-06-01 19:33:18 +00006251MachineBasicBlock *ARMTargetLowering::
6252EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6253 // This pseudo instruction has 3 operands: dst, src, size
6254 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6255 // Otherwise, we will generate unrolled scalar copies.
6256 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6257 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6258 MachineFunction::iterator It = BB;
6259 ++It;
6260
6261 unsigned dest = MI->getOperand(0).getReg();
6262 unsigned src = MI->getOperand(1).getReg();
6263 unsigned SizeVal = MI->getOperand(2).getImm();
6264 unsigned Align = MI->getOperand(3).getImm();
6265 DebugLoc dl = MI->getDebugLoc();
6266
6267 bool isThumb2 = Subtarget->isThumb2();
6268 MachineFunction *MF = BB->getParent();
6269 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006270 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006271
6272 const TargetRegisterClass *TRC = isThumb2 ?
6273 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6274 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006275 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006276
6277 if (Align & 1) {
6278 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6279 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6280 UnitSize = 1;
6281 } else if (Align & 2) {
6282 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6283 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6284 UnitSize = 2;
6285 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006286 // Check whether we can use NEON instructions.
6287 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6288 Subtarget->hasNEON()) {
6289 if ((Align % 16 == 0) && SizeVal >= 16) {
6290 ldrOpc = ARM::VLD1q32wb_fixed;
6291 strOpc = ARM::VST1q32wb_fixed;
6292 UnitSize = 16;
6293 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6294 }
6295 else if ((Align % 8 == 0) && SizeVal >= 8) {
6296 ldrOpc = ARM::VLD1d32wb_fixed;
6297 strOpc = ARM::VST1d32wb_fixed;
6298 UnitSize = 8;
6299 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6300 }
6301 }
6302 // Can't use NEON instructions.
6303 if (UnitSize == 0) {
6304 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6305 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6306 UnitSize = 4;
6307 }
Manman Ren68f25572012-06-01 19:33:18 +00006308 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006309
Manman Ren68f25572012-06-01 19:33:18 +00006310 unsigned BytesLeft = SizeVal % UnitSize;
6311 unsigned LoopSize = SizeVal - BytesLeft;
6312
6313 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6314 // Use LDR and STR to copy.
6315 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6316 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6317 unsigned srcIn = src;
6318 unsigned destIn = dest;
6319 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006320 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006321 unsigned srcOut = MRI.createVirtualRegister(TRC);
6322 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006323 if (UnitSize >= 8) {
6324 AddDefaultPred(BuildMI(*BB, MI, dl,
6325 TII->get(ldrOpc), scratch)
6326 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6327
6328 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6329 .addReg(destIn).addImm(0).addReg(scratch));
6330 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006331 AddDefaultPred(BuildMI(*BB, MI, dl,
6332 TII->get(ldrOpc), scratch)
6333 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6334
6335 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6336 .addReg(scratch).addReg(destIn)
6337 .addImm(UnitSize));
6338 } else {
6339 AddDefaultPred(BuildMI(*BB, MI, dl,
6340 TII->get(ldrOpc), scratch)
6341 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6342 .addImm(UnitSize));
6343
6344 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6345 .addReg(scratch).addReg(destIn)
6346 .addReg(0).addImm(UnitSize));
6347 }
6348 srcIn = srcOut;
6349 destIn = destOut;
6350 }
6351
6352 // Handle the leftover bytes with LDRB and STRB.
6353 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6354 // [destOut] = STRB_POST(scratch, destIn, 1)
6355 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6356 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6357 for (unsigned i = 0; i < BytesLeft; i++) {
6358 unsigned scratch = MRI.createVirtualRegister(TRC);
6359 unsigned srcOut = MRI.createVirtualRegister(TRC);
6360 unsigned destOut = MRI.createVirtualRegister(TRC);
6361 if (isThumb2) {
6362 AddDefaultPred(BuildMI(*BB, MI, dl,
6363 TII->get(ldrOpc),scratch)
6364 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6365
6366 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6367 .addReg(scratch).addReg(destIn)
6368 .addReg(0).addImm(1));
6369 } else {
6370 AddDefaultPred(BuildMI(*BB, MI, dl,
6371 TII->get(ldrOpc),scratch)
6372 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6373
6374 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6375 .addReg(scratch).addReg(destIn)
6376 .addReg(0).addImm(1));
6377 }
6378 srcIn = srcOut;
6379 destIn = destOut;
6380 }
6381 MI->eraseFromParent(); // The instruction is gone now.
6382 return BB;
6383 }
6384
6385 // Expand the pseudo op to a loop.
6386 // thisMBB:
6387 // ...
6388 // movw varEnd, # --> with thumb2
6389 // movt varEnd, #
6390 // ldrcp varEnd, idx --> without thumb2
6391 // fallthrough --> loopMBB
6392 // loopMBB:
6393 // PHI varPhi, varEnd, varLoop
6394 // PHI srcPhi, src, srcLoop
6395 // PHI destPhi, dst, destLoop
6396 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6397 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6398 // subs varLoop, varPhi, #UnitSize
6399 // bne loopMBB
6400 // fallthrough --> exitMBB
6401 // exitMBB:
6402 // epilogue to handle left-over bytes
6403 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6404 // [destOut] = STRB_POST(scratch, destLoop, 1)
6405 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6406 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6407 MF->insert(It, loopMBB);
6408 MF->insert(It, exitMBB);
6409
6410 // Transfer the remainder of BB and its successor edges to exitMBB.
6411 exitMBB->splice(exitMBB->begin(), BB,
6412 llvm::next(MachineBasicBlock::iterator(MI)),
6413 BB->end());
6414 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6415
6416 // Load an immediate to varEnd.
6417 unsigned varEnd = MRI.createVirtualRegister(TRC);
6418 if (isThumb2) {
6419 unsigned VReg1 = varEnd;
6420 if ((LoopSize & 0xFFFF0000) != 0)
6421 VReg1 = MRI.createVirtualRegister(TRC);
6422 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6423 .addImm(LoopSize & 0xFFFF));
6424
6425 if ((LoopSize & 0xFFFF0000) != 0)
6426 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6427 .addReg(VReg1)
6428 .addImm(LoopSize >> 16));
6429 } else {
6430 MachineConstantPool *ConstantPool = MF->getConstantPool();
6431 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6432 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6433
6434 // MachineConstantPool wants an explicit alignment.
6435 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6436 if (Align == 0)
6437 Align = getTargetData()->getTypeAllocSize(C->getType());
6438 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6439
6440 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6441 .addReg(varEnd, RegState::Define)
6442 .addConstantPoolIndex(Idx)
6443 .addImm(0));
6444 }
6445 BB->addSuccessor(loopMBB);
6446
6447 // Generate the loop body:
6448 // varPhi = PHI(varLoop, varEnd)
6449 // srcPhi = PHI(srcLoop, src)
6450 // destPhi = PHI(destLoop, dst)
6451 MachineBasicBlock *entryBB = BB;
6452 BB = loopMBB;
6453 unsigned varLoop = MRI.createVirtualRegister(TRC);
6454 unsigned varPhi = MRI.createVirtualRegister(TRC);
6455 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6456 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6457 unsigned destLoop = MRI.createVirtualRegister(TRC);
6458 unsigned destPhi = MRI.createVirtualRegister(TRC);
6459
6460 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6461 .addReg(varLoop).addMBB(loopMBB)
6462 .addReg(varEnd).addMBB(entryBB);
6463 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6464 .addReg(srcLoop).addMBB(loopMBB)
6465 .addReg(src).addMBB(entryBB);
6466 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6467 .addReg(destLoop).addMBB(loopMBB)
6468 .addReg(dest).addMBB(entryBB);
6469
6470 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6471 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006472 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6473 if (UnitSize >= 8) {
6474 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6475 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6476
6477 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6478 .addReg(destPhi).addImm(0).addReg(scratch));
6479 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006480 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6481 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6482
6483 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6484 .addReg(scratch).addReg(destPhi)
6485 .addImm(UnitSize));
6486 } else {
6487 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6488 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6489 .addImm(UnitSize));
6490
6491 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6492 .addReg(scratch).addReg(destPhi)
6493 .addReg(0).addImm(UnitSize));
6494 }
6495
6496 // Decrement loop variable by UnitSize.
6497 MachineInstrBuilder MIB = BuildMI(BB, dl,
6498 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6499 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6500 MIB->getOperand(5).setReg(ARM::CPSR);
6501 MIB->getOperand(5).setIsDef(true);
6502
6503 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6504 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6505
6506 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6507 BB->addSuccessor(loopMBB);
6508 BB->addSuccessor(exitMBB);
6509
6510 // Add epilogue to handle BytesLeft.
6511 BB = exitMBB;
6512 MachineInstr *StartOfExit = exitMBB->begin();
6513 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6514 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6515
6516 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6517 // [destOut] = STRB_POST(scratch, destLoop, 1)
6518 unsigned srcIn = srcLoop;
6519 unsigned destIn = destLoop;
6520 for (unsigned i = 0; i < BytesLeft; i++) {
6521 unsigned scratch = MRI.createVirtualRegister(TRC);
6522 unsigned srcOut = MRI.createVirtualRegister(TRC);
6523 unsigned destOut = MRI.createVirtualRegister(TRC);
6524 if (isThumb2) {
6525 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6526 TII->get(ldrOpc),scratch)
6527 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6528
6529 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6530 .addReg(scratch).addReg(destIn)
6531 .addImm(1));
6532 } else {
6533 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6534 TII->get(ldrOpc),scratch)
6535 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6536
6537 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6538 .addReg(scratch).addReg(destIn)
6539 .addReg(0).addImm(1));
6540 }
6541 srcIn = srcOut;
6542 destIn = destOut;
6543 }
6544
6545 MI->eraseFromParent(); // The instruction is gone now.
6546 return BB;
6547}
6548
Jim Grosbache801dc42009-12-12 01:40:06 +00006549MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006550ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006551 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006552 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006553 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006554 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006555 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006556 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006557 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006558 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006559 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006560 // The Thumb2 pre-indexed stores have the same MI operands, they just
6561 // define them differently in the .td files from the isel patterns, so
6562 // they need pseudos.
6563 case ARM::t2STR_preidx:
6564 MI->setDesc(TII->get(ARM::t2STR_PRE));
6565 return BB;
6566 case ARM::t2STRB_preidx:
6567 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6568 return BB;
6569 case ARM::t2STRH_preidx:
6570 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6571 return BB;
6572
Jim Grosbach19dec202011-08-05 20:35:44 +00006573 case ARM::STRi_preidx:
6574 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006575 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006576 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6577 // Decode the offset.
6578 unsigned Offset = MI->getOperand(4).getImm();
6579 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6580 Offset = ARM_AM::getAM2Offset(Offset);
6581 if (isSub)
6582 Offset = -Offset;
6583
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006584 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006585 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006586 .addOperand(MI->getOperand(0)) // Rn_wb
6587 .addOperand(MI->getOperand(1)) // Rt
6588 .addOperand(MI->getOperand(2)) // Rn
6589 .addImm(Offset) // offset (skip GPR==zero_reg)
6590 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006591 .addOperand(MI->getOperand(6))
6592 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006593 MI->eraseFromParent();
6594 return BB;
6595 }
6596 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006597 case ARM::STRBr_preidx:
6598 case ARM::STRH_preidx: {
6599 unsigned NewOpc;
6600 switch (MI->getOpcode()) {
6601 default: llvm_unreachable("unexpected opcode!");
6602 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6603 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6604 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6605 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006606 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6607 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6608 MIB.addOperand(MI->getOperand(i));
6609 MI->eraseFromParent();
6610 return BB;
6611 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006612 case ARM::ATOMIC_LOAD_ADD_I8:
6613 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6614 case ARM::ATOMIC_LOAD_ADD_I16:
6615 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6616 case ARM::ATOMIC_LOAD_ADD_I32:
6617 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006618
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006619 case ARM::ATOMIC_LOAD_AND_I8:
6620 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6621 case ARM::ATOMIC_LOAD_AND_I16:
6622 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6623 case ARM::ATOMIC_LOAD_AND_I32:
6624 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006625
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006626 case ARM::ATOMIC_LOAD_OR_I8:
6627 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6628 case ARM::ATOMIC_LOAD_OR_I16:
6629 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6630 case ARM::ATOMIC_LOAD_OR_I32:
6631 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006632
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006633 case ARM::ATOMIC_LOAD_XOR_I8:
6634 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6635 case ARM::ATOMIC_LOAD_XOR_I16:
6636 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6637 case ARM::ATOMIC_LOAD_XOR_I32:
6638 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006639
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006640 case ARM::ATOMIC_LOAD_NAND_I8:
6641 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6642 case ARM::ATOMIC_LOAD_NAND_I16:
6643 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6644 case ARM::ATOMIC_LOAD_NAND_I32:
6645 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006646
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006647 case ARM::ATOMIC_LOAD_SUB_I8:
6648 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6649 case ARM::ATOMIC_LOAD_SUB_I16:
6650 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6651 case ARM::ATOMIC_LOAD_SUB_I32:
6652 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006653
Jim Grosbachf7da8822011-04-26 19:44:18 +00006654 case ARM::ATOMIC_LOAD_MIN_I8:
6655 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6656 case ARM::ATOMIC_LOAD_MIN_I16:
6657 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6658 case ARM::ATOMIC_LOAD_MIN_I32:
6659 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6660
6661 case ARM::ATOMIC_LOAD_MAX_I8:
6662 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6663 case ARM::ATOMIC_LOAD_MAX_I16:
6664 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6665 case ARM::ATOMIC_LOAD_MAX_I32:
6666 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6667
6668 case ARM::ATOMIC_LOAD_UMIN_I8:
6669 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6670 case ARM::ATOMIC_LOAD_UMIN_I16:
6671 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6672 case ARM::ATOMIC_LOAD_UMIN_I32:
6673 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6674
6675 case ARM::ATOMIC_LOAD_UMAX_I8:
6676 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6677 case ARM::ATOMIC_LOAD_UMAX_I16:
6678 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6679 case ARM::ATOMIC_LOAD_UMAX_I32:
6680 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6681
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006682 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6683 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6684 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006685
6686 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6687 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6688 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006689
Eli Friedman2bdffe42011-08-31 00:31:29 +00006690
6691 case ARM::ATOMADD6432:
6692 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006693 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6694 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006695 case ARM::ATOMSUB6432:
6696 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006697 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6698 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006699 case ARM::ATOMOR6432:
6700 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006701 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006702 case ARM::ATOMXOR6432:
6703 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006704 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006705 case ARM::ATOMAND6432:
6706 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006707 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006708 case ARM::ATOMSWAP6432:
6709 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006710 case ARM::ATOMCMPXCHG6432:
6711 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6712 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6713 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006714
Evan Cheng007ea272009-08-12 05:17:19 +00006715 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006716 // To "insert" a SELECT_CC instruction, we actually have to insert the
6717 // diamond control-flow pattern. The incoming instruction knows the
6718 // destination vreg to set, the condition code register to branch on, the
6719 // true/false values to select between, and a branch opcode to use.
6720 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006721 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006722 ++It;
6723
6724 // thisMBB:
6725 // ...
6726 // TrueVal = ...
6727 // cmpTY ccX, r1, r2
6728 // bCC copy1MBB
6729 // fallthrough --> copy0MBB
6730 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006731 MachineFunction *F = BB->getParent();
6732 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6733 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006734 F->insert(It, copy0MBB);
6735 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006736
6737 // Transfer the remainder of BB and its successor edges to sinkMBB.
6738 sinkMBB->splice(sinkMBB->begin(), BB,
6739 llvm::next(MachineBasicBlock::iterator(MI)),
6740 BB->end());
6741 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6742
Dan Gohman258c58c2010-07-06 15:49:48 +00006743 BB->addSuccessor(copy0MBB);
6744 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006745
Dan Gohman14152b42010-07-06 20:24:04 +00006746 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6747 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6748
Evan Chenga8e29892007-01-19 07:51:42 +00006749 // copy0MBB:
6750 // %FalseValue = ...
6751 // # fallthrough to sinkMBB
6752 BB = copy0MBB;
6753
6754 // Update machine-CFG edges
6755 BB->addSuccessor(sinkMBB);
6756
6757 // sinkMBB:
6758 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6759 // ...
6760 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006761 BuildMI(*BB, BB->begin(), dl,
6762 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006763 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6764 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6765
Dan Gohman14152b42010-07-06 20:24:04 +00006766 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006767 return BB;
6768 }
Evan Cheng86198642009-08-07 00:34:42 +00006769
Evan Cheng218977b2010-07-13 19:27:42 +00006770 case ARM::BCCi64:
6771 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006772 // If there is an unconditional branch to the other successor, remove it.
6773 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006774
Evan Cheng218977b2010-07-13 19:27:42 +00006775 // Compare both parts that make up the double comparison separately for
6776 // equality.
6777 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6778
6779 unsigned LHS1 = MI->getOperand(1).getReg();
6780 unsigned LHS2 = MI->getOperand(2).getReg();
6781 if (RHSisZero) {
6782 AddDefaultPred(BuildMI(BB, dl,
6783 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6784 .addReg(LHS1).addImm(0));
6785 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6786 .addReg(LHS2).addImm(0)
6787 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6788 } else {
6789 unsigned RHS1 = MI->getOperand(3).getReg();
6790 unsigned RHS2 = MI->getOperand(4).getReg();
6791 AddDefaultPred(BuildMI(BB, dl,
6792 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6793 .addReg(LHS1).addReg(RHS1));
6794 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6795 .addReg(LHS2).addReg(RHS2)
6796 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6797 }
6798
6799 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6800 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6801 if (MI->getOperand(0).getImm() == ARMCC::NE)
6802 std::swap(destMBB, exitMBB);
6803
6804 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6805 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006806 if (isThumb2)
6807 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6808 else
6809 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006810
6811 MI->eraseFromParent(); // The pseudo instruction is gone now.
6812 return BB;
6813 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006814
Bill Wendling5bc85282011-10-17 20:37:20 +00006815 case ARM::Int_eh_sjlj_setjmp:
6816 case ARM::Int_eh_sjlj_setjmp_nofp:
6817 case ARM::tInt_eh_sjlj_setjmp:
6818 case ARM::t2Int_eh_sjlj_setjmp:
6819 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6820 EmitSjLjDispatchBlock(MI, BB);
6821 return BB;
6822
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006823 case ARM::ABS:
6824 case ARM::t2ABS: {
6825 // To insert an ABS instruction, we have to insert the
6826 // diamond control-flow pattern. The incoming instruction knows the
6827 // source vreg to test against 0, the destination vreg to set,
6828 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006829 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006830 // It transforms
6831 // V1 = ABS V0
6832 // into
6833 // V2 = MOVS V0
6834 // BCC (branch to SinkBB if V0 >= 0)
6835 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006836 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006837 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6838 MachineFunction::iterator BBI = BB;
6839 ++BBI;
6840 MachineFunction *Fn = BB->getParent();
6841 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6842 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6843 Fn->insert(BBI, RSBBB);
6844 Fn->insert(BBI, SinkBB);
6845
6846 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6847 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6848 bool isThumb2 = Subtarget->isThumb2();
6849 MachineRegisterInfo &MRI = Fn->getRegInfo();
6850 // In Thumb mode S must not be specified if source register is the SP or
6851 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006852 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6853 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6854 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006855
6856 // Transfer the remainder of BB and its successor edges to sinkMBB.
6857 SinkBB->splice(SinkBB->begin(), BB,
6858 llvm::next(MachineBasicBlock::iterator(MI)),
6859 BB->end());
6860 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6861
6862 BB->addSuccessor(RSBBB);
6863 BB->addSuccessor(SinkBB);
6864
6865 // fall through to SinkMBB
6866 RSBBB->addSuccessor(SinkBB);
6867
Manman Ren307473d2012-06-15 21:32:12 +00006868 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006869 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006870 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6871 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006872
6873 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006874 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006875 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6876 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6877
6878 // insert rsbri in RSBBB
6879 // Note: BCC and rsbri will be converted into predicated rsbmi
6880 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006881 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006882 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006883 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006884 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6885
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006886 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006887 // reuse ABSDstReg to not change uses of ABS instruction
6888 BuildMI(*SinkBB, SinkBB->begin(), dl,
6889 TII->get(ARM::PHI), ABSDstReg)
6890 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006891 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006892
6893 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006894 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006895
6896 // return last added BB
6897 return SinkBB;
6898 }
Manman Ren68f25572012-06-01 19:33:18 +00006899 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006900 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006901 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006902 }
6903}
6904
Evan Cheng37fefc22011-08-30 19:09:48 +00006905void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6906 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006907 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006908 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6909 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6910 return;
6911 }
6912
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006913 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006914 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6915 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6916 // operand is still set to noreg. If needed, set the optional operand's
6917 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006918 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006919 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006920
Andrew Trick3be654f2011-09-21 02:20:46 +00006921 // Rename pseudo opcodes.
6922 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6923 if (NewOpc) {
6924 const ARMBaseInstrInfo *TII =
6925 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006926 MCID = &TII->get(NewOpc);
6927
6928 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6929 "converted opcode should be the same except for cc_out");
6930
6931 MI->setDesc(*MCID);
6932
6933 // Add the optional cc_out operand
6934 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006935 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006936 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006937
6938 // Any ARM instruction that sets the 's' bit should specify an optional
6939 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006940 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006941 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006942 return;
6943 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006944 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6945 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006946 bool definesCPSR = false;
6947 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006948 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006949 i != e; ++i) {
6950 const MachineOperand &MO = MI->getOperand(i);
6951 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6952 definesCPSR = true;
6953 if (MO.isDead())
6954 deadCPSR = true;
6955 MI->RemoveOperand(i);
6956 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006957 }
6958 }
Andrew Trick4815d562011-09-20 03:17:40 +00006959 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006960 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006961 return;
6962 }
6963 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006964 if (deadCPSR) {
6965 assert(!MI->getOperand(ccOutIdx).getReg() &&
6966 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006967 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006968 }
Andrew Trick4815d562011-09-20 03:17:40 +00006969
Andrew Trick3be654f2011-09-21 02:20:46 +00006970 // If this instruction was defined with an optional CPSR def and its dag node
6971 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006972 MachineOperand &MO = MI->getOperand(ccOutIdx);
6973 MO.setReg(ARM::CPSR);
6974 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006975}
6976
Evan Chenga8e29892007-01-19 07:51:42 +00006977//===----------------------------------------------------------------------===//
6978// ARM Optimization Hooks
6979//===----------------------------------------------------------------------===//
6980
Chris Lattnerd1980a52009-03-12 06:52:53 +00006981static
6982SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6983 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006984 SelectionDAG &DAG = DCI.DAG;
6985 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006986 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006987 unsigned Opc = N->getOpcode();
6988 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6989 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6990 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6991 ISD::CondCode CC = ISD::SETCC_INVALID;
6992
6993 if (isSlctCC) {
6994 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6995 } else {
6996 SDValue CCOp = Slct.getOperand(0);
6997 if (CCOp.getOpcode() == ISD::SETCC)
6998 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6999 }
7000
7001 bool DoXform = false;
7002 bool InvCC = false;
7003 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
7004 "Bad input!");
7005
7006 if (LHS.getOpcode() == ISD::Constant &&
7007 cast<ConstantSDNode>(LHS)->isNullValue()) {
7008 DoXform = true;
7009 } else if (CC != ISD::SETCC_INVALID &&
7010 RHS.getOpcode() == ISD::Constant &&
7011 cast<ConstantSDNode>(RHS)->isNullValue()) {
7012 std::swap(LHS, RHS);
7013 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007014 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00007015 Op0.getOperand(0).getValueType();
7016 bool isInt = OpVT.isInteger();
7017 CC = ISD::getSetCCInverse(CC, isInt);
7018
7019 if (!TLI.isCondCodeLegal(CC, OpVT))
7020 return SDValue(); // Inverse operator isn't legal.
7021
7022 DoXform = true;
7023 InvCC = true;
7024 }
7025
7026 if (DoXform) {
7027 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
7028 if (isSlctCC)
7029 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
7030 Slct.getOperand(0), Slct.getOperand(1), CC);
7031 SDValue CCOp = Slct.getOperand(0);
7032 if (InvCC)
7033 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
7034 CCOp.getOperand(0), CCOp.getOperand(1), CC);
7035 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
7036 CCOp, OtherOp, Result);
7037 }
7038 return SDValue();
7039}
7040
Eric Christopherfa6f5912011-06-29 21:10:36 +00007041// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007042// (only after legalization).
7043static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7044 TargetLowering::DAGCombinerInfo &DCI,
7045 const ARMSubtarget *Subtarget) {
7046
7047 // Only perform optimization if after legalize, and if NEON is available. We
7048 // also expected both operands to be BUILD_VECTORs.
7049 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7050 || N0.getOpcode() != ISD::BUILD_VECTOR
7051 || N1.getOpcode() != ISD::BUILD_VECTOR)
7052 return SDValue();
7053
7054 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7055 EVT VT = N->getValueType(0);
7056 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7057 return SDValue();
7058
7059 // Check that the vector operands are of the right form.
7060 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7061 // operands, where N is the size of the formed vector.
7062 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7063 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007064
7065 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007066 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007067 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007068 SDValue Vec = N0->getOperand(0)->getOperand(0);
7069 SDNode *V = Vec.getNode();
7070 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007071
Eric Christopherfa6f5912011-06-29 21:10:36 +00007072 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007073 // check to see if each of their operands are an EXTRACT_VECTOR with
7074 // the same vector and appropriate index.
7075 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7076 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7077 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007078
Tanya Lattner189531f2011-06-14 23:48:48 +00007079 SDValue ExtVec0 = N0->getOperand(i);
7080 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007081
Tanya Lattner189531f2011-06-14 23:48:48 +00007082 // First operand is the vector, verify its the same.
7083 if (V != ExtVec0->getOperand(0).getNode() ||
7084 V != ExtVec1->getOperand(0).getNode())
7085 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007086
Tanya Lattner189531f2011-06-14 23:48:48 +00007087 // Second is the constant, verify its correct.
7088 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7089 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007090
Tanya Lattner189531f2011-06-14 23:48:48 +00007091 // For the constant, we want to see all the even or all the odd.
7092 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7093 || C1->getZExtValue() != nextIndex+1)
7094 return SDValue();
7095
7096 // Increment index.
7097 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007098 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007099 return SDValue();
7100 }
7101
7102 // Create VPADDL node.
7103 SelectionDAG &DAG = DCI.DAG;
7104 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007105
7106 // Build operand list.
7107 SmallVector<SDValue, 8> Ops;
7108 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7109 TLI.getPointerTy()));
7110
7111 // Input is the vector.
7112 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007113
Tanya Lattner189531f2011-06-14 23:48:48 +00007114 // Get widened type and narrowed type.
7115 MVT widenType;
7116 unsigned numElem = VT.getVectorNumElements();
7117 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7118 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7119 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7120 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7121 default:
Craig Topperbc219812012-02-07 02:50:20 +00007122 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007123 }
7124
7125 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7126 widenType, &Ops[0], Ops.size());
7127 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7128}
7129
Bob Wilson3d5792a2010-07-29 20:34:14 +00007130/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7131/// operands N0 and N1. This is a helper for PerformADDCombine that is
7132/// called with the default operands, and if that fails, with commuted
7133/// operands.
7134static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007135 TargetLowering::DAGCombinerInfo &DCI,
7136 const ARMSubtarget *Subtarget){
7137
7138 // Attempt to create vpaddl for this add.
7139 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7140 if (Result.getNode())
7141 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007142
Chris Lattnerd1980a52009-03-12 06:52:53 +00007143 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7144 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7145 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7146 if (Result.getNode()) return Result;
7147 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007148 return SDValue();
7149}
7150
Bob Wilson3d5792a2010-07-29 20:34:14 +00007151/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7152///
7153static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007154 TargetLowering::DAGCombinerInfo &DCI,
7155 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007156 SDValue N0 = N->getOperand(0);
7157 SDValue N1 = N->getOperand(1);
7158
7159 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007160 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007161 if (Result.getNode())
7162 return Result;
7163
7164 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007165 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007166}
7167
Chris Lattnerd1980a52009-03-12 06:52:53 +00007168/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007169///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007170static SDValue PerformSUBCombine(SDNode *N,
7171 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007172 SDValue N0 = N->getOperand(0);
7173 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007174
Chris Lattnerd1980a52009-03-12 06:52:53 +00007175 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7176 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7177 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7178 if (Result.getNode()) return Result;
7179 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007180
Chris Lattnerd1980a52009-03-12 06:52:53 +00007181 return SDValue();
7182}
7183
Evan Cheng463d3582011-03-31 19:38:48 +00007184/// PerformVMULCombine
7185/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7186/// special multiplier accumulator forwarding.
7187/// vmul d3, d0, d2
7188/// vmla d3, d1, d2
7189/// is faster than
7190/// vadd d3, d0, d1
7191/// vmul d3, d3, d2
7192static SDValue PerformVMULCombine(SDNode *N,
7193 TargetLowering::DAGCombinerInfo &DCI,
7194 const ARMSubtarget *Subtarget) {
7195 if (!Subtarget->hasVMLxForwarding())
7196 return SDValue();
7197
7198 SelectionDAG &DAG = DCI.DAG;
7199 SDValue N0 = N->getOperand(0);
7200 SDValue N1 = N->getOperand(1);
7201 unsigned Opcode = N0.getOpcode();
7202 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7203 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007204 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007205 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7206 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7207 return SDValue();
7208 std::swap(N0, N1);
7209 }
7210
7211 EVT VT = N->getValueType(0);
7212 DebugLoc DL = N->getDebugLoc();
7213 SDValue N00 = N0->getOperand(0);
7214 SDValue N01 = N0->getOperand(1);
7215 return DAG.getNode(Opcode, DL, VT,
7216 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7217 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7218}
7219
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007220static SDValue PerformMULCombine(SDNode *N,
7221 TargetLowering::DAGCombinerInfo &DCI,
7222 const ARMSubtarget *Subtarget) {
7223 SelectionDAG &DAG = DCI.DAG;
7224
7225 if (Subtarget->isThumb1Only())
7226 return SDValue();
7227
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007228 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7229 return SDValue();
7230
7231 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007232 if (VT.is64BitVector() || VT.is128BitVector())
7233 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007234 if (VT != MVT::i32)
7235 return SDValue();
7236
7237 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7238 if (!C)
7239 return SDValue();
7240
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007241 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007242 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007243
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007244 ShiftAmt = ShiftAmt & (32 - 1);
7245 SDValue V = N->getOperand(0);
7246 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007247
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007248 SDValue Res;
7249 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007250
7251 if (MulAmt >= 0) {
7252 if (isPowerOf2_32(MulAmt - 1)) {
7253 // (mul x, 2^N + 1) => (add (shl x, N), x)
7254 Res = DAG.getNode(ISD::ADD, DL, VT,
7255 V,
7256 DAG.getNode(ISD::SHL, DL, VT,
7257 V,
7258 DAG.getConstant(Log2_32(MulAmt - 1),
7259 MVT::i32)));
7260 } else if (isPowerOf2_32(MulAmt + 1)) {
7261 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7262 Res = DAG.getNode(ISD::SUB, DL, VT,
7263 DAG.getNode(ISD::SHL, DL, VT,
7264 V,
7265 DAG.getConstant(Log2_32(MulAmt + 1),
7266 MVT::i32)),
7267 V);
7268 } else
7269 return SDValue();
7270 } else {
7271 uint64_t MulAmtAbs = -MulAmt;
7272 if (isPowerOf2_32(MulAmtAbs + 1)) {
7273 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7274 Res = DAG.getNode(ISD::SUB, DL, VT,
7275 V,
7276 DAG.getNode(ISD::SHL, DL, VT,
7277 V,
7278 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7279 MVT::i32)));
7280 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7281 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7282 Res = DAG.getNode(ISD::ADD, DL, VT,
7283 V,
7284 DAG.getNode(ISD::SHL, DL, VT,
7285 V,
7286 DAG.getConstant(Log2_32(MulAmtAbs-1),
7287 MVT::i32)));
7288 Res = DAG.getNode(ISD::SUB, DL, VT,
7289 DAG.getConstant(0, MVT::i32),Res);
7290
7291 } else
7292 return SDValue();
7293 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007294
7295 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007296 Res = DAG.getNode(ISD::SHL, DL, VT,
7297 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007298
7299 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007300 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007301 return SDValue();
7302}
7303
Evan Chengc892aeb2012-02-23 01:19:06 +00007304static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7305 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7306 return false;
7307
7308 SDValue FalseVal = N.getOperand(0);
7309 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7310 if (!C)
7311 return false;
7312 if (AllOnes)
7313 return C->isAllOnesValue();
7314 return C->isNullValue();
7315}
7316
7317/// formConditionalOp - Combine an operation with a conditional move operand
7318/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7319/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7320static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7321 bool Commutable) {
7322 SDValue N0 = N->getOperand(0);
7323 SDValue N1 = N->getOperand(1);
7324
7325 bool isAND = N->getOpcode() == ISD::AND;
7326 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7327 if (!isCand && Commutable) {
7328 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7329 if (isCand)
7330 std::swap(N0, N1);
7331 }
7332 if (!isCand)
7333 return SDValue();
7334
7335 unsigned Opc = 0;
7336 switch (N->getOpcode()) {
7337 default: llvm_unreachable("Unexpected node");
7338 case ISD::AND: Opc = ARMISD::CAND; break;
7339 case ISD::OR: Opc = ARMISD::COR; break;
7340 case ISD::XOR: Opc = ARMISD::CXOR; break;
7341 }
7342 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7343 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7344 N1.getOperand(4));
7345}
7346
Owen Anderson080c0922010-11-05 19:27:46 +00007347static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007348 TargetLowering::DAGCombinerInfo &DCI,
7349 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007350
Owen Anderson080c0922010-11-05 19:27:46 +00007351 // Attempt to use immediate-form VBIC
7352 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7353 DebugLoc dl = N->getDebugLoc();
7354 EVT VT = N->getValueType(0);
7355 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007356
Tanya Lattner0433b212011-04-07 15:24:20 +00007357 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7358 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007359
Owen Anderson080c0922010-11-05 19:27:46 +00007360 APInt SplatBits, SplatUndef;
7361 unsigned SplatBitSize;
7362 bool HasAnyUndefs;
7363 if (BVN &&
7364 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7365 if (SplatBitSize <= 64) {
7366 EVT VbicVT;
7367 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7368 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007369 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007370 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007371 if (Val.getNode()) {
7372 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007373 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007374 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007375 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007376 }
7377 }
7378 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007379
Evan Chengc892aeb2012-02-23 01:19:06 +00007380 if (!Subtarget->isThumb1Only()) {
7381 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7382 SDValue CAND = formConditionalOp(N, DAG, true);
7383 if (CAND.getNode())
7384 return CAND;
7385 }
7386
Owen Anderson080c0922010-11-05 19:27:46 +00007387 return SDValue();
7388}
7389
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007390/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7391static SDValue PerformORCombine(SDNode *N,
7392 TargetLowering::DAGCombinerInfo &DCI,
7393 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007394 // Attempt to use immediate-form VORR
7395 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7396 DebugLoc dl = N->getDebugLoc();
7397 EVT VT = N->getValueType(0);
7398 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007399
Tanya Lattner0433b212011-04-07 15:24:20 +00007400 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7401 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007402
Owen Anderson60f48702010-11-03 23:15:26 +00007403 APInt SplatBits, SplatUndef;
7404 unsigned SplatBitSize;
7405 bool HasAnyUndefs;
7406 if (BVN && Subtarget->hasNEON() &&
7407 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7408 if (SplatBitSize <= 64) {
7409 EVT VorrVT;
7410 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7411 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007412 DAG, VorrVT, VT.is128BitVector(),
7413 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007414 if (Val.getNode()) {
7415 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007416 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007417 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007418 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007419 }
7420 }
7421 }
7422
Evan Chengc892aeb2012-02-23 01:19:06 +00007423 if (!Subtarget->isThumb1Only()) {
7424 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7425 SDValue COR = formConditionalOp(N, DAG, true);
7426 if (COR.getNode())
7427 return COR;
7428 }
7429
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007430 SDValue N0 = N->getOperand(0);
7431 if (N0.getOpcode() != ISD::AND)
7432 return SDValue();
7433 SDValue N1 = N->getOperand(1);
7434
7435 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7436 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7437 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7438 APInt SplatUndef;
7439 unsigned SplatBitSize;
7440 bool HasAnyUndefs;
7441
7442 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7443 APInt SplatBits0;
7444 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7445 HasAnyUndefs) && !HasAnyUndefs) {
7446 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7447 APInt SplatBits1;
7448 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7449 HasAnyUndefs) && !HasAnyUndefs &&
7450 SplatBits0 == ~SplatBits1) {
7451 // Canonicalize the vector type to make instruction selection simpler.
7452 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7453 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7454 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007455 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007456 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7457 }
7458 }
7459 }
7460
Jim Grosbach54238562010-07-17 03:30:54 +00007461 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7462 // reasonable.
7463
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007464 // BFI is only available on V6T2+
7465 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7466 return SDValue();
7467
Jim Grosbach54238562010-07-17 03:30:54 +00007468 DebugLoc DL = N->getDebugLoc();
7469 // 1) or (and A, mask), val => ARMbfi A, val, mask
7470 // iff (val & mask) == val
7471 //
7472 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7473 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007474 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007475 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007476 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007477 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007478
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007479 if (VT != MVT::i32)
7480 return SDValue();
7481
Evan Cheng30fb13f2010-12-13 20:32:54 +00007482 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007483
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007484 // The value and the mask need to be constants so we can verify this is
7485 // actually a bitfield set. If the mask is 0xffff, we can do better
7486 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007487 SDValue MaskOp = N0.getOperand(1);
7488 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7489 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007490 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007491 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007492 if (Mask == 0xffff)
7493 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007494 SDValue Res;
7495 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007496 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7497 if (N1C) {
7498 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007499 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007500 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007501
Evan Chenga9688c42010-12-11 04:11:38 +00007502 if (ARM::isBitFieldInvertedMask(Mask)) {
7503 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007504
Evan Cheng30fb13f2010-12-13 20:32:54 +00007505 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007506 DAG.getConstant(Val, MVT::i32),
7507 DAG.getConstant(Mask, MVT::i32));
7508
7509 // Do not add new nodes to DAG combiner worklist.
7510 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007511 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007512 }
Jim Grosbach54238562010-07-17 03:30:54 +00007513 } else if (N1.getOpcode() == ISD::AND) {
7514 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007515 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7516 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007517 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007518 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007519
Eric Christopher29aeed12011-03-26 01:21:03 +00007520 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7521 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007522 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007523 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007524 // The pack halfword instruction works better for masks that fit it,
7525 // so use that when it's available.
7526 if (Subtarget->hasT2ExtractPack() &&
7527 (Mask == 0xffff || Mask == 0xffff0000))
7528 return SDValue();
7529 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007530 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007531 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007532 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007533 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007534 DAG.getConstant(Mask, MVT::i32));
7535 // Do not add new nodes to DAG combiner worklist.
7536 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007537 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007538 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007539 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007540 // The pack halfword instruction works better for masks that fit it,
7541 // so use that when it's available.
7542 if (Subtarget->hasT2ExtractPack() &&
7543 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7544 return SDValue();
7545 // 2b
7546 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007547 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007548 DAG.getConstant(lsb, MVT::i32));
7549 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007550 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007551 // Do not add new nodes to DAG combiner worklist.
7552 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007553 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007554 }
7555 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007556
Evan Cheng30fb13f2010-12-13 20:32:54 +00007557 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7558 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7559 ARM::isBitFieldInvertedMask(~Mask)) {
7560 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7561 // where lsb(mask) == #shamt and masked bits of B are known zero.
7562 SDValue ShAmt = N00.getOperand(1);
7563 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7564 unsigned LSB = CountTrailingZeros_32(Mask);
7565 if (ShAmtC != LSB)
7566 return SDValue();
7567
7568 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7569 DAG.getConstant(~Mask, MVT::i32));
7570
7571 // Do not add new nodes to DAG combiner worklist.
7572 DCI.CombineTo(N, Res, false);
7573 }
7574
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007575 return SDValue();
7576}
7577
Evan Chengc892aeb2012-02-23 01:19:06 +00007578static SDValue PerformXORCombine(SDNode *N,
7579 TargetLowering::DAGCombinerInfo &DCI,
7580 const ARMSubtarget *Subtarget) {
7581 EVT VT = N->getValueType(0);
7582 SelectionDAG &DAG = DCI.DAG;
7583
7584 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7585 return SDValue();
7586
7587 if (!Subtarget->isThumb1Only()) {
7588 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7589 SDValue CXOR = formConditionalOp(N, DAG, true);
7590 if (CXOR.getNode())
7591 return CXOR;
7592 }
7593
7594 return SDValue();
7595}
7596
Evan Chengbf188ae2011-06-15 01:12:31 +00007597/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7598/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007599static SDValue PerformBFICombine(SDNode *N,
7600 TargetLowering::DAGCombinerInfo &DCI) {
7601 SDValue N1 = N->getOperand(1);
7602 if (N1.getOpcode() == ISD::AND) {
7603 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7604 if (!N11C)
7605 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007606 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7607 unsigned LSB = CountTrailingZeros_32(~InvMask);
7608 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7609 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007610 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007611 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007612 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7613 N->getOperand(0), N1.getOperand(0),
7614 N->getOperand(2));
7615 }
7616 return SDValue();
7617}
7618
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007619/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7620/// ARMISD::VMOVRRD.
7621static SDValue PerformVMOVRRDCombine(SDNode *N,
7622 TargetLowering::DAGCombinerInfo &DCI) {
7623 // vmovrrd(vmovdrr x, y) -> x,y
7624 SDValue InDouble = N->getOperand(0);
7625 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7626 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007627
7628 // vmovrrd(load f64) -> (load i32), (load i32)
7629 SDNode *InNode = InDouble.getNode();
7630 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7631 InNode->getValueType(0) == MVT::f64 &&
7632 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7633 !cast<LoadSDNode>(InNode)->isVolatile()) {
7634 // TODO: Should this be done for non-FrameIndex operands?
7635 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7636
7637 SelectionDAG &DAG = DCI.DAG;
7638 DebugLoc DL = LD->getDebugLoc();
7639 SDValue BasePtr = LD->getBasePtr();
7640 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7641 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007642 LD->isNonTemporal(), LD->isInvariant(),
7643 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007644
7645 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7646 DAG.getConstant(4, MVT::i32));
7647 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7648 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007649 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007650 std::min(4U, LD->getAlignment() / 2));
7651
7652 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7653 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7654 DCI.RemoveFromWorklist(LD);
7655 DAG.DeleteNode(LD);
7656 return Result;
7657 }
7658
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007659 return SDValue();
7660}
7661
7662/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7663/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7664static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7665 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7666 SDValue Op0 = N->getOperand(0);
7667 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007668 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007669 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007670 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007671 Op1 = Op1.getOperand(0);
7672 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7673 Op0.getNode() == Op1.getNode() &&
7674 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007675 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007676 N->getValueType(0), Op0.getOperand(0));
7677 return SDValue();
7678}
7679
Bob Wilson31600902010-12-21 06:43:19 +00007680/// PerformSTORECombine - Target-specific dag combine xforms for
7681/// ISD::STORE.
7682static SDValue PerformSTORECombine(SDNode *N,
7683 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007684 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007685 if (St->isVolatile())
7686 return SDValue();
7687
Andrew Trick49b446f2012-07-18 18:34:24 +00007688 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007689 // pack all of the elements in one place. Next, store to memory in fewer
7690 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007691 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007692 EVT VT = StVal.getValueType();
7693 if (St->isTruncatingStore() && VT.isVector()) {
7694 SelectionDAG &DAG = DCI.DAG;
7695 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7696 EVT StVT = St->getMemoryVT();
7697 unsigned NumElems = VT.getVectorNumElements();
7698 assert(StVT != VT && "Cannot truncate to the same type");
7699 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7700 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7701
7702 // From, To sizes and ElemCount must be pow of two
7703 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7704
7705 // We are going to use the original vector elt for storing.
7706 // Accumulated smaller vector elements must be a multiple of the store size.
7707 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7708
7709 unsigned SizeRatio = FromEltSz / ToEltSz;
7710 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7711
7712 // Create a type on which we perform the shuffle.
7713 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7714 NumElems*SizeRatio);
7715 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7716
7717 DebugLoc DL = St->getDebugLoc();
7718 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7719 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7720 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7721
7722 // Can't shuffle using an illegal type.
7723 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7724
7725 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7726 DAG.getUNDEF(WideVec.getValueType()),
7727 ShuffleVec.data());
7728 // At this point all of the data is stored at the bottom of the
7729 // register. We now need to save it to mem.
7730
7731 // Find the largest store unit
7732 MVT StoreType = MVT::i8;
7733 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7734 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7735 MVT Tp = (MVT::SimpleValueType)tp;
7736 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7737 StoreType = Tp;
7738 }
7739 // Didn't find a legal store type.
7740 if (!TLI.isTypeLegal(StoreType))
7741 return SDValue();
7742
7743 // Bitcast the original vector into a vector of store-size units
7744 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7745 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7746 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7747 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7748 SmallVector<SDValue, 8> Chains;
7749 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7750 TLI.getPointerTy());
7751 SDValue BasePtr = St->getBasePtr();
7752
7753 // Perform one or more big stores into memory.
7754 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7755 for (unsigned I = 0; I < E; I++) {
7756 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7757 StoreType, ShuffWide,
7758 DAG.getIntPtrConstant(I));
7759 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7760 St->getPointerInfo(), St->isVolatile(),
7761 St->isNonTemporal(), St->getAlignment());
7762 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7763 Increment);
7764 Chains.push_back(Ch);
7765 }
7766 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7767 Chains.size());
7768 }
7769
7770 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007771 return SDValue();
7772
Chad Rosier96b66d62012-04-09 19:38:15 +00007773 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7774 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007775 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007776 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007777 SelectionDAG &DAG = DCI.DAG;
7778 DebugLoc DL = St->getDebugLoc();
7779 SDValue BasePtr = St->getBasePtr();
7780 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7781 StVal.getNode()->getOperand(0), BasePtr,
7782 St->getPointerInfo(), St->isVolatile(),
7783 St->isNonTemporal(), St->getAlignment());
7784
7785 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7786 DAG.getConstant(4, MVT::i32));
7787 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7788 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7789 St->isNonTemporal(),
7790 std::min(4U, St->getAlignment() / 2));
7791 }
7792
7793 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007794 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7795 return SDValue();
7796
Chad Rosier96b66d62012-04-09 19:38:15 +00007797 // Bitcast an i64 store extracted from a vector to f64.
7798 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007799 SelectionDAG &DAG = DCI.DAG;
7800 DebugLoc dl = StVal.getDebugLoc();
7801 SDValue IntVec = StVal.getOperand(0);
7802 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7803 IntVec.getValueType().getVectorNumElements());
7804 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7805 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7806 Vec, StVal.getOperand(1));
7807 dl = N->getDebugLoc();
7808 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7809 // Make the DAGCombiner fold the bitcasts.
7810 DCI.AddToWorklist(Vec.getNode());
7811 DCI.AddToWorklist(ExtElt.getNode());
7812 DCI.AddToWorklist(V.getNode());
7813 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7814 St->getPointerInfo(), St->isVolatile(),
7815 St->isNonTemporal(), St->getAlignment(),
7816 St->getTBAAInfo());
7817}
7818
7819/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7820/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7821/// i64 vector to have f64 elements, since the value can then be loaded
7822/// directly into a VFP register.
7823static bool hasNormalLoadOperand(SDNode *N) {
7824 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7825 for (unsigned i = 0; i < NumElts; ++i) {
7826 SDNode *Elt = N->getOperand(i).getNode();
7827 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7828 return true;
7829 }
7830 return false;
7831}
7832
Bob Wilson75f02882010-09-17 22:59:05 +00007833/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7834/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007835static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7836 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007837 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7838 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7839 // into a pair of GPRs, which is fine when the value is used as a scalar,
7840 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007841 SelectionDAG &DAG = DCI.DAG;
7842 if (N->getNumOperands() == 2) {
7843 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7844 if (RV.getNode())
7845 return RV;
7846 }
Bob Wilson75f02882010-09-17 22:59:05 +00007847
Bob Wilson31600902010-12-21 06:43:19 +00007848 // Load i64 elements as f64 values so that type legalization does not split
7849 // them up into i32 values.
7850 EVT VT = N->getValueType(0);
7851 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7852 return SDValue();
7853 DebugLoc dl = N->getDebugLoc();
7854 SmallVector<SDValue, 8> Ops;
7855 unsigned NumElts = VT.getVectorNumElements();
7856 for (unsigned i = 0; i < NumElts; ++i) {
7857 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7858 Ops.push_back(V);
7859 // Make the DAGCombiner fold the bitcast.
7860 DCI.AddToWorklist(V.getNode());
7861 }
7862 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7863 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7864 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7865}
7866
7867/// PerformInsertEltCombine - Target-specific dag combine xforms for
7868/// ISD::INSERT_VECTOR_ELT.
7869static SDValue PerformInsertEltCombine(SDNode *N,
7870 TargetLowering::DAGCombinerInfo &DCI) {
7871 // Bitcast an i64 load inserted into a vector to f64.
7872 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7873 EVT VT = N->getValueType(0);
7874 SDNode *Elt = N->getOperand(1).getNode();
7875 if (VT.getVectorElementType() != MVT::i64 ||
7876 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7877 return SDValue();
7878
7879 SelectionDAG &DAG = DCI.DAG;
7880 DebugLoc dl = N->getDebugLoc();
7881 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7882 VT.getVectorNumElements());
7883 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7884 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7885 // Make the DAGCombiner fold the bitcasts.
7886 DCI.AddToWorklist(Vec.getNode());
7887 DCI.AddToWorklist(V.getNode());
7888 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7889 Vec, V, N->getOperand(2));
7890 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007891}
7892
Bob Wilsonf20700c2010-10-27 20:38:28 +00007893/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7894/// ISD::VECTOR_SHUFFLE.
7895static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7896 // The LLVM shufflevector instruction does not require the shuffle mask
7897 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7898 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7899 // operands do not match the mask length, they are extended by concatenating
7900 // them with undef vectors. That is probably the right thing for other
7901 // targets, but for NEON it is better to concatenate two double-register
7902 // size vector operands into a single quad-register size vector. Do that
7903 // transformation here:
7904 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7905 // shuffle(concat(v1, v2), undef)
7906 SDValue Op0 = N->getOperand(0);
7907 SDValue Op1 = N->getOperand(1);
7908 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7909 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7910 Op0.getNumOperands() != 2 ||
7911 Op1.getNumOperands() != 2)
7912 return SDValue();
7913 SDValue Concat0Op1 = Op0.getOperand(1);
7914 SDValue Concat1Op1 = Op1.getOperand(1);
7915 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7916 Concat1Op1.getOpcode() != ISD::UNDEF)
7917 return SDValue();
7918 // Skip the transformation if any of the types are illegal.
7919 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7920 EVT VT = N->getValueType(0);
7921 if (!TLI.isTypeLegal(VT) ||
7922 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7923 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7924 return SDValue();
7925
7926 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7927 Op0.getOperand(0), Op1.getOperand(0));
7928 // Translate the shuffle mask.
7929 SmallVector<int, 16> NewMask;
7930 unsigned NumElts = VT.getVectorNumElements();
7931 unsigned HalfElts = NumElts/2;
7932 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7933 for (unsigned n = 0; n < NumElts; ++n) {
7934 int MaskElt = SVN->getMaskElt(n);
7935 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007936 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007937 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007938 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007939 NewElt = HalfElts + MaskElt - NumElts;
7940 NewMask.push_back(NewElt);
7941 }
7942 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7943 DAG.getUNDEF(VT), NewMask.data());
7944}
7945
Bob Wilson1c3ef902011-02-07 17:43:21 +00007946/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7947/// NEON load/store intrinsics to merge base address updates.
7948static SDValue CombineBaseUpdate(SDNode *N,
7949 TargetLowering::DAGCombinerInfo &DCI) {
7950 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7951 return SDValue();
7952
7953 SelectionDAG &DAG = DCI.DAG;
7954 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7955 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7956 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7957 SDValue Addr = N->getOperand(AddrOpIdx);
7958
7959 // Search for a use of the address operand that is an increment.
7960 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7961 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7962 SDNode *User = *UI;
7963 if (User->getOpcode() != ISD::ADD ||
7964 UI.getUse().getResNo() != Addr.getResNo())
7965 continue;
7966
7967 // Check that the add is independent of the load/store. Otherwise, folding
7968 // it would create a cycle.
7969 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7970 continue;
7971
7972 // Find the new opcode for the updating load/store.
7973 bool isLoad = true;
7974 bool isLaneOp = false;
7975 unsigned NewOpc = 0;
7976 unsigned NumVecs = 0;
7977 if (isIntrinsic) {
7978 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7979 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007980 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007981 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7982 NumVecs = 1; break;
7983 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7984 NumVecs = 2; break;
7985 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7986 NumVecs = 3; break;
7987 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7988 NumVecs = 4; break;
7989 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7990 NumVecs = 2; isLaneOp = true; break;
7991 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7992 NumVecs = 3; isLaneOp = true; break;
7993 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7994 NumVecs = 4; isLaneOp = true; break;
7995 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7996 NumVecs = 1; isLoad = false; break;
7997 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7998 NumVecs = 2; isLoad = false; break;
7999 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8000 NumVecs = 3; isLoad = false; break;
8001 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8002 NumVecs = 4; isLoad = false; break;
8003 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8004 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8005 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8006 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8007 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8008 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8009 }
8010 } else {
8011 isLaneOp = true;
8012 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008013 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008014 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8015 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8016 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8017 }
8018 }
8019
8020 // Find the size of memory referenced by the load/store.
8021 EVT VecTy;
8022 if (isLoad)
8023 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008024 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008025 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8026 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8027 if (isLaneOp)
8028 NumBytes /= VecTy.getVectorNumElements();
8029
8030 // If the increment is a constant, it must match the memory ref size.
8031 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8032 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8033 uint64_t IncVal = CInc->getZExtValue();
8034 if (IncVal != NumBytes)
8035 continue;
8036 } else if (NumBytes >= 3 * 16) {
8037 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8038 // separate instructions that make it harder to use a non-constant update.
8039 continue;
8040 }
8041
8042 // Create the new updating load/store node.
8043 EVT Tys[6];
8044 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8045 unsigned n;
8046 for (n = 0; n < NumResultVecs; ++n)
8047 Tys[n] = VecTy;
8048 Tys[n++] = MVT::i32;
8049 Tys[n] = MVT::Other;
8050 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8051 SmallVector<SDValue, 8> Ops;
8052 Ops.push_back(N->getOperand(0)); // incoming chain
8053 Ops.push_back(N->getOperand(AddrOpIdx));
8054 Ops.push_back(Inc);
8055 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8056 Ops.push_back(N->getOperand(i));
8057 }
8058 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8059 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8060 Ops.data(), Ops.size(),
8061 MemInt->getMemoryVT(),
8062 MemInt->getMemOperand());
8063
8064 // Update the uses.
8065 std::vector<SDValue> NewResults;
8066 for (unsigned i = 0; i < NumResultVecs; ++i) {
8067 NewResults.push_back(SDValue(UpdN.getNode(), i));
8068 }
8069 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8070 DCI.CombineTo(N, NewResults);
8071 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8072
8073 break;
Owen Anderson76706012011-04-05 21:48:57 +00008074 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008075 return SDValue();
8076}
8077
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008078/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8079/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8080/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8081/// return true.
8082static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8083 SelectionDAG &DAG = DCI.DAG;
8084 EVT VT = N->getValueType(0);
8085 // vldN-dup instructions only support 64-bit vectors for N > 1.
8086 if (!VT.is64BitVector())
8087 return false;
8088
8089 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8090 SDNode *VLD = N->getOperand(0).getNode();
8091 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8092 return false;
8093 unsigned NumVecs = 0;
8094 unsigned NewOpc = 0;
8095 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8096 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8097 NumVecs = 2;
8098 NewOpc = ARMISD::VLD2DUP;
8099 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8100 NumVecs = 3;
8101 NewOpc = ARMISD::VLD3DUP;
8102 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8103 NumVecs = 4;
8104 NewOpc = ARMISD::VLD4DUP;
8105 } else {
8106 return false;
8107 }
8108
8109 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8110 // numbers match the load.
8111 unsigned VLDLaneNo =
8112 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8113 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8114 UI != UE; ++UI) {
8115 // Ignore uses of the chain result.
8116 if (UI.getUse().getResNo() == NumVecs)
8117 continue;
8118 SDNode *User = *UI;
8119 if (User->getOpcode() != ARMISD::VDUPLANE ||
8120 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8121 return false;
8122 }
8123
8124 // Create the vldN-dup node.
8125 EVT Tys[5];
8126 unsigned n;
8127 for (n = 0; n < NumVecs; ++n)
8128 Tys[n] = VT;
8129 Tys[n] = MVT::Other;
8130 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8131 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8132 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8133 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8134 Ops, 2, VLDMemInt->getMemoryVT(),
8135 VLDMemInt->getMemOperand());
8136
8137 // Update the uses.
8138 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8139 UI != UE; ++UI) {
8140 unsigned ResNo = UI.getUse().getResNo();
8141 // Ignore uses of the chain result.
8142 if (ResNo == NumVecs)
8143 continue;
8144 SDNode *User = *UI;
8145 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8146 }
8147
8148 // Now the vldN-lane intrinsic is dead except for its chain result.
8149 // Update uses of the chain.
8150 std::vector<SDValue> VLDDupResults;
8151 for (unsigned n = 0; n < NumVecs; ++n)
8152 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8153 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8154 DCI.CombineTo(VLD, VLDDupResults);
8155
8156 return true;
8157}
8158
Bob Wilson9e82bf12010-07-14 01:22:12 +00008159/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8160/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008161static SDValue PerformVDUPLANECombine(SDNode *N,
8162 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008163 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008164
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008165 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8166 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8167 if (CombineVLDDUP(N, DCI))
8168 return SDValue(N, 0);
8169
8170 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8171 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008172 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008173 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008174 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008175 return SDValue();
8176
8177 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8178 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8179 // The canonical VMOV for a zero vector uses a 32-bit element size.
8180 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8181 unsigned EltBits;
8182 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8183 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008184 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008185 if (EltSize > VT.getVectorElementType().getSizeInBits())
8186 return SDValue();
8187
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008188 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008189}
8190
Eric Christopherfa6f5912011-06-29 21:10:36 +00008191// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008192// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8193static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8194{
Chad Rosier118c9a02011-06-28 17:26:57 +00008195 integerPart cN;
8196 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008197 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8198 I != E; I++) {
8199 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8200 if (!C)
8201 return false;
8202
Eric Christopherfa6f5912011-06-29 21:10:36 +00008203 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008204 APFloat APF = C->getValueAPF();
8205 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8206 != APFloat::opOK || !isExact)
8207 return false;
8208
8209 c0 = (I == 0) ? cN : c0;
8210 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8211 return false;
8212 }
8213 C = c0;
8214 return true;
8215}
8216
8217/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8218/// can replace combinations of VMUL and VCVT (floating-point to integer)
8219/// when the VMUL has a constant operand that is a power of 2.
8220///
8221/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8222/// vmul.f32 d16, d17, d16
8223/// vcvt.s32.f32 d16, d16
8224/// becomes:
8225/// vcvt.s32.f32 d16, d16, #3
8226static SDValue PerformVCVTCombine(SDNode *N,
8227 TargetLowering::DAGCombinerInfo &DCI,
8228 const ARMSubtarget *Subtarget) {
8229 SelectionDAG &DAG = DCI.DAG;
8230 SDValue Op = N->getOperand(0);
8231
8232 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8233 Op.getOpcode() != ISD::FMUL)
8234 return SDValue();
8235
8236 uint64_t C;
8237 SDValue N0 = Op->getOperand(0);
8238 SDValue ConstVec = Op->getOperand(1);
8239 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8240
Eric Christopherfa6f5912011-06-29 21:10:36 +00008241 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008242 !isConstVecPow2(ConstVec, isSigned, C))
8243 return SDValue();
8244
8245 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8246 Intrinsic::arm_neon_vcvtfp2fxu;
8247 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8248 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008249 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008250 DAG.getConstant(Log2_64(C), MVT::i32));
8251}
8252
8253/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8254/// can replace combinations of VCVT (integer to floating-point) and VDIV
8255/// when the VDIV has a constant operand that is a power of 2.
8256///
8257/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8258/// vcvt.f32.s32 d16, d16
8259/// vdiv.f32 d16, d17, d16
8260/// becomes:
8261/// vcvt.f32.s32 d16, d16, #3
8262static SDValue PerformVDIVCombine(SDNode *N,
8263 TargetLowering::DAGCombinerInfo &DCI,
8264 const ARMSubtarget *Subtarget) {
8265 SelectionDAG &DAG = DCI.DAG;
8266 SDValue Op = N->getOperand(0);
8267 unsigned OpOpcode = Op.getNode()->getOpcode();
8268
8269 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8270 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8271 return SDValue();
8272
8273 uint64_t C;
8274 SDValue ConstVec = N->getOperand(1);
8275 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8276
8277 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8278 !isConstVecPow2(ConstVec, isSigned, C))
8279 return SDValue();
8280
Eric Christopherfa6f5912011-06-29 21:10:36 +00008281 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008282 Intrinsic::arm_neon_vcvtfxu2fp;
8283 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8284 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008285 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008286 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8287}
8288
8289/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008290/// operand of a vector shift operation, where all the elements of the
8291/// build_vector must have the same constant integer value.
8292static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8293 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008294 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008295 Op = Op.getOperand(0);
8296 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8297 APInt SplatBits, SplatUndef;
8298 unsigned SplatBitSize;
8299 bool HasAnyUndefs;
8300 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8301 HasAnyUndefs, ElementBits) ||
8302 SplatBitSize > ElementBits)
8303 return false;
8304 Cnt = SplatBits.getSExtValue();
8305 return true;
8306}
8307
8308/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8309/// operand of a vector shift left operation. That value must be in the range:
8310/// 0 <= Value < ElementBits for a left shift; or
8311/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008312static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008313 assert(VT.isVector() && "vector shift count is not a vector type");
8314 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8315 if (! getVShiftImm(Op, ElementBits, Cnt))
8316 return false;
8317 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8318}
8319
8320/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8321/// operand of a vector shift right operation. For a shift opcode, the value
8322/// is positive, but for an intrinsic the value count must be negative. The
8323/// absolute value must be in the range:
8324/// 1 <= |Value| <= ElementBits for a right shift; or
8325/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008326static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008327 int64_t &Cnt) {
8328 assert(VT.isVector() && "vector shift count is not a vector type");
8329 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8330 if (! getVShiftImm(Op, ElementBits, Cnt))
8331 return false;
8332 if (isIntrinsic)
8333 Cnt = -Cnt;
8334 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8335}
8336
8337/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8338static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8339 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8340 switch (IntNo) {
8341 default:
8342 // Don't do anything for most intrinsics.
8343 break;
8344
8345 // Vector shifts: check for immediate versions and lower them.
8346 // Note: This is done during DAG combining instead of DAG legalizing because
8347 // the build_vectors for 64-bit vector element shift counts are generally
8348 // not legal, and it is hard to see their values after they get legalized to
8349 // loads from a constant pool.
8350 case Intrinsic::arm_neon_vshifts:
8351 case Intrinsic::arm_neon_vshiftu:
8352 case Intrinsic::arm_neon_vshiftls:
8353 case Intrinsic::arm_neon_vshiftlu:
8354 case Intrinsic::arm_neon_vshiftn:
8355 case Intrinsic::arm_neon_vrshifts:
8356 case Intrinsic::arm_neon_vrshiftu:
8357 case Intrinsic::arm_neon_vrshiftn:
8358 case Intrinsic::arm_neon_vqshifts:
8359 case Intrinsic::arm_neon_vqshiftu:
8360 case Intrinsic::arm_neon_vqshiftsu:
8361 case Intrinsic::arm_neon_vqshiftns:
8362 case Intrinsic::arm_neon_vqshiftnu:
8363 case Intrinsic::arm_neon_vqshiftnsu:
8364 case Intrinsic::arm_neon_vqrshiftns:
8365 case Intrinsic::arm_neon_vqrshiftnu:
8366 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008367 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008368 int64_t Cnt;
8369 unsigned VShiftOpc = 0;
8370
8371 switch (IntNo) {
8372 case Intrinsic::arm_neon_vshifts:
8373 case Intrinsic::arm_neon_vshiftu:
8374 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8375 VShiftOpc = ARMISD::VSHL;
8376 break;
8377 }
8378 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8379 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8380 ARMISD::VSHRs : ARMISD::VSHRu);
8381 break;
8382 }
8383 return SDValue();
8384
8385 case Intrinsic::arm_neon_vshiftls:
8386 case Intrinsic::arm_neon_vshiftlu:
8387 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8388 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008389 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008390
8391 case Intrinsic::arm_neon_vrshifts:
8392 case Intrinsic::arm_neon_vrshiftu:
8393 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8394 break;
8395 return SDValue();
8396
8397 case Intrinsic::arm_neon_vqshifts:
8398 case Intrinsic::arm_neon_vqshiftu:
8399 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8400 break;
8401 return SDValue();
8402
8403 case Intrinsic::arm_neon_vqshiftsu:
8404 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8405 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008406 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008407
8408 case Intrinsic::arm_neon_vshiftn:
8409 case Intrinsic::arm_neon_vrshiftn:
8410 case Intrinsic::arm_neon_vqshiftns:
8411 case Intrinsic::arm_neon_vqshiftnu:
8412 case Intrinsic::arm_neon_vqshiftnsu:
8413 case Intrinsic::arm_neon_vqrshiftns:
8414 case Intrinsic::arm_neon_vqrshiftnu:
8415 case Intrinsic::arm_neon_vqrshiftnsu:
8416 // Narrowing shifts require an immediate right shift.
8417 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8418 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008419 llvm_unreachable("invalid shift count for narrowing vector shift "
8420 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008421
8422 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008423 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008424 }
8425
8426 switch (IntNo) {
8427 case Intrinsic::arm_neon_vshifts:
8428 case Intrinsic::arm_neon_vshiftu:
8429 // Opcode already set above.
8430 break;
8431 case Intrinsic::arm_neon_vshiftls:
8432 case Intrinsic::arm_neon_vshiftlu:
8433 if (Cnt == VT.getVectorElementType().getSizeInBits())
8434 VShiftOpc = ARMISD::VSHLLi;
8435 else
8436 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8437 ARMISD::VSHLLs : ARMISD::VSHLLu);
8438 break;
8439 case Intrinsic::arm_neon_vshiftn:
8440 VShiftOpc = ARMISD::VSHRN; break;
8441 case Intrinsic::arm_neon_vrshifts:
8442 VShiftOpc = ARMISD::VRSHRs; break;
8443 case Intrinsic::arm_neon_vrshiftu:
8444 VShiftOpc = ARMISD::VRSHRu; break;
8445 case Intrinsic::arm_neon_vrshiftn:
8446 VShiftOpc = ARMISD::VRSHRN; break;
8447 case Intrinsic::arm_neon_vqshifts:
8448 VShiftOpc = ARMISD::VQSHLs; break;
8449 case Intrinsic::arm_neon_vqshiftu:
8450 VShiftOpc = ARMISD::VQSHLu; break;
8451 case Intrinsic::arm_neon_vqshiftsu:
8452 VShiftOpc = ARMISD::VQSHLsu; break;
8453 case Intrinsic::arm_neon_vqshiftns:
8454 VShiftOpc = ARMISD::VQSHRNs; break;
8455 case Intrinsic::arm_neon_vqshiftnu:
8456 VShiftOpc = ARMISD::VQSHRNu; break;
8457 case Intrinsic::arm_neon_vqshiftnsu:
8458 VShiftOpc = ARMISD::VQSHRNsu; break;
8459 case Intrinsic::arm_neon_vqrshiftns:
8460 VShiftOpc = ARMISD::VQRSHRNs; break;
8461 case Intrinsic::arm_neon_vqrshiftnu:
8462 VShiftOpc = ARMISD::VQRSHRNu; break;
8463 case Intrinsic::arm_neon_vqrshiftnsu:
8464 VShiftOpc = ARMISD::VQRSHRNsu; break;
8465 }
8466
8467 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008468 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008469 }
8470
8471 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008472 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008473 int64_t Cnt;
8474 unsigned VShiftOpc = 0;
8475
8476 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8477 VShiftOpc = ARMISD::VSLI;
8478 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8479 VShiftOpc = ARMISD::VSRI;
8480 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008481 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008482 }
8483
8484 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8485 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008486 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008487 }
8488
8489 case Intrinsic::arm_neon_vqrshifts:
8490 case Intrinsic::arm_neon_vqrshiftu:
8491 // No immediate versions of these to check for.
8492 break;
8493 }
8494
8495 return SDValue();
8496}
8497
8498/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8499/// lowers them. As with the vector shift intrinsics, this is done during DAG
8500/// combining instead of DAG legalizing because the build_vectors for 64-bit
8501/// vector element shift counts are generally not legal, and it is hard to see
8502/// their values after they get legalized to loads from a constant pool.
8503static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8504 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008505 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008506 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8507 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8508 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8509 SDValue N1 = N->getOperand(1);
8510 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8511 SDValue N0 = N->getOperand(0);
8512 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8513 DAG.MaskedValueIsZero(N0.getOperand(0),
8514 APInt::getHighBitsSet(32, 16)))
8515 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8516 }
8517 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008518
8519 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008520 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8521 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008522 return SDValue();
8523
8524 assert(ST->hasNEON() && "unexpected vector shift");
8525 int64_t Cnt;
8526
8527 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008528 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008529
8530 case ISD::SHL:
8531 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8532 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008533 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008534 break;
8535
8536 case ISD::SRA:
8537 case ISD::SRL:
8538 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8539 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8540 ARMISD::VSHRs : ARMISD::VSHRu);
8541 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008542 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008543 }
8544 }
8545 return SDValue();
8546}
8547
8548/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8549/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8550static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8551 const ARMSubtarget *ST) {
8552 SDValue N0 = N->getOperand(0);
8553
8554 // Check for sign- and zero-extensions of vector extract operations of 8-
8555 // and 16-bit vector elements. NEON supports these directly. They are
8556 // handled during DAG combining because type legalization will promote them
8557 // to 32-bit types and it is messy to recognize the operations after that.
8558 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8559 SDValue Vec = N0.getOperand(0);
8560 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008561 EVT VT = N->getValueType(0);
8562 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008563 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8564
Owen Anderson825b72b2009-08-11 20:47:22 +00008565 if (VT == MVT::i32 &&
8566 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008567 TLI.isTypeLegal(Vec.getValueType()) &&
8568 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008569
8570 unsigned Opc = 0;
8571 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008572 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008573 case ISD::SIGN_EXTEND:
8574 Opc = ARMISD::VGETLANEs;
8575 break;
8576 case ISD::ZERO_EXTEND:
8577 case ISD::ANY_EXTEND:
8578 Opc = ARMISD::VGETLANEu;
8579 break;
8580 }
8581 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8582 }
8583 }
8584
8585 return SDValue();
8586}
8587
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008588/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8589/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8590static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8591 const ARMSubtarget *ST) {
8592 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008593 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008594 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8595 // a NaN; only do the transformation when it matches that behavior.
8596
8597 // For now only do this when using NEON for FP operations; if using VFP, it
8598 // is not obvious that the benefit outweighs the cost of switching to the
8599 // NEON pipeline.
8600 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8601 N->getValueType(0) != MVT::f32)
8602 return SDValue();
8603
8604 SDValue CondLHS = N->getOperand(0);
8605 SDValue CondRHS = N->getOperand(1);
8606 SDValue LHS = N->getOperand(2);
8607 SDValue RHS = N->getOperand(3);
8608 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8609
8610 unsigned Opcode = 0;
8611 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008612 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008613 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008614 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008615 IsReversed = true ; // x CC y ? y : x
8616 } else {
8617 return SDValue();
8618 }
8619
Bob Wilsone742bb52010-02-24 22:15:53 +00008620 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008621 switch (CC) {
8622 default: break;
8623 case ISD::SETOLT:
8624 case ISD::SETOLE:
8625 case ISD::SETLT:
8626 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008627 case ISD::SETULT:
8628 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008629 // If LHS is NaN, an ordered comparison will be false and the result will
8630 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8631 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8632 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8633 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8634 break;
8635 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8636 // will return -0, so vmin can only be used for unsafe math or if one of
8637 // the operands is known to be nonzero.
8638 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008639 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008640 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8641 break;
8642 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008643 break;
8644
8645 case ISD::SETOGT:
8646 case ISD::SETOGE:
8647 case ISD::SETGT:
8648 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008649 case ISD::SETUGT:
8650 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008651 // If LHS is NaN, an ordered comparison will be false and the result will
8652 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8653 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8654 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8655 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8656 break;
8657 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8658 // will return +0, so vmax can only be used for unsafe math or if one of
8659 // the operands is known to be nonzero.
8660 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008661 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008662 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8663 break;
8664 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008665 break;
8666 }
8667
8668 if (!Opcode)
8669 return SDValue();
8670 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8671}
8672
Evan Chenge721f5c2011-07-13 00:42:17 +00008673/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8674SDValue
8675ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8676 SDValue Cmp = N->getOperand(4);
8677 if (Cmp.getOpcode() != ARMISD::CMPZ)
8678 // Only looking at EQ and NE cases.
8679 return SDValue();
8680
8681 EVT VT = N->getValueType(0);
8682 DebugLoc dl = N->getDebugLoc();
8683 SDValue LHS = Cmp.getOperand(0);
8684 SDValue RHS = Cmp.getOperand(1);
8685 SDValue FalseVal = N->getOperand(0);
8686 SDValue TrueVal = N->getOperand(1);
8687 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008688 ARMCC::CondCodes CC =
8689 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008690
8691 // Simplify
8692 // mov r1, r0
8693 // cmp r1, x
8694 // mov r0, y
8695 // moveq r0, x
8696 // to
8697 // cmp r0, x
8698 // movne r0, y
8699 //
8700 // mov r1, r0
8701 // cmp r1, x
8702 // mov r0, x
8703 // movne r0, y
8704 // to
8705 // cmp r0, x
8706 // movne r0, y
8707 /// FIXME: Turn this into a target neutral optimization?
8708 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008709 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008710 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8711 N->getOperand(3), Cmp);
8712 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8713 SDValue ARMcc;
8714 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8715 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8716 N->getOperand(3), NewCmp);
8717 }
8718
8719 if (Res.getNode()) {
8720 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008721 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008722 // Capture demanded bits information that would be otherwise lost.
8723 if (KnownZero == 0xfffffffe)
8724 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8725 DAG.getValueType(MVT::i1));
8726 else if (KnownZero == 0xffffff00)
8727 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8728 DAG.getValueType(MVT::i8));
8729 else if (KnownZero == 0xffff0000)
8730 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8731 DAG.getValueType(MVT::i16));
8732 }
8733
8734 return Res;
8735}
8736
Dan Gohman475871a2008-07-27 21:46:04 +00008737SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008738 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008739 switch (N->getOpcode()) {
8740 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008741 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008742 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008743 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008744 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008745 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8746 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008747 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008748 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008749 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008750 case ISD::STORE: return PerformSTORECombine(N, DCI);
8751 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8752 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008753 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008754 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008755 case ISD::FP_TO_SINT:
8756 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8757 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008758 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008759 case ISD::SHL:
8760 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008761 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008762 case ISD::SIGN_EXTEND:
8763 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008764 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8765 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008766 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008767 case ARMISD::VLD2DUP:
8768 case ARMISD::VLD3DUP:
8769 case ARMISD::VLD4DUP:
8770 return CombineBaseUpdate(N, DCI);
8771 case ISD::INTRINSIC_VOID:
8772 case ISD::INTRINSIC_W_CHAIN:
8773 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8774 case Intrinsic::arm_neon_vld1:
8775 case Intrinsic::arm_neon_vld2:
8776 case Intrinsic::arm_neon_vld3:
8777 case Intrinsic::arm_neon_vld4:
8778 case Intrinsic::arm_neon_vld2lane:
8779 case Intrinsic::arm_neon_vld3lane:
8780 case Intrinsic::arm_neon_vld4lane:
8781 case Intrinsic::arm_neon_vst1:
8782 case Intrinsic::arm_neon_vst2:
8783 case Intrinsic::arm_neon_vst3:
8784 case Intrinsic::arm_neon_vst4:
8785 case Intrinsic::arm_neon_vst2lane:
8786 case Intrinsic::arm_neon_vst3lane:
8787 case Intrinsic::arm_neon_vst4lane:
8788 return CombineBaseUpdate(N, DCI);
8789 default: break;
8790 }
8791 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008792 }
Dan Gohman475871a2008-07-27 21:46:04 +00008793 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008794}
8795
Evan Cheng31959b12011-02-02 01:06:55 +00008796bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8797 EVT VT) const {
8798 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8799}
8800
Bill Wendlingaf566342009-08-15 21:21:19 +00008801bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008802 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008803 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008804
8805 switch (VT.getSimpleVT().SimpleTy) {
8806 default:
8807 return false;
8808 case MVT::i8:
8809 case MVT::i16:
8810 case MVT::i32:
8811 return true;
8812 // FIXME: VLD1 etc with standard alignment is legal.
8813 }
8814}
8815
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008816static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8817 unsigned AlignCheck) {
8818 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8819 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8820}
8821
8822EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8823 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008824 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008825 bool MemcpyStrSrc,
8826 MachineFunction &MF) const {
8827 const Function *F = MF.getFunction();
8828
8829 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008830 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008831 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8832 Subtarget->hasNEON()) {
8833 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8834 return MVT::v4i32;
8835 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8836 return MVT::v2i32;
8837 }
8838 }
8839
Lang Hames5207bf22011-11-08 18:56:23 +00008840 // Lowering to i32/i16 if the size permits.
8841 if (Size >= 4) {
8842 return MVT::i32;
8843 } else if (Size >= 2) {
8844 return MVT::i16;
8845 }
8846
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008847 // Let the target-independent logic figure it out.
8848 return MVT::Other;
8849}
8850
Evan Chenge6c835f2009-08-14 20:09:37 +00008851static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8852 if (V < 0)
8853 return false;
8854
8855 unsigned Scale = 1;
8856 switch (VT.getSimpleVT().SimpleTy) {
8857 default: return false;
8858 case MVT::i1:
8859 case MVT::i8:
8860 // Scale == 1;
8861 break;
8862 case MVT::i16:
8863 // Scale == 2;
8864 Scale = 2;
8865 break;
8866 case MVT::i32:
8867 // Scale == 4;
8868 Scale = 4;
8869 break;
8870 }
8871
8872 if ((V & (Scale - 1)) != 0)
8873 return false;
8874 V /= Scale;
8875 return V == (V & ((1LL << 5) - 1));
8876}
8877
8878static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8879 const ARMSubtarget *Subtarget) {
8880 bool isNeg = false;
8881 if (V < 0) {
8882 isNeg = true;
8883 V = - V;
8884 }
8885
8886 switch (VT.getSimpleVT().SimpleTy) {
8887 default: return false;
8888 case MVT::i1:
8889 case MVT::i8:
8890 case MVT::i16:
8891 case MVT::i32:
8892 // + imm12 or - imm8
8893 if (isNeg)
8894 return V == (V & ((1LL << 8) - 1));
8895 return V == (V & ((1LL << 12) - 1));
8896 case MVT::f32:
8897 case MVT::f64:
8898 // Same as ARM mode. FIXME: NEON?
8899 if (!Subtarget->hasVFP2())
8900 return false;
8901 if ((V & 3) != 0)
8902 return false;
8903 V >>= 2;
8904 return V == (V & ((1LL << 8) - 1));
8905 }
8906}
8907
Evan Chengb01fad62007-03-12 23:30:29 +00008908/// isLegalAddressImmediate - Return true if the integer value can be used
8909/// as the offset of the target addressing mode for load / store of the
8910/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008911static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008912 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008913 if (V == 0)
8914 return true;
8915
Evan Cheng65011532009-03-09 19:15:00 +00008916 if (!VT.isSimple())
8917 return false;
8918
Evan Chenge6c835f2009-08-14 20:09:37 +00008919 if (Subtarget->isThumb1Only())
8920 return isLegalT1AddressImmediate(V, VT);
8921 else if (Subtarget->isThumb2())
8922 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008923
Evan Chenge6c835f2009-08-14 20:09:37 +00008924 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008925 if (V < 0)
8926 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008927 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008928 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008929 case MVT::i1:
8930 case MVT::i8:
8931 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008932 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008933 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008934 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008935 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008936 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008937 case MVT::f32:
8938 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008939 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008940 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008941 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008942 return false;
8943 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008944 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008945 }
Evan Chenga8e29892007-01-19 07:51:42 +00008946}
8947
Evan Chenge6c835f2009-08-14 20:09:37 +00008948bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8949 EVT VT) const {
8950 int Scale = AM.Scale;
8951 if (Scale < 0)
8952 return false;
8953
8954 switch (VT.getSimpleVT().SimpleTy) {
8955 default: return false;
8956 case MVT::i1:
8957 case MVT::i8:
8958 case MVT::i16:
8959 case MVT::i32:
8960 if (Scale == 1)
8961 return true;
8962 // r + r << imm
8963 Scale = Scale & ~1;
8964 return Scale == 2 || Scale == 4 || Scale == 8;
8965 case MVT::i64:
8966 // r + r
8967 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8968 return true;
8969 return false;
8970 case MVT::isVoid:
8971 // Note, we allow "void" uses (basically, uses that aren't loads or
8972 // stores), because arm allows folding a scale into many arithmetic
8973 // operations. This should be made more precise and revisited later.
8974
8975 // Allow r << imm, but the imm has to be a multiple of two.
8976 if (Scale & 1) return false;
8977 return isPowerOf2_32(Scale);
8978 }
8979}
8980
Chris Lattner37caf8c2007-04-09 23:33:39 +00008981/// isLegalAddressingMode - Return true if the addressing mode represented
8982/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008983bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008984 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008985 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008986 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008987 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008988
Chris Lattner37caf8c2007-04-09 23:33:39 +00008989 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008990 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008991 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008992
Chris Lattner37caf8c2007-04-09 23:33:39 +00008993 switch (AM.Scale) {
8994 case 0: // no scale reg, must be "r+i" or "r", or "i".
8995 break;
8996 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008997 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008998 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008999 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009000 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009001 // ARM doesn't support any R+R*scale+imm addr modes.
9002 if (AM.BaseOffs)
9003 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009004
Bob Wilson2c7dab12009-04-08 17:55:28 +00009005 if (!VT.isSimple())
9006 return false;
9007
Evan Chenge6c835f2009-08-14 20:09:37 +00009008 if (Subtarget->isThumb2())
9009 return isLegalT2ScaledAddressingMode(AM, VT);
9010
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009011 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009013 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009014 case MVT::i1:
9015 case MVT::i8:
9016 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009017 if (Scale < 0) Scale = -Scale;
9018 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009019 return true;
9020 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009021 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009022 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009023 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009024 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009025 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009026 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009027 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009028
Owen Anderson825b72b2009-08-11 20:47:22 +00009029 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009030 // Note, we allow "void" uses (basically, uses that aren't loads or
9031 // stores), because arm allows folding a scale into many arithmetic
9032 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009033
Chris Lattner37caf8c2007-04-09 23:33:39 +00009034 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009035 if (Scale & 1) return false;
9036 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009037 }
Evan Chengb01fad62007-03-12 23:30:29 +00009038 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009039 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009040}
9041
Evan Cheng77e47512009-11-11 19:05:52 +00009042/// isLegalICmpImmediate - Return true if the specified immediate is legal
9043/// icmp immediate, that is the target has icmp instructions which can compare
9044/// a register against the immediate without having to materialize the
9045/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009046bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009047 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009048 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009049 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009050 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009051 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009052 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009053 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009054}
9055
Andrew Trick8d8d9612012-07-18 18:34:27 +00009056/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9057/// *or sub* immediate, that is the target has add or sub instructions which can
9058/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009059/// immediate into a register.
9060bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009061 // Same encoding for add/sub, just flip the sign.
9062 int64_t AbsImm = llvm::abs64(Imm);
9063 if (!Subtarget->isThumb())
9064 return ARM_AM::getSOImmVal(AbsImm) != -1;
9065 if (Subtarget->isThumb2())
9066 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9067 // Thumb1 only has 8-bit unsigned immediate.
9068 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009069}
9070
Owen Andersone50ed302009-08-10 22:56:29 +00009071static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009072 bool isSEXTLoad, SDValue &Base,
9073 SDValue &Offset, bool &isInc,
9074 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009075 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9076 return false;
9077
Owen Anderson825b72b2009-08-11 20:47:22 +00009078 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009079 // AddressingMode 3
9080 Base = Ptr->getOperand(0);
9081 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009082 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009083 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009084 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009085 isInc = false;
9086 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9087 return true;
9088 }
9089 }
9090 isInc = (Ptr->getOpcode() == ISD::ADD);
9091 Offset = Ptr->getOperand(1);
9092 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009093 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009094 // AddressingMode 2
9095 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009096 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009097 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009098 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009099 isInc = false;
9100 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9101 Base = Ptr->getOperand(0);
9102 return true;
9103 }
9104 }
9105
9106 if (Ptr->getOpcode() == ISD::ADD) {
9107 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009108 ARM_AM::ShiftOpc ShOpcVal=
9109 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009110 if (ShOpcVal != ARM_AM::no_shift) {
9111 Base = Ptr->getOperand(1);
9112 Offset = Ptr->getOperand(0);
9113 } else {
9114 Base = Ptr->getOperand(0);
9115 Offset = Ptr->getOperand(1);
9116 }
9117 return true;
9118 }
9119
9120 isInc = (Ptr->getOpcode() == ISD::ADD);
9121 Base = Ptr->getOperand(0);
9122 Offset = Ptr->getOperand(1);
9123 return true;
9124 }
9125
Jim Grosbache5165492009-11-09 00:11:35 +00009126 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009127 return false;
9128}
9129
Owen Andersone50ed302009-08-10 22:56:29 +00009130static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009131 bool isSEXTLoad, SDValue &Base,
9132 SDValue &Offset, bool &isInc,
9133 SelectionDAG &DAG) {
9134 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9135 return false;
9136
9137 Base = Ptr->getOperand(0);
9138 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9139 int RHSC = (int)RHS->getZExtValue();
9140 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9141 assert(Ptr->getOpcode() == ISD::ADD);
9142 isInc = false;
9143 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9144 return true;
9145 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9146 isInc = Ptr->getOpcode() == ISD::ADD;
9147 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9148 return true;
9149 }
9150 }
9151
9152 return false;
9153}
9154
Evan Chenga8e29892007-01-19 07:51:42 +00009155/// getPreIndexedAddressParts - returns true by value, base pointer and
9156/// offset pointer and addressing mode by reference if the node's address
9157/// can be legally represented as pre-indexed load / store address.
9158bool
Dan Gohman475871a2008-07-27 21:46:04 +00009159ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9160 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009161 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009162 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009163 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009164 return false;
9165
Owen Andersone50ed302009-08-10 22:56:29 +00009166 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009167 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009168 bool isSEXTLoad = false;
9169 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9170 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009171 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009172 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9173 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9174 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009175 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009176 } else
9177 return false;
9178
9179 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009180 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009181 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009182 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9183 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009184 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009185 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009186 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009187 if (!isLegal)
9188 return false;
9189
9190 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9191 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009192}
9193
9194/// getPostIndexedAddressParts - returns true by value, base pointer and
9195/// offset pointer and addressing mode by reference if this node can be
9196/// combined with a load / store to form a post-indexed load / store.
9197bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009198 SDValue &Base,
9199 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009200 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009201 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009202 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009203 return false;
9204
Owen Andersone50ed302009-08-10 22:56:29 +00009205 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009206 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009207 bool isSEXTLoad = false;
9208 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009209 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009210 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009211 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9212 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009213 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009214 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009215 } else
9216 return false;
9217
9218 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009219 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009220 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009221 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009222 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009223 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009224 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9225 isInc, DAG);
9226 if (!isLegal)
9227 return false;
9228
Evan Cheng28dad2a2010-05-18 21:31:17 +00009229 if (Ptr != Base) {
9230 // Swap base ptr and offset to catch more post-index load / store when
9231 // it's legal. In Thumb2 mode, offset must be an immediate.
9232 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9233 !Subtarget->isThumb2())
9234 std::swap(Base, Offset);
9235
9236 // Post-indexed load / store update the base pointer.
9237 if (Ptr != Base)
9238 return false;
9239 }
9240
Evan Chenge88d5ce2009-07-02 07:28:31 +00009241 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9242 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009243}
9244
Dan Gohman475871a2008-07-27 21:46:04 +00009245void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009246 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009247 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009248 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009249 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009250 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009251 switch (Op.getOpcode()) {
9252 default: break;
9253 case ARMISD::CMOV: {
9254 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009255 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009256 if (KnownZero == 0 && KnownOne == 0) return;
9257
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009258 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009259 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009260 KnownZero &= KnownZeroRHS;
9261 KnownOne &= KnownOneRHS;
9262 return;
9263 }
9264 }
9265}
9266
9267//===----------------------------------------------------------------------===//
9268// ARM Inline Assembly Support
9269//===----------------------------------------------------------------------===//
9270
Evan Cheng55d42002011-01-08 01:24:27 +00009271bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9272 // Looking for "rev" which is V6+.
9273 if (!Subtarget->hasV6Ops())
9274 return false;
9275
9276 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9277 std::string AsmStr = IA->getAsmString();
9278 SmallVector<StringRef, 4> AsmPieces;
9279 SplitString(AsmStr, AsmPieces, ";\n");
9280
9281 switch (AsmPieces.size()) {
9282 default: return false;
9283 case 1:
9284 AsmStr = AsmPieces[0];
9285 AsmPieces.clear();
9286 SplitString(AsmStr, AsmPieces, " \t,");
9287
9288 // rev $0, $1
9289 if (AsmPieces.size() == 3 &&
9290 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9291 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009292 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009293 if (Ty && Ty->getBitWidth() == 32)
9294 return IntrinsicLowering::LowerToByteSwap(CI);
9295 }
9296 break;
9297 }
9298
9299 return false;
9300}
9301
Evan Chenga8e29892007-01-19 07:51:42 +00009302/// getConstraintType - Given a constraint letter, return the type of
9303/// constraint it is for this target.
9304ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009305ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9306 if (Constraint.size() == 1) {
9307 switch (Constraint[0]) {
9308 default: break;
9309 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009310 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009311 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009312 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009313 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009314 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009315 // An address with a single base register. Due to the way we
9316 // currently handle addresses it is the same as an 'r' memory constraint.
9317 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009318 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009319 } else if (Constraint.size() == 2) {
9320 switch (Constraint[0]) {
9321 default: break;
9322 // All 'U+' constraints are addresses.
9323 case 'U': return C_Memory;
9324 }
Evan Chenga8e29892007-01-19 07:51:42 +00009325 }
Chris Lattner4234f572007-03-25 02:14:49 +00009326 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009327}
9328
John Thompson44ab89e2010-10-29 17:29:13 +00009329/// Examine constraint type and operand type and determine a weight value.
9330/// This object must already have been set up with the operand type
9331/// and the current alternative constraint selected.
9332TargetLowering::ConstraintWeight
9333ARMTargetLowering::getSingleConstraintMatchWeight(
9334 AsmOperandInfo &info, const char *constraint) const {
9335 ConstraintWeight weight = CW_Invalid;
9336 Value *CallOperandVal = info.CallOperandVal;
9337 // If we don't have a value, we can't do a match,
9338 // but allow it at the lowest weight.
9339 if (CallOperandVal == NULL)
9340 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009341 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009342 // Look at the constraint type.
9343 switch (*constraint) {
9344 default:
9345 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9346 break;
9347 case 'l':
9348 if (type->isIntegerTy()) {
9349 if (Subtarget->isThumb())
9350 weight = CW_SpecificReg;
9351 else
9352 weight = CW_Register;
9353 }
9354 break;
9355 case 'w':
9356 if (type->isFloatingPointTy())
9357 weight = CW_Register;
9358 break;
9359 }
9360 return weight;
9361}
9362
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009363typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9364RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009365ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009366 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009367 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009368 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009369 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009370 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009371 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009372 return RCPair(0U, &ARM::tGPRRegClass);
9373 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009374 case 'h': // High regs or no regs.
9375 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009376 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009377 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009378 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009379 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009380 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009381 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009382 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009383 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009384 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009385 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009386 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009387 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009388 case 'x':
9389 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009390 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009391 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009392 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009393 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009394 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009395 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009396 case 't':
9397 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009398 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009399 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009400 }
9401 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009402 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009403 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009404
Evan Chenga8e29892007-01-19 07:51:42 +00009405 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9406}
9407
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009408/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9409/// vector. If it is invalid, don't add anything to Ops.
9410void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009411 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009412 std::vector<SDValue>&Ops,
9413 SelectionDAG &DAG) const {
9414 SDValue Result(0, 0);
9415
Eric Christopher100c8332011-06-02 23:16:42 +00009416 // Currently only support length 1 constraints.
9417 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009418
Eric Christopher100c8332011-06-02 23:16:42 +00009419 char ConstraintLetter = Constraint[0];
9420 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009421 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009422 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009423 case 'I': case 'J': case 'K': case 'L':
9424 case 'M': case 'N': case 'O':
9425 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9426 if (!C)
9427 return;
9428
9429 int64_t CVal64 = C->getSExtValue();
9430 int CVal = (int) CVal64;
9431 // None of these constraints allow values larger than 32 bits. Check
9432 // that the value fits in an int.
9433 if (CVal != CVal64)
9434 return;
9435
Eric Christopher100c8332011-06-02 23:16:42 +00009436 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009437 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009438 // Constant suitable for movw, must be between 0 and
9439 // 65535.
9440 if (Subtarget->hasV6T2Ops())
9441 if (CVal >= 0 && CVal <= 65535)
9442 break;
9443 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009444 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009445 if (Subtarget->isThumb1Only()) {
9446 // This must be a constant between 0 and 255, for ADD
9447 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009448 if (CVal >= 0 && CVal <= 255)
9449 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009450 } else if (Subtarget->isThumb2()) {
9451 // A constant that can be used as an immediate value in a
9452 // data-processing instruction.
9453 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9454 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009455 } else {
9456 // A constant that can be used as an immediate value in a
9457 // data-processing instruction.
9458 if (ARM_AM::getSOImmVal(CVal) != -1)
9459 break;
9460 }
9461 return;
9462
9463 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009464 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009465 // This must be a constant between -255 and -1, for negated ADD
9466 // immediates. This can be used in GCC with an "n" modifier that
9467 // prints the negated value, for use with SUB instructions. It is
9468 // not useful otherwise but is implemented for compatibility.
9469 if (CVal >= -255 && CVal <= -1)
9470 break;
9471 } else {
9472 // This must be a constant between -4095 and 4095. It is not clear
9473 // what this constraint is intended for. Implemented for
9474 // compatibility with GCC.
9475 if (CVal >= -4095 && CVal <= 4095)
9476 break;
9477 }
9478 return;
9479
9480 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009481 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009482 // A 32-bit value where only one byte has a nonzero value. Exclude
9483 // zero to match GCC. This constraint is used by GCC internally for
9484 // constants that can be loaded with a move/shift combination.
9485 // It is not useful otherwise but is implemented for compatibility.
9486 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9487 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009488 } else if (Subtarget->isThumb2()) {
9489 // A constant whose bitwise inverse can be used as an immediate
9490 // value in a data-processing instruction. This can be used in GCC
9491 // with a "B" modifier that prints the inverted value, for use with
9492 // BIC and MVN instructions. It is not useful otherwise but is
9493 // implemented for compatibility.
9494 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9495 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009496 } else {
9497 // A constant whose bitwise inverse can be used as an immediate
9498 // value in a data-processing instruction. This can be used in GCC
9499 // with a "B" modifier that prints the inverted value, for use with
9500 // BIC and MVN instructions. It is not useful otherwise but is
9501 // implemented for compatibility.
9502 if (ARM_AM::getSOImmVal(~CVal) != -1)
9503 break;
9504 }
9505 return;
9506
9507 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009508 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009509 // This must be a constant between -7 and 7,
9510 // for 3-operand ADD/SUB immediate instructions.
9511 if (CVal >= -7 && CVal < 7)
9512 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009513 } else if (Subtarget->isThumb2()) {
9514 // A constant whose negation can be used as an immediate value in a
9515 // data-processing instruction. This can be used in GCC with an "n"
9516 // modifier that prints the negated value, for use with SUB
9517 // instructions. It is not useful otherwise but is implemented for
9518 // compatibility.
9519 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9520 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009521 } else {
9522 // A constant whose negation can be used as an immediate value in a
9523 // data-processing instruction. This can be used in GCC with an "n"
9524 // modifier that prints the negated value, for use with SUB
9525 // instructions. It is not useful otherwise but is implemented for
9526 // compatibility.
9527 if (ARM_AM::getSOImmVal(-CVal) != -1)
9528 break;
9529 }
9530 return;
9531
9532 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009533 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009534 // This must be a multiple of 4 between 0 and 1020, for
9535 // ADD sp + immediate.
9536 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9537 break;
9538 } else {
9539 // A power of two or a constant between 0 and 32. This is used in
9540 // GCC for the shift amount on shifted register operands, but it is
9541 // useful in general for any shift amounts.
9542 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9543 break;
9544 }
9545 return;
9546
9547 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009548 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009549 // This must be a constant between 0 and 31, for shift amounts.
9550 if (CVal >= 0 && CVal <= 31)
9551 break;
9552 }
9553 return;
9554
9555 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009556 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009557 // This must be a multiple of 4 between -508 and 508, for
9558 // ADD/SUB sp = sp + immediate.
9559 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9560 break;
9561 }
9562 return;
9563 }
9564 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9565 break;
9566 }
9567
9568 if (Result.getNode()) {
9569 Ops.push_back(Result);
9570 return;
9571 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009572 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009573}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009574
9575bool
9576ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9577 // The ARM target isn't yet aware of offsets.
9578 return false;
9579}
Evan Cheng39382422009-10-28 01:44:26 +00009580
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009581bool ARM::isBitFieldInvertedMask(unsigned v) {
9582 if (v == 0xffffffff)
9583 return 0;
9584 // there can be 1's on either or both "outsides", all the "inside"
9585 // bits must be 0's
9586 unsigned int lsb = 0, msb = 31;
9587 while (v & (1 << msb)) --msb;
9588 while (v & (1 << lsb)) ++lsb;
9589 for (unsigned int i = lsb; i <= msb; ++i) {
9590 if (v & (1 << i))
9591 return 0;
9592 }
9593 return 1;
9594}
9595
Evan Cheng39382422009-10-28 01:44:26 +00009596/// isFPImmLegal - Returns true if the target can instruction select the
9597/// specified FP immediate natively. If false, the legalizer will
9598/// materialize the FP immediate as a load from a constant pool.
9599bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9600 if (!Subtarget->hasVFP3())
9601 return false;
9602 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009603 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009604 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009605 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009606 return false;
9607}
Bob Wilson65ffec42010-09-21 17:56:22 +00009608
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009609/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009610/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9611/// specified in the intrinsic calls.
9612bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9613 const CallInst &I,
9614 unsigned Intrinsic) const {
9615 switch (Intrinsic) {
9616 case Intrinsic::arm_neon_vld1:
9617 case Intrinsic::arm_neon_vld2:
9618 case Intrinsic::arm_neon_vld3:
9619 case Intrinsic::arm_neon_vld4:
9620 case Intrinsic::arm_neon_vld2lane:
9621 case Intrinsic::arm_neon_vld3lane:
9622 case Intrinsic::arm_neon_vld4lane: {
9623 Info.opc = ISD::INTRINSIC_W_CHAIN;
9624 // Conservatively set memVT to the entire set of vectors loaded.
9625 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9626 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9627 Info.ptrVal = I.getArgOperand(0);
9628 Info.offset = 0;
9629 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9630 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9631 Info.vol = false; // volatile loads with NEON intrinsics not supported
9632 Info.readMem = true;
9633 Info.writeMem = false;
9634 return true;
9635 }
9636 case Intrinsic::arm_neon_vst1:
9637 case Intrinsic::arm_neon_vst2:
9638 case Intrinsic::arm_neon_vst3:
9639 case Intrinsic::arm_neon_vst4:
9640 case Intrinsic::arm_neon_vst2lane:
9641 case Intrinsic::arm_neon_vst3lane:
9642 case Intrinsic::arm_neon_vst4lane: {
9643 Info.opc = ISD::INTRINSIC_VOID;
9644 // Conservatively set memVT to the entire set of vectors stored.
9645 unsigned NumElts = 0;
9646 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009647 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009648 if (!ArgTy->isVectorTy())
9649 break;
9650 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9651 }
9652 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9653 Info.ptrVal = I.getArgOperand(0);
9654 Info.offset = 0;
9655 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9656 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9657 Info.vol = false; // volatile stores with NEON intrinsics not supported
9658 Info.readMem = false;
9659 Info.writeMem = true;
9660 return true;
9661 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009662 case Intrinsic::arm_strexd: {
9663 Info.opc = ISD::INTRINSIC_W_CHAIN;
9664 Info.memVT = MVT::i64;
9665 Info.ptrVal = I.getArgOperand(2);
9666 Info.offset = 0;
9667 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009668 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009669 Info.readMem = false;
9670 Info.writeMem = true;
9671 return true;
9672 }
9673 case Intrinsic::arm_ldrexd: {
9674 Info.opc = ISD::INTRINSIC_W_CHAIN;
9675 Info.memVT = MVT::i64;
9676 Info.ptrVal = I.getArgOperand(0);
9677 Info.offset = 0;
9678 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009679 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009680 Info.readMem = true;
9681 Info.writeMem = false;
9682 return true;
9683 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009684 default:
9685 break;
9686 }
9687
9688 return false;
9689}