blob: 9d8b7dff108945c0cc3d4ac204137a6d3c49e2b4 [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 *
1044ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1045 return ARM::createFastISel(funcInfo);
1046}
1047
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001048/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1049/// be used for loads / stores from the global.
1050unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1051 return (Subtarget->isThumb1Only() ? 127 : 4095);
1052}
1053
Evan Cheng1cc39842010-05-20 23:26:43 +00001054Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001055 unsigned NumVals = N->getNumValues();
1056 if (!NumVals)
1057 return Sched::RegPressure;
1058
1059 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001060 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001061 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001062 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001063 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001064 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001065 }
Evan Chengc10f5432010-05-28 23:25:23 +00001066
1067 if (!N->isMachineOpcode())
1068 return Sched::RegPressure;
1069
1070 // Load are scheduled for latency even if there instruction itinerary
1071 // is not available.
1072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001073 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001074
Evan Chenge837dea2011-06-28 19:10:37 +00001075 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001076 return Sched::RegPressure;
1077 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001078 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001079 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001080
Evan Cheng1cc39842010-05-20 23:26:43 +00001081 return Sched::RegPressure;
1082}
1083
Evan Chenga8e29892007-01-19 07:51:42 +00001084//===----------------------------------------------------------------------===//
1085// Lowering Code
1086//===----------------------------------------------------------------------===//
1087
Evan Chenga8e29892007-01-19 07:51:42 +00001088/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1089static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1090 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001091 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001092 case ISD::SETNE: return ARMCC::NE;
1093 case ISD::SETEQ: return ARMCC::EQ;
1094 case ISD::SETGT: return ARMCC::GT;
1095 case ISD::SETGE: return ARMCC::GE;
1096 case ISD::SETLT: return ARMCC::LT;
1097 case ISD::SETLE: return ARMCC::LE;
1098 case ISD::SETUGT: return ARMCC::HI;
1099 case ISD::SETUGE: return ARMCC::HS;
1100 case ISD::SETULT: return ARMCC::LO;
1101 case ISD::SETULE: return ARMCC::LS;
1102 }
1103}
1104
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001105/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1106static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001107 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001108 CondCode2 = ARMCC::AL;
1109 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001110 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001111 case ISD::SETEQ:
1112 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1113 case ISD::SETGT:
1114 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1115 case ISD::SETGE:
1116 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1117 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001118 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001119 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1120 case ISD::SETO: CondCode = ARMCC::VC; break;
1121 case ISD::SETUO: CondCode = ARMCC::VS; break;
1122 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1123 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1124 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1125 case ISD::SETLT:
1126 case ISD::SETULT: CondCode = ARMCC::LT; break;
1127 case ISD::SETLE:
1128 case ISD::SETULE: CondCode = ARMCC::LE; break;
1129 case ISD::SETNE:
1130 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1131 }
Evan Chenga8e29892007-01-19 07:51:42 +00001132}
1133
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134//===----------------------------------------------------------------------===//
1135// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136//===----------------------------------------------------------------------===//
1137
1138#include "ARMGenCallingConv.inc"
1139
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001140/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1141/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001142CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001143 bool Return,
1144 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001145 switch (CC) {
1146 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001147 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001148 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001149 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001150 if (!Subtarget->isAAPCS_ABI())
1151 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1152 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1153 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1154 }
1155 // Fallthrough
1156 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001157 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001158 if (!Subtarget->isAAPCS_ABI())
1159 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1160 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001161 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1162 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001163 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1164 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1165 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001166 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001167 if (!isVarArg)
1168 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1169 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001170 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001171 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001172 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001173 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001174 }
1175}
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177/// LowerCallResult - Lower the result values of a call into the
1178/// appropriate copies out of appropriate physical registers.
1179SDValue
1180ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001181 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182 const SmallVectorImpl<ISD::InputArg> &Ins,
1183 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001184 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185
Bob Wilson1f595bb2009-04-17 19:07:39 +00001186 // Assign locations to each value returned by this call.
1187 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001188 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1189 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001191 CCAssignFnForNode(CallConv, /* Return*/ true,
1192 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193
1194 // Copy all of the result registers out of their specified physreg.
1195 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1196 CCValAssign VA = RVLocs[i];
1197
Bob Wilson80915242009-04-25 00:33:20 +00001198 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001199 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001200 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001201 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001203 Chain = Lo.getValue(1);
1204 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001207 InFlag);
1208 Chain = Hi.getValue(1);
1209 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001210 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001211
Owen Anderson825b72b2009-08-11 20:47:22 +00001212 if (VA.getLocVT() == MVT::v2f64) {
1213 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1214 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1215 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001216
1217 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001219 Chain = Lo.getValue(1);
1220 InFlag = Lo.getValue(2);
1221 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 Chain = Hi.getValue(1);
1224 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001225 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1227 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001228 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001230 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1231 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001232 Chain = Val.getValue(1);
1233 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 }
Bob Wilson80915242009-04-25 00:33:20 +00001235
1236 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001237 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001238 case CCValAssign::Full: break;
1239 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001240 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001241 break;
1242 }
1243
Dan Gohman98ca4f22009-08-05 01:29:28 +00001244 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 }
1246
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248}
1249
Bob Wilsondee46d72009-04-17 20:35:10 +00001250/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1253 SDValue StackPtr, SDValue Arg,
1254 DebugLoc dl, SelectionDAG &DAG,
1255 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001256 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 unsigned LocMemOffset = VA.getLocMemOffset();
1258 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1259 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001261 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001262 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001263}
1264
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001266 SDValue Chain, SDValue &Arg,
1267 RegsToPassVector &RegsToPass,
1268 CCValAssign &VA, CCValAssign &NextVA,
1269 SDValue &StackPtr,
1270 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001271 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001272
Jim Grosbache5165492009-11-09 00:11:35 +00001273 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1276
1277 if (NextVA.isRegLoc())
1278 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1279 else {
1280 assert(NextVA.isMemLoc());
1281 if (StackPtr.getNode() == 0)
1282 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1283
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1285 dl, DAG, NextVA,
1286 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001287 }
1288}
1289
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001291/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1292/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001294ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001295 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001296 SelectionDAG &DAG = CLI.DAG;
1297 DebugLoc &dl = CLI.DL;
1298 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1299 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1300 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1301 SDValue Chain = CLI.Chain;
1302 SDValue Callee = CLI.Callee;
1303 bool &isTailCall = CLI.IsTailCall;
1304 CallingConv::ID CallConv = CLI.CallConv;
1305 bool doesNotRet = CLI.DoesNotReturn;
1306 bool isVarArg = CLI.IsVarArg;
1307
Dale Johannesen51e28e62010-06-03 21:09:53 +00001308 MachineFunction &MF = DAG.getMachineFunction();
1309 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1310 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001311 // Disable tail calls if they're not supported.
1312 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001313 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001314 if (isTailCall) {
1315 // Check if it's really possible to do a tail call.
1316 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1317 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001318 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001319 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1320 // detected sibcalls.
1321 if (isTailCall) {
1322 ++NumTailCalls;
1323 IsSibCall = true;
1324 }
1325 }
Evan Chenga8e29892007-01-19 07:51:42 +00001326
Bob Wilson1f595bb2009-04-17 19:07:39 +00001327 // Analyze operands of the call, assigning locations to each operand.
1328 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001329 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1330 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001331 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001332 CCAssignFnForNode(CallConv, /* Return*/ false,
1333 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001334
Bob Wilson1f595bb2009-04-17 19:07:39 +00001335 // Get a count of how many bytes are to be pushed on the stack.
1336 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001337
Dale Johannesen51e28e62010-06-03 21:09:53 +00001338 // For tail calls, memory operands are available in our caller's stack.
1339 if (IsSibCall)
1340 NumBytes = 0;
1341
Evan Chenga8e29892007-01-19 07:51:42 +00001342 // Adjust the stack pointer for the new arguments...
1343 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001344 if (!IsSibCall)
1345 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001346
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001347 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001348
Bob Wilson5bafff32009-06-22 23:27:02 +00001349 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001351
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001353 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001354 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1355 i != e;
1356 ++i, ++realArgIdx) {
1357 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001358 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001360 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001361
Bob Wilson1f595bb2009-04-17 19:07:39 +00001362 // Promote the value if needed.
1363 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001364 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001365 case CCValAssign::Full: break;
1366 case CCValAssign::SExt:
1367 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1368 break;
1369 case CCValAssign::ZExt:
1370 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1371 break;
1372 case CCValAssign::AExt:
1373 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1374 break;
1375 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001376 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001377 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001378 }
1379
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001380 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001381 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001382 if (VA.getLocVT() == MVT::v2f64) {
1383 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1384 DAG.getConstant(0, MVT::i32));
1385 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1386 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001387
Dan Gohman98ca4f22009-08-05 01:29:28 +00001388 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001389 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1390
1391 VA = ArgLocs[++i]; // skip ahead to next loc
1392 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001393 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001394 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1395 } else {
1396 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001397
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1399 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001400 }
1401 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001403 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001404 }
1405 } else if (VA.isRegLoc()) {
1406 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001407 } else if (isByVal) {
1408 assert(VA.isMemLoc());
1409 unsigned offset = 0;
1410
1411 // True if this byval aggregate will be split between registers
1412 // and memory.
1413 if (CCInfo.isFirstByValRegValid()) {
1414 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1415 unsigned int i, j;
1416 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1417 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1418 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1419 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1420 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001421 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001422 MemOpChains.push_back(Load.getValue(1));
1423 RegsToPass.push_back(std::make_pair(j, Load));
1424 }
1425 offset = ARM::R4 - CCInfo.getFirstByValReg();
1426 CCInfo.clearFirstByValReg();
1427 }
1428
Manman Ren763a75d2012-06-01 02:44:42 +00001429 if (Flags.getByValSize() - 4*offset > 0) {
1430 unsigned LocMemOffset = VA.getLocMemOffset();
1431 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1432 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1433 StkPtrOff);
1434 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1435 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1436 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1437 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001438 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001439
Manman Ren763a75d2012-06-01 02:44:42 +00001440 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001441 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001442 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1443 Ops, array_lengthof(Ops)));
1444 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001445 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001446 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001447
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1449 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001450 }
Evan Chenga8e29892007-01-19 07:51:42 +00001451 }
1452
1453 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001454 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001455 &MemOpChains[0], MemOpChains.size());
1456
1457 // Build a sequence of copy-to-reg nodes chained together with token chain
1458 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001460 // Tail call byval lowering might overwrite argument registers so in case of
1461 // tail call optimization the copies to registers are lowered later.
1462 if (!isTailCall)
1463 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1464 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1465 RegsToPass[i].second, InFlag);
1466 InFlag = Chain.getValue(1);
1467 }
Evan Chenga8e29892007-01-19 07:51:42 +00001468
Dale Johannesen51e28e62010-06-03 21:09:53 +00001469 // For tail calls lower the arguments to the 'real' stack slot.
1470 if (isTailCall) {
1471 // Force all the incoming stack arguments to be loaded from the stack
1472 // before any new outgoing arguments are stored to the stack, because the
1473 // outgoing stack slots may alias the incoming argument stack slots, and
1474 // the alias isn't otherwise explicit. This is slightly more conservative
1475 // than necessary, because it means that each store effectively depends
1476 // on every argument instead of just those arguments it would clobber.
1477
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001478 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001479 InFlag = SDValue();
1480 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1481 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1482 RegsToPass[i].second, InFlag);
1483 InFlag = Chain.getValue(1);
1484 }
1485 InFlag =SDValue();
1486 }
1487
Bill Wendling056292f2008-09-16 21:48:12 +00001488 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1489 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1490 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001491 bool isDirect = false;
1492 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001493 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001494 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001495
1496 if (EnableARMLongCalls) {
1497 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1498 && "long-calls with non-static relocation model!");
1499 // Handle a global address or an external symbol. If it's not one of
1500 // those, the target's already in a register, so we don't need to do
1501 // anything extra.
1502 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001503 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001504 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001505 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001506 ARMConstantPoolValue *CPV =
1507 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1508
Jim Grosbache7b52522010-04-14 22:28:31 +00001509 // Get the address of the callee into a register
1510 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1511 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1512 Callee = DAG.getLoad(getPointerTy(), dl,
1513 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001514 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001515 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001516 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1517 const char *Sym = S->getSymbol();
1518
1519 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001520 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001521 ARMConstantPoolValue *CPV =
1522 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1523 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001524 // Get the address of the callee into a register
1525 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1526 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1527 Callee = DAG.getLoad(getPointerTy(), dl,
1528 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001529 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001530 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001531 }
1532 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001533 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001534 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001535 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001536 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001537 getTargetMachine().getRelocationModel() != Reloc::Static;
1538 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001539 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001540 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001541 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001542 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001543 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001544 ARMConstantPoolValue *CPV =
1545 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001546 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001548 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001549 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001550 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001551 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001552 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001553 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001554 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001555 } else {
1556 // On ELF targets for PIC code, direct calls should go through the PLT
1557 unsigned OpFlags = 0;
1558 if (Subtarget->isTargetELF() &&
1559 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1560 OpFlags = ARMII::MO_PLT;
1561 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1562 }
Bill Wendling056292f2008-09-16 21:48:12 +00001563 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001564 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001565 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001566 getTargetMachine().getRelocationModel() != Reloc::Static;
1567 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001568 // tBX takes a register source operand.
1569 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001570 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001571 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001572 ARMConstantPoolValue *CPV =
1573 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1574 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001575 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001577 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001578 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001579 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001580 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001581 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001582 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001583 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001584 } else {
1585 unsigned OpFlags = 0;
1586 // On ELF targets for PIC code, direct calls should go through the PLT
1587 if (Subtarget->isTargetELF() &&
1588 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1589 OpFlags = ARMII::MO_PLT;
1590 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1591 }
Evan Chenga8e29892007-01-19 07:51:42 +00001592 }
1593
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001594 // FIXME: handle tail calls differently.
1595 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001596 if (Subtarget->isThumb()) {
1597 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001598 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001599 else if (doesNotRet && isDirect && !isARMFunc &&
1600 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1601 // "mov lr, pc; b _foo" to avoid confusing the RSP
1602 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001603 else
1604 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1605 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001606 if (!isDirect && !Subtarget->hasV5TOps()) {
1607 CallOpc = ARMISD::CALL_NOLINK;
1608 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1609 // "mov lr, pc; b _foo" to avoid confusing the RSP
1610 CallOpc = ARMISD::CALL_NOLINK;
1611 else
1612 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001613 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001614
Dan Gohman475871a2008-07-27 21:46:04 +00001615 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001616 Ops.push_back(Chain);
1617 Ops.push_back(Callee);
1618
1619 // Add argument registers to the end of the list so that they are known live
1620 // into the call.
1621 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1622 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1623 RegsToPass[i].second.getValueType()));
1624
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001625 // Add a register mask operand representing the call-preserved registers.
1626 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1627 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1628 assert(Mask && "Missing call preserved mask for calling convention");
1629 Ops.push_back(DAG.getRegisterMask(Mask));
1630
Gabor Greifba36cb52008-08-28 21:40:38 +00001631 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001632 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001633
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001634 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001635 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001637
Duncan Sands4bdcb612008-07-02 17:40:58 +00001638 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001639 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001640 InFlag = Chain.getValue(1);
1641
Chris Lattnere563bbc2008-10-11 22:08:30 +00001642 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1643 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001645 InFlag = Chain.getValue(1);
1646
Bob Wilson1f595bb2009-04-17 19:07:39 +00001647 // Handle result values, copying them out of physregs into vregs that we
1648 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1650 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001651}
1652
Stuart Hastingsf222e592011-02-28 17:17:53 +00001653/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001654/// on the stack. Remember the next parameter register to allocate,
1655/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001656/// this.
1657void
Craig Topperc89c7442012-03-27 07:21:54 +00001658ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001659 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1660 assert((State->getCallOrPrologue() == Prologue ||
1661 State->getCallOrPrologue() == Call) &&
1662 "unhandled ParmContext");
1663 if ((!State->isFirstByValRegValid()) &&
1664 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1665 State->setFirstByValReg(reg);
1666 // At a call site, a byval parameter that is split between
1667 // registers and memory needs its size truncated here. In a
1668 // function prologue, such byval parameters are reassembled in
1669 // memory, and are not truncated.
1670 if (State->getCallOrPrologue() == Call) {
1671 unsigned excess = 4 * (ARM::R4 - reg);
1672 assert(size >= excess && "expected larger existing stack allocation");
1673 size -= excess;
1674 }
1675 }
1676 // Confiscate any remaining parameter registers to preclude their
1677 // assignment to subsequent parameters.
1678 while (State->AllocateReg(GPRArgRegs, 4))
1679 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001680}
1681
Dale Johannesen51e28e62010-06-03 21:09:53 +00001682/// MatchingStackOffset - Return true if the given stack call argument is
1683/// already available in the same position (relatively) of the caller's
1684/// incoming argument stack.
1685static
1686bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1687 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001688 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1690 int FI = INT_MAX;
1691 if (Arg.getOpcode() == ISD::CopyFromReg) {
1692 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001693 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 return false;
1695 MachineInstr *Def = MRI->getVRegDef(VR);
1696 if (!Def)
1697 return false;
1698 if (!Flags.isByVal()) {
1699 if (!TII->isLoadFromStackSlot(Def, FI))
1700 return false;
1701 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001702 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001703 }
1704 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1705 if (Flags.isByVal())
1706 // ByVal argument is passed in as a pointer but it's now being
1707 // dereferenced. e.g.
1708 // define @foo(%struct.X* %A) {
1709 // tail call @bar(%struct.X* byval %A)
1710 // }
1711 return false;
1712 SDValue Ptr = Ld->getBasePtr();
1713 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1714 if (!FINode)
1715 return false;
1716 FI = FINode->getIndex();
1717 } else
1718 return false;
1719
1720 assert(FI != INT_MAX);
1721 if (!MFI->isFixedObjectIndex(FI))
1722 return false;
1723 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1724}
1725
1726/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1727/// for tail call optimization. Targets which want to do tail call
1728/// optimization should implement this function.
1729bool
1730ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1731 CallingConv::ID CalleeCC,
1732 bool isVarArg,
1733 bool isCalleeStructRet,
1734 bool isCallerStructRet,
1735 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001736 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001737 const SmallVectorImpl<ISD::InputArg> &Ins,
1738 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001739 const Function *CallerF = DAG.getMachineFunction().getFunction();
1740 CallingConv::ID CallerCC = CallerF->getCallingConv();
1741 bool CCMatch = CallerCC == CalleeCC;
1742
1743 // Look for obvious safe cases to perform tail call optimization that do not
1744 // require ABI changes. This is what gcc calls sibcall.
1745
Jim Grosbach7616b642010-06-16 23:45:49 +00001746 // Do not sibcall optimize vararg calls unless the call site is not passing
1747 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001748 if (isVarArg && !Outs.empty())
1749 return false;
1750
1751 // Also avoid sibcall optimization if either caller or callee uses struct
1752 // return semantics.
1753 if (isCalleeStructRet || isCallerStructRet)
1754 return false;
1755
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001756 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001757 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1758 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1759 // support in the assembler and linker to be used. This would need to be
1760 // fixed to fully support tail calls in Thumb1.
1761 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001762 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1763 // LR. This means if we need to reload LR, it takes an extra instructions,
1764 // which outweighs the value of the tail call; but here we don't know yet
1765 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001766 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001767 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001768
1769 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1770 // but we need to make sure there are enough registers; the only valid
1771 // registers are the 4 used for parameters. We don't currently do this
1772 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001773 if (Subtarget->isThumb1Only())
1774 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001775
Dale Johannesen51e28e62010-06-03 21:09:53 +00001776 // If the calling conventions do not match, then we'd better make sure the
1777 // results are returned in the same way as what the caller expects.
1778 if (!CCMatch) {
1779 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001780 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1781 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001782 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1783
1784 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001785 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1786 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001787 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1788
1789 if (RVLocs1.size() != RVLocs2.size())
1790 return false;
1791 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1792 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1793 return false;
1794 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1795 return false;
1796 if (RVLocs1[i].isRegLoc()) {
1797 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1798 return false;
1799 } else {
1800 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1801 return false;
1802 }
1803 }
1804 }
1805
1806 // If the callee takes no arguments then go on to check the results of the
1807 // call.
1808 if (!Outs.empty()) {
1809 // Check if stack adjustment is needed. For now, do not do this if any
1810 // argument is passed on the stack.
1811 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001812 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1813 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001814 CCInfo.AnalyzeCallOperands(Outs,
1815 CCAssignFnForNode(CalleeCC, false, isVarArg));
1816 if (CCInfo.getNextStackOffset()) {
1817 MachineFunction &MF = DAG.getMachineFunction();
1818
1819 // Check if the arguments are already laid out in the right way as
1820 // the caller's fixed stack objects.
1821 MachineFrameInfo *MFI = MF.getFrameInfo();
1822 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001823 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001824 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1825 i != e;
1826 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001827 CCValAssign &VA = ArgLocs[i];
1828 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001829 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001830 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001831 if (VA.getLocInfo() == CCValAssign::Indirect)
1832 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001833 if (VA.needsCustom()) {
1834 // f64 and vector types are split into multiple registers or
1835 // register/stack-slot combinations. The types will not match
1836 // the registers; give up on memory f64 refs until we figure
1837 // out what to do about this.
1838 if (!VA.isRegLoc())
1839 return false;
1840 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001841 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001842 if (RegVT == MVT::v2f64) {
1843 if (!ArgLocs[++i].isRegLoc())
1844 return false;
1845 if (!ArgLocs[++i].isRegLoc())
1846 return false;
1847 }
1848 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001849 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1850 MFI, MRI, TII))
1851 return false;
1852 }
1853 }
1854 }
1855 }
1856
1857 return true;
1858}
1859
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860SDValue
1861ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001862 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001864 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001865 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001866
Bob Wilsondee46d72009-04-17 20:35:10 +00001867 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001868 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001869
Bob Wilsondee46d72009-04-17 20:35:10 +00001870 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001871 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1872 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001873
Dan Gohman98ca4f22009-08-05 01:29:28 +00001874 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001875 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1876 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001877
1878 // If this is the first return lowered for this function, add
1879 // the regs to the liveout set for the function.
1880 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1881 for (unsigned i = 0; i != RVLocs.size(); ++i)
1882 if (RVLocs[i].isRegLoc())
1883 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001884 }
1885
Bob Wilson1f595bb2009-04-17 19:07:39 +00001886 SDValue Flag;
1887
1888 // Copy the result values into the output registers.
1889 for (unsigned i = 0, realRVLocIdx = 0;
1890 i != RVLocs.size();
1891 ++i, ++realRVLocIdx) {
1892 CCValAssign &VA = RVLocs[i];
1893 assert(VA.isRegLoc() && "Can only return in registers!");
1894
Dan Gohmanc9403652010-07-07 15:54:55 +00001895 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001896
1897 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001898 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001899 case CCValAssign::Full: break;
1900 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001901 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001902 break;
1903 }
1904
Bob Wilson1f595bb2009-04-17 19:07:39 +00001905 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001907 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1909 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001910 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001912
1913 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1914 Flag = Chain.getValue(1);
1915 VA = RVLocs[++i]; // skip ahead to next loc
1916 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1917 HalfGPRs.getValue(1), Flag);
1918 Flag = Chain.getValue(1);
1919 VA = RVLocs[++i]; // skip ahead to next loc
1920
1921 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1923 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001924 }
1925 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1926 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001927 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001929 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001930 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001931 VA = RVLocs[++i]; // skip ahead to next loc
1932 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1933 Flag);
1934 } else
1935 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1936
Bob Wilsondee46d72009-04-17 20:35:10 +00001937 // Guarantee that all emitted copies are
1938 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001939 Flag = Chain.getValue(1);
1940 }
1941
1942 SDValue result;
1943 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001945 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001947
1948 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001949}
1950
Evan Chengbf010eb2012-04-10 01:51:00 +00001951bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001952 if (N->getNumValues() != 1)
1953 return false;
1954 if (!N->hasNUsesOfValue(1, 0))
1955 return false;
1956
Evan Chengbf010eb2012-04-10 01:51:00 +00001957 SDValue TCChain = Chain;
1958 SDNode *Copy = *N->use_begin();
1959 if (Copy->getOpcode() == ISD::CopyToReg) {
1960 // If the copy has a glue operand, we conservatively assume it isn't safe to
1961 // perform a tail call.
1962 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1963 return false;
1964 TCChain = Copy->getOperand(0);
1965 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1966 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001967 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001968 SmallPtrSet<SDNode*, 2> Copies;
1969 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001970 UI != UE; ++UI) {
1971 if (UI->getOpcode() != ISD::CopyToReg)
1972 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001973 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001974 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001975 if (Copies.size() > 2)
1976 return false;
1977
1978 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1979 UI != UE; ++UI) {
1980 SDValue UseChain = UI->getOperand(0);
1981 if (Copies.count(UseChain.getNode()))
1982 // Second CopyToReg
1983 Copy = *UI;
1984 else
1985 // First CopyToReg
1986 TCChain = UseChain;
1987 }
1988 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001989 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001990 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001991 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001992 Copy = *Copy->use_begin();
1993 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001995 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001996 } else {
1997 return false;
1998 }
1999
Evan Cheng1bf891a2010-12-01 22:59:46 +00002000 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002001 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2002 UI != UE; ++UI) {
2003 if (UI->getOpcode() != ARMISD::RET_FLAG)
2004 return false;
2005 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002006 }
2007
Evan Chengbf010eb2012-04-10 01:51:00 +00002008 if (!HasRet)
2009 return false;
2010
2011 Chain = TCChain;
2012 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002013}
2014
Evan Cheng485fafc2011-03-21 01:19:09 +00002015bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002016 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002017 return false;
2018
2019 if (!CI->isTailCall())
2020 return false;
2021
2022 return !Subtarget->isThumb1Only();
2023}
2024
Bob Wilsonb62d2572009-11-03 00:02:05 +00002025// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2026// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2027// one of the above mentioned nodes. It has to be wrapped because otherwise
2028// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2029// be used to form addressing mode. These wrapped nodes will be selected
2030// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002031static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002032 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002033 // FIXME there is no actual debug info here
2034 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002035 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002037 if (CP->isMachineConstantPoolEntry())
2038 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2039 CP->getAlignment());
2040 else
2041 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2042 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002044}
2045
Jim Grosbache1102ca2010-07-19 17:20:38 +00002046unsigned ARMTargetLowering::getJumpTableEncoding() const {
2047 return MachineJumpTableInfo::EK_Inline;
2048}
2049
Dan Gohmand858e902010-04-17 15:26:15 +00002050SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2051 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002052 MachineFunction &MF = DAG.getMachineFunction();
2053 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2054 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002055 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002056 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002057 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002058 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2059 SDValue CPAddr;
2060 if (RelocM == Reloc::Static) {
2061 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2062 } else {
2063 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002064 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002065 ARMConstantPoolValue *CPV =
2066 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2067 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002068 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2069 }
2070 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2071 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002072 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002073 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002074 if (RelocM == Reloc::Static)
2075 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002076 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002077 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002078}
2079
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002080// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002081SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002082ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002083 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002084 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002085 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002086 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002087 MachineFunction &MF = DAG.getMachineFunction();
2088 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002089 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002090 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002091 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2092 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002093 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002095 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002096 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002097 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002098 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002099
Evan Chenge7e0d622009-11-06 22:24:13 +00002100 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002101 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002102
2103 // call __tls_get_addr.
2104 ArgListTy Args;
2105 ArgListEntry Entry;
2106 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002107 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002108 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002109 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002110 TargetLowering::CallLoweringInfo CLI(Chain,
2111 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002112 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002113 0, CallingConv::C, /*isTailCall=*/false,
2114 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002115 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002116 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002117 return CallResult.first;
2118}
2119
2120// Lower ISD::GlobalTLSAddress using the "initial exec" or
2121// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002122SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002123ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002124 SelectionDAG &DAG,
2125 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002126 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002127 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue Offset;
2129 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002130 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002131 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002132 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002134 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002135 MachineFunction &MF = DAG.getMachineFunction();
2136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002137 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002139 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2140 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002141 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2142 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2143 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002144 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002146 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002147 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002148 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002149 Chain = Offset.getValue(1);
2150
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002153
Evan Cheng9eda6892009-10-31 03:39:36 +00002154 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002155 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002156 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002157 } else {
2158 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002159 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002160 ARMConstantPoolValue *CPV =
2161 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002162 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002164 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002165 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002166 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002167 }
2168
2169 // The address of the thread local variable is the add of the thread
2170 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002171 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002172}
2173
Dan Gohman475871a2008-07-27 21:46:04 +00002174SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002175ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002176 // TODO: implement the "local dynamic" model
2177 assert(Subtarget->isTargetELF() &&
2178 "TLS not implemented for non-ELF targets");
2179 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002180
2181 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2182
2183 switch (model) {
2184 case TLSModel::GeneralDynamic:
2185 case TLSModel::LocalDynamic:
2186 return LowerToTLSGeneralDynamicModel(GA, DAG);
2187 case TLSModel::InitialExec:
2188 case TLSModel::LocalExec:
2189 return LowerToTLSExecModels(GA, DAG, model);
2190 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002191 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002192}
2193
Dan Gohman475871a2008-07-27 21:46:04 +00002194SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002195 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002196 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002197 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002198 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002199 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2200 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002201 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002202 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002203 ARMConstantPoolConstant::Create(GV,
2204 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002205 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002207 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002208 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002209 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002210 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002211 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002212 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002213 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002214 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002215 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002216 MachinePointerInfo::getGOT(),
2217 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002218 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002219 }
2220
2221 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002222 // pair. This is always cheaper.
2223 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002224 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002225 // FIXME: Once remat is capable of dealing with instructions with register
2226 // operands, expand this into two nodes.
2227 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2228 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002229 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002230 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2231 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2232 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2233 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002234 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002235 }
2236}
2237
Dan Gohman475871a2008-07-27 21:46:04 +00002238SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002239 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002240 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002241 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002242 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002243 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002244 MachineFunction &MF = DAG.getMachineFunction();
2245 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2246
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002247 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2248 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002249 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002250 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002251 // FIXME: Once remat is capable of dealing with instructions with register
2252 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002253 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002254 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2255 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2256
Evan Cheng53519f02011-01-21 18:55:51 +00002257 unsigned Wrapper = (RelocM == Reloc::PIC_)
2258 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2259 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002260 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002261 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2262 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002263 MachinePointerInfo::getGOT(),
2264 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002265 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002266 }
2267
2268 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002269 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002270 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002271 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002272 } else {
2273 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002274 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2275 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002276 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2277 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002278 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002279 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002281
Evan Cheng9eda6892009-10-31 03:39:36 +00002282 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002283 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002284 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002285 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002286
2287 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002288 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002289 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002290 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002291
Evan Cheng63476a82009-09-03 07:04:02 +00002292 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002293 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002294 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002295
2296 return Result;
2297}
2298
Dan Gohman475871a2008-07-27 21:46:04 +00002299SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002300 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002301 assert(Subtarget->isTargetELF() &&
2302 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002303 MachineFunction &MF = DAG.getMachineFunction();
2304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002305 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002306 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002307 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002308 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002309 ARMConstantPoolValue *CPV =
2310 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2311 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002312 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002314 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002315 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002316 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002317 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002318 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002319}
2320
Jim Grosbach0e0da732009-05-12 23:59:14 +00002321SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002322ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2323 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002324 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002325 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2326 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002327 Op.getOperand(1), Val);
2328}
2329
2330SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002331ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2332 DebugLoc dl = Op.getDebugLoc();
2333 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2334 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2335}
2336
2337SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002338ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002339 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002340 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002341 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002342 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002343 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002344 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002345 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002346 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2347 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002348 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002349 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002350 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002351 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002352 EVT PtrVT = getPointerTy();
2353 DebugLoc dl = Op.getDebugLoc();
2354 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2355 SDValue CPAddr;
2356 unsigned PCAdj = (RelocM != Reloc::PIC_)
2357 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002358 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002359 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2360 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002361 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002362 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002363 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002364 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002365 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002366 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002367
2368 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002369 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002370 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2371 }
2372 return Result;
2373 }
Evan Cheng92e39162011-03-29 23:06:19 +00002374 case Intrinsic::arm_neon_vmulls:
2375 case Intrinsic::arm_neon_vmullu: {
2376 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2377 ? ARMISD::VMULLs : ARMISD::VMULLu;
2378 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2379 Op.getOperand(1), Op.getOperand(2));
2380 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002381 }
2382}
2383
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002384static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002385 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002386 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002387 if (!Subtarget->hasDataBarrier()) {
2388 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2389 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2390 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002391 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002392 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002393 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002394 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002395 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002396
2397 SDValue Op5 = Op.getOperand(5);
2398 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2399 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2400 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2401 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2402
2403 ARM_MB::MemBOpt DMBOpt;
2404 if (isDeviceBarrier)
2405 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2406 else
2407 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2408 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2409 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002410}
2411
Eli Friedman26689ac2011-08-03 21:06:02 +00002412
2413static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2414 const ARMSubtarget *Subtarget) {
2415 // FIXME: handle "fence singlethread" more efficiently.
2416 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002417 if (!Subtarget->hasDataBarrier()) {
2418 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2419 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2420 // here.
2421 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2422 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002423 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002424 DAG.getConstant(0, MVT::i32));
2425 }
2426
Eli Friedman26689ac2011-08-03 21:06:02 +00002427 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002428 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002429}
2430
Evan Chengdfed19f2010-11-03 06:34:55 +00002431static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2432 const ARMSubtarget *Subtarget) {
2433 // ARM pre v5TE and Thumb1 does not have preload instructions.
2434 if (!(Subtarget->isThumb2() ||
2435 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2436 // Just preserve the chain.
2437 return Op.getOperand(0);
2438
2439 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002440 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2441 if (!isRead &&
2442 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2443 // ARMv7 with MP extension has PLDW.
2444 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002445
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002446 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2447 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002448 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002449 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002450 isData = ~isData & 1;
2451 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002452
2453 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002454 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2455 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002456}
2457
Dan Gohman1e93df62010-04-17 14:41:14 +00002458static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2459 MachineFunction &MF = DAG.getMachineFunction();
2460 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2461
Evan Chenga8e29892007-01-19 07:51:42 +00002462 // vastart just stores the address of the VarArgsFrameIndex slot into the
2463 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002464 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002465 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002466 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002467 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002468 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2469 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002470}
2471
Dan Gohman475871a2008-07-27 21:46:04 +00002472SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002473ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2474 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002475 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 MachineFunction &MF = DAG.getMachineFunction();
2477 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2478
Craig Topper44d23822012-02-22 05:59:10 +00002479 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002480 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002481 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002482 else
Craig Topper420761a2012-04-20 07:30:17 +00002483 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002484
2485 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002486 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002487 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002488
2489 SDValue ArgValue2;
2490 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002491 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002492 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002493
2494 // Create load node to retrieve arguments from the stack.
2495 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002496 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002497 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002498 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002499 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002500 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002501 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002502 }
2503
Jim Grosbache5165492009-11-09 00:11:35 +00002504 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002505}
2506
Stuart Hastingsc7315872011-04-20 16:47:52 +00002507void
2508ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2509 unsigned &VARegSize, unsigned &VARegSaveSize)
2510 const {
2511 unsigned NumGPRs;
2512 if (CCInfo.isFirstByValRegValid())
2513 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2514 else {
2515 unsigned int firstUnalloced;
2516 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2517 sizeof(GPRArgRegs) /
2518 sizeof(GPRArgRegs[0]));
2519 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2520 }
2521
2522 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2523 VARegSize = NumGPRs * 4;
2524 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2525}
2526
2527// The remaining GPRs hold either the beginning of variable-argument
2528// data, or the beginning of an aggregate passed by value (usuall
2529// byval). Either way, we allocate stack slots adjacent to the data
2530// provided by our caller, and store the unallocated registers there.
2531// If this is a variadic function, the va_list pointer will begin with
2532// these values; otherwise, this reassembles a (byval) structure that
2533// was split between registers and memory.
2534void
2535ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2536 DebugLoc dl, SDValue &Chain,
2537 unsigned ArgOffset) const {
2538 MachineFunction &MF = DAG.getMachineFunction();
2539 MachineFrameInfo *MFI = MF.getFrameInfo();
2540 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2541 unsigned firstRegToSaveIndex;
2542 if (CCInfo.isFirstByValRegValid())
2543 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2544 else {
2545 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2546 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2547 }
2548
2549 unsigned VARegSize, VARegSaveSize;
2550 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2551 if (VARegSaveSize) {
2552 // If this function is vararg, store any remaining integer argument regs
2553 // to their spots on the stack so that they may be loaded by deferencing
2554 // the result of va_next.
2555 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002556 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2557 ArgOffset + VARegSaveSize
2558 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002559 false));
2560 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2561 getPointerTy());
2562
2563 SmallVector<SDValue, 4> MemOps;
2564 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002565 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002566 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002567 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002568 else
Craig Topper420761a2012-04-20 07:30:17 +00002569 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002570
2571 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2572 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2573 SDValue Store =
2574 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002575 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002576 false, false, 0);
2577 MemOps.push_back(Store);
2578 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2579 DAG.getConstant(4, getPointerTy()));
2580 }
2581 if (!MemOps.empty())
2582 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2583 &MemOps[0], MemOps.size());
2584 } else
2585 // This will point to the next argument passed via stack.
2586 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2587}
2588
Bob Wilson5bafff32009-06-22 23:27:02 +00002589SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002590ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002591 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002592 const SmallVectorImpl<ISD::InputArg>
2593 &Ins,
2594 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002595 SmallVectorImpl<SDValue> &InVals)
2596 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002597 MachineFunction &MF = DAG.getMachineFunction();
2598 MachineFrameInfo *MFI = MF.getFrameInfo();
2599
Bob Wilson1f595bb2009-04-17 19:07:39 +00002600 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2601
2602 // Assign locations to all of the incoming arguments.
2603 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002604 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2605 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002607 CCAssignFnForNode(CallConv, /* Return*/ false,
2608 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002609
2610 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002611 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002612
Stuart Hastingsf222e592011-02-28 17:17:53 +00002613 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002614 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2615 CCValAssign &VA = ArgLocs[i];
2616
Bob Wilsondee46d72009-04-17 20:35:10 +00002617 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002618 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002619 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620
Bob Wilson1f595bb2009-04-17 19:07:39 +00002621 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002622 // f64 and vector types are split up into multiple registers or
2623 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002624 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002625 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002626 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002627 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002628 SDValue ArgValue2;
2629 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002630 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002631 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2632 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002633 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002634 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002635 } else {
2636 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2637 Chain, DAG, dl);
2638 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002639 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2640 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002641 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002643 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2644 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002645 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002646
Bob Wilson5bafff32009-06-22 23:27:02 +00002647 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002648 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002649
Owen Anderson825b72b2009-08-11 20:47:22 +00002650 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002651 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002653 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002655 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002656 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002657 RC = AFI->isThumb1OnlyFunction() ?
2658 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2659 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002660 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002661 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002662
2663 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002664 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002666 }
2667
2668 // If this is an 8 or 16-bit value, it is really passed promoted
2669 // to 32 bits. Insert an assert[sz]ext to capture this, then
2670 // truncate to the right size.
2671 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002672 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002673 case CCValAssign::Full: break;
2674 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002675 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002676 break;
2677 case CCValAssign::SExt:
2678 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2679 DAG.getValueType(VA.getValVT()));
2680 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2681 break;
2682 case CCValAssign::ZExt:
2683 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2684 DAG.getValueType(VA.getValVT()));
2685 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2686 break;
2687 }
2688
Dan Gohman98ca4f22009-08-05 01:29:28 +00002689 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002690
2691 } else { // VA.isRegLoc()
2692
2693 // sanity check
2694 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002695 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002696
Stuart Hastingsf222e592011-02-28 17:17:53 +00002697 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002698
Stuart Hastingsf222e592011-02-28 17:17:53 +00002699 // Some Ins[] entries become multiple ArgLoc[] entries.
2700 // Process them only once.
2701 if (index != lastInsIndex)
2702 {
2703 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002704 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002705 // This can be changed with more analysis.
2706 // In case of tail call optimization mark all arguments mutable.
2707 // Since they could be overwritten by lowering of arguments in case of
2708 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002709 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002710 unsigned VARegSize, VARegSaveSize;
2711 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2712 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2713 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002714 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002715 int FI = MFI->CreateFixedObject(Bytes,
2716 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002717 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2718 } else {
2719 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2720 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002721
Stuart Hastingsf222e592011-02-28 17:17:53 +00002722 // Create load nodes to retrieve arguments from the stack.
2723 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2724 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2725 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002726 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002727 }
2728 lastInsIndex = index;
2729 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002730 }
2731 }
2732
2733 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002734 if (isVarArg)
2735 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002736
Dan Gohman98ca4f22009-08-05 01:29:28 +00002737 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002738}
2739
2740/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002741static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002742 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002743 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002744 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002745 // Maybe this has already been legalized into the constant pool?
2746 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002747 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002748 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002749 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002750 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002751 }
2752 }
2753 return false;
2754}
2755
Evan Chenga8e29892007-01-19 07:51:42 +00002756/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2757/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002758SDValue
2759ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002760 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002761 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002762 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002763 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002764 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002765 // Constant does not fit, try adjusting it by one?
2766 switch (CC) {
2767 default: break;
2768 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002769 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002770 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002771 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002772 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002773 }
2774 break;
2775 case ISD::SETULT:
2776 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002777 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002778 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002780 }
2781 break;
2782 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002783 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002784 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002785 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002786 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002787 }
2788 break;
2789 case ISD::SETULE:
2790 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002791 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002792 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002794 }
2795 break;
2796 }
2797 }
2798 }
2799
2800 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002801 ARMISD::NodeType CompareType;
2802 switch (CondCode) {
2803 default:
2804 CompareType = ARMISD::CMP;
2805 break;
2806 case ARMCC::EQ:
2807 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002808 // Uses only Z Flag
2809 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002810 break;
2811 }
Evan Cheng218977b2010-07-13 19:27:42 +00002812 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002813 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002814}
2815
2816/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002817SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002818ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002819 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002820 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002821 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002822 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002823 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002824 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2825 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002826}
2827
Bob Wilson79f56c92011-03-08 01:17:20 +00002828/// duplicateCmp - Glue values can have only one use, so this function
2829/// duplicates a comparison node.
2830SDValue
2831ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2832 unsigned Opc = Cmp.getOpcode();
2833 DebugLoc DL = Cmp.getDebugLoc();
2834 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2835 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2836
2837 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2838 Cmp = Cmp.getOperand(0);
2839 Opc = Cmp.getOpcode();
2840 if (Opc == ARMISD::CMPFP)
2841 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2842 else {
2843 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2844 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2845 }
2846 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2847}
2848
Bill Wendlingde2b1512010-08-11 08:43:16 +00002849SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2850 SDValue Cond = Op.getOperand(0);
2851 SDValue SelectTrue = Op.getOperand(1);
2852 SDValue SelectFalse = Op.getOperand(2);
2853 DebugLoc dl = Op.getDebugLoc();
2854
2855 // Convert:
2856 //
2857 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2858 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2859 //
2860 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2861 const ConstantSDNode *CMOVTrue =
2862 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2863 const ConstantSDNode *CMOVFalse =
2864 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2865
2866 if (CMOVTrue && CMOVFalse) {
2867 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2868 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2869
2870 SDValue True;
2871 SDValue False;
2872 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2873 True = SelectTrue;
2874 False = SelectFalse;
2875 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2876 True = SelectFalse;
2877 False = SelectTrue;
2878 }
2879
2880 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002881 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002882 SDValue ARMcc = Cond.getOperand(2);
2883 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002884 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002885 assert(True.getValueType() == VT);
2886 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002887 }
2888 }
2889 }
2890
Dan Gohmandb953892012-02-24 00:09:36 +00002891 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2892 // undefined bits before doing a full-word comparison with zero.
2893 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2894 DAG.getConstant(1, Cond.getValueType()));
2895
Bill Wendlingde2b1512010-08-11 08:43:16 +00002896 return DAG.getSelectCC(dl, Cond,
2897 DAG.getConstant(0, Cond.getValueType()),
2898 SelectTrue, SelectFalse, ISD::SETNE);
2899}
2900
Dan Gohmand858e902010-04-17 15:26:15 +00002901SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002902 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002903 SDValue LHS = Op.getOperand(0);
2904 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002905 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue TrueVal = Op.getOperand(2);
2907 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002908 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002909
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002911 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002912 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002913 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002914 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002915 }
2916
2917 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002918 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002919
Evan Cheng218977b2010-07-13 19:27:42 +00002920 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2921 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002923 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002924 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002926 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002927 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002928 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002929 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002930 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002931 }
2932 return Result;
2933}
2934
Evan Cheng218977b2010-07-13 19:27:42 +00002935/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2936/// to morph to an integer compare sequence.
2937static bool canChangeToInt(SDValue Op, bool &SeenZero,
2938 const ARMSubtarget *Subtarget) {
2939 SDNode *N = Op.getNode();
2940 if (!N->hasOneUse())
2941 // Otherwise it requires moving the value from fp to integer registers.
2942 return false;
2943 if (!N->getNumValues())
2944 return false;
2945 EVT VT = Op.getValueType();
2946 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2947 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2948 // vmrs are very slow, e.g. cortex-a8.
2949 return false;
2950
2951 if (isFloatingPointZero(Op)) {
2952 SeenZero = true;
2953 return true;
2954 }
2955 return ISD::isNormalLoad(N);
2956}
2957
2958static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2959 if (isFloatingPointZero(Op))
2960 return DAG.getConstant(0, MVT::i32);
2961
2962 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2963 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002964 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002965 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002966 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002967
2968 llvm_unreachable("Unknown VFP cmp argument!");
2969}
2970
2971static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2972 SDValue &RetVal1, SDValue &RetVal2) {
2973 if (isFloatingPointZero(Op)) {
2974 RetVal1 = DAG.getConstant(0, MVT::i32);
2975 RetVal2 = DAG.getConstant(0, MVT::i32);
2976 return;
2977 }
2978
2979 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2980 SDValue Ptr = Ld->getBasePtr();
2981 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2982 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002983 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002984 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002985 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002986
2987 EVT PtrType = Ptr.getValueType();
2988 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2989 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2990 PtrType, Ptr, DAG.getConstant(4, PtrType));
2991 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2992 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002993 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002994 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002995 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002996 return;
2997 }
2998
2999 llvm_unreachable("Unknown VFP cmp argument!");
3000}
3001
3002/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3003/// f32 and even f64 comparisons to integer ones.
3004SDValue
3005ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3006 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003007 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003008 SDValue LHS = Op.getOperand(2);
3009 SDValue RHS = Op.getOperand(3);
3010 SDValue Dest = Op.getOperand(4);
3011 DebugLoc dl = Op.getDebugLoc();
3012
Evan Chengfc501a32012-03-01 23:27:13 +00003013 bool LHSSeenZero = false;
3014 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3015 bool RHSSeenZero = false;
3016 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3017 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003018 // If unsafe fp math optimization is enabled and there are no other uses of
3019 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003020 // to an integer comparison.
3021 if (CC == ISD::SETOEQ)
3022 CC = ISD::SETEQ;
3023 else if (CC == ISD::SETUNE)
3024 CC = ISD::SETNE;
3025
Evan Chengfc501a32012-03-01 23:27:13 +00003026 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003027 SDValue ARMcc;
3028 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003029 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3030 bitcastf32Toi32(LHS, DAG), Mask);
3031 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3032 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003033 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3034 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3035 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3036 Chain, Dest, ARMcc, CCR, Cmp);
3037 }
3038
3039 SDValue LHS1, LHS2;
3040 SDValue RHS1, RHS2;
3041 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3042 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003043 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3044 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003045 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3046 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003047 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003048 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3049 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3050 }
3051
3052 return SDValue();
3053}
3054
3055SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3056 SDValue Chain = Op.getOperand(0);
3057 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3058 SDValue LHS = Op.getOperand(2);
3059 SDValue RHS = Op.getOperand(3);
3060 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003061 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003062
Owen Anderson825b72b2009-08-11 20:47:22 +00003063 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003064 SDValue ARMcc;
3065 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003068 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003069 }
3070
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003072
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003073 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003074 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3075 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3076 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3077 if (Result.getNode())
3078 return Result;
3079 }
3080
Evan Chenga8e29892007-01-19 07:51:42 +00003081 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003082 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003083
Evan Cheng218977b2010-07-13 19:27:42 +00003084 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3085 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003087 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003088 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003089 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003090 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003091 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3092 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003093 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003094 }
3095 return Res;
3096}
3097
Dan Gohmand858e902010-04-17 15:26:15 +00003098SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003099 SDValue Chain = Op.getOperand(0);
3100 SDValue Table = Op.getOperand(1);
3101 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003102 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003103
Owen Andersone50ed302009-08-10 22:56:29 +00003104 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003105 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3106 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003107 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003110 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3111 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003112 if (Subtarget->isThumb2()) {
3113 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3114 // which does another jump to the destination. This also makes it easier
3115 // to translate it to TBB / TBH later.
3116 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003117 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003118 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003119 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003120 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003121 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003122 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003123 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003124 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003125 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003126 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003127 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003128 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003129 MachinePointerInfo::getJumpTable(),
3130 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003131 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003132 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003133 }
Evan Chenga8e29892007-01-19 07:51:42 +00003134}
3135
Eli Friedman14e809c2011-11-09 23:36:02 +00003136static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003137 EVT VT = Op.getValueType();
3138 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003139
James Molloy873fd5f2012-02-20 09:24:05 +00003140 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3141 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3142 return Op;
3143 return DAG.UnrollVectorOp(Op.getNode());
3144 }
3145
3146 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3147 "Invalid type for custom lowering!");
3148 if (VT != MVT::v4i16)
3149 return DAG.UnrollVectorOp(Op.getNode());
3150
3151 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3152 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003153}
3154
Bob Wilson76a312b2010-03-19 22:51:32 +00003155static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003156 EVT VT = Op.getValueType();
3157 if (VT.isVector())
3158 return LowerVectorFP_TO_INT(Op, DAG);
3159
Bob Wilson76a312b2010-03-19 22:51:32 +00003160 DebugLoc dl = Op.getDebugLoc();
3161 unsigned Opc;
3162
3163 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003164 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003165 case ISD::FP_TO_SINT:
3166 Opc = ARMISD::FTOSI;
3167 break;
3168 case ISD::FP_TO_UINT:
3169 Opc = ARMISD::FTOUI;
3170 break;
3171 }
3172 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003173 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003174}
3175
Cameron Zwarich3007d332011-03-29 21:41:55 +00003176static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3177 EVT VT = Op.getValueType();
3178 DebugLoc dl = Op.getDebugLoc();
3179
Eli Friedman14e809c2011-11-09 23:36:02 +00003180 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3181 if (VT.getVectorElementType() == MVT::f32)
3182 return Op;
3183 return DAG.UnrollVectorOp(Op.getNode());
3184 }
3185
Duncan Sands1f6a3292011-08-12 14:54:45 +00003186 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3187 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003188 if (VT != MVT::v4f32)
3189 return DAG.UnrollVectorOp(Op.getNode());
3190
3191 unsigned CastOpc;
3192 unsigned Opc;
3193 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003194 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003195 case ISD::SINT_TO_FP:
3196 CastOpc = ISD::SIGN_EXTEND;
3197 Opc = ISD::SINT_TO_FP;
3198 break;
3199 case ISD::UINT_TO_FP:
3200 CastOpc = ISD::ZERO_EXTEND;
3201 Opc = ISD::UINT_TO_FP;
3202 break;
3203 }
3204
3205 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3206 return DAG.getNode(Opc, dl, VT, Op);
3207}
3208
Bob Wilson76a312b2010-03-19 22:51:32 +00003209static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3210 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003211 if (VT.isVector())
3212 return LowerVectorINT_TO_FP(Op, DAG);
3213
Bob Wilson76a312b2010-03-19 22:51:32 +00003214 DebugLoc dl = Op.getDebugLoc();
3215 unsigned Opc;
3216
3217 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003218 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003219 case ISD::SINT_TO_FP:
3220 Opc = ARMISD::SITOF;
3221 break;
3222 case ISD::UINT_TO_FP:
3223 Opc = ARMISD::UITOF;
3224 break;
3225 }
3226
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003227 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003228 return DAG.getNode(Opc, dl, VT, Op);
3229}
3230
Evan Cheng515fe3a2010-07-08 02:08:50 +00003231SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003232 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003233 SDValue Tmp0 = Op.getOperand(0);
3234 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003235 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003236 EVT VT = Op.getValueType();
3237 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003238 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3239 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3240 bool UseNEON = !InGPR && Subtarget->hasNEON();
3241
3242 if (UseNEON) {
3243 // Use VBSL to copy the sign bit.
3244 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3245 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3246 DAG.getTargetConstant(EncodedVal, MVT::i32));
3247 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3248 if (VT == MVT::f64)
3249 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3250 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3251 DAG.getConstant(32, MVT::i32));
3252 else /*if (VT == MVT::f32)*/
3253 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3254 if (SrcVT == MVT::f32) {
3255 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3256 if (VT == MVT::f64)
3257 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3258 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3259 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003260 } else if (VT == MVT::f32)
3261 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3262 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3263 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003264 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3265 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3266
3267 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3268 MVT::i32);
3269 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3270 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3271 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003272
Evan Chenge573fb32011-02-23 02:24:55 +00003273 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3274 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3275 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003276 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003277 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3278 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3279 DAG.getConstant(0, MVT::i32));
3280 } else {
3281 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3282 }
3283
3284 return Res;
3285 }
Evan Chengc143dd42011-02-11 02:28:55 +00003286
3287 // Bitcast operand 1 to i32.
3288 if (SrcVT == MVT::f64)
3289 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3290 &Tmp1, 1).getValue(1);
3291 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3292
Evan Chenge573fb32011-02-23 02:24:55 +00003293 // Or in the signbit with integer operations.
3294 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3295 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3296 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3297 if (VT == MVT::f32) {
3298 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3299 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3300 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3301 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003302 }
3303
Evan Chenge573fb32011-02-23 02:24:55 +00003304 // f64: Or the high part with signbit and then combine two parts.
3305 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3306 &Tmp0, 1);
3307 SDValue Lo = Tmp0.getValue(0);
3308 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3309 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3310 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003311}
3312
Evan Cheng2457f2c2010-05-22 01:47:14 +00003313SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3314 MachineFunction &MF = DAG.getMachineFunction();
3315 MachineFrameInfo *MFI = MF.getFrameInfo();
3316 MFI->setReturnAddressIsTaken(true);
3317
3318 EVT VT = Op.getValueType();
3319 DebugLoc dl = Op.getDebugLoc();
3320 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3321 if (Depth) {
3322 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3323 SDValue Offset = DAG.getConstant(4, MVT::i32);
3324 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3325 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003326 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003327 }
3328
3329 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003330 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003331 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3332}
3333
Dan Gohmand858e902010-04-17 15:26:15 +00003334SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003335 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3336 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003337
Owen Andersone50ed302009-08-10 22:56:29 +00003338 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003339 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3340 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003341 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003342 ? ARM::R7 : ARM::R11;
3343 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3344 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003345 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3346 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003347 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003348 return FrameAddr;
3349}
3350
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003351/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003352/// expand a bit convert where either the source or destination type is i64 to
3353/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3354/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3355/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003356static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3358 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003359 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003360
Bob Wilson9f3f0612010-04-17 05:30:19 +00003361 // This function is only supposed to be called for i64 types, either as the
3362 // source or destination of the bit convert.
3363 EVT SrcVT = Op.getValueType();
3364 EVT DstVT = N->getValueType(0);
3365 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003366 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003367
Bob Wilson9f3f0612010-04-17 05:30:19 +00003368 // Turn i64->f64 into VMOVDRR.
3369 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3371 DAG.getConstant(0, MVT::i32));
3372 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3373 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003374 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003375 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003376 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003377
Jim Grosbache5165492009-11-09 00:11:35 +00003378 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003379 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3380 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3381 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3382 // Merge the pieces into a single i64 value.
3383 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3384 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003385
Bob Wilson9f3f0612010-04-17 05:30:19 +00003386 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003387}
3388
Bob Wilson5bafff32009-06-22 23:27:02 +00003389/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003390/// Zero vectors are used to represent vector negation and in those cases
3391/// will be implemented with the NEON VNEG instruction. However, VNEG does
3392/// not support i64 elements, so sometimes the zero vectors will need to be
3393/// explicitly constructed. Regardless, use a canonical VMOV to create the
3394/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003395static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003397 // The canonical modified immediate encoding of a zero vector is....0!
3398 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3399 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3400 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003401 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003402}
3403
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003404/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3405/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003406SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3407 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003408 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3409 EVT VT = Op.getValueType();
3410 unsigned VTBits = VT.getSizeInBits();
3411 DebugLoc dl = Op.getDebugLoc();
3412 SDValue ShOpLo = Op.getOperand(0);
3413 SDValue ShOpHi = Op.getOperand(1);
3414 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003415 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003416 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003417
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003418 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3419
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003420 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3421 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3422 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3423 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3424 DAG.getConstant(VTBits, MVT::i32));
3425 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3426 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003427 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003428
3429 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3430 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003431 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003432 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003433 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003434 CCR, Cmp);
3435
3436 SDValue Ops[2] = { Lo, Hi };
3437 return DAG.getMergeValues(Ops, 2, dl);
3438}
3439
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003440/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3441/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003442SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3443 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003444 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3445 EVT VT = Op.getValueType();
3446 unsigned VTBits = VT.getSizeInBits();
3447 DebugLoc dl = Op.getDebugLoc();
3448 SDValue ShOpLo = Op.getOperand(0);
3449 SDValue ShOpHi = Op.getOperand(1);
3450 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003451 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003452
3453 assert(Op.getOpcode() == ISD::SHL_PARTS);
3454 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3455 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3456 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3457 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3458 DAG.getConstant(VTBits, MVT::i32));
3459 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3460 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3461
3462 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3463 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3464 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003465 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003466 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003467 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003468 CCR, Cmp);
3469
3470 SDValue Ops[2] = { Lo, Hi };
3471 return DAG.getMergeValues(Ops, 2, dl);
3472}
3473
Jim Grosbach4725ca72010-09-08 03:54:02 +00003474SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003475 SelectionDAG &DAG) const {
3476 // The rounding mode is in bits 23:22 of the FPSCR.
3477 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3478 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3479 // so that the shift + and get folded into a bitfield extract.
3480 DebugLoc dl = Op.getDebugLoc();
3481 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3482 DAG.getConstant(Intrinsic::arm_get_fpscr,
3483 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003484 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003485 DAG.getConstant(1U << 22, MVT::i32));
3486 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3487 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003488 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003489 DAG.getConstant(3, MVT::i32));
3490}
3491
Jim Grosbach3482c802010-01-18 19:58:49 +00003492static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3493 const ARMSubtarget *ST) {
3494 EVT VT = N->getValueType(0);
3495 DebugLoc dl = N->getDebugLoc();
3496
3497 if (!ST->hasV6T2Ops())
3498 return SDValue();
3499
3500 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3501 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3502}
3503
Bob Wilson5bafff32009-06-22 23:27:02 +00003504static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3505 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003506 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 DebugLoc dl = N->getDebugLoc();
3508
Bob Wilsond5448bb2010-11-18 21:16:28 +00003509 if (!VT.isVector())
3510 return SDValue();
3511
Bob Wilson5bafff32009-06-22 23:27:02 +00003512 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003513 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003514
Bob Wilsond5448bb2010-11-18 21:16:28 +00003515 // Left shifts translate directly to the vshiftu intrinsic.
3516 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003517 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003518 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3519 N->getOperand(0), N->getOperand(1));
3520
3521 assert((N->getOpcode() == ISD::SRA ||
3522 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3523
3524 // NEON uses the same intrinsics for both left and right shifts. For
3525 // right shifts, the shift amounts are negative, so negate the vector of
3526 // shift amounts.
3527 EVT ShiftVT = N->getOperand(1).getValueType();
3528 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3529 getZeroVector(ShiftVT, DAG, dl),
3530 N->getOperand(1));
3531 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3532 Intrinsic::arm_neon_vshifts :
3533 Intrinsic::arm_neon_vshiftu);
3534 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3535 DAG.getConstant(vshiftInt, MVT::i32),
3536 N->getOperand(0), NegatedCount);
3537}
3538
3539static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3540 const ARMSubtarget *ST) {
3541 EVT VT = N->getValueType(0);
3542 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003543
Eli Friedmance392eb2009-08-22 03:13:10 +00003544 // We can get here for a node like i32 = ISD::SHL i32, i64
3545 if (VT != MVT::i64)
3546 return SDValue();
3547
3548 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003549 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003550
Chris Lattner27a6c732007-11-24 07:07:01 +00003551 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3552 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003553 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003554 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003555
Chris Lattner27a6c732007-11-24 07:07:01 +00003556 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003557 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattner27a6c732007-11-24 07:07:01 +00003559 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003561 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003563 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003564
Chris Lattner27a6c732007-11-24 07:07:01 +00003565 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3566 // captures the result into a carry flag.
3567 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003568 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003569
Chris Lattner27a6c732007-11-24 07:07:01 +00003570 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003572
Chris Lattner27a6c732007-11-24 07:07:01 +00003573 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003575}
3576
Bob Wilson5bafff32009-06-22 23:27:02 +00003577static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3578 SDValue TmpOp0, TmpOp1;
3579 bool Invert = false;
3580 bool Swap = false;
3581 unsigned Opc = 0;
3582
3583 SDValue Op0 = Op.getOperand(0);
3584 SDValue Op1 = Op.getOperand(1);
3585 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003586 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003587 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3588 DebugLoc dl = Op.getDebugLoc();
3589
3590 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3591 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003592 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003593 case ISD::SETUNE:
3594 case ISD::SETNE: Invert = true; // Fallthrough
3595 case ISD::SETOEQ:
3596 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3597 case ISD::SETOLT:
3598 case ISD::SETLT: Swap = true; // Fallthrough
3599 case ISD::SETOGT:
3600 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3601 case ISD::SETOLE:
3602 case ISD::SETLE: Swap = true; // Fallthrough
3603 case ISD::SETOGE:
3604 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3605 case ISD::SETUGE: Swap = true; // Fallthrough
3606 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3607 case ISD::SETUGT: Swap = true; // Fallthrough
3608 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3609 case ISD::SETUEQ: Invert = true; // Fallthrough
3610 case ISD::SETONE:
3611 // Expand this to (OLT | OGT).
3612 TmpOp0 = Op0;
3613 TmpOp1 = Op1;
3614 Opc = ISD::OR;
3615 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3616 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3617 break;
3618 case ISD::SETUO: Invert = true; // Fallthrough
3619 case ISD::SETO:
3620 // Expand this to (OLT | OGE).
3621 TmpOp0 = Op0;
3622 TmpOp1 = Op1;
3623 Opc = ISD::OR;
3624 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3625 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3626 break;
3627 }
3628 } else {
3629 // Integer comparisons.
3630 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003631 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003632 case ISD::SETNE: Invert = true;
3633 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3634 case ISD::SETLT: Swap = true;
3635 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3636 case ISD::SETLE: Swap = true;
3637 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3638 case ISD::SETULT: Swap = true;
3639 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3640 case ISD::SETULE: Swap = true;
3641 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3642 }
3643
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003644 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003645 if (Opc == ARMISD::VCEQ) {
3646
3647 SDValue AndOp;
3648 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3649 AndOp = Op0;
3650 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3651 AndOp = Op1;
3652
3653 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003654 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003655 AndOp = AndOp.getOperand(0);
3656
3657 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3658 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003659 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3660 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003661 Invert = !Invert;
3662 }
3663 }
3664 }
3665
3666 if (Swap)
3667 std::swap(Op0, Op1);
3668
Owen Andersonc24cb352010-11-08 23:21:22 +00003669 // If one of the operands is a constant vector zero, attempt to fold the
3670 // comparison to a specialized compare-against-zero form.
3671 SDValue SingleOp;
3672 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3673 SingleOp = Op0;
3674 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3675 if (Opc == ARMISD::VCGE)
3676 Opc = ARMISD::VCLEZ;
3677 else if (Opc == ARMISD::VCGT)
3678 Opc = ARMISD::VCLTZ;
3679 SingleOp = Op1;
3680 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003681
Owen Andersonc24cb352010-11-08 23:21:22 +00003682 SDValue Result;
3683 if (SingleOp.getNode()) {
3684 switch (Opc) {
3685 case ARMISD::VCEQ:
3686 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3687 case ARMISD::VCGE:
3688 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3689 case ARMISD::VCLEZ:
3690 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3691 case ARMISD::VCGT:
3692 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3693 case ARMISD::VCLTZ:
3694 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3695 default:
3696 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3697 }
3698 } else {
3699 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3700 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003701
3702 if (Invert)
3703 Result = DAG.getNOT(dl, Result, VT);
3704
3705 return Result;
3706}
3707
Bob Wilsond3c42842010-06-14 22:19:57 +00003708/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3709/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003710/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003711static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3712 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003713 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003714 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003715
Bob Wilson827b2102010-06-15 19:05:35 +00003716 // SplatBitSize is set to the smallest size that splats the vector, so a
3717 // zero vector will always have SplatBitSize == 8. However, NEON modified
3718 // immediate instructions others than VMOV do not support the 8-bit encoding
3719 // of a zero vector, and the default encoding of zero is supposed to be the
3720 // 32-bit version.
3721 if (SplatBits == 0)
3722 SplatBitSize = 32;
3723
Bob Wilson5bafff32009-06-22 23:27:02 +00003724 switch (SplatBitSize) {
3725 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003726 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003727 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003728 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003729 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003730 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003732 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003733 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003734
3735 case 16:
3736 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003737 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003738 if ((SplatBits & ~0xff) == 0) {
3739 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003740 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003741 Imm = SplatBits;
3742 break;
3743 }
3744 if ((SplatBits & ~0xff00) == 0) {
3745 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003746 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003747 Imm = SplatBits >> 8;
3748 break;
3749 }
3750 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003751
3752 case 32:
3753 // NEON's 32-bit VMOV supports splat values where:
3754 // * only one byte is nonzero, or
3755 // * the least significant byte is 0xff and the second byte is nonzero, or
3756 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003757 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003758 if ((SplatBits & ~0xff) == 0) {
3759 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003760 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003761 Imm = SplatBits;
3762 break;
3763 }
3764 if ((SplatBits & ~0xff00) == 0) {
3765 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003766 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003767 Imm = SplatBits >> 8;
3768 break;
3769 }
3770 if ((SplatBits & ~0xff0000) == 0) {
3771 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003772 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003773 Imm = SplatBits >> 16;
3774 break;
3775 }
3776 if ((SplatBits & ~0xff000000) == 0) {
3777 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003778 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003779 Imm = SplatBits >> 24;
3780 break;
3781 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003782
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003783 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3784 if (type == OtherModImm) return SDValue();
3785
Bob Wilson5bafff32009-06-22 23:27:02 +00003786 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003787 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3788 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003789 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003790 Imm = SplatBits >> 8;
3791 SplatBits |= 0xff;
3792 break;
3793 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003794
3795 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003796 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3797 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003798 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003799 Imm = SplatBits >> 16;
3800 SplatBits |= 0xffff;
3801 break;
3802 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003803
3804 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3805 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3806 // VMOV.I32. A (very) minor optimization would be to replicate the value
3807 // and fall through here to test for a valid 64-bit splat. But, then the
3808 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003809 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003810
3811 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003812 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003813 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003814 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003815 uint64_t BitMask = 0xff;
3816 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003817 unsigned ImmMask = 1;
3818 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003821 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003822 Imm |= ImmMask;
3823 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003826 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003828 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003829 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003830 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003831 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003832 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003833 break;
3834 }
3835
Bob Wilson1a913ed2010-06-11 21:34:50 +00003836 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003837 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003838 }
3839
Bob Wilsoncba270d2010-07-13 21:16:48 +00003840 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3841 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003842}
3843
Lang Hamesc0a9f822012-03-29 21:56:11 +00003844SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3845 const ARMSubtarget *ST) const {
3846 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3847 return SDValue();
3848
3849 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3850 assert(Op.getValueType() == MVT::f32 &&
3851 "ConstantFP custom lowering should only occur for f32.");
3852
3853 // Try splatting with a VMOV.f32...
3854 APFloat FPVal = CFP->getValueAPF();
3855 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3856 if (ImmVal != -1) {
3857 DebugLoc DL = Op.getDebugLoc();
3858 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3859 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3860 NewVal);
3861 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3862 DAG.getConstant(0, MVT::i32));
3863 }
3864
3865 // If that fails, try a VMOV.i32
3866 EVT VMovVT;
3867 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3868 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3869 VMOVModImm);
3870 if (NewVal != SDValue()) {
3871 DebugLoc DL = Op.getDebugLoc();
3872 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3873 NewVal);
3874 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3875 VecConstant);
3876 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3877 DAG.getConstant(0, MVT::i32));
3878 }
3879
3880 // Finally, try a VMVN.i32
3881 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3882 VMVNModImm);
3883 if (NewVal != SDValue()) {
3884 DebugLoc DL = Op.getDebugLoc();
3885 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3886 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3887 VecConstant);
3888 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3889 DAG.getConstant(0, MVT::i32));
3890 }
3891
3892 return SDValue();
3893}
3894
3895
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003896static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003897 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003898 unsigned NumElts = VT.getVectorNumElements();
3899 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003900
3901 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3902 if (M[0] < 0)
3903 return false;
3904
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003905 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003906
3907 // If this is a VEXT shuffle, the immediate value is the index of the first
3908 // element. The other shuffle indices must be the successive elements after
3909 // the first one.
3910 unsigned ExpectedElt = Imm;
3911 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003912 // Increment the expected index. If it wraps around, it may still be
3913 // a VEXT but the source vectors must be swapped.
3914 ExpectedElt += 1;
3915 if (ExpectedElt == NumElts * 2) {
3916 ExpectedElt = 0;
3917 ReverseVEXT = true;
3918 }
3919
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003920 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003921 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003922 return false;
3923 }
3924
3925 // Adjust the index value if the source operands will be swapped.
3926 if (ReverseVEXT)
3927 Imm -= NumElts;
3928
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003929 return true;
3930}
3931
Bob Wilson8bb9e482009-07-26 00:39:34 +00003932/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3933/// instruction with the specified blocksize. (The order of the elements
3934/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003935static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003936 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3937 "Only possible block sizes for VREV are: 16, 32, 64");
3938
Bob Wilson8bb9e482009-07-26 00:39:34 +00003939 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003940 if (EltSz == 64)
3941 return false;
3942
3943 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003944 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003945 // If the first shuffle index is UNDEF, be optimistic.
3946 if (M[0] < 0)
3947 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003948
3949 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3950 return false;
3951
3952 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003953 if (M[i] < 0) continue; // ignore UNDEF indices
3954 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003955 return false;
3956 }
3957
3958 return true;
3959}
3960
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003961static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003962 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3963 // range, then 0 is placed into the resulting vector. So pretty much any mask
3964 // of 8 elements can work here.
3965 return VT == MVT::v8i8 && M.size() == 8;
3966}
3967
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003968static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003969 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3970 if (EltSz == 64)
3971 return false;
3972
Bob Wilsonc692cb72009-08-21 20:54:19 +00003973 unsigned NumElts = VT.getVectorNumElements();
3974 WhichResult = (M[0] == 0 ? 0 : 1);
3975 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003976 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3977 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003978 return false;
3979 }
3980 return true;
3981}
3982
Bob Wilson324f4f12009-12-03 06:40:55 +00003983/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3984/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3985/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003986static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003987 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3988 if (EltSz == 64)
3989 return false;
3990
3991 unsigned NumElts = VT.getVectorNumElements();
3992 WhichResult = (M[0] == 0 ? 0 : 1);
3993 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003994 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3995 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003996 return false;
3997 }
3998 return true;
3999}
4000
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004001static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004002 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4003 if (EltSz == 64)
4004 return false;
4005
Bob Wilsonc692cb72009-08-21 20:54:19 +00004006 unsigned NumElts = VT.getVectorNumElements();
4007 WhichResult = (M[0] == 0 ? 0 : 1);
4008 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004009 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004010 if ((unsigned) M[i] != 2 * i + WhichResult)
4011 return false;
4012 }
4013
4014 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004015 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004016 return false;
4017
4018 return true;
4019}
4020
Bob Wilson324f4f12009-12-03 06:40:55 +00004021/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4022/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4023/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004024static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004025 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4026 if (EltSz == 64)
4027 return false;
4028
4029 unsigned Half = VT.getVectorNumElements() / 2;
4030 WhichResult = (M[0] == 0 ? 0 : 1);
4031 for (unsigned j = 0; j != 2; ++j) {
4032 unsigned Idx = WhichResult;
4033 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004034 int MIdx = M[i + j * Half];
4035 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004036 return false;
4037 Idx += 2;
4038 }
4039 }
4040
4041 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4042 if (VT.is64BitVector() && EltSz == 32)
4043 return false;
4044
4045 return true;
4046}
4047
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004048static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004049 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4050 if (EltSz == 64)
4051 return false;
4052
Bob Wilsonc692cb72009-08-21 20:54:19 +00004053 unsigned NumElts = VT.getVectorNumElements();
4054 WhichResult = (M[0] == 0 ? 0 : 1);
4055 unsigned Idx = WhichResult * NumElts / 2;
4056 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004057 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4058 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004059 return false;
4060 Idx += 1;
4061 }
4062
4063 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004064 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004065 return false;
4066
4067 return true;
4068}
4069
Bob Wilson324f4f12009-12-03 06:40:55 +00004070/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4071/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4072/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004073static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004074 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4075 if (EltSz == 64)
4076 return false;
4077
4078 unsigned NumElts = VT.getVectorNumElements();
4079 WhichResult = (M[0] == 0 ? 0 : 1);
4080 unsigned Idx = WhichResult * NumElts / 2;
4081 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004082 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4083 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004084 return false;
4085 Idx += 1;
4086 }
4087
4088 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4089 if (VT.is64BitVector() && EltSz == 32)
4090 return false;
4091
4092 return true;
4093}
4094
Dale Johannesenf630c712010-07-29 20:10:08 +00004095// If N is an integer constant that can be moved into a register in one
4096// instruction, return an SDValue of such a constant (will become a MOV
4097// instruction). Otherwise return null.
4098static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4099 const ARMSubtarget *ST, DebugLoc dl) {
4100 uint64_t Val;
4101 if (!isa<ConstantSDNode>(N))
4102 return SDValue();
4103 Val = cast<ConstantSDNode>(N)->getZExtValue();
4104
4105 if (ST->isThumb1Only()) {
4106 if (Val <= 255 || ~Val <= 255)
4107 return DAG.getConstant(Val, MVT::i32);
4108 } else {
4109 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4110 return DAG.getConstant(Val, MVT::i32);
4111 }
4112 return SDValue();
4113}
4114
Bob Wilson5bafff32009-06-22 23:27:02 +00004115// If this is a case we can't handle, return null and let the default
4116// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4118 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004119 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004120 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004121 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004122
4123 APInt SplatBits, SplatUndef;
4124 unsigned SplatBitSize;
4125 bool HasAnyUndefs;
4126 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004127 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004128 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004129 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004130 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004131 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004132 DAG, VmovVT, VT.is128BitVector(),
4133 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004134 if (Val.getNode()) {
4135 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004136 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004137 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004138
4139 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004140 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004141 Val = isNEONModifiedImm(NegatedImm,
4142 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004143 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004144 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004145 if (Val.getNode()) {
4146 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004147 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004148 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004149
4150 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004151 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004152 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004153 if (ImmVal != -1) {
4154 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4155 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4156 }
4157 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004158 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004159 }
4160
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004161 // Scan through the operands to see if only one value is used.
4162 unsigned NumElts = VT.getVectorNumElements();
4163 bool isOnlyLowElement = true;
4164 bool usesOnlyOneValue = true;
4165 bool isConstant = true;
4166 SDValue Value;
4167 for (unsigned i = 0; i < NumElts; ++i) {
4168 SDValue V = Op.getOperand(i);
4169 if (V.getOpcode() == ISD::UNDEF)
4170 continue;
4171 if (i > 0)
4172 isOnlyLowElement = false;
4173 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4174 isConstant = false;
4175
4176 if (!Value.getNode())
4177 Value = V;
4178 else if (V != Value)
4179 usesOnlyOneValue = false;
4180 }
4181
4182 if (!Value.getNode())
4183 return DAG.getUNDEF(VT);
4184
4185 if (isOnlyLowElement)
4186 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4187
Dale Johannesenf630c712010-07-29 20:10:08 +00004188 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4189
Dale Johannesen575cd142010-10-19 20:00:17 +00004190 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4191 // i32 and try again.
4192 if (usesOnlyOneValue && EltSize <= 32) {
4193 if (!isConstant)
4194 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4195 if (VT.getVectorElementType().isFloatingPoint()) {
4196 SmallVector<SDValue, 8> Ops;
4197 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004198 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004199 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004200 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4201 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004202 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4203 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004204 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004205 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004206 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4207 if (Val.getNode())
4208 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004209 }
4210
4211 // If all elements are constants and the case above didn't get hit, fall back
4212 // to the default expansion, which will generate a load from the constant
4213 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004214 if (isConstant)
4215 return SDValue();
4216
Bob Wilson11a1dff2011-01-07 21:37:30 +00004217 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4218 if (NumElts >= 4) {
4219 SDValue shuffle = ReconstructShuffle(Op, DAG);
4220 if (shuffle != SDValue())
4221 return shuffle;
4222 }
4223
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004224 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004225 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4226 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004227 if (EltSize >= 32) {
4228 // Do the expansion with floating-point types, since that is what the VFP
4229 // registers are defined to use, and since i64 is not legal.
4230 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4231 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004232 SmallVector<SDValue, 8> Ops;
4233 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004234 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004235 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004236 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004237 }
4238
4239 return SDValue();
4240}
4241
Bob Wilson11a1dff2011-01-07 21:37:30 +00004242// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004243// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004244SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4245 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004246 DebugLoc dl = Op.getDebugLoc();
4247 EVT VT = Op.getValueType();
4248 unsigned NumElts = VT.getVectorNumElements();
4249
4250 SmallVector<SDValue, 2> SourceVecs;
4251 SmallVector<unsigned, 2> MinElts;
4252 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004253
Bob Wilson11a1dff2011-01-07 21:37:30 +00004254 for (unsigned i = 0; i < NumElts; ++i) {
4255 SDValue V = Op.getOperand(i);
4256 if (V.getOpcode() == ISD::UNDEF)
4257 continue;
4258 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4259 // A shuffle can only come from building a vector from various
4260 // elements of other vectors.
4261 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004262 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4263 VT.getVectorElementType()) {
4264 // This code doesn't know how to handle shuffles where the vector
4265 // element types do not match (this happens because type legalization
4266 // promotes the return type of EXTRACT_VECTOR_ELT).
4267 // FIXME: It might be appropriate to extend this code to handle
4268 // mismatched types.
4269 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004270 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004271
Bob Wilson11a1dff2011-01-07 21:37:30 +00004272 // Record this extraction against the appropriate vector if possible...
4273 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004274 // If the element number isn't a constant, we can't effectively
4275 // analyze what's going on.
4276 if (!isa<ConstantSDNode>(V.getOperand(1)))
4277 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004278 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4279 bool FoundSource = false;
4280 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4281 if (SourceVecs[j] == SourceVec) {
4282 if (MinElts[j] > EltNo)
4283 MinElts[j] = EltNo;
4284 if (MaxElts[j] < EltNo)
4285 MaxElts[j] = EltNo;
4286 FoundSource = true;
4287 break;
4288 }
4289 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004290
Bob Wilson11a1dff2011-01-07 21:37:30 +00004291 // Or record a new source if not...
4292 if (!FoundSource) {
4293 SourceVecs.push_back(SourceVec);
4294 MinElts.push_back(EltNo);
4295 MaxElts.push_back(EltNo);
4296 }
4297 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004298
Bob Wilson11a1dff2011-01-07 21:37:30 +00004299 // Currently only do something sane when at most two source vectors
4300 // involved.
4301 if (SourceVecs.size() > 2)
4302 return SDValue();
4303
4304 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4305 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004306
Bob Wilson11a1dff2011-01-07 21:37:30 +00004307 // This loop extracts the usage patterns of the source vectors
4308 // and prepares appropriate SDValues for a shuffle if possible.
4309 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4310 if (SourceVecs[i].getValueType() == VT) {
4311 // No VEXT necessary
4312 ShuffleSrcs[i] = SourceVecs[i];
4313 VEXTOffsets[i] = 0;
4314 continue;
4315 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4316 // It probably isn't worth padding out a smaller vector just to
4317 // break it down again in a shuffle.
4318 return SDValue();
4319 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004320
Bob Wilson11a1dff2011-01-07 21:37:30 +00004321 // Since only 64-bit and 128-bit vectors are legal on ARM and
4322 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004323 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4324 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004325
Bob Wilson11a1dff2011-01-07 21:37:30 +00004326 if (MaxElts[i] - MinElts[i] >= NumElts) {
4327 // Span too large for a VEXT to cope
4328 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004329 }
4330
Bob Wilson11a1dff2011-01-07 21:37:30 +00004331 if (MinElts[i] >= NumElts) {
4332 // The extraction can just take the second half
4333 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004334 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4335 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004336 DAG.getIntPtrConstant(NumElts));
4337 } else if (MaxElts[i] < NumElts) {
4338 // The extraction can just take the first half
4339 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004340 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4341 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004342 DAG.getIntPtrConstant(0));
4343 } else {
4344 // An actual VEXT is needed
4345 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004346 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4347 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004348 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004349 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4350 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004351 DAG.getIntPtrConstant(NumElts));
4352 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4353 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4354 }
4355 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004356
Bob Wilson11a1dff2011-01-07 21:37:30 +00004357 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004358
Bob Wilson11a1dff2011-01-07 21:37:30 +00004359 for (unsigned i = 0; i < NumElts; ++i) {
4360 SDValue Entry = Op.getOperand(i);
4361 if (Entry.getOpcode() == ISD::UNDEF) {
4362 Mask.push_back(-1);
4363 continue;
4364 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004365
Bob Wilson11a1dff2011-01-07 21:37:30 +00004366 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004367 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4368 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004369 if (ExtractVec == SourceVecs[0]) {
4370 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4371 } else {
4372 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4373 }
4374 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004375
Bob Wilson11a1dff2011-01-07 21:37:30 +00004376 // Final check before we try to produce nonsense...
4377 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004378 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4379 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004380
Bob Wilson11a1dff2011-01-07 21:37:30 +00004381 return SDValue();
4382}
4383
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004384/// isShuffleMaskLegal - Targets can use this to indicate that they only
4385/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4386/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4387/// are assumed to be legal.
4388bool
4389ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4390 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004391 if (VT.getVectorNumElements() == 4 &&
4392 (VT.is128BitVector() || VT.is64BitVector())) {
4393 unsigned PFIndexes[4];
4394 for (unsigned i = 0; i != 4; ++i) {
4395 if (M[i] < 0)
4396 PFIndexes[i] = 8;
4397 else
4398 PFIndexes[i] = M[i];
4399 }
4400
4401 // Compute the index in the perfect shuffle table.
4402 unsigned PFTableIndex =
4403 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4404 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4405 unsigned Cost = (PFEntry >> 30);
4406
4407 if (Cost <= 4)
4408 return true;
4409 }
4410
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004411 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004412 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004413
Bob Wilson53dd2452010-06-07 23:53:38 +00004414 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4415 return (EltSize >= 32 ||
4416 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004417 isVREVMask(M, VT, 64) ||
4418 isVREVMask(M, VT, 32) ||
4419 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004420 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004421 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004422 isVTRNMask(M, VT, WhichResult) ||
4423 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004424 isVZIPMask(M, VT, WhichResult) ||
4425 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4426 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4427 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004428}
4429
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004430/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4431/// the specified operations to build the shuffle.
4432static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4433 SDValue RHS, SelectionDAG &DAG,
4434 DebugLoc dl) {
4435 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4436 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4437 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4438
4439 enum {
4440 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4441 OP_VREV,
4442 OP_VDUP0,
4443 OP_VDUP1,
4444 OP_VDUP2,
4445 OP_VDUP3,
4446 OP_VEXT1,
4447 OP_VEXT2,
4448 OP_VEXT3,
4449 OP_VUZPL, // VUZP, left result
4450 OP_VUZPR, // VUZP, right result
4451 OP_VZIPL, // VZIP, left result
4452 OP_VZIPR, // VZIP, right result
4453 OP_VTRNL, // VTRN, left result
4454 OP_VTRNR // VTRN, right result
4455 };
4456
4457 if (OpNum == OP_COPY) {
4458 if (LHSID == (1*9+2)*9+3) return LHS;
4459 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4460 return RHS;
4461 }
4462
4463 SDValue OpLHS, OpRHS;
4464 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4465 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4466 EVT VT = OpLHS.getValueType();
4467
4468 switch (OpNum) {
4469 default: llvm_unreachable("Unknown shuffle opcode!");
4470 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004471 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004472 if (VT.getVectorElementType() == MVT::i32 ||
4473 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004474 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4475 // vrev <4 x i16> -> VREV32
4476 if (VT.getVectorElementType() == MVT::i16)
4477 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4478 // vrev <4 x i8> -> VREV16
4479 assert(VT.getVectorElementType() == MVT::i8);
4480 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004481 case OP_VDUP0:
4482 case OP_VDUP1:
4483 case OP_VDUP2:
4484 case OP_VDUP3:
4485 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004486 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004487 case OP_VEXT1:
4488 case OP_VEXT2:
4489 case OP_VEXT3:
4490 return DAG.getNode(ARMISD::VEXT, dl, VT,
4491 OpLHS, OpRHS,
4492 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4493 case OP_VUZPL:
4494 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004495 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004496 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4497 case OP_VZIPL:
4498 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004499 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004500 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4501 case OP_VTRNL:
4502 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004503 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4504 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004505 }
4506}
4507
Bill Wendling69a05a72011-03-14 23:02:38 +00004508static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004509 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004510 SelectionDAG &DAG) {
4511 // Check to see if we can use the VTBL instruction.
4512 SDValue V1 = Op.getOperand(0);
4513 SDValue V2 = Op.getOperand(1);
4514 DebugLoc DL = Op.getDebugLoc();
4515
4516 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004517 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004518 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4519 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4520
4521 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4522 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4523 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4524 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004525
Owen Anderson76706012011-04-05 21:48:57 +00004526 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004527 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4528 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004529}
4530
Bob Wilson5bafff32009-06-22 23:27:02 +00004531static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004532 SDValue V1 = Op.getOperand(0);
4533 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004534 DebugLoc dl = Op.getDebugLoc();
4535 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004536 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004537
Bob Wilson28865062009-08-13 02:13:04 +00004538 // Convert shuffles that are directly supported on NEON to target-specific
4539 // DAG nodes, instead of keeping them as shuffles and matching them again
4540 // during code selection. This is more efficient and avoids the possibility
4541 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004542 // FIXME: floating-point vectors should be canonicalized to integer vectors
4543 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004544 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004545
Bob Wilson53dd2452010-06-07 23:53:38 +00004546 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4547 if (EltSize <= 32) {
4548 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4549 int Lane = SVN->getSplatIndex();
4550 // If this is undef splat, generate it via "just" vdup, if possible.
4551 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004552
Dan Gohman65fd6562011-11-03 21:49:52 +00004553 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004554 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4555 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4556 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004557 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4558 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4559 // reaches it).
4560 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4561 !isa<ConstantSDNode>(V1.getOperand(0))) {
4562 bool IsScalarToVector = true;
4563 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4564 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4565 IsScalarToVector = false;
4566 break;
4567 }
4568 if (IsScalarToVector)
4569 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4570 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004571 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4572 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004573 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004574
4575 bool ReverseVEXT;
4576 unsigned Imm;
4577 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4578 if (ReverseVEXT)
4579 std::swap(V1, V2);
4580 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4581 DAG.getConstant(Imm, MVT::i32));
4582 }
4583
4584 if (isVREVMask(ShuffleMask, VT, 64))
4585 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4586 if (isVREVMask(ShuffleMask, VT, 32))
4587 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4588 if (isVREVMask(ShuffleMask, VT, 16))
4589 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4590
4591 // Check for Neon shuffles that modify both input vectors in place.
4592 // If both results are used, i.e., if there are two shuffles with the same
4593 // source operands and with masks corresponding to both results of one of
4594 // these operations, DAG memoization will ensure that a single node is
4595 // used for both shuffles.
4596 unsigned WhichResult;
4597 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4598 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4599 V1, V2).getValue(WhichResult);
4600 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4601 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4602 V1, V2).getValue(WhichResult);
4603 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4604 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4605 V1, V2).getValue(WhichResult);
4606
4607 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4608 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4609 V1, V1).getValue(WhichResult);
4610 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4611 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4612 V1, V1).getValue(WhichResult);
4613 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4614 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4615 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004616 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004617
Bob Wilsonc692cb72009-08-21 20:54:19 +00004618 // If the shuffle is not directly supported and it has 4 elements, use
4619 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004620 unsigned NumElts = VT.getVectorNumElements();
4621 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004622 unsigned PFIndexes[4];
4623 for (unsigned i = 0; i != 4; ++i) {
4624 if (ShuffleMask[i] < 0)
4625 PFIndexes[i] = 8;
4626 else
4627 PFIndexes[i] = ShuffleMask[i];
4628 }
4629
4630 // Compute the index in the perfect shuffle table.
4631 unsigned PFTableIndex =
4632 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004633 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4634 unsigned Cost = (PFEntry >> 30);
4635
4636 if (Cost <= 4)
4637 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4638 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004639
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004640 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004641 if (EltSize >= 32) {
4642 // Do the expansion with floating-point types, since that is what the VFP
4643 // registers are defined to use, and since i64 is not legal.
4644 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4645 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004646 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4647 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004648 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004649 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004650 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004651 Ops.push_back(DAG.getUNDEF(EltVT));
4652 else
4653 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4654 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4655 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4656 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004657 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004658 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004659 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004660 }
4661
Bill Wendling69a05a72011-03-14 23:02:38 +00004662 if (VT == MVT::v8i8) {
4663 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4664 if (NewOp.getNode())
4665 return NewOp;
4666 }
4667
Bob Wilson22cac0d2009-08-14 05:16:33 +00004668 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004669}
4670
Eli Friedman5c89cb82011-10-24 23:08:52 +00004671static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4672 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4673 SDValue Lane = Op.getOperand(2);
4674 if (!isa<ConstantSDNode>(Lane))
4675 return SDValue();
4676
4677 return Op;
4678}
4679
Bob Wilson5bafff32009-06-22 23:27:02 +00004680static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004681 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004682 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004683 if (!isa<ConstantSDNode>(Lane))
4684 return SDValue();
4685
4686 SDValue Vec = Op.getOperand(0);
4687 if (Op.getValueType() == MVT::i32 &&
4688 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4689 DebugLoc dl = Op.getDebugLoc();
4690 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4691 }
4692
4693 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004694}
4695
Bob Wilsona6d65862009-08-03 20:36:38 +00004696static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4697 // The only time a CONCAT_VECTORS operation can have legal types is when
4698 // two 64-bit vectors are concatenated to a 128-bit vector.
4699 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4700 "unexpected CONCAT_VECTORS");
4701 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004702 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004703 SDValue Op0 = Op.getOperand(0);
4704 SDValue Op1 = Op.getOperand(1);
4705 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004707 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004708 DAG.getIntPtrConstant(0));
4709 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004711 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004712 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004713 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004714}
4715
Bob Wilson626613d2010-11-23 19:38:38 +00004716/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4717/// element has been zero/sign-extended, depending on the isSigned parameter,
4718/// from an integer type half its size.
4719static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4720 bool isSigned) {
4721 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4722 EVT VT = N->getValueType(0);
4723 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4724 SDNode *BVN = N->getOperand(0).getNode();
4725 if (BVN->getValueType(0) != MVT::v4i32 ||
4726 BVN->getOpcode() != ISD::BUILD_VECTOR)
4727 return false;
4728 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4729 unsigned HiElt = 1 - LoElt;
4730 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4731 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4732 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4733 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4734 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4735 return false;
4736 if (isSigned) {
4737 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4738 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4739 return true;
4740 } else {
4741 if (Hi0->isNullValue() && Hi1->isNullValue())
4742 return true;
4743 }
4744 return false;
4745 }
4746
4747 if (N->getOpcode() != ISD::BUILD_VECTOR)
4748 return false;
4749
4750 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4751 SDNode *Elt = N->getOperand(i).getNode();
4752 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4753 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4754 unsigned HalfSize = EltSize / 2;
4755 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004756 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004757 return false;
4758 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004759 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004760 return false;
4761 }
4762 continue;
4763 }
4764 return false;
4765 }
4766
4767 return true;
4768}
4769
4770/// isSignExtended - Check if a node is a vector value that is sign-extended
4771/// or a constant BUILD_VECTOR with sign-extended elements.
4772static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4773 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4774 return true;
4775 if (isExtendedBUILD_VECTOR(N, DAG, true))
4776 return true;
4777 return false;
4778}
4779
4780/// isZeroExtended - Check if a node is a vector value that is zero-extended
4781/// or a constant BUILD_VECTOR with zero-extended elements.
4782static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4783 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4784 return true;
4785 if (isExtendedBUILD_VECTOR(N, DAG, false))
4786 return true;
4787 return false;
4788}
4789
4790/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4791/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004792static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4793 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4794 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004795 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4796 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4797 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004798 LD->isNonTemporal(), LD->isInvariant(),
4799 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004800 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4801 // have been legalized as a BITCAST from v4i32.
4802 if (N->getOpcode() == ISD::BITCAST) {
4803 SDNode *BVN = N->getOperand(0).getNode();
4804 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4805 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4806 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4807 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4808 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4809 }
4810 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4811 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4812 EVT VT = N->getValueType(0);
4813 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4814 unsigned NumElts = VT.getVectorNumElements();
4815 MVT TruncVT = MVT::getIntegerVT(EltSize);
4816 SmallVector<SDValue, 8> Ops;
4817 for (unsigned i = 0; i != NumElts; ++i) {
4818 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4819 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004820 // Element types smaller than 32 bits are not legal, so use i32 elements.
4821 // The values are implicitly truncated so sext vs. zext doesn't matter.
4822 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004823 }
4824 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4825 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004826}
4827
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004828static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4829 unsigned Opcode = N->getOpcode();
4830 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4831 SDNode *N0 = N->getOperand(0).getNode();
4832 SDNode *N1 = N->getOperand(1).getNode();
4833 return N0->hasOneUse() && N1->hasOneUse() &&
4834 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4835 }
4836 return false;
4837}
4838
4839static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4840 unsigned Opcode = N->getOpcode();
4841 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4842 SDNode *N0 = N->getOperand(0).getNode();
4843 SDNode *N1 = N->getOperand(1).getNode();
4844 return N0->hasOneUse() && N1->hasOneUse() &&
4845 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4846 }
4847 return false;
4848}
4849
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004850static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4851 // Multiplications are only custom-lowered for 128-bit vectors so that
4852 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4853 EVT VT = Op.getValueType();
4854 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4855 SDNode *N0 = Op.getOperand(0).getNode();
4856 SDNode *N1 = Op.getOperand(1).getNode();
4857 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004858 bool isMLA = false;
4859 bool isN0SExt = isSignExtended(N0, DAG);
4860 bool isN1SExt = isSignExtended(N1, DAG);
4861 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004862 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004863 else {
4864 bool isN0ZExt = isZeroExtended(N0, DAG);
4865 bool isN1ZExt = isZeroExtended(N1, DAG);
4866 if (isN0ZExt && isN1ZExt)
4867 NewOpc = ARMISD::VMULLu;
4868 else if (isN1SExt || isN1ZExt) {
4869 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4870 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4871 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4872 NewOpc = ARMISD::VMULLs;
4873 isMLA = true;
4874 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4875 NewOpc = ARMISD::VMULLu;
4876 isMLA = true;
4877 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4878 std::swap(N0, N1);
4879 NewOpc = ARMISD::VMULLu;
4880 isMLA = true;
4881 }
4882 }
4883
4884 if (!NewOpc) {
4885 if (VT == MVT::v2i64)
4886 // Fall through to expand this. It is not legal.
4887 return SDValue();
4888 else
4889 // Other vector multiplications are legal.
4890 return Op;
4891 }
4892 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004893
4894 // Legalize to a VMULL instruction.
4895 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004896 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004897 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004898 if (!isMLA) {
4899 Op0 = SkipExtension(N0, DAG);
4900 assert(Op0.getValueType().is64BitVector() &&
4901 Op1.getValueType().is64BitVector() &&
4902 "unexpected types for extended operands to VMULL");
4903 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4904 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004905
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004906 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4907 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4908 // vmull q0, d4, d6
4909 // vmlal q0, d5, d6
4910 // is faster than
4911 // vaddl q0, d4, d5
4912 // vmovl q1, d6
4913 // vmul q0, q0, q1
4914 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4915 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4916 EVT Op1VT = Op1.getValueType();
4917 return DAG.getNode(N0->getOpcode(), DL, VT,
4918 DAG.getNode(NewOpc, DL, VT,
4919 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4920 DAG.getNode(NewOpc, DL, VT,
4921 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004922}
4923
Owen Anderson76706012011-04-05 21:48:57 +00004924static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004925LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4926 // Convert to float
4927 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4928 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4929 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4930 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4931 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4932 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4933 // Get reciprocal estimate.
4934 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004935 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004936 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4937 // Because char has a smaller range than uchar, we can actually get away
4938 // without any newton steps. This requires that we use a weird bias
4939 // of 0xb000, however (again, this has been exhaustively tested).
4940 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4941 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4942 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4943 Y = DAG.getConstant(0xb000, MVT::i32);
4944 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4945 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4946 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4947 // Convert back to short.
4948 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4949 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4950 return X;
4951}
4952
Owen Anderson76706012011-04-05 21:48:57 +00004953static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004954LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4955 SDValue N2;
4956 // Convert to float.
4957 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4958 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4959 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4960 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4961 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4962 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004963
Nate Begeman7973f352011-02-11 20:53:29 +00004964 // Use reciprocal estimate and one refinement step.
4965 // float4 recip = vrecpeq_f32(yf);
4966 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004967 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004968 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004969 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004970 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4971 N1, N2);
4972 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4973 // Because short has a smaller range than ushort, we can actually get away
4974 // with only a single newton step. This requires that we use a weird bias
4975 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004976 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004977 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4978 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004979 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004980 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4981 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4982 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4983 // Convert back to integer and return.
4984 // return vmovn_s32(vcvt_s32_f32(result));
4985 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4986 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4987 return N0;
4988}
4989
4990static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4991 EVT VT = Op.getValueType();
4992 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4993 "unexpected type for custom-lowering ISD::SDIV");
4994
4995 DebugLoc dl = Op.getDebugLoc();
4996 SDValue N0 = Op.getOperand(0);
4997 SDValue N1 = Op.getOperand(1);
4998 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004999
Nate Begeman7973f352011-02-11 20:53:29 +00005000 if (VT == MVT::v8i8) {
5001 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5002 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005003
Nate Begeman7973f352011-02-11 20:53:29 +00005004 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5005 DAG.getIntPtrConstant(4));
5006 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005007 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005008 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5009 DAG.getIntPtrConstant(0));
5010 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5011 DAG.getIntPtrConstant(0));
5012
5013 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5014 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5015
5016 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5017 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005018
Nate Begeman7973f352011-02-11 20:53:29 +00005019 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5020 return N0;
5021 }
5022 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5023}
5024
5025static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5026 EVT VT = Op.getValueType();
5027 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5028 "unexpected type for custom-lowering ISD::UDIV");
5029
5030 DebugLoc dl = Op.getDebugLoc();
5031 SDValue N0 = Op.getOperand(0);
5032 SDValue N1 = Op.getOperand(1);
5033 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005034
Nate Begeman7973f352011-02-11 20:53:29 +00005035 if (VT == MVT::v8i8) {
5036 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5037 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005038
Nate Begeman7973f352011-02-11 20:53:29 +00005039 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5040 DAG.getIntPtrConstant(4));
5041 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005042 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005043 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5044 DAG.getIntPtrConstant(0));
5045 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5046 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005047
Nate Begeman7973f352011-02-11 20:53:29 +00005048 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5049 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005050
Nate Begeman7973f352011-02-11 20:53:29 +00005051 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5052 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005053
5054 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005055 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5056 N0);
5057 return N0;
5058 }
Owen Anderson76706012011-04-05 21:48:57 +00005059
Nate Begeman7973f352011-02-11 20:53:29 +00005060 // v4i16 sdiv ... Convert to float.
5061 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5062 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5063 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5064 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5065 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005066 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005067
5068 // Use reciprocal estimate and two refinement steps.
5069 // float4 recip = vrecpeq_f32(yf);
5070 // recip *= vrecpsq_f32(yf, recip);
5071 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005072 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005073 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005074 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005075 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005076 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005077 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005078 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005079 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005080 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005081 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5082 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5083 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5084 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005085 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005086 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5087 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5088 N1 = DAG.getConstant(2, MVT::i32);
5089 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5090 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5091 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5092 // Convert back to integer and return.
5093 // return vmovn_u32(vcvt_s32_f32(result));
5094 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5095 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5096 return N0;
5097}
5098
Evan Cheng342e3162011-08-30 01:34:54 +00005099static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5100 EVT VT = Op.getNode()->getValueType(0);
5101 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5102
5103 unsigned Opc;
5104 bool ExtraOp = false;
5105 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005106 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005107 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5108 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5109 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5110 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5111 }
5112
5113 if (!ExtraOp)
5114 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5115 Op.getOperand(1));
5116 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5117 Op.getOperand(1), Op.getOperand(2));
5118}
5119
Eli Friedman74bf18c2011-09-15 22:26:18 +00005120static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005121 // Monotonic load/store is legal for all targets
5122 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5123 return Op;
5124
5125 // Aquire/Release load/store is not legal for targets without a
5126 // dmb or equivalent available.
5127 return SDValue();
5128}
5129
5130
Eli Friedman2bdffe42011-08-31 00:31:29 +00005131static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005132ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5133 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005134 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005135 assert (Node->getValueType(0) == MVT::i64 &&
5136 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005137
Eli Friedman4d3f3292011-08-31 17:52:22 +00005138 SmallVector<SDValue, 6> Ops;
5139 Ops.push_back(Node->getOperand(0)); // Chain
5140 Ops.push_back(Node->getOperand(1)); // Ptr
5141 // Low part of Val1
5142 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5143 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5144 // High part of Val1
5145 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5146 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005147 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005148 // High part of Val1
5149 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5150 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5151 // High part of Val2
5152 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5153 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5154 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005155 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5156 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005157 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005158 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005159 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005160 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5161 Results.push_back(Result.getValue(2));
5162}
5163
Dan Gohmand858e902010-04-17 15:26:15 +00005164SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005165 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005166 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005167 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005168 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005169 case ISD::GlobalAddress:
5170 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5171 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005172 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005173 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005174 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5175 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005176 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005177 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005178 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005179 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005180 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005181 case ISD::SINT_TO_FP:
5182 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5183 case ISD::FP_TO_SINT:
5184 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005185 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005186 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005187 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005188 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005189 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005190 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005191 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5192 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005193 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005194 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005195 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005196 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005197 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005198 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005199 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005200 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005201 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005202 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005203 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005204 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005205 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005206 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005207 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005208 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005209 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005210 case ISD::SDIV: return LowerSDIV(Op, DAG);
5211 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005212 case ISD::ADDC:
5213 case ISD::ADDE:
5214 case ISD::SUBC:
5215 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005216 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005217 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005218 }
Evan Chenga8e29892007-01-19 07:51:42 +00005219}
5220
Duncan Sands1607f052008-12-01 11:39:25 +00005221/// ReplaceNodeResults - Replace the results of node with an illegal result
5222/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005223void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5224 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005225 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005226 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005227 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005228 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005229 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005230 case ISD::BITCAST:
5231 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005232 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005233 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005234 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005235 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005236 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005237 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005238 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005239 return;
5240 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005241 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005242 return;
5243 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005244 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005245 return;
5246 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005247 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005248 return;
5249 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005250 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005251 return;
5252 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005253 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005254 return;
5255 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005256 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005257 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005258 case ISD::ATOMIC_CMP_SWAP:
5259 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5260 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005261 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005262 if (Res.getNode())
5263 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005264}
Chris Lattner27a6c732007-11-24 07:07:01 +00005265
Evan Chenga8e29892007-01-19 07:51:42 +00005266//===----------------------------------------------------------------------===//
5267// ARM Scheduler Hooks
5268//===----------------------------------------------------------------------===//
5269
5270MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005271ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5272 MachineBasicBlock *BB,
5273 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005274 unsigned dest = MI->getOperand(0).getReg();
5275 unsigned ptr = MI->getOperand(1).getReg();
5276 unsigned oldval = MI->getOperand(2).getReg();
5277 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005278 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5279 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005280 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005281
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005282 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005283 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5284 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5285 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005286
5287 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005288 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5289 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5290 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005291 }
5292
Jim Grosbach5278eb82009-12-11 01:42:04 +00005293 unsigned ldrOpc, strOpc;
5294 switch (Size) {
5295 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005296 case 1:
5297 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005298 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005299 break;
5300 case 2:
5301 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5302 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5303 break;
5304 case 4:
5305 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5306 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5307 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005308 }
5309
5310 MachineFunction *MF = BB->getParent();
5311 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5312 MachineFunction::iterator It = BB;
5313 ++It; // insert the new blocks after the current block
5314
5315 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5316 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5317 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5318 MF->insert(It, loop1MBB);
5319 MF->insert(It, loop2MBB);
5320 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005321
5322 // Transfer the remainder of BB and its successor edges to exitMBB.
5323 exitMBB->splice(exitMBB->begin(), BB,
5324 llvm::next(MachineBasicBlock::iterator(MI)),
5325 BB->end());
5326 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005327
5328 // thisMBB:
5329 // ...
5330 // fallthrough --> loop1MBB
5331 BB->addSuccessor(loop1MBB);
5332
5333 // loop1MBB:
5334 // ldrex dest, [ptr]
5335 // cmp dest, oldval
5336 // bne exitMBB
5337 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005338 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5339 if (ldrOpc == ARM::t2LDREX)
5340 MIB.addImm(0);
5341 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005342 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005343 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005344 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5345 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005346 BB->addSuccessor(loop2MBB);
5347 BB->addSuccessor(exitMBB);
5348
5349 // loop2MBB:
5350 // strex scratch, newval, [ptr]
5351 // cmp scratch, #0
5352 // bne loop1MBB
5353 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005354 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5355 if (strOpc == ARM::t2STREX)
5356 MIB.addImm(0);
5357 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005358 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005359 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005360 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5361 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005362 BB->addSuccessor(loop1MBB);
5363 BB->addSuccessor(exitMBB);
5364
5365 // exitMBB:
5366 // ...
5367 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005368
Dan Gohman14152b42010-07-06 20:24:04 +00005369 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005370
Jim Grosbach5278eb82009-12-11 01:42:04 +00005371 return BB;
5372}
5373
5374MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005375ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5376 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005377 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5378 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5379
5380 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005381 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005382 MachineFunction::iterator It = BB;
5383 ++It;
5384
5385 unsigned dest = MI->getOperand(0).getReg();
5386 unsigned ptr = MI->getOperand(1).getReg();
5387 unsigned incr = MI->getOperand(2).getReg();
5388 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005389 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005390
5391 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5392 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005393 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5394 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005395 }
5396
Jim Grosbachc3c23542009-12-14 04:22:04 +00005397 unsigned ldrOpc, strOpc;
5398 switch (Size) {
5399 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005400 case 1:
5401 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005402 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005403 break;
5404 case 2:
5405 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5406 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5407 break;
5408 case 4:
5409 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5410 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5411 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005412 }
5413
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005414 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5415 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5416 MF->insert(It, loopMBB);
5417 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005418
5419 // Transfer the remainder of BB and its successor edges to exitMBB.
5420 exitMBB->splice(exitMBB->begin(), BB,
5421 llvm::next(MachineBasicBlock::iterator(MI)),
5422 BB->end());
5423 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005424
Craig Topper420761a2012-04-20 07:30:17 +00005425 const TargetRegisterClass *TRC = isThumb2 ?
5426 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5427 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005428 unsigned scratch = MRI.createVirtualRegister(TRC);
5429 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005430
5431 // thisMBB:
5432 // ...
5433 // fallthrough --> loopMBB
5434 BB->addSuccessor(loopMBB);
5435
5436 // loopMBB:
5437 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005438 // <binop> scratch2, dest, incr
5439 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005440 // cmp scratch, #0
5441 // bne- loopMBB
5442 // fallthrough --> exitMBB
5443 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005444 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5445 if (ldrOpc == ARM::t2LDREX)
5446 MIB.addImm(0);
5447 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005448 if (BinOpcode) {
5449 // operand order needs to go the other way for NAND
5450 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5451 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5452 addReg(incr).addReg(dest)).addReg(0);
5453 else
5454 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5455 addReg(dest).addReg(incr)).addReg(0);
5456 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005457
Jim Grosbachb6aed502011-09-09 18:37:27 +00005458 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5459 if (strOpc == ARM::t2STREX)
5460 MIB.addImm(0);
5461 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005462 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005463 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005464 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5465 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005466
5467 BB->addSuccessor(loopMBB);
5468 BB->addSuccessor(exitMBB);
5469
5470 // exitMBB:
5471 // ...
5472 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005473
Dan Gohman14152b42010-07-06 20:24:04 +00005474 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005475
Jim Grosbachc3c23542009-12-14 04:22:04 +00005476 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005477}
5478
Jim Grosbachf7da8822011-04-26 19:44:18 +00005479MachineBasicBlock *
5480ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5481 MachineBasicBlock *BB,
5482 unsigned Size,
5483 bool signExtend,
5484 ARMCC::CondCodes Cond) const {
5485 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5486
5487 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5488 MachineFunction *MF = BB->getParent();
5489 MachineFunction::iterator It = BB;
5490 ++It;
5491
5492 unsigned dest = MI->getOperand(0).getReg();
5493 unsigned ptr = MI->getOperand(1).getReg();
5494 unsigned incr = MI->getOperand(2).getReg();
5495 unsigned oldval = dest;
5496 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005497 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005498
5499 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5500 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005501 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5502 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005503 }
5504
Jim Grosbachf7da8822011-04-26 19:44:18 +00005505 unsigned ldrOpc, strOpc, extendOpc;
5506 switch (Size) {
5507 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5508 case 1:
5509 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5510 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005511 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005512 break;
5513 case 2:
5514 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5515 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005516 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005517 break;
5518 case 4:
5519 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5520 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5521 extendOpc = 0;
5522 break;
5523 }
5524
5525 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5526 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5527 MF->insert(It, loopMBB);
5528 MF->insert(It, exitMBB);
5529
5530 // Transfer the remainder of BB and its successor edges to exitMBB.
5531 exitMBB->splice(exitMBB->begin(), BB,
5532 llvm::next(MachineBasicBlock::iterator(MI)),
5533 BB->end());
5534 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5535
Craig Topper420761a2012-04-20 07:30:17 +00005536 const TargetRegisterClass *TRC = isThumb2 ?
5537 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5538 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005539 unsigned scratch = MRI.createVirtualRegister(TRC);
5540 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005541
5542 // thisMBB:
5543 // ...
5544 // fallthrough --> loopMBB
5545 BB->addSuccessor(loopMBB);
5546
5547 // loopMBB:
5548 // ldrex dest, ptr
5549 // (sign extend dest, if required)
5550 // cmp dest, incr
5551 // cmov.cond scratch2, dest, incr
5552 // strex scratch, scratch2, ptr
5553 // cmp scratch, #0
5554 // bne- loopMBB
5555 // fallthrough --> exitMBB
5556 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005557 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5558 if (ldrOpc == ARM::t2LDREX)
5559 MIB.addImm(0);
5560 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005561
5562 // Sign extend the value, if necessary.
5563 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005564 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005565 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5566 .addReg(dest)
5567 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005568 }
5569
5570 // Build compare and cmov instructions.
5571 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5572 .addReg(oldval).addReg(incr));
5573 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5574 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5575
Jim Grosbachb6aed502011-09-09 18:37:27 +00005576 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5577 if (strOpc == ARM::t2STREX)
5578 MIB.addImm(0);
5579 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005580 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5581 .addReg(scratch).addImm(0));
5582 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5583 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5584
5585 BB->addSuccessor(loopMBB);
5586 BB->addSuccessor(exitMBB);
5587
5588 // exitMBB:
5589 // ...
5590 BB = exitMBB;
5591
5592 MI->eraseFromParent(); // The instruction is gone now.
5593
5594 return BB;
5595}
5596
Eli Friedman2bdffe42011-08-31 00:31:29 +00005597MachineBasicBlock *
5598ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5599 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005600 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005601 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5602 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5603
5604 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5605 MachineFunction *MF = BB->getParent();
5606 MachineFunction::iterator It = BB;
5607 ++It;
5608
5609 unsigned destlo = MI->getOperand(0).getReg();
5610 unsigned desthi = MI->getOperand(1).getReg();
5611 unsigned ptr = MI->getOperand(2).getReg();
5612 unsigned vallo = MI->getOperand(3).getReg();
5613 unsigned valhi = MI->getOperand(4).getReg();
5614 DebugLoc dl = MI->getDebugLoc();
5615 bool isThumb2 = Subtarget->isThumb2();
5616
5617 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5618 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005619 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5620 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5621 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005622 }
5623
5624 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5625 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5626
5627 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005628 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005629 if (IsCmpxchg) {
5630 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5631 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5632 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005633 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5634 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005635 if (IsCmpxchg) {
5636 MF->insert(It, contBB);
5637 MF->insert(It, cont2BB);
5638 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005639 MF->insert(It, exitMBB);
5640
5641 // Transfer the remainder of BB and its successor edges to exitMBB.
5642 exitMBB->splice(exitMBB->begin(), BB,
5643 llvm::next(MachineBasicBlock::iterator(MI)),
5644 BB->end());
5645 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5646
Craig Topper420761a2012-04-20 07:30:17 +00005647 const TargetRegisterClass *TRC = isThumb2 ?
5648 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5649 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005650 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5651
5652 // thisMBB:
5653 // ...
5654 // fallthrough --> loopMBB
5655 BB->addSuccessor(loopMBB);
5656
5657 // loopMBB:
5658 // ldrexd r2, r3, ptr
5659 // <binopa> r0, r2, incr
5660 // <binopb> r1, r3, incr
5661 // strexd storesuccess, r0, r1, ptr
5662 // cmp storesuccess, #0
5663 // bne- loopMBB
5664 // fallthrough --> exitMBB
5665 //
5666 // Note that the registers are explicitly specified because there is not any
5667 // way to force the register allocator to allocate a register pair.
5668 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005669 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005670 // need to properly enforce the restriction that the two output registers
5671 // for ldrexd must be different.
5672 BB = loopMBB;
5673 // Load
5674 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5675 .addReg(ARM::R2, RegState::Define)
5676 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5677 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5678 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5679 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005680
5681 if (IsCmpxchg) {
5682 // Add early exit
5683 for (unsigned i = 0; i < 2; i++) {
5684 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5685 ARM::CMPrr))
5686 .addReg(i == 0 ? destlo : desthi)
5687 .addReg(i == 0 ? vallo : valhi));
5688 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5689 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5690 BB->addSuccessor(exitMBB);
5691 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5692 BB = (i == 0 ? contBB : cont2BB);
5693 }
5694
5695 // Copy to physregs for strexd
5696 unsigned setlo = MI->getOperand(5).getReg();
5697 unsigned sethi = MI->getOperand(6).getReg();
5698 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5699 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5700 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005701 // Perform binary operation
5702 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5703 .addReg(destlo).addReg(vallo))
5704 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5705 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5706 .addReg(desthi).addReg(valhi)).addReg(0);
5707 } else {
5708 // Copy to physregs for strexd
5709 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5710 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5711 }
5712
5713 // Store
5714 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5715 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5716 // Cmp+jump
5717 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5718 .addReg(storesuccess).addImm(0));
5719 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5720 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5721
5722 BB->addSuccessor(loopMBB);
5723 BB->addSuccessor(exitMBB);
5724
5725 // exitMBB:
5726 // ...
5727 BB = exitMBB;
5728
5729 MI->eraseFromParent(); // The instruction is gone now.
5730
5731 return BB;
5732}
5733
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005734/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5735/// registers the function context.
5736void ARMTargetLowering::
5737SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5738 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005739 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5740 DebugLoc dl = MI->getDebugLoc();
5741 MachineFunction *MF = MBB->getParent();
5742 MachineRegisterInfo *MRI = &MF->getRegInfo();
5743 MachineConstantPool *MCP = MF->getConstantPool();
5744 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5745 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005746
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005747 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005748 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005749
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005750 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005751 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005752 ARMConstantPoolValue *CPV =
5753 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5754 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5755
Craig Topper420761a2012-04-20 07:30:17 +00005756 const TargetRegisterClass *TRC = isThumb ?
5757 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5758 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005759
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005760 // Grab constant pool and fixed stack memory operands.
5761 MachineMemOperand *CPMMO =
5762 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5763 MachineMemOperand::MOLoad, 4, 4);
5764
5765 MachineMemOperand *FIMMOSt =
5766 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5767 MachineMemOperand::MOStore, 4, 4);
5768
5769 // Load the address of the dispatch MBB into the jump buffer.
5770 if (isThumb2) {
5771 // Incoming value: jbuf
5772 // ldr.n r5, LCPI1_1
5773 // orr r5, r5, #1
5774 // add r5, pc
5775 // str r5, [$jbuf, #+4] ; &jbuf[1]
5776 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5777 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5778 .addConstantPoolIndex(CPI)
5779 .addMemOperand(CPMMO));
5780 // Set the low bit because of thumb mode.
5781 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5782 AddDefaultCC(
5783 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5784 .addReg(NewVReg1, RegState::Kill)
5785 .addImm(0x01)));
5786 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5787 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5788 .addReg(NewVReg2, RegState::Kill)
5789 .addImm(PCLabelId);
5790 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5791 .addReg(NewVReg3, RegState::Kill)
5792 .addFrameIndex(FI)
5793 .addImm(36) // &jbuf[1] :: pc
5794 .addMemOperand(FIMMOSt));
5795 } else if (isThumb) {
5796 // Incoming value: jbuf
5797 // ldr.n r1, LCPI1_4
5798 // add r1, pc
5799 // mov r2, #1
5800 // orrs r1, r2
5801 // add r2, $jbuf, #+4 ; &jbuf[1]
5802 // str r1, [r2]
5803 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5804 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5805 .addConstantPoolIndex(CPI)
5806 .addMemOperand(CPMMO));
5807 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5808 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5809 .addReg(NewVReg1, RegState::Kill)
5810 .addImm(PCLabelId);
5811 // Set the low bit because of thumb mode.
5812 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5813 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5814 .addReg(ARM::CPSR, RegState::Define)
5815 .addImm(1));
5816 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5817 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5818 .addReg(ARM::CPSR, RegState::Define)
5819 .addReg(NewVReg2, RegState::Kill)
5820 .addReg(NewVReg3, RegState::Kill));
5821 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5822 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5823 .addFrameIndex(FI)
5824 .addImm(36)); // &jbuf[1] :: pc
5825 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5826 .addReg(NewVReg4, RegState::Kill)
5827 .addReg(NewVReg5, RegState::Kill)
5828 .addImm(0)
5829 .addMemOperand(FIMMOSt));
5830 } else {
5831 // Incoming value: jbuf
5832 // ldr r1, LCPI1_1
5833 // add r1, pc, r1
5834 // str r1, [$jbuf, #+4] ; &jbuf[1]
5835 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5836 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5837 .addConstantPoolIndex(CPI)
5838 .addImm(0)
5839 .addMemOperand(CPMMO));
5840 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5841 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5842 .addReg(NewVReg1, RegState::Kill)
5843 .addImm(PCLabelId));
5844 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5845 .addReg(NewVReg2, RegState::Kill)
5846 .addFrameIndex(FI)
5847 .addImm(36) // &jbuf[1] :: pc
5848 .addMemOperand(FIMMOSt));
5849 }
5850}
5851
5852MachineBasicBlock *ARMTargetLowering::
5853EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5854 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5855 DebugLoc dl = MI->getDebugLoc();
5856 MachineFunction *MF = MBB->getParent();
5857 MachineRegisterInfo *MRI = &MF->getRegInfo();
5858 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5859 MachineFrameInfo *MFI = MF->getFrameInfo();
5860 int FI = MFI->getFunctionContextIndex();
5861
Craig Topper420761a2012-04-20 07:30:17 +00005862 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5863 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005864 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005865
Bill Wendling04f15b42011-10-06 21:29:56 +00005866 // Get a mapping of the call site numbers to all of the landing pads they're
5867 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005868 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5869 unsigned MaxCSNum = 0;
5870 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005871 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5872 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005873 if (!BB->isLandingPad()) continue;
5874
5875 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5876 // pad.
5877 for (MachineBasicBlock::iterator
5878 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5879 if (!II->isEHLabel()) continue;
5880
5881 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005882 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005883
Bill Wendling5cbef192011-10-05 23:28:57 +00005884 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5885 for (SmallVectorImpl<unsigned>::iterator
5886 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5887 CSI != CSE; ++CSI) {
5888 CallSiteNumToLPad[*CSI].push_back(BB);
5889 MaxCSNum = std::max(MaxCSNum, *CSI);
5890 }
Bill Wendling2a850152011-10-05 00:02:33 +00005891 break;
5892 }
5893 }
5894
5895 // Get an ordered list of the machine basic blocks for the jump table.
5896 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005897 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005898 LPadList.reserve(CallSiteNumToLPad.size());
5899 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5900 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5901 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005902 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005903 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005904 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5905 }
Bill Wendling2a850152011-10-05 00:02:33 +00005906 }
5907
Bill Wendling5cbef192011-10-05 23:28:57 +00005908 assert(!LPadList.empty() &&
5909 "No landing pad destinations for the dispatch jump table!");
5910
Bill Wendling04f15b42011-10-06 21:29:56 +00005911 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005912 MachineJumpTableInfo *JTI =
5913 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5914 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5915 unsigned UId = AFI->createJumpTableUId();
5916
Bill Wendling04f15b42011-10-06 21:29:56 +00005917 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005918
5919 // Shove the dispatch's address into the return slot in the function context.
5920 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5921 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005922
Bill Wendlingbb734682011-10-05 00:39:32 +00005923 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005924 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005925 DispatchBB->addSuccessor(TrapBB);
5926
5927 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5928 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005929
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005930 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005931 MF->insert(MF->end(), DispatchBB);
5932 MF->insert(MF->end(), DispContBB);
5933 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005934
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005935 // Insert code into the entry block that creates and registers the function
5936 // context.
5937 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5938
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005939 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005940 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005941 MachineMemOperand::MOLoad |
5942 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005943
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005944 if (AFI->isThumb1OnlyFunction())
5945 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5946 else if (!Subtarget->hasVFP2())
5947 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005948 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005949 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005950
Bill Wendling952cb502011-10-18 22:49:07 +00005951 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005952 if (Subtarget->isThumb2()) {
5953 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5954 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5955 .addFrameIndex(FI)
5956 .addImm(4)
5957 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005958
Bill Wendling952cb502011-10-18 22:49:07 +00005959 if (NumLPads < 256) {
5960 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5961 .addReg(NewVReg1)
5962 .addImm(LPadList.size()));
5963 } else {
5964 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5965 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005966 .addImm(NumLPads & 0xFFFF));
5967
5968 unsigned VReg2 = VReg1;
5969 if ((NumLPads & 0xFFFF0000) != 0) {
5970 VReg2 = MRI->createVirtualRegister(TRC);
5971 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5972 .addReg(VReg1)
5973 .addImm(NumLPads >> 16));
5974 }
5975
Bill Wendling952cb502011-10-18 22:49:07 +00005976 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5977 .addReg(NewVReg1)
5978 .addReg(VReg2));
5979 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005980
Bill Wendling95ce2e92011-10-06 22:53:00 +00005981 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5982 .addMBB(TrapBB)
5983 .addImm(ARMCC::HI)
5984 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005985
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005986 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5987 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005988 .addJumpTableIndex(MJTI)
5989 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005990
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005991 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005992 AddDefaultCC(
5993 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005994 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5995 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005996 .addReg(NewVReg1)
5997 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5998
5999 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006000 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006001 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006002 .addJumpTableIndex(MJTI)
6003 .addImm(UId);
6004 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006005 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6006 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6007 .addFrameIndex(FI)
6008 .addImm(1)
6009 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006010
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006011 if (NumLPads < 256) {
6012 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6013 .addReg(NewVReg1)
6014 .addImm(NumLPads));
6015 } else {
6016 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006017 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6018 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6019
6020 // MachineConstantPool wants an explicit alignment.
6021 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6022 if (Align == 0)
6023 Align = getTargetData()->getTypeAllocSize(C->getType());
6024 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006025
6026 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6027 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6028 .addReg(VReg1, RegState::Define)
6029 .addConstantPoolIndex(Idx));
6030 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6031 .addReg(NewVReg1)
6032 .addReg(VReg1));
6033 }
6034
Bill Wendling083a8eb2011-10-06 23:37:36 +00006035 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6036 .addMBB(TrapBB)
6037 .addImm(ARMCC::HI)
6038 .addReg(ARM::CPSR);
6039
6040 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6041 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6042 .addReg(ARM::CPSR, RegState::Define)
6043 .addReg(NewVReg1)
6044 .addImm(2));
6045
6046 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006047 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006048 .addJumpTableIndex(MJTI)
6049 .addImm(UId));
6050
6051 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6052 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6053 .addReg(ARM::CPSR, RegState::Define)
6054 .addReg(NewVReg2, RegState::Kill)
6055 .addReg(NewVReg3));
6056
6057 MachineMemOperand *JTMMOLd =
6058 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6059 MachineMemOperand::MOLoad, 4, 4);
6060
6061 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6062 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6063 .addReg(NewVReg4, RegState::Kill)
6064 .addImm(0)
6065 .addMemOperand(JTMMOLd));
6066
6067 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6068 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6069 .addReg(ARM::CPSR, RegState::Define)
6070 .addReg(NewVReg5, RegState::Kill)
6071 .addReg(NewVReg3));
6072
6073 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6074 .addReg(NewVReg6, RegState::Kill)
6075 .addJumpTableIndex(MJTI)
6076 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006077 } else {
6078 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6079 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6080 .addFrameIndex(FI)
6081 .addImm(4)
6082 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006083
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006084 if (NumLPads < 256) {
6085 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6086 .addReg(NewVReg1)
6087 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006088 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006089 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6090 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006091 .addImm(NumLPads & 0xFFFF));
6092
6093 unsigned VReg2 = VReg1;
6094 if ((NumLPads & 0xFFFF0000) != 0) {
6095 VReg2 = MRI->createVirtualRegister(TRC);
6096 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6097 .addReg(VReg1)
6098 .addImm(NumLPads >> 16));
6099 }
6100
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006101 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6102 .addReg(NewVReg1)
6103 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006104 } else {
6105 MachineConstantPool *ConstantPool = MF->getConstantPool();
6106 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6107 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6108
6109 // MachineConstantPool wants an explicit alignment.
6110 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6111 if (Align == 0)
6112 Align = getTargetData()->getTypeAllocSize(C->getType());
6113 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6114
6115 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6116 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6117 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006118 .addConstantPoolIndex(Idx)
6119 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006120 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6121 .addReg(NewVReg1)
6122 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006123 }
6124
Bill Wendling95ce2e92011-10-06 22:53:00 +00006125 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6126 .addMBB(TrapBB)
6127 .addImm(ARMCC::HI)
6128 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006129
Bill Wendling564392b2011-10-18 22:11:18 +00006130 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006131 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006132 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006133 .addReg(NewVReg1)
6134 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006135 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6136 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006137 .addJumpTableIndex(MJTI)
6138 .addImm(UId));
6139
6140 MachineMemOperand *JTMMOLd =
6141 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6142 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006143 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006144 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006145 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6146 .addReg(NewVReg3, RegState::Kill)
6147 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006148 .addImm(0)
6149 .addMemOperand(JTMMOLd));
6150
6151 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006152 .addReg(NewVReg5, RegState::Kill)
6153 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006154 .addJumpTableIndex(MJTI)
6155 .addImm(UId);
6156 }
Bill Wendling2a850152011-10-05 00:02:33 +00006157
Bill Wendlingbb734682011-10-05 00:39:32 +00006158 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006159 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006160 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006161 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6162 MachineBasicBlock *CurMBB = *I;
6163 if (PrevMBB != CurMBB)
6164 DispContBB->addSuccessor(CurMBB);
6165 PrevMBB = CurMBB;
6166 }
6167
Bill Wendling24bb9252011-10-17 05:25:09 +00006168 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006169 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6170 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006171 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006172 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006173 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6174 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6175 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006176
6177 // Remove the landing pad successor from the invoke block and replace it
6178 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006179 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6180 BB->succ_end());
6181 while (!Successors.empty()) {
6182 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006183 if (SMBB->isLandingPad()) {
6184 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006185 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006186 }
6187 }
6188
6189 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006190
6191 // Find the invoke call and mark all of the callee-saved registers as
6192 // 'implicit defined' so that they're spilled. This prevents code from
6193 // moving instructions to before the EH block, where they will never be
6194 // executed.
6195 for (MachineBasicBlock::reverse_iterator
6196 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006197 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006198
6199 DenseMap<unsigned, bool> DefRegs;
6200 for (MachineInstr::mop_iterator
6201 OI = II->operands_begin(), OE = II->operands_end();
6202 OI != OE; ++OI) {
6203 if (!OI->isReg()) continue;
6204 DefRegs[OI->getReg()] = true;
6205 }
6206
6207 MachineInstrBuilder MIB(&*II);
6208
Bill Wendling5d798592011-10-14 23:55:44 +00006209 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006210 unsigned Reg = SavedRegs[i];
6211 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006212 !ARM::tGPRRegClass.contains(Reg) &&
6213 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006214 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006215 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006216 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006217 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006218 continue;
6219 if (!DefRegs[Reg])
6220 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006221 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006222
6223 break;
6224 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006225 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006226
Bill Wendlingf7b02072011-10-18 18:30:49 +00006227 // Mark all former landing pads as non-landing pads. The dispatch is the only
6228 // landing pad now.
6229 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6230 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6231 (*I)->setIsLandingPad(false);
6232
Bill Wendlingbb734682011-10-05 00:39:32 +00006233 // The instruction is gone now.
6234 MI->eraseFromParent();
6235
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006236 return MBB;
6237}
6238
Evan Cheng218977b2010-07-13 19:27:42 +00006239static
6240MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6241 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6242 E = MBB->succ_end(); I != E; ++I)
6243 if (*I != Succ)
6244 return *I;
6245 llvm_unreachable("Expecting a BB with two successors!");
6246}
6247
Manman Ren68f25572012-06-01 19:33:18 +00006248MachineBasicBlock *ARMTargetLowering::
6249EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6250 // This pseudo instruction has 3 operands: dst, src, size
6251 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6252 // Otherwise, we will generate unrolled scalar copies.
6253 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6254 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6255 MachineFunction::iterator It = BB;
6256 ++It;
6257
6258 unsigned dest = MI->getOperand(0).getReg();
6259 unsigned src = MI->getOperand(1).getReg();
6260 unsigned SizeVal = MI->getOperand(2).getImm();
6261 unsigned Align = MI->getOperand(3).getImm();
6262 DebugLoc dl = MI->getDebugLoc();
6263
6264 bool isThumb2 = Subtarget->isThumb2();
6265 MachineFunction *MF = BB->getParent();
6266 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006267 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006268
6269 const TargetRegisterClass *TRC = isThumb2 ?
6270 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6271 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006272 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006273
6274 if (Align & 1) {
6275 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6276 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6277 UnitSize = 1;
6278 } else if (Align & 2) {
6279 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6280 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6281 UnitSize = 2;
6282 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006283 // Check whether we can use NEON instructions.
6284 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6285 Subtarget->hasNEON()) {
6286 if ((Align % 16 == 0) && SizeVal >= 16) {
6287 ldrOpc = ARM::VLD1q32wb_fixed;
6288 strOpc = ARM::VST1q32wb_fixed;
6289 UnitSize = 16;
6290 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6291 }
6292 else if ((Align % 8 == 0) && SizeVal >= 8) {
6293 ldrOpc = ARM::VLD1d32wb_fixed;
6294 strOpc = ARM::VST1d32wb_fixed;
6295 UnitSize = 8;
6296 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6297 }
6298 }
6299 // Can't use NEON instructions.
6300 if (UnitSize == 0) {
6301 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6302 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6303 UnitSize = 4;
6304 }
Manman Ren68f25572012-06-01 19:33:18 +00006305 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006306
Manman Ren68f25572012-06-01 19:33:18 +00006307 unsigned BytesLeft = SizeVal % UnitSize;
6308 unsigned LoopSize = SizeVal - BytesLeft;
6309
6310 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6311 // Use LDR and STR to copy.
6312 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6313 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6314 unsigned srcIn = src;
6315 unsigned destIn = dest;
6316 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006317 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006318 unsigned srcOut = MRI.createVirtualRegister(TRC);
6319 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006320 if (UnitSize >= 8) {
6321 AddDefaultPred(BuildMI(*BB, MI, dl,
6322 TII->get(ldrOpc), scratch)
6323 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6324
6325 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6326 .addReg(destIn).addImm(0).addReg(scratch));
6327 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006328 AddDefaultPred(BuildMI(*BB, MI, dl,
6329 TII->get(ldrOpc), scratch)
6330 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6331
6332 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6333 .addReg(scratch).addReg(destIn)
6334 .addImm(UnitSize));
6335 } else {
6336 AddDefaultPred(BuildMI(*BB, MI, dl,
6337 TII->get(ldrOpc), scratch)
6338 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6339 .addImm(UnitSize));
6340
6341 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6342 .addReg(scratch).addReg(destIn)
6343 .addReg(0).addImm(UnitSize));
6344 }
6345 srcIn = srcOut;
6346 destIn = destOut;
6347 }
6348
6349 // Handle the leftover bytes with LDRB and STRB.
6350 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6351 // [destOut] = STRB_POST(scratch, destIn, 1)
6352 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6353 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6354 for (unsigned i = 0; i < BytesLeft; i++) {
6355 unsigned scratch = MRI.createVirtualRegister(TRC);
6356 unsigned srcOut = MRI.createVirtualRegister(TRC);
6357 unsigned destOut = MRI.createVirtualRegister(TRC);
6358 if (isThumb2) {
6359 AddDefaultPred(BuildMI(*BB, MI, dl,
6360 TII->get(ldrOpc),scratch)
6361 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6362
6363 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6364 .addReg(scratch).addReg(destIn)
6365 .addReg(0).addImm(1));
6366 } else {
6367 AddDefaultPred(BuildMI(*BB, MI, dl,
6368 TII->get(ldrOpc),scratch)
6369 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6370
6371 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6372 .addReg(scratch).addReg(destIn)
6373 .addReg(0).addImm(1));
6374 }
6375 srcIn = srcOut;
6376 destIn = destOut;
6377 }
6378 MI->eraseFromParent(); // The instruction is gone now.
6379 return BB;
6380 }
6381
6382 // Expand the pseudo op to a loop.
6383 // thisMBB:
6384 // ...
6385 // movw varEnd, # --> with thumb2
6386 // movt varEnd, #
6387 // ldrcp varEnd, idx --> without thumb2
6388 // fallthrough --> loopMBB
6389 // loopMBB:
6390 // PHI varPhi, varEnd, varLoop
6391 // PHI srcPhi, src, srcLoop
6392 // PHI destPhi, dst, destLoop
6393 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6394 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6395 // subs varLoop, varPhi, #UnitSize
6396 // bne loopMBB
6397 // fallthrough --> exitMBB
6398 // exitMBB:
6399 // epilogue to handle left-over bytes
6400 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6401 // [destOut] = STRB_POST(scratch, destLoop, 1)
6402 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6403 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6404 MF->insert(It, loopMBB);
6405 MF->insert(It, exitMBB);
6406
6407 // Transfer the remainder of BB and its successor edges to exitMBB.
6408 exitMBB->splice(exitMBB->begin(), BB,
6409 llvm::next(MachineBasicBlock::iterator(MI)),
6410 BB->end());
6411 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6412
6413 // Load an immediate to varEnd.
6414 unsigned varEnd = MRI.createVirtualRegister(TRC);
6415 if (isThumb2) {
6416 unsigned VReg1 = varEnd;
6417 if ((LoopSize & 0xFFFF0000) != 0)
6418 VReg1 = MRI.createVirtualRegister(TRC);
6419 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6420 .addImm(LoopSize & 0xFFFF));
6421
6422 if ((LoopSize & 0xFFFF0000) != 0)
6423 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6424 .addReg(VReg1)
6425 .addImm(LoopSize >> 16));
6426 } else {
6427 MachineConstantPool *ConstantPool = MF->getConstantPool();
6428 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6429 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6430
6431 // MachineConstantPool wants an explicit alignment.
6432 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6433 if (Align == 0)
6434 Align = getTargetData()->getTypeAllocSize(C->getType());
6435 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6436
6437 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6438 .addReg(varEnd, RegState::Define)
6439 .addConstantPoolIndex(Idx)
6440 .addImm(0));
6441 }
6442 BB->addSuccessor(loopMBB);
6443
6444 // Generate the loop body:
6445 // varPhi = PHI(varLoop, varEnd)
6446 // srcPhi = PHI(srcLoop, src)
6447 // destPhi = PHI(destLoop, dst)
6448 MachineBasicBlock *entryBB = BB;
6449 BB = loopMBB;
6450 unsigned varLoop = MRI.createVirtualRegister(TRC);
6451 unsigned varPhi = MRI.createVirtualRegister(TRC);
6452 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6453 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6454 unsigned destLoop = MRI.createVirtualRegister(TRC);
6455 unsigned destPhi = MRI.createVirtualRegister(TRC);
6456
6457 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6458 .addReg(varLoop).addMBB(loopMBB)
6459 .addReg(varEnd).addMBB(entryBB);
6460 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6461 .addReg(srcLoop).addMBB(loopMBB)
6462 .addReg(src).addMBB(entryBB);
6463 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6464 .addReg(destLoop).addMBB(loopMBB)
6465 .addReg(dest).addMBB(entryBB);
6466
6467 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6468 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006469 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6470 if (UnitSize >= 8) {
6471 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6472 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6473
6474 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6475 .addReg(destPhi).addImm(0).addReg(scratch));
6476 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006477 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6478 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6479
6480 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6481 .addReg(scratch).addReg(destPhi)
6482 .addImm(UnitSize));
6483 } else {
6484 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6485 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6486 .addImm(UnitSize));
6487
6488 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6489 .addReg(scratch).addReg(destPhi)
6490 .addReg(0).addImm(UnitSize));
6491 }
6492
6493 // Decrement loop variable by UnitSize.
6494 MachineInstrBuilder MIB = BuildMI(BB, dl,
6495 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6496 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6497 MIB->getOperand(5).setReg(ARM::CPSR);
6498 MIB->getOperand(5).setIsDef(true);
6499
6500 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6501 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6502
6503 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6504 BB->addSuccessor(loopMBB);
6505 BB->addSuccessor(exitMBB);
6506
6507 // Add epilogue to handle BytesLeft.
6508 BB = exitMBB;
6509 MachineInstr *StartOfExit = exitMBB->begin();
6510 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6511 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6512
6513 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6514 // [destOut] = STRB_POST(scratch, destLoop, 1)
6515 unsigned srcIn = srcLoop;
6516 unsigned destIn = destLoop;
6517 for (unsigned i = 0; i < BytesLeft; i++) {
6518 unsigned scratch = MRI.createVirtualRegister(TRC);
6519 unsigned srcOut = MRI.createVirtualRegister(TRC);
6520 unsigned destOut = MRI.createVirtualRegister(TRC);
6521 if (isThumb2) {
6522 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6523 TII->get(ldrOpc),scratch)
6524 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6525
6526 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6527 .addReg(scratch).addReg(destIn)
6528 .addImm(1));
6529 } else {
6530 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6531 TII->get(ldrOpc),scratch)
6532 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6533
6534 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6535 .addReg(scratch).addReg(destIn)
6536 .addReg(0).addImm(1));
6537 }
6538 srcIn = srcOut;
6539 destIn = destOut;
6540 }
6541
6542 MI->eraseFromParent(); // The instruction is gone now.
6543 return BB;
6544}
6545
Jim Grosbache801dc42009-12-12 01:40:06 +00006546MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006547ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006548 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006549 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006550 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006551 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006552 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006553 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006554 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006555 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006556 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006557 // The Thumb2 pre-indexed stores have the same MI operands, they just
6558 // define them differently in the .td files from the isel patterns, so
6559 // they need pseudos.
6560 case ARM::t2STR_preidx:
6561 MI->setDesc(TII->get(ARM::t2STR_PRE));
6562 return BB;
6563 case ARM::t2STRB_preidx:
6564 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6565 return BB;
6566 case ARM::t2STRH_preidx:
6567 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6568 return BB;
6569
Jim Grosbach19dec202011-08-05 20:35:44 +00006570 case ARM::STRi_preidx:
6571 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006572 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006573 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6574 // Decode the offset.
6575 unsigned Offset = MI->getOperand(4).getImm();
6576 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6577 Offset = ARM_AM::getAM2Offset(Offset);
6578 if (isSub)
6579 Offset = -Offset;
6580
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006581 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006582 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006583 .addOperand(MI->getOperand(0)) // Rn_wb
6584 .addOperand(MI->getOperand(1)) // Rt
6585 .addOperand(MI->getOperand(2)) // Rn
6586 .addImm(Offset) // offset (skip GPR==zero_reg)
6587 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006588 .addOperand(MI->getOperand(6))
6589 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006590 MI->eraseFromParent();
6591 return BB;
6592 }
6593 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006594 case ARM::STRBr_preidx:
6595 case ARM::STRH_preidx: {
6596 unsigned NewOpc;
6597 switch (MI->getOpcode()) {
6598 default: llvm_unreachable("unexpected opcode!");
6599 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6600 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6601 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6602 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006603 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6604 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6605 MIB.addOperand(MI->getOperand(i));
6606 MI->eraseFromParent();
6607 return BB;
6608 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006609 case ARM::ATOMIC_LOAD_ADD_I8:
6610 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6611 case ARM::ATOMIC_LOAD_ADD_I16:
6612 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6613 case ARM::ATOMIC_LOAD_ADD_I32:
6614 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006615
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006616 case ARM::ATOMIC_LOAD_AND_I8:
6617 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6618 case ARM::ATOMIC_LOAD_AND_I16:
6619 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6620 case ARM::ATOMIC_LOAD_AND_I32:
6621 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006622
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006623 case ARM::ATOMIC_LOAD_OR_I8:
6624 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6625 case ARM::ATOMIC_LOAD_OR_I16:
6626 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6627 case ARM::ATOMIC_LOAD_OR_I32:
6628 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006629
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006630 case ARM::ATOMIC_LOAD_XOR_I8:
6631 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6632 case ARM::ATOMIC_LOAD_XOR_I16:
6633 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6634 case ARM::ATOMIC_LOAD_XOR_I32:
6635 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006636
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006637 case ARM::ATOMIC_LOAD_NAND_I8:
6638 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6639 case ARM::ATOMIC_LOAD_NAND_I16:
6640 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6641 case ARM::ATOMIC_LOAD_NAND_I32:
6642 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006643
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006644 case ARM::ATOMIC_LOAD_SUB_I8:
6645 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6646 case ARM::ATOMIC_LOAD_SUB_I16:
6647 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6648 case ARM::ATOMIC_LOAD_SUB_I32:
6649 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006650
Jim Grosbachf7da8822011-04-26 19:44:18 +00006651 case ARM::ATOMIC_LOAD_MIN_I8:
6652 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6653 case ARM::ATOMIC_LOAD_MIN_I16:
6654 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6655 case ARM::ATOMIC_LOAD_MIN_I32:
6656 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6657
6658 case ARM::ATOMIC_LOAD_MAX_I8:
6659 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6660 case ARM::ATOMIC_LOAD_MAX_I16:
6661 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6662 case ARM::ATOMIC_LOAD_MAX_I32:
6663 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6664
6665 case ARM::ATOMIC_LOAD_UMIN_I8:
6666 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6667 case ARM::ATOMIC_LOAD_UMIN_I16:
6668 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6669 case ARM::ATOMIC_LOAD_UMIN_I32:
6670 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6671
6672 case ARM::ATOMIC_LOAD_UMAX_I8:
6673 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6674 case ARM::ATOMIC_LOAD_UMAX_I16:
6675 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6676 case ARM::ATOMIC_LOAD_UMAX_I32:
6677 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6678
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006679 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6680 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6681 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006682
6683 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6684 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6685 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006686
Eli Friedman2bdffe42011-08-31 00:31:29 +00006687
6688 case ARM::ATOMADD6432:
6689 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006690 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6691 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006692 case ARM::ATOMSUB6432:
6693 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006694 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6695 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006696 case ARM::ATOMOR6432:
6697 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006698 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006699 case ARM::ATOMXOR6432:
6700 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006701 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006702 case ARM::ATOMAND6432:
6703 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006704 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006705 case ARM::ATOMSWAP6432:
6706 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006707 case ARM::ATOMCMPXCHG6432:
6708 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6709 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6710 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006711
Evan Cheng007ea272009-08-12 05:17:19 +00006712 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006713 // To "insert" a SELECT_CC instruction, we actually have to insert the
6714 // diamond control-flow pattern. The incoming instruction knows the
6715 // destination vreg to set, the condition code register to branch on, the
6716 // true/false values to select between, and a branch opcode to use.
6717 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006718 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006719 ++It;
6720
6721 // thisMBB:
6722 // ...
6723 // TrueVal = ...
6724 // cmpTY ccX, r1, r2
6725 // bCC copy1MBB
6726 // fallthrough --> copy0MBB
6727 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006728 MachineFunction *F = BB->getParent();
6729 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6730 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006731 F->insert(It, copy0MBB);
6732 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006733
6734 // Transfer the remainder of BB and its successor edges to sinkMBB.
6735 sinkMBB->splice(sinkMBB->begin(), BB,
6736 llvm::next(MachineBasicBlock::iterator(MI)),
6737 BB->end());
6738 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6739
Dan Gohman258c58c2010-07-06 15:49:48 +00006740 BB->addSuccessor(copy0MBB);
6741 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006742
Dan Gohman14152b42010-07-06 20:24:04 +00006743 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6744 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6745
Evan Chenga8e29892007-01-19 07:51:42 +00006746 // copy0MBB:
6747 // %FalseValue = ...
6748 // # fallthrough to sinkMBB
6749 BB = copy0MBB;
6750
6751 // Update machine-CFG edges
6752 BB->addSuccessor(sinkMBB);
6753
6754 // sinkMBB:
6755 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6756 // ...
6757 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006758 BuildMI(*BB, BB->begin(), dl,
6759 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006760 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6761 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6762
Dan Gohman14152b42010-07-06 20:24:04 +00006763 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006764 return BB;
6765 }
Evan Cheng86198642009-08-07 00:34:42 +00006766
Evan Cheng218977b2010-07-13 19:27:42 +00006767 case ARM::BCCi64:
6768 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006769 // If there is an unconditional branch to the other successor, remove it.
6770 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006771
Evan Cheng218977b2010-07-13 19:27:42 +00006772 // Compare both parts that make up the double comparison separately for
6773 // equality.
6774 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6775
6776 unsigned LHS1 = MI->getOperand(1).getReg();
6777 unsigned LHS2 = MI->getOperand(2).getReg();
6778 if (RHSisZero) {
6779 AddDefaultPred(BuildMI(BB, dl,
6780 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6781 .addReg(LHS1).addImm(0));
6782 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6783 .addReg(LHS2).addImm(0)
6784 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6785 } else {
6786 unsigned RHS1 = MI->getOperand(3).getReg();
6787 unsigned RHS2 = MI->getOperand(4).getReg();
6788 AddDefaultPred(BuildMI(BB, dl,
6789 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6790 .addReg(LHS1).addReg(RHS1));
6791 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6792 .addReg(LHS2).addReg(RHS2)
6793 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6794 }
6795
6796 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6797 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6798 if (MI->getOperand(0).getImm() == ARMCC::NE)
6799 std::swap(destMBB, exitMBB);
6800
6801 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6802 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006803 if (isThumb2)
6804 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6805 else
6806 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006807
6808 MI->eraseFromParent(); // The pseudo instruction is gone now.
6809 return BB;
6810 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006811
Bill Wendling5bc85282011-10-17 20:37:20 +00006812 case ARM::Int_eh_sjlj_setjmp:
6813 case ARM::Int_eh_sjlj_setjmp_nofp:
6814 case ARM::tInt_eh_sjlj_setjmp:
6815 case ARM::t2Int_eh_sjlj_setjmp:
6816 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6817 EmitSjLjDispatchBlock(MI, BB);
6818 return BB;
6819
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006820 case ARM::ABS:
6821 case ARM::t2ABS: {
6822 // To insert an ABS instruction, we have to insert the
6823 // diamond control-flow pattern. The incoming instruction knows the
6824 // source vreg to test against 0, the destination vreg to set,
6825 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006826 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006827 // It transforms
6828 // V1 = ABS V0
6829 // into
6830 // V2 = MOVS V0
6831 // BCC (branch to SinkBB if V0 >= 0)
6832 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006833 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006834 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6835 MachineFunction::iterator BBI = BB;
6836 ++BBI;
6837 MachineFunction *Fn = BB->getParent();
6838 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6839 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6840 Fn->insert(BBI, RSBBB);
6841 Fn->insert(BBI, SinkBB);
6842
6843 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6844 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6845 bool isThumb2 = Subtarget->isThumb2();
6846 MachineRegisterInfo &MRI = Fn->getRegInfo();
6847 // In Thumb mode S must not be specified if source register is the SP or
6848 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006849 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6850 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6851 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006852
6853 // Transfer the remainder of BB and its successor edges to sinkMBB.
6854 SinkBB->splice(SinkBB->begin(), BB,
6855 llvm::next(MachineBasicBlock::iterator(MI)),
6856 BB->end());
6857 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6858
6859 BB->addSuccessor(RSBBB);
6860 BB->addSuccessor(SinkBB);
6861
6862 // fall through to SinkMBB
6863 RSBBB->addSuccessor(SinkBB);
6864
Manman Ren307473d2012-06-15 21:32:12 +00006865 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006866 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006867 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6868 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006869
6870 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006871 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006872 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6873 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6874
6875 // insert rsbri in RSBBB
6876 // Note: BCC and rsbri will be converted into predicated rsbmi
6877 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006878 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006879 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006880 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006881 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6882
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006883 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006884 // reuse ABSDstReg to not change uses of ABS instruction
6885 BuildMI(*SinkBB, SinkBB->begin(), dl,
6886 TII->get(ARM::PHI), ABSDstReg)
6887 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006888 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006889
6890 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006891 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006892
6893 // return last added BB
6894 return SinkBB;
6895 }
Manman Ren68f25572012-06-01 19:33:18 +00006896 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006897 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006898 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006899 }
6900}
6901
Evan Cheng37fefc22011-08-30 19:09:48 +00006902void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6903 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006904 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006905 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6906 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6907 return;
6908 }
6909
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006910 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006911 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6912 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6913 // operand is still set to noreg. If needed, set the optional operand's
6914 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006915 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006916 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006917
Andrew Trick3be654f2011-09-21 02:20:46 +00006918 // Rename pseudo opcodes.
6919 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6920 if (NewOpc) {
6921 const ARMBaseInstrInfo *TII =
6922 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006923 MCID = &TII->get(NewOpc);
6924
6925 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6926 "converted opcode should be the same except for cc_out");
6927
6928 MI->setDesc(*MCID);
6929
6930 // Add the optional cc_out operand
6931 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006932 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006933 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006934
6935 // Any ARM instruction that sets the 's' bit should specify an optional
6936 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006937 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006938 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006939 return;
6940 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006941 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6942 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006943 bool definesCPSR = false;
6944 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006945 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006946 i != e; ++i) {
6947 const MachineOperand &MO = MI->getOperand(i);
6948 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6949 definesCPSR = true;
6950 if (MO.isDead())
6951 deadCPSR = true;
6952 MI->RemoveOperand(i);
6953 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006954 }
6955 }
Andrew Trick4815d562011-09-20 03:17:40 +00006956 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006957 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006958 return;
6959 }
6960 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006961 if (deadCPSR) {
6962 assert(!MI->getOperand(ccOutIdx).getReg() &&
6963 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006964 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006965 }
Andrew Trick4815d562011-09-20 03:17:40 +00006966
Andrew Trick3be654f2011-09-21 02:20:46 +00006967 // If this instruction was defined with an optional CPSR def and its dag node
6968 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006969 MachineOperand &MO = MI->getOperand(ccOutIdx);
6970 MO.setReg(ARM::CPSR);
6971 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006972}
6973
Evan Chenga8e29892007-01-19 07:51:42 +00006974//===----------------------------------------------------------------------===//
6975// ARM Optimization Hooks
6976//===----------------------------------------------------------------------===//
6977
Chris Lattnerd1980a52009-03-12 06:52:53 +00006978static
6979SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6980 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006981 SelectionDAG &DAG = DCI.DAG;
6982 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006983 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006984 unsigned Opc = N->getOpcode();
6985 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6986 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6987 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6988 ISD::CondCode CC = ISD::SETCC_INVALID;
6989
6990 if (isSlctCC) {
6991 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6992 } else {
6993 SDValue CCOp = Slct.getOperand(0);
6994 if (CCOp.getOpcode() == ISD::SETCC)
6995 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6996 }
6997
6998 bool DoXform = false;
6999 bool InvCC = false;
7000 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
7001 "Bad input!");
7002
7003 if (LHS.getOpcode() == ISD::Constant &&
7004 cast<ConstantSDNode>(LHS)->isNullValue()) {
7005 DoXform = true;
7006 } else if (CC != ISD::SETCC_INVALID &&
7007 RHS.getOpcode() == ISD::Constant &&
7008 cast<ConstantSDNode>(RHS)->isNullValue()) {
7009 std::swap(LHS, RHS);
7010 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007011 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00007012 Op0.getOperand(0).getValueType();
7013 bool isInt = OpVT.isInteger();
7014 CC = ISD::getSetCCInverse(CC, isInt);
7015
7016 if (!TLI.isCondCodeLegal(CC, OpVT))
7017 return SDValue(); // Inverse operator isn't legal.
7018
7019 DoXform = true;
7020 InvCC = true;
7021 }
7022
7023 if (DoXform) {
7024 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
7025 if (isSlctCC)
7026 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
7027 Slct.getOperand(0), Slct.getOperand(1), CC);
7028 SDValue CCOp = Slct.getOperand(0);
7029 if (InvCC)
7030 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
7031 CCOp.getOperand(0), CCOp.getOperand(1), CC);
7032 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
7033 CCOp, OtherOp, Result);
7034 }
7035 return SDValue();
7036}
7037
Eric Christopherfa6f5912011-06-29 21:10:36 +00007038// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007039// (only after legalization).
7040static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7041 TargetLowering::DAGCombinerInfo &DCI,
7042 const ARMSubtarget *Subtarget) {
7043
7044 // Only perform optimization if after legalize, and if NEON is available. We
7045 // also expected both operands to be BUILD_VECTORs.
7046 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7047 || N0.getOpcode() != ISD::BUILD_VECTOR
7048 || N1.getOpcode() != ISD::BUILD_VECTOR)
7049 return SDValue();
7050
7051 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7052 EVT VT = N->getValueType(0);
7053 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7054 return SDValue();
7055
7056 // Check that the vector operands are of the right form.
7057 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7058 // operands, where N is the size of the formed vector.
7059 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7060 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007061
7062 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007063 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007064 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007065 SDValue Vec = N0->getOperand(0)->getOperand(0);
7066 SDNode *V = Vec.getNode();
7067 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007068
Eric Christopherfa6f5912011-06-29 21:10:36 +00007069 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007070 // check to see if each of their operands are an EXTRACT_VECTOR with
7071 // the same vector and appropriate index.
7072 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7073 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7074 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007075
Tanya Lattner189531f2011-06-14 23:48:48 +00007076 SDValue ExtVec0 = N0->getOperand(i);
7077 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007078
Tanya Lattner189531f2011-06-14 23:48:48 +00007079 // First operand is the vector, verify its the same.
7080 if (V != ExtVec0->getOperand(0).getNode() ||
7081 V != ExtVec1->getOperand(0).getNode())
7082 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007083
Tanya Lattner189531f2011-06-14 23:48:48 +00007084 // Second is the constant, verify its correct.
7085 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7086 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007087
Tanya Lattner189531f2011-06-14 23:48:48 +00007088 // For the constant, we want to see all the even or all the odd.
7089 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7090 || C1->getZExtValue() != nextIndex+1)
7091 return SDValue();
7092
7093 // Increment index.
7094 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007095 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007096 return SDValue();
7097 }
7098
7099 // Create VPADDL node.
7100 SelectionDAG &DAG = DCI.DAG;
7101 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007102
7103 // Build operand list.
7104 SmallVector<SDValue, 8> Ops;
7105 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7106 TLI.getPointerTy()));
7107
7108 // Input is the vector.
7109 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007110
Tanya Lattner189531f2011-06-14 23:48:48 +00007111 // Get widened type and narrowed type.
7112 MVT widenType;
7113 unsigned numElem = VT.getVectorNumElements();
7114 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7115 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7116 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7117 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7118 default:
Craig Topperbc219812012-02-07 02:50:20 +00007119 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007120 }
7121
7122 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7123 widenType, &Ops[0], Ops.size());
7124 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7125}
7126
Bob Wilson3d5792a2010-07-29 20:34:14 +00007127/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7128/// operands N0 and N1. This is a helper for PerformADDCombine that is
7129/// called with the default operands, and if that fails, with commuted
7130/// operands.
7131static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007132 TargetLowering::DAGCombinerInfo &DCI,
7133 const ARMSubtarget *Subtarget){
7134
7135 // Attempt to create vpaddl for this add.
7136 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7137 if (Result.getNode())
7138 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007139
Chris Lattnerd1980a52009-03-12 06:52:53 +00007140 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7141 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7142 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7143 if (Result.getNode()) return Result;
7144 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007145 return SDValue();
7146}
7147
Bob Wilson3d5792a2010-07-29 20:34:14 +00007148/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7149///
7150static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007151 TargetLowering::DAGCombinerInfo &DCI,
7152 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007153 SDValue N0 = N->getOperand(0);
7154 SDValue N1 = N->getOperand(1);
7155
7156 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007157 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007158 if (Result.getNode())
7159 return Result;
7160
7161 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007162 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007163}
7164
Chris Lattnerd1980a52009-03-12 06:52:53 +00007165/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007166///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007167static SDValue PerformSUBCombine(SDNode *N,
7168 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007169 SDValue N0 = N->getOperand(0);
7170 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007171
Chris Lattnerd1980a52009-03-12 06:52:53 +00007172 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7173 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7174 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7175 if (Result.getNode()) return Result;
7176 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007177
Chris Lattnerd1980a52009-03-12 06:52:53 +00007178 return SDValue();
7179}
7180
Evan Cheng463d3582011-03-31 19:38:48 +00007181/// PerformVMULCombine
7182/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7183/// special multiplier accumulator forwarding.
7184/// vmul d3, d0, d2
7185/// vmla d3, d1, d2
7186/// is faster than
7187/// vadd d3, d0, d1
7188/// vmul d3, d3, d2
7189static SDValue PerformVMULCombine(SDNode *N,
7190 TargetLowering::DAGCombinerInfo &DCI,
7191 const ARMSubtarget *Subtarget) {
7192 if (!Subtarget->hasVMLxForwarding())
7193 return SDValue();
7194
7195 SelectionDAG &DAG = DCI.DAG;
7196 SDValue N0 = N->getOperand(0);
7197 SDValue N1 = N->getOperand(1);
7198 unsigned Opcode = N0.getOpcode();
7199 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7200 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007201 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007202 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7203 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7204 return SDValue();
7205 std::swap(N0, N1);
7206 }
7207
7208 EVT VT = N->getValueType(0);
7209 DebugLoc DL = N->getDebugLoc();
7210 SDValue N00 = N0->getOperand(0);
7211 SDValue N01 = N0->getOperand(1);
7212 return DAG.getNode(Opcode, DL, VT,
7213 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7214 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7215}
7216
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007217static SDValue PerformMULCombine(SDNode *N,
7218 TargetLowering::DAGCombinerInfo &DCI,
7219 const ARMSubtarget *Subtarget) {
7220 SelectionDAG &DAG = DCI.DAG;
7221
7222 if (Subtarget->isThumb1Only())
7223 return SDValue();
7224
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007225 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7226 return SDValue();
7227
7228 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007229 if (VT.is64BitVector() || VT.is128BitVector())
7230 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007231 if (VT != MVT::i32)
7232 return SDValue();
7233
7234 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7235 if (!C)
7236 return SDValue();
7237
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007238 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007239 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007240
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007241 ShiftAmt = ShiftAmt & (32 - 1);
7242 SDValue V = N->getOperand(0);
7243 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007244
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007245 SDValue Res;
7246 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007247
7248 if (MulAmt >= 0) {
7249 if (isPowerOf2_32(MulAmt - 1)) {
7250 // (mul x, 2^N + 1) => (add (shl x, N), x)
7251 Res = DAG.getNode(ISD::ADD, DL, VT,
7252 V,
7253 DAG.getNode(ISD::SHL, DL, VT,
7254 V,
7255 DAG.getConstant(Log2_32(MulAmt - 1),
7256 MVT::i32)));
7257 } else if (isPowerOf2_32(MulAmt + 1)) {
7258 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7259 Res = DAG.getNode(ISD::SUB, DL, VT,
7260 DAG.getNode(ISD::SHL, DL, VT,
7261 V,
7262 DAG.getConstant(Log2_32(MulAmt + 1),
7263 MVT::i32)),
7264 V);
7265 } else
7266 return SDValue();
7267 } else {
7268 uint64_t MulAmtAbs = -MulAmt;
7269 if (isPowerOf2_32(MulAmtAbs + 1)) {
7270 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7271 Res = DAG.getNode(ISD::SUB, DL, VT,
7272 V,
7273 DAG.getNode(ISD::SHL, DL, VT,
7274 V,
7275 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7276 MVT::i32)));
7277 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7278 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7279 Res = DAG.getNode(ISD::ADD, DL, VT,
7280 V,
7281 DAG.getNode(ISD::SHL, DL, VT,
7282 V,
7283 DAG.getConstant(Log2_32(MulAmtAbs-1),
7284 MVT::i32)));
7285 Res = DAG.getNode(ISD::SUB, DL, VT,
7286 DAG.getConstant(0, MVT::i32),Res);
7287
7288 } else
7289 return SDValue();
7290 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007291
7292 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007293 Res = DAG.getNode(ISD::SHL, DL, VT,
7294 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007295
7296 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007297 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007298 return SDValue();
7299}
7300
Evan Chengc892aeb2012-02-23 01:19:06 +00007301static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7302 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7303 return false;
7304
7305 SDValue FalseVal = N.getOperand(0);
7306 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7307 if (!C)
7308 return false;
7309 if (AllOnes)
7310 return C->isAllOnesValue();
7311 return C->isNullValue();
7312}
7313
7314/// formConditionalOp - Combine an operation with a conditional move operand
7315/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7316/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7317static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7318 bool Commutable) {
7319 SDValue N0 = N->getOperand(0);
7320 SDValue N1 = N->getOperand(1);
7321
7322 bool isAND = N->getOpcode() == ISD::AND;
7323 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7324 if (!isCand && Commutable) {
7325 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7326 if (isCand)
7327 std::swap(N0, N1);
7328 }
7329 if (!isCand)
7330 return SDValue();
7331
7332 unsigned Opc = 0;
7333 switch (N->getOpcode()) {
7334 default: llvm_unreachable("Unexpected node");
7335 case ISD::AND: Opc = ARMISD::CAND; break;
7336 case ISD::OR: Opc = ARMISD::COR; break;
7337 case ISD::XOR: Opc = ARMISD::CXOR; break;
7338 }
7339 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7340 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7341 N1.getOperand(4));
7342}
7343
Owen Anderson080c0922010-11-05 19:27:46 +00007344static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007345 TargetLowering::DAGCombinerInfo &DCI,
7346 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007347
Owen Anderson080c0922010-11-05 19:27:46 +00007348 // Attempt to use immediate-form VBIC
7349 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7350 DebugLoc dl = N->getDebugLoc();
7351 EVT VT = N->getValueType(0);
7352 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007353
Tanya Lattner0433b212011-04-07 15:24:20 +00007354 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7355 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007356
Owen Anderson080c0922010-11-05 19:27:46 +00007357 APInt SplatBits, SplatUndef;
7358 unsigned SplatBitSize;
7359 bool HasAnyUndefs;
7360 if (BVN &&
7361 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7362 if (SplatBitSize <= 64) {
7363 EVT VbicVT;
7364 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7365 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007366 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007367 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007368 if (Val.getNode()) {
7369 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007370 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007371 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007372 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007373 }
7374 }
7375 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007376
Evan Chengc892aeb2012-02-23 01:19:06 +00007377 if (!Subtarget->isThumb1Only()) {
7378 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7379 SDValue CAND = formConditionalOp(N, DAG, true);
7380 if (CAND.getNode())
7381 return CAND;
7382 }
7383
Owen Anderson080c0922010-11-05 19:27:46 +00007384 return SDValue();
7385}
7386
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007387/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7388static SDValue PerformORCombine(SDNode *N,
7389 TargetLowering::DAGCombinerInfo &DCI,
7390 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007391 // Attempt to use immediate-form VORR
7392 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7393 DebugLoc dl = N->getDebugLoc();
7394 EVT VT = N->getValueType(0);
7395 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007396
Tanya Lattner0433b212011-04-07 15:24:20 +00007397 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7398 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007399
Owen Anderson60f48702010-11-03 23:15:26 +00007400 APInt SplatBits, SplatUndef;
7401 unsigned SplatBitSize;
7402 bool HasAnyUndefs;
7403 if (BVN && Subtarget->hasNEON() &&
7404 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7405 if (SplatBitSize <= 64) {
7406 EVT VorrVT;
7407 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7408 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007409 DAG, VorrVT, VT.is128BitVector(),
7410 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007411 if (Val.getNode()) {
7412 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007413 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007414 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007415 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007416 }
7417 }
7418 }
7419
Evan Chengc892aeb2012-02-23 01:19:06 +00007420 if (!Subtarget->isThumb1Only()) {
7421 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7422 SDValue COR = formConditionalOp(N, DAG, true);
7423 if (COR.getNode())
7424 return COR;
7425 }
7426
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007427 SDValue N0 = N->getOperand(0);
7428 if (N0.getOpcode() != ISD::AND)
7429 return SDValue();
7430 SDValue N1 = N->getOperand(1);
7431
7432 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7433 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7434 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7435 APInt SplatUndef;
7436 unsigned SplatBitSize;
7437 bool HasAnyUndefs;
7438
7439 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7440 APInt SplatBits0;
7441 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7442 HasAnyUndefs) && !HasAnyUndefs) {
7443 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7444 APInt SplatBits1;
7445 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7446 HasAnyUndefs) && !HasAnyUndefs &&
7447 SplatBits0 == ~SplatBits1) {
7448 // Canonicalize the vector type to make instruction selection simpler.
7449 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7450 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7451 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007452 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007453 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7454 }
7455 }
7456 }
7457
Jim Grosbach54238562010-07-17 03:30:54 +00007458 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7459 // reasonable.
7460
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007461 // BFI is only available on V6T2+
7462 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7463 return SDValue();
7464
Jim Grosbach54238562010-07-17 03:30:54 +00007465 DebugLoc DL = N->getDebugLoc();
7466 // 1) or (and A, mask), val => ARMbfi A, val, mask
7467 // iff (val & mask) == val
7468 //
7469 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7470 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007471 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007472 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007473 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007474 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007475
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007476 if (VT != MVT::i32)
7477 return SDValue();
7478
Evan Cheng30fb13f2010-12-13 20:32:54 +00007479 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007480
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007481 // The value and the mask need to be constants so we can verify this is
7482 // actually a bitfield set. If the mask is 0xffff, we can do better
7483 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007484 SDValue MaskOp = N0.getOperand(1);
7485 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7486 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007487 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007488 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007489 if (Mask == 0xffff)
7490 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007491 SDValue Res;
7492 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007493 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7494 if (N1C) {
7495 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007496 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007497 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007498
Evan Chenga9688c42010-12-11 04:11:38 +00007499 if (ARM::isBitFieldInvertedMask(Mask)) {
7500 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007501
Evan Cheng30fb13f2010-12-13 20:32:54 +00007502 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007503 DAG.getConstant(Val, MVT::i32),
7504 DAG.getConstant(Mask, MVT::i32));
7505
7506 // Do not add new nodes to DAG combiner worklist.
7507 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007508 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007509 }
Jim Grosbach54238562010-07-17 03:30:54 +00007510 } else if (N1.getOpcode() == ISD::AND) {
7511 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007512 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7513 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007514 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007515 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007516
Eric Christopher29aeed12011-03-26 01:21:03 +00007517 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7518 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007519 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007520 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007521 // The pack halfword instruction works better for masks that fit it,
7522 // so use that when it's available.
7523 if (Subtarget->hasT2ExtractPack() &&
7524 (Mask == 0xffff || Mask == 0xffff0000))
7525 return SDValue();
7526 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007527 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007528 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007529 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007530 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007531 DAG.getConstant(Mask, MVT::i32));
7532 // Do not add new nodes to DAG combiner worklist.
7533 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007534 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007535 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007536 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007537 // The pack halfword instruction works better for masks that fit it,
7538 // so use that when it's available.
7539 if (Subtarget->hasT2ExtractPack() &&
7540 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7541 return SDValue();
7542 // 2b
7543 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007544 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007545 DAG.getConstant(lsb, MVT::i32));
7546 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007547 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007548 // Do not add new nodes to DAG combiner worklist.
7549 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007550 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007551 }
7552 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007553
Evan Cheng30fb13f2010-12-13 20:32:54 +00007554 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7555 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7556 ARM::isBitFieldInvertedMask(~Mask)) {
7557 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7558 // where lsb(mask) == #shamt and masked bits of B are known zero.
7559 SDValue ShAmt = N00.getOperand(1);
7560 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7561 unsigned LSB = CountTrailingZeros_32(Mask);
7562 if (ShAmtC != LSB)
7563 return SDValue();
7564
7565 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7566 DAG.getConstant(~Mask, MVT::i32));
7567
7568 // Do not add new nodes to DAG combiner worklist.
7569 DCI.CombineTo(N, Res, false);
7570 }
7571
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007572 return SDValue();
7573}
7574
Evan Chengc892aeb2012-02-23 01:19:06 +00007575static SDValue PerformXORCombine(SDNode *N,
7576 TargetLowering::DAGCombinerInfo &DCI,
7577 const ARMSubtarget *Subtarget) {
7578 EVT VT = N->getValueType(0);
7579 SelectionDAG &DAG = DCI.DAG;
7580
7581 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7582 return SDValue();
7583
7584 if (!Subtarget->isThumb1Only()) {
7585 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7586 SDValue CXOR = formConditionalOp(N, DAG, true);
7587 if (CXOR.getNode())
7588 return CXOR;
7589 }
7590
7591 return SDValue();
7592}
7593
Evan Chengbf188ae2011-06-15 01:12:31 +00007594/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7595/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007596static SDValue PerformBFICombine(SDNode *N,
7597 TargetLowering::DAGCombinerInfo &DCI) {
7598 SDValue N1 = N->getOperand(1);
7599 if (N1.getOpcode() == ISD::AND) {
7600 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7601 if (!N11C)
7602 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007603 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7604 unsigned LSB = CountTrailingZeros_32(~InvMask);
7605 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7606 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007607 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007608 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007609 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7610 N->getOperand(0), N1.getOperand(0),
7611 N->getOperand(2));
7612 }
7613 return SDValue();
7614}
7615
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007616/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7617/// ARMISD::VMOVRRD.
7618static SDValue PerformVMOVRRDCombine(SDNode *N,
7619 TargetLowering::DAGCombinerInfo &DCI) {
7620 // vmovrrd(vmovdrr x, y) -> x,y
7621 SDValue InDouble = N->getOperand(0);
7622 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7623 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007624
7625 // vmovrrd(load f64) -> (load i32), (load i32)
7626 SDNode *InNode = InDouble.getNode();
7627 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7628 InNode->getValueType(0) == MVT::f64 &&
7629 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7630 !cast<LoadSDNode>(InNode)->isVolatile()) {
7631 // TODO: Should this be done for non-FrameIndex operands?
7632 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7633
7634 SelectionDAG &DAG = DCI.DAG;
7635 DebugLoc DL = LD->getDebugLoc();
7636 SDValue BasePtr = LD->getBasePtr();
7637 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7638 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007639 LD->isNonTemporal(), LD->isInvariant(),
7640 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007641
7642 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7643 DAG.getConstant(4, MVT::i32));
7644 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7645 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007646 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007647 std::min(4U, LD->getAlignment() / 2));
7648
7649 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7650 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7651 DCI.RemoveFromWorklist(LD);
7652 DAG.DeleteNode(LD);
7653 return Result;
7654 }
7655
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007656 return SDValue();
7657}
7658
7659/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7660/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7661static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7662 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7663 SDValue Op0 = N->getOperand(0);
7664 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007665 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007666 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007667 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007668 Op1 = Op1.getOperand(0);
7669 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7670 Op0.getNode() == Op1.getNode() &&
7671 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007672 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007673 N->getValueType(0), Op0.getOperand(0));
7674 return SDValue();
7675}
7676
Bob Wilson31600902010-12-21 06:43:19 +00007677/// PerformSTORECombine - Target-specific dag combine xforms for
7678/// ISD::STORE.
7679static SDValue PerformSTORECombine(SDNode *N,
7680 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007681 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007682 if (St->isVolatile())
7683 return SDValue();
7684
Andrew Trick49b446f2012-07-18 18:34:24 +00007685 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007686 // pack all of the elements in one place. Next, store to memory in fewer
7687 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007688 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007689 EVT VT = StVal.getValueType();
7690 if (St->isTruncatingStore() && VT.isVector()) {
7691 SelectionDAG &DAG = DCI.DAG;
7692 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7693 EVT StVT = St->getMemoryVT();
7694 unsigned NumElems = VT.getVectorNumElements();
7695 assert(StVT != VT && "Cannot truncate to the same type");
7696 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7697 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7698
7699 // From, To sizes and ElemCount must be pow of two
7700 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7701
7702 // We are going to use the original vector elt for storing.
7703 // Accumulated smaller vector elements must be a multiple of the store size.
7704 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7705
7706 unsigned SizeRatio = FromEltSz / ToEltSz;
7707 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7708
7709 // Create a type on which we perform the shuffle.
7710 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7711 NumElems*SizeRatio);
7712 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7713
7714 DebugLoc DL = St->getDebugLoc();
7715 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7716 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7717 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7718
7719 // Can't shuffle using an illegal type.
7720 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7721
7722 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7723 DAG.getUNDEF(WideVec.getValueType()),
7724 ShuffleVec.data());
7725 // At this point all of the data is stored at the bottom of the
7726 // register. We now need to save it to mem.
7727
7728 // Find the largest store unit
7729 MVT StoreType = MVT::i8;
7730 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7731 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7732 MVT Tp = (MVT::SimpleValueType)tp;
7733 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7734 StoreType = Tp;
7735 }
7736 // Didn't find a legal store type.
7737 if (!TLI.isTypeLegal(StoreType))
7738 return SDValue();
7739
7740 // Bitcast the original vector into a vector of store-size units
7741 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7742 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7743 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7744 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7745 SmallVector<SDValue, 8> Chains;
7746 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7747 TLI.getPointerTy());
7748 SDValue BasePtr = St->getBasePtr();
7749
7750 // Perform one or more big stores into memory.
7751 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7752 for (unsigned I = 0; I < E; I++) {
7753 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7754 StoreType, ShuffWide,
7755 DAG.getIntPtrConstant(I));
7756 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7757 St->getPointerInfo(), St->isVolatile(),
7758 St->isNonTemporal(), St->getAlignment());
7759 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7760 Increment);
7761 Chains.push_back(Ch);
7762 }
7763 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7764 Chains.size());
7765 }
7766
7767 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007768 return SDValue();
7769
Chad Rosier96b66d62012-04-09 19:38:15 +00007770 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7771 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007772 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007773 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007774 SelectionDAG &DAG = DCI.DAG;
7775 DebugLoc DL = St->getDebugLoc();
7776 SDValue BasePtr = St->getBasePtr();
7777 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7778 StVal.getNode()->getOperand(0), BasePtr,
7779 St->getPointerInfo(), St->isVolatile(),
7780 St->isNonTemporal(), St->getAlignment());
7781
7782 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7783 DAG.getConstant(4, MVT::i32));
7784 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7785 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7786 St->isNonTemporal(),
7787 std::min(4U, St->getAlignment() / 2));
7788 }
7789
7790 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007791 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7792 return SDValue();
7793
Chad Rosier96b66d62012-04-09 19:38:15 +00007794 // Bitcast an i64 store extracted from a vector to f64.
7795 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007796 SelectionDAG &DAG = DCI.DAG;
7797 DebugLoc dl = StVal.getDebugLoc();
7798 SDValue IntVec = StVal.getOperand(0);
7799 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7800 IntVec.getValueType().getVectorNumElements());
7801 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7802 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7803 Vec, StVal.getOperand(1));
7804 dl = N->getDebugLoc();
7805 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7806 // Make the DAGCombiner fold the bitcasts.
7807 DCI.AddToWorklist(Vec.getNode());
7808 DCI.AddToWorklist(ExtElt.getNode());
7809 DCI.AddToWorklist(V.getNode());
7810 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7811 St->getPointerInfo(), St->isVolatile(),
7812 St->isNonTemporal(), St->getAlignment(),
7813 St->getTBAAInfo());
7814}
7815
7816/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7817/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7818/// i64 vector to have f64 elements, since the value can then be loaded
7819/// directly into a VFP register.
7820static bool hasNormalLoadOperand(SDNode *N) {
7821 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7822 for (unsigned i = 0; i < NumElts; ++i) {
7823 SDNode *Elt = N->getOperand(i).getNode();
7824 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7825 return true;
7826 }
7827 return false;
7828}
7829
Bob Wilson75f02882010-09-17 22:59:05 +00007830/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7831/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007832static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7833 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007834 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7835 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7836 // into a pair of GPRs, which is fine when the value is used as a scalar,
7837 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007838 SelectionDAG &DAG = DCI.DAG;
7839 if (N->getNumOperands() == 2) {
7840 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7841 if (RV.getNode())
7842 return RV;
7843 }
Bob Wilson75f02882010-09-17 22:59:05 +00007844
Bob Wilson31600902010-12-21 06:43:19 +00007845 // Load i64 elements as f64 values so that type legalization does not split
7846 // them up into i32 values.
7847 EVT VT = N->getValueType(0);
7848 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7849 return SDValue();
7850 DebugLoc dl = N->getDebugLoc();
7851 SmallVector<SDValue, 8> Ops;
7852 unsigned NumElts = VT.getVectorNumElements();
7853 for (unsigned i = 0; i < NumElts; ++i) {
7854 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7855 Ops.push_back(V);
7856 // Make the DAGCombiner fold the bitcast.
7857 DCI.AddToWorklist(V.getNode());
7858 }
7859 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7860 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7861 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7862}
7863
7864/// PerformInsertEltCombine - Target-specific dag combine xforms for
7865/// ISD::INSERT_VECTOR_ELT.
7866static SDValue PerformInsertEltCombine(SDNode *N,
7867 TargetLowering::DAGCombinerInfo &DCI) {
7868 // Bitcast an i64 load inserted into a vector to f64.
7869 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7870 EVT VT = N->getValueType(0);
7871 SDNode *Elt = N->getOperand(1).getNode();
7872 if (VT.getVectorElementType() != MVT::i64 ||
7873 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7874 return SDValue();
7875
7876 SelectionDAG &DAG = DCI.DAG;
7877 DebugLoc dl = N->getDebugLoc();
7878 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7879 VT.getVectorNumElements());
7880 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7881 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7882 // Make the DAGCombiner fold the bitcasts.
7883 DCI.AddToWorklist(Vec.getNode());
7884 DCI.AddToWorklist(V.getNode());
7885 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7886 Vec, V, N->getOperand(2));
7887 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007888}
7889
Bob Wilsonf20700c2010-10-27 20:38:28 +00007890/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7891/// ISD::VECTOR_SHUFFLE.
7892static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7893 // The LLVM shufflevector instruction does not require the shuffle mask
7894 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7895 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7896 // operands do not match the mask length, they are extended by concatenating
7897 // them with undef vectors. That is probably the right thing for other
7898 // targets, but for NEON it is better to concatenate two double-register
7899 // size vector operands into a single quad-register size vector. Do that
7900 // transformation here:
7901 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7902 // shuffle(concat(v1, v2), undef)
7903 SDValue Op0 = N->getOperand(0);
7904 SDValue Op1 = N->getOperand(1);
7905 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7906 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7907 Op0.getNumOperands() != 2 ||
7908 Op1.getNumOperands() != 2)
7909 return SDValue();
7910 SDValue Concat0Op1 = Op0.getOperand(1);
7911 SDValue Concat1Op1 = Op1.getOperand(1);
7912 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7913 Concat1Op1.getOpcode() != ISD::UNDEF)
7914 return SDValue();
7915 // Skip the transformation if any of the types are illegal.
7916 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7917 EVT VT = N->getValueType(0);
7918 if (!TLI.isTypeLegal(VT) ||
7919 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7920 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7921 return SDValue();
7922
7923 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7924 Op0.getOperand(0), Op1.getOperand(0));
7925 // Translate the shuffle mask.
7926 SmallVector<int, 16> NewMask;
7927 unsigned NumElts = VT.getVectorNumElements();
7928 unsigned HalfElts = NumElts/2;
7929 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7930 for (unsigned n = 0; n < NumElts; ++n) {
7931 int MaskElt = SVN->getMaskElt(n);
7932 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007933 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007934 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007935 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007936 NewElt = HalfElts + MaskElt - NumElts;
7937 NewMask.push_back(NewElt);
7938 }
7939 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7940 DAG.getUNDEF(VT), NewMask.data());
7941}
7942
Bob Wilson1c3ef902011-02-07 17:43:21 +00007943/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7944/// NEON load/store intrinsics to merge base address updates.
7945static SDValue CombineBaseUpdate(SDNode *N,
7946 TargetLowering::DAGCombinerInfo &DCI) {
7947 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7948 return SDValue();
7949
7950 SelectionDAG &DAG = DCI.DAG;
7951 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7952 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7953 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7954 SDValue Addr = N->getOperand(AddrOpIdx);
7955
7956 // Search for a use of the address operand that is an increment.
7957 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7958 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7959 SDNode *User = *UI;
7960 if (User->getOpcode() != ISD::ADD ||
7961 UI.getUse().getResNo() != Addr.getResNo())
7962 continue;
7963
7964 // Check that the add is independent of the load/store. Otherwise, folding
7965 // it would create a cycle.
7966 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7967 continue;
7968
7969 // Find the new opcode for the updating load/store.
7970 bool isLoad = true;
7971 bool isLaneOp = false;
7972 unsigned NewOpc = 0;
7973 unsigned NumVecs = 0;
7974 if (isIntrinsic) {
7975 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7976 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007977 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007978 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7979 NumVecs = 1; break;
7980 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7981 NumVecs = 2; break;
7982 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7983 NumVecs = 3; break;
7984 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7985 NumVecs = 4; break;
7986 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7987 NumVecs = 2; isLaneOp = true; break;
7988 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7989 NumVecs = 3; isLaneOp = true; break;
7990 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7991 NumVecs = 4; isLaneOp = true; break;
7992 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7993 NumVecs = 1; isLoad = false; break;
7994 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7995 NumVecs = 2; isLoad = false; break;
7996 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7997 NumVecs = 3; isLoad = false; break;
7998 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7999 NumVecs = 4; isLoad = false; break;
8000 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8001 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8002 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8003 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8004 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8005 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8006 }
8007 } else {
8008 isLaneOp = true;
8009 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008010 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008011 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8012 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8013 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8014 }
8015 }
8016
8017 // Find the size of memory referenced by the load/store.
8018 EVT VecTy;
8019 if (isLoad)
8020 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008021 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008022 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8023 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8024 if (isLaneOp)
8025 NumBytes /= VecTy.getVectorNumElements();
8026
8027 // If the increment is a constant, it must match the memory ref size.
8028 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8029 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8030 uint64_t IncVal = CInc->getZExtValue();
8031 if (IncVal != NumBytes)
8032 continue;
8033 } else if (NumBytes >= 3 * 16) {
8034 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8035 // separate instructions that make it harder to use a non-constant update.
8036 continue;
8037 }
8038
8039 // Create the new updating load/store node.
8040 EVT Tys[6];
8041 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8042 unsigned n;
8043 for (n = 0; n < NumResultVecs; ++n)
8044 Tys[n] = VecTy;
8045 Tys[n++] = MVT::i32;
8046 Tys[n] = MVT::Other;
8047 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8048 SmallVector<SDValue, 8> Ops;
8049 Ops.push_back(N->getOperand(0)); // incoming chain
8050 Ops.push_back(N->getOperand(AddrOpIdx));
8051 Ops.push_back(Inc);
8052 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8053 Ops.push_back(N->getOperand(i));
8054 }
8055 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8056 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8057 Ops.data(), Ops.size(),
8058 MemInt->getMemoryVT(),
8059 MemInt->getMemOperand());
8060
8061 // Update the uses.
8062 std::vector<SDValue> NewResults;
8063 for (unsigned i = 0; i < NumResultVecs; ++i) {
8064 NewResults.push_back(SDValue(UpdN.getNode(), i));
8065 }
8066 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8067 DCI.CombineTo(N, NewResults);
8068 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8069
8070 break;
Owen Anderson76706012011-04-05 21:48:57 +00008071 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008072 return SDValue();
8073}
8074
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008075/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8076/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8077/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8078/// return true.
8079static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8080 SelectionDAG &DAG = DCI.DAG;
8081 EVT VT = N->getValueType(0);
8082 // vldN-dup instructions only support 64-bit vectors for N > 1.
8083 if (!VT.is64BitVector())
8084 return false;
8085
8086 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8087 SDNode *VLD = N->getOperand(0).getNode();
8088 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8089 return false;
8090 unsigned NumVecs = 0;
8091 unsigned NewOpc = 0;
8092 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8093 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8094 NumVecs = 2;
8095 NewOpc = ARMISD::VLD2DUP;
8096 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8097 NumVecs = 3;
8098 NewOpc = ARMISD::VLD3DUP;
8099 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8100 NumVecs = 4;
8101 NewOpc = ARMISD::VLD4DUP;
8102 } else {
8103 return false;
8104 }
8105
8106 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8107 // numbers match the load.
8108 unsigned VLDLaneNo =
8109 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8110 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8111 UI != UE; ++UI) {
8112 // Ignore uses of the chain result.
8113 if (UI.getUse().getResNo() == NumVecs)
8114 continue;
8115 SDNode *User = *UI;
8116 if (User->getOpcode() != ARMISD::VDUPLANE ||
8117 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8118 return false;
8119 }
8120
8121 // Create the vldN-dup node.
8122 EVT Tys[5];
8123 unsigned n;
8124 for (n = 0; n < NumVecs; ++n)
8125 Tys[n] = VT;
8126 Tys[n] = MVT::Other;
8127 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8128 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8129 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8130 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8131 Ops, 2, VLDMemInt->getMemoryVT(),
8132 VLDMemInt->getMemOperand());
8133
8134 // Update the uses.
8135 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8136 UI != UE; ++UI) {
8137 unsigned ResNo = UI.getUse().getResNo();
8138 // Ignore uses of the chain result.
8139 if (ResNo == NumVecs)
8140 continue;
8141 SDNode *User = *UI;
8142 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8143 }
8144
8145 // Now the vldN-lane intrinsic is dead except for its chain result.
8146 // Update uses of the chain.
8147 std::vector<SDValue> VLDDupResults;
8148 for (unsigned n = 0; n < NumVecs; ++n)
8149 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8150 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8151 DCI.CombineTo(VLD, VLDDupResults);
8152
8153 return true;
8154}
8155
Bob Wilson9e82bf12010-07-14 01:22:12 +00008156/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8157/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008158static SDValue PerformVDUPLANECombine(SDNode *N,
8159 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008160 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008161
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008162 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8163 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8164 if (CombineVLDDUP(N, DCI))
8165 return SDValue(N, 0);
8166
8167 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8168 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008169 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008170 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008171 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008172 return SDValue();
8173
8174 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8175 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8176 // The canonical VMOV for a zero vector uses a 32-bit element size.
8177 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8178 unsigned EltBits;
8179 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8180 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008181 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008182 if (EltSize > VT.getVectorElementType().getSizeInBits())
8183 return SDValue();
8184
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008185 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008186}
8187
Eric Christopherfa6f5912011-06-29 21:10:36 +00008188// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008189// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8190static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8191{
Chad Rosier118c9a02011-06-28 17:26:57 +00008192 integerPart cN;
8193 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008194 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8195 I != E; I++) {
8196 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8197 if (!C)
8198 return false;
8199
Eric Christopherfa6f5912011-06-29 21:10:36 +00008200 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008201 APFloat APF = C->getValueAPF();
8202 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8203 != APFloat::opOK || !isExact)
8204 return false;
8205
8206 c0 = (I == 0) ? cN : c0;
8207 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8208 return false;
8209 }
8210 C = c0;
8211 return true;
8212}
8213
8214/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8215/// can replace combinations of VMUL and VCVT (floating-point to integer)
8216/// when the VMUL has a constant operand that is a power of 2.
8217///
8218/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8219/// vmul.f32 d16, d17, d16
8220/// vcvt.s32.f32 d16, d16
8221/// becomes:
8222/// vcvt.s32.f32 d16, d16, #3
8223static SDValue PerformVCVTCombine(SDNode *N,
8224 TargetLowering::DAGCombinerInfo &DCI,
8225 const ARMSubtarget *Subtarget) {
8226 SelectionDAG &DAG = DCI.DAG;
8227 SDValue Op = N->getOperand(0);
8228
8229 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8230 Op.getOpcode() != ISD::FMUL)
8231 return SDValue();
8232
8233 uint64_t C;
8234 SDValue N0 = Op->getOperand(0);
8235 SDValue ConstVec = Op->getOperand(1);
8236 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8237
Eric Christopherfa6f5912011-06-29 21:10:36 +00008238 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008239 !isConstVecPow2(ConstVec, isSigned, C))
8240 return SDValue();
8241
8242 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8243 Intrinsic::arm_neon_vcvtfp2fxu;
8244 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8245 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008246 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008247 DAG.getConstant(Log2_64(C), MVT::i32));
8248}
8249
8250/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8251/// can replace combinations of VCVT (integer to floating-point) and VDIV
8252/// when the VDIV has a constant operand that is a power of 2.
8253///
8254/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8255/// vcvt.f32.s32 d16, d16
8256/// vdiv.f32 d16, d17, d16
8257/// becomes:
8258/// vcvt.f32.s32 d16, d16, #3
8259static SDValue PerformVDIVCombine(SDNode *N,
8260 TargetLowering::DAGCombinerInfo &DCI,
8261 const ARMSubtarget *Subtarget) {
8262 SelectionDAG &DAG = DCI.DAG;
8263 SDValue Op = N->getOperand(0);
8264 unsigned OpOpcode = Op.getNode()->getOpcode();
8265
8266 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8267 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8268 return SDValue();
8269
8270 uint64_t C;
8271 SDValue ConstVec = N->getOperand(1);
8272 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8273
8274 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8275 !isConstVecPow2(ConstVec, isSigned, C))
8276 return SDValue();
8277
Eric Christopherfa6f5912011-06-29 21:10:36 +00008278 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008279 Intrinsic::arm_neon_vcvtfxu2fp;
8280 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8281 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008282 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008283 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8284}
8285
8286/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008287/// operand of a vector shift operation, where all the elements of the
8288/// build_vector must have the same constant integer value.
8289static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8290 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008291 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008292 Op = Op.getOperand(0);
8293 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8294 APInt SplatBits, SplatUndef;
8295 unsigned SplatBitSize;
8296 bool HasAnyUndefs;
8297 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8298 HasAnyUndefs, ElementBits) ||
8299 SplatBitSize > ElementBits)
8300 return false;
8301 Cnt = SplatBits.getSExtValue();
8302 return true;
8303}
8304
8305/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8306/// operand of a vector shift left operation. That value must be in the range:
8307/// 0 <= Value < ElementBits for a left shift; or
8308/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008309static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008310 assert(VT.isVector() && "vector shift count is not a vector type");
8311 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8312 if (! getVShiftImm(Op, ElementBits, Cnt))
8313 return false;
8314 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8315}
8316
8317/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8318/// operand of a vector shift right operation. For a shift opcode, the value
8319/// is positive, but for an intrinsic the value count must be negative. The
8320/// absolute value must be in the range:
8321/// 1 <= |Value| <= ElementBits for a right shift; or
8322/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008323static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008324 int64_t &Cnt) {
8325 assert(VT.isVector() && "vector shift count is not a vector type");
8326 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8327 if (! getVShiftImm(Op, ElementBits, Cnt))
8328 return false;
8329 if (isIntrinsic)
8330 Cnt = -Cnt;
8331 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8332}
8333
8334/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8335static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8336 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8337 switch (IntNo) {
8338 default:
8339 // Don't do anything for most intrinsics.
8340 break;
8341
8342 // Vector shifts: check for immediate versions and lower them.
8343 // Note: This is done during DAG combining instead of DAG legalizing because
8344 // the build_vectors for 64-bit vector element shift counts are generally
8345 // not legal, and it is hard to see their values after they get legalized to
8346 // loads from a constant pool.
8347 case Intrinsic::arm_neon_vshifts:
8348 case Intrinsic::arm_neon_vshiftu:
8349 case Intrinsic::arm_neon_vshiftls:
8350 case Intrinsic::arm_neon_vshiftlu:
8351 case Intrinsic::arm_neon_vshiftn:
8352 case Intrinsic::arm_neon_vrshifts:
8353 case Intrinsic::arm_neon_vrshiftu:
8354 case Intrinsic::arm_neon_vrshiftn:
8355 case Intrinsic::arm_neon_vqshifts:
8356 case Intrinsic::arm_neon_vqshiftu:
8357 case Intrinsic::arm_neon_vqshiftsu:
8358 case Intrinsic::arm_neon_vqshiftns:
8359 case Intrinsic::arm_neon_vqshiftnu:
8360 case Intrinsic::arm_neon_vqshiftnsu:
8361 case Intrinsic::arm_neon_vqrshiftns:
8362 case Intrinsic::arm_neon_vqrshiftnu:
8363 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008364 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008365 int64_t Cnt;
8366 unsigned VShiftOpc = 0;
8367
8368 switch (IntNo) {
8369 case Intrinsic::arm_neon_vshifts:
8370 case Intrinsic::arm_neon_vshiftu:
8371 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8372 VShiftOpc = ARMISD::VSHL;
8373 break;
8374 }
8375 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8376 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8377 ARMISD::VSHRs : ARMISD::VSHRu);
8378 break;
8379 }
8380 return SDValue();
8381
8382 case Intrinsic::arm_neon_vshiftls:
8383 case Intrinsic::arm_neon_vshiftlu:
8384 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8385 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008386 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008387
8388 case Intrinsic::arm_neon_vrshifts:
8389 case Intrinsic::arm_neon_vrshiftu:
8390 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8391 break;
8392 return SDValue();
8393
8394 case Intrinsic::arm_neon_vqshifts:
8395 case Intrinsic::arm_neon_vqshiftu:
8396 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8397 break;
8398 return SDValue();
8399
8400 case Intrinsic::arm_neon_vqshiftsu:
8401 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8402 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008403 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008404
8405 case Intrinsic::arm_neon_vshiftn:
8406 case Intrinsic::arm_neon_vrshiftn:
8407 case Intrinsic::arm_neon_vqshiftns:
8408 case Intrinsic::arm_neon_vqshiftnu:
8409 case Intrinsic::arm_neon_vqshiftnsu:
8410 case Intrinsic::arm_neon_vqrshiftns:
8411 case Intrinsic::arm_neon_vqrshiftnu:
8412 case Intrinsic::arm_neon_vqrshiftnsu:
8413 // Narrowing shifts require an immediate right shift.
8414 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8415 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008416 llvm_unreachable("invalid shift count for narrowing vector shift "
8417 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008418
8419 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008420 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008421 }
8422
8423 switch (IntNo) {
8424 case Intrinsic::arm_neon_vshifts:
8425 case Intrinsic::arm_neon_vshiftu:
8426 // Opcode already set above.
8427 break;
8428 case Intrinsic::arm_neon_vshiftls:
8429 case Intrinsic::arm_neon_vshiftlu:
8430 if (Cnt == VT.getVectorElementType().getSizeInBits())
8431 VShiftOpc = ARMISD::VSHLLi;
8432 else
8433 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8434 ARMISD::VSHLLs : ARMISD::VSHLLu);
8435 break;
8436 case Intrinsic::arm_neon_vshiftn:
8437 VShiftOpc = ARMISD::VSHRN; break;
8438 case Intrinsic::arm_neon_vrshifts:
8439 VShiftOpc = ARMISD::VRSHRs; break;
8440 case Intrinsic::arm_neon_vrshiftu:
8441 VShiftOpc = ARMISD::VRSHRu; break;
8442 case Intrinsic::arm_neon_vrshiftn:
8443 VShiftOpc = ARMISD::VRSHRN; break;
8444 case Intrinsic::arm_neon_vqshifts:
8445 VShiftOpc = ARMISD::VQSHLs; break;
8446 case Intrinsic::arm_neon_vqshiftu:
8447 VShiftOpc = ARMISD::VQSHLu; break;
8448 case Intrinsic::arm_neon_vqshiftsu:
8449 VShiftOpc = ARMISD::VQSHLsu; break;
8450 case Intrinsic::arm_neon_vqshiftns:
8451 VShiftOpc = ARMISD::VQSHRNs; break;
8452 case Intrinsic::arm_neon_vqshiftnu:
8453 VShiftOpc = ARMISD::VQSHRNu; break;
8454 case Intrinsic::arm_neon_vqshiftnsu:
8455 VShiftOpc = ARMISD::VQSHRNsu; break;
8456 case Intrinsic::arm_neon_vqrshiftns:
8457 VShiftOpc = ARMISD::VQRSHRNs; break;
8458 case Intrinsic::arm_neon_vqrshiftnu:
8459 VShiftOpc = ARMISD::VQRSHRNu; break;
8460 case Intrinsic::arm_neon_vqrshiftnsu:
8461 VShiftOpc = ARMISD::VQRSHRNsu; break;
8462 }
8463
8464 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008465 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008466 }
8467
8468 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008469 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008470 int64_t Cnt;
8471 unsigned VShiftOpc = 0;
8472
8473 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8474 VShiftOpc = ARMISD::VSLI;
8475 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8476 VShiftOpc = ARMISD::VSRI;
8477 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008478 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008479 }
8480
8481 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8482 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008483 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008484 }
8485
8486 case Intrinsic::arm_neon_vqrshifts:
8487 case Intrinsic::arm_neon_vqrshiftu:
8488 // No immediate versions of these to check for.
8489 break;
8490 }
8491
8492 return SDValue();
8493}
8494
8495/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8496/// lowers them. As with the vector shift intrinsics, this is done during DAG
8497/// combining instead of DAG legalizing because the build_vectors for 64-bit
8498/// vector element shift counts are generally not legal, and it is hard to see
8499/// their values after they get legalized to loads from a constant pool.
8500static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8501 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008502 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008503 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8504 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8505 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8506 SDValue N1 = N->getOperand(1);
8507 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8508 SDValue N0 = N->getOperand(0);
8509 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8510 DAG.MaskedValueIsZero(N0.getOperand(0),
8511 APInt::getHighBitsSet(32, 16)))
8512 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8513 }
8514 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008515
8516 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008517 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8518 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008519 return SDValue();
8520
8521 assert(ST->hasNEON() && "unexpected vector shift");
8522 int64_t Cnt;
8523
8524 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008525 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008526
8527 case ISD::SHL:
8528 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8529 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008530 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008531 break;
8532
8533 case ISD::SRA:
8534 case ISD::SRL:
8535 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8536 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8537 ARMISD::VSHRs : ARMISD::VSHRu);
8538 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008539 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008540 }
8541 }
8542 return SDValue();
8543}
8544
8545/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8546/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8547static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8548 const ARMSubtarget *ST) {
8549 SDValue N0 = N->getOperand(0);
8550
8551 // Check for sign- and zero-extensions of vector extract operations of 8-
8552 // and 16-bit vector elements. NEON supports these directly. They are
8553 // handled during DAG combining because type legalization will promote them
8554 // to 32-bit types and it is messy to recognize the operations after that.
8555 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8556 SDValue Vec = N0.getOperand(0);
8557 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008558 EVT VT = N->getValueType(0);
8559 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008560 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8561
Owen Anderson825b72b2009-08-11 20:47:22 +00008562 if (VT == MVT::i32 &&
8563 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008564 TLI.isTypeLegal(Vec.getValueType()) &&
8565 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008566
8567 unsigned Opc = 0;
8568 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008569 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008570 case ISD::SIGN_EXTEND:
8571 Opc = ARMISD::VGETLANEs;
8572 break;
8573 case ISD::ZERO_EXTEND:
8574 case ISD::ANY_EXTEND:
8575 Opc = ARMISD::VGETLANEu;
8576 break;
8577 }
8578 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8579 }
8580 }
8581
8582 return SDValue();
8583}
8584
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008585/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8586/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8587static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8588 const ARMSubtarget *ST) {
8589 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008590 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008591 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8592 // a NaN; only do the transformation when it matches that behavior.
8593
8594 // For now only do this when using NEON for FP operations; if using VFP, it
8595 // is not obvious that the benefit outweighs the cost of switching to the
8596 // NEON pipeline.
8597 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8598 N->getValueType(0) != MVT::f32)
8599 return SDValue();
8600
8601 SDValue CondLHS = N->getOperand(0);
8602 SDValue CondRHS = N->getOperand(1);
8603 SDValue LHS = N->getOperand(2);
8604 SDValue RHS = N->getOperand(3);
8605 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8606
8607 unsigned Opcode = 0;
8608 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008609 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008610 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008611 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008612 IsReversed = true ; // x CC y ? y : x
8613 } else {
8614 return SDValue();
8615 }
8616
Bob Wilsone742bb52010-02-24 22:15:53 +00008617 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008618 switch (CC) {
8619 default: break;
8620 case ISD::SETOLT:
8621 case ISD::SETOLE:
8622 case ISD::SETLT:
8623 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008624 case ISD::SETULT:
8625 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008626 // If LHS is NaN, an ordered comparison will be false and the result will
8627 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8628 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8629 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8630 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8631 break;
8632 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8633 // will return -0, so vmin can only be used for unsafe math or if one of
8634 // the operands is known to be nonzero.
8635 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008636 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008637 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8638 break;
8639 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008640 break;
8641
8642 case ISD::SETOGT:
8643 case ISD::SETOGE:
8644 case ISD::SETGT:
8645 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008646 case ISD::SETUGT:
8647 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008648 // If LHS is NaN, an ordered comparison will be false and the result will
8649 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8650 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8651 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8652 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8653 break;
8654 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8655 // will return +0, so vmax can only be used for unsafe math or if one of
8656 // the operands is known to be nonzero.
8657 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008658 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008659 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8660 break;
8661 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008662 break;
8663 }
8664
8665 if (!Opcode)
8666 return SDValue();
8667 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8668}
8669
Evan Chenge721f5c2011-07-13 00:42:17 +00008670/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8671SDValue
8672ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8673 SDValue Cmp = N->getOperand(4);
8674 if (Cmp.getOpcode() != ARMISD::CMPZ)
8675 // Only looking at EQ and NE cases.
8676 return SDValue();
8677
8678 EVT VT = N->getValueType(0);
8679 DebugLoc dl = N->getDebugLoc();
8680 SDValue LHS = Cmp.getOperand(0);
8681 SDValue RHS = Cmp.getOperand(1);
8682 SDValue FalseVal = N->getOperand(0);
8683 SDValue TrueVal = N->getOperand(1);
8684 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008685 ARMCC::CondCodes CC =
8686 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008687
8688 // Simplify
8689 // mov r1, r0
8690 // cmp r1, x
8691 // mov r0, y
8692 // moveq r0, x
8693 // to
8694 // cmp r0, x
8695 // movne r0, y
8696 //
8697 // mov r1, r0
8698 // cmp r1, x
8699 // mov r0, x
8700 // movne r0, y
8701 // to
8702 // cmp r0, x
8703 // movne r0, y
8704 /// FIXME: Turn this into a target neutral optimization?
8705 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008706 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008707 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8708 N->getOperand(3), Cmp);
8709 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8710 SDValue ARMcc;
8711 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8712 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8713 N->getOperand(3), NewCmp);
8714 }
8715
8716 if (Res.getNode()) {
8717 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008718 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008719 // Capture demanded bits information that would be otherwise lost.
8720 if (KnownZero == 0xfffffffe)
8721 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8722 DAG.getValueType(MVT::i1));
8723 else if (KnownZero == 0xffffff00)
8724 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8725 DAG.getValueType(MVT::i8));
8726 else if (KnownZero == 0xffff0000)
8727 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8728 DAG.getValueType(MVT::i16));
8729 }
8730
8731 return Res;
8732}
8733
Dan Gohman475871a2008-07-27 21:46:04 +00008734SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008735 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008736 switch (N->getOpcode()) {
8737 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008738 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008739 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008740 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008741 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008742 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8743 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008744 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008745 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008746 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008747 case ISD::STORE: return PerformSTORECombine(N, DCI);
8748 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8749 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008750 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008751 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008752 case ISD::FP_TO_SINT:
8753 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8754 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008755 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008756 case ISD::SHL:
8757 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008758 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008759 case ISD::SIGN_EXTEND:
8760 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008761 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8762 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008763 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008764 case ARMISD::VLD2DUP:
8765 case ARMISD::VLD3DUP:
8766 case ARMISD::VLD4DUP:
8767 return CombineBaseUpdate(N, DCI);
8768 case ISD::INTRINSIC_VOID:
8769 case ISD::INTRINSIC_W_CHAIN:
8770 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8771 case Intrinsic::arm_neon_vld1:
8772 case Intrinsic::arm_neon_vld2:
8773 case Intrinsic::arm_neon_vld3:
8774 case Intrinsic::arm_neon_vld4:
8775 case Intrinsic::arm_neon_vld2lane:
8776 case Intrinsic::arm_neon_vld3lane:
8777 case Intrinsic::arm_neon_vld4lane:
8778 case Intrinsic::arm_neon_vst1:
8779 case Intrinsic::arm_neon_vst2:
8780 case Intrinsic::arm_neon_vst3:
8781 case Intrinsic::arm_neon_vst4:
8782 case Intrinsic::arm_neon_vst2lane:
8783 case Intrinsic::arm_neon_vst3lane:
8784 case Intrinsic::arm_neon_vst4lane:
8785 return CombineBaseUpdate(N, DCI);
8786 default: break;
8787 }
8788 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008789 }
Dan Gohman475871a2008-07-27 21:46:04 +00008790 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008791}
8792
Evan Cheng31959b12011-02-02 01:06:55 +00008793bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8794 EVT VT) const {
8795 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8796}
8797
Bill Wendlingaf566342009-08-15 21:21:19 +00008798bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008799 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008800 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008801
8802 switch (VT.getSimpleVT().SimpleTy) {
8803 default:
8804 return false;
8805 case MVT::i8:
8806 case MVT::i16:
8807 case MVT::i32:
8808 return true;
8809 // FIXME: VLD1 etc with standard alignment is legal.
8810 }
8811}
8812
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008813static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8814 unsigned AlignCheck) {
8815 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8816 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8817}
8818
8819EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8820 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008821 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008822 bool MemcpyStrSrc,
8823 MachineFunction &MF) const {
8824 const Function *F = MF.getFunction();
8825
8826 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008827 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008828 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8829 Subtarget->hasNEON()) {
8830 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8831 return MVT::v4i32;
8832 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8833 return MVT::v2i32;
8834 }
8835 }
8836
Lang Hames5207bf22011-11-08 18:56:23 +00008837 // Lowering to i32/i16 if the size permits.
8838 if (Size >= 4) {
8839 return MVT::i32;
8840 } else if (Size >= 2) {
8841 return MVT::i16;
8842 }
8843
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008844 // Let the target-independent logic figure it out.
8845 return MVT::Other;
8846}
8847
Evan Chenge6c835f2009-08-14 20:09:37 +00008848static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8849 if (V < 0)
8850 return false;
8851
8852 unsigned Scale = 1;
8853 switch (VT.getSimpleVT().SimpleTy) {
8854 default: return false;
8855 case MVT::i1:
8856 case MVT::i8:
8857 // Scale == 1;
8858 break;
8859 case MVT::i16:
8860 // Scale == 2;
8861 Scale = 2;
8862 break;
8863 case MVT::i32:
8864 // Scale == 4;
8865 Scale = 4;
8866 break;
8867 }
8868
8869 if ((V & (Scale - 1)) != 0)
8870 return false;
8871 V /= Scale;
8872 return V == (V & ((1LL << 5) - 1));
8873}
8874
8875static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8876 const ARMSubtarget *Subtarget) {
8877 bool isNeg = false;
8878 if (V < 0) {
8879 isNeg = true;
8880 V = - V;
8881 }
8882
8883 switch (VT.getSimpleVT().SimpleTy) {
8884 default: return false;
8885 case MVT::i1:
8886 case MVT::i8:
8887 case MVT::i16:
8888 case MVT::i32:
8889 // + imm12 or - imm8
8890 if (isNeg)
8891 return V == (V & ((1LL << 8) - 1));
8892 return V == (V & ((1LL << 12) - 1));
8893 case MVT::f32:
8894 case MVT::f64:
8895 // Same as ARM mode. FIXME: NEON?
8896 if (!Subtarget->hasVFP2())
8897 return false;
8898 if ((V & 3) != 0)
8899 return false;
8900 V >>= 2;
8901 return V == (V & ((1LL << 8) - 1));
8902 }
8903}
8904
Evan Chengb01fad62007-03-12 23:30:29 +00008905/// isLegalAddressImmediate - Return true if the integer value can be used
8906/// as the offset of the target addressing mode for load / store of the
8907/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008908static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008909 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008910 if (V == 0)
8911 return true;
8912
Evan Cheng65011532009-03-09 19:15:00 +00008913 if (!VT.isSimple())
8914 return false;
8915
Evan Chenge6c835f2009-08-14 20:09:37 +00008916 if (Subtarget->isThumb1Only())
8917 return isLegalT1AddressImmediate(V, VT);
8918 else if (Subtarget->isThumb2())
8919 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008920
Evan Chenge6c835f2009-08-14 20:09:37 +00008921 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008922 if (V < 0)
8923 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008924 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008925 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008926 case MVT::i1:
8927 case MVT::i8:
8928 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008929 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008930 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008931 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008932 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008933 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008934 case MVT::f32:
8935 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008936 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008937 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008938 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008939 return false;
8940 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008941 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008942 }
Evan Chenga8e29892007-01-19 07:51:42 +00008943}
8944
Evan Chenge6c835f2009-08-14 20:09:37 +00008945bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8946 EVT VT) const {
8947 int Scale = AM.Scale;
8948 if (Scale < 0)
8949 return false;
8950
8951 switch (VT.getSimpleVT().SimpleTy) {
8952 default: return false;
8953 case MVT::i1:
8954 case MVT::i8:
8955 case MVT::i16:
8956 case MVT::i32:
8957 if (Scale == 1)
8958 return true;
8959 // r + r << imm
8960 Scale = Scale & ~1;
8961 return Scale == 2 || Scale == 4 || Scale == 8;
8962 case MVT::i64:
8963 // r + r
8964 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8965 return true;
8966 return false;
8967 case MVT::isVoid:
8968 // Note, we allow "void" uses (basically, uses that aren't loads or
8969 // stores), because arm allows folding a scale into many arithmetic
8970 // operations. This should be made more precise and revisited later.
8971
8972 // Allow r << imm, but the imm has to be a multiple of two.
8973 if (Scale & 1) return false;
8974 return isPowerOf2_32(Scale);
8975 }
8976}
8977
Chris Lattner37caf8c2007-04-09 23:33:39 +00008978/// isLegalAddressingMode - Return true if the addressing mode represented
8979/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008980bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008981 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008982 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008983 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008984 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008985
Chris Lattner37caf8c2007-04-09 23:33:39 +00008986 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008987 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008988 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008989
Chris Lattner37caf8c2007-04-09 23:33:39 +00008990 switch (AM.Scale) {
8991 case 0: // no scale reg, must be "r+i" or "r", or "i".
8992 break;
8993 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008994 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008995 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008996 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008997 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008998 // ARM doesn't support any R+R*scale+imm addr modes.
8999 if (AM.BaseOffs)
9000 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009001
Bob Wilson2c7dab12009-04-08 17:55:28 +00009002 if (!VT.isSimple())
9003 return false;
9004
Evan Chenge6c835f2009-08-14 20:09:37 +00009005 if (Subtarget->isThumb2())
9006 return isLegalT2ScaledAddressingMode(AM, VT);
9007
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009008 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009009 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009010 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009011 case MVT::i1:
9012 case MVT::i8:
9013 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009014 if (Scale < 0) Scale = -Scale;
9015 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009016 return true;
9017 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009018 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009019 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009020 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009021 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009022 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009023 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009024 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009025
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009027 // Note, we allow "void" uses (basically, uses that aren't loads or
9028 // stores), because arm allows folding a scale into many arithmetic
9029 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009030
Chris Lattner37caf8c2007-04-09 23:33:39 +00009031 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009032 if (Scale & 1) return false;
9033 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009034 }
Evan Chengb01fad62007-03-12 23:30:29 +00009035 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009036 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009037}
9038
Evan Cheng77e47512009-11-11 19:05:52 +00009039/// isLegalICmpImmediate - Return true if the specified immediate is legal
9040/// icmp immediate, that is the target has icmp instructions which can compare
9041/// a register against the immediate without having to materialize the
9042/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009043bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009044 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009045 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009046 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009047 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009048 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009049 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009050 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009051}
9052
Andrew Trick8d8d9612012-07-18 18:34:27 +00009053/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9054/// *or sub* immediate, that is the target has add or sub instructions which can
9055/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009056/// immediate into a register.
9057bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009058 // Same encoding for add/sub, just flip the sign.
9059 int64_t AbsImm = llvm::abs64(Imm);
9060 if (!Subtarget->isThumb())
9061 return ARM_AM::getSOImmVal(AbsImm) != -1;
9062 if (Subtarget->isThumb2())
9063 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9064 // Thumb1 only has 8-bit unsigned immediate.
9065 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009066}
9067
Owen Andersone50ed302009-08-10 22:56:29 +00009068static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009069 bool isSEXTLoad, SDValue &Base,
9070 SDValue &Offset, bool &isInc,
9071 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009072 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9073 return false;
9074
Owen Anderson825b72b2009-08-11 20:47:22 +00009075 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009076 // AddressingMode 3
9077 Base = Ptr->getOperand(0);
9078 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009079 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009080 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009081 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009082 isInc = false;
9083 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9084 return true;
9085 }
9086 }
9087 isInc = (Ptr->getOpcode() == ISD::ADD);
9088 Offset = Ptr->getOperand(1);
9089 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009090 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009091 // AddressingMode 2
9092 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009093 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009094 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009095 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009096 isInc = false;
9097 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9098 Base = Ptr->getOperand(0);
9099 return true;
9100 }
9101 }
9102
9103 if (Ptr->getOpcode() == ISD::ADD) {
9104 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009105 ARM_AM::ShiftOpc ShOpcVal=
9106 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009107 if (ShOpcVal != ARM_AM::no_shift) {
9108 Base = Ptr->getOperand(1);
9109 Offset = Ptr->getOperand(0);
9110 } else {
9111 Base = Ptr->getOperand(0);
9112 Offset = Ptr->getOperand(1);
9113 }
9114 return true;
9115 }
9116
9117 isInc = (Ptr->getOpcode() == ISD::ADD);
9118 Base = Ptr->getOperand(0);
9119 Offset = Ptr->getOperand(1);
9120 return true;
9121 }
9122
Jim Grosbache5165492009-11-09 00:11:35 +00009123 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009124 return false;
9125}
9126
Owen Andersone50ed302009-08-10 22:56:29 +00009127static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009128 bool isSEXTLoad, SDValue &Base,
9129 SDValue &Offset, bool &isInc,
9130 SelectionDAG &DAG) {
9131 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9132 return false;
9133
9134 Base = Ptr->getOperand(0);
9135 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9136 int RHSC = (int)RHS->getZExtValue();
9137 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9138 assert(Ptr->getOpcode() == ISD::ADD);
9139 isInc = false;
9140 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9141 return true;
9142 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9143 isInc = Ptr->getOpcode() == ISD::ADD;
9144 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9145 return true;
9146 }
9147 }
9148
9149 return false;
9150}
9151
Evan Chenga8e29892007-01-19 07:51:42 +00009152/// getPreIndexedAddressParts - returns true by value, base pointer and
9153/// offset pointer and addressing mode by reference if the node's address
9154/// can be legally represented as pre-indexed load / store address.
9155bool
Dan Gohman475871a2008-07-27 21:46:04 +00009156ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9157 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009158 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009159 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009160 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009161 return false;
9162
Owen Andersone50ed302009-08-10 22:56:29 +00009163 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009164 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009165 bool isSEXTLoad = false;
9166 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9167 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009168 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009169 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9170 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9171 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009172 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009173 } else
9174 return false;
9175
9176 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009177 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009178 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009179 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9180 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009181 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009182 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009183 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009184 if (!isLegal)
9185 return false;
9186
9187 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9188 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009189}
9190
9191/// getPostIndexedAddressParts - returns true by value, base pointer and
9192/// offset pointer and addressing mode by reference if this node can be
9193/// combined with a load / store to form a post-indexed load / store.
9194bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009195 SDValue &Base,
9196 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009197 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009198 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009199 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009200 return false;
9201
Owen Andersone50ed302009-08-10 22:56:29 +00009202 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009203 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009204 bool isSEXTLoad = false;
9205 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009206 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009207 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009208 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9209 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009210 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009211 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009212 } else
9213 return false;
9214
9215 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009216 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009217 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009218 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009219 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009220 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009221 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9222 isInc, DAG);
9223 if (!isLegal)
9224 return false;
9225
Evan Cheng28dad2a2010-05-18 21:31:17 +00009226 if (Ptr != Base) {
9227 // Swap base ptr and offset to catch more post-index load / store when
9228 // it's legal. In Thumb2 mode, offset must be an immediate.
9229 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9230 !Subtarget->isThumb2())
9231 std::swap(Base, Offset);
9232
9233 // Post-indexed load / store update the base pointer.
9234 if (Ptr != Base)
9235 return false;
9236 }
9237
Evan Chenge88d5ce2009-07-02 07:28:31 +00009238 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9239 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009240}
9241
Dan Gohman475871a2008-07-27 21:46:04 +00009242void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009243 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009244 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009245 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009246 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009247 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009248 switch (Op.getOpcode()) {
9249 default: break;
9250 case ARMISD::CMOV: {
9251 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009252 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009253 if (KnownZero == 0 && KnownOne == 0) return;
9254
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009255 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009256 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009257 KnownZero &= KnownZeroRHS;
9258 KnownOne &= KnownOneRHS;
9259 return;
9260 }
9261 }
9262}
9263
9264//===----------------------------------------------------------------------===//
9265// ARM Inline Assembly Support
9266//===----------------------------------------------------------------------===//
9267
Evan Cheng55d42002011-01-08 01:24:27 +00009268bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9269 // Looking for "rev" which is V6+.
9270 if (!Subtarget->hasV6Ops())
9271 return false;
9272
9273 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9274 std::string AsmStr = IA->getAsmString();
9275 SmallVector<StringRef, 4> AsmPieces;
9276 SplitString(AsmStr, AsmPieces, ";\n");
9277
9278 switch (AsmPieces.size()) {
9279 default: return false;
9280 case 1:
9281 AsmStr = AsmPieces[0];
9282 AsmPieces.clear();
9283 SplitString(AsmStr, AsmPieces, " \t,");
9284
9285 // rev $0, $1
9286 if (AsmPieces.size() == 3 &&
9287 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9288 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009289 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009290 if (Ty && Ty->getBitWidth() == 32)
9291 return IntrinsicLowering::LowerToByteSwap(CI);
9292 }
9293 break;
9294 }
9295
9296 return false;
9297}
9298
Evan Chenga8e29892007-01-19 07:51:42 +00009299/// getConstraintType - Given a constraint letter, return the type of
9300/// constraint it is for this target.
9301ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009302ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9303 if (Constraint.size() == 1) {
9304 switch (Constraint[0]) {
9305 default: break;
9306 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009307 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009308 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009309 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009310 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009311 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009312 // An address with a single base register. Due to the way we
9313 // currently handle addresses it is the same as an 'r' memory constraint.
9314 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009315 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009316 } else if (Constraint.size() == 2) {
9317 switch (Constraint[0]) {
9318 default: break;
9319 // All 'U+' constraints are addresses.
9320 case 'U': return C_Memory;
9321 }
Evan Chenga8e29892007-01-19 07:51:42 +00009322 }
Chris Lattner4234f572007-03-25 02:14:49 +00009323 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009324}
9325
John Thompson44ab89e2010-10-29 17:29:13 +00009326/// Examine constraint type and operand type and determine a weight value.
9327/// This object must already have been set up with the operand type
9328/// and the current alternative constraint selected.
9329TargetLowering::ConstraintWeight
9330ARMTargetLowering::getSingleConstraintMatchWeight(
9331 AsmOperandInfo &info, const char *constraint) const {
9332 ConstraintWeight weight = CW_Invalid;
9333 Value *CallOperandVal = info.CallOperandVal;
9334 // If we don't have a value, we can't do a match,
9335 // but allow it at the lowest weight.
9336 if (CallOperandVal == NULL)
9337 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009338 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009339 // Look at the constraint type.
9340 switch (*constraint) {
9341 default:
9342 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9343 break;
9344 case 'l':
9345 if (type->isIntegerTy()) {
9346 if (Subtarget->isThumb())
9347 weight = CW_SpecificReg;
9348 else
9349 weight = CW_Register;
9350 }
9351 break;
9352 case 'w':
9353 if (type->isFloatingPointTy())
9354 weight = CW_Register;
9355 break;
9356 }
9357 return weight;
9358}
9359
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009360typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9361RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009362ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009363 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009364 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009365 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009366 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009367 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009368 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009369 return RCPair(0U, &ARM::tGPRRegClass);
9370 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009371 case 'h': // High regs or no regs.
9372 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009373 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009374 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009375 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009376 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009377 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009378 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009379 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009380 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009381 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009382 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009383 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009384 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009385 case 'x':
9386 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009387 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009388 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009389 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009390 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009391 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009392 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009393 case 't':
9394 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009395 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009396 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009397 }
9398 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009399 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009400 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009401
Evan Chenga8e29892007-01-19 07:51:42 +00009402 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9403}
9404
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009405/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9406/// vector. If it is invalid, don't add anything to Ops.
9407void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009408 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009409 std::vector<SDValue>&Ops,
9410 SelectionDAG &DAG) const {
9411 SDValue Result(0, 0);
9412
Eric Christopher100c8332011-06-02 23:16:42 +00009413 // Currently only support length 1 constraints.
9414 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009415
Eric Christopher100c8332011-06-02 23:16:42 +00009416 char ConstraintLetter = Constraint[0];
9417 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009418 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009419 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009420 case 'I': case 'J': case 'K': case 'L':
9421 case 'M': case 'N': case 'O':
9422 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9423 if (!C)
9424 return;
9425
9426 int64_t CVal64 = C->getSExtValue();
9427 int CVal = (int) CVal64;
9428 // None of these constraints allow values larger than 32 bits. Check
9429 // that the value fits in an int.
9430 if (CVal != CVal64)
9431 return;
9432
Eric Christopher100c8332011-06-02 23:16:42 +00009433 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009434 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009435 // Constant suitable for movw, must be between 0 and
9436 // 65535.
9437 if (Subtarget->hasV6T2Ops())
9438 if (CVal >= 0 && CVal <= 65535)
9439 break;
9440 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009441 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009442 if (Subtarget->isThumb1Only()) {
9443 // This must be a constant between 0 and 255, for ADD
9444 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009445 if (CVal >= 0 && CVal <= 255)
9446 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009447 } else if (Subtarget->isThumb2()) {
9448 // A constant that can be used as an immediate value in a
9449 // data-processing instruction.
9450 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9451 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009452 } else {
9453 // A constant that can be used as an immediate value in a
9454 // data-processing instruction.
9455 if (ARM_AM::getSOImmVal(CVal) != -1)
9456 break;
9457 }
9458 return;
9459
9460 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009461 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009462 // This must be a constant between -255 and -1, for negated ADD
9463 // immediates. This can be used in GCC with an "n" modifier that
9464 // prints the negated value, for use with SUB instructions. It is
9465 // not useful otherwise but is implemented for compatibility.
9466 if (CVal >= -255 && CVal <= -1)
9467 break;
9468 } else {
9469 // This must be a constant between -4095 and 4095. It is not clear
9470 // what this constraint is intended for. Implemented for
9471 // compatibility with GCC.
9472 if (CVal >= -4095 && CVal <= 4095)
9473 break;
9474 }
9475 return;
9476
9477 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009478 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009479 // A 32-bit value where only one byte has a nonzero value. Exclude
9480 // zero to match GCC. This constraint is used by GCC internally for
9481 // constants that can be loaded with a move/shift combination.
9482 // It is not useful otherwise but is implemented for compatibility.
9483 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9484 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009485 } else if (Subtarget->isThumb2()) {
9486 // A constant whose bitwise inverse can be used as an immediate
9487 // value in a data-processing instruction. This can be used in GCC
9488 // with a "B" modifier that prints the inverted value, for use with
9489 // BIC and MVN instructions. It is not useful otherwise but is
9490 // implemented for compatibility.
9491 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9492 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009493 } else {
9494 // A constant whose bitwise inverse can be used as an immediate
9495 // value in a data-processing instruction. This can be used in GCC
9496 // with a "B" modifier that prints the inverted value, for use with
9497 // BIC and MVN instructions. It is not useful otherwise but is
9498 // implemented for compatibility.
9499 if (ARM_AM::getSOImmVal(~CVal) != -1)
9500 break;
9501 }
9502 return;
9503
9504 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009505 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009506 // This must be a constant between -7 and 7,
9507 // for 3-operand ADD/SUB immediate instructions.
9508 if (CVal >= -7 && CVal < 7)
9509 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009510 } else if (Subtarget->isThumb2()) {
9511 // A constant whose negation can be used as an immediate value in a
9512 // data-processing instruction. This can be used in GCC with an "n"
9513 // modifier that prints the negated value, for use with SUB
9514 // instructions. It is not useful otherwise but is implemented for
9515 // compatibility.
9516 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9517 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009518 } else {
9519 // A constant whose negation can be used as an immediate value in a
9520 // data-processing instruction. This can be used in GCC with an "n"
9521 // modifier that prints the negated value, for use with SUB
9522 // instructions. It is not useful otherwise but is implemented for
9523 // compatibility.
9524 if (ARM_AM::getSOImmVal(-CVal) != -1)
9525 break;
9526 }
9527 return;
9528
9529 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009530 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009531 // This must be a multiple of 4 between 0 and 1020, for
9532 // ADD sp + immediate.
9533 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9534 break;
9535 } else {
9536 // A power of two or a constant between 0 and 32. This is used in
9537 // GCC for the shift amount on shifted register operands, but it is
9538 // useful in general for any shift amounts.
9539 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9540 break;
9541 }
9542 return;
9543
9544 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009545 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009546 // This must be a constant between 0 and 31, for shift amounts.
9547 if (CVal >= 0 && CVal <= 31)
9548 break;
9549 }
9550 return;
9551
9552 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009553 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009554 // This must be a multiple of 4 between -508 and 508, for
9555 // ADD/SUB sp = sp + immediate.
9556 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9557 break;
9558 }
9559 return;
9560 }
9561 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9562 break;
9563 }
9564
9565 if (Result.getNode()) {
9566 Ops.push_back(Result);
9567 return;
9568 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009569 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009570}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009571
9572bool
9573ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9574 // The ARM target isn't yet aware of offsets.
9575 return false;
9576}
Evan Cheng39382422009-10-28 01:44:26 +00009577
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009578bool ARM::isBitFieldInvertedMask(unsigned v) {
9579 if (v == 0xffffffff)
9580 return 0;
9581 // there can be 1's on either or both "outsides", all the "inside"
9582 // bits must be 0's
9583 unsigned int lsb = 0, msb = 31;
9584 while (v & (1 << msb)) --msb;
9585 while (v & (1 << lsb)) ++lsb;
9586 for (unsigned int i = lsb; i <= msb; ++i) {
9587 if (v & (1 << i))
9588 return 0;
9589 }
9590 return 1;
9591}
9592
Evan Cheng39382422009-10-28 01:44:26 +00009593/// isFPImmLegal - Returns true if the target can instruction select the
9594/// specified FP immediate natively. If false, the legalizer will
9595/// materialize the FP immediate as a load from a constant pool.
9596bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9597 if (!Subtarget->hasVFP3())
9598 return false;
9599 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009600 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009601 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009602 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009603 return false;
9604}
Bob Wilson65ffec42010-09-21 17:56:22 +00009605
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009606/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009607/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9608/// specified in the intrinsic calls.
9609bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9610 const CallInst &I,
9611 unsigned Intrinsic) const {
9612 switch (Intrinsic) {
9613 case Intrinsic::arm_neon_vld1:
9614 case Intrinsic::arm_neon_vld2:
9615 case Intrinsic::arm_neon_vld3:
9616 case Intrinsic::arm_neon_vld4:
9617 case Intrinsic::arm_neon_vld2lane:
9618 case Intrinsic::arm_neon_vld3lane:
9619 case Intrinsic::arm_neon_vld4lane: {
9620 Info.opc = ISD::INTRINSIC_W_CHAIN;
9621 // Conservatively set memVT to the entire set of vectors loaded.
9622 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9623 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9624 Info.ptrVal = I.getArgOperand(0);
9625 Info.offset = 0;
9626 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9627 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9628 Info.vol = false; // volatile loads with NEON intrinsics not supported
9629 Info.readMem = true;
9630 Info.writeMem = false;
9631 return true;
9632 }
9633 case Intrinsic::arm_neon_vst1:
9634 case Intrinsic::arm_neon_vst2:
9635 case Intrinsic::arm_neon_vst3:
9636 case Intrinsic::arm_neon_vst4:
9637 case Intrinsic::arm_neon_vst2lane:
9638 case Intrinsic::arm_neon_vst3lane:
9639 case Intrinsic::arm_neon_vst4lane: {
9640 Info.opc = ISD::INTRINSIC_VOID;
9641 // Conservatively set memVT to the entire set of vectors stored.
9642 unsigned NumElts = 0;
9643 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009644 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009645 if (!ArgTy->isVectorTy())
9646 break;
9647 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9648 }
9649 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9650 Info.ptrVal = I.getArgOperand(0);
9651 Info.offset = 0;
9652 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9653 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9654 Info.vol = false; // volatile stores with NEON intrinsics not supported
9655 Info.readMem = false;
9656 Info.writeMem = true;
9657 return true;
9658 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009659 case Intrinsic::arm_strexd: {
9660 Info.opc = ISD::INTRINSIC_W_CHAIN;
9661 Info.memVT = MVT::i64;
9662 Info.ptrVal = I.getArgOperand(2);
9663 Info.offset = 0;
9664 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009665 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009666 Info.readMem = false;
9667 Info.writeMem = true;
9668 return true;
9669 }
9670 case Intrinsic::arm_ldrexd: {
9671 Info.opc = ISD::INTRINSIC_W_CHAIN;
9672 Info.memVT = MVT::i64;
9673 Info.ptrVal = I.getArgOperand(0);
9674 Info.offset = 0;
9675 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009676 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009677 Info.readMem = true;
9678 Info.writeMem = false;
9679 return true;
9680 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009681 default:
9682 break;
9683 }
9684
9685 return false;
9686}