blob: 8c71021c6ce3a59ab78109db86659bf70238c8dd [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";
David Goodwinc0309b42009-06-29 15:33:01 +0000898 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000899 case ARMISD::CMPFP: return "ARMISD::CMPFP";
900 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000901 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000902 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000903
Evan Chenga8e29892007-01-19 07:51:42 +0000904 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000905 case ARMISD::CAND: return "ARMISD::CAND";
906 case ARMISD::COR: return "ARMISD::COR";
907 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000908
Jim Grosbach3482c802010-01-18 19:58:49 +0000909 case ARMISD::RBIT: return "ARMISD::RBIT";
910
Bob Wilson76a312b2010-03-19 22:51:32 +0000911 case ARMISD::FTOSI: return "ARMISD::FTOSI";
912 case ARMISD::FTOUI: return "ARMISD::FTOUI";
913 case ARMISD::SITOF: return "ARMISD::SITOF";
914 case ARMISD::UITOF: return "ARMISD::UITOF";
915
Evan Chenga8e29892007-01-19 07:51:42 +0000916 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
917 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
918 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000919
Evan Cheng342e3162011-08-30 01:34:54 +0000920 case ARMISD::ADDC: return "ARMISD::ADDC";
921 case ARMISD::ADDE: return "ARMISD::ADDE";
922 case ARMISD::SUBC: return "ARMISD::SUBC";
923 case ARMISD::SUBE: return "ARMISD::SUBE";
924
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000925 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
926 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000927
Evan Chengc5942082009-10-28 06:55:03 +0000928 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
929 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
930
Dale Johannesen51e28e62010-06-03 21:09:53 +0000931 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000932
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000933 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000934
Evan Cheng86198642009-08-07 00:34:42 +0000935 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
936
Jim Grosbach3728e962009-12-10 00:11:09 +0000937 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000938 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000939
Evan Chengdfed19f2010-11-03 06:34:55 +0000940 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
941
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000943 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000944 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000945 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
946 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000947 case ARMISD::VCGEU: return "ARMISD::VCGEU";
948 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000949 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
950 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000951 case ARMISD::VCGTU: return "ARMISD::VCGTU";
952 case ARMISD::VTST: return "ARMISD::VTST";
953
954 case ARMISD::VSHL: return "ARMISD::VSHL";
955 case ARMISD::VSHRs: return "ARMISD::VSHRs";
956 case ARMISD::VSHRu: return "ARMISD::VSHRu";
957 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
958 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
959 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
960 case ARMISD::VSHRN: return "ARMISD::VSHRN";
961 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
962 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
963 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
964 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
965 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
966 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
967 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
968 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
969 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
970 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
971 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
972 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
973 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
974 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000975 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000976 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000977 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000978 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000979 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000980 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000981 case ARMISD::VREV64: return "ARMISD::VREV64";
982 case ARMISD::VREV32: return "ARMISD::VREV32";
983 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000984 case ARMISD::VZIP: return "ARMISD::VZIP";
985 case ARMISD::VUZP: return "ARMISD::VUZP";
986 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000987 case ARMISD::VTBL1: return "ARMISD::VTBL1";
988 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000989 case ARMISD::VMULLs: return "ARMISD::VMULLs";
990 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000991 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000992 case ARMISD::FMAX: return "ARMISD::FMAX";
993 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000994 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000995 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
996 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000997 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000998 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
999 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1000 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001001 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1002 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1003 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1004 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1005 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1006 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1007 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1008 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1009 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1010 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1011 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1012 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1013 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1014 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1015 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1016 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1017 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001018 }
1019}
1020
Duncan Sands28b77e92011-09-06 19:07:46 +00001021EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1022 if (!VT.isVector()) return getPointerTy();
1023 return VT.changeVectorElementTypeToInteger();
1024}
1025
Evan Cheng06b666c2010-05-15 02:18:07 +00001026/// getRegClassFor - Return the register class that should be used for the
1027/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001028const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001029 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1030 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1031 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001032 if (Subtarget->hasNEON()) {
1033 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001034 return &ARM::QQPRRegClass;
1035 if (VT == MVT::v8i64)
1036 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001037 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001038 return TargetLowering::getRegClassFor(VT);
1039}
1040
Eric Christopherab695882010-07-21 22:26:11 +00001041// Create a fast isel object.
1042FastISel *
1043ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1044 return ARM::createFastISel(funcInfo);
1045}
1046
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001047/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1048/// be used for loads / stores from the global.
1049unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1050 return (Subtarget->isThumb1Only() ? 127 : 4095);
1051}
1052
Evan Cheng1cc39842010-05-20 23:26:43 +00001053Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001054 unsigned NumVals = N->getNumValues();
1055 if (!NumVals)
1056 return Sched::RegPressure;
1057
1058 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001059 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001060 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001061 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001062 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001063 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001064 }
Evan Chengc10f5432010-05-28 23:25:23 +00001065
1066 if (!N->isMachineOpcode())
1067 return Sched::RegPressure;
1068
1069 // Load are scheduled for latency even if there instruction itinerary
1070 // is not available.
1071 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001072 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001073
Evan Chenge837dea2011-06-28 19:10:37 +00001074 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001075 return Sched::RegPressure;
1076 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001077 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001078 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001079
Evan Cheng1cc39842010-05-20 23:26:43 +00001080 return Sched::RegPressure;
1081}
1082
Evan Chenga8e29892007-01-19 07:51:42 +00001083//===----------------------------------------------------------------------===//
1084// Lowering Code
1085//===----------------------------------------------------------------------===//
1086
Evan Chenga8e29892007-01-19 07:51:42 +00001087/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1088static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1089 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001090 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001091 case ISD::SETNE: return ARMCC::NE;
1092 case ISD::SETEQ: return ARMCC::EQ;
1093 case ISD::SETGT: return ARMCC::GT;
1094 case ISD::SETGE: return ARMCC::GE;
1095 case ISD::SETLT: return ARMCC::LT;
1096 case ISD::SETLE: return ARMCC::LE;
1097 case ISD::SETUGT: return ARMCC::HI;
1098 case ISD::SETUGE: return ARMCC::HS;
1099 case ISD::SETULT: return ARMCC::LO;
1100 case ISD::SETULE: return ARMCC::LS;
1101 }
1102}
1103
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001104/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1105static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001106 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001107 CondCode2 = ARMCC::AL;
1108 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001109 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001110 case ISD::SETEQ:
1111 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1112 case ISD::SETGT:
1113 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1114 case ISD::SETGE:
1115 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1116 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001117 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001118 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1119 case ISD::SETO: CondCode = ARMCC::VC; break;
1120 case ISD::SETUO: CondCode = ARMCC::VS; break;
1121 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1122 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1123 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1124 case ISD::SETLT:
1125 case ISD::SETULT: CondCode = ARMCC::LT; break;
1126 case ISD::SETLE:
1127 case ISD::SETULE: CondCode = ARMCC::LE; break;
1128 case ISD::SETNE:
1129 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1130 }
Evan Chenga8e29892007-01-19 07:51:42 +00001131}
1132
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133//===----------------------------------------------------------------------===//
1134// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135//===----------------------------------------------------------------------===//
1136
1137#include "ARMGenCallingConv.inc"
1138
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001139/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1140/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001141CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001142 bool Return,
1143 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001144 switch (CC) {
1145 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001146 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001147 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001148 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001149 if (!Subtarget->isAAPCS_ABI())
1150 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1151 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1152 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1153 }
1154 // Fallthrough
1155 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001156 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001157 if (!Subtarget->isAAPCS_ABI())
1158 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1159 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001160 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1161 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001162 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1163 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1164 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001165 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001166 if (!isVarArg)
1167 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1168 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001170 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001171 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001172 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001173 }
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176/// LowerCallResult - Lower the result values of a call into the
1177/// appropriate copies out of appropriate physical registers.
1178SDValue
1179ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001180 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 const SmallVectorImpl<ISD::InputArg> &Ins,
1182 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001183 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 // Assign locations to each value returned by this call.
1186 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001187 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1188 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001190 CCAssignFnForNode(CallConv, /* Return*/ true,
1191 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192
1193 // Copy all of the result registers out of their specified physreg.
1194 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1195 CCValAssign VA = RVLocs[i];
1196
Bob Wilson80915242009-04-25 00:33:20 +00001197 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001199 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001202 Chain = Lo.getValue(1);
1203 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001206 InFlag);
1207 Chain = Hi.getValue(1);
1208 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001209 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001210
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 if (VA.getLocVT() == MVT::v2f64) {
1212 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1213 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1214 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001215
1216 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001218 Chain = Lo.getValue(1);
1219 InFlag = Lo.getValue(2);
1220 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001221 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001222 Chain = Hi.getValue(1);
1223 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001224 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1226 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001227 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001229 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1230 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001231 Chain = Val.getValue(1);
1232 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 }
Bob Wilson80915242009-04-25 00:33:20 +00001234
1235 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001236 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001237 case CCValAssign::Full: break;
1238 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001239 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001240 break;
1241 }
1242
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244 }
1245
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247}
1248
Bob Wilsondee46d72009-04-17 20:35:10 +00001249/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1252 SDValue StackPtr, SDValue Arg,
1253 DebugLoc dl, SelectionDAG &DAG,
1254 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001255 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 unsigned LocMemOffset = VA.getLocMemOffset();
1257 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1258 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001260 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001261 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001262}
1263
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001265 SDValue Chain, SDValue &Arg,
1266 RegsToPassVector &RegsToPass,
1267 CCValAssign &VA, CCValAssign &NextVA,
1268 SDValue &StackPtr,
1269 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001270 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001271
Jim Grosbache5165492009-11-09 00:11:35 +00001272 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1275
1276 if (NextVA.isRegLoc())
1277 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1278 else {
1279 assert(NextVA.isMemLoc());
1280 if (StackPtr.getNode() == 0)
1281 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1284 dl, DAG, NextVA,
1285 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001286 }
1287}
1288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001290/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1291/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001293ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001294 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001295 SelectionDAG &DAG = CLI.DAG;
1296 DebugLoc &dl = CLI.DL;
1297 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1298 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1299 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1300 SDValue Chain = CLI.Chain;
1301 SDValue Callee = CLI.Callee;
1302 bool &isTailCall = CLI.IsTailCall;
1303 CallingConv::ID CallConv = CLI.CallConv;
1304 bool doesNotRet = CLI.DoesNotReturn;
1305 bool isVarArg = CLI.IsVarArg;
1306
Dale Johannesen51e28e62010-06-03 21:09:53 +00001307 MachineFunction &MF = DAG.getMachineFunction();
1308 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1309 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001310 // Disable tail calls if they're not supported.
1311 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001312 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001313 if (isTailCall) {
1314 // Check if it's really possible to do a tail call.
1315 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1316 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001317 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001318 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1319 // detected sibcalls.
1320 if (isTailCall) {
1321 ++NumTailCalls;
1322 IsSibCall = true;
1323 }
1324 }
Evan Chenga8e29892007-01-19 07:51:42 +00001325
Bob Wilson1f595bb2009-04-17 19:07:39 +00001326 // Analyze operands of the call, assigning locations to each operand.
1327 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001328 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1329 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001330 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001331 CCAssignFnForNode(CallConv, /* Return*/ false,
1332 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001333
Bob Wilson1f595bb2009-04-17 19:07:39 +00001334 // Get a count of how many bytes are to be pushed on the stack.
1335 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001336
Dale Johannesen51e28e62010-06-03 21:09:53 +00001337 // For tail calls, memory operands are available in our caller's stack.
1338 if (IsSibCall)
1339 NumBytes = 0;
1340
Evan Chenga8e29892007-01-19 07:51:42 +00001341 // Adjust the stack pointer for the new arguments...
1342 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 if (!IsSibCall)
1344 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001345
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001346 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001347
Bob Wilson5bafff32009-06-22 23:27:02 +00001348 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001350
Bob Wilson1f595bb2009-04-17 19:07:39 +00001351 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001352 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1354 i != e;
1355 ++i, ++realArgIdx) {
1356 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001357 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001359 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001360
Bob Wilson1f595bb2009-04-17 19:07:39 +00001361 // Promote the value if needed.
1362 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001363 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 case CCValAssign::Full: break;
1365 case CCValAssign::SExt:
1366 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1367 break;
1368 case CCValAssign::ZExt:
1369 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1370 break;
1371 case CCValAssign::AExt:
1372 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1373 break;
1374 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001375 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001377 }
1378
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001379 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001380 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001381 if (VA.getLocVT() == MVT::v2f64) {
1382 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1383 DAG.getConstant(0, MVT::i32));
1384 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1385 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001386
Dan Gohman98ca4f22009-08-05 01:29:28 +00001387 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001388 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1389
1390 VA = ArgLocs[++i]; // skip ahead to next loc
1391 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001393 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1394 } else {
1395 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001396
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1398 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001399 }
1400 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001402 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001403 }
1404 } else if (VA.isRegLoc()) {
1405 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001406 } else if (isByVal) {
1407 assert(VA.isMemLoc());
1408 unsigned offset = 0;
1409
1410 // True if this byval aggregate will be split between registers
1411 // and memory.
1412 if (CCInfo.isFirstByValRegValid()) {
1413 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1414 unsigned int i, j;
1415 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1416 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1417 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1418 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1419 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001420 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001421 MemOpChains.push_back(Load.getValue(1));
1422 RegsToPass.push_back(std::make_pair(j, Load));
1423 }
1424 offset = ARM::R4 - CCInfo.getFirstByValReg();
1425 CCInfo.clearFirstByValReg();
1426 }
1427
Manman Ren763a75d2012-06-01 02:44:42 +00001428 if (Flags.getByValSize() - 4*offset > 0) {
1429 unsigned LocMemOffset = VA.getLocMemOffset();
1430 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1431 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1432 StkPtrOff);
1433 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1434 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1435 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1436 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001437 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001438
Manman Ren763a75d2012-06-01 02:44:42 +00001439 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001440 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001441 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1442 Ops, array_lengthof(Ops)));
1443 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001444 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001445 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001446
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1448 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001449 }
Evan Chenga8e29892007-01-19 07:51:42 +00001450 }
1451
1452 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001453 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001454 &MemOpChains[0], MemOpChains.size());
1455
1456 // Build a sequence of copy-to-reg nodes chained together with token chain
1457 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001458 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001459 // Tail call byval lowering might overwrite argument registers so in case of
1460 // tail call optimization the copies to registers are lowered later.
1461 if (!isTailCall)
1462 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1463 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1464 RegsToPass[i].second, InFlag);
1465 InFlag = Chain.getValue(1);
1466 }
Evan Chenga8e29892007-01-19 07:51:42 +00001467
Dale Johannesen51e28e62010-06-03 21:09:53 +00001468 // For tail calls lower the arguments to the 'real' stack slot.
1469 if (isTailCall) {
1470 // Force all the incoming stack arguments to be loaded from the stack
1471 // before any new outgoing arguments are stored to the stack, because the
1472 // outgoing stack slots may alias the incoming argument stack slots, and
1473 // the alias isn't otherwise explicit. This is slightly more conservative
1474 // than necessary, because it means that each store effectively depends
1475 // on every argument instead of just those arguments it would clobber.
1476
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001477 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001478 InFlag = SDValue();
1479 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1480 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1481 RegsToPass[i].second, InFlag);
1482 InFlag = Chain.getValue(1);
1483 }
1484 InFlag =SDValue();
1485 }
1486
Bill Wendling056292f2008-09-16 21:48:12 +00001487 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1488 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1489 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001490 bool isDirect = false;
1491 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001492 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001493 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001494
1495 if (EnableARMLongCalls) {
1496 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1497 && "long-calls with non-static relocation model!");
1498 // Handle a global address or an external symbol. If it's not one of
1499 // those, the target's already in a register, so we don't need to do
1500 // anything extra.
1501 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001502 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001503 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001504 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001505 ARMConstantPoolValue *CPV =
1506 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1507
Jim Grosbache7b52522010-04-14 22:28:31 +00001508 // Get the address of the callee into a register
1509 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1510 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1511 Callee = DAG.getLoad(getPointerTy(), dl,
1512 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001513 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001514 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001515 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1516 const char *Sym = S->getSymbol();
1517
1518 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001519 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001520 ARMConstantPoolValue *CPV =
1521 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1522 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001523 // Get the address of the callee into a register
1524 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1525 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1526 Callee = DAG.getLoad(getPointerTy(), dl,
1527 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001528 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001529 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001530 }
1531 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001532 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001533 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001534 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001535 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001536 getTargetMachine().getRelocationModel() != Reloc::Static;
1537 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001538 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001539 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001540 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001541 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001542 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001543 ARMConstantPoolValue *CPV =
1544 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001545 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001547 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001548 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001549 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001550 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001551 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001552 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001553 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001554 } else {
1555 // On ELF targets for PIC code, direct calls should go through the PLT
1556 unsigned OpFlags = 0;
1557 if (Subtarget->isTargetELF() &&
1558 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1559 OpFlags = ARMII::MO_PLT;
1560 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1561 }
Bill Wendling056292f2008-09-16 21:48:12 +00001562 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001563 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001564 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001565 getTargetMachine().getRelocationModel() != Reloc::Static;
1566 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001567 // tBX takes a register source operand.
1568 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001569 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001570 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001571 ARMConstantPoolValue *CPV =
1572 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1573 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001574 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001576 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001577 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001578 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001579 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001580 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001581 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001582 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001583 } else {
1584 unsigned OpFlags = 0;
1585 // On ELF targets for PIC code, direct calls should go through the PLT
1586 if (Subtarget->isTargetELF() &&
1587 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1588 OpFlags = ARMII::MO_PLT;
1589 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1590 }
Evan Chenga8e29892007-01-19 07:51:42 +00001591 }
1592
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001593 // FIXME: handle tail calls differently.
1594 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001595 if (Subtarget->isThumb()) {
1596 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001597 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001598 else if (doesNotRet && isDirect && !isARMFunc &&
1599 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1600 // "mov lr, pc; b _foo" to avoid confusing the RSP
1601 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001602 else
1603 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1604 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001605 if (!isDirect && !Subtarget->hasV5TOps()) {
1606 CallOpc = ARMISD::CALL_NOLINK;
1607 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1608 // "mov lr, pc; b _foo" to avoid confusing the RSP
1609 CallOpc = ARMISD::CALL_NOLINK;
1610 else
1611 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001612 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001613
Dan Gohman475871a2008-07-27 21:46:04 +00001614 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001615 Ops.push_back(Chain);
1616 Ops.push_back(Callee);
1617
1618 // Add argument registers to the end of the list so that they are known live
1619 // into the call.
1620 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1621 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1622 RegsToPass[i].second.getValueType()));
1623
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001624 // Add a register mask operand representing the call-preserved registers.
1625 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1626 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1627 assert(Mask && "Missing call preserved mask for calling convention");
1628 Ops.push_back(DAG.getRegisterMask(Mask));
1629
Gabor Greifba36cb52008-08-28 21:40:38 +00001630 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001631 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001632
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001633 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001634 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636
Duncan Sands4bdcb612008-07-02 17:40:58 +00001637 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001638 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001639 InFlag = Chain.getValue(1);
1640
Chris Lattnere563bbc2008-10-11 22:08:30 +00001641 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1642 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001643 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001644 InFlag = Chain.getValue(1);
1645
Bob Wilson1f595bb2009-04-17 19:07:39 +00001646 // Handle result values, copying them out of physregs into vregs that we
1647 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1649 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001650}
1651
Stuart Hastingsf222e592011-02-28 17:17:53 +00001652/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001653/// on the stack. Remember the next parameter register to allocate,
1654/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001655/// this.
1656void
Craig Topperc89c7442012-03-27 07:21:54 +00001657ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001658 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1659 assert((State->getCallOrPrologue() == Prologue ||
1660 State->getCallOrPrologue() == Call) &&
1661 "unhandled ParmContext");
1662 if ((!State->isFirstByValRegValid()) &&
1663 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1664 State->setFirstByValReg(reg);
1665 // At a call site, a byval parameter that is split between
1666 // registers and memory needs its size truncated here. In a
1667 // function prologue, such byval parameters are reassembled in
1668 // memory, and are not truncated.
1669 if (State->getCallOrPrologue() == Call) {
1670 unsigned excess = 4 * (ARM::R4 - reg);
1671 assert(size >= excess && "expected larger existing stack allocation");
1672 size -= excess;
1673 }
1674 }
1675 // Confiscate any remaining parameter registers to preclude their
1676 // assignment to subsequent parameters.
1677 while (State->AllocateReg(GPRArgRegs, 4))
1678 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001679}
1680
Dale Johannesen51e28e62010-06-03 21:09:53 +00001681/// MatchingStackOffset - Return true if the given stack call argument is
1682/// already available in the same position (relatively) of the caller's
1683/// incoming argument stack.
1684static
1685bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1686 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001687 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1689 int FI = INT_MAX;
1690 if (Arg.getOpcode() == ISD::CopyFromReg) {
1691 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001692 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001693 return false;
1694 MachineInstr *Def = MRI->getVRegDef(VR);
1695 if (!Def)
1696 return false;
1697 if (!Flags.isByVal()) {
1698 if (!TII->isLoadFromStackSlot(Def, FI))
1699 return false;
1700 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001701 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001702 }
1703 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1704 if (Flags.isByVal())
1705 // ByVal argument is passed in as a pointer but it's now being
1706 // dereferenced. e.g.
1707 // define @foo(%struct.X* %A) {
1708 // tail call @bar(%struct.X* byval %A)
1709 // }
1710 return false;
1711 SDValue Ptr = Ld->getBasePtr();
1712 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1713 if (!FINode)
1714 return false;
1715 FI = FINode->getIndex();
1716 } else
1717 return false;
1718
1719 assert(FI != INT_MAX);
1720 if (!MFI->isFixedObjectIndex(FI))
1721 return false;
1722 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1723}
1724
1725/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1726/// for tail call optimization. Targets which want to do tail call
1727/// optimization should implement this function.
1728bool
1729ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1730 CallingConv::ID CalleeCC,
1731 bool isVarArg,
1732 bool isCalleeStructRet,
1733 bool isCallerStructRet,
1734 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001735 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001736 const SmallVectorImpl<ISD::InputArg> &Ins,
1737 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001738 const Function *CallerF = DAG.getMachineFunction().getFunction();
1739 CallingConv::ID CallerCC = CallerF->getCallingConv();
1740 bool CCMatch = CallerCC == CalleeCC;
1741
1742 // Look for obvious safe cases to perform tail call optimization that do not
1743 // require ABI changes. This is what gcc calls sibcall.
1744
Jim Grosbach7616b642010-06-16 23:45:49 +00001745 // Do not sibcall optimize vararg calls unless the call site is not passing
1746 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001747 if (isVarArg && !Outs.empty())
1748 return false;
1749
1750 // Also avoid sibcall optimization if either caller or callee uses struct
1751 // return semantics.
1752 if (isCalleeStructRet || isCallerStructRet)
1753 return false;
1754
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001755 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001756 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1757 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1758 // support in the assembler and linker to be used. This would need to be
1759 // fixed to fully support tail calls in Thumb1.
1760 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001761 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1762 // LR. This means if we need to reload LR, it takes an extra instructions,
1763 // which outweighs the value of the tail call; but here we don't know yet
1764 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001765 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001766 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001767
1768 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1769 // but we need to make sure there are enough registers; the only valid
1770 // registers are the 4 used for parameters. We don't currently do this
1771 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001772 if (Subtarget->isThumb1Only())
1773 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001774
Dale Johannesen51e28e62010-06-03 21:09:53 +00001775 // If the calling conventions do not match, then we'd better make sure the
1776 // results are returned in the same way as what the caller expects.
1777 if (!CCMatch) {
1778 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001779 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1780 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001781 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1782
1783 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001784 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1785 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001786 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1787
1788 if (RVLocs1.size() != RVLocs2.size())
1789 return false;
1790 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1791 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1792 return false;
1793 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1794 return false;
1795 if (RVLocs1[i].isRegLoc()) {
1796 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1797 return false;
1798 } else {
1799 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1800 return false;
1801 }
1802 }
1803 }
1804
1805 // If the callee takes no arguments then go on to check the results of the
1806 // call.
1807 if (!Outs.empty()) {
1808 // Check if stack adjustment is needed. For now, do not do this if any
1809 // argument is passed on the stack.
1810 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001811 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1812 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001813 CCInfo.AnalyzeCallOperands(Outs,
1814 CCAssignFnForNode(CalleeCC, false, isVarArg));
1815 if (CCInfo.getNextStackOffset()) {
1816 MachineFunction &MF = DAG.getMachineFunction();
1817
1818 // Check if the arguments are already laid out in the right way as
1819 // the caller's fixed stack objects.
1820 MachineFrameInfo *MFI = MF.getFrameInfo();
1821 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001822 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001823 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1824 i != e;
1825 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001826 CCValAssign &VA = ArgLocs[i];
1827 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001828 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001829 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001830 if (VA.getLocInfo() == CCValAssign::Indirect)
1831 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001832 if (VA.needsCustom()) {
1833 // f64 and vector types are split into multiple registers or
1834 // register/stack-slot combinations. The types will not match
1835 // the registers; give up on memory f64 refs until we figure
1836 // out what to do about this.
1837 if (!VA.isRegLoc())
1838 return false;
1839 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001840 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001841 if (RegVT == MVT::v2f64) {
1842 if (!ArgLocs[++i].isRegLoc())
1843 return false;
1844 if (!ArgLocs[++i].isRegLoc())
1845 return false;
1846 }
1847 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001848 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1849 MFI, MRI, TII))
1850 return false;
1851 }
1852 }
1853 }
1854 }
1855
1856 return true;
1857}
1858
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859SDValue
1860ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001861 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001863 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001864 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001865
Bob Wilsondee46d72009-04-17 20:35:10 +00001866 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001867 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001868
Bob Wilsondee46d72009-04-17 20:35:10 +00001869 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001870 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1871 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001874 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1875 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001876
1877 // If this is the first return lowered for this function, add
1878 // the regs to the liveout set for the function.
1879 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1880 for (unsigned i = 0; i != RVLocs.size(); ++i)
1881 if (RVLocs[i].isRegLoc())
1882 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001883 }
1884
Bob Wilson1f595bb2009-04-17 19:07:39 +00001885 SDValue Flag;
1886
1887 // Copy the result values into the output registers.
1888 for (unsigned i = 0, realRVLocIdx = 0;
1889 i != RVLocs.size();
1890 ++i, ++realRVLocIdx) {
1891 CCValAssign &VA = RVLocs[i];
1892 assert(VA.isRegLoc() && "Can only return in registers!");
1893
Dan Gohmanc9403652010-07-07 15:54:55 +00001894 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001895
1896 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001897 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001898 case CCValAssign::Full: break;
1899 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001900 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001901 break;
1902 }
1903
Bob Wilson1f595bb2009-04-17 19:07:39 +00001904 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001905 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001906 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1908 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001909 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001911
1912 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1913 Flag = Chain.getValue(1);
1914 VA = RVLocs[++i]; // skip ahead to next loc
1915 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1916 HalfGPRs.getValue(1), Flag);
1917 Flag = Chain.getValue(1);
1918 VA = RVLocs[++i]; // skip ahead to next loc
1919
1920 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1922 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001923 }
1924 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1925 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001926 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001928 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001929 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001930 VA = RVLocs[++i]; // skip ahead to next loc
1931 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1932 Flag);
1933 } else
1934 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1935
Bob Wilsondee46d72009-04-17 20:35:10 +00001936 // Guarantee that all emitted copies are
1937 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001938 Flag = Chain.getValue(1);
1939 }
1940
1941 SDValue result;
1942 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001944 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001946
1947 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001948}
1949
Evan Chengbf010eb2012-04-10 01:51:00 +00001950bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001951 if (N->getNumValues() != 1)
1952 return false;
1953 if (!N->hasNUsesOfValue(1, 0))
1954 return false;
1955
Evan Chengbf010eb2012-04-10 01:51:00 +00001956 SDValue TCChain = Chain;
1957 SDNode *Copy = *N->use_begin();
1958 if (Copy->getOpcode() == ISD::CopyToReg) {
1959 // If the copy has a glue operand, we conservatively assume it isn't safe to
1960 // perform a tail call.
1961 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1962 return false;
1963 TCChain = Copy->getOperand(0);
1964 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1965 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001966 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001967 SmallPtrSet<SDNode*, 2> Copies;
1968 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001969 UI != UE; ++UI) {
1970 if (UI->getOpcode() != ISD::CopyToReg)
1971 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001972 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001973 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001974 if (Copies.size() > 2)
1975 return false;
1976
1977 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1978 UI != UE; ++UI) {
1979 SDValue UseChain = UI->getOperand(0);
1980 if (Copies.count(UseChain.getNode()))
1981 // Second CopyToReg
1982 Copy = *UI;
1983 else
1984 // First CopyToReg
1985 TCChain = UseChain;
1986 }
1987 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001988 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001989 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001990 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001991 Copy = *Copy->use_begin();
1992 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001993 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001994 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001995 } else {
1996 return false;
1997 }
1998
Evan Cheng1bf891a2010-12-01 22:59:46 +00001999 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002000 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2001 UI != UE; ++UI) {
2002 if (UI->getOpcode() != ARMISD::RET_FLAG)
2003 return false;
2004 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002005 }
2006
Evan Chengbf010eb2012-04-10 01:51:00 +00002007 if (!HasRet)
2008 return false;
2009
2010 Chain = TCChain;
2011 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002012}
2013
Evan Cheng485fafc2011-03-21 01:19:09 +00002014bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002015 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002016 return false;
2017
2018 if (!CI->isTailCall())
2019 return false;
2020
2021 return !Subtarget->isThumb1Only();
2022}
2023
Bob Wilsonb62d2572009-11-03 00:02:05 +00002024// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2025// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2026// one of the above mentioned nodes. It has to be wrapped because otherwise
2027// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2028// be used to form addressing mode. These wrapped nodes will be selected
2029// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002030static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002032 // FIXME there is no actual debug info here
2033 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002034 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002036 if (CP->isMachineConstantPoolEntry())
2037 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2038 CP->getAlignment());
2039 else
2040 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2041 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002043}
2044
Jim Grosbache1102ca2010-07-19 17:20:38 +00002045unsigned ARMTargetLowering::getJumpTableEncoding() const {
2046 return MachineJumpTableInfo::EK_Inline;
2047}
2048
Dan Gohmand858e902010-04-17 15:26:15 +00002049SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2050 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002051 MachineFunction &MF = DAG.getMachineFunction();
2052 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2053 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002054 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002055 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002056 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002057 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2058 SDValue CPAddr;
2059 if (RelocM == Reloc::Static) {
2060 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2061 } else {
2062 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002063 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002064 ARMConstantPoolValue *CPV =
2065 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2066 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002067 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2068 }
2069 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2070 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002071 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002072 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002073 if (RelocM == Reloc::Static)
2074 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002075 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002076 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002077}
2078
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002080SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002081ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002082 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002083 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002084 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002086 MachineFunction &MF = DAG.getMachineFunction();
2087 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002088 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002089 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002090 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2091 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002092 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002094 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002095 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002096 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002097 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002098
Evan Chenge7e0d622009-11-06 22:24:13 +00002099 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002100 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002101
2102 // call __tls_get_addr.
2103 ArgListTy Args;
2104 ArgListEntry Entry;
2105 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002106 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002107 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002108 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002109 TargetLowering::CallLoweringInfo CLI(Chain,
2110 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002111 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002112 0, CallingConv::C, /*isTailCall=*/false,
2113 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002114 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002115 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002116 return CallResult.first;
2117}
2118
2119// Lower ISD::GlobalTLSAddress using the "initial exec" or
2120// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002121SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002122ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002123 SelectionDAG &DAG,
2124 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002125 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002126 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002127 SDValue Offset;
2128 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002129 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002130 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002132
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002133 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002134 MachineFunction &MF = DAG.getMachineFunction();
2135 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002136 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002137 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002138 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2139 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002140 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2141 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2142 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002143 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002144 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002145 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002146 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002147 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002148 Chain = Offset.getValue(1);
2149
Evan Chenge7e0d622009-11-06 22:24:13 +00002150 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002151 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002152
Evan Cheng9eda6892009-10-31 03:39:36 +00002153 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002154 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002155 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002156 } else {
2157 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002158 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002159 ARMConstantPoolValue *CPV =
2160 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002161 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002162 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002163 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002164 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002165 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002166 }
2167
2168 // The address of the thread local variable is the add of the thread
2169 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002170 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002171}
2172
Dan Gohman475871a2008-07-27 21:46:04 +00002173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002174ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002175 // TODO: implement the "local dynamic" model
2176 assert(Subtarget->isTargetELF() &&
2177 "TLS not implemented for non-ELF targets");
2178 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002179
2180 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2181
2182 switch (model) {
2183 case TLSModel::GeneralDynamic:
2184 case TLSModel::LocalDynamic:
2185 return LowerToTLSGeneralDynamicModel(GA, DAG);
2186 case TLSModel::InitialExec:
2187 case TLSModel::LocalExec:
2188 return LowerToTLSExecModels(GA, DAG, model);
2189 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002190 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002191}
2192
Dan Gohman475871a2008-07-27 21:46:04 +00002193SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002194 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002195 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002196 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002197 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002198 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2199 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002200 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002201 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002202 ARMConstantPoolConstant::Create(GV,
2203 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002204 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002206 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002207 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002208 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002209 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002210 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002211 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002212 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002213 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002214 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002215 MachinePointerInfo::getGOT(),
2216 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002217 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002218 }
2219
2220 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002221 // pair. This is always cheaper.
2222 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002223 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002224 // FIXME: Once remat is capable of dealing with instructions with register
2225 // operands, expand this into two nodes.
2226 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2227 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002228 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002229 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2230 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2231 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2232 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002233 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002234 }
2235}
2236
Dan Gohman475871a2008-07-27 21:46:04 +00002237SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002238 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002239 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002240 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002241 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002242 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002243 MachineFunction &MF = DAG.getMachineFunction();
2244 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2245
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002246 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2247 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002248 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002249 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002250 // FIXME: Once remat is capable of dealing with instructions with register
2251 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002252 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002253 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2254 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2255
Evan Cheng53519f02011-01-21 18:55:51 +00002256 unsigned Wrapper = (RelocM == Reloc::PIC_)
2257 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2258 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002259 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002260 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2261 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002262 MachinePointerInfo::getGOT(),
2263 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002264 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002265 }
2266
2267 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002269 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002270 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002271 } else {
2272 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002273 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2274 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002275 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2276 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002277 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002278 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002280
Evan Cheng9eda6892009-10-31 03:39:36 +00002281 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002282 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002283 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002285
2286 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002287 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002288 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002289 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002290
Evan Cheng63476a82009-09-03 07:04:02 +00002291 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002292 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002293 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002294
2295 return Result;
2296}
2297
Dan Gohman475871a2008-07-27 21:46:04 +00002298SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002299 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002300 assert(Subtarget->isTargetELF() &&
2301 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002302 MachineFunction &MF = DAG.getMachineFunction();
2303 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002304 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002305 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002306 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002307 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002308 ARMConstantPoolValue *CPV =
2309 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2310 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002311 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002312 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002313 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002314 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002315 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002316 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002317 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002318}
2319
Jim Grosbach0e0da732009-05-12 23:59:14 +00002320SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002321ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2322 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002323 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002324 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2325 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002326 Op.getOperand(1), Val);
2327}
2328
2329SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002330ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2331 DebugLoc dl = Op.getDebugLoc();
2332 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2333 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2334}
2335
2336SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002337ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002338 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002339 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002340 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002341 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002342 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002343 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002344 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002345 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2346 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002347 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002348 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002349 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002350 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002351 EVT PtrVT = getPointerTy();
2352 DebugLoc dl = Op.getDebugLoc();
2353 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2354 SDValue CPAddr;
2355 unsigned PCAdj = (RelocM != Reloc::PIC_)
2356 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002357 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002358 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2359 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002360 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002361 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002362 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002363 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002364 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002365 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002366
2367 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002368 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002369 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2370 }
2371 return Result;
2372 }
Evan Cheng92e39162011-03-29 23:06:19 +00002373 case Intrinsic::arm_neon_vmulls:
2374 case Intrinsic::arm_neon_vmullu: {
2375 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2376 ? ARMISD::VMULLs : ARMISD::VMULLu;
2377 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2378 Op.getOperand(1), Op.getOperand(2));
2379 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002380 }
2381}
2382
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002383static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002384 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002385 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002386 if (!Subtarget->hasDataBarrier()) {
2387 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2388 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2389 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002390 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002391 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002392 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002393 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002394 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002395
2396 SDValue Op5 = Op.getOperand(5);
2397 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2398 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2399 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2400 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2401
2402 ARM_MB::MemBOpt DMBOpt;
2403 if (isDeviceBarrier)
2404 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2405 else
2406 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2407 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2408 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002409}
2410
Eli Friedman26689ac2011-08-03 21:06:02 +00002411
2412static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2413 const ARMSubtarget *Subtarget) {
2414 // FIXME: handle "fence singlethread" more efficiently.
2415 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002416 if (!Subtarget->hasDataBarrier()) {
2417 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2418 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2419 // here.
2420 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2421 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002422 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002423 DAG.getConstant(0, MVT::i32));
2424 }
2425
Eli Friedman26689ac2011-08-03 21:06:02 +00002426 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002427 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002428}
2429
Evan Chengdfed19f2010-11-03 06:34:55 +00002430static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2431 const ARMSubtarget *Subtarget) {
2432 // ARM pre v5TE and Thumb1 does not have preload instructions.
2433 if (!(Subtarget->isThumb2() ||
2434 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2435 // Just preserve the chain.
2436 return Op.getOperand(0);
2437
2438 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002439 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2440 if (!isRead &&
2441 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2442 // ARMv7 with MP extension has PLDW.
2443 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002444
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002445 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2446 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002447 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002448 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002449 isData = ~isData & 1;
2450 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002451
2452 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002453 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2454 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002455}
2456
Dan Gohman1e93df62010-04-17 14:41:14 +00002457static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2458 MachineFunction &MF = DAG.getMachineFunction();
2459 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2460
Evan Chenga8e29892007-01-19 07:51:42 +00002461 // vastart just stores the address of the VarArgsFrameIndex slot into the
2462 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002463 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002464 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002465 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002466 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002467 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2468 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002469}
2470
Dan Gohman475871a2008-07-27 21:46:04 +00002471SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002472ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2473 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002474 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002475 MachineFunction &MF = DAG.getMachineFunction();
2476 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2477
Craig Topper44d23822012-02-22 05:59:10 +00002478 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002479 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002480 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002481 else
Craig Topper420761a2012-04-20 07:30:17 +00002482 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002483
2484 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002485 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002487
2488 SDValue ArgValue2;
2489 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002490 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002491 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002492
2493 // Create load node to retrieve arguments from the stack.
2494 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002495 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002496 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002497 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002498 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002499 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002500 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 }
2502
Jim Grosbache5165492009-11-09 00:11:35 +00002503 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002504}
2505
Stuart Hastingsc7315872011-04-20 16:47:52 +00002506void
2507ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2508 unsigned &VARegSize, unsigned &VARegSaveSize)
2509 const {
2510 unsigned NumGPRs;
2511 if (CCInfo.isFirstByValRegValid())
2512 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2513 else {
2514 unsigned int firstUnalloced;
2515 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2516 sizeof(GPRArgRegs) /
2517 sizeof(GPRArgRegs[0]));
2518 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2519 }
2520
2521 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2522 VARegSize = NumGPRs * 4;
2523 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2524}
2525
2526// The remaining GPRs hold either the beginning of variable-argument
2527// data, or the beginning of an aggregate passed by value (usuall
2528// byval). Either way, we allocate stack slots adjacent to the data
2529// provided by our caller, and store the unallocated registers there.
2530// If this is a variadic function, the va_list pointer will begin with
2531// these values; otherwise, this reassembles a (byval) structure that
2532// was split between registers and memory.
2533void
2534ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2535 DebugLoc dl, SDValue &Chain,
2536 unsigned ArgOffset) const {
2537 MachineFunction &MF = DAG.getMachineFunction();
2538 MachineFrameInfo *MFI = MF.getFrameInfo();
2539 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2540 unsigned firstRegToSaveIndex;
2541 if (CCInfo.isFirstByValRegValid())
2542 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2543 else {
2544 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2545 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2546 }
2547
2548 unsigned VARegSize, VARegSaveSize;
2549 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2550 if (VARegSaveSize) {
2551 // If this function is vararg, store any remaining integer argument regs
2552 // to their spots on the stack so that they may be loaded by deferencing
2553 // the result of va_next.
2554 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002555 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2556 ArgOffset + VARegSaveSize
2557 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002558 false));
2559 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2560 getPointerTy());
2561
2562 SmallVector<SDValue, 4> MemOps;
2563 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002564 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002565 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002566 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002567 else
Craig Topper420761a2012-04-20 07:30:17 +00002568 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002569
2570 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2571 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2572 SDValue Store =
2573 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002574 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002575 false, false, 0);
2576 MemOps.push_back(Store);
2577 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2578 DAG.getConstant(4, getPointerTy()));
2579 }
2580 if (!MemOps.empty())
2581 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2582 &MemOps[0], MemOps.size());
2583 } else
2584 // This will point to the next argument passed via stack.
2585 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2586}
2587
Bob Wilson5bafff32009-06-22 23:27:02 +00002588SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002591 const SmallVectorImpl<ISD::InputArg>
2592 &Ins,
2593 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002594 SmallVectorImpl<SDValue> &InVals)
2595 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002596 MachineFunction &MF = DAG.getMachineFunction();
2597 MachineFrameInfo *MFI = MF.getFrameInfo();
2598
Bob Wilson1f595bb2009-04-17 19:07:39 +00002599 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2600
2601 // Assign locations to all of the incoming arguments.
2602 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002603 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2604 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002605 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002606 CCAssignFnForNode(CallConv, /* Return*/ false,
2607 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002608
2609 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002610 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002611
Stuart Hastingsf222e592011-02-28 17:17:53 +00002612 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002613 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2614 CCValAssign &VA = ArgLocs[i];
2615
Bob Wilsondee46d72009-04-17 20:35:10 +00002616 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002617 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002618 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002621 // f64 and vector types are split up into multiple registers or
2622 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002624 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002625 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002626 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002627 SDValue ArgValue2;
2628 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002629 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002630 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2631 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002632 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002633 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002634 } else {
2635 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2636 Chain, DAG, dl);
2637 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2639 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002640 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002642 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2643 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002644 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002645
Bob Wilson5bafff32009-06-22 23:27:02 +00002646 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002647 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002648
Owen Anderson825b72b2009-08-11 20:47:22 +00002649 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002650 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002652 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002654 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002656 RC = AFI->isThumb1OnlyFunction() ?
2657 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2658 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002659 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002660 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002661
2662 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002663 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002665 }
2666
2667 // If this is an 8 or 16-bit value, it is really passed promoted
2668 // to 32 bits. Insert an assert[sz]ext to capture this, then
2669 // truncate to the right size.
2670 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002671 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002672 case CCValAssign::Full: break;
2673 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002674 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002675 break;
2676 case CCValAssign::SExt:
2677 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2678 DAG.getValueType(VA.getValVT()));
2679 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2680 break;
2681 case CCValAssign::ZExt:
2682 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2683 DAG.getValueType(VA.getValVT()));
2684 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2685 break;
2686 }
2687
Dan Gohman98ca4f22009-08-05 01:29:28 +00002688 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002689
2690 } else { // VA.isRegLoc()
2691
2692 // sanity check
2693 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002694 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002695
Stuart Hastingsf222e592011-02-28 17:17:53 +00002696 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002697
Stuart Hastingsf222e592011-02-28 17:17:53 +00002698 // Some Ins[] entries become multiple ArgLoc[] entries.
2699 // Process them only once.
2700 if (index != lastInsIndex)
2701 {
2702 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002703 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002704 // This can be changed with more analysis.
2705 // In case of tail call optimization mark all arguments mutable.
2706 // Since they could be overwritten by lowering of arguments in case of
2707 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002708 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002709 unsigned VARegSize, VARegSaveSize;
2710 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2711 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2712 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002713 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002714 int FI = MFI->CreateFixedObject(Bytes,
2715 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002716 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2717 } else {
2718 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2719 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002720
Stuart Hastingsf222e592011-02-28 17:17:53 +00002721 // Create load nodes to retrieve arguments from the stack.
2722 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2723 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2724 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002725 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002726 }
2727 lastInsIndex = index;
2728 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002729 }
2730 }
2731
2732 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002733 if (isVarArg)
2734 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002735
Dan Gohman98ca4f22009-08-05 01:29:28 +00002736 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002737}
2738
2739/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002740static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002741 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002742 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002743 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002744 // Maybe this has already been legalized into the constant pool?
2745 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002747 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002748 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002749 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002750 }
2751 }
2752 return false;
2753}
2754
Evan Chenga8e29892007-01-19 07:51:42 +00002755/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2756/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002757SDValue
2758ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002759 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002760 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002761 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002762 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002763 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002764 // Constant does not fit, try adjusting it by one?
2765 switch (CC) {
2766 default: break;
2767 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002768 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002769 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002770 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002771 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002772 }
2773 break;
2774 case ISD::SETULT:
2775 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002776 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002777 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002779 }
2780 break;
2781 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002782 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002783 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002784 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002785 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002786 }
2787 break;
2788 case ISD::SETULE:
2789 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002790 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002791 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002792 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002793 }
2794 break;
2795 }
2796 }
2797 }
2798
2799 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002800 ARMISD::NodeType CompareType;
2801 switch (CondCode) {
2802 default:
2803 CompareType = ARMISD::CMP;
2804 break;
2805 case ARMCC::EQ:
2806 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002807 // Uses only Z Flag
2808 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002809 break;
2810 }
Evan Cheng218977b2010-07-13 19:27:42 +00002811 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002812 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002813}
2814
2815/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002816SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002817ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002818 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002819 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002820 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002821 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002822 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002823 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2824 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002825}
2826
Bob Wilson79f56c92011-03-08 01:17:20 +00002827/// duplicateCmp - Glue values can have only one use, so this function
2828/// duplicates a comparison node.
2829SDValue
2830ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2831 unsigned Opc = Cmp.getOpcode();
2832 DebugLoc DL = Cmp.getDebugLoc();
2833 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2834 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2835
2836 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2837 Cmp = Cmp.getOperand(0);
2838 Opc = Cmp.getOpcode();
2839 if (Opc == ARMISD::CMPFP)
2840 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2841 else {
2842 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2843 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2844 }
2845 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2846}
2847
Bill Wendlingde2b1512010-08-11 08:43:16 +00002848SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2849 SDValue Cond = Op.getOperand(0);
2850 SDValue SelectTrue = Op.getOperand(1);
2851 SDValue SelectFalse = Op.getOperand(2);
2852 DebugLoc dl = Op.getDebugLoc();
2853
2854 // Convert:
2855 //
2856 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2857 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2858 //
2859 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2860 const ConstantSDNode *CMOVTrue =
2861 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2862 const ConstantSDNode *CMOVFalse =
2863 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2864
2865 if (CMOVTrue && CMOVFalse) {
2866 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2867 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2868
2869 SDValue True;
2870 SDValue False;
2871 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2872 True = SelectTrue;
2873 False = SelectFalse;
2874 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2875 True = SelectFalse;
2876 False = SelectTrue;
2877 }
2878
2879 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002880 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002881 SDValue ARMcc = Cond.getOperand(2);
2882 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002883 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002884 assert(True.getValueType() == VT);
2885 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002886 }
2887 }
2888 }
2889
Dan Gohmandb953892012-02-24 00:09:36 +00002890 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2891 // undefined bits before doing a full-word comparison with zero.
2892 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2893 DAG.getConstant(1, Cond.getValueType()));
2894
Bill Wendlingde2b1512010-08-11 08:43:16 +00002895 return DAG.getSelectCC(dl, Cond,
2896 DAG.getConstant(0, Cond.getValueType()),
2897 SelectTrue, SelectFalse, ISD::SETNE);
2898}
2899
Dan Gohmand858e902010-04-17 15:26:15 +00002900SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002901 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002902 SDValue LHS = Op.getOperand(0);
2903 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002904 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue TrueVal = Op.getOperand(2);
2906 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002907 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002908
Owen Anderson825b72b2009-08-11 20:47:22 +00002909 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002910 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002911 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002912 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002913 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002914 }
2915
2916 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002917 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002918
Evan Cheng218977b2010-07-13 19:27:42 +00002919 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2920 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002922 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002923 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002924 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002925 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002926 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002927 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002928 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002929 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002930 }
2931 return Result;
2932}
2933
Evan Cheng218977b2010-07-13 19:27:42 +00002934/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2935/// to morph to an integer compare sequence.
2936static bool canChangeToInt(SDValue Op, bool &SeenZero,
2937 const ARMSubtarget *Subtarget) {
2938 SDNode *N = Op.getNode();
2939 if (!N->hasOneUse())
2940 // Otherwise it requires moving the value from fp to integer registers.
2941 return false;
2942 if (!N->getNumValues())
2943 return false;
2944 EVT VT = Op.getValueType();
2945 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2946 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2947 // vmrs are very slow, e.g. cortex-a8.
2948 return false;
2949
2950 if (isFloatingPointZero(Op)) {
2951 SeenZero = true;
2952 return true;
2953 }
2954 return ISD::isNormalLoad(N);
2955}
2956
2957static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2958 if (isFloatingPointZero(Op))
2959 return DAG.getConstant(0, MVT::i32);
2960
2961 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2962 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002963 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002964 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002965 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002966
2967 llvm_unreachable("Unknown VFP cmp argument!");
2968}
2969
2970static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2971 SDValue &RetVal1, SDValue &RetVal2) {
2972 if (isFloatingPointZero(Op)) {
2973 RetVal1 = DAG.getConstant(0, MVT::i32);
2974 RetVal2 = DAG.getConstant(0, MVT::i32);
2975 return;
2976 }
2977
2978 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2979 SDValue Ptr = Ld->getBasePtr();
2980 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2981 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002982 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002983 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002984 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002985
2986 EVT PtrType = Ptr.getValueType();
2987 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2988 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2989 PtrType, Ptr, DAG.getConstant(4, PtrType));
2990 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2991 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002992 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002993 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002994 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002995 return;
2996 }
2997
2998 llvm_unreachable("Unknown VFP cmp argument!");
2999}
3000
3001/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3002/// f32 and even f64 comparisons to integer ones.
3003SDValue
3004ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3005 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003006 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003007 SDValue LHS = Op.getOperand(2);
3008 SDValue RHS = Op.getOperand(3);
3009 SDValue Dest = Op.getOperand(4);
3010 DebugLoc dl = Op.getDebugLoc();
3011
Evan Chengfc501a32012-03-01 23:27:13 +00003012 bool LHSSeenZero = false;
3013 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3014 bool RHSSeenZero = false;
3015 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3016 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003017 // If unsafe fp math optimization is enabled and there are no other uses of
3018 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003019 // to an integer comparison.
3020 if (CC == ISD::SETOEQ)
3021 CC = ISD::SETEQ;
3022 else if (CC == ISD::SETUNE)
3023 CC = ISD::SETNE;
3024
Evan Chengfc501a32012-03-01 23:27:13 +00003025 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003026 SDValue ARMcc;
3027 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003028 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3029 bitcastf32Toi32(LHS, DAG), Mask);
3030 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3031 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003032 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3033 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3034 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3035 Chain, Dest, ARMcc, CCR, Cmp);
3036 }
3037
3038 SDValue LHS1, LHS2;
3039 SDValue RHS1, RHS2;
3040 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3041 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003042 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3043 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003044 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3045 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003046 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003047 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3048 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3049 }
3050
3051 return SDValue();
3052}
3053
3054SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3055 SDValue Chain = Op.getOperand(0);
3056 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3057 SDValue LHS = Op.getOperand(2);
3058 SDValue RHS = Op.getOperand(3);
3059 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003060 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003061
Owen Anderson825b72b2009-08-11 20:47:22 +00003062 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003063 SDValue ARMcc;
3064 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003065 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003067 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003068 }
3069
Owen Anderson825b72b2009-08-11 20:47:22 +00003070 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003071
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003072 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003073 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3074 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3075 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3076 if (Result.getNode())
3077 return Result;
3078 }
3079
Evan Chenga8e29892007-01-19 07:51:42 +00003080 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003081 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003082
Evan Cheng218977b2010-07-13 19:27:42 +00003083 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3084 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003085 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003086 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003087 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003088 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003089 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003090 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3091 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003092 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003093 }
3094 return Res;
3095}
3096
Dan Gohmand858e902010-04-17 15:26:15 +00003097SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003098 SDValue Chain = Op.getOperand(0);
3099 SDValue Table = Op.getOperand(1);
3100 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003101 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003102
Owen Andersone50ed302009-08-10 22:56:29 +00003103 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003104 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3105 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003106 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003107 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003109 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3110 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003111 if (Subtarget->isThumb2()) {
3112 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3113 // which does another jump to the destination. This also makes it easier
3114 // to translate it to TBB / TBH later.
3115 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003117 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003118 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003119 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003120 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003121 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003122 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003123 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003124 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003126 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003127 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003128 MachinePointerInfo::getJumpTable(),
3129 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003130 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003132 }
Evan Chenga8e29892007-01-19 07:51:42 +00003133}
3134
Eli Friedman14e809c2011-11-09 23:36:02 +00003135static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003136 EVT VT = Op.getValueType();
3137 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003138
James Molloy873fd5f2012-02-20 09:24:05 +00003139 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3140 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3141 return Op;
3142 return DAG.UnrollVectorOp(Op.getNode());
3143 }
3144
3145 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3146 "Invalid type for custom lowering!");
3147 if (VT != MVT::v4i16)
3148 return DAG.UnrollVectorOp(Op.getNode());
3149
3150 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3151 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003152}
3153
Bob Wilson76a312b2010-03-19 22:51:32 +00003154static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003155 EVT VT = Op.getValueType();
3156 if (VT.isVector())
3157 return LowerVectorFP_TO_INT(Op, DAG);
3158
Bob Wilson76a312b2010-03-19 22:51:32 +00003159 DebugLoc dl = Op.getDebugLoc();
3160 unsigned Opc;
3161
3162 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003163 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003164 case ISD::FP_TO_SINT:
3165 Opc = ARMISD::FTOSI;
3166 break;
3167 case ISD::FP_TO_UINT:
3168 Opc = ARMISD::FTOUI;
3169 break;
3170 }
3171 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003172 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003173}
3174
Cameron Zwarich3007d332011-03-29 21:41:55 +00003175static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3176 EVT VT = Op.getValueType();
3177 DebugLoc dl = Op.getDebugLoc();
3178
Eli Friedman14e809c2011-11-09 23:36:02 +00003179 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3180 if (VT.getVectorElementType() == MVT::f32)
3181 return Op;
3182 return DAG.UnrollVectorOp(Op.getNode());
3183 }
3184
Duncan Sands1f6a3292011-08-12 14:54:45 +00003185 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3186 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003187 if (VT != MVT::v4f32)
3188 return DAG.UnrollVectorOp(Op.getNode());
3189
3190 unsigned CastOpc;
3191 unsigned Opc;
3192 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003193 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003194 case ISD::SINT_TO_FP:
3195 CastOpc = ISD::SIGN_EXTEND;
3196 Opc = ISD::SINT_TO_FP;
3197 break;
3198 case ISD::UINT_TO_FP:
3199 CastOpc = ISD::ZERO_EXTEND;
3200 Opc = ISD::UINT_TO_FP;
3201 break;
3202 }
3203
3204 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3205 return DAG.getNode(Opc, dl, VT, Op);
3206}
3207
Bob Wilson76a312b2010-03-19 22:51:32 +00003208static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3209 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003210 if (VT.isVector())
3211 return LowerVectorINT_TO_FP(Op, DAG);
3212
Bob Wilson76a312b2010-03-19 22:51:32 +00003213 DebugLoc dl = Op.getDebugLoc();
3214 unsigned Opc;
3215
3216 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003217 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003218 case ISD::SINT_TO_FP:
3219 Opc = ARMISD::SITOF;
3220 break;
3221 case ISD::UINT_TO_FP:
3222 Opc = ARMISD::UITOF;
3223 break;
3224 }
3225
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003226 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003227 return DAG.getNode(Opc, dl, VT, Op);
3228}
3229
Evan Cheng515fe3a2010-07-08 02:08:50 +00003230SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003231 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003232 SDValue Tmp0 = Op.getOperand(0);
3233 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003234 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003235 EVT VT = Op.getValueType();
3236 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003237 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3238 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3239 bool UseNEON = !InGPR && Subtarget->hasNEON();
3240
3241 if (UseNEON) {
3242 // Use VBSL to copy the sign bit.
3243 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3244 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3245 DAG.getTargetConstant(EncodedVal, MVT::i32));
3246 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3247 if (VT == MVT::f64)
3248 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3249 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3250 DAG.getConstant(32, MVT::i32));
3251 else /*if (VT == MVT::f32)*/
3252 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3253 if (SrcVT == MVT::f32) {
3254 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3255 if (VT == MVT::f64)
3256 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3257 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3258 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003259 } else if (VT == MVT::f32)
3260 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3261 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3262 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003263 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3264 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3265
3266 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3267 MVT::i32);
3268 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3269 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3270 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003271
Evan Chenge573fb32011-02-23 02:24:55 +00003272 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3273 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3274 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003275 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003276 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3277 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3278 DAG.getConstant(0, MVT::i32));
3279 } else {
3280 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3281 }
3282
3283 return Res;
3284 }
Evan Chengc143dd42011-02-11 02:28:55 +00003285
3286 // Bitcast operand 1 to i32.
3287 if (SrcVT == MVT::f64)
3288 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3289 &Tmp1, 1).getValue(1);
3290 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3291
Evan Chenge573fb32011-02-23 02:24:55 +00003292 // Or in the signbit with integer operations.
3293 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3294 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3295 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3296 if (VT == MVT::f32) {
3297 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3298 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3299 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3300 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003301 }
3302
Evan Chenge573fb32011-02-23 02:24:55 +00003303 // f64: Or the high part with signbit and then combine two parts.
3304 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3305 &Tmp0, 1);
3306 SDValue Lo = Tmp0.getValue(0);
3307 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3308 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3309 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003310}
3311
Evan Cheng2457f2c2010-05-22 01:47:14 +00003312SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3313 MachineFunction &MF = DAG.getMachineFunction();
3314 MachineFrameInfo *MFI = MF.getFrameInfo();
3315 MFI->setReturnAddressIsTaken(true);
3316
3317 EVT VT = Op.getValueType();
3318 DebugLoc dl = Op.getDebugLoc();
3319 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3320 if (Depth) {
3321 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3322 SDValue Offset = DAG.getConstant(4, MVT::i32);
3323 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3324 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003325 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003326 }
3327
3328 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003329 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003330 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3331}
3332
Dan Gohmand858e902010-04-17 15:26:15 +00003333SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003334 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3335 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003336
Owen Andersone50ed302009-08-10 22:56:29 +00003337 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003338 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3339 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003340 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003341 ? ARM::R7 : ARM::R11;
3342 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3343 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003344 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3345 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003346 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003347 return FrameAddr;
3348}
3349
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003350/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003351/// expand a bit convert where either the source or destination type is i64 to
3352/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3353/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3354/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003355static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003356 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3357 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003358 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003359
Bob Wilson9f3f0612010-04-17 05:30:19 +00003360 // This function is only supposed to be called for i64 types, either as the
3361 // source or destination of the bit convert.
3362 EVT SrcVT = Op.getValueType();
3363 EVT DstVT = N->getValueType(0);
3364 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003365 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003366
Bob Wilson9f3f0612010-04-17 05:30:19 +00003367 // Turn i64->f64 into VMOVDRR.
3368 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3370 DAG.getConstant(0, MVT::i32));
3371 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3372 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003373 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003374 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003375 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003376
Jim Grosbache5165492009-11-09 00:11:35 +00003377 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003378 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3379 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3380 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3381 // Merge the pieces into a single i64 value.
3382 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3383 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003384
Bob Wilson9f3f0612010-04-17 05:30:19 +00003385 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003386}
3387
Bob Wilson5bafff32009-06-22 23:27:02 +00003388/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003389/// Zero vectors are used to represent vector negation and in those cases
3390/// will be implemented with the NEON VNEG instruction. However, VNEG does
3391/// not support i64 elements, so sometimes the zero vectors will need to be
3392/// explicitly constructed. Regardless, use a canonical VMOV to create the
3393/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003394static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003395 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003396 // The canonical modified immediate encoding of a zero vector is....0!
3397 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3398 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3399 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003400 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003401}
3402
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003403/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3404/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003405SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3406 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003407 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3408 EVT VT = Op.getValueType();
3409 unsigned VTBits = VT.getSizeInBits();
3410 DebugLoc dl = Op.getDebugLoc();
3411 SDValue ShOpLo = Op.getOperand(0);
3412 SDValue ShOpHi = Op.getOperand(1);
3413 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003414 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003415 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003416
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003417 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3418
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003419 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3420 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3421 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3422 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3423 DAG.getConstant(VTBits, MVT::i32));
3424 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3425 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003426 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003427
3428 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3429 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003430 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003431 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003432 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003433 CCR, Cmp);
3434
3435 SDValue Ops[2] = { Lo, Hi };
3436 return DAG.getMergeValues(Ops, 2, dl);
3437}
3438
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003439/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3440/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003441SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3442 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003443 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3444 EVT VT = Op.getValueType();
3445 unsigned VTBits = VT.getSizeInBits();
3446 DebugLoc dl = Op.getDebugLoc();
3447 SDValue ShOpLo = Op.getOperand(0);
3448 SDValue ShOpHi = Op.getOperand(1);
3449 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003450 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003451
3452 assert(Op.getOpcode() == ISD::SHL_PARTS);
3453 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3454 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3455 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3456 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3457 DAG.getConstant(VTBits, MVT::i32));
3458 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3459 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3460
3461 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3462 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3463 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003464 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003465 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003466 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003467 CCR, Cmp);
3468
3469 SDValue Ops[2] = { Lo, Hi };
3470 return DAG.getMergeValues(Ops, 2, dl);
3471}
3472
Jim Grosbach4725ca72010-09-08 03:54:02 +00003473SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003474 SelectionDAG &DAG) const {
3475 // The rounding mode is in bits 23:22 of the FPSCR.
3476 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3477 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3478 // so that the shift + and get folded into a bitfield extract.
3479 DebugLoc dl = Op.getDebugLoc();
3480 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3481 DAG.getConstant(Intrinsic::arm_get_fpscr,
3482 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003483 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003484 DAG.getConstant(1U << 22, MVT::i32));
3485 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3486 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003487 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003488 DAG.getConstant(3, MVT::i32));
3489}
3490
Jim Grosbach3482c802010-01-18 19:58:49 +00003491static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3492 const ARMSubtarget *ST) {
3493 EVT VT = N->getValueType(0);
3494 DebugLoc dl = N->getDebugLoc();
3495
3496 if (!ST->hasV6T2Ops())
3497 return SDValue();
3498
3499 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3500 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3501}
3502
Bob Wilson5bafff32009-06-22 23:27:02 +00003503static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3504 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003505 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003506 DebugLoc dl = N->getDebugLoc();
3507
Bob Wilsond5448bb2010-11-18 21:16:28 +00003508 if (!VT.isVector())
3509 return SDValue();
3510
Bob Wilson5bafff32009-06-22 23:27:02 +00003511 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003512 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003513
Bob Wilsond5448bb2010-11-18 21:16:28 +00003514 // Left shifts translate directly to the vshiftu intrinsic.
3515 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003516 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003517 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3518 N->getOperand(0), N->getOperand(1));
3519
3520 assert((N->getOpcode() == ISD::SRA ||
3521 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3522
3523 // NEON uses the same intrinsics for both left and right shifts. For
3524 // right shifts, the shift amounts are negative, so negate the vector of
3525 // shift amounts.
3526 EVT ShiftVT = N->getOperand(1).getValueType();
3527 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3528 getZeroVector(ShiftVT, DAG, dl),
3529 N->getOperand(1));
3530 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3531 Intrinsic::arm_neon_vshifts :
3532 Intrinsic::arm_neon_vshiftu);
3533 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3534 DAG.getConstant(vshiftInt, MVT::i32),
3535 N->getOperand(0), NegatedCount);
3536}
3537
3538static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3539 const ARMSubtarget *ST) {
3540 EVT VT = N->getValueType(0);
3541 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003542
Eli Friedmance392eb2009-08-22 03:13:10 +00003543 // We can get here for a node like i32 = ISD::SHL i32, i64
3544 if (VT != MVT::i64)
3545 return SDValue();
3546
3547 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003548 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003549
Chris Lattner27a6c732007-11-24 07:07:01 +00003550 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3551 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003552 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003553 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003554
Chris Lattner27a6c732007-11-24 07:07:01 +00003555 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003556 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003557
Chris Lattner27a6c732007-11-24 07:07:01 +00003558 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003560 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003562 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003563
Chris Lattner27a6c732007-11-24 07:07:01 +00003564 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3565 // captures the result into a carry flag.
3566 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003567 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003568
Chris Lattner27a6c732007-11-24 07:07:01 +00003569 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003571
Chris Lattner27a6c732007-11-24 07:07:01 +00003572 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003574}
3575
Bob Wilson5bafff32009-06-22 23:27:02 +00003576static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3577 SDValue TmpOp0, TmpOp1;
3578 bool Invert = false;
3579 bool Swap = false;
3580 unsigned Opc = 0;
3581
3582 SDValue Op0 = Op.getOperand(0);
3583 SDValue Op1 = Op.getOperand(1);
3584 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003585 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003586 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3587 DebugLoc dl = Op.getDebugLoc();
3588
3589 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3590 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003591 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003592 case ISD::SETUNE:
3593 case ISD::SETNE: Invert = true; // Fallthrough
3594 case ISD::SETOEQ:
3595 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3596 case ISD::SETOLT:
3597 case ISD::SETLT: Swap = true; // Fallthrough
3598 case ISD::SETOGT:
3599 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3600 case ISD::SETOLE:
3601 case ISD::SETLE: Swap = true; // Fallthrough
3602 case ISD::SETOGE:
3603 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3604 case ISD::SETUGE: Swap = true; // Fallthrough
3605 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3606 case ISD::SETUGT: Swap = true; // Fallthrough
3607 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3608 case ISD::SETUEQ: Invert = true; // Fallthrough
3609 case ISD::SETONE:
3610 // Expand this to (OLT | OGT).
3611 TmpOp0 = Op0;
3612 TmpOp1 = Op1;
3613 Opc = ISD::OR;
3614 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3615 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3616 break;
3617 case ISD::SETUO: Invert = true; // Fallthrough
3618 case ISD::SETO:
3619 // Expand this to (OLT | OGE).
3620 TmpOp0 = Op0;
3621 TmpOp1 = Op1;
3622 Opc = ISD::OR;
3623 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3624 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3625 break;
3626 }
3627 } else {
3628 // Integer comparisons.
3629 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003630 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 case ISD::SETNE: Invert = true;
3632 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3633 case ISD::SETLT: Swap = true;
3634 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3635 case ISD::SETLE: Swap = true;
3636 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3637 case ISD::SETULT: Swap = true;
3638 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3639 case ISD::SETULE: Swap = true;
3640 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3641 }
3642
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003643 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003644 if (Opc == ARMISD::VCEQ) {
3645
3646 SDValue AndOp;
3647 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3648 AndOp = Op0;
3649 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3650 AndOp = Op1;
3651
3652 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003653 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003654 AndOp = AndOp.getOperand(0);
3655
3656 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3657 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003658 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3659 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003660 Invert = !Invert;
3661 }
3662 }
3663 }
3664
3665 if (Swap)
3666 std::swap(Op0, Op1);
3667
Owen Andersonc24cb352010-11-08 23:21:22 +00003668 // If one of the operands is a constant vector zero, attempt to fold the
3669 // comparison to a specialized compare-against-zero form.
3670 SDValue SingleOp;
3671 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3672 SingleOp = Op0;
3673 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3674 if (Opc == ARMISD::VCGE)
3675 Opc = ARMISD::VCLEZ;
3676 else if (Opc == ARMISD::VCGT)
3677 Opc = ARMISD::VCLTZ;
3678 SingleOp = Op1;
3679 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003680
Owen Andersonc24cb352010-11-08 23:21:22 +00003681 SDValue Result;
3682 if (SingleOp.getNode()) {
3683 switch (Opc) {
3684 case ARMISD::VCEQ:
3685 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3686 case ARMISD::VCGE:
3687 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3688 case ARMISD::VCLEZ:
3689 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3690 case ARMISD::VCGT:
3691 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3692 case ARMISD::VCLTZ:
3693 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3694 default:
3695 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3696 }
3697 } else {
3698 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3699 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003700
3701 if (Invert)
3702 Result = DAG.getNOT(dl, Result, VT);
3703
3704 return Result;
3705}
3706
Bob Wilsond3c42842010-06-14 22:19:57 +00003707/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3708/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003709/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003710static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3711 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003712 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003713 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003714
Bob Wilson827b2102010-06-15 19:05:35 +00003715 // SplatBitSize is set to the smallest size that splats the vector, so a
3716 // zero vector will always have SplatBitSize == 8. However, NEON modified
3717 // immediate instructions others than VMOV do not support the 8-bit encoding
3718 // of a zero vector, and the default encoding of zero is supposed to be the
3719 // 32-bit version.
3720 if (SplatBits == 0)
3721 SplatBitSize = 32;
3722
Bob Wilson5bafff32009-06-22 23:27:02 +00003723 switch (SplatBitSize) {
3724 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003725 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003726 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003727 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003728 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003729 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003730 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003731 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003732 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003733
3734 case 16:
3735 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003736 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003737 if ((SplatBits & ~0xff) == 0) {
3738 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003739 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003740 Imm = SplatBits;
3741 break;
3742 }
3743 if ((SplatBits & ~0xff00) == 0) {
3744 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003745 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003746 Imm = SplatBits >> 8;
3747 break;
3748 }
3749 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003750
3751 case 32:
3752 // NEON's 32-bit VMOV supports splat values where:
3753 // * only one byte is nonzero, or
3754 // * the least significant byte is 0xff and the second byte is nonzero, or
3755 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003756 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003757 if ((SplatBits & ~0xff) == 0) {
3758 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003759 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003760 Imm = SplatBits;
3761 break;
3762 }
3763 if ((SplatBits & ~0xff00) == 0) {
3764 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003765 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003766 Imm = SplatBits >> 8;
3767 break;
3768 }
3769 if ((SplatBits & ~0xff0000) == 0) {
3770 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003771 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003772 Imm = SplatBits >> 16;
3773 break;
3774 }
3775 if ((SplatBits & ~0xff000000) == 0) {
3776 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003777 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003778 Imm = SplatBits >> 24;
3779 break;
3780 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003781
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003782 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3783 if (type == OtherModImm) return SDValue();
3784
Bob Wilson5bafff32009-06-22 23:27:02 +00003785 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003786 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3787 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003788 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003789 Imm = SplatBits >> 8;
3790 SplatBits |= 0xff;
3791 break;
3792 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003793
3794 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003795 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3796 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003797 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003798 Imm = SplatBits >> 16;
3799 SplatBits |= 0xffff;
3800 break;
3801 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003802
3803 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3804 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3805 // VMOV.I32. A (very) minor optimization would be to replicate the value
3806 // and fall through here to test for a valid 64-bit splat. But, then the
3807 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003808 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003809
3810 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003811 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003812 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003813 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003814 uint64_t BitMask = 0xff;
3815 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003816 unsigned ImmMask = 1;
3817 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003818 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003819 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003820 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003821 Imm |= ImmMask;
3822 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003823 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003824 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003825 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003826 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003827 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003828 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003829 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003830 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003831 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003832 break;
3833 }
3834
Bob Wilson1a913ed2010-06-11 21:34:50 +00003835 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003836 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003837 }
3838
Bob Wilsoncba270d2010-07-13 21:16:48 +00003839 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3840 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003841}
3842
Lang Hamesc0a9f822012-03-29 21:56:11 +00003843SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3844 const ARMSubtarget *ST) const {
3845 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3846 return SDValue();
3847
3848 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3849 assert(Op.getValueType() == MVT::f32 &&
3850 "ConstantFP custom lowering should only occur for f32.");
3851
3852 // Try splatting with a VMOV.f32...
3853 APFloat FPVal = CFP->getValueAPF();
3854 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3855 if (ImmVal != -1) {
3856 DebugLoc DL = Op.getDebugLoc();
3857 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3858 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3859 NewVal);
3860 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3861 DAG.getConstant(0, MVT::i32));
3862 }
3863
3864 // If that fails, try a VMOV.i32
3865 EVT VMovVT;
3866 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3867 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3868 VMOVModImm);
3869 if (NewVal != SDValue()) {
3870 DebugLoc DL = Op.getDebugLoc();
3871 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3872 NewVal);
3873 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3874 VecConstant);
3875 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3876 DAG.getConstant(0, MVT::i32));
3877 }
3878
3879 // Finally, try a VMVN.i32
3880 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3881 VMVNModImm);
3882 if (NewVal != SDValue()) {
3883 DebugLoc DL = Op.getDebugLoc();
3884 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3885 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3886 VecConstant);
3887 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3888 DAG.getConstant(0, MVT::i32));
3889 }
3890
3891 return SDValue();
3892}
3893
3894
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003895static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003896 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003897 unsigned NumElts = VT.getVectorNumElements();
3898 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003899
3900 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3901 if (M[0] < 0)
3902 return false;
3903
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003904 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003905
3906 // If this is a VEXT shuffle, the immediate value is the index of the first
3907 // element. The other shuffle indices must be the successive elements after
3908 // the first one.
3909 unsigned ExpectedElt = Imm;
3910 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003911 // Increment the expected index. If it wraps around, it may still be
3912 // a VEXT but the source vectors must be swapped.
3913 ExpectedElt += 1;
3914 if (ExpectedElt == NumElts * 2) {
3915 ExpectedElt = 0;
3916 ReverseVEXT = true;
3917 }
3918
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003919 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003920 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003921 return false;
3922 }
3923
3924 // Adjust the index value if the source operands will be swapped.
3925 if (ReverseVEXT)
3926 Imm -= NumElts;
3927
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003928 return true;
3929}
3930
Bob Wilson8bb9e482009-07-26 00:39:34 +00003931/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3932/// instruction with the specified blocksize. (The order of the elements
3933/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003934static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003935 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3936 "Only possible block sizes for VREV are: 16, 32, 64");
3937
Bob Wilson8bb9e482009-07-26 00:39:34 +00003938 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003939 if (EltSz == 64)
3940 return false;
3941
3942 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003943 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003944 // If the first shuffle index is UNDEF, be optimistic.
3945 if (M[0] < 0)
3946 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003947
3948 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3949 return false;
3950
3951 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003952 if (M[i] < 0) continue; // ignore UNDEF indices
3953 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003954 return false;
3955 }
3956
3957 return true;
3958}
3959
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003960static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003961 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3962 // range, then 0 is placed into the resulting vector. So pretty much any mask
3963 // of 8 elements can work here.
3964 return VT == MVT::v8i8 && M.size() == 8;
3965}
3966
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003967static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003968 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3969 if (EltSz == 64)
3970 return false;
3971
Bob Wilsonc692cb72009-08-21 20:54:19 +00003972 unsigned NumElts = VT.getVectorNumElements();
3973 WhichResult = (M[0] == 0 ? 0 : 1);
3974 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003975 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3976 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003977 return false;
3978 }
3979 return true;
3980}
3981
Bob Wilson324f4f12009-12-03 06:40:55 +00003982/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3983/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3984/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003985static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003986 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3987 if (EltSz == 64)
3988 return false;
3989
3990 unsigned NumElts = VT.getVectorNumElements();
3991 WhichResult = (M[0] == 0 ? 0 : 1);
3992 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003993 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3994 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003995 return false;
3996 }
3997 return true;
3998}
3999
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004000static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004001 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4002 if (EltSz == 64)
4003 return false;
4004
Bob Wilsonc692cb72009-08-21 20:54:19 +00004005 unsigned NumElts = VT.getVectorNumElements();
4006 WhichResult = (M[0] == 0 ? 0 : 1);
4007 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004008 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004009 if ((unsigned) M[i] != 2 * i + WhichResult)
4010 return false;
4011 }
4012
4013 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004014 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004015 return false;
4016
4017 return true;
4018}
4019
Bob Wilson324f4f12009-12-03 06:40:55 +00004020/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4021/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4022/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004023static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004024 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4025 if (EltSz == 64)
4026 return false;
4027
4028 unsigned Half = VT.getVectorNumElements() / 2;
4029 WhichResult = (M[0] == 0 ? 0 : 1);
4030 for (unsigned j = 0; j != 2; ++j) {
4031 unsigned Idx = WhichResult;
4032 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004033 int MIdx = M[i + j * Half];
4034 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004035 return false;
4036 Idx += 2;
4037 }
4038 }
4039
4040 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4041 if (VT.is64BitVector() && EltSz == 32)
4042 return false;
4043
4044 return true;
4045}
4046
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004047static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004048 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4049 if (EltSz == 64)
4050 return false;
4051
Bob Wilsonc692cb72009-08-21 20:54:19 +00004052 unsigned NumElts = VT.getVectorNumElements();
4053 WhichResult = (M[0] == 0 ? 0 : 1);
4054 unsigned Idx = WhichResult * NumElts / 2;
4055 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004056 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4057 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004058 return false;
4059 Idx += 1;
4060 }
4061
4062 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004063 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004064 return false;
4065
4066 return true;
4067}
4068
Bob Wilson324f4f12009-12-03 06:40:55 +00004069/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4070/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4071/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004072static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004073 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4074 if (EltSz == 64)
4075 return false;
4076
4077 unsigned NumElts = VT.getVectorNumElements();
4078 WhichResult = (M[0] == 0 ? 0 : 1);
4079 unsigned Idx = WhichResult * NumElts / 2;
4080 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004081 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4082 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004083 return false;
4084 Idx += 1;
4085 }
4086
4087 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4088 if (VT.is64BitVector() && EltSz == 32)
4089 return false;
4090
4091 return true;
4092}
4093
Dale Johannesenf630c712010-07-29 20:10:08 +00004094// If N is an integer constant that can be moved into a register in one
4095// instruction, return an SDValue of such a constant (will become a MOV
4096// instruction). Otherwise return null.
4097static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4098 const ARMSubtarget *ST, DebugLoc dl) {
4099 uint64_t Val;
4100 if (!isa<ConstantSDNode>(N))
4101 return SDValue();
4102 Val = cast<ConstantSDNode>(N)->getZExtValue();
4103
4104 if (ST->isThumb1Only()) {
4105 if (Val <= 255 || ~Val <= 255)
4106 return DAG.getConstant(Val, MVT::i32);
4107 } else {
4108 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4109 return DAG.getConstant(Val, MVT::i32);
4110 }
4111 return SDValue();
4112}
4113
Bob Wilson5bafff32009-06-22 23:27:02 +00004114// If this is a case we can't handle, return null and let the default
4115// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004116SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4117 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004118 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004119 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004120 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004121
4122 APInt SplatBits, SplatUndef;
4123 unsigned SplatBitSize;
4124 bool HasAnyUndefs;
4125 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004126 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004127 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004128 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004129 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004130 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004131 DAG, VmovVT, VT.is128BitVector(),
4132 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004133 if (Val.getNode()) {
4134 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004135 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004136 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004137
4138 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004139 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004140 Val = isNEONModifiedImm(NegatedImm,
4141 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004142 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004143 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004144 if (Val.getNode()) {
4145 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004146 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004147 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004148
4149 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004150 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004151 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004152 if (ImmVal != -1) {
4153 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4154 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4155 }
4156 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004157 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004158 }
4159
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004160 // Scan through the operands to see if only one value is used.
4161 unsigned NumElts = VT.getVectorNumElements();
4162 bool isOnlyLowElement = true;
4163 bool usesOnlyOneValue = true;
4164 bool isConstant = true;
4165 SDValue Value;
4166 for (unsigned i = 0; i < NumElts; ++i) {
4167 SDValue V = Op.getOperand(i);
4168 if (V.getOpcode() == ISD::UNDEF)
4169 continue;
4170 if (i > 0)
4171 isOnlyLowElement = false;
4172 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4173 isConstant = false;
4174
4175 if (!Value.getNode())
4176 Value = V;
4177 else if (V != Value)
4178 usesOnlyOneValue = false;
4179 }
4180
4181 if (!Value.getNode())
4182 return DAG.getUNDEF(VT);
4183
4184 if (isOnlyLowElement)
4185 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4186
Dale Johannesenf630c712010-07-29 20:10:08 +00004187 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4188
Dale Johannesen575cd142010-10-19 20:00:17 +00004189 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4190 // i32 and try again.
4191 if (usesOnlyOneValue && EltSize <= 32) {
4192 if (!isConstant)
4193 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4194 if (VT.getVectorElementType().isFloatingPoint()) {
4195 SmallVector<SDValue, 8> Ops;
4196 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004197 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004198 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004199 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4200 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004201 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4202 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004203 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004204 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004205 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4206 if (Val.getNode())
4207 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004208 }
4209
4210 // If all elements are constants and the case above didn't get hit, fall back
4211 // to the default expansion, which will generate a load from the constant
4212 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004213 if (isConstant)
4214 return SDValue();
4215
Bob Wilson11a1dff2011-01-07 21:37:30 +00004216 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4217 if (NumElts >= 4) {
4218 SDValue shuffle = ReconstructShuffle(Op, DAG);
4219 if (shuffle != SDValue())
4220 return shuffle;
4221 }
4222
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004223 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004224 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4225 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004226 if (EltSize >= 32) {
4227 // Do the expansion with floating-point types, since that is what the VFP
4228 // registers are defined to use, and since i64 is not legal.
4229 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4230 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004231 SmallVector<SDValue, 8> Ops;
4232 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004233 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004234 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004235 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004236 }
4237
4238 return SDValue();
4239}
4240
Bob Wilson11a1dff2011-01-07 21:37:30 +00004241// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004242// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004243SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4244 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004245 DebugLoc dl = Op.getDebugLoc();
4246 EVT VT = Op.getValueType();
4247 unsigned NumElts = VT.getVectorNumElements();
4248
4249 SmallVector<SDValue, 2> SourceVecs;
4250 SmallVector<unsigned, 2> MinElts;
4251 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004252
Bob Wilson11a1dff2011-01-07 21:37:30 +00004253 for (unsigned i = 0; i < NumElts; ++i) {
4254 SDValue V = Op.getOperand(i);
4255 if (V.getOpcode() == ISD::UNDEF)
4256 continue;
4257 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4258 // A shuffle can only come from building a vector from various
4259 // elements of other vectors.
4260 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004261 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4262 VT.getVectorElementType()) {
4263 // This code doesn't know how to handle shuffles where the vector
4264 // element types do not match (this happens because type legalization
4265 // promotes the return type of EXTRACT_VECTOR_ELT).
4266 // FIXME: It might be appropriate to extend this code to handle
4267 // mismatched types.
4268 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004269 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004270
Bob Wilson11a1dff2011-01-07 21:37:30 +00004271 // Record this extraction against the appropriate vector if possible...
4272 SDValue SourceVec = V.getOperand(0);
4273 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4274 bool FoundSource = false;
4275 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4276 if (SourceVecs[j] == SourceVec) {
4277 if (MinElts[j] > EltNo)
4278 MinElts[j] = EltNo;
4279 if (MaxElts[j] < EltNo)
4280 MaxElts[j] = EltNo;
4281 FoundSource = true;
4282 break;
4283 }
4284 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004285
Bob Wilson11a1dff2011-01-07 21:37:30 +00004286 // Or record a new source if not...
4287 if (!FoundSource) {
4288 SourceVecs.push_back(SourceVec);
4289 MinElts.push_back(EltNo);
4290 MaxElts.push_back(EltNo);
4291 }
4292 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004293
Bob Wilson11a1dff2011-01-07 21:37:30 +00004294 // Currently only do something sane when at most two source vectors
4295 // involved.
4296 if (SourceVecs.size() > 2)
4297 return SDValue();
4298
4299 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4300 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004301
Bob Wilson11a1dff2011-01-07 21:37:30 +00004302 // This loop extracts the usage patterns of the source vectors
4303 // and prepares appropriate SDValues for a shuffle if possible.
4304 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4305 if (SourceVecs[i].getValueType() == VT) {
4306 // No VEXT necessary
4307 ShuffleSrcs[i] = SourceVecs[i];
4308 VEXTOffsets[i] = 0;
4309 continue;
4310 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4311 // It probably isn't worth padding out a smaller vector just to
4312 // break it down again in a shuffle.
4313 return SDValue();
4314 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004315
Bob Wilson11a1dff2011-01-07 21:37:30 +00004316 // Since only 64-bit and 128-bit vectors are legal on ARM and
4317 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004318 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4319 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004320
Bob Wilson11a1dff2011-01-07 21:37:30 +00004321 if (MaxElts[i] - MinElts[i] >= NumElts) {
4322 // Span too large for a VEXT to cope
4323 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004324 }
4325
Bob Wilson11a1dff2011-01-07 21:37:30 +00004326 if (MinElts[i] >= NumElts) {
4327 // The extraction can just take the second half
4328 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004329 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4330 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004331 DAG.getIntPtrConstant(NumElts));
4332 } else if (MaxElts[i] < NumElts) {
4333 // The extraction can just take the first half
4334 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004335 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4336 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004337 DAG.getIntPtrConstant(0));
4338 } else {
4339 // An actual VEXT is needed
4340 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004341 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4342 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004343 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004344 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4345 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004346 DAG.getIntPtrConstant(NumElts));
4347 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4348 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4349 }
4350 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004351
Bob Wilson11a1dff2011-01-07 21:37:30 +00004352 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004353
Bob Wilson11a1dff2011-01-07 21:37:30 +00004354 for (unsigned i = 0; i < NumElts; ++i) {
4355 SDValue Entry = Op.getOperand(i);
4356 if (Entry.getOpcode() == ISD::UNDEF) {
4357 Mask.push_back(-1);
4358 continue;
4359 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004360
Bob Wilson11a1dff2011-01-07 21:37:30 +00004361 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004362 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4363 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004364 if (ExtractVec == SourceVecs[0]) {
4365 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4366 } else {
4367 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4368 }
4369 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004370
Bob Wilson11a1dff2011-01-07 21:37:30 +00004371 // Final check before we try to produce nonsense...
4372 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004373 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4374 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004375
Bob Wilson11a1dff2011-01-07 21:37:30 +00004376 return SDValue();
4377}
4378
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004379/// isShuffleMaskLegal - Targets can use this to indicate that they only
4380/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4381/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4382/// are assumed to be legal.
4383bool
4384ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4385 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004386 if (VT.getVectorNumElements() == 4 &&
4387 (VT.is128BitVector() || VT.is64BitVector())) {
4388 unsigned PFIndexes[4];
4389 for (unsigned i = 0; i != 4; ++i) {
4390 if (M[i] < 0)
4391 PFIndexes[i] = 8;
4392 else
4393 PFIndexes[i] = M[i];
4394 }
4395
4396 // Compute the index in the perfect shuffle table.
4397 unsigned PFTableIndex =
4398 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4399 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4400 unsigned Cost = (PFEntry >> 30);
4401
4402 if (Cost <= 4)
4403 return true;
4404 }
4405
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004406 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004407 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004408
Bob Wilson53dd2452010-06-07 23:53:38 +00004409 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4410 return (EltSize >= 32 ||
4411 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004412 isVREVMask(M, VT, 64) ||
4413 isVREVMask(M, VT, 32) ||
4414 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004415 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004416 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004417 isVTRNMask(M, VT, WhichResult) ||
4418 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004419 isVZIPMask(M, VT, WhichResult) ||
4420 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4421 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4422 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004423}
4424
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004425/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4426/// the specified operations to build the shuffle.
4427static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4428 SDValue RHS, SelectionDAG &DAG,
4429 DebugLoc dl) {
4430 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4431 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4432 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4433
4434 enum {
4435 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4436 OP_VREV,
4437 OP_VDUP0,
4438 OP_VDUP1,
4439 OP_VDUP2,
4440 OP_VDUP3,
4441 OP_VEXT1,
4442 OP_VEXT2,
4443 OP_VEXT3,
4444 OP_VUZPL, // VUZP, left result
4445 OP_VUZPR, // VUZP, right result
4446 OP_VZIPL, // VZIP, left result
4447 OP_VZIPR, // VZIP, right result
4448 OP_VTRNL, // VTRN, left result
4449 OP_VTRNR // VTRN, right result
4450 };
4451
4452 if (OpNum == OP_COPY) {
4453 if (LHSID == (1*9+2)*9+3) return LHS;
4454 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4455 return RHS;
4456 }
4457
4458 SDValue OpLHS, OpRHS;
4459 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4460 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4461 EVT VT = OpLHS.getValueType();
4462
4463 switch (OpNum) {
4464 default: llvm_unreachable("Unknown shuffle opcode!");
4465 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004466 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004467 if (VT.getVectorElementType() == MVT::i32 ||
4468 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004469 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4470 // vrev <4 x i16> -> VREV32
4471 if (VT.getVectorElementType() == MVT::i16)
4472 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4473 // vrev <4 x i8> -> VREV16
4474 assert(VT.getVectorElementType() == MVT::i8);
4475 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004476 case OP_VDUP0:
4477 case OP_VDUP1:
4478 case OP_VDUP2:
4479 case OP_VDUP3:
4480 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004481 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004482 case OP_VEXT1:
4483 case OP_VEXT2:
4484 case OP_VEXT3:
4485 return DAG.getNode(ARMISD::VEXT, dl, VT,
4486 OpLHS, OpRHS,
4487 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4488 case OP_VUZPL:
4489 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004490 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004491 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4492 case OP_VZIPL:
4493 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004494 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004495 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4496 case OP_VTRNL:
4497 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004498 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4499 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004500 }
4501}
4502
Bill Wendling69a05a72011-03-14 23:02:38 +00004503static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004504 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004505 SelectionDAG &DAG) {
4506 // Check to see if we can use the VTBL instruction.
4507 SDValue V1 = Op.getOperand(0);
4508 SDValue V2 = Op.getOperand(1);
4509 DebugLoc DL = Op.getDebugLoc();
4510
4511 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004512 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004513 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4514 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4515
4516 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4517 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4518 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4519 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004520
Owen Anderson76706012011-04-05 21:48:57 +00004521 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004522 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4523 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004524}
4525
Bob Wilson5bafff32009-06-22 23:27:02 +00004526static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004527 SDValue V1 = Op.getOperand(0);
4528 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004529 DebugLoc dl = Op.getDebugLoc();
4530 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004531 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004532
Bob Wilson28865062009-08-13 02:13:04 +00004533 // Convert shuffles that are directly supported on NEON to target-specific
4534 // DAG nodes, instead of keeping them as shuffles and matching them again
4535 // during code selection. This is more efficient and avoids the possibility
4536 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004537 // FIXME: floating-point vectors should be canonicalized to integer vectors
4538 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004539 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004540
Bob Wilson53dd2452010-06-07 23:53:38 +00004541 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4542 if (EltSize <= 32) {
4543 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4544 int Lane = SVN->getSplatIndex();
4545 // If this is undef splat, generate it via "just" vdup, if possible.
4546 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004547
Dan Gohman65fd6562011-11-03 21:49:52 +00004548 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004549 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4550 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4551 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004552 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4553 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4554 // reaches it).
4555 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4556 !isa<ConstantSDNode>(V1.getOperand(0))) {
4557 bool IsScalarToVector = true;
4558 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4559 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4560 IsScalarToVector = false;
4561 break;
4562 }
4563 if (IsScalarToVector)
4564 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4565 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004566 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4567 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004568 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004569
4570 bool ReverseVEXT;
4571 unsigned Imm;
4572 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4573 if (ReverseVEXT)
4574 std::swap(V1, V2);
4575 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4576 DAG.getConstant(Imm, MVT::i32));
4577 }
4578
4579 if (isVREVMask(ShuffleMask, VT, 64))
4580 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4581 if (isVREVMask(ShuffleMask, VT, 32))
4582 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4583 if (isVREVMask(ShuffleMask, VT, 16))
4584 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4585
4586 // Check for Neon shuffles that modify both input vectors in place.
4587 // If both results are used, i.e., if there are two shuffles with the same
4588 // source operands and with masks corresponding to both results of one of
4589 // these operations, DAG memoization will ensure that a single node is
4590 // used for both shuffles.
4591 unsigned WhichResult;
4592 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4593 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4594 V1, V2).getValue(WhichResult);
4595 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4596 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4597 V1, V2).getValue(WhichResult);
4598 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4599 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4600 V1, V2).getValue(WhichResult);
4601
4602 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4603 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4604 V1, V1).getValue(WhichResult);
4605 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4606 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4607 V1, V1).getValue(WhichResult);
4608 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4609 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4610 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004611 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004612
Bob Wilsonc692cb72009-08-21 20:54:19 +00004613 // If the shuffle is not directly supported and it has 4 elements, use
4614 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004615 unsigned NumElts = VT.getVectorNumElements();
4616 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004617 unsigned PFIndexes[4];
4618 for (unsigned i = 0; i != 4; ++i) {
4619 if (ShuffleMask[i] < 0)
4620 PFIndexes[i] = 8;
4621 else
4622 PFIndexes[i] = ShuffleMask[i];
4623 }
4624
4625 // Compute the index in the perfect shuffle table.
4626 unsigned PFTableIndex =
4627 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004628 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4629 unsigned Cost = (PFEntry >> 30);
4630
4631 if (Cost <= 4)
4632 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4633 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004634
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004635 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004636 if (EltSize >= 32) {
4637 // Do the expansion with floating-point types, since that is what the VFP
4638 // registers are defined to use, and since i64 is not legal.
4639 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4640 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004641 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4642 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004643 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004644 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004645 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004646 Ops.push_back(DAG.getUNDEF(EltVT));
4647 else
4648 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4649 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4650 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4651 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004652 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004653 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004655 }
4656
Bill Wendling69a05a72011-03-14 23:02:38 +00004657 if (VT == MVT::v8i8) {
4658 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4659 if (NewOp.getNode())
4660 return NewOp;
4661 }
4662
Bob Wilson22cac0d2009-08-14 05:16:33 +00004663 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004664}
4665
Eli Friedman5c89cb82011-10-24 23:08:52 +00004666static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4667 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4668 SDValue Lane = Op.getOperand(2);
4669 if (!isa<ConstantSDNode>(Lane))
4670 return SDValue();
4671
4672 return Op;
4673}
4674
Bob Wilson5bafff32009-06-22 23:27:02 +00004675static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004676 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004677 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004678 if (!isa<ConstantSDNode>(Lane))
4679 return SDValue();
4680
4681 SDValue Vec = Op.getOperand(0);
4682 if (Op.getValueType() == MVT::i32 &&
4683 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4684 DebugLoc dl = Op.getDebugLoc();
4685 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4686 }
4687
4688 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004689}
4690
Bob Wilsona6d65862009-08-03 20:36:38 +00004691static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4692 // The only time a CONCAT_VECTORS operation can have legal types is when
4693 // two 64-bit vectors are concatenated to a 128-bit vector.
4694 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4695 "unexpected CONCAT_VECTORS");
4696 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004697 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004698 SDValue Op0 = Op.getOperand(0);
4699 SDValue Op1 = Op.getOperand(1);
4700 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004701 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004702 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004703 DAG.getIntPtrConstant(0));
4704 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004705 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004706 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004707 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004708 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004709}
4710
Bob Wilson626613d2010-11-23 19:38:38 +00004711/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4712/// element has been zero/sign-extended, depending on the isSigned parameter,
4713/// from an integer type half its size.
4714static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4715 bool isSigned) {
4716 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4717 EVT VT = N->getValueType(0);
4718 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4719 SDNode *BVN = N->getOperand(0).getNode();
4720 if (BVN->getValueType(0) != MVT::v4i32 ||
4721 BVN->getOpcode() != ISD::BUILD_VECTOR)
4722 return false;
4723 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4724 unsigned HiElt = 1 - LoElt;
4725 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4726 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4727 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4728 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4729 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4730 return false;
4731 if (isSigned) {
4732 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4733 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4734 return true;
4735 } else {
4736 if (Hi0->isNullValue() && Hi1->isNullValue())
4737 return true;
4738 }
4739 return false;
4740 }
4741
4742 if (N->getOpcode() != ISD::BUILD_VECTOR)
4743 return false;
4744
4745 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4746 SDNode *Elt = N->getOperand(i).getNode();
4747 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4748 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4749 unsigned HalfSize = EltSize / 2;
4750 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004751 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004752 return false;
4753 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004754 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004755 return false;
4756 }
4757 continue;
4758 }
4759 return false;
4760 }
4761
4762 return true;
4763}
4764
4765/// isSignExtended - Check if a node is a vector value that is sign-extended
4766/// or a constant BUILD_VECTOR with sign-extended elements.
4767static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4768 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4769 return true;
4770 if (isExtendedBUILD_VECTOR(N, DAG, true))
4771 return true;
4772 return false;
4773}
4774
4775/// isZeroExtended - Check if a node is a vector value that is zero-extended
4776/// or a constant BUILD_VECTOR with zero-extended elements.
4777static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4778 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4779 return true;
4780 if (isExtendedBUILD_VECTOR(N, DAG, false))
4781 return true;
4782 return false;
4783}
4784
4785/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4786/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004787static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4788 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4789 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004790 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4791 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4792 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004793 LD->isNonTemporal(), LD->isInvariant(),
4794 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004795 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4796 // have been legalized as a BITCAST from v4i32.
4797 if (N->getOpcode() == ISD::BITCAST) {
4798 SDNode *BVN = N->getOperand(0).getNode();
4799 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4800 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4801 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4802 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4803 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4804 }
4805 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4806 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4807 EVT VT = N->getValueType(0);
4808 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4809 unsigned NumElts = VT.getVectorNumElements();
4810 MVT TruncVT = MVT::getIntegerVT(EltSize);
4811 SmallVector<SDValue, 8> Ops;
4812 for (unsigned i = 0; i != NumElts; ++i) {
4813 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4814 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004815 // Element types smaller than 32 bits are not legal, so use i32 elements.
4816 // The values are implicitly truncated so sext vs. zext doesn't matter.
4817 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004818 }
4819 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4820 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004821}
4822
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004823static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4824 unsigned Opcode = N->getOpcode();
4825 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4826 SDNode *N0 = N->getOperand(0).getNode();
4827 SDNode *N1 = N->getOperand(1).getNode();
4828 return N0->hasOneUse() && N1->hasOneUse() &&
4829 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4830 }
4831 return false;
4832}
4833
4834static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4835 unsigned Opcode = N->getOpcode();
4836 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4837 SDNode *N0 = N->getOperand(0).getNode();
4838 SDNode *N1 = N->getOperand(1).getNode();
4839 return N0->hasOneUse() && N1->hasOneUse() &&
4840 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4841 }
4842 return false;
4843}
4844
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004845static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4846 // Multiplications are only custom-lowered for 128-bit vectors so that
4847 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4848 EVT VT = Op.getValueType();
4849 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4850 SDNode *N0 = Op.getOperand(0).getNode();
4851 SDNode *N1 = Op.getOperand(1).getNode();
4852 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004853 bool isMLA = false;
4854 bool isN0SExt = isSignExtended(N0, DAG);
4855 bool isN1SExt = isSignExtended(N1, DAG);
4856 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004857 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004858 else {
4859 bool isN0ZExt = isZeroExtended(N0, DAG);
4860 bool isN1ZExt = isZeroExtended(N1, DAG);
4861 if (isN0ZExt && isN1ZExt)
4862 NewOpc = ARMISD::VMULLu;
4863 else if (isN1SExt || isN1ZExt) {
4864 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4865 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4866 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4867 NewOpc = ARMISD::VMULLs;
4868 isMLA = true;
4869 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4870 NewOpc = ARMISD::VMULLu;
4871 isMLA = true;
4872 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4873 std::swap(N0, N1);
4874 NewOpc = ARMISD::VMULLu;
4875 isMLA = true;
4876 }
4877 }
4878
4879 if (!NewOpc) {
4880 if (VT == MVT::v2i64)
4881 // Fall through to expand this. It is not legal.
4882 return SDValue();
4883 else
4884 // Other vector multiplications are legal.
4885 return Op;
4886 }
4887 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004888
4889 // Legalize to a VMULL instruction.
4890 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004891 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004892 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004893 if (!isMLA) {
4894 Op0 = SkipExtension(N0, DAG);
4895 assert(Op0.getValueType().is64BitVector() &&
4896 Op1.getValueType().is64BitVector() &&
4897 "unexpected types for extended operands to VMULL");
4898 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4899 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004900
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004901 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4902 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4903 // vmull q0, d4, d6
4904 // vmlal q0, d5, d6
4905 // is faster than
4906 // vaddl q0, d4, d5
4907 // vmovl q1, d6
4908 // vmul q0, q0, q1
4909 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4910 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4911 EVT Op1VT = Op1.getValueType();
4912 return DAG.getNode(N0->getOpcode(), DL, VT,
4913 DAG.getNode(NewOpc, DL, VT,
4914 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4915 DAG.getNode(NewOpc, DL, VT,
4916 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004917}
4918
Owen Anderson76706012011-04-05 21:48:57 +00004919static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004920LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4921 // Convert to float
4922 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4923 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4924 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4925 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4926 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4927 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4928 // Get reciprocal estimate.
4929 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004930 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004931 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4932 // Because char has a smaller range than uchar, we can actually get away
4933 // without any newton steps. This requires that we use a weird bias
4934 // of 0xb000, however (again, this has been exhaustively tested).
4935 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4936 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4937 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4938 Y = DAG.getConstant(0xb000, MVT::i32);
4939 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4940 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4941 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4942 // Convert back to short.
4943 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4944 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4945 return X;
4946}
4947
Owen Anderson76706012011-04-05 21:48:57 +00004948static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004949LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4950 SDValue N2;
4951 // Convert to float.
4952 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4953 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4954 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4955 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4956 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4957 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004958
Nate Begeman7973f352011-02-11 20:53:29 +00004959 // Use reciprocal estimate and one refinement step.
4960 // float4 recip = vrecpeq_f32(yf);
4961 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004962 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004963 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004964 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004965 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4966 N1, N2);
4967 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4968 // Because short has a smaller range than ushort, we can actually get away
4969 // with only a single newton step. This requires that we use a weird bias
4970 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004971 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004972 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4973 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004974 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004975 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4976 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4977 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4978 // Convert back to integer and return.
4979 // return vmovn_s32(vcvt_s32_f32(result));
4980 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4981 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4982 return N0;
4983}
4984
4985static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4986 EVT VT = Op.getValueType();
4987 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4988 "unexpected type for custom-lowering ISD::SDIV");
4989
4990 DebugLoc dl = Op.getDebugLoc();
4991 SDValue N0 = Op.getOperand(0);
4992 SDValue N1 = Op.getOperand(1);
4993 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004994
Nate Begeman7973f352011-02-11 20:53:29 +00004995 if (VT == MVT::v8i8) {
4996 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4997 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004998
Nate Begeman7973f352011-02-11 20:53:29 +00004999 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5000 DAG.getIntPtrConstant(4));
5001 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005002 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005003 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5004 DAG.getIntPtrConstant(0));
5005 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5006 DAG.getIntPtrConstant(0));
5007
5008 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5009 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5010
5011 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5012 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005013
Nate Begeman7973f352011-02-11 20:53:29 +00005014 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5015 return N0;
5016 }
5017 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5018}
5019
5020static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5021 EVT VT = Op.getValueType();
5022 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5023 "unexpected type for custom-lowering ISD::UDIV");
5024
5025 DebugLoc dl = Op.getDebugLoc();
5026 SDValue N0 = Op.getOperand(0);
5027 SDValue N1 = Op.getOperand(1);
5028 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005029
Nate Begeman7973f352011-02-11 20:53:29 +00005030 if (VT == MVT::v8i8) {
5031 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5032 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005033
Nate Begeman7973f352011-02-11 20:53:29 +00005034 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5035 DAG.getIntPtrConstant(4));
5036 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005037 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005038 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5039 DAG.getIntPtrConstant(0));
5040 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5041 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005042
Nate Begeman7973f352011-02-11 20:53:29 +00005043 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5044 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005045
Nate Begeman7973f352011-02-11 20:53:29 +00005046 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5047 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005048
5049 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005050 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5051 N0);
5052 return N0;
5053 }
Owen Anderson76706012011-04-05 21:48:57 +00005054
Nate Begeman7973f352011-02-11 20:53:29 +00005055 // v4i16 sdiv ... Convert to float.
5056 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5057 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5058 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5059 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5060 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005061 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005062
5063 // Use reciprocal estimate and two refinement steps.
5064 // float4 recip = vrecpeq_f32(yf);
5065 // recip *= vrecpsq_f32(yf, recip);
5066 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005067 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005068 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005069 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005070 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005071 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005072 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005073 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005074 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005075 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005076 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5077 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5078 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5079 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005080 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005081 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5082 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5083 N1 = DAG.getConstant(2, MVT::i32);
5084 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5085 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5086 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5087 // Convert back to integer and return.
5088 // return vmovn_u32(vcvt_s32_f32(result));
5089 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5090 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5091 return N0;
5092}
5093
Evan Cheng342e3162011-08-30 01:34:54 +00005094static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5095 EVT VT = Op.getNode()->getValueType(0);
5096 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5097
5098 unsigned Opc;
5099 bool ExtraOp = false;
5100 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005101 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005102 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5103 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5104 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5105 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5106 }
5107
5108 if (!ExtraOp)
5109 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5110 Op.getOperand(1));
5111 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5112 Op.getOperand(1), Op.getOperand(2));
5113}
5114
Eli Friedman74bf18c2011-09-15 22:26:18 +00005115static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005116 // Monotonic load/store is legal for all targets
5117 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5118 return Op;
5119
5120 // Aquire/Release load/store is not legal for targets without a
5121 // dmb or equivalent available.
5122 return SDValue();
5123}
5124
5125
Eli Friedman2bdffe42011-08-31 00:31:29 +00005126static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005127ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5128 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005129 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005130 assert (Node->getValueType(0) == MVT::i64 &&
5131 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005132
Eli Friedman4d3f3292011-08-31 17:52:22 +00005133 SmallVector<SDValue, 6> Ops;
5134 Ops.push_back(Node->getOperand(0)); // Chain
5135 Ops.push_back(Node->getOperand(1)); // Ptr
5136 // Low part of Val1
5137 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5138 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5139 // High part of Val1
5140 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5141 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005142 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005143 // High part of Val1
5144 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5145 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5146 // High part of Val2
5147 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5148 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5149 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005150 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5151 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005152 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005153 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005154 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005155 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5156 Results.push_back(Result.getValue(2));
5157}
5158
Dan Gohmand858e902010-04-17 15:26:15 +00005159SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005160 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005161 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005162 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005163 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005164 case ISD::GlobalAddress:
5165 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5166 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005167 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005168 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005169 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5170 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005171 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005172 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005173 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005174 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005175 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005176 case ISD::SINT_TO_FP:
5177 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5178 case ISD::FP_TO_SINT:
5179 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005180 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005181 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005182 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005183 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005184 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005185 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005186 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5187 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005188 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005189 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005190 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005191 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005192 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005193 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005194 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005195 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005196 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005197 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005198 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005199 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005200 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005201 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005202 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005203 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005204 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005205 case ISD::SDIV: return LowerSDIV(Op, DAG);
5206 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005207 case ISD::ADDC:
5208 case ISD::ADDE:
5209 case ISD::SUBC:
5210 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005211 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005212 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005213 }
Evan Chenga8e29892007-01-19 07:51:42 +00005214}
5215
Duncan Sands1607f052008-12-01 11:39:25 +00005216/// ReplaceNodeResults - Replace the results of node with an illegal result
5217/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005218void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5219 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005220 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005221 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005222 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005223 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005224 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005225 case ISD::BITCAST:
5226 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005227 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005228 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005229 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005230 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005231 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005232 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005233 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005234 return;
5235 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005236 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005237 return;
5238 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005239 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005240 return;
5241 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005242 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005243 return;
5244 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005245 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005246 return;
5247 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005248 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005249 return;
5250 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005251 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005252 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005253 case ISD::ATOMIC_CMP_SWAP:
5254 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5255 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005256 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005257 if (Res.getNode())
5258 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005259}
Chris Lattner27a6c732007-11-24 07:07:01 +00005260
Evan Chenga8e29892007-01-19 07:51:42 +00005261//===----------------------------------------------------------------------===//
5262// ARM Scheduler Hooks
5263//===----------------------------------------------------------------------===//
5264
5265MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005266ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5267 MachineBasicBlock *BB,
5268 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005269 unsigned dest = MI->getOperand(0).getReg();
5270 unsigned ptr = MI->getOperand(1).getReg();
5271 unsigned oldval = MI->getOperand(2).getReg();
5272 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005273 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5274 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005275 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005276
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005277 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005278 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5279 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5280 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005281
5282 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005283 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5284 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5285 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005286 }
5287
Jim Grosbach5278eb82009-12-11 01:42:04 +00005288 unsigned ldrOpc, strOpc;
5289 switch (Size) {
5290 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005291 case 1:
5292 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005293 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005294 break;
5295 case 2:
5296 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5297 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5298 break;
5299 case 4:
5300 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5301 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5302 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005303 }
5304
5305 MachineFunction *MF = BB->getParent();
5306 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5307 MachineFunction::iterator It = BB;
5308 ++It; // insert the new blocks after the current block
5309
5310 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5311 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5312 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5313 MF->insert(It, loop1MBB);
5314 MF->insert(It, loop2MBB);
5315 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005316
5317 // Transfer the remainder of BB and its successor edges to exitMBB.
5318 exitMBB->splice(exitMBB->begin(), BB,
5319 llvm::next(MachineBasicBlock::iterator(MI)),
5320 BB->end());
5321 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005322
5323 // thisMBB:
5324 // ...
5325 // fallthrough --> loop1MBB
5326 BB->addSuccessor(loop1MBB);
5327
5328 // loop1MBB:
5329 // ldrex dest, [ptr]
5330 // cmp dest, oldval
5331 // bne exitMBB
5332 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005333 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5334 if (ldrOpc == ARM::t2LDREX)
5335 MIB.addImm(0);
5336 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005337 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005338 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005339 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5340 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005341 BB->addSuccessor(loop2MBB);
5342 BB->addSuccessor(exitMBB);
5343
5344 // loop2MBB:
5345 // strex scratch, newval, [ptr]
5346 // cmp scratch, #0
5347 // bne loop1MBB
5348 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005349 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5350 if (strOpc == ARM::t2STREX)
5351 MIB.addImm(0);
5352 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005353 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005354 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005355 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5356 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005357 BB->addSuccessor(loop1MBB);
5358 BB->addSuccessor(exitMBB);
5359
5360 // exitMBB:
5361 // ...
5362 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005363
Dan Gohman14152b42010-07-06 20:24:04 +00005364 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005365
Jim Grosbach5278eb82009-12-11 01:42:04 +00005366 return BB;
5367}
5368
5369MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005370ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5371 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005372 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5373 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5374
5375 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005376 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005377 MachineFunction::iterator It = BB;
5378 ++It;
5379
5380 unsigned dest = MI->getOperand(0).getReg();
5381 unsigned ptr = MI->getOperand(1).getReg();
5382 unsigned incr = MI->getOperand(2).getReg();
5383 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005384 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005385
5386 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5387 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005388 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5389 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005390 }
5391
Jim Grosbachc3c23542009-12-14 04:22:04 +00005392 unsigned ldrOpc, strOpc;
5393 switch (Size) {
5394 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005395 case 1:
5396 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005397 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005398 break;
5399 case 2:
5400 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5401 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5402 break;
5403 case 4:
5404 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5405 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5406 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005407 }
5408
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005409 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5410 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5411 MF->insert(It, loopMBB);
5412 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005413
5414 // Transfer the remainder of BB and its successor edges to exitMBB.
5415 exitMBB->splice(exitMBB->begin(), BB,
5416 llvm::next(MachineBasicBlock::iterator(MI)),
5417 BB->end());
5418 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005419
Craig Topper420761a2012-04-20 07:30:17 +00005420 const TargetRegisterClass *TRC = isThumb2 ?
5421 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5422 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005423 unsigned scratch = MRI.createVirtualRegister(TRC);
5424 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005425
5426 // thisMBB:
5427 // ...
5428 // fallthrough --> loopMBB
5429 BB->addSuccessor(loopMBB);
5430
5431 // loopMBB:
5432 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005433 // <binop> scratch2, dest, incr
5434 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005435 // cmp scratch, #0
5436 // bne- loopMBB
5437 // fallthrough --> exitMBB
5438 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005439 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5440 if (ldrOpc == ARM::t2LDREX)
5441 MIB.addImm(0);
5442 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005443 if (BinOpcode) {
5444 // operand order needs to go the other way for NAND
5445 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5446 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5447 addReg(incr).addReg(dest)).addReg(0);
5448 else
5449 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5450 addReg(dest).addReg(incr)).addReg(0);
5451 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005452
Jim Grosbachb6aed502011-09-09 18:37:27 +00005453 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5454 if (strOpc == ARM::t2STREX)
5455 MIB.addImm(0);
5456 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005457 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005458 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005459 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5460 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005461
5462 BB->addSuccessor(loopMBB);
5463 BB->addSuccessor(exitMBB);
5464
5465 // exitMBB:
5466 // ...
5467 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005468
Dan Gohman14152b42010-07-06 20:24:04 +00005469 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005470
Jim Grosbachc3c23542009-12-14 04:22:04 +00005471 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005472}
5473
Jim Grosbachf7da8822011-04-26 19:44:18 +00005474MachineBasicBlock *
5475ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5476 MachineBasicBlock *BB,
5477 unsigned Size,
5478 bool signExtend,
5479 ARMCC::CondCodes Cond) const {
5480 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5481
5482 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5483 MachineFunction *MF = BB->getParent();
5484 MachineFunction::iterator It = BB;
5485 ++It;
5486
5487 unsigned dest = MI->getOperand(0).getReg();
5488 unsigned ptr = MI->getOperand(1).getReg();
5489 unsigned incr = MI->getOperand(2).getReg();
5490 unsigned oldval = dest;
5491 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005492 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005493
5494 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5495 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005496 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5497 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005498 }
5499
Jim Grosbachf7da8822011-04-26 19:44:18 +00005500 unsigned ldrOpc, strOpc, extendOpc;
5501 switch (Size) {
5502 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5503 case 1:
5504 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5505 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005506 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005507 break;
5508 case 2:
5509 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5510 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005511 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005512 break;
5513 case 4:
5514 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5515 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5516 extendOpc = 0;
5517 break;
5518 }
5519
5520 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5521 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5522 MF->insert(It, loopMBB);
5523 MF->insert(It, exitMBB);
5524
5525 // Transfer the remainder of BB and its successor edges to exitMBB.
5526 exitMBB->splice(exitMBB->begin(), BB,
5527 llvm::next(MachineBasicBlock::iterator(MI)),
5528 BB->end());
5529 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5530
Craig Topper420761a2012-04-20 07:30:17 +00005531 const TargetRegisterClass *TRC = isThumb2 ?
5532 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5533 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005534 unsigned scratch = MRI.createVirtualRegister(TRC);
5535 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005536
5537 // thisMBB:
5538 // ...
5539 // fallthrough --> loopMBB
5540 BB->addSuccessor(loopMBB);
5541
5542 // loopMBB:
5543 // ldrex dest, ptr
5544 // (sign extend dest, if required)
5545 // cmp dest, incr
5546 // cmov.cond scratch2, dest, incr
5547 // strex scratch, scratch2, ptr
5548 // cmp scratch, #0
5549 // bne- loopMBB
5550 // fallthrough --> exitMBB
5551 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005552 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5553 if (ldrOpc == ARM::t2LDREX)
5554 MIB.addImm(0);
5555 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005556
5557 // Sign extend the value, if necessary.
5558 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005559 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005560 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5561 .addReg(dest)
5562 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005563 }
5564
5565 // Build compare and cmov instructions.
5566 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5567 .addReg(oldval).addReg(incr));
5568 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5569 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5570
Jim Grosbachb6aed502011-09-09 18:37:27 +00005571 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5572 if (strOpc == ARM::t2STREX)
5573 MIB.addImm(0);
5574 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005575 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5576 .addReg(scratch).addImm(0));
5577 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5578 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5579
5580 BB->addSuccessor(loopMBB);
5581 BB->addSuccessor(exitMBB);
5582
5583 // exitMBB:
5584 // ...
5585 BB = exitMBB;
5586
5587 MI->eraseFromParent(); // The instruction is gone now.
5588
5589 return BB;
5590}
5591
Eli Friedman2bdffe42011-08-31 00:31:29 +00005592MachineBasicBlock *
5593ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5594 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005595 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005596 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5597 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5598
5599 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5600 MachineFunction *MF = BB->getParent();
5601 MachineFunction::iterator It = BB;
5602 ++It;
5603
5604 unsigned destlo = MI->getOperand(0).getReg();
5605 unsigned desthi = MI->getOperand(1).getReg();
5606 unsigned ptr = MI->getOperand(2).getReg();
5607 unsigned vallo = MI->getOperand(3).getReg();
5608 unsigned valhi = MI->getOperand(4).getReg();
5609 DebugLoc dl = MI->getDebugLoc();
5610 bool isThumb2 = Subtarget->isThumb2();
5611
5612 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5613 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005614 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5615 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5616 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005617 }
5618
5619 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5620 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5621
5622 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005623 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005624 if (IsCmpxchg) {
5625 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5626 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5627 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005628 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5629 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005630 if (IsCmpxchg) {
5631 MF->insert(It, contBB);
5632 MF->insert(It, cont2BB);
5633 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005634 MF->insert(It, exitMBB);
5635
5636 // Transfer the remainder of BB and its successor edges to exitMBB.
5637 exitMBB->splice(exitMBB->begin(), BB,
5638 llvm::next(MachineBasicBlock::iterator(MI)),
5639 BB->end());
5640 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5641
Craig Topper420761a2012-04-20 07:30:17 +00005642 const TargetRegisterClass *TRC = isThumb2 ?
5643 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5644 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005645 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5646
5647 // thisMBB:
5648 // ...
5649 // fallthrough --> loopMBB
5650 BB->addSuccessor(loopMBB);
5651
5652 // loopMBB:
5653 // ldrexd r2, r3, ptr
5654 // <binopa> r0, r2, incr
5655 // <binopb> r1, r3, incr
5656 // strexd storesuccess, r0, r1, ptr
5657 // cmp storesuccess, #0
5658 // bne- loopMBB
5659 // fallthrough --> exitMBB
5660 //
5661 // Note that the registers are explicitly specified because there is not any
5662 // way to force the register allocator to allocate a register pair.
5663 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005664 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005665 // need to properly enforce the restriction that the two output registers
5666 // for ldrexd must be different.
5667 BB = loopMBB;
5668 // Load
5669 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5670 .addReg(ARM::R2, RegState::Define)
5671 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5672 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5673 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5674 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005675
5676 if (IsCmpxchg) {
5677 // Add early exit
5678 for (unsigned i = 0; i < 2; i++) {
5679 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5680 ARM::CMPrr))
5681 .addReg(i == 0 ? destlo : desthi)
5682 .addReg(i == 0 ? vallo : valhi));
5683 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5684 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5685 BB->addSuccessor(exitMBB);
5686 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5687 BB = (i == 0 ? contBB : cont2BB);
5688 }
5689
5690 // Copy to physregs for strexd
5691 unsigned setlo = MI->getOperand(5).getReg();
5692 unsigned sethi = MI->getOperand(6).getReg();
5693 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5694 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5695 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005696 // Perform binary operation
5697 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5698 .addReg(destlo).addReg(vallo))
5699 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5700 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5701 .addReg(desthi).addReg(valhi)).addReg(0);
5702 } else {
5703 // Copy to physregs for strexd
5704 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5705 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5706 }
5707
5708 // Store
5709 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5710 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5711 // Cmp+jump
5712 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5713 .addReg(storesuccess).addImm(0));
5714 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5715 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5716
5717 BB->addSuccessor(loopMBB);
5718 BB->addSuccessor(exitMBB);
5719
5720 // exitMBB:
5721 // ...
5722 BB = exitMBB;
5723
5724 MI->eraseFromParent(); // The instruction is gone now.
5725
5726 return BB;
5727}
5728
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005729/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5730/// registers the function context.
5731void ARMTargetLowering::
5732SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5733 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005734 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5735 DebugLoc dl = MI->getDebugLoc();
5736 MachineFunction *MF = MBB->getParent();
5737 MachineRegisterInfo *MRI = &MF->getRegInfo();
5738 MachineConstantPool *MCP = MF->getConstantPool();
5739 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5740 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005741
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005742 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005743 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005744
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005745 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005746 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005747 ARMConstantPoolValue *CPV =
5748 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5749 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5750
Craig Topper420761a2012-04-20 07:30:17 +00005751 const TargetRegisterClass *TRC = isThumb ?
5752 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5753 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005754
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005755 // Grab constant pool and fixed stack memory operands.
5756 MachineMemOperand *CPMMO =
5757 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5758 MachineMemOperand::MOLoad, 4, 4);
5759
5760 MachineMemOperand *FIMMOSt =
5761 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5762 MachineMemOperand::MOStore, 4, 4);
5763
5764 // Load the address of the dispatch MBB into the jump buffer.
5765 if (isThumb2) {
5766 // Incoming value: jbuf
5767 // ldr.n r5, LCPI1_1
5768 // orr r5, r5, #1
5769 // add r5, pc
5770 // str r5, [$jbuf, #+4] ; &jbuf[1]
5771 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5772 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5773 .addConstantPoolIndex(CPI)
5774 .addMemOperand(CPMMO));
5775 // Set the low bit because of thumb mode.
5776 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5777 AddDefaultCC(
5778 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5779 .addReg(NewVReg1, RegState::Kill)
5780 .addImm(0x01)));
5781 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5782 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5783 .addReg(NewVReg2, RegState::Kill)
5784 .addImm(PCLabelId);
5785 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5786 .addReg(NewVReg3, RegState::Kill)
5787 .addFrameIndex(FI)
5788 .addImm(36) // &jbuf[1] :: pc
5789 .addMemOperand(FIMMOSt));
5790 } else if (isThumb) {
5791 // Incoming value: jbuf
5792 // ldr.n r1, LCPI1_4
5793 // add r1, pc
5794 // mov r2, #1
5795 // orrs r1, r2
5796 // add r2, $jbuf, #+4 ; &jbuf[1]
5797 // str r1, [r2]
5798 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5799 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5800 .addConstantPoolIndex(CPI)
5801 .addMemOperand(CPMMO));
5802 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5803 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5804 .addReg(NewVReg1, RegState::Kill)
5805 .addImm(PCLabelId);
5806 // Set the low bit because of thumb mode.
5807 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5808 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5809 .addReg(ARM::CPSR, RegState::Define)
5810 .addImm(1));
5811 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5812 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5813 .addReg(ARM::CPSR, RegState::Define)
5814 .addReg(NewVReg2, RegState::Kill)
5815 .addReg(NewVReg3, RegState::Kill));
5816 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5817 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5818 .addFrameIndex(FI)
5819 .addImm(36)); // &jbuf[1] :: pc
5820 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5821 .addReg(NewVReg4, RegState::Kill)
5822 .addReg(NewVReg5, RegState::Kill)
5823 .addImm(0)
5824 .addMemOperand(FIMMOSt));
5825 } else {
5826 // Incoming value: jbuf
5827 // ldr r1, LCPI1_1
5828 // add r1, pc, r1
5829 // str r1, [$jbuf, #+4] ; &jbuf[1]
5830 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5831 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5832 .addConstantPoolIndex(CPI)
5833 .addImm(0)
5834 .addMemOperand(CPMMO));
5835 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5836 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5837 .addReg(NewVReg1, RegState::Kill)
5838 .addImm(PCLabelId));
5839 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5840 .addReg(NewVReg2, RegState::Kill)
5841 .addFrameIndex(FI)
5842 .addImm(36) // &jbuf[1] :: pc
5843 .addMemOperand(FIMMOSt));
5844 }
5845}
5846
5847MachineBasicBlock *ARMTargetLowering::
5848EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5849 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5850 DebugLoc dl = MI->getDebugLoc();
5851 MachineFunction *MF = MBB->getParent();
5852 MachineRegisterInfo *MRI = &MF->getRegInfo();
5853 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5854 MachineFrameInfo *MFI = MF->getFrameInfo();
5855 int FI = MFI->getFunctionContextIndex();
5856
Craig Topper420761a2012-04-20 07:30:17 +00005857 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5858 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005859 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005860
Bill Wendling04f15b42011-10-06 21:29:56 +00005861 // Get a mapping of the call site numbers to all of the landing pads they're
5862 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005863 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5864 unsigned MaxCSNum = 0;
5865 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005866 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5867 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005868 if (!BB->isLandingPad()) continue;
5869
5870 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5871 // pad.
5872 for (MachineBasicBlock::iterator
5873 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5874 if (!II->isEHLabel()) continue;
5875
5876 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005877 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005878
Bill Wendling5cbef192011-10-05 23:28:57 +00005879 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5880 for (SmallVectorImpl<unsigned>::iterator
5881 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5882 CSI != CSE; ++CSI) {
5883 CallSiteNumToLPad[*CSI].push_back(BB);
5884 MaxCSNum = std::max(MaxCSNum, *CSI);
5885 }
Bill Wendling2a850152011-10-05 00:02:33 +00005886 break;
5887 }
5888 }
5889
5890 // Get an ordered list of the machine basic blocks for the jump table.
5891 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005892 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005893 LPadList.reserve(CallSiteNumToLPad.size());
5894 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5895 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5896 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005897 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005898 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005899 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5900 }
Bill Wendling2a850152011-10-05 00:02:33 +00005901 }
5902
Bill Wendling5cbef192011-10-05 23:28:57 +00005903 assert(!LPadList.empty() &&
5904 "No landing pad destinations for the dispatch jump table!");
5905
Bill Wendling04f15b42011-10-06 21:29:56 +00005906 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005907 MachineJumpTableInfo *JTI =
5908 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5909 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5910 unsigned UId = AFI->createJumpTableUId();
5911
Bill Wendling04f15b42011-10-06 21:29:56 +00005912 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005913
5914 // Shove the dispatch's address into the return slot in the function context.
5915 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5916 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005917
Bill Wendlingbb734682011-10-05 00:39:32 +00005918 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005919 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005920 DispatchBB->addSuccessor(TrapBB);
5921
5922 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5923 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005924
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005925 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005926 MF->insert(MF->end(), DispatchBB);
5927 MF->insert(MF->end(), DispContBB);
5928 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005929
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005930 // Insert code into the entry block that creates and registers the function
5931 // context.
5932 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5933
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005934 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005935 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005936 MachineMemOperand::MOLoad |
5937 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005938
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005939 if (AFI->isThumb1OnlyFunction())
5940 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5941 else if (!Subtarget->hasVFP2())
5942 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005943 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005944 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005945
Bill Wendling952cb502011-10-18 22:49:07 +00005946 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005947 if (Subtarget->isThumb2()) {
5948 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5949 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5950 .addFrameIndex(FI)
5951 .addImm(4)
5952 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005953
Bill Wendling952cb502011-10-18 22:49:07 +00005954 if (NumLPads < 256) {
5955 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5956 .addReg(NewVReg1)
5957 .addImm(LPadList.size()));
5958 } else {
5959 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5960 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005961 .addImm(NumLPads & 0xFFFF));
5962
5963 unsigned VReg2 = VReg1;
5964 if ((NumLPads & 0xFFFF0000) != 0) {
5965 VReg2 = MRI->createVirtualRegister(TRC);
5966 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5967 .addReg(VReg1)
5968 .addImm(NumLPads >> 16));
5969 }
5970
Bill Wendling952cb502011-10-18 22:49:07 +00005971 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5972 .addReg(NewVReg1)
5973 .addReg(VReg2));
5974 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005975
Bill Wendling95ce2e92011-10-06 22:53:00 +00005976 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5977 .addMBB(TrapBB)
5978 .addImm(ARMCC::HI)
5979 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005980
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005981 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5982 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005983 .addJumpTableIndex(MJTI)
5984 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005985
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005986 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005987 AddDefaultCC(
5988 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005989 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5990 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005991 .addReg(NewVReg1)
5992 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5993
5994 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005995 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005996 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005997 .addJumpTableIndex(MJTI)
5998 .addImm(UId);
5999 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006000 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6001 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6002 .addFrameIndex(FI)
6003 .addImm(1)
6004 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006005
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006006 if (NumLPads < 256) {
6007 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6008 .addReg(NewVReg1)
6009 .addImm(NumLPads));
6010 } else {
6011 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006012 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6013 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6014
6015 // MachineConstantPool wants an explicit alignment.
6016 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6017 if (Align == 0)
6018 Align = getTargetData()->getTypeAllocSize(C->getType());
6019 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006020
6021 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6022 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6023 .addReg(VReg1, RegState::Define)
6024 .addConstantPoolIndex(Idx));
6025 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6026 .addReg(NewVReg1)
6027 .addReg(VReg1));
6028 }
6029
Bill Wendling083a8eb2011-10-06 23:37:36 +00006030 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6031 .addMBB(TrapBB)
6032 .addImm(ARMCC::HI)
6033 .addReg(ARM::CPSR);
6034
6035 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6036 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6037 .addReg(ARM::CPSR, RegState::Define)
6038 .addReg(NewVReg1)
6039 .addImm(2));
6040
6041 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006042 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006043 .addJumpTableIndex(MJTI)
6044 .addImm(UId));
6045
6046 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6047 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6048 .addReg(ARM::CPSR, RegState::Define)
6049 .addReg(NewVReg2, RegState::Kill)
6050 .addReg(NewVReg3));
6051
6052 MachineMemOperand *JTMMOLd =
6053 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6054 MachineMemOperand::MOLoad, 4, 4);
6055
6056 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6057 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6058 .addReg(NewVReg4, RegState::Kill)
6059 .addImm(0)
6060 .addMemOperand(JTMMOLd));
6061
6062 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6063 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6064 .addReg(ARM::CPSR, RegState::Define)
6065 .addReg(NewVReg5, RegState::Kill)
6066 .addReg(NewVReg3));
6067
6068 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6069 .addReg(NewVReg6, RegState::Kill)
6070 .addJumpTableIndex(MJTI)
6071 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006072 } else {
6073 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6074 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6075 .addFrameIndex(FI)
6076 .addImm(4)
6077 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006078
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006079 if (NumLPads < 256) {
6080 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6081 .addReg(NewVReg1)
6082 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006083 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006084 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6085 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006086 .addImm(NumLPads & 0xFFFF));
6087
6088 unsigned VReg2 = VReg1;
6089 if ((NumLPads & 0xFFFF0000) != 0) {
6090 VReg2 = MRI->createVirtualRegister(TRC);
6091 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6092 .addReg(VReg1)
6093 .addImm(NumLPads >> 16));
6094 }
6095
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006096 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6097 .addReg(NewVReg1)
6098 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006099 } else {
6100 MachineConstantPool *ConstantPool = MF->getConstantPool();
6101 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6102 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6103
6104 // MachineConstantPool wants an explicit alignment.
6105 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6106 if (Align == 0)
6107 Align = getTargetData()->getTypeAllocSize(C->getType());
6108 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6109
6110 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6111 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6112 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006113 .addConstantPoolIndex(Idx)
6114 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006115 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6116 .addReg(NewVReg1)
6117 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006118 }
6119
Bill Wendling95ce2e92011-10-06 22:53:00 +00006120 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6121 .addMBB(TrapBB)
6122 .addImm(ARMCC::HI)
6123 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006124
Bill Wendling564392b2011-10-18 22:11:18 +00006125 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006126 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006127 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006128 .addReg(NewVReg1)
6129 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006130 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6131 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006132 .addJumpTableIndex(MJTI)
6133 .addImm(UId));
6134
6135 MachineMemOperand *JTMMOLd =
6136 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6137 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006138 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006139 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006140 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6141 .addReg(NewVReg3, RegState::Kill)
6142 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006143 .addImm(0)
6144 .addMemOperand(JTMMOLd));
6145
6146 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006147 .addReg(NewVReg5, RegState::Kill)
6148 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006149 .addJumpTableIndex(MJTI)
6150 .addImm(UId);
6151 }
Bill Wendling2a850152011-10-05 00:02:33 +00006152
Bill Wendlingbb734682011-10-05 00:39:32 +00006153 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006154 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006155 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006156 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6157 MachineBasicBlock *CurMBB = *I;
6158 if (PrevMBB != CurMBB)
6159 DispContBB->addSuccessor(CurMBB);
6160 PrevMBB = CurMBB;
6161 }
6162
Bill Wendling24bb9252011-10-17 05:25:09 +00006163 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006164 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6165 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006166 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006167 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006168 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6169 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6170 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006171
6172 // Remove the landing pad successor from the invoke block and replace it
6173 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006174 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6175 BB->succ_end());
6176 while (!Successors.empty()) {
6177 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006178 if (SMBB->isLandingPad()) {
6179 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006180 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006181 }
6182 }
6183
6184 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006185
6186 // Find the invoke call and mark all of the callee-saved registers as
6187 // 'implicit defined' so that they're spilled. This prevents code from
6188 // moving instructions to before the EH block, where they will never be
6189 // executed.
6190 for (MachineBasicBlock::reverse_iterator
6191 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006192 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006193
6194 DenseMap<unsigned, bool> DefRegs;
6195 for (MachineInstr::mop_iterator
6196 OI = II->operands_begin(), OE = II->operands_end();
6197 OI != OE; ++OI) {
6198 if (!OI->isReg()) continue;
6199 DefRegs[OI->getReg()] = true;
6200 }
6201
6202 MachineInstrBuilder MIB(&*II);
6203
Bill Wendling5d798592011-10-14 23:55:44 +00006204 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006205 unsigned Reg = SavedRegs[i];
6206 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006207 !ARM::tGPRRegClass.contains(Reg) &&
6208 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006209 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006210 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006211 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006212 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006213 continue;
6214 if (!DefRegs[Reg])
6215 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006216 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006217
6218 break;
6219 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006220 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006221
Bill Wendlingf7b02072011-10-18 18:30:49 +00006222 // Mark all former landing pads as non-landing pads. The dispatch is the only
6223 // landing pad now.
6224 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6225 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6226 (*I)->setIsLandingPad(false);
6227
Bill Wendlingbb734682011-10-05 00:39:32 +00006228 // The instruction is gone now.
6229 MI->eraseFromParent();
6230
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006231 return MBB;
6232}
6233
Evan Cheng218977b2010-07-13 19:27:42 +00006234static
6235MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6236 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6237 E = MBB->succ_end(); I != E; ++I)
6238 if (*I != Succ)
6239 return *I;
6240 llvm_unreachable("Expecting a BB with two successors!");
6241}
6242
Manman Ren68f25572012-06-01 19:33:18 +00006243MachineBasicBlock *ARMTargetLowering::
6244EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6245 // This pseudo instruction has 3 operands: dst, src, size
6246 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6247 // Otherwise, we will generate unrolled scalar copies.
6248 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6249 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6250 MachineFunction::iterator It = BB;
6251 ++It;
6252
6253 unsigned dest = MI->getOperand(0).getReg();
6254 unsigned src = MI->getOperand(1).getReg();
6255 unsigned SizeVal = MI->getOperand(2).getImm();
6256 unsigned Align = MI->getOperand(3).getImm();
6257 DebugLoc dl = MI->getDebugLoc();
6258
6259 bool isThumb2 = Subtarget->isThumb2();
6260 MachineFunction *MF = BB->getParent();
6261 MachineRegisterInfo &MRI = MF->getRegInfo();
6262 unsigned ldrOpc, strOpc, UnitSize;
6263
6264 const TargetRegisterClass *TRC = isThumb2 ?
6265 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6266 (const TargetRegisterClass*)&ARM::GPRRegClass;
6267
6268 if (Align & 1) {
6269 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6270 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6271 UnitSize = 1;
6272 } else if (Align & 2) {
6273 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6274 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6275 UnitSize = 2;
6276 } else {
6277 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6278 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6279 UnitSize = 4;
6280 }
6281 unsigned BytesLeft = SizeVal % UnitSize;
6282 unsigned LoopSize = SizeVal - BytesLeft;
6283
6284 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6285 // Use LDR and STR to copy.
6286 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6287 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6288 unsigned srcIn = src;
6289 unsigned destIn = dest;
6290 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
6291 unsigned scratch = MRI.createVirtualRegister(TRC);
6292 unsigned srcOut = MRI.createVirtualRegister(TRC);
6293 unsigned destOut = MRI.createVirtualRegister(TRC);
6294 if (isThumb2) {
6295 AddDefaultPred(BuildMI(*BB, MI, dl,
6296 TII->get(ldrOpc), scratch)
6297 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6298
6299 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6300 .addReg(scratch).addReg(destIn)
6301 .addImm(UnitSize));
6302 } else {
6303 AddDefaultPred(BuildMI(*BB, MI, dl,
6304 TII->get(ldrOpc), scratch)
6305 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6306 .addImm(UnitSize));
6307
6308 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6309 .addReg(scratch).addReg(destIn)
6310 .addReg(0).addImm(UnitSize));
6311 }
6312 srcIn = srcOut;
6313 destIn = destOut;
6314 }
6315
6316 // Handle the leftover bytes with LDRB and STRB.
6317 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6318 // [destOut] = STRB_POST(scratch, destIn, 1)
6319 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6320 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6321 for (unsigned i = 0; i < BytesLeft; i++) {
6322 unsigned scratch = MRI.createVirtualRegister(TRC);
6323 unsigned srcOut = MRI.createVirtualRegister(TRC);
6324 unsigned destOut = MRI.createVirtualRegister(TRC);
6325 if (isThumb2) {
6326 AddDefaultPred(BuildMI(*BB, MI, dl,
6327 TII->get(ldrOpc),scratch)
6328 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6329
6330 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6331 .addReg(scratch).addReg(destIn)
6332 .addReg(0).addImm(1));
6333 } else {
6334 AddDefaultPred(BuildMI(*BB, MI, dl,
6335 TII->get(ldrOpc),scratch)
6336 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6337
6338 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6339 .addReg(scratch).addReg(destIn)
6340 .addReg(0).addImm(1));
6341 }
6342 srcIn = srcOut;
6343 destIn = destOut;
6344 }
6345 MI->eraseFromParent(); // The instruction is gone now.
6346 return BB;
6347 }
6348
6349 // Expand the pseudo op to a loop.
6350 // thisMBB:
6351 // ...
6352 // movw varEnd, # --> with thumb2
6353 // movt varEnd, #
6354 // ldrcp varEnd, idx --> without thumb2
6355 // fallthrough --> loopMBB
6356 // loopMBB:
6357 // PHI varPhi, varEnd, varLoop
6358 // PHI srcPhi, src, srcLoop
6359 // PHI destPhi, dst, destLoop
6360 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6361 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6362 // subs varLoop, varPhi, #UnitSize
6363 // bne loopMBB
6364 // fallthrough --> exitMBB
6365 // exitMBB:
6366 // epilogue to handle left-over bytes
6367 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6368 // [destOut] = STRB_POST(scratch, destLoop, 1)
6369 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6370 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6371 MF->insert(It, loopMBB);
6372 MF->insert(It, exitMBB);
6373
6374 // Transfer the remainder of BB and its successor edges to exitMBB.
6375 exitMBB->splice(exitMBB->begin(), BB,
6376 llvm::next(MachineBasicBlock::iterator(MI)),
6377 BB->end());
6378 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6379
6380 // Load an immediate to varEnd.
6381 unsigned varEnd = MRI.createVirtualRegister(TRC);
6382 if (isThumb2) {
6383 unsigned VReg1 = varEnd;
6384 if ((LoopSize & 0xFFFF0000) != 0)
6385 VReg1 = MRI.createVirtualRegister(TRC);
6386 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6387 .addImm(LoopSize & 0xFFFF));
6388
6389 if ((LoopSize & 0xFFFF0000) != 0)
6390 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6391 .addReg(VReg1)
6392 .addImm(LoopSize >> 16));
6393 } else {
6394 MachineConstantPool *ConstantPool = MF->getConstantPool();
6395 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6396 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6397
6398 // MachineConstantPool wants an explicit alignment.
6399 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6400 if (Align == 0)
6401 Align = getTargetData()->getTypeAllocSize(C->getType());
6402 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6403
6404 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6405 .addReg(varEnd, RegState::Define)
6406 .addConstantPoolIndex(Idx)
6407 .addImm(0));
6408 }
6409 BB->addSuccessor(loopMBB);
6410
6411 // Generate the loop body:
6412 // varPhi = PHI(varLoop, varEnd)
6413 // srcPhi = PHI(srcLoop, src)
6414 // destPhi = PHI(destLoop, dst)
6415 MachineBasicBlock *entryBB = BB;
6416 BB = loopMBB;
6417 unsigned varLoop = MRI.createVirtualRegister(TRC);
6418 unsigned varPhi = MRI.createVirtualRegister(TRC);
6419 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6420 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6421 unsigned destLoop = MRI.createVirtualRegister(TRC);
6422 unsigned destPhi = MRI.createVirtualRegister(TRC);
6423
6424 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6425 .addReg(varLoop).addMBB(loopMBB)
6426 .addReg(varEnd).addMBB(entryBB);
6427 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6428 .addReg(srcLoop).addMBB(loopMBB)
6429 .addReg(src).addMBB(entryBB);
6430 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6431 .addReg(destLoop).addMBB(loopMBB)
6432 .addReg(dest).addMBB(entryBB);
6433
6434 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6435 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
6436 unsigned scratch = MRI.createVirtualRegister(TRC);
6437 if (isThumb2) {
6438 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6439 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6440
6441 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6442 .addReg(scratch).addReg(destPhi)
6443 .addImm(UnitSize));
6444 } else {
6445 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6446 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6447 .addImm(UnitSize));
6448
6449 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6450 .addReg(scratch).addReg(destPhi)
6451 .addReg(0).addImm(UnitSize));
6452 }
6453
6454 // Decrement loop variable by UnitSize.
6455 MachineInstrBuilder MIB = BuildMI(BB, dl,
6456 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6457 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6458 MIB->getOperand(5).setReg(ARM::CPSR);
6459 MIB->getOperand(5).setIsDef(true);
6460
6461 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6462 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6463
6464 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6465 BB->addSuccessor(loopMBB);
6466 BB->addSuccessor(exitMBB);
6467
6468 // Add epilogue to handle BytesLeft.
6469 BB = exitMBB;
6470 MachineInstr *StartOfExit = exitMBB->begin();
6471 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6472 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6473
6474 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6475 // [destOut] = STRB_POST(scratch, destLoop, 1)
6476 unsigned srcIn = srcLoop;
6477 unsigned destIn = destLoop;
6478 for (unsigned i = 0; i < BytesLeft; i++) {
6479 unsigned scratch = MRI.createVirtualRegister(TRC);
6480 unsigned srcOut = MRI.createVirtualRegister(TRC);
6481 unsigned destOut = MRI.createVirtualRegister(TRC);
6482 if (isThumb2) {
6483 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6484 TII->get(ldrOpc),scratch)
6485 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6486
6487 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6488 .addReg(scratch).addReg(destIn)
6489 .addImm(1));
6490 } else {
6491 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6492 TII->get(ldrOpc),scratch)
6493 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6494
6495 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6496 .addReg(scratch).addReg(destIn)
6497 .addReg(0).addImm(1));
6498 }
6499 srcIn = srcOut;
6500 destIn = destOut;
6501 }
6502
6503 MI->eraseFromParent(); // The instruction is gone now.
6504 return BB;
6505}
6506
Jim Grosbache801dc42009-12-12 01:40:06 +00006507MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006508ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006509 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006510 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006511 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006512 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006513 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006514 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006515 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006516 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006517 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006518 // The Thumb2 pre-indexed stores have the same MI operands, they just
6519 // define them differently in the .td files from the isel patterns, so
6520 // they need pseudos.
6521 case ARM::t2STR_preidx:
6522 MI->setDesc(TII->get(ARM::t2STR_PRE));
6523 return BB;
6524 case ARM::t2STRB_preidx:
6525 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6526 return BB;
6527 case ARM::t2STRH_preidx:
6528 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6529 return BB;
6530
Jim Grosbach19dec202011-08-05 20:35:44 +00006531 case ARM::STRi_preidx:
6532 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006533 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006534 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6535 // Decode the offset.
6536 unsigned Offset = MI->getOperand(4).getImm();
6537 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6538 Offset = ARM_AM::getAM2Offset(Offset);
6539 if (isSub)
6540 Offset = -Offset;
6541
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006542 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006543 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006544 .addOperand(MI->getOperand(0)) // Rn_wb
6545 .addOperand(MI->getOperand(1)) // Rt
6546 .addOperand(MI->getOperand(2)) // Rn
6547 .addImm(Offset) // offset (skip GPR==zero_reg)
6548 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006549 .addOperand(MI->getOperand(6))
6550 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006551 MI->eraseFromParent();
6552 return BB;
6553 }
6554 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006555 case ARM::STRBr_preidx:
6556 case ARM::STRH_preidx: {
6557 unsigned NewOpc;
6558 switch (MI->getOpcode()) {
6559 default: llvm_unreachable("unexpected opcode!");
6560 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6561 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6562 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6563 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006564 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6565 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6566 MIB.addOperand(MI->getOperand(i));
6567 MI->eraseFromParent();
6568 return BB;
6569 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006570 case ARM::ATOMIC_LOAD_ADD_I8:
6571 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6572 case ARM::ATOMIC_LOAD_ADD_I16:
6573 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6574 case ARM::ATOMIC_LOAD_ADD_I32:
6575 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006576
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006577 case ARM::ATOMIC_LOAD_AND_I8:
6578 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6579 case ARM::ATOMIC_LOAD_AND_I16:
6580 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6581 case ARM::ATOMIC_LOAD_AND_I32:
6582 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006583
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006584 case ARM::ATOMIC_LOAD_OR_I8:
6585 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6586 case ARM::ATOMIC_LOAD_OR_I16:
6587 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6588 case ARM::ATOMIC_LOAD_OR_I32:
6589 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006590
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006591 case ARM::ATOMIC_LOAD_XOR_I8:
6592 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6593 case ARM::ATOMIC_LOAD_XOR_I16:
6594 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6595 case ARM::ATOMIC_LOAD_XOR_I32:
6596 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006597
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006598 case ARM::ATOMIC_LOAD_NAND_I8:
6599 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6600 case ARM::ATOMIC_LOAD_NAND_I16:
6601 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6602 case ARM::ATOMIC_LOAD_NAND_I32:
6603 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006604
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006605 case ARM::ATOMIC_LOAD_SUB_I8:
6606 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6607 case ARM::ATOMIC_LOAD_SUB_I16:
6608 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6609 case ARM::ATOMIC_LOAD_SUB_I32:
6610 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006611
Jim Grosbachf7da8822011-04-26 19:44:18 +00006612 case ARM::ATOMIC_LOAD_MIN_I8:
6613 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6614 case ARM::ATOMIC_LOAD_MIN_I16:
6615 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6616 case ARM::ATOMIC_LOAD_MIN_I32:
6617 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6618
6619 case ARM::ATOMIC_LOAD_MAX_I8:
6620 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6621 case ARM::ATOMIC_LOAD_MAX_I16:
6622 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6623 case ARM::ATOMIC_LOAD_MAX_I32:
6624 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6625
6626 case ARM::ATOMIC_LOAD_UMIN_I8:
6627 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6628 case ARM::ATOMIC_LOAD_UMIN_I16:
6629 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6630 case ARM::ATOMIC_LOAD_UMIN_I32:
6631 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6632
6633 case ARM::ATOMIC_LOAD_UMAX_I8:
6634 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6635 case ARM::ATOMIC_LOAD_UMAX_I16:
6636 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6637 case ARM::ATOMIC_LOAD_UMAX_I32:
6638 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6639
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006640 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6641 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6642 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006643
6644 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6645 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6646 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006647
Eli Friedman2bdffe42011-08-31 00:31:29 +00006648
6649 case ARM::ATOMADD6432:
6650 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006651 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6652 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006653 case ARM::ATOMSUB6432:
6654 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006655 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6656 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006657 case ARM::ATOMOR6432:
6658 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006659 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006660 case ARM::ATOMXOR6432:
6661 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006662 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006663 case ARM::ATOMAND6432:
6664 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006665 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006666 case ARM::ATOMSWAP6432:
6667 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006668 case ARM::ATOMCMPXCHG6432:
6669 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6670 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6671 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006672
Evan Cheng007ea272009-08-12 05:17:19 +00006673 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006674 // To "insert" a SELECT_CC instruction, we actually have to insert the
6675 // diamond control-flow pattern. The incoming instruction knows the
6676 // destination vreg to set, the condition code register to branch on, the
6677 // true/false values to select between, and a branch opcode to use.
6678 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006679 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006680 ++It;
6681
6682 // thisMBB:
6683 // ...
6684 // TrueVal = ...
6685 // cmpTY ccX, r1, r2
6686 // bCC copy1MBB
6687 // fallthrough --> copy0MBB
6688 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006689 MachineFunction *F = BB->getParent();
6690 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6691 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006692 F->insert(It, copy0MBB);
6693 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006694
6695 // Transfer the remainder of BB and its successor edges to sinkMBB.
6696 sinkMBB->splice(sinkMBB->begin(), BB,
6697 llvm::next(MachineBasicBlock::iterator(MI)),
6698 BB->end());
6699 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6700
Dan Gohman258c58c2010-07-06 15:49:48 +00006701 BB->addSuccessor(copy0MBB);
6702 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006703
Dan Gohman14152b42010-07-06 20:24:04 +00006704 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6705 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6706
Evan Chenga8e29892007-01-19 07:51:42 +00006707 // copy0MBB:
6708 // %FalseValue = ...
6709 // # fallthrough to sinkMBB
6710 BB = copy0MBB;
6711
6712 // Update machine-CFG edges
6713 BB->addSuccessor(sinkMBB);
6714
6715 // sinkMBB:
6716 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6717 // ...
6718 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006719 BuildMI(*BB, BB->begin(), dl,
6720 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006721 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6722 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6723
Dan Gohman14152b42010-07-06 20:24:04 +00006724 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006725 return BB;
6726 }
Evan Cheng86198642009-08-07 00:34:42 +00006727
Evan Cheng218977b2010-07-13 19:27:42 +00006728 case ARM::BCCi64:
6729 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006730 // If there is an unconditional branch to the other successor, remove it.
6731 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006732
Evan Cheng218977b2010-07-13 19:27:42 +00006733 // Compare both parts that make up the double comparison separately for
6734 // equality.
6735 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6736
6737 unsigned LHS1 = MI->getOperand(1).getReg();
6738 unsigned LHS2 = MI->getOperand(2).getReg();
6739 if (RHSisZero) {
6740 AddDefaultPred(BuildMI(BB, dl,
6741 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6742 .addReg(LHS1).addImm(0));
6743 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6744 .addReg(LHS2).addImm(0)
6745 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6746 } else {
6747 unsigned RHS1 = MI->getOperand(3).getReg();
6748 unsigned RHS2 = MI->getOperand(4).getReg();
6749 AddDefaultPred(BuildMI(BB, dl,
6750 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6751 .addReg(LHS1).addReg(RHS1));
6752 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6753 .addReg(LHS2).addReg(RHS2)
6754 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6755 }
6756
6757 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6758 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6759 if (MI->getOperand(0).getImm() == ARMCC::NE)
6760 std::swap(destMBB, exitMBB);
6761
6762 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6763 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006764 if (isThumb2)
6765 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6766 else
6767 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006768
6769 MI->eraseFromParent(); // The pseudo instruction is gone now.
6770 return BB;
6771 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006772
Bill Wendling5bc85282011-10-17 20:37:20 +00006773 case ARM::Int_eh_sjlj_setjmp:
6774 case ARM::Int_eh_sjlj_setjmp_nofp:
6775 case ARM::tInt_eh_sjlj_setjmp:
6776 case ARM::t2Int_eh_sjlj_setjmp:
6777 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6778 EmitSjLjDispatchBlock(MI, BB);
6779 return BB;
6780
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006781 case ARM::ABS:
6782 case ARM::t2ABS: {
6783 // To insert an ABS instruction, we have to insert the
6784 // diamond control-flow pattern. The incoming instruction knows the
6785 // source vreg to test against 0, the destination vreg to set,
6786 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006787 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006788 // It transforms
6789 // V1 = ABS V0
6790 // into
6791 // V2 = MOVS V0
6792 // BCC (branch to SinkBB if V0 >= 0)
6793 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006794 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006795 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6796 MachineFunction::iterator BBI = BB;
6797 ++BBI;
6798 MachineFunction *Fn = BB->getParent();
6799 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6800 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6801 Fn->insert(BBI, RSBBB);
6802 Fn->insert(BBI, SinkBB);
6803
6804 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6805 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6806 bool isThumb2 = Subtarget->isThumb2();
6807 MachineRegisterInfo &MRI = Fn->getRegInfo();
6808 // In Thumb mode S must not be specified if source register is the SP or
6809 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006810 unsigned NewMovDstReg = MRI.createVirtualRegister(isThumb2 ?
6811 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6812 (const TargetRegisterClass*)&ARM::GPRRegClass);
6813 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6814 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6815 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006816
6817 // Transfer the remainder of BB and its successor edges to sinkMBB.
6818 SinkBB->splice(SinkBB->begin(), BB,
6819 llvm::next(MachineBasicBlock::iterator(MI)),
6820 BB->end());
6821 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6822
6823 BB->addSuccessor(RSBBB);
6824 BB->addSuccessor(SinkBB);
6825
6826 // fall through to SinkMBB
6827 RSBBB->addSuccessor(SinkBB);
6828
6829 // insert a movs at the end of BB
6830 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6831 NewMovDstReg)
6832 .addReg(ABSSrcReg, RegState::Kill)
6833 .addImm((unsigned)ARMCC::AL).addReg(0)
6834 .addReg(ARM::CPSR, RegState::Define);
6835
6836 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006837 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006838 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6839 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6840
6841 // insert rsbri in RSBBB
6842 // Note: BCC and rsbri will be converted into predicated rsbmi
6843 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006844 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006845 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6846 .addReg(NewMovDstReg, RegState::Kill)
6847 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6848
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006849 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006850 // reuse ABSDstReg to not change uses of ABS instruction
6851 BuildMI(*SinkBB, SinkBB->begin(), dl,
6852 TII->get(ARM::PHI), ABSDstReg)
6853 .addReg(NewRsbDstReg).addMBB(RSBBB)
6854 .addReg(NewMovDstReg).addMBB(BB);
6855
6856 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006857 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006858
6859 // return last added BB
6860 return SinkBB;
6861 }
Manman Ren68f25572012-06-01 19:33:18 +00006862 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006863 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006864 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006865 }
6866}
6867
Evan Cheng37fefc22011-08-30 19:09:48 +00006868void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6869 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006870 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006871 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6872 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6873 return;
6874 }
6875
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006876 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006877 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6878 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6879 // operand is still set to noreg. If needed, set the optional operand's
6880 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006881 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006882 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006883
Andrew Trick3be654f2011-09-21 02:20:46 +00006884 // Rename pseudo opcodes.
6885 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6886 if (NewOpc) {
6887 const ARMBaseInstrInfo *TII =
6888 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006889 MCID = &TII->get(NewOpc);
6890
6891 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6892 "converted opcode should be the same except for cc_out");
6893
6894 MI->setDesc(*MCID);
6895
6896 // Add the optional cc_out operand
6897 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006898 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006899 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006900
6901 // Any ARM instruction that sets the 's' bit should specify an optional
6902 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006903 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006904 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006905 return;
6906 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006907 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6908 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006909 bool definesCPSR = false;
6910 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006911 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006912 i != e; ++i) {
6913 const MachineOperand &MO = MI->getOperand(i);
6914 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6915 definesCPSR = true;
6916 if (MO.isDead())
6917 deadCPSR = true;
6918 MI->RemoveOperand(i);
6919 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006920 }
6921 }
Andrew Trick4815d562011-09-20 03:17:40 +00006922 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006923 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006924 return;
6925 }
6926 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006927 if (deadCPSR) {
6928 assert(!MI->getOperand(ccOutIdx).getReg() &&
6929 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006930 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006931 }
Andrew Trick4815d562011-09-20 03:17:40 +00006932
Andrew Trick3be654f2011-09-21 02:20:46 +00006933 // If this instruction was defined with an optional CPSR def and its dag node
6934 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006935 MachineOperand &MO = MI->getOperand(ccOutIdx);
6936 MO.setReg(ARM::CPSR);
6937 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006938}
6939
Evan Chenga8e29892007-01-19 07:51:42 +00006940//===----------------------------------------------------------------------===//
6941// ARM Optimization Hooks
6942//===----------------------------------------------------------------------===//
6943
Chris Lattnerd1980a52009-03-12 06:52:53 +00006944static
6945SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6946 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006947 SelectionDAG &DAG = DCI.DAG;
6948 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006949 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006950 unsigned Opc = N->getOpcode();
6951 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6952 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6953 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6954 ISD::CondCode CC = ISD::SETCC_INVALID;
6955
6956 if (isSlctCC) {
6957 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6958 } else {
6959 SDValue CCOp = Slct.getOperand(0);
6960 if (CCOp.getOpcode() == ISD::SETCC)
6961 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6962 }
6963
6964 bool DoXform = false;
6965 bool InvCC = false;
6966 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6967 "Bad input!");
6968
6969 if (LHS.getOpcode() == ISD::Constant &&
6970 cast<ConstantSDNode>(LHS)->isNullValue()) {
6971 DoXform = true;
6972 } else if (CC != ISD::SETCC_INVALID &&
6973 RHS.getOpcode() == ISD::Constant &&
6974 cast<ConstantSDNode>(RHS)->isNullValue()) {
6975 std::swap(LHS, RHS);
6976 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006977 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006978 Op0.getOperand(0).getValueType();
6979 bool isInt = OpVT.isInteger();
6980 CC = ISD::getSetCCInverse(CC, isInt);
6981
6982 if (!TLI.isCondCodeLegal(CC, OpVT))
6983 return SDValue(); // Inverse operator isn't legal.
6984
6985 DoXform = true;
6986 InvCC = true;
6987 }
6988
6989 if (DoXform) {
6990 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6991 if (isSlctCC)
6992 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6993 Slct.getOperand(0), Slct.getOperand(1), CC);
6994 SDValue CCOp = Slct.getOperand(0);
6995 if (InvCC)
6996 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6997 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6998 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6999 CCOp, OtherOp, Result);
7000 }
7001 return SDValue();
7002}
7003
Eric Christopherfa6f5912011-06-29 21:10:36 +00007004// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007005// (only after legalization).
7006static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7007 TargetLowering::DAGCombinerInfo &DCI,
7008 const ARMSubtarget *Subtarget) {
7009
7010 // Only perform optimization if after legalize, and if NEON is available. We
7011 // also expected both operands to be BUILD_VECTORs.
7012 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7013 || N0.getOpcode() != ISD::BUILD_VECTOR
7014 || N1.getOpcode() != ISD::BUILD_VECTOR)
7015 return SDValue();
7016
7017 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7018 EVT VT = N->getValueType(0);
7019 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7020 return SDValue();
7021
7022 // Check that the vector operands are of the right form.
7023 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7024 // operands, where N is the size of the formed vector.
7025 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7026 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007027
7028 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007029 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007030 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007031 SDValue Vec = N0->getOperand(0)->getOperand(0);
7032 SDNode *V = Vec.getNode();
7033 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007034
Eric Christopherfa6f5912011-06-29 21:10:36 +00007035 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007036 // check to see if each of their operands are an EXTRACT_VECTOR with
7037 // the same vector and appropriate index.
7038 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7039 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7040 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007041
Tanya Lattner189531f2011-06-14 23:48:48 +00007042 SDValue ExtVec0 = N0->getOperand(i);
7043 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007044
Tanya Lattner189531f2011-06-14 23:48:48 +00007045 // First operand is the vector, verify its the same.
7046 if (V != ExtVec0->getOperand(0).getNode() ||
7047 V != ExtVec1->getOperand(0).getNode())
7048 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007049
Tanya Lattner189531f2011-06-14 23:48:48 +00007050 // Second is the constant, verify its correct.
7051 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7052 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007053
Tanya Lattner189531f2011-06-14 23:48:48 +00007054 // For the constant, we want to see all the even or all the odd.
7055 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7056 || C1->getZExtValue() != nextIndex+1)
7057 return SDValue();
7058
7059 // Increment index.
7060 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007061 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007062 return SDValue();
7063 }
7064
7065 // Create VPADDL node.
7066 SelectionDAG &DAG = DCI.DAG;
7067 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007068
7069 // Build operand list.
7070 SmallVector<SDValue, 8> Ops;
7071 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7072 TLI.getPointerTy()));
7073
7074 // Input is the vector.
7075 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007076
Tanya Lattner189531f2011-06-14 23:48:48 +00007077 // Get widened type and narrowed type.
7078 MVT widenType;
7079 unsigned numElem = VT.getVectorNumElements();
7080 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7081 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7082 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7083 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7084 default:
Craig Topperbc219812012-02-07 02:50:20 +00007085 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007086 }
7087
7088 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7089 widenType, &Ops[0], Ops.size());
7090 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7091}
7092
Bob Wilson3d5792a2010-07-29 20:34:14 +00007093/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7094/// operands N0 and N1. This is a helper for PerformADDCombine that is
7095/// called with the default operands, and if that fails, with commuted
7096/// operands.
7097static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007098 TargetLowering::DAGCombinerInfo &DCI,
7099 const ARMSubtarget *Subtarget){
7100
7101 // Attempt to create vpaddl for this add.
7102 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7103 if (Result.getNode())
7104 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007105
Chris Lattnerd1980a52009-03-12 06:52:53 +00007106 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7107 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7108 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7109 if (Result.getNode()) return Result;
7110 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007111 return SDValue();
7112}
7113
Bob Wilson3d5792a2010-07-29 20:34:14 +00007114/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7115///
7116static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007117 TargetLowering::DAGCombinerInfo &DCI,
7118 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007119 SDValue N0 = N->getOperand(0);
7120 SDValue N1 = N->getOperand(1);
7121
7122 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007123 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007124 if (Result.getNode())
7125 return Result;
7126
7127 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007128 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007129}
7130
Chris Lattnerd1980a52009-03-12 06:52:53 +00007131/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007132///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007133static SDValue PerformSUBCombine(SDNode *N,
7134 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007135 SDValue N0 = N->getOperand(0);
7136 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007137
Chris Lattnerd1980a52009-03-12 06:52:53 +00007138 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7139 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7140 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7141 if (Result.getNode()) return Result;
7142 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007143
Chris Lattnerd1980a52009-03-12 06:52:53 +00007144 return SDValue();
7145}
7146
Evan Cheng463d3582011-03-31 19:38:48 +00007147/// PerformVMULCombine
7148/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7149/// special multiplier accumulator forwarding.
7150/// vmul d3, d0, d2
7151/// vmla d3, d1, d2
7152/// is faster than
7153/// vadd d3, d0, d1
7154/// vmul d3, d3, d2
7155static SDValue PerformVMULCombine(SDNode *N,
7156 TargetLowering::DAGCombinerInfo &DCI,
7157 const ARMSubtarget *Subtarget) {
7158 if (!Subtarget->hasVMLxForwarding())
7159 return SDValue();
7160
7161 SelectionDAG &DAG = DCI.DAG;
7162 SDValue N0 = N->getOperand(0);
7163 SDValue N1 = N->getOperand(1);
7164 unsigned Opcode = N0.getOpcode();
7165 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7166 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007167 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007168 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7169 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7170 return SDValue();
7171 std::swap(N0, N1);
7172 }
7173
7174 EVT VT = N->getValueType(0);
7175 DebugLoc DL = N->getDebugLoc();
7176 SDValue N00 = N0->getOperand(0);
7177 SDValue N01 = N0->getOperand(1);
7178 return DAG.getNode(Opcode, DL, VT,
7179 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7180 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7181}
7182
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007183static SDValue PerformMULCombine(SDNode *N,
7184 TargetLowering::DAGCombinerInfo &DCI,
7185 const ARMSubtarget *Subtarget) {
7186 SelectionDAG &DAG = DCI.DAG;
7187
7188 if (Subtarget->isThumb1Only())
7189 return SDValue();
7190
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007191 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7192 return SDValue();
7193
7194 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007195 if (VT.is64BitVector() || VT.is128BitVector())
7196 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007197 if (VT != MVT::i32)
7198 return SDValue();
7199
7200 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7201 if (!C)
7202 return SDValue();
7203
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007204 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007205 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007206
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007207 ShiftAmt = ShiftAmt & (32 - 1);
7208 SDValue V = N->getOperand(0);
7209 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007210
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007211 SDValue Res;
7212 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007213
7214 if (MulAmt >= 0) {
7215 if (isPowerOf2_32(MulAmt - 1)) {
7216 // (mul x, 2^N + 1) => (add (shl x, N), x)
7217 Res = DAG.getNode(ISD::ADD, DL, VT,
7218 V,
7219 DAG.getNode(ISD::SHL, DL, VT,
7220 V,
7221 DAG.getConstant(Log2_32(MulAmt - 1),
7222 MVT::i32)));
7223 } else if (isPowerOf2_32(MulAmt + 1)) {
7224 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7225 Res = DAG.getNode(ISD::SUB, DL, VT,
7226 DAG.getNode(ISD::SHL, DL, VT,
7227 V,
7228 DAG.getConstant(Log2_32(MulAmt + 1),
7229 MVT::i32)),
7230 V);
7231 } else
7232 return SDValue();
7233 } else {
7234 uint64_t MulAmtAbs = -MulAmt;
7235 if (isPowerOf2_32(MulAmtAbs + 1)) {
7236 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7237 Res = DAG.getNode(ISD::SUB, DL, VT,
7238 V,
7239 DAG.getNode(ISD::SHL, DL, VT,
7240 V,
7241 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7242 MVT::i32)));
7243 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7244 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7245 Res = DAG.getNode(ISD::ADD, DL, VT,
7246 V,
7247 DAG.getNode(ISD::SHL, DL, VT,
7248 V,
7249 DAG.getConstant(Log2_32(MulAmtAbs-1),
7250 MVT::i32)));
7251 Res = DAG.getNode(ISD::SUB, DL, VT,
7252 DAG.getConstant(0, MVT::i32),Res);
7253
7254 } else
7255 return SDValue();
7256 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007257
7258 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007259 Res = DAG.getNode(ISD::SHL, DL, VT,
7260 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007261
7262 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007263 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007264 return SDValue();
7265}
7266
Evan Chengc892aeb2012-02-23 01:19:06 +00007267static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7268 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7269 return false;
7270
7271 SDValue FalseVal = N.getOperand(0);
7272 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7273 if (!C)
7274 return false;
7275 if (AllOnes)
7276 return C->isAllOnesValue();
7277 return C->isNullValue();
7278}
7279
7280/// formConditionalOp - Combine an operation with a conditional move operand
7281/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7282/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7283static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7284 bool Commutable) {
7285 SDValue N0 = N->getOperand(0);
7286 SDValue N1 = N->getOperand(1);
7287
7288 bool isAND = N->getOpcode() == ISD::AND;
7289 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7290 if (!isCand && Commutable) {
7291 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7292 if (isCand)
7293 std::swap(N0, N1);
7294 }
7295 if (!isCand)
7296 return SDValue();
7297
7298 unsigned Opc = 0;
7299 switch (N->getOpcode()) {
7300 default: llvm_unreachable("Unexpected node");
7301 case ISD::AND: Opc = ARMISD::CAND; break;
7302 case ISD::OR: Opc = ARMISD::COR; break;
7303 case ISD::XOR: Opc = ARMISD::CXOR; break;
7304 }
7305 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7306 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7307 N1.getOperand(4));
7308}
7309
Owen Anderson080c0922010-11-05 19:27:46 +00007310static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007311 TargetLowering::DAGCombinerInfo &DCI,
7312 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007313
Owen Anderson080c0922010-11-05 19:27:46 +00007314 // Attempt to use immediate-form VBIC
7315 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7316 DebugLoc dl = N->getDebugLoc();
7317 EVT VT = N->getValueType(0);
7318 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007319
Tanya Lattner0433b212011-04-07 15:24:20 +00007320 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7321 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007322
Owen Anderson080c0922010-11-05 19:27:46 +00007323 APInt SplatBits, SplatUndef;
7324 unsigned SplatBitSize;
7325 bool HasAnyUndefs;
7326 if (BVN &&
7327 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7328 if (SplatBitSize <= 64) {
7329 EVT VbicVT;
7330 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7331 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007332 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007333 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007334 if (Val.getNode()) {
7335 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007336 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007337 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007338 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007339 }
7340 }
7341 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007342
Evan Chengc892aeb2012-02-23 01:19:06 +00007343 if (!Subtarget->isThumb1Only()) {
7344 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7345 SDValue CAND = formConditionalOp(N, DAG, true);
7346 if (CAND.getNode())
7347 return CAND;
7348 }
7349
Owen Anderson080c0922010-11-05 19:27:46 +00007350 return SDValue();
7351}
7352
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007353/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7354static SDValue PerformORCombine(SDNode *N,
7355 TargetLowering::DAGCombinerInfo &DCI,
7356 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007357 // Attempt to use immediate-form VORR
7358 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7359 DebugLoc dl = N->getDebugLoc();
7360 EVT VT = N->getValueType(0);
7361 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007362
Tanya Lattner0433b212011-04-07 15:24:20 +00007363 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7364 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007365
Owen Anderson60f48702010-11-03 23:15:26 +00007366 APInt SplatBits, SplatUndef;
7367 unsigned SplatBitSize;
7368 bool HasAnyUndefs;
7369 if (BVN && Subtarget->hasNEON() &&
7370 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7371 if (SplatBitSize <= 64) {
7372 EVT VorrVT;
7373 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7374 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007375 DAG, VorrVT, VT.is128BitVector(),
7376 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007377 if (Val.getNode()) {
7378 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007379 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007380 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007381 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007382 }
7383 }
7384 }
7385
Evan Chengc892aeb2012-02-23 01:19:06 +00007386 if (!Subtarget->isThumb1Only()) {
7387 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7388 SDValue COR = formConditionalOp(N, DAG, true);
7389 if (COR.getNode())
7390 return COR;
7391 }
7392
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007393 SDValue N0 = N->getOperand(0);
7394 if (N0.getOpcode() != ISD::AND)
7395 return SDValue();
7396 SDValue N1 = N->getOperand(1);
7397
7398 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7399 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7400 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7401 APInt SplatUndef;
7402 unsigned SplatBitSize;
7403 bool HasAnyUndefs;
7404
7405 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7406 APInt SplatBits0;
7407 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7408 HasAnyUndefs) && !HasAnyUndefs) {
7409 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7410 APInt SplatBits1;
7411 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7412 HasAnyUndefs) && !HasAnyUndefs &&
7413 SplatBits0 == ~SplatBits1) {
7414 // Canonicalize the vector type to make instruction selection simpler.
7415 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7416 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7417 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007418 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007419 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7420 }
7421 }
7422 }
7423
Jim Grosbach54238562010-07-17 03:30:54 +00007424 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7425 // reasonable.
7426
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007427 // BFI is only available on V6T2+
7428 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7429 return SDValue();
7430
Jim Grosbach54238562010-07-17 03:30:54 +00007431 DebugLoc DL = N->getDebugLoc();
7432 // 1) or (and A, mask), val => ARMbfi A, val, mask
7433 // iff (val & mask) == val
7434 //
7435 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7436 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007437 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007438 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007439 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007440 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007441
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007442 if (VT != MVT::i32)
7443 return SDValue();
7444
Evan Cheng30fb13f2010-12-13 20:32:54 +00007445 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007446
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007447 // The value and the mask need to be constants so we can verify this is
7448 // actually a bitfield set. If the mask is 0xffff, we can do better
7449 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007450 SDValue MaskOp = N0.getOperand(1);
7451 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7452 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007453 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007454 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007455 if (Mask == 0xffff)
7456 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007457 SDValue Res;
7458 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007459 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7460 if (N1C) {
7461 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007462 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007463 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007464
Evan Chenga9688c42010-12-11 04:11:38 +00007465 if (ARM::isBitFieldInvertedMask(Mask)) {
7466 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007467
Evan Cheng30fb13f2010-12-13 20:32:54 +00007468 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007469 DAG.getConstant(Val, MVT::i32),
7470 DAG.getConstant(Mask, MVT::i32));
7471
7472 // Do not add new nodes to DAG combiner worklist.
7473 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007474 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007475 }
Jim Grosbach54238562010-07-17 03:30:54 +00007476 } else if (N1.getOpcode() == ISD::AND) {
7477 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007478 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7479 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007480 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007481 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007482
Eric Christopher29aeed12011-03-26 01:21:03 +00007483 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7484 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007485 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007486 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007487 // The pack halfword instruction works better for masks that fit it,
7488 // so use that when it's available.
7489 if (Subtarget->hasT2ExtractPack() &&
7490 (Mask == 0xffff || Mask == 0xffff0000))
7491 return SDValue();
7492 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007493 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007494 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007495 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007496 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007497 DAG.getConstant(Mask, MVT::i32));
7498 // Do not add new nodes to DAG combiner worklist.
7499 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007500 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007501 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007502 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007503 // The pack halfword instruction works better for masks that fit it,
7504 // so use that when it's available.
7505 if (Subtarget->hasT2ExtractPack() &&
7506 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7507 return SDValue();
7508 // 2b
7509 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007510 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007511 DAG.getConstant(lsb, MVT::i32));
7512 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007513 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007514 // Do not add new nodes to DAG combiner worklist.
7515 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007516 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007517 }
7518 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007519
Evan Cheng30fb13f2010-12-13 20:32:54 +00007520 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7521 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7522 ARM::isBitFieldInvertedMask(~Mask)) {
7523 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7524 // where lsb(mask) == #shamt and masked bits of B are known zero.
7525 SDValue ShAmt = N00.getOperand(1);
7526 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7527 unsigned LSB = CountTrailingZeros_32(Mask);
7528 if (ShAmtC != LSB)
7529 return SDValue();
7530
7531 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7532 DAG.getConstant(~Mask, MVT::i32));
7533
7534 // Do not add new nodes to DAG combiner worklist.
7535 DCI.CombineTo(N, Res, false);
7536 }
7537
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007538 return SDValue();
7539}
7540
Evan Chengc892aeb2012-02-23 01:19:06 +00007541static SDValue PerformXORCombine(SDNode *N,
7542 TargetLowering::DAGCombinerInfo &DCI,
7543 const ARMSubtarget *Subtarget) {
7544 EVT VT = N->getValueType(0);
7545 SelectionDAG &DAG = DCI.DAG;
7546
7547 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7548 return SDValue();
7549
7550 if (!Subtarget->isThumb1Only()) {
7551 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7552 SDValue CXOR = formConditionalOp(N, DAG, true);
7553 if (CXOR.getNode())
7554 return CXOR;
7555 }
7556
7557 return SDValue();
7558}
7559
Evan Chengbf188ae2011-06-15 01:12:31 +00007560/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7561/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007562static SDValue PerformBFICombine(SDNode *N,
7563 TargetLowering::DAGCombinerInfo &DCI) {
7564 SDValue N1 = N->getOperand(1);
7565 if (N1.getOpcode() == ISD::AND) {
7566 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7567 if (!N11C)
7568 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007569 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7570 unsigned LSB = CountTrailingZeros_32(~InvMask);
7571 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7572 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007573 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007574 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007575 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7576 N->getOperand(0), N1.getOperand(0),
7577 N->getOperand(2));
7578 }
7579 return SDValue();
7580}
7581
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007582/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7583/// ARMISD::VMOVRRD.
7584static SDValue PerformVMOVRRDCombine(SDNode *N,
7585 TargetLowering::DAGCombinerInfo &DCI) {
7586 // vmovrrd(vmovdrr x, y) -> x,y
7587 SDValue InDouble = N->getOperand(0);
7588 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7589 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007590
7591 // vmovrrd(load f64) -> (load i32), (load i32)
7592 SDNode *InNode = InDouble.getNode();
7593 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7594 InNode->getValueType(0) == MVT::f64 &&
7595 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7596 !cast<LoadSDNode>(InNode)->isVolatile()) {
7597 // TODO: Should this be done for non-FrameIndex operands?
7598 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7599
7600 SelectionDAG &DAG = DCI.DAG;
7601 DebugLoc DL = LD->getDebugLoc();
7602 SDValue BasePtr = LD->getBasePtr();
7603 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7604 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007605 LD->isNonTemporal(), LD->isInvariant(),
7606 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007607
7608 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7609 DAG.getConstant(4, MVT::i32));
7610 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7611 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007612 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007613 std::min(4U, LD->getAlignment() / 2));
7614
7615 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7616 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7617 DCI.RemoveFromWorklist(LD);
7618 DAG.DeleteNode(LD);
7619 return Result;
7620 }
7621
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007622 return SDValue();
7623}
7624
7625/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7626/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7627static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7628 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7629 SDValue Op0 = N->getOperand(0);
7630 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007631 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007632 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007633 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007634 Op1 = Op1.getOperand(0);
7635 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7636 Op0.getNode() == Op1.getNode() &&
7637 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007638 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007639 N->getValueType(0), Op0.getOperand(0));
7640 return SDValue();
7641}
7642
Bob Wilson31600902010-12-21 06:43:19 +00007643/// PerformSTORECombine - Target-specific dag combine xforms for
7644/// ISD::STORE.
7645static SDValue PerformSTORECombine(SDNode *N,
7646 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007647 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007648 if (St->isVolatile())
7649 return SDValue();
7650
7651 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
7652 // pack all of the elements in one place. Next, store to memory in fewer
7653 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007654 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007655 EVT VT = StVal.getValueType();
7656 if (St->isTruncatingStore() && VT.isVector()) {
7657 SelectionDAG &DAG = DCI.DAG;
7658 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7659 EVT StVT = St->getMemoryVT();
7660 unsigned NumElems = VT.getVectorNumElements();
7661 assert(StVT != VT && "Cannot truncate to the same type");
7662 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7663 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7664
7665 // From, To sizes and ElemCount must be pow of two
7666 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7667
7668 // We are going to use the original vector elt for storing.
7669 // Accumulated smaller vector elements must be a multiple of the store size.
7670 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7671
7672 unsigned SizeRatio = FromEltSz / ToEltSz;
7673 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7674
7675 // Create a type on which we perform the shuffle.
7676 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7677 NumElems*SizeRatio);
7678 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7679
7680 DebugLoc DL = St->getDebugLoc();
7681 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7682 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7683 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7684
7685 // Can't shuffle using an illegal type.
7686 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7687
7688 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7689 DAG.getUNDEF(WideVec.getValueType()),
7690 ShuffleVec.data());
7691 // At this point all of the data is stored at the bottom of the
7692 // register. We now need to save it to mem.
7693
7694 // Find the largest store unit
7695 MVT StoreType = MVT::i8;
7696 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7697 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7698 MVT Tp = (MVT::SimpleValueType)tp;
7699 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7700 StoreType = Tp;
7701 }
7702 // Didn't find a legal store type.
7703 if (!TLI.isTypeLegal(StoreType))
7704 return SDValue();
7705
7706 // Bitcast the original vector into a vector of store-size units
7707 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7708 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7709 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7710 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7711 SmallVector<SDValue, 8> Chains;
7712 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7713 TLI.getPointerTy());
7714 SDValue BasePtr = St->getBasePtr();
7715
7716 // Perform one or more big stores into memory.
7717 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7718 for (unsigned I = 0; I < E; I++) {
7719 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7720 StoreType, ShuffWide,
7721 DAG.getIntPtrConstant(I));
7722 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7723 St->getPointerInfo(), St->isVolatile(),
7724 St->isNonTemporal(), St->getAlignment());
7725 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7726 Increment);
7727 Chains.push_back(Ch);
7728 }
7729 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7730 Chains.size());
7731 }
7732
7733 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007734 return SDValue();
7735
Chad Rosier96b66d62012-04-09 19:38:15 +00007736 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7737 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007738 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007739 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007740 SelectionDAG &DAG = DCI.DAG;
7741 DebugLoc DL = St->getDebugLoc();
7742 SDValue BasePtr = St->getBasePtr();
7743 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7744 StVal.getNode()->getOperand(0), BasePtr,
7745 St->getPointerInfo(), St->isVolatile(),
7746 St->isNonTemporal(), St->getAlignment());
7747
7748 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7749 DAG.getConstant(4, MVT::i32));
7750 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7751 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7752 St->isNonTemporal(),
7753 std::min(4U, St->getAlignment() / 2));
7754 }
7755
7756 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007757 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7758 return SDValue();
7759
Chad Rosier96b66d62012-04-09 19:38:15 +00007760 // Bitcast an i64 store extracted from a vector to f64.
7761 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007762 SelectionDAG &DAG = DCI.DAG;
7763 DebugLoc dl = StVal.getDebugLoc();
7764 SDValue IntVec = StVal.getOperand(0);
7765 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7766 IntVec.getValueType().getVectorNumElements());
7767 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7768 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7769 Vec, StVal.getOperand(1));
7770 dl = N->getDebugLoc();
7771 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7772 // Make the DAGCombiner fold the bitcasts.
7773 DCI.AddToWorklist(Vec.getNode());
7774 DCI.AddToWorklist(ExtElt.getNode());
7775 DCI.AddToWorklist(V.getNode());
7776 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7777 St->getPointerInfo(), St->isVolatile(),
7778 St->isNonTemporal(), St->getAlignment(),
7779 St->getTBAAInfo());
7780}
7781
7782/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7783/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7784/// i64 vector to have f64 elements, since the value can then be loaded
7785/// directly into a VFP register.
7786static bool hasNormalLoadOperand(SDNode *N) {
7787 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7788 for (unsigned i = 0; i < NumElts; ++i) {
7789 SDNode *Elt = N->getOperand(i).getNode();
7790 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7791 return true;
7792 }
7793 return false;
7794}
7795
Bob Wilson75f02882010-09-17 22:59:05 +00007796/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7797/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007798static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7799 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007800 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7801 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7802 // into a pair of GPRs, which is fine when the value is used as a scalar,
7803 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007804 SelectionDAG &DAG = DCI.DAG;
7805 if (N->getNumOperands() == 2) {
7806 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7807 if (RV.getNode())
7808 return RV;
7809 }
Bob Wilson75f02882010-09-17 22:59:05 +00007810
Bob Wilson31600902010-12-21 06:43:19 +00007811 // Load i64 elements as f64 values so that type legalization does not split
7812 // them up into i32 values.
7813 EVT VT = N->getValueType(0);
7814 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7815 return SDValue();
7816 DebugLoc dl = N->getDebugLoc();
7817 SmallVector<SDValue, 8> Ops;
7818 unsigned NumElts = VT.getVectorNumElements();
7819 for (unsigned i = 0; i < NumElts; ++i) {
7820 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7821 Ops.push_back(V);
7822 // Make the DAGCombiner fold the bitcast.
7823 DCI.AddToWorklist(V.getNode());
7824 }
7825 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7826 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7827 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7828}
7829
7830/// PerformInsertEltCombine - Target-specific dag combine xforms for
7831/// ISD::INSERT_VECTOR_ELT.
7832static SDValue PerformInsertEltCombine(SDNode *N,
7833 TargetLowering::DAGCombinerInfo &DCI) {
7834 // Bitcast an i64 load inserted into a vector to f64.
7835 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7836 EVT VT = N->getValueType(0);
7837 SDNode *Elt = N->getOperand(1).getNode();
7838 if (VT.getVectorElementType() != MVT::i64 ||
7839 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7840 return SDValue();
7841
7842 SelectionDAG &DAG = DCI.DAG;
7843 DebugLoc dl = N->getDebugLoc();
7844 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7845 VT.getVectorNumElements());
7846 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7847 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7848 // Make the DAGCombiner fold the bitcasts.
7849 DCI.AddToWorklist(Vec.getNode());
7850 DCI.AddToWorklist(V.getNode());
7851 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7852 Vec, V, N->getOperand(2));
7853 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007854}
7855
Bob Wilsonf20700c2010-10-27 20:38:28 +00007856/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7857/// ISD::VECTOR_SHUFFLE.
7858static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7859 // The LLVM shufflevector instruction does not require the shuffle mask
7860 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7861 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7862 // operands do not match the mask length, they are extended by concatenating
7863 // them with undef vectors. That is probably the right thing for other
7864 // targets, but for NEON it is better to concatenate two double-register
7865 // size vector operands into a single quad-register size vector. Do that
7866 // transformation here:
7867 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7868 // shuffle(concat(v1, v2), undef)
7869 SDValue Op0 = N->getOperand(0);
7870 SDValue Op1 = N->getOperand(1);
7871 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7872 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7873 Op0.getNumOperands() != 2 ||
7874 Op1.getNumOperands() != 2)
7875 return SDValue();
7876 SDValue Concat0Op1 = Op0.getOperand(1);
7877 SDValue Concat1Op1 = Op1.getOperand(1);
7878 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7879 Concat1Op1.getOpcode() != ISD::UNDEF)
7880 return SDValue();
7881 // Skip the transformation if any of the types are illegal.
7882 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7883 EVT VT = N->getValueType(0);
7884 if (!TLI.isTypeLegal(VT) ||
7885 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7886 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7887 return SDValue();
7888
7889 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7890 Op0.getOperand(0), Op1.getOperand(0));
7891 // Translate the shuffle mask.
7892 SmallVector<int, 16> NewMask;
7893 unsigned NumElts = VT.getVectorNumElements();
7894 unsigned HalfElts = NumElts/2;
7895 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7896 for (unsigned n = 0; n < NumElts; ++n) {
7897 int MaskElt = SVN->getMaskElt(n);
7898 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007899 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007900 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007901 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007902 NewElt = HalfElts + MaskElt - NumElts;
7903 NewMask.push_back(NewElt);
7904 }
7905 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7906 DAG.getUNDEF(VT), NewMask.data());
7907}
7908
Bob Wilson1c3ef902011-02-07 17:43:21 +00007909/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7910/// NEON load/store intrinsics to merge base address updates.
7911static SDValue CombineBaseUpdate(SDNode *N,
7912 TargetLowering::DAGCombinerInfo &DCI) {
7913 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7914 return SDValue();
7915
7916 SelectionDAG &DAG = DCI.DAG;
7917 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7918 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7919 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7920 SDValue Addr = N->getOperand(AddrOpIdx);
7921
7922 // Search for a use of the address operand that is an increment.
7923 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7924 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7925 SDNode *User = *UI;
7926 if (User->getOpcode() != ISD::ADD ||
7927 UI.getUse().getResNo() != Addr.getResNo())
7928 continue;
7929
7930 // Check that the add is independent of the load/store. Otherwise, folding
7931 // it would create a cycle.
7932 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7933 continue;
7934
7935 // Find the new opcode for the updating load/store.
7936 bool isLoad = true;
7937 bool isLaneOp = false;
7938 unsigned NewOpc = 0;
7939 unsigned NumVecs = 0;
7940 if (isIntrinsic) {
7941 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7942 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007943 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007944 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7945 NumVecs = 1; break;
7946 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7947 NumVecs = 2; break;
7948 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7949 NumVecs = 3; break;
7950 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7951 NumVecs = 4; break;
7952 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7953 NumVecs = 2; isLaneOp = true; break;
7954 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7955 NumVecs = 3; isLaneOp = true; break;
7956 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7957 NumVecs = 4; isLaneOp = true; break;
7958 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7959 NumVecs = 1; isLoad = false; break;
7960 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7961 NumVecs = 2; isLoad = false; break;
7962 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7963 NumVecs = 3; isLoad = false; break;
7964 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7965 NumVecs = 4; isLoad = false; break;
7966 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7967 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7968 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7969 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7970 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7971 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7972 }
7973 } else {
7974 isLaneOp = true;
7975 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007976 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007977 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7978 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7979 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7980 }
7981 }
7982
7983 // Find the size of memory referenced by the load/store.
7984 EVT VecTy;
7985 if (isLoad)
7986 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007987 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007988 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7989 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7990 if (isLaneOp)
7991 NumBytes /= VecTy.getVectorNumElements();
7992
7993 // If the increment is a constant, it must match the memory ref size.
7994 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7995 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7996 uint64_t IncVal = CInc->getZExtValue();
7997 if (IncVal != NumBytes)
7998 continue;
7999 } else if (NumBytes >= 3 * 16) {
8000 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8001 // separate instructions that make it harder to use a non-constant update.
8002 continue;
8003 }
8004
8005 // Create the new updating load/store node.
8006 EVT Tys[6];
8007 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8008 unsigned n;
8009 for (n = 0; n < NumResultVecs; ++n)
8010 Tys[n] = VecTy;
8011 Tys[n++] = MVT::i32;
8012 Tys[n] = MVT::Other;
8013 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8014 SmallVector<SDValue, 8> Ops;
8015 Ops.push_back(N->getOperand(0)); // incoming chain
8016 Ops.push_back(N->getOperand(AddrOpIdx));
8017 Ops.push_back(Inc);
8018 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8019 Ops.push_back(N->getOperand(i));
8020 }
8021 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8022 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8023 Ops.data(), Ops.size(),
8024 MemInt->getMemoryVT(),
8025 MemInt->getMemOperand());
8026
8027 // Update the uses.
8028 std::vector<SDValue> NewResults;
8029 for (unsigned i = 0; i < NumResultVecs; ++i) {
8030 NewResults.push_back(SDValue(UpdN.getNode(), i));
8031 }
8032 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8033 DCI.CombineTo(N, NewResults);
8034 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8035
8036 break;
Owen Anderson76706012011-04-05 21:48:57 +00008037 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008038 return SDValue();
8039}
8040
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008041/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8042/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8043/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8044/// return true.
8045static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8046 SelectionDAG &DAG = DCI.DAG;
8047 EVT VT = N->getValueType(0);
8048 // vldN-dup instructions only support 64-bit vectors for N > 1.
8049 if (!VT.is64BitVector())
8050 return false;
8051
8052 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8053 SDNode *VLD = N->getOperand(0).getNode();
8054 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8055 return false;
8056 unsigned NumVecs = 0;
8057 unsigned NewOpc = 0;
8058 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8059 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8060 NumVecs = 2;
8061 NewOpc = ARMISD::VLD2DUP;
8062 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8063 NumVecs = 3;
8064 NewOpc = ARMISD::VLD3DUP;
8065 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8066 NumVecs = 4;
8067 NewOpc = ARMISD::VLD4DUP;
8068 } else {
8069 return false;
8070 }
8071
8072 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8073 // numbers match the load.
8074 unsigned VLDLaneNo =
8075 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8076 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8077 UI != UE; ++UI) {
8078 // Ignore uses of the chain result.
8079 if (UI.getUse().getResNo() == NumVecs)
8080 continue;
8081 SDNode *User = *UI;
8082 if (User->getOpcode() != ARMISD::VDUPLANE ||
8083 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8084 return false;
8085 }
8086
8087 // Create the vldN-dup node.
8088 EVT Tys[5];
8089 unsigned n;
8090 for (n = 0; n < NumVecs; ++n)
8091 Tys[n] = VT;
8092 Tys[n] = MVT::Other;
8093 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8094 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8095 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8096 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8097 Ops, 2, VLDMemInt->getMemoryVT(),
8098 VLDMemInt->getMemOperand());
8099
8100 // Update the uses.
8101 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8102 UI != UE; ++UI) {
8103 unsigned ResNo = UI.getUse().getResNo();
8104 // Ignore uses of the chain result.
8105 if (ResNo == NumVecs)
8106 continue;
8107 SDNode *User = *UI;
8108 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8109 }
8110
8111 // Now the vldN-lane intrinsic is dead except for its chain result.
8112 // Update uses of the chain.
8113 std::vector<SDValue> VLDDupResults;
8114 for (unsigned n = 0; n < NumVecs; ++n)
8115 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8116 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8117 DCI.CombineTo(VLD, VLDDupResults);
8118
8119 return true;
8120}
8121
Bob Wilson9e82bf12010-07-14 01:22:12 +00008122/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8123/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008124static SDValue PerformVDUPLANECombine(SDNode *N,
8125 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008126 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008127
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008128 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8129 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8130 if (CombineVLDDUP(N, DCI))
8131 return SDValue(N, 0);
8132
8133 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8134 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008135 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008136 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008137 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008138 return SDValue();
8139
8140 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8141 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8142 // The canonical VMOV for a zero vector uses a 32-bit element size.
8143 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8144 unsigned EltBits;
8145 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8146 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008147 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008148 if (EltSize > VT.getVectorElementType().getSizeInBits())
8149 return SDValue();
8150
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008151 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008152}
8153
Eric Christopherfa6f5912011-06-29 21:10:36 +00008154// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008155// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8156static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8157{
Chad Rosier118c9a02011-06-28 17:26:57 +00008158 integerPart cN;
8159 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008160 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8161 I != E; I++) {
8162 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8163 if (!C)
8164 return false;
8165
Eric Christopherfa6f5912011-06-29 21:10:36 +00008166 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008167 APFloat APF = C->getValueAPF();
8168 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8169 != APFloat::opOK || !isExact)
8170 return false;
8171
8172 c0 = (I == 0) ? cN : c0;
8173 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8174 return false;
8175 }
8176 C = c0;
8177 return true;
8178}
8179
8180/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8181/// can replace combinations of VMUL and VCVT (floating-point to integer)
8182/// when the VMUL has a constant operand that is a power of 2.
8183///
8184/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8185/// vmul.f32 d16, d17, d16
8186/// vcvt.s32.f32 d16, d16
8187/// becomes:
8188/// vcvt.s32.f32 d16, d16, #3
8189static SDValue PerformVCVTCombine(SDNode *N,
8190 TargetLowering::DAGCombinerInfo &DCI,
8191 const ARMSubtarget *Subtarget) {
8192 SelectionDAG &DAG = DCI.DAG;
8193 SDValue Op = N->getOperand(0);
8194
8195 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8196 Op.getOpcode() != ISD::FMUL)
8197 return SDValue();
8198
8199 uint64_t C;
8200 SDValue N0 = Op->getOperand(0);
8201 SDValue ConstVec = Op->getOperand(1);
8202 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8203
Eric Christopherfa6f5912011-06-29 21:10:36 +00008204 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008205 !isConstVecPow2(ConstVec, isSigned, C))
8206 return SDValue();
8207
8208 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8209 Intrinsic::arm_neon_vcvtfp2fxu;
8210 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8211 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008212 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008213 DAG.getConstant(Log2_64(C), MVT::i32));
8214}
8215
8216/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8217/// can replace combinations of VCVT (integer to floating-point) and VDIV
8218/// when the VDIV has a constant operand that is a power of 2.
8219///
8220/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8221/// vcvt.f32.s32 d16, d16
8222/// vdiv.f32 d16, d17, d16
8223/// becomes:
8224/// vcvt.f32.s32 d16, d16, #3
8225static SDValue PerformVDIVCombine(SDNode *N,
8226 TargetLowering::DAGCombinerInfo &DCI,
8227 const ARMSubtarget *Subtarget) {
8228 SelectionDAG &DAG = DCI.DAG;
8229 SDValue Op = N->getOperand(0);
8230 unsigned OpOpcode = Op.getNode()->getOpcode();
8231
8232 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8233 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8234 return SDValue();
8235
8236 uint64_t C;
8237 SDValue ConstVec = N->getOperand(1);
8238 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8239
8240 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8241 !isConstVecPow2(ConstVec, isSigned, C))
8242 return SDValue();
8243
Eric Christopherfa6f5912011-06-29 21:10:36 +00008244 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008245 Intrinsic::arm_neon_vcvtfxu2fp;
8246 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8247 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008248 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008249 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8250}
8251
8252/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008253/// operand of a vector shift operation, where all the elements of the
8254/// build_vector must have the same constant integer value.
8255static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8256 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008257 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008258 Op = Op.getOperand(0);
8259 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8260 APInt SplatBits, SplatUndef;
8261 unsigned SplatBitSize;
8262 bool HasAnyUndefs;
8263 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8264 HasAnyUndefs, ElementBits) ||
8265 SplatBitSize > ElementBits)
8266 return false;
8267 Cnt = SplatBits.getSExtValue();
8268 return true;
8269}
8270
8271/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8272/// operand of a vector shift left operation. That value must be in the range:
8273/// 0 <= Value < ElementBits for a left shift; or
8274/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008275static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008276 assert(VT.isVector() && "vector shift count is not a vector type");
8277 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8278 if (! getVShiftImm(Op, ElementBits, Cnt))
8279 return false;
8280 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8281}
8282
8283/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8284/// operand of a vector shift right operation. For a shift opcode, the value
8285/// is positive, but for an intrinsic the value count must be negative. The
8286/// absolute value must be in the range:
8287/// 1 <= |Value| <= ElementBits for a right shift; or
8288/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008289static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008290 int64_t &Cnt) {
8291 assert(VT.isVector() && "vector shift count is not a vector type");
8292 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8293 if (! getVShiftImm(Op, ElementBits, Cnt))
8294 return false;
8295 if (isIntrinsic)
8296 Cnt = -Cnt;
8297 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8298}
8299
8300/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8301static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8302 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8303 switch (IntNo) {
8304 default:
8305 // Don't do anything for most intrinsics.
8306 break;
8307
8308 // Vector shifts: check for immediate versions and lower them.
8309 // Note: This is done during DAG combining instead of DAG legalizing because
8310 // the build_vectors for 64-bit vector element shift counts are generally
8311 // not legal, and it is hard to see their values after they get legalized to
8312 // loads from a constant pool.
8313 case Intrinsic::arm_neon_vshifts:
8314 case Intrinsic::arm_neon_vshiftu:
8315 case Intrinsic::arm_neon_vshiftls:
8316 case Intrinsic::arm_neon_vshiftlu:
8317 case Intrinsic::arm_neon_vshiftn:
8318 case Intrinsic::arm_neon_vrshifts:
8319 case Intrinsic::arm_neon_vrshiftu:
8320 case Intrinsic::arm_neon_vrshiftn:
8321 case Intrinsic::arm_neon_vqshifts:
8322 case Intrinsic::arm_neon_vqshiftu:
8323 case Intrinsic::arm_neon_vqshiftsu:
8324 case Intrinsic::arm_neon_vqshiftns:
8325 case Intrinsic::arm_neon_vqshiftnu:
8326 case Intrinsic::arm_neon_vqshiftnsu:
8327 case Intrinsic::arm_neon_vqrshiftns:
8328 case Intrinsic::arm_neon_vqrshiftnu:
8329 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008330 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008331 int64_t Cnt;
8332 unsigned VShiftOpc = 0;
8333
8334 switch (IntNo) {
8335 case Intrinsic::arm_neon_vshifts:
8336 case Intrinsic::arm_neon_vshiftu:
8337 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8338 VShiftOpc = ARMISD::VSHL;
8339 break;
8340 }
8341 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8342 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8343 ARMISD::VSHRs : ARMISD::VSHRu);
8344 break;
8345 }
8346 return SDValue();
8347
8348 case Intrinsic::arm_neon_vshiftls:
8349 case Intrinsic::arm_neon_vshiftlu:
8350 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8351 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008352 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008353
8354 case Intrinsic::arm_neon_vrshifts:
8355 case Intrinsic::arm_neon_vrshiftu:
8356 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8357 break;
8358 return SDValue();
8359
8360 case Intrinsic::arm_neon_vqshifts:
8361 case Intrinsic::arm_neon_vqshiftu:
8362 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8363 break;
8364 return SDValue();
8365
8366 case Intrinsic::arm_neon_vqshiftsu:
8367 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8368 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008369 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008370
8371 case Intrinsic::arm_neon_vshiftn:
8372 case Intrinsic::arm_neon_vrshiftn:
8373 case Intrinsic::arm_neon_vqshiftns:
8374 case Intrinsic::arm_neon_vqshiftnu:
8375 case Intrinsic::arm_neon_vqshiftnsu:
8376 case Intrinsic::arm_neon_vqrshiftns:
8377 case Intrinsic::arm_neon_vqrshiftnu:
8378 case Intrinsic::arm_neon_vqrshiftnsu:
8379 // Narrowing shifts require an immediate right shift.
8380 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8381 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008382 llvm_unreachable("invalid shift count for narrowing vector shift "
8383 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008384
8385 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008386 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008387 }
8388
8389 switch (IntNo) {
8390 case Intrinsic::arm_neon_vshifts:
8391 case Intrinsic::arm_neon_vshiftu:
8392 // Opcode already set above.
8393 break;
8394 case Intrinsic::arm_neon_vshiftls:
8395 case Intrinsic::arm_neon_vshiftlu:
8396 if (Cnt == VT.getVectorElementType().getSizeInBits())
8397 VShiftOpc = ARMISD::VSHLLi;
8398 else
8399 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8400 ARMISD::VSHLLs : ARMISD::VSHLLu);
8401 break;
8402 case Intrinsic::arm_neon_vshiftn:
8403 VShiftOpc = ARMISD::VSHRN; break;
8404 case Intrinsic::arm_neon_vrshifts:
8405 VShiftOpc = ARMISD::VRSHRs; break;
8406 case Intrinsic::arm_neon_vrshiftu:
8407 VShiftOpc = ARMISD::VRSHRu; break;
8408 case Intrinsic::arm_neon_vrshiftn:
8409 VShiftOpc = ARMISD::VRSHRN; break;
8410 case Intrinsic::arm_neon_vqshifts:
8411 VShiftOpc = ARMISD::VQSHLs; break;
8412 case Intrinsic::arm_neon_vqshiftu:
8413 VShiftOpc = ARMISD::VQSHLu; break;
8414 case Intrinsic::arm_neon_vqshiftsu:
8415 VShiftOpc = ARMISD::VQSHLsu; break;
8416 case Intrinsic::arm_neon_vqshiftns:
8417 VShiftOpc = ARMISD::VQSHRNs; break;
8418 case Intrinsic::arm_neon_vqshiftnu:
8419 VShiftOpc = ARMISD::VQSHRNu; break;
8420 case Intrinsic::arm_neon_vqshiftnsu:
8421 VShiftOpc = ARMISD::VQSHRNsu; break;
8422 case Intrinsic::arm_neon_vqrshiftns:
8423 VShiftOpc = ARMISD::VQRSHRNs; break;
8424 case Intrinsic::arm_neon_vqrshiftnu:
8425 VShiftOpc = ARMISD::VQRSHRNu; break;
8426 case Intrinsic::arm_neon_vqrshiftnsu:
8427 VShiftOpc = ARMISD::VQRSHRNsu; break;
8428 }
8429
8430 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008431 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008432 }
8433
8434 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008435 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008436 int64_t Cnt;
8437 unsigned VShiftOpc = 0;
8438
8439 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8440 VShiftOpc = ARMISD::VSLI;
8441 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8442 VShiftOpc = ARMISD::VSRI;
8443 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008444 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008445 }
8446
8447 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8448 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008449 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008450 }
8451
8452 case Intrinsic::arm_neon_vqrshifts:
8453 case Intrinsic::arm_neon_vqrshiftu:
8454 // No immediate versions of these to check for.
8455 break;
8456 }
8457
8458 return SDValue();
8459}
8460
8461/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8462/// lowers them. As with the vector shift intrinsics, this is done during DAG
8463/// combining instead of DAG legalizing because the build_vectors for 64-bit
8464/// vector element shift counts are generally not legal, and it is hard to see
8465/// their values after they get legalized to loads from a constant pool.
8466static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8467 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008468 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008469 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8470 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8471 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8472 SDValue N1 = N->getOperand(1);
8473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8474 SDValue N0 = N->getOperand(0);
8475 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8476 DAG.MaskedValueIsZero(N0.getOperand(0),
8477 APInt::getHighBitsSet(32, 16)))
8478 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8479 }
8480 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008481
8482 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008483 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8484 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008485 return SDValue();
8486
8487 assert(ST->hasNEON() && "unexpected vector shift");
8488 int64_t Cnt;
8489
8490 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008491 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008492
8493 case ISD::SHL:
8494 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8495 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008496 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008497 break;
8498
8499 case ISD::SRA:
8500 case ISD::SRL:
8501 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8502 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8503 ARMISD::VSHRs : ARMISD::VSHRu);
8504 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008505 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008506 }
8507 }
8508 return SDValue();
8509}
8510
8511/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8512/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8513static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8514 const ARMSubtarget *ST) {
8515 SDValue N0 = N->getOperand(0);
8516
8517 // Check for sign- and zero-extensions of vector extract operations of 8-
8518 // and 16-bit vector elements. NEON supports these directly. They are
8519 // handled during DAG combining because type legalization will promote them
8520 // to 32-bit types and it is messy to recognize the operations after that.
8521 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8522 SDValue Vec = N0.getOperand(0);
8523 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008524 EVT VT = N->getValueType(0);
8525 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008526 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8527
Owen Anderson825b72b2009-08-11 20:47:22 +00008528 if (VT == MVT::i32 &&
8529 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008530 TLI.isTypeLegal(Vec.getValueType()) &&
8531 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008532
8533 unsigned Opc = 0;
8534 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008535 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008536 case ISD::SIGN_EXTEND:
8537 Opc = ARMISD::VGETLANEs;
8538 break;
8539 case ISD::ZERO_EXTEND:
8540 case ISD::ANY_EXTEND:
8541 Opc = ARMISD::VGETLANEu;
8542 break;
8543 }
8544 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8545 }
8546 }
8547
8548 return SDValue();
8549}
8550
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008551/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8552/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8553static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8554 const ARMSubtarget *ST) {
8555 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008556 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008557 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8558 // a NaN; only do the transformation when it matches that behavior.
8559
8560 // For now only do this when using NEON for FP operations; if using VFP, it
8561 // is not obvious that the benefit outweighs the cost of switching to the
8562 // NEON pipeline.
8563 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8564 N->getValueType(0) != MVT::f32)
8565 return SDValue();
8566
8567 SDValue CondLHS = N->getOperand(0);
8568 SDValue CondRHS = N->getOperand(1);
8569 SDValue LHS = N->getOperand(2);
8570 SDValue RHS = N->getOperand(3);
8571 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8572
8573 unsigned Opcode = 0;
8574 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008575 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008576 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008577 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008578 IsReversed = true ; // x CC y ? y : x
8579 } else {
8580 return SDValue();
8581 }
8582
Bob Wilsone742bb52010-02-24 22:15:53 +00008583 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008584 switch (CC) {
8585 default: break;
8586 case ISD::SETOLT:
8587 case ISD::SETOLE:
8588 case ISD::SETLT:
8589 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008590 case ISD::SETULT:
8591 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008592 // If LHS is NaN, an ordered comparison will be false and the result will
8593 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8594 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8595 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8596 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8597 break;
8598 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8599 // will return -0, so vmin can only be used for unsafe math or if one of
8600 // the operands is known to be nonzero.
8601 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008602 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008603 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8604 break;
8605 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008606 break;
8607
8608 case ISD::SETOGT:
8609 case ISD::SETOGE:
8610 case ISD::SETGT:
8611 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008612 case ISD::SETUGT:
8613 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008614 // If LHS is NaN, an ordered comparison will be false and the result will
8615 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8616 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8617 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8618 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8619 break;
8620 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8621 // will return +0, so vmax can only be used for unsafe math or if one of
8622 // the operands is known to be nonzero.
8623 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008624 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008625 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8626 break;
8627 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008628 break;
8629 }
8630
8631 if (!Opcode)
8632 return SDValue();
8633 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8634}
8635
Evan Chenge721f5c2011-07-13 00:42:17 +00008636/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8637SDValue
8638ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8639 SDValue Cmp = N->getOperand(4);
8640 if (Cmp.getOpcode() != ARMISD::CMPZ)
8641 // Only looking at EQ and NE cases.
8642 return SDValue();
8643
8644 EVT VT = N->getValueType(0);
8645 DebugLoc dl = N->getDebugLoc();
8646 SDValue LHS = Cmp.getOperand(0);
8647 SDValue RHS = Cmp.getOperand(1);
8648 SDValue FalseVal = N->getOperand(0);
8649 SDValue TrueVal = N->getOperand(1);
8650 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008651 ARMCC::CondCodes CC =
8652 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008653
8654 // Simplify
8655 // mov r1, r0
8656 // cmp r1, x
8657 // mov r0, y
8658 // moveq r0, x
8659 // to
8660 // cmp r0, x
8661 // movne r0, y
8662 //
8663 // mov r1, r0
8664 // cmp r1, x
8665 // mov r0, x
8666 // movne r0, y
8667 // to
8668 // cmp r0, x
8669 // movne r0, y
8670 /// FIXME: Turn this into a target neutral optimization?
8671 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008672 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008673 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8674 N->getOperand(3), Cmp);
8675 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8676 SDValue ARMcc;
8677 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8678 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8679 N->getOperand(3), NewCmp);
8680 }
8681
8682 if (Res.getNode()) {
8683 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008684 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008685 // Capture demanded bits information that would be otherwise lost.
8686 if (KnownZero == 0xfffffffe)
8687 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8688 DAG.getValueType(MVT::i1));
8689 else if (KnownZero == 0xffffff00)
8690 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8691 DAG.getValueType(MVT::i8));
8692 else if (KnownZero == 0xffff0000)
8693 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8694 DAG.getValueType(MVT::i16));
8695 }
8696
8697 return Res;
8698}
8699
Dan Gohman475871a2008-07-27 21:46:04 +00008700SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008701 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008702 switch (N->getOpcode()) {
8703 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008704 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008705 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008706 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008707 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008708 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8709 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008710 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008711 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008712 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008713 case ISD::STORE: return PerformSTORECombine(N, DCI);
8714 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8715 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008716 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008717 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008718 case ISD::FP_TO_SINT:
8719 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8720 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008721 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008722 case ISD::SHL:
8723 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008724 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008725 case ISD::SIGN_EXTEND:
8726 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008727 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8728 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008729 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008730 case ARMISD::VLD2DUP:
8731 case ARMISD::VLD3DUP:
8732 case ARMISD::VLD4DUP:
8733 return CombineBaseUpdate(N, DCI);
8734 case ISD::INTRINSIC_VOID:
8735 case ISD::INTRINSIC_W_CHAIN:
8736 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8737 case Intrinsic::arm_neon_vld1:
8738 case Intrinsic::arm_neon_vld2:
8739 case Intrinsic::arm_neon_vld3:
8740 case Intrinsic::arm_neon_vld4:
8741 case Intrinsic::arm_neon_vld2lane:
8742 case Intrinsic::arm_neon_vld3lane:
8743 case Intrinsic::arm_neon_vld4lane:
8744 case Intrinsic::arm_neon_vst1:
8745 case Intrinsic::arm_neon_vst2:
8746 case Intrinsic::arm_neon_vst3:
8747 case Intrinsic::arm_neon_vst4:
8748 case Intrinsic::arm_neon_vst2lane:
8749 case Intrinsic::arm_neon_vst3lane:
8750 case Intrinsic::arm_neon_vst4lane:
8751 return CombineBaseUpdate(N, DCI);
8752 default: break;
8753 }
8754 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008755 }
Dan Gohman475871a2008-07-27 21:46:04 +00008756 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008757}
8758
Evan Cheng31959b12011-02-02 01:06:55 +00008759bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8760 EVT VT) const {
8761 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8762}
8763
Bill Wendlingaf566342009-08-15 21:21:19 +00008764bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008765 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008766 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008767
8768 switch (VT.getSimpleVT().SimpleTy) {
8769 default:
8770 return false;
8771 case MVT::i8:
8772 case MVT::i16:
8773 case MVT::i32:
8774 return true;
8775 // FIXME: VLD1 etc with standard alignment is legal.
8776 }
8777}
8778
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008779static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8780 unsigned AlignCheck) {
8781 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8782 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8783}
8784
8785EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8786 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008787 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008788 bool MemcpyStrSrc,
8789 MachineFunction &MF) const {
8790 const Function *F = MF.getFunction();
8791
8792 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008793 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008794 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8795 Subtarget->hasNEON()) {
8796 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8797 return MVT::v4i32;
8798 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8799 return MVT::v2i32;
8800 }
8801 }
8802
Lang Hames5207bf22011-11-08 18:56:23 +00008803 // Lowering to i32/i16 if the size permits.
8804 if (Size >= 4) {
8805 return MVT::i32;
8806 } else if (Size >= 2) {
8807 return MVT::i16;
8808 }
8809
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008810 // Let the target-independent logic figure it out.
8811 return MVT::Other;
8812}
8813
Evan Chenge6c835f2009-08-14 20:09:37 +00008814static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8815 if (V < 0)
8816 return false;
8817
8818 unsigned Scale = 1;
8819 switch (VT.getSimpleVT().SimpleTy) {
8820 default: return false;
8821 case MVT::i1:
8822 case MVT::i8:
8823 // Scale == 1;
8824 break;
8825 case MVT::i16:
8826 // Scale == 2;
8827 Scale = 2;
8828 break;
8829 case MVT::i32:
8830 // Scale == 4;
8831 Scale = 4;
8832 break;
8833 }
8834
8835 if ((V & (Scale - 1)) != 0)
8836 return false;
8837 V /= Scale;
8838 return V == (V & ((1LL << 5) - 1));
8839}
8840
8841static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8842 const ARMSubtarget *Subtarget) {
8843 bool isNeg = false;
8844 if (V < 0) {
8845 isNeg = true;
8846 V = - V;
8847 }
8848
8849 switch (VT.getSimpleVT().SimpleTy) {
8850 default: return false;
8851 case MVT::i1:
8852 case MVT::i8:
8853 case MVT::i16:
8854 case MVT::i32:
8855 // + imm12 or - imm8
8856 if (isNeg)
8857 return V == (V & ((1LL << 8) - 1));
8858 return V == (V & ((1LL << 12) - 1));
8859 case MVT::f32:
8860 case MVT::f64:
8861 // Same as ARM mode. FIXME: NEON?
8862 if (!Subtarget->hasVFP2())
8863 return false;
8864 if ((V & 3) != 0)
8865 return false;
8866 V >>= 2;
8867 return V == (V & ((1LL << 8) - 1));
8868 }
8869}
8870
Evan Chengb01fad62007-03-12 23:30:29 +00008871/// isLegalAddressImmediate - Return true if the integer value can be used
8872/// as the offset of the target addressing mode for load / store of the
8873/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008874static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008875 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008876 if (V == 0)
8877 return true;
8878
Evan Cheng65011532009-03-09 19:15:00 +00008879 if (!VT.isSimple())
8880 return false;
8881
Evan Chenge6c835f2009-08-14 20:09:37 +00008882 if (Subtarget->isThumb1Only())
8883 return isLegalT1AddressImmediate(V, VT);
8884 else if (Subtarget->isThumb2())
8885 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008886
Evan Chenge6c835f2009-08-14 20:09:37 +00008887 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008888 if (V < 0)
8889 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008890 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008891 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008892 case MVT::i1:
8893 case MVT::i8:
8894 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008895 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008896 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008897 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008898 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008899 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008900 case MVT::f32:
8901 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008902 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008903 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008904 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008905 return false;
8906 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008907 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008908 }
Evan Chenga8e29892007-01-19 07:51:42 +00008909}
8910
Evan Chenge6c835f2009-08-14 20:09:37 +00008911bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8912 EVT VT) const {
8913 int Scale = AM.Scale;
8914 if (Scale < 0)
8915 return false;
8916
8917 switch (VT.getSimpleVT().SimpleTy) {
8918 default: return false;
8919 case MVT::i1:
8920 case MVT::i8:
8921 case MVT::i16:
8922 case MVT::i32:
8923 if (Scale == 1)
8924 return true;
8925 // r + r << imm
8926 Scale = Scale & ~1;
8927 return Scale == 2 || Scale == 4 || Scale == 8;
8928 case MVT::i64:
8929 // r + r
8930 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8931 return true;
8932 return false;
8933 case MVT::isVoid:
8934 // Note, we allow "void" uses (basically, uses that aren't loads or
8935 // stores), because arm allows folding a scale into many arithmetic
8936 // operations. This should be made more precise and revisited later.
8937
8938 // Allow r << imm, but the imm has to be a multiple of two.
8939 if (Scale & 1) return false;
8940 return isPowerOf2_32(Scale);
8941 }
8942}
8943
Chris Lattner37caf8c2007-04-09 23:33:39 +00008944/// isLegalAddressingMode - Return true if the addressing mode represented
8945/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008946bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008947 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008948 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008949 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008950 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008951
Chris Lattner37caf8c2007-04-09 23:33:39 +00008952 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008953 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008954 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008955
Chris Lattner37caf8c2007-04-09 23:33:39 +00008956 switch (AM.Scale) {
8957 case 0: // no scale reg, must be "r+i" or "r", or "i".
8958 break;
8959 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008960 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008961 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008962 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008963 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008964 // ARM doesn't support any R+R*scale+imm addr modes.
8965 if (AM.BaseOffs)
8966 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008967
Bob Wilson2c7dab12009-04-08 17:55:28 +00008968 if (!VT.isSimple())
8969 return false;
8970
Evan Chenge6c835f2009-08-14 20:09:37 +00008971 if (Subtarget->isThumb2())
8972 return isLegalT2ScaledAddressingMode(AM, VT);
8973
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008974 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008975 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008976 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008977 case MVT::i1:
8978 case MVT::i8:
8979 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008980 if (Scale < 0) Scale = -Scale;
8981 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008982 return true;
8983 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008984 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008985 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008986 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008987 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008988 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008989 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008990 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008991
Owen Anderson825b72b2009-08-11 20:47:22 +00008992 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008993 // Note, we allow "void" uses (basically, uses that aren't loads or
8994 // stores), because arm allows folding a scale into many arithmetic
8995 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008996
Chris Lattner37caf8c2007-04-09 23:33:39 +00008997 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008998 if (Scale & 1) return false;
8999 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009000 }
Evan Chengb01fad62007-03-12 23:30:29 +00009001 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009002 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009003}
9004
Evan Cheng77e47512009-11-11 19:05:52 +00009005/// isLegalICmpImmediate - Return true if the specified immediate is legal
9006/// icmp immediate, that is the target has icmp instructions which can compare
9007/// a register against the immediate without having to materialize the
9008/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009009bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009010 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009011 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009012 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009013 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009014 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009015 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009016 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009017}
9018
Dan Gohmancca82142011-05-03 00:46:49 +00009019/// isLegalAddImmediate - Return true if the specified immediate is legal
9020/// add immediate, that is the target has add instructions which can add
9021/// a register with the immediate without having to materialize the
9022/// immediate into a register.
9023bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
9024 return ARM_AM::getSOImmVal(Imm) != -1;
9025}
9026
Owen Andersone50ed302009-08-10 22:56:29 +00009027static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009028 bool isSEXTLoad, SDValue &Base,
9029 SDValue &Offset, bool &isInc,
9030 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009031 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9032 return false;
9033
Owen Anderson825b72b2009-08-11 20:47:22 +00009034 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009035 // AddressingMode 3
9036 Base = Ptr->getOperand(0);
9037 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009038 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009039 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009040 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009041 isInc = false;
9042 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9043 return true;
9044 }
9045 }
9046 isInc = (Ptr->getOpcode() == ISD::ADD);
9047 Offset = Ptr->getOperand(1);
9048 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009049 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009050 // AddressingMode 2
9051 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009052 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009053 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009054 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009055 isInc = false;
9056 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9057 Base = Ptr->getOperand(0);
9058 return true;
9059 }
9060 }
9061
9062 if (Ptr->getOpcode() == ISD::ADD) {
9063 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009064 ARM_AM::ShiftOpc ShOpcVal=
9065 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009066 if (ShOpcVal != ARM_AM::no_shift) {
9067 Base = Ptr->getOperand(1);
9068 Offset = Ptr->getOperand(0);
9069 } else {
9070 Base = Ptr->getOperand(0);
9071 Offset = Ptr->getOperand(1);
9072 }
9073 return true;
9074 }
9075
9076 isInc = (Ptr->getOpcode() == ISD::ADD);
9077 Base = Ptr->getOperand(0);
9078 Offset = Ptr->getOperand(1);
9079 return true;
9080 }
9081
Jim Grosbache5165492009-11-09 00:11:35 +00009082 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009083 return false;
9084}
9085
Owen Andersone50ed302009-08-10 22:56:29 +00009086static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009087 bool isSEXTLoad, SDValue &Base,
9088 SDValue &Offset, bool &isInc,
9089 SelectionDAG &DAG) {
9090 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9091 return false;
9092
9093 Base = Ptr->getOperand(0);
9094 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9095 int RHSC = (int)RHS->getZExtValue();
9096 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9097 assert(Ptr->getOpcode() == ISD::ADD);
9098 isInc = false;
9099 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9100 return true;
9101 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9102 isInc = Ptr->getOpcode() == ISD::ADD;
9103 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9104 return true;
9105 }
9106 }
9107
9108 return false;
9109}
9110
Evan Chenga8e29892007-01-19 07:51:42 +00009111/// getPreIndexedAddressParts - returns true by value, base pointer and
9112/// offset pointer and addressing mode by reference if the node's address
9113/// can be legally represented as pre-indexed load / store address.
9114bool
Dan Gohman475871a2008-07-27 21:46:04 +00009115ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9116 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009117 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009118 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009119 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009120 return false;
9121
Owen Andersone50ed302009-08-10 22:56:29 +00009122 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009123 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009124 bool isSEXTLoad = false;
9125 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9126 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009127 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009128 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9129 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9130 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009131 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009132 } else
9133 return false;
9134
9135 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009136 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009137 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009138 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9139 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009140 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009141 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009142 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009143 if (!isLegal)
9144 return false;
9145
9146 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9147 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009148}
9149
9150/// getPostIndexedAddressParts - returns true by value, base pointer and
9151/// offset pointer and addressing mode by reference if this node can be
9152/// combined with a load / store to form a post-indexed load / store.
9153bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009154 SDValue &Base,
9155 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009156 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009157 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009158 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009159 return false;
9160
Owen Andersone50ed302009-08-10 22:56:29 +00009161 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009162 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009163 bool isSEXTLoad = false;
9164 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009165 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009166 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009167 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9168 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009169 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009170 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009171 } else
9172 return false;
9173
9174 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009175 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009176 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009177 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009178 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009179 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009180 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9181 isInc, DAG);
9182 if (!isLegal)
9183 return false;
9184
Evan Cheng28dad2a2010-05-18 21:31:17 +00009185 if (Ptr != Base) {
9186 // Swap base ptr and offset to catch more post-index load / store when
9187 // it's legal. In Thumb2 mode, offset must be an immediate.
9188 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9189 !Subtarget->isThumb2())
9190 std::swap(Base, Offset);
9191
9192 // Post-indexed load / store update the base pointer.
9193 if (Ptr != Base)
9194 return false;
9195 }
9196
Evan Chenge88d5ce2009-07-02 07:28:31 +00009197 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9198 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009199}
9200
Dan Gohman475871a2008-07-27 21:46:04 +00009201void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009202 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009203 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009204 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009205 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009206 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009207 switch (Op.getOpcode()) {
9208 default: break;
9209 case ARMISD::CMOV: {
9210 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009211 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009212 if (KnownZero == 0 && KnownOne == 0) return;
9213
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009214 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009215 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009216 KnownZero &= KnownZeroRHS;
9217 KnownOne &= KnownOneRHS;
9218 return;
9219 }
9220 }
9221}
9222
9223//===----------------------------------------------------------------------===//
9224// ARM Inline Assembly Support
9225//===----------------------------------------------------------------------===//
9226
Evan Cheng55d42002011-01-08 01:24:27 +00009227bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9228 // Looking for "rev" which is V6+.
9229 if (!Subtarget->hasV6Ops())
9230 return false;
9231
9232 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9233 std::string AsmStr = IA->getAsmString();
9234 SmallVector<StringRef, 4> AsmPieces;
9235 SplitString(AsmStr, AsmPieces, ";\n");
9236
9237 switch (AsmPieces.size()) {
9238 default: return false;
9239 case 1:
9240 AsmStr = AsmPieces[0];
9241 AsmPieces.clear();
9242 SplitString(AsmStr, AsmPieces, " \t,");
9243
9244 // rev $0, $1
9245 if (AsmPieces.size() == 3 &&
9246 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9247 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009248 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009249 if (Ty && Ty->getBitWidth() == 32)
9250 return IntrinsicLowering::LowerToByteSwap(CI);
9251 }
9252 break;
9253 }
9254
9255 return false;
9256}
9257
Evan Chenga8e29892007-01-19 07:51:42 +00009258/// getConstraintType - Given a constraint letter, return the type of
9259/// constraint it is for this target.
9260ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009261ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9262 if (Constraint.size() == 1) {
9263 switch (Constraint[0]) {
9264 default: break;
9265 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009266 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009267 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009268 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009269 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009270 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009271 // An address with a single base register. Due to the way we
9272 // currently handle addresses it is the same as an 'r' memory constraint.
9273 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009274 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009275 } else if (Constraint.size() == 2) {
9276 switch (Constraint[0]) {
9277 default: break;
9278 // All 'U+' constraints are addresses.
9279 case 'U': return C_Memory;
9280 }
Evan Chenga8e29892007-01-19 07:51:42 +00009281 }
Chris Lattner4234f572007-03-25 02:14:49 +00009282 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009283}
9284
John Thompson44ab89e2010-10-29 17:29:13 +00009285/// Examine constraint type and operand type and determine a weight value.
9286/// This object must already have been set up with the operand type
9287/// and the current alternative constraint selected.
9288TargetLowering::ConstraintWeight
9289ARMTargetLowering::getSingleConstraintMatchWeight(
9290 AsmOperandInfo &info, const char *constraint) const {
9291 ConstraintWeight weight = CW_Invalid;
9292 Value *CallOperandVal = info.CallOperandVal;
9293 // If we don't have a value, we can't do a match,
9294 // but allow it at the lowest weight.
9295 if (CallOperandVal == NULL)
9296 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009297 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009298 // Look at the constraint type.
9299 switch (*constraint) {
9300 default:
9301 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9302 break;
9303 case 'l':
9304 if (type->isIntegerTy()) {
9305 if (Subtarget->isThumb())
9306 weight = CW_SpecificReg;
9307 else
9308 weight = CW_Register;
9309 }
9310 break;
9311 case 'w':
9312 if (type->isFloatingPointTy())
9313 weight = CW_Register;
9314 break;
9315 }
9316 return weight;
9317}
9318
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009319typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9320RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009321ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009322 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009323 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009324 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009325 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009326 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009327 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009328 return RCPair(0U, &ARM::tGPRRegClass);
9329 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009330 case 'h': // High regs or no regs.
9331 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009332 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009333 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009334 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009335 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009336 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009337 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009338 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009339 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009340 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009341 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009342 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009343 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009344 case 'x':
9345 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009346 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009347 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009348 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009349 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009350 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009351 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009352 case 't':
9353 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009354 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009355 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009356 }
9357 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009358 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009359 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009360
Evan Chenga8e29892007-01-19 07:51:42 +00009361 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9362}
9363
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009364/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9365/// vector. If it is invalid, don't add anything to Ops.
9366void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009367 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009368 std::vector<SDValue>&Ops,
9369 SelectionDAG &DAG) const {
9370 SDValue Result(0, 0);
9371
Eric Christopher100c8332011-06-02 23:16:42 +00009372 // Currently only support length 1 constraints.
9373 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009374
Eric Christopher100c8332011-06-02 23:16:42 +00009375 char ConstraintLetter = Constraint[0];
9376 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009377 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009378 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009379 case 'I': case 'J': case 'K': case 'L':
9380 case 'M': case 'N': case 'O':
9381 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9382 if (!C)
9383 return;
9384
9385 int64_t CVal64 = C->getSExtValue();
9386 int CVal = (int) CVal64;
9387 // None of these constraints allow values larger than 32 bits. Check
9388 // that the value fits in an int.
9389 if (CVal != CVal64)
9390 return;
9391
Eric Christopher100c8332011-06-02 23:16:42 +00009392 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009393 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009394 // Constant suitable for movw, must be between 0 and
9395 // 65535.
9396 if (Subtarget->hasV6T2Ops())
9397 if (CVal >= 0 && CVal <= 65535)
9398 break;
9399 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009400 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009401 if (Subtarget->isThumb1Only()) {
9402 // This must be a constant between 0 and 255, for ADD
9403 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009404 if (CVal >= 0 && CVal <= 255)
9405 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009406 } else if (Subtarget->isThumb2()) {
9407 // A constant that can be used as an immediate value in a
9408 // data-processing instruction.
9409 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9410 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009411 } else {
9412 // A constant that can be used as an immediate value in a
9413 // data-processing instruction.
9414 if (ARM_AM::getSOImmVal(CVal) != -1)
9415 break;
9416 }
9417 return;
9418
9419 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009420 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009421 // This must be a constant between -255 and -1, for negated ADD
9422 // immediates. This can be used in GCC with an "n" modifier that
9423 // prints the negated value, for use with SUB instructions. It is
9424 // not useful otherwise but is implemented for compatibility.
9425 if (CVal >= -255 && CVal <= -1)
9426 break;
9427 } else {
9428 // This must be a constant between -4095 and 4095. It is not clear
9429 // what this constraint is intended for. Implemented for
9430 // compatibility with GCC.
9431 if (CVal >= -4095 && CVal <= 4095)
9432 break;
9433 }
9434 return;
9435
9436 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009437 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009438 // A 32-bit value where only one byte has a nonzero value. Exclude
9439 // zero to match GCC. This constraint is used by GCC internally for
9440 // constants that can be loaded with a move/shift combination.
9441 // It is not useful otherwise but is implemented for compatibility.
9442 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9443 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009444 } else if (Subtarget->isThumb2()) {
9445 // A constant whose bitwise inverse can be used as an immediate
9446 // value in a data-processing instruction. This can be used in GCC
9447 // with a "B" modifier that prints the inverted value, for use with
9448 // BIC and MVN instructions. It is not useful otherwise but is
9449 // implemented for compatibility.
9450 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9451 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009452 } else {
9453 // A constant whose bitwise inverse can be used as an immediate
9454 // value in a data-processing instruction. This can be used in GCC
9455 // with a "B" modifier that prints the inverted value, for use with
9456 // BIC and MVN instructions. It is not useful otherwise but is
9457 // implemented for compatibility.
9458 if (ARM_AM::getSOImmVal(~CVal) != -1)
9459 break;
9460 }
9461 return;
9462
9463 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009464 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009465 // This must be a constant between -7 and 7,
9466 // for 3-operand ADD/SUB immediate instructions.
9467 if (CVal >= -7 && CVal < 7)
9468 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009469 } else if (Subtarget->isThumb2()) {
9470 // A constant whose negation can be used as an immediate value in a
9471 // data-processing instruction. This can be used in GCC with an "n"
9472 // modifier that prints the negated value, for use with SUB
9473 // instructions. It is not useful otherwise but is implemented for
9474 // compatibility.
9475 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9476 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009477 } else {
9478 // A constant whose negation can be used as an immediate value in a
9479 // data-processing instruction. This can be used in GCC with an "n"
9480 // modifier that prints the negated value, for use with SUB
9481 // instructions. It is not useful otherwise but is implemented for
9482 // compatibility.
9483 if (ARM_AM::getSOImmVal(-CVal) != -1)
9484 break;
9485 }
9486 return;
9487
9488 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009489 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009490 // This must be a multiple of 4 between 0 and 1020, for
9491 // ADD sp + immediate.
9492 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9493 break;
9494 } else {
9495 // A power of two or a constant between 0 and 32. This is used in
9496 // GCC for the shift amount on shifted register operands, but it is
9497 // useful in general for any shift amounts.
9498 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9499 break;
9500 }
9501 return;
9502
9503 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009504 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009505 // This must be a constant between 0 and 31, for shift amounts.
9506 if (CVal >= 0 && CVal <= 31)
9507 break;
9508 }
9509 return;
9510
9511 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009512 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009513 // This must be a multiple of 4 between -508 and 508, for
9514 // ADD/SUB sp = sp + immediate.
9515 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9516 break;
9517 }
9518 return;
9519 }
9520 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9521 break;
9522 }
9523
9524 if (Result.getNode()) {
9525 Ops.push_back(Result);
9526 return;
9527 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009528 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009529}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009530
9531bool
9532ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9533 // The ARM target isn't yet aware of offsets.
9534 return false;
9535}
Evan Cheng39382422009-10-28 01:44:26 +00009536
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009537bool ARM::isBitFieldInvertedMask(unsigned v) {
9538 if (v == 0xffffffff)
9539 return 0;
9540 // there can be 1's on either or both "outsides", all the "inside"
9541 // bits must be 0's
9542 unsigned int lsb = 0, msb = 31;
9543 while (v & (1 << msb)) --msb;
9544 while (v & (1 << lsb)) ++lsb;
9545 for (unsigned int i = lsb; i <= msb; ++i) {
9546 if (v & (1 << i))
9547 return 0;
9548 }
9549 return 1;
9550}
9551
Evan Cheng39382422009-10-28 01:44:26 +00009552/// isFPImmLegal - Returns true if the target can instruction select the
9553/// specified FP immediate natively. If false, the legalizer will
9554/// materialize the FP immediate as a load from a constant pool.
9555bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9556 if (!Subtarget->hasVFP3())
9557 return false;
9558 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009559 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009560 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009561 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009562 return false;
9563}
Bob Wilson65ffec42010-09-21 17:56:22 +00009564
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009565/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009566/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9567/// specified in the intrinsic calls.
9568bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9569 const CallInst &I,
9570 unsigned Intrinsic) const {
9571 switch (Intrinsic) {
9572 case Intrinsic::arm_neon_vld1:
9573 case Intrinsic::arm_neon_vld2:
9574 case Intrinsic::arm_neon_vld3:
9575 case Intrinsic::arm_neon_vld4:
9576 case Intrinsic::arm_neon_vld2lane:
9577 case Intrinsic::arm_neon_vld3lane:
9578 case Intrinsic::arm_neon_vld4lane: {
9579 Info.opc = ISD::INTRINSIC_W_CHAIN;
9580 // Conservatively set memVT to the entire set of vectors loaded.
9581 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9582 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9583 Info.ptrVal = I.getArgOperand(0);
9584 Info.offset = 0;
9585 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9586 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9587 Info.vol = false; // volatile loads with NEON intrinsics not supported
9588 Info.readMem = true;
9589 Info.writeMem = false;
9590 return true;
9591 }
9592 case Intrinsic::arm_neon_vst1:
9593 case Intrinsic::arm_neon_vst2:
9594 case Intrinsic::arm_neon_vst3:
9595 case Intrinsic::arm_neon_vst4:
9596 case Intrinsic::arm_neon_vst2lane:
9597 case Intrinsic::arm_neon_vst3lane:
9598 case Intrinsic::arm_neon_vst4lane: {
9599 Info.opc = ISD::INTRINSIC_VOID;
9600 // Conservatively set memVT to the entire set of vectors stored.
9601 unsigned NumElts = 0;
9602 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009603 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009604 if (!ArgTy->isVectorTy())
9605 break;
9606 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9607 }
9608 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9609 Info.ptrVal = I.getArgOperand(0);
9610 Info.offset = 0;
9611 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9612 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9613 Info.vol = false; // volatile stores with NEON intrinsics not supported
9614 Info.readMem = false;
9615 Info.writeMem = true;
9616 return true;
9617 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009618 case Intrinsic::arm_strexd: {
9619 Info.opc = ISD::INTRINSIC_W_CHAIN;
9620 Info.memVT = MVT::i64;
9621 Info.ptrVal = I.getArgOperand(2);
9622 Info.offset = 0;
9623 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009624 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009625 Info.readMem = false;
9626 Info.writeMem = true;
9627 return true;
9628 }
9629 case Intrinsic::arm_ldrexd: {
9630 Info.opc = ISD::INTRINSIC_W_CHAIN;
9631 Info.memVT = MVT::i64;
9632 Info.ptrVal = I.getArgOperand(0);
9633 Info.offset = 0;
9634 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009635 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009636 Info.readMem = true;
9637 Info.writeMem = false;
9638 return true;
9639 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009640 default:
9641 break;
9642 }
9643
9644 return false;
9645}