blob: 6094811caa5d65e618185d4d16496a0eb2864e06 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Craig Topperc1f6f422012-03-17 07:33:42 +000016#include "ARMISelLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000031#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000032#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000033#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000034#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000035#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/MachineBasicBlock.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000043#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000045#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000046#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000047#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000050#include "llvm/Support/raw_ostream.h"
Evan Chenga8e29892007-01-19 07:51:42 +000051using namespace llvm;
52
Dale Johannesen51e28e62010-06-03 21:09:53 +000053STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000054STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren763a75d2012-06-01 02:44:42 +000055STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesen51e28e62010-06-03 21:09:53 +000056
Bob Wilson703af3a2010-08-13 22:43:33 +000057// This option should go away when tail calls fully work.
58static cl::opt<bool>
59EnableARMTailCalls("arm-tail-calls", cl::Hidden,
60 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
61 cl::init(false));
62
Eric Christopher836c6242010-12-15 23:47:29 +000063cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000064EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000065 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000066 cl::init(false));
67
Evan Cheng46df4eb2010-06-16 07:35:02 +000068static cl::opt<bool>
69ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
71 cl::init(true));
72
Benjamin Kramer0861f572011-11-26 23:01:57 +000073namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000074 class ARMCCState : public CCState {
75 public:
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
77 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
78 LLVMContext &C, ParmContext PC)
79 : CCState(CC, isVarArg, MF, TM, locs, C) {
80 assert(((PC == Call) || (PC == Prologue)) &&
81 "ARMCCState users must specify whether their context is call"
82 "or prologue generation.");
83 CallOrPrologue = PC;
84 }
85 };
86}
87
Stuart Hastingsc7315872011-04-20 16:47:52 +000088// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000089static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000090 ARM::R0, ARM::R1, ARM::R2, ARM::R3
91};
92
Owen Andersone50ed302009-08-10 22:56:29 +000093void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
94 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000096 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000097 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
98 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000099
Owen Anderson70671842009-08-10 20:18:46 +0000100 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000101 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000102 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000103 }
104
Owen Andersone50ed302009-08-10 22:56:29 +0000105 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000107 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000108 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000109 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000110 if (ElemTy == MVT::i32) {
111 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
112 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
115 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000116 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
120 }
Owen Anderson70671842009-08-10 20:18:46 +0000121 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
122 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000123 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000124 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000125 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000128 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000132 }
133
134 // Promote all bit-wise operations.
135 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000136 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000137 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
138 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000139 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000140 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000141 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000142 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000143 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000144 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000145 }
Bob Wilson16330762009-09-16 00:17:28 +0000146
147 // Neon does not support vector divide/remainder operations.
148 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
149 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Owen Andersone50ed302009-08-10 22:56:29 +0000156void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000157 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Owen Andersone50ed302009-08-10 22:56:29 +0000161void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000162 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000164}
165
Chris Lattnerf0144122009-07-28 03:13:23 +0000166static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
167 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000168 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000169
Chris Lattner80ec2792009-08-02 00:34:36 +0000170 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000171}
172
Evan Chenga8e29892007-01-19 07:51:42 +0000173ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000174 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000175 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000176 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000177 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000178
Duncan Sands28b77e92011-09-06 19:07:46 +0000179 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Evan Cheng07043272012-02-21 20:46:00 +0000263 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000388 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
389 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
390 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
391 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
397
398 // Integer division functions
399 // RTABI chapter 4.3.1
400 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
401 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
402 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000403 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000404 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
405 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000407 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000411 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000412 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000414 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000415 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000422 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
423 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000425 }
426
Bob Wilson2fef4572011-10-07 16:59:21 +0000427 // Use divmod compiler-rt calls for iOS 5.0 and later.
428 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
429 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
430 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
431 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
432 }
433
David Goodwinf1daf7d2009-07-08 23:10:31 +0000434 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000435 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000436 else
Craig Topper420761a2012-04-20 07:30:17 +0000437 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000438 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
439 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000440 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000441 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000442 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000443
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000445 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000446
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000447 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
448 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
449 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
450 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
451 setTruncStoreAction((MVT::SimpleValueType)VT,
452 (MVT::SimpleValueType)InnerVT, Expand);
453 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
454 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
456 }
457
Lang Hames45b5f882012-03-15 18:49:02 +0000458 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
459
Bob Wilson5bafff32009-06-22 23:27:02 +0000460 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 addDRTypeForNEON(MVT::v2f32);
462 addDRTypeForNEON(MVT::v8i8);
463 addDRTypeForNEON(MVT::v4i16);
464 addDRTypeForNEON(MVT::v2i32);
465 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000466
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 addQRTypeForNEON(MVT::v4f32);
468 addQRTypeForNEON(MVT::v2f64);
469 addQRTypeForNEON(MVT::v16i8);
470 addQRTypeForNEON(MVT::v8i16);
471 addQRTypeForNEON(MVT::v4i32);
472 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000473
Bob Wilson74dc72e2009-09-15 23:55:57 +0000474 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
475 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
477 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
479 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
480 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000481 // FIXME: Code duplication: FDIV and FREM are expanded always, see
482 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
484 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000485 // FIXME: Create unittest.
486 // In another words, find a way when "copysign" appears in DAG with vector
487 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000489 // FIXME: Code duplication: SETCC has custom operation action, see
490 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000491 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000492 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000493 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
494 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
496 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
497 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
499 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
504 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000505 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000506 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
507 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
508 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
510 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000511
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000512 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
513 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
514 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
515 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
516 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
519 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
520 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
521 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000522
Bob Wilson642b3292009-09-16 00:32:15 +0000523 // Neon does not support some operations on v1i64 and v2i64 types.
524 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000525 // Custom handling for some quad-vector types to detect VMULL.
526 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
527 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
528 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000529 // Custom handling for some vector types to avoid expensive expansions
530 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
531 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
532 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
533 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000534 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
535 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000536 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000537 // a destination type that is wider than the source, and nor does
538 // it have a FP_TO_[SU]INT instruction with a narrower destination than
539 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000540 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
541 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000542 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
543 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000544
Bob Wilson1c3ef902011-02-07 17:43:21 +0000545 setTargetDAGCombine(ISD::INTRINSIC_VOID);
546 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000547 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
548 setTargetDAGCombine(ISD::SHL);
549 setTargetDAGCombine(ISD::SRL);
550 setTargetDAGCombine(ISD::SRA);
551 setTargetDAGCombine(ISD::SIGN_EXTEND);
552 setTargetDAGCombine(ISD::ZERO_EXTEND);
553 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000554 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000555 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000556 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000557 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
558 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000559 setTargetDAGCombine(ISD::FP_TO_SINT);
560 setTargetDAGCombine(ISD::FP_TO_UINT);
561 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000562
James Molloy873fd5f2012-02-20 09:24:05 +0000563 // It is legal to extload from v4i8 to v4i16 or v4i32.
564 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
565 MVT::v4i16, MVT::v2i16,
566 MVT::v2i32};
567 for (unsigned i = 0; i < 6; ++i) {
568 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
569 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
570 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
571 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000572 }
573
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000574 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000575
576 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000578
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000579 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000583 if (!Subtarget->isThumb1Only()) {
584 for (unsigned im = (unsigned)ISD::PRE_INC;
585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setIndexedLoadAction(im, MVT::i1, Legal);
587 setIndexedLoadAction(im, MVT::i8, Legal);
588 setIndexedLoadAction(im, MVT::i16, Legal);
589 setIndexedLoadAction(im, MVT::i32, Legal);
590 setIndexedStoreAction(im, MVT::i1, Legal);
591 setIndexedStoreAction(im, MVT::i8, Legal);
592 setIndexedStoreAction(im, MVT::i16, Legal);
593 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595 }
596
597 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000598 setOperationAction(ISD::MUL, MVT::i64, Expand);
599 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000600 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000606 setOperationAction(ISD::MULHS, MVT::i32, Expand);
607
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SRL, MVT::i64, Custom);
612 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Evan Cheng342e3162011-08-30 01:34:54 +0000614 if (!Subtarget->isThumb1Only()) {
615 // FIXME: We should do this for Thumb1 as well.
616 setOperationAction(ISD::ADDC, MVT::i32, Custom);
617 setOperationAction(ISD::ADDE, MVT::i32, Custom);
618 setOperationAction(ISD::SUBC, MVT::i32, Custom);
619 setOperationAction(ISD::SUBE, MVT::i32, Custom);
620 }
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000624 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Chandler Carruth63974b22011-12-13 01:56:10 +0000629 // These just redirect to CTTZ and CTLZ on ARM.
630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
632
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000633 // Only ARMv6 has BSWAP.
634 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000639 // v7M has a hardware divider
640 setOperationAction(ISD::SDIV, MVT::i32, Expand);
641 setOperationAction(ISD::UDIV, MVT::i32, Expand);
642 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::UREM, MVT::i32, Expand);
645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000654 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000655
Evan Chenga8e29892007-01-19 07:51:42 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VASTART, MVT::Other, Custom);
658 setOperationAction(ISD::VAARG, MVT::Other, Expand);
659 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
660 setOperationAction(ISD::VAEND, MVT::Other, Expand);
661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000663
664 if (!Subtarget->isTargetDarwin()) {
665 // Non-Darwin platforms may return values in these registers via the
666 // personality function.
667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
669 setExceptionPointerRegister(ARM::R0);
670 setExceptionSelectorRegister(ARM::R1);
671 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000672
Evan Cheng3a1588a2010-04-15 22:20:34 +0000673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
675 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000676 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000677 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 // membarrier needs custom lowering; the rest are legal and handled
680 // normally.
681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000683 // Custom lowering for 64-bit ops
684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
692 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 } else {
694 // Set them all for expansion, which will force libcalls.
695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
710 // Unordered/Monotonic case.
711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000713 // Since the libcalls include locking, fold in the fences
714 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 }
Evan Chenga8e29892007-01-19 07:51:42 +0000716
Evan Cheng416941d2010-11-04 05:19:35 +0000717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000718
Eli Friedmana2c6f452010-06-26 04:36:50 +0000719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
720 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000723 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
727 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
729 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
732 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000733
734 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000736 if (Subtarget->isTargetDarwin()) {
737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000740 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SETCC, MVT::i32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000745 setOperationAction(ISD::SELECT, MVT::i32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
753 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
756 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000757
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000758 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN, MVT::f64, Expand);
760 setOperationAction(ISD::FSIN, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000769 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::FPOW, MVT::f64, Expand);
771 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000773 if (!Subtarget->hasVFP4()) {
774 setOperationAction(ISD::FMA, MVT::f64, Expand);
775 setOperationAction(ISD::FMA, MVT::f32, Expand);
776 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000777
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000778 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000779 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000780 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
781 if (Subtarget->hasVFP2()) {
782 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
783 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
784 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
785 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
786 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000787 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000788 if (!Subtarget->hasFP16()) {
789 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
790 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000791 }
Evan Cheng110cf482008-04-01 01:50:16 +0000792 }
Evan Chenga8e29892007-01-19 07:51:42 +0000793
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000794 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000795 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000796 setTargetDAGCombine(ISD::ADD);
797 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000798 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000799
Evan Chengc892aeb2012-02-23 01:19:06 +0000800 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000801 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000802 setTargetDAGCombine(ISD::OR);
803 setTargetDAGCombine(ISD::XOR);
804 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000805
Evan Cheng5fb468a2012-02-23 02:58:19 +0000806 if (Subtarget->hasV6Ops())
807 setTargetDAGCombine(ISD::SRL);
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000810
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000811 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
812 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000813 setSchedulingPreference(Sched::RegPressure);
814 else
815 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000816
Evan Cheng05219282011-01-06 06:52:41 +0000817 //// temporary - rewrite interface to use type
818 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000819 maxStoresPerMemset = 16;
820 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000821
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000822 // On ARM arguments smaller than 4 bytes are extended, so all arguments
823 // are at least 4 bytes aligned.
824 setMinStackArgumentAlignment(4);
825
Evan Chengfff606d2010-09-24 19:07:23 +0000826 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000827
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000828 // Prefer likely predicted branches to selects on out-of-order cores.
829 predictableSelectIsExpensive = Subtarget->isCortexA9();
830
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000831 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000832}
833
Andrew Trick32cec0a2011-01-19 02:35:27 +0000834// FIXME: It might make sense to define the representative register class as the
835// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
836// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
837// SPR's representative would be DPR_VFP2. This should work well if register
838// pressure tracking were modified such that a register use would increment the
839// pressure of the register class's representative and all of it's super
840// classes' representatives transitively. We have not implemented this because
841// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000842// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000843// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844std::pair<const TargetRegisterClass*, uint8_t>
845ARMTargetLowering::findRepresentativeClass(EVT VT) const{
846 const TargetRegisterClass *RRC = 0;
847 uint8_t Cost = 1;
848 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000849 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000851 // Use DPR as representative register class for all floating point
852 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
853 // the cost is 1 for both f32 and f64.
854 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000855 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000856 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000857 // When NEON is used for SP, only half of the register file is available
858 // because operations that define both SP and DP results will be constrained
859 // to the VFP2 class (D0-D15). We currently model this constraint prior to
860 // coalescing by double-counting the SP regs. See the FIXME above.
861 if (Subtarget->useNEONForSinglePrecisionFP())
862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
865 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000867 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000870 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000871 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 break;
873 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000874 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000875 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000876 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000877 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000879}
880
Evan Chenga8e29892007-01-19 07:51:42 +0000881const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
882 switch (Opcode) {
883 default: return 0;
884 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000885 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000886 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000887 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
888 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000889 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
891 case ARMISD::tCALL: return "ARMISD::tCALL";
892 case ARMISD::BRCOND: return "ARMISD::BRCOND";
893 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000894 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
896 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
897 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000898 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000899 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::CMPFP: return "ARMISD::CMPFP";
901 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000902 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000903 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000904
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000906 case ARMISD::CAND: return "ARMISD::CAND";
907 case ARMISD::COR: return "ARMISD::COR";
908 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000909
Jim Grosbach3482c802010-01-18 19:58:49 +0000910 case ARMISD::RBIT: return "ARMISD::RBIT";
911
Bob Wilson76a312b2010-03-19 22:51:32 +0000912 case ARMISD::FTOSI: return "ARMISD::FTOSI";
913 case ARMISD::FTOUI: return "ARMISD::FTOUI";
914 case ARMISD::SITOF: return "ARMISD::SITOF";
915 case ARMISD::UITOF: return "ARMISD::UITOF";
916
Evan Chenga8e29892007-01-19 07:51:42 +0000917 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
918 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
919 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000920
Evan Cheng342e3162011-08-30 01:34:54 +0000921 case ARMISD::ADDC: return "ARMISD::ADDC";
922 case ARMISD::ADDE: return "ARMISD::ADDE";
923 case ARMISD::SUBC: return "ARMISD::SUBC";
924 case ARMISD::SUBE: return "ARMISD::SUBE";
925
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000926 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
927 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000928
Evan Chengc5942082009-10-28 06:55:03 +0000929 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
930 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
931
Dale Johannesen51e28e62010-06-03 21:09:53 +0000932 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000933
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000934 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000935
Evan Cheng86198642009-08-07 00:34:42 +0000936 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
937
Jim Grosbach3728e962009-12-10 00:11:09 +0000938 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000939 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000940
Evan Chengdfed19f2010-11-03 06:34:55 +0000941 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
942
Bob Wilson5bafff32009-06-22 23:27:02 +0000943 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000944 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000946 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
947 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 case ARMISD::VCGEU: return "ARMISD::VCGEU";
949 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000950 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
951 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000952 case ARMISD::VCGTU: return "ARMISD::VCGTU";
953 case ARMISD::VTST: return "ARMISD::VTST";
954
955 case ARMISD::VSHL: return "ARMISD::VSHL";
956 case ARMISD::VSHRs: return "ARMISD::VSHRs";
957 case ARMISD::VSHRu: return "ARMISD::VSHRu";
958 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
959 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
960 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
961 case ARMISD::VSHRN: return "ARMISD::VSHRN";
962 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
963 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
964 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
965 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
966 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
967 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
968 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
969 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
970 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
971 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
972 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
973 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
974 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
975 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000976 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000977 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000978 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000979 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000980 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000981 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000982 case ARMISD::VREV64: return "ARMISD::VREV64";
983 case ARMISD::VREV32: return "ARMISD::VREV32";
984 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000985 case ARMISD::VZIP: return "ARMISD::VZIP";
986 case ARMISD::VUZP: return "ARMISD::VUZP";
987 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000988 case ARMISD::VTBL1: return "ARMISD::VTBL1";
989 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000990 case ARMISD::VMULLs: return "ARMISD::VMULLs";
991 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000992 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000993 case ARMISD::FMAX: return "ARMISD::FMAX";
994 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000995 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000996 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
997 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000998 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000999 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1000 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1001 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001002 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1003 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1004 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1005 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1006 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1007 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1008 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1009 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1010 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1011 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1012 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1013 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1014 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1015 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1016 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1017 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1018 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001019 }
1020}
1021
Duncan Sands28b77e92011-09-06 19:07:46 +00001022EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1023 if (!VT.isVector()) return getPointerTy();
1024 return VT.changeVectorElementTypeToInteger();
1025}
1026
Evan Cheng06b666c2010-05-15 02:18:07 +00001027/// getRegClassFor - Return the register class that should be used for the
1028/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001029const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001030 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1031 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1032 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001033 if (Subtarget->hasNEON()) {
1034 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001035 return &ARM::QQPRRegClass;
1036 if (VT == MVT::v8i64)
1037 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001038 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001039 return TargetLowering::getRegClassFor(VT);
1040}
1041
Eric Christopherab695882010-07-21 22:26:11 +00001042// Create a fast isel object.
1043FastISel *
1044ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1045 return ARM::createFastISel(funcInfo);
1046}
1047
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001048/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1049/// be used for loads / stores from the global.
1050unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1051 return (Subtarget->isThumb1Only() ? 127 : 4095);
1052}
1053
Evan Cheng1cc39842010-05-20 23:26:43 +00001054Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001055 unsigned NumVals = N->getNumValues();
1056 if (!NumVals)
1057 return Sched::RegPressure;
1058
1059 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001060 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001061 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001062 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001063 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001064 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001065 }
Evan Chengc10f5432010-05-28 23:25:23 +00001066
1067 if (!N->isMachineOpcode())
1068 return Sched::RegPressure;
1069
1070 // Load are scheduled for latency even if there instruction itinerary
1071 // is not available.
1072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001073 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001074
Evan Chenge837dea2011-06-28 19:10:37 +00001075 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001076 return Sched::RegPressure;
1077 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001078 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001079 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001080
Evan Cheng1cc39842010-05-20 23:26:43 +00001081 return Sched::RegPressure;
1082}
1083
Evan Chenga8e29892007-01-19 07:51:42 +00001084//===----------------------------------------------------------------------===//
1085// Lowering Code
1086//===----------------------------------------------------------------------===//
1087
Evan Chenga8e29892007-01-19 07:51:42 +00001088/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1089static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1090 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001091 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001092 case ISD::SETNE: return ARMCC::NE;
1093 case ISD::SETEQ: return ARMCC::EQ;
1094 case ISD::SETGT: return ARMCC::GT;
1095 case ISD::SETGE: return ARMCC::GE;
1096 case ISD::SETLT: return ARMCC::LT;
1097 case ISD::SETLE: return ARMCC::LE;
1098 case ISD::SETUGT: return ARMCC::HI;
1099 case ISD::SETUGE: return ARMCC::HS;
1100 case ISD::SETULT: return ARMCC::LO;
1101 case ISD::SETULE: return ARMCC::LS;
1102 }
1103}
1104
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001105/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1106static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001107 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001108 CondCode2 = ARMCC::AL;
1109 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001110 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001111 case ISD::SETEQ:
1112 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1113 case ISD::SETGT:
1114 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1115 case ISD::SETGE:
1116 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1117 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001118 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001119 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1120 case ISD::SETO: CondCode = ARMCC::VC; break;
1121 case ISD::SETUO: CondCode = ARMCC::VS; break;
1122 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1123 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1124 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1125 case ISD::SETLT:
1126 case ISD::SETULT: CondCode = ARMCC::LT; break;
1127 case ISD::SETLE:
1128 case ISD::SETULE: CondCode = ARMCC::LE; break;
1129 case ISD::SETNE:
1130 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1131 }
Evan Chenga8e29892007-01-19 07:51:42 +00001132}
1133
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134//===----------------------------------------------------------------------===//
1135// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136//===----------------------------------------------------------------------===//
1137
1138#include "ARMGenCallingConv.inc"
1139
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001140/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1141/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001142CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001143 bool Return,
1144 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001145 switch (CC) {
1146 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001147 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001148 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001149 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001150 if (!Subtarget->isAAPCS_ABI())
1151 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1152 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1153 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1154 }
1155 // Fallthrough
1156 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001157 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001158 if (!Subtarget->isAAPCS_ABI())
1159 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1160 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001161 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1162 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001163 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1164 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1165 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001166 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001167 if (!isVarArg)
1168 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1169 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001170 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001171 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001172 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001173 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001174 }
1175}
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177/// LowerCallResult - Lower the result values of a call into the
1178/// appropriate copies out of appropriate physical registers.
1179SDValue
1180ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001181 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182 const SmallVectorImpl<ISD::InputArg> &Ins,
1183 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001184 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185
Bob Wilson1f595bb2009-04-17 19:07:39 +00001186 // Assign locations to each value returned by this call.
1187 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001188 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1189 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001191 CCAssignFnForNode(CallConv, /* Return*/ true,
1192 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193
1194 // Copy all of the result registers out of their specified physreg.
1195 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1196 CCValAssign VA = RVLocs[i];
1197
Bob Wilson80915242009-04-25 00:33:20 +00001198 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001199 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001200 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001201 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001202 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001203 Chain = Lo.getValue(1);
1204 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001207 InFlag);
1208 Chain = Hi.getValue(1);
1209 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001210 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001211
Owen Anderson825b72b2009-08-11 20:47:22 +00001212 if (VA.getLocVT() == MVT::v2f64) {
1213 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1214 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1215 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001216
1217 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001219 Chain = Lo.getValue(1);
1220 InFlag = Lo.getValue(2);
1221 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 Chain = Hi.getValue(1);
1224 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001225 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1227 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001228 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001229 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001230 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1231 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001232 Chain = Val.getValue(1);
1233 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234 }
Bob Wilson80915242009-04-25 00:33:20 +00001235
1236 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001237 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001238 case CCValAssign::Full: break;
1239 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001240 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001241 break;
1242 }
1243
Dan Gohman98ca4f22009-08-05 01:29:28 +00001244 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001245 }
1246
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248}
1249
Bob Wilsondee46d72009-04-17 20:35:10 +00001250/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1253 SDValue StackPtr, SDValue Arg,
1254 DebugLoc dl, SelectionDAG &DAG,
1255 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001256 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 unsigned LocMemOffset = VA.getLocMemOffset();
1258 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1259 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001261 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001262 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001263}
1264
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001266 SDValue Chain, SDValue &Arg,
1267 RegsToPassVector &RegsToPass,
1268 CCValAssign &VA, CCValAssign &NextVA,
1269 SDValue &StackPtr,
1270 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001271 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001272
Jim Grosbache5165492009-11-09 00:11:35 +00001273 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001275 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1276
1277 if (NextVA.isRegLoc())
1278 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1279 else {
1280 assert(NextVA.isMemLoc());
1281 if (StackPtr.getNode() == 0)
1282 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1283
Dan Gohman98ca4f22009-08-05 01:29:28 +00001284 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1285 dl, DAG, NextVA,
1286 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001287 }
1288}
1289
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001291/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1292/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001294ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001295 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001296 SelectionDAG &DAG = CLI.DAG;
1297 DebugLoc &dl = CLI.DL;
1298 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1299 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1300 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1301 SDValue Chain = CLI.Chain;
1302 SDValue Callee = CLI.Callee;
1303 bool &isTailCall = CLI.IsTailCall;
1304 CallingConv::ID CallConv = CLI.CallConv;
1305 bool doesNotRet = CLI.DoesNotReturn;
1306 bool isVarArg = CLI.IsVarArg;
1307
Dale Johannesen51e28e62010-06-03 21:09:53 +00001308 MachineFunction &MF = DAG.getMachineFunction();
1309 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1310 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001311 // Disable tail calls if they're not supported.
1312 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001313 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001314 if (isTailCall) {
1315 // Check if it's really possible to do a tail call.
1316 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1317 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001318 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001319 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1320 // detected sibcalls.
1321 if (isTailCall) {
1322 ++NumTailCalls;
1323 IsSibCall = true;
1324 }
1325 }
Evan Chenga8e29892007-01-19 07:51:42 +00001326
Bob Wilson1f595bb2009-04-17 19:07:39 +00001327 // Analyze operands of the call, assigning locations to each operand.
1328 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001329 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1330 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001331 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001332 CCAssignFnForNode(CallConv, /* Return*/ false,
1333 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001334
Bob Wilson1f595bb2009-04-17 19:07:39 +00001335 // Get a count of how many bytes are to be pushed on the stack.
1336 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001337
Dale Johannesen51e28e62010-06-03 21:09:53 +00001338 // For tail calls, memory operands are available in our caller's stack.
1339 if (IsSibCall)
1340 NumBytes = 0;
1341
Evan Chenga8e29892007-01-19 07:51:42 +00001342 // Adjust the stack pointer for the new arguments...
1343 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001344 if (!IsSibCall)
1345 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001346
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001347 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001348
Bob Wilson5bafff32009-06-22 23:27:02 +00001349 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001351
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001353 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001354 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1355 i != e;
1356 ++i, ++realArgIdx) {
1357 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001358 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001360 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001361
Bob Wilson1f595bb2009-04-17 19:07:39 +00001362 // Promote the value if needed.
1363 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001364 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001365 case CCValAssign::Full: break;
1366 case CCValAssign::SExt:
1367 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1368 break;
1369 case CCValAssign::ZExt:
1370 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1371 break;
1372 case CCValAssign::AExt:
1373 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1374 break;
1375 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001376 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001377 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001378 }
1379
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001380 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001381 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001382 if (VA.getLocVT() == MVT::v2f64) {
1383 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1384 DAG.getConstant(0, MVT::i32));
1385 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1386 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001387
Dan Gohman98ca4f22009-08-05 01:29:28 +00001388 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001389 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1390
1391 VA = ArgLocs[++i]; // skip ahead to next loc
1392 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001393 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001394 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1395 } else {
1396 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001397
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1399 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001400 }
1401 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001402 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001403 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001404 }
1405 } else if (VA.isRegLoc()) {
1406 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001407 } else if (isByVal) {
1408 assert(VA.isMemLoc());
1409 unsigned offset = 0;
1410
1411 // True if this byval aggregate will be split between registers
1412 // and memory.
1413 if (CCInfo.isFirstByValRegValid()) {
1414 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1415 unsigned int i, j;
1416 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1417 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1418 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1419 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1420 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001421 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001422 MemOpChains.push_back(Load.getValue(1));
1423 RegsToPass.push_back(std::make_pair(j, Load));
1424 }
1425 offset = ARM::R4 - CCInfo.getFirstByValReg();
1426 CCInfo.clearFirstByValReg();
1427 }
1428
Manman Ren763a75d2012-06-01 02:44:42 +00001429 if (Flags.getByValSize() - 4*offset > 0) {
1430 unsigned LocMemOffset = VA.getLocMemOffset();
1431 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1432 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1433 StkPtrOff);
1434 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1435 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1436 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1437 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001438 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001439
Manman Ren763a75d2012-06-01 02:44:42 +00001440 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001441 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001442 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1443 Ops, array_lengthof(Ops)));
1444 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001445 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001446 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001447
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1449 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001450 }
Evan Chenga8e29892007-01-19 07:51:42 +00001451 }
1452
1453 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001454 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001455 &MemOpChains[0], MemOpChains.size());
1456
1457 // Build a sequence of copy-to-reg nodes chained together with token chain
1458 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001460 // Tail call byval lowering might overwrite argument registers so in case of
1461 // tail call optimization the copies to registers are lowered later.
1462 if (!isTailCall)
1463 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1464 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1465 RegsToPass[i].second, InFlag);
1466 InFlag = Chain.getValue(1);
1467 }
Evan Chenga8e29892007-01-19 07:51:42 +00001468
Dale Johannesen51e28e62010-06-03 21:09:53 +00001469 // For tail calls lower the arguments to the 'real' stack slot.
1470 if (isTailCall) {
1471 // Force all the incoming stack arguments to be loaded from the stack
1472 // before any new outgoing arguments are stored to the stack, because the
1473 // outgoing stack slots may alias the incoming argument stack slots, and
1474 // the alias isn't otherwise explicit. This is slightly more conservative
1475 // than necessary, because it means that each store effectively depends
1476 // on every argument instead of just those arguments it would clobber.
1477
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001478 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001479 InFlag = SDValue();
1480 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1481 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1482 RegsToPass[i].second, InFlag);
1483 InFlag = Chain.getValue(1);
1484 }
1485 InFlag =SDValue();
1486 }
1487
Bill Wendling056292f2008-09-16 21:48:12 +00001488 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1489 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1490 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001491 bool isDirect = false;
1492 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001493 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001494 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001495
1496 if (EnableARMLongCalls) {
1497 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1498 && "long-calls with non-static relocation model!");
1499 // Handle a global address or an external symbol. If it's not one of
1500 // those, the target's already in a register, so we don't need to do
1501 // anything extra.
1502 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001503 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001504 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001505 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001506 ARMConstantPoolValue *CPV =
1507 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1508
Jim Grosbache7b52522010-04-14 22:28:31 +00001509 // Get the address of the callee into a register
1510 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1511 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1512 Callee = DAG.getLoad(getPointerTy(), dl,
1513 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001514 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001515 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001516 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1517 const char *Sym = S->getSymbol();
1518
1519 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001520 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001521 ARMConstantPoolValue *CPV =
1522 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1523 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001524 // Get the address of the callee into a register
1525 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1526 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1527 Callee = DAG.getLoad(getPointerTy(), dl,
1528 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001529 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001530 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001531 }
1532 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001533 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001534 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001535 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001536 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001537 getTargetMachine().getRelocationModel() != Reloc::Static;
1538 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001539 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001540 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001541 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001542 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001543 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001544 ARMConstantPoolValue *CPV =
1545 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001546 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001548 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001549 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001550 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001551 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001552 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001553 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001554 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001555 } else {
1556 // On ELF targets for PIC code, direct calls should go through the PLT
1557 unsigned OpFlags = 0;
1558 if (Subtarget->isTargetELF() &&
1559 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1560 OpFlags = ARMII::MO_PLT;
1561 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1562 }
Bill Wendling056292f2008-09-16 21:48:12 +00001563 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001564 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001565 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001566 getTargetMachine().getRelocationModel() != Reloc::Static;
1567 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001568 // tBX takes a register source operand.
1569 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001570 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001571 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001572 ARMConstantPoolValue *CPV =
1573 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1574 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001575 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001577 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001578 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001579 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001580 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001581 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001582 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001583 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001584 } else {
1585 unsigned OpFlags = 0;
1586 // On ELF targets for PIC code, direct calls should go through the PLT
1587 if (Subtarget->isTargetELF() &&
1588 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1589 OpFlags = ARMII::MO_PLT;
1590 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1591 }
Evan Chenga8e29892007-01-19 07:51:42 +00001592 }
1593
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001594 // FIXME: handle tail calls differently.
1595 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001596 if (Subtarget->isThumb()) {
1597 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001598 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001599 else if (doesNotRet && isDirect && !isARMFunc &&
1600 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1601 // "mov lr, pc; b _foo" to avoid confusing the RSP
1602 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001603 else
1604 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1605 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001606 if (!isDirect && !Subtarget->hasV5TOps()) {
1607 CallOpc = ARMISD::CALL_NOLINK;
1608 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1609 // "mov lr, pc; b _foo" to avoid confusing the RSP
1610 CallOpc = ARMISD::CALL_NOLINK;
1611 else
1612 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001613 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001614
Dan Gohman475871a2008-07-27 21:46:04 +00001615 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001616 Ops.push_back(Chain);
1617 Ops.push_back(Callee);
1618
1619 // Add argument registers to the end of the list so that they are known live
1620 // into the call.
1621 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1622 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1623 RegsToPass[i].second.getValueType()));
1624
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001625 // Add a register mask operand representing the call-preserved registers.
1626 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1627 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1628 assert(Mask && "Missing call preserved mask for calling convention");
1629 Ops.push_back(DAG.getRegisterMask(Mask));
1630
Gabor Greifba36cb52008-08-28 21:40:38 +00001631 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001632 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001633
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001634 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001635 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001637
Duncan Sands4bdcb612008-07-02 17:40:58 +00001638 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001639 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001640 InFlag = Chain.getValue(1);
1641
Chris Lattnere563bbc2008-10-11 22:08:30 +00001642 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1643 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001645 InFlag = Chain.getValue(1);
1646
Bob Wilson1f595bb2009-04-17 19:07:39 +00001647 // Handle result values, copying them out of physregs into vregs that we
1648 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1650 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001651}
1652
Stuart Hastingsf222e592011-02-28 17:17:53 +00001653/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001654/// on the stack. Remember the next parameter register to allocate,
1655/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001656/// this.
1657void
Craig Topperc89c7442012-03-27 07:21:54 +00001658ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001659 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1660 assert((State->getCallOrPrologue() == Prologue ||
1661 State->getCallOrPrologue() == Call) &&
1662 "unhandled ParmContext");
1663 if ((!State->isFirstByValRegValid()) &&
1664 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1665 State->setFirstByValReg(reg);
1666 // At a call site, a byval parameter that is split between
1667 // registers and memory needs its size truncated here. In a
1668 // function prologue, such byval parameters are reassembled in
1669 // memory, and are not truncated.
1670 if (State->getCallOrPrologue() == Call) {
1671 unsigned excess = 4 * (ARM::R4 - reg);
1672 assert(size >= excess && "expected larger existing stack allocation");
1673 size -= excess;
1674 }
1675 }
1676 // Confiscate any remaining parameter registers to preclude their
1677 // assignment to subsequent parameters.
1678 while (State->AllocateReg(GPRArgRegs, 4))
1679 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001680}
1681
Dale Johannesen51e28e62010-06-03 21:09:53 +00001682/// MatchingStackOffset - Return true if the given stack call argument is
1683/// already available in the same position (relatively) of the caller's
1684/// incoming argument stack.
1685static
1686bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1687 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001688 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001689 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1690 int FI = INT_MAX;
1691 if (Arg.getOpcode() == ISD::CopyFromReg) {
1692 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001693 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001694 return false;
1695 MachineInstr *Def = MRI->getVRegDef(VR);
1696 if (!Def)
1697 return false;
1698 if (!Flags.isByVal()) {
1699 if (!TII->isLoadFromStackSlot(Def, FI))
1700 return false;
1701 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001702 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001703 }
1704 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1705 if (Flags.isByVal())
1706 // ByVal argument is passed in as a pointer but it's now being
1707 // dereferenced. e.g.
1708 // define @foo(%struct.X* %A) {
1709 // tail call @bar(%struct.X* byval %A)
1710 // }
1711 return false;
1712 SDValue Ptr = Ld->getBasePtr();
1713 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1714 if (!FINode)
1715 return false;
1716 FI = FINode->getIndex();
1717 } else
1718 return false;
1719
1720 assert(FI != INT_MAX);
1721 if (!MFI->isFixedObjectIndex(FI))
1722 return false;
1723 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1724}
1725
1726/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1727/// for tail call optimization. Targets which want to do tail call
1728/// optimization should implement this function.
1729bool
1730ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1731 CallingConv::ID CalleeCC,
1732 bool isVarArg,
1733 bool isCalleeStructRet,
1734 bool isCallerStructRet,
1735 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001736 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001737 const SmallVectorImpl<ISD::InputArg> &Ins,
1738 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001739 const Function *CallerF = DAG.getMachineFunction().getFunction();
1740 CallingConv::ID CallerCC = CallerF->getCallingConv();
1741 bool CCMatch = CallerCC == CalleeCC;
1742
1743 // Look for obvious safe cases to perform tail call optimization that do not
1744 // require ABI changes. This is what gcc calls sibcall.
1745
Jim Grosbach7616b642010-06-16 23:45:49 +00001746 // Do not sibcall optimize vararg calls unless the call site is not passing
1747 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001748 if (isVarArg && !Outs.empty())
1749 return false;
1750
1751 // Also avoid sibcall optimization if either caller or callee uses struct
1752 // return semantics.
1753 if (isCalleeStructRet || isCallerStructRet)
1754 return false;
1755
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001756 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001757 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1758 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1759 // support in the assembler and linker to be used. This would need to be
1760 // fixed to fully support tail calls in Thumb1.
1761 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001762 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1763 // LR. This means if we need to reload LR, it takes an extra instructions,
1764 // which outweighs the value of the tail call; but here we don't know yet
1765 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001766 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001767 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001768
1769 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1770 // but we need to make sure there are enough registers; the only valid
1771 // registers are the 4 used for parameters. We don't currently do this
1772 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001773 if (Subtarget->isThumb1Only())
1774 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001775
Dale Johannesen51e28e62010-06-03 21:09:53 +00001776 // If the calling conventions do not match, then we'd better make sure the
1777 // results are returned in the same way as what the caller expects.
1778 if (!CCMatch) {
1779 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001780 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1781 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001782 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1783
1784 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001785 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1786 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001787 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1788
1789 if (RVLocs1.size() != RVLocs2.size())
1790 return false;
1791 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1792 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1793 return false;
1794 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1795 return false;
1796 if (RVLocs1[i].isRegLoc()) {
1797 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1798 return false;
1799 } else {
1800 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1801 return false;
1802 }
1803 }
1804 }
1805
1806 // If the callee takes no arguments then go on to check the results of the
1807 // call.
1808 if (!Outs.empty()) {
1809 // Check if stack adjustment is needed. For now, do not do this if any
1810 // argument is passed on the stack.
1811 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001812 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1813 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001814 CCInfo.AnalyzeCallOperands(Outs,
1815 CCAssignFnForNode(CalleeCC, false, isVarArg));
1816 if (CCInfo.getNextStackOffset()) {
1817 MachineFunction &MF = DAG.getMachineFunction();
1818
1819 // Check if the arguments are already laid out in the right way as
1820 // the caller's fixed stack objects.
1821 MachineFrameInfo *MFI = MF.getFrameInfo();
1822 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001823 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001824 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1825 i != e;
1826 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001827 CCValAssign &VA = ArgLocs[i];
1828 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001829 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001830 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001831 if (VA.getLocInfo() == CCValAssign::Indirect)
1832 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001833 if (VA.needsCustom()) {
1834 // f64 and vector types are split into multiple registers or
1835 // register/stack-slot combinations. The types will not match
1836 // the registers; give up on memory f64 refs until we figure
1837 // out what to do about this.
1838 if (!VA.isRegLoc())
1839 return false;
1840 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001841 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001842 if (RegVT == MVT::v2f64) {
1843 if (!ArgLocs[++i].isRegLoc())
1844 return false;
1845 if (!ArgLocs[++i].isRegLoc())
1846 return false;
1847 }
1848 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001849 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1850 MFI, MRI, TII))
1851 return false;
1852 }
1853 }
1854 }
1855 }
1856
1857 return true;
1858}
1859
Dan Gohman98ca4f22009-08-05 01:29:28 +00001860SDValue
1861ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001862 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001863 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001864 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001865 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001866
Bob Wilsondee46d72009-04-17 20:35:10 +00001867 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001868 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001869
Bob Wilsondee46d72009-04-17 20:35:10 +00001870 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001871 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1872 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001873
Dan Gohman98ca4f22009-08-05 01:29:28 +00001874 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001875 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1876 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001877
1878 // If this is the first return lowered for this function, add
1879 // the regs to the liveout set for the function.
1880 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1881 for (unsigned i = 0; i != RVLocs.size(); ++i)
1882 if (RVLocs[i].isRegLoc())
1883 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001884 }
1885
Bob Wilson1f595bb2009-04-17 19:07:39 +00001886 SDValue Flag;
1887
1888 // Copy the result values into the output registers.
1889 for (unsigned i = 0, realRVLocIdx = 0;
1890 i != RVLocs.size();
1891 ++i, ++realRVLocIdx) {
1892 CCValAssign &VA = RVLocs[i];
1893 assert(VA.isRegLoc() && "Can only return in registers!");
1894
Dan Gohmanc9403652010-07-07 15:54:55 +00001895 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001896
1897 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001898 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001899 case CCValAssign::Full: break;
1900 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001901 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001902 break;
1903 }
1904
Bob Wilson1f595bb2009-04-17 19:07:39 +00001905 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001907 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1909 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001910 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001911 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001912
1913 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1914 Flag = Chain.getValue(1);
1915 VA = RVLocs[++i]; // skip ahead to next loc
1916 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1917 HalfGPRs.getValue(1), Flag);
1918 Flag = Chain.getValue(1);
1919 VA = RVLocs[++i]; // skip ahead to next loc
1920
1921 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1923 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001924 }
1925 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1926 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001927 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001929 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001930 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001931 VA = RVLocs[++i]; // skip ahead to next loc
1932 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1933 Flag);
1934 } else
1935 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1936
Bob Wilsondee46d72009-04-17 20:35:10 +00001937 // Guarantee that all emitted copies are
1938 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001939 Flag = Chain.getValue(1);
1940 }
1941
1942 SDValue result;
1943 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001945 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001947
1948 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001949}
1950
Evan Chengbf010eb2012-04-10 01:51:00 +00001951bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001952 if (N->getNumValues() != 1)
1953 return false;
1954 if (!N->hasNUsesOfValue(1, 0))
1955 return false;
1956
Evan Chengbf010eb2012-04-10 01:51:00 +00001957 SDValue TCChain = Chain;
1958 SDNode *Copy = *N->use_begin();
1959 if (Copy->getOpcode() == ISD::CopyToReg) {
1960 // If the copy has a glue operand, we conservatively assume it isn't safe to
1961 // perform a tail call.
1962 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1963 return false;
1964 TCChain = Copy->getOperand(0);
1965 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1966 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001967 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001968 SmallPtrSet<SDNode*, 2> Copies;
1969 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001970 UI != UE; ++UI) {
1971 if (UI->getOpcode() != ISD::CopyToReg)
1972 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001973 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001974 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001975 if (Copies.size() > 2)
1976 return false;
1977
1978 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1979 UI != UE; ++UI) {
1980 SDValue UseChain = UI->getOperand(0);
1981 if (Copies.count(UseChain.getNode()))
1982 // Second CopyToReg
1983 Copy = *UI;
1984 else
1985 // First CopyToReg
1986 TCChain = UseChain;
1987 }
1988 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001989 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001990 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001991 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001992 Copy = *Copy->use_begin();
1993 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001995 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001996 } else {
1997 return false;
1998 }
1999
Evan Cheng1bf891a2010-12-01 22:59:46 +00002000 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002001 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2002 UI != UE; ++UI) {
2003 if (UI->getOpcode() != ARMISD::RET_FLAG)
2004 return false;
2005 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002006 }
2007
Evan Chengbf010eb2012-04-10 01:51:00 +00002008 if (!HasRet)
2009 return false;
2010
2011 Chain = TCChain;
2012 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002013}
2014
Evan Cheng485fafc2011-03-21 01:19:09 +00002015bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002016 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002017 return false;
2018
2019 if (!CI->isTailCall())
2020 return false;
2021
2022 return !Subtarget->isThumb1Only();
2023}
2024
Bob Wilsonb62d2572009-11-03 00:02:05 +00002025// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2026// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2027// one of the above mentioned nodes. It has to be wrapped because otherwise
2028// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2029// be used to form addressing mode. These wrapped nodes will be selected
2030// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002031static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002032 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002033 // FIXME there is no actual debug info here
2034 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002035 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002037 if (CP->isMachineConstantPoolEntry())
2038 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2039 CP->getAlignment());
2040 else
2041 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2042 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002043 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002044}
2045
Jim Grosbache1102ca2010-07-19 17:20:38 +00002046unsigned ARMTargetLowering::getJumpTableEncoding() const {
2047 return MachineJumpTableInfo::EK_Inline;
2048}
2049
Dan Gohmand858e902010-04-17 15:26:15 +00002050SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2051 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002052 MachineFunction &MF = DAG.getMachineFunction();
2053 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2054 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002055 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002056 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002057 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002058 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2059 SDValue CPAddr;
2060 if (RelocM == Reloc::Static) {
2061 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2062 } else {
2063 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002064 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002065 ARMConstantPoolValue *CPV =
2066 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2067 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002068 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2069 }
2070 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2071 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002072 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002073 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002074 if (RelocM == Reloc::Static)
2075 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002076 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002077 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002078}
2079
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002080// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002081SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002082ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002083 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002084 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002085 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002086 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002087 MachineFunction &MF = DAG.getMachineFunction();
2088 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002089 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002090 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002091 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2092 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002093 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002095 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002096 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002097 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002098 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002099
Evan Chenge7e0d622009-11-06 22:24:13 +00002100 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002101 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002102
2103 // call __tls_get_addr.
2104 ArgListTy Args;
2105 ArgListEntry Entry;
2106 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002107 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002108 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002109 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002110 TargetLowering::CallLoweringInfo CLI(Chain,
2111 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002112 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002113 0, CallingConv::C, /*isTailCall=*/false,
2114 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002115 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002116 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002117 return CallResult.first;
2118}
2119
2120// Lower ISD::GlobalTLSAddress using the "initial exec" or
2121// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002122SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002123ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002124 SelectionDAG &DAG,
2125 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002126 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002127 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue Offset;
2129 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002130 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002131 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002132 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002134 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002135 MachineFunction &MF = DAG.getMachineFunction();
2136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002137 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002139 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2140 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002141 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2142 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2143 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002144 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002146 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002147 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002148 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002149 Chain = Offset.getValue(1);
2150
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002153
Evan Cheng9eda6892009-10-31 03:39:36 +00002154 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002155 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002156 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002157 } else {
2158 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002159 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002160 ARMConstantPoolValue *CPV =
2161 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002162 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002163 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002164 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002165 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002166 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002167 }
2168
2169 // The address of the thread local variable is the add of the thread
2170 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002171 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002172}
2173
Dan Gohman475871a2008-07-27 21:46:04 +00002174SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002175ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002176 // TODO: implement the "local dynamic" model
2177 assert(Subtarget->isTargetELF() &&
2178 "TLS not implemented for non-ELF targets");
2179 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002180
2181 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2182
2183 switch (model) {
2184 case TLSModel::GeneralDynamic:
2185 case TLSModel::LocalDynamic:
2186 return LowerToTLSGeneralDynamicModel(GA, DAG);
2187 case TLSModel::InitialExec:
2188 case TLSModel::LocalExec:
2189 return LowerToTLSExecModels(GA, DAG, model);
2190 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002191 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002192}
2193
Dan Gohman475871a2008-07-27 21:46:04 +00002194SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002195 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002196 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002197 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002198 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002199 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2200 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002201 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002202 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002203 ARMConstantPoolConstant::Create(GV,
2204 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002205 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002207 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002208 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002209 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002210 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002211 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002212 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002213 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002214 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002215 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002216 MachinePointerInfo::getGOT(),
2217 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002218 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002219 }
2220
2221 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002222 // pair. This is always cheaper.
2223 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002224 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002225 // FIXME: Once remat is capable of dealing with instructions with register
2226 // operands, expand this into two nodes.
2227 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2228 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002229 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002230 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2231 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2232 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2233 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002234 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002235 }
2236}
2237
Dan Gohman475871a2008-07-27 21:46:04 +00002238SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002239 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002240 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002241 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002242 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002243 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002244 MachineFunction &MF = DAG.getMachineFunction();
2245 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2246
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002247 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2248 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002249 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002250 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002251 // FIXME: Once remat is capable of dealing with instructions with register
2252 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002253 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002254 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2255 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2256
Evan Cheng53519f02011-01-21 18:55:51 +00002257 unsigned Wrapper = (RelocM == Reloc::PIC_)
2258 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2259 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002260 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002261 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2262 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002263 MachinePointerInfo::getGOT(),
2264 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002265 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002266 }
2267
2268 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002269 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002270 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002271 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002272 } else {
2273 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002274 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2275 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002276 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2277 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002278 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002279 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002281
Evan Cheng9eda6892009-10-31 03:39:36 +00002282 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002283 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002284 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002285 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002286
2287 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002288 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002289 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002290 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002291
Evan Cheng63476a82009-09-03 07:04:02 +00002292 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002293 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002294 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002295
2296 return Result;
2297}
2298
Dan Gohman475871a2008-07-27 21:46:04 +00002299SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002300 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002301 assert(Subtarget->isTargetELF() &&
2302 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002303 MachineFunction &MF = DAG.getMachineFunction();
2304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002305 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002306 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002307 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002308 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002309 ARMConstantPoolValue *CPV =
2310 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2311 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002312 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002314 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002315 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002316 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002317 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002318 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002319}
2320
Jim Grosbach0e0da732009-05-12 23:59:14 +00002321SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002322ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2323 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002324 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002325 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2326 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002327 Op.getOperand(1), Val);
2328}
2329
2330SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002331ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2332 DebugLoc dl = Op.getDebugLoc();
2333 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2334 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2335}
2336
2337SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002338ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002339 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002340 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002341 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002342 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002343 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002344 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002345 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002346 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2347 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002348 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002349 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002350 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002351 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002352 EVT PtrVT = getPointerTy();
2353 DebugLoc dl = Op.getDebugLoc();
2354 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2355 SDValue CPAddr;
2356 unsigned PCAdj = (RelocM != Reloc::PIC_)
2357 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002358 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002359 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2360 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002361 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002362 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002363 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002364 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002365 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002366 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002367
2368 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002369 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002370 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2371 }
2372 return Result;
2373 }
Evan Cheng92e39162011-03-29 23:06:19 +00002374 case Intrinsic::arm_neon_vmulls:
2375 case Intrinsic::arm_neon_vmullu: {
2376 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2377 ? ARMISD::VMULLs : ARMISD::VMULLu;
2378 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2379 Op.getOperand(1), Op.getOperand(2));
2380 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002381 }
2382}
2383
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002384static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002385 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002386 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002387 if (!Subtarget->hasDataBarrier()) {
2388 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2389 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2390 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002391 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002392 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002393 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002394 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002395 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002396
2397 SDValue Op5 = Op.getOperand(5);
2398 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2399 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2400 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2401 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2402
2403 ARM_MB::MemBOpt DMBOpt;
2404 if (isDeviceBarrier)
2405 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2406 else
2407 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2408 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2409 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002410}
2411
Eli Friedman26689ac2011-08-03 21:06:02 +00002412
2413static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2414 const ARMSubtarget *Subtarget) {
2415 // FIXME: handle "fence singlethread" more efficiently.
2416 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002417 if (!Subtarget->hasDataBarrier()) {
2418 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2419 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2420 // here.
2421 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2422 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002423 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002424 DAG.getConstant(0, MVT::i32));
2425 }
2426
Eli Friedman26689ac2011-08-03 21:06:02 +00002427 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002428 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002429}
2430
Evan Chengdfed19f2010-11-03 06:34:55 +00002431static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2432 const ARMSubtarget *Subtarget) {
2433 // ARM pre v5TE and Thumb1 does not have preload instructions.
2434 if (!(Subtarget->isThumb2() ||
2435 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2436 // Just preserve the chain.
2437 return Op.getOperand(0);
2438
2439 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002440 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2441 if (!isRead &&
2442 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2443 // ARMv7 with MP extension has PLDW.
2444 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002445
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002446 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2447 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002448 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002449 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002450 isData = ~isData & 1;
2451 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002452
2453 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002454 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2455 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002456}
2457
Dan Gohman1e93df62010-04-17 14:41:14 +00002458static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2459 MachineFunction &MF = DAG.getMachineFunction();
2460 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2461
Evan Chenga8e29892007-01-19 07:51:42 +00002462 // vastart just stores the address of the VarArgsFrameIndex slot into the
2463 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002464 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002465 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002466 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002467 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002468 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2469 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002470}
2471
Dan Gohman475871a2008-07-27 21:46:04 +00002472SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002473ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2474 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002475 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002476 MachineFunction &MF = DAG.getMachineFunction();
2477 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2478
Craig Topper44d23822012-02-22 05:59:10 +00002479 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002480 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002481 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002482 else
Craig Topper420761a2012-04-20 07:30:17 +00002483 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002484
2485 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002486 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002487 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002488
2489 SDValue ArgValue2;
2490 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002491 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002492 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002493
2494 // Create load node to retrieve arguments from the stack.
2495 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002496 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002497 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002498 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002499 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002500 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002501 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002502 }
2503
Jim Grosbache5165492009-11-09 00:11:35 +00002504 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002505}
2506
Stuart Hastingsc7315872011-04-20 16:47:52 +00002507void
2508ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2509 unsigned &VARegSize, unsigned &VARegSaveSize)
2510 const {
2511 unsigned NumGPRs;
2512 if (CCInfo.isFirstByValRegValid())
2513 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2514 else {
2515 unsigned int firstUnalloced;
2516 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2517 sizeof(GPRArgRegs) /
2518 sizeof(GPRArgRegs[0]));
2519 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2520 }
2521
2522 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2523 VARegSize = NumGPRs * 4;
2524 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2525}
2526
2527// The remaining GPRs hold either the beginning of variable-argument
2528// data, or the beginning of an aggregate passed by value (usuall
2529// byval). Either way, we allocate stack slots adjacent to the data
2530// provided by our caller, and store the unallocated registers there.
2531// If this is a variadic function, the va_list pointer will begin with
2532// these values; otherwise, this reassembles a (byval) structure that
2533// was split between registers and memory.
2534void
2535ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2536 DebugLoc dl, SDValue &Chain,
2537 unsigned ArgOffset) const {
2538 MachineFunction &MF = DAG.getMachineFunction();
2539 MachineFrameInfo *MFI = MF.getFrameInfo();
2540 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2541 unsigned firstRegToSaveIndex;
2542 if (CCInfo.isFirstByValRegValid())
2543 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2544 else {
2545 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2546 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2547 }
2548
2549 unsigned VARegSize, VARegSaveSize;
2550 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2551 if (VARegSaveSize) {
2552 // If this function is vararg, store any remaining integer argument regs
2553 // to their spots on the stack so that they may be loaded by deferencing
2554 // the result of va_next.
2555 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002556 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2557 ArgOffset + VARegSaveSize
2558 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002559 false));
2560 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2561 getPointerTy());
2562
2563 SmallVector<SDValue, 4> MemOps;
2564 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002565 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002566 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002567 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002568 else
Craig Topper420761a2012-04-20 07:30:17 +00002569 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002570
2571 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2572 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2573 SDValue Store =
2574 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002575 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002576 false, false, 0);
2577 MemOps.push_back(Store);
2578 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2579 DAG.getConstant(4, getPointerTy()));
2580 }
2581 if (!MemOps.empty())
2582 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2583 &MemOps[0], MemOps.size());
2584 } else
2585 // This will point to the next argument passed via stack.
2586 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2587}
2588
Bob Wilson5bafff32009-06-22 23:27:02 +00002589SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002590ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002591 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002592 const SmallVectorImpl<ISD::InputArg>
2593 &Ins,
2594 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002595 SmallVectorImpl<SDValue> &InVals)
2596 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002597 MachineFunction &MF = DAG.getMachineFunction();
2598 MachineFrameInfo *MFI = MF.getFrameInfo();
2599
Bob Wilson1f595bb2009-04-17 19:07:39 +00002600 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2601
2602 // Assign locations to all of the incoming arguments.
2603 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002604 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2605 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002607 CCAssignFnForNode(CallConv, /* Return*/ false,
2608 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002609
2610 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002611 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002612
Stuart Hastingsf222e592011-02-28 17:17:53 +00002613 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002614 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2615 CCValAssign &VA = ArgLocs[i];
2616
Bob Wilsondee46d72009-04-17 20:35:10 +00002617 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002618 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002619 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620
Bob Wilson1f595bb2009-04-17 19:07:39 +00002621 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002622 // f64 and vector types are split up into multiple registers or
2623 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002624 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002625 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002626 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002627 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002628 SDValue ArgValue2;
2629 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002630 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002631 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2632 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002633 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002634 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002635 } else {
2636 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2637 Chain, DAG, dl);
2638 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002639 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2640 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002641 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002643 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2644 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002645 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002646
Bob Wilson5bafff32009-06-22 23:27:02 +00002647 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002648 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002649
Owen Anderson825b72b2009-08-11 20:47:22 +00002650 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002651 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002653 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002655 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002656 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002657 RC = AFI->isThumb1OnlyFunction() ?
2658 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2659 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002660 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002661 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002662
2663 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002664 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002666 }
2667
2668 // If this is an 8 or 16-bit value, it is really passed promoted
2669 // to 32 bits. Insert an assert[sz]ext to capture this, then
2670 // truncate to the right size.
2671 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002672 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002673 case CCValAssign::Full: break;
2674 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002675 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002676 break;
2677 case CCValAssign::SExt:
2678 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2679 DAG.getValueType(VA.getValVT()));
2680 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2681 break;
2682 case CCValAssign::ZExt:
2683 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2684 DAG.getValueType(VA.getValVT()));
2685 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2686 break;
2687 }
2688
Dan Gohman98ca4f22009-08-05 01:29:28 +00002689 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002690
2691 } else { // VA.isRegLoc()
2692
2693 // sanity check
2694 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002695 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002696
Stuart Hastingsf222e592011-02-28 17:17:53 +00002697 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002698
Stuart Hastingsf222e592011-02-28 17:17:53 +00002699 // Some Ins[] entries become multiple ArgLoc[] entries.
2700 // Process them only once.
2701 if (index != lastInsIndex)
2702 {
2703 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002704 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002705 // This can be changed with more analysis.
2706 // In case of tail call optimization mark all arguments mutable.
2707 // Since they could be overwritten by lowering of arguments in case of
2708 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002709 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002710 unsigned VARegSize, VARegSaveSize;
2711 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2712 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2713 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002714 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002715 int FI = MFI->CreateFixedObject(Bytes,
2716 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002717 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2718 } else {
2719 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2720 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002721
Stuart Hastingsf222e592011-02-28 17:17:53 +00002722 // Create load nodes to retrieve arguments from the stack.
2723 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2724 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2725 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002726 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002727 }
2728 lastInsIndex = index;
2729 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002730 }
2731 }
2732
2733 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002734 if (isVarArg)
2735 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002736
Dan Gohman98ca4f22009-08-05 01:29:28 +00002737 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002738}
2739
2740/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002741static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002742 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002743 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002744 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002745 // Maybe this has already been legalized into the constant pool?
2746 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002747 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002748 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002749 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002750 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002751 }
2752 }
2753 return false;
2754}
2755
Evan Chenga8e29892007-01-19 07:51:42 +00002756/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2757/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002758SDValue
2759ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002760 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002761 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002762 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002763 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002764 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002765 // Constant does not fit, try adjusting it by one?
2766 switch (CC) {
2767 default: break;
2768 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002769 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002770 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002771 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002772 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002773 }
2774 break;
2775 case ISD::SETULT:
2776 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002777 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002778 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002780 }
2781 break;
2782 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002783 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002784 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002785 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002786 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002787 }
2788 break;
2789 case ISD::SETULE:
2790 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002791 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002792 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002794 }
2795 break;
2796 }
2797 }
2798 }
2799
2800 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002801 ARMISD::NodeType CompareType;
2802 switch (CondCode) {
2803 default:
2804 CompareType = ARMISD::CMP;
2805 break;
2806 case ARMCC::EQ:
2807 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002808 // Uses only Z Flag
2809 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002810 break;
2811 }
Evan Cheng218977b2010-07-13 19:27:42 +00002812 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002813 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002814}
2815
2816/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002817SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002818ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002819 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002820 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002821 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002822 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002823 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002824 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2825 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002826}
2827
Bob Wilson79f56c92011-03-08 01:17:20 +00002828/// duplicateCmp - Glue values can have only one use, so this function
2829/// duplicates a comparison node.
2830SDValue
2831ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2832 unsigned Opc = Cmp.getOpcode();
2833 DebugLoc DL = Cmp.getDebugLoc();
2834 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2835 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2836
2837 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2838 Cmp = Cmp.getOperand(0);
2839 Opc = Cmp.getOpcode();
2840 if (Opc == ARMISD::CMPFP)
2841 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2842 else {
2843 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2844 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2845 }
2846 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2847}
2848
Bill Wendlingde2b1512010-08-11 08:43:16 +00002849SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2850 SDValue Cond = Op.getOperand(0);
2851 SDValue SelectTrue = Op.getOperand(1);
2852 SDValue SelectFalse = Op.getOperand(2);
2853 DebugLoc dl = Op.getDebugLoc();
2854
2855 // Convert:
2856 //
2857 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2858 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2859 //
2860 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2861 const ConstantSDNode *CMOVTrue =
2862 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2863 const ConstantSDNode *CMOVFalse =
2864 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2865
2866 if (CMOVTrue && CMOVFalse) {
2867 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2868 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2869
2870 SDValue True;
2871 SDValue False;
2872 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2873 True = SelectTrue;
2874 False = SelectFalse;
2875 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2876 True = SelectFalse;
2877 False = SelectTrue;
2878 }
2879
2880 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002881 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002882 SDValue ARMcc = Cond.getOperand(2);
2883 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002884 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002885 assert(True.getValueType() == VT);
2886 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002887 }
2888 }
2889 }
2890
Dan Gohmandb953892012-02-24 00:09:36 +00002891 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2892 // undefined bits before doing a full-word comparison with zero.
2893 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2894 DAG.getConstant(1, Cond.getValueType()));
2895
Bill Wendlingde2b1512010-08-11 08:43:16 +00002896 return DAG.getSelectCC(dl, Cond,
2897 DAG.getConstant(0, Cond.getValueType()),
2898 SelectTrue, SelectFalse, ISD::SETNE);
2899}
2900
Dan Gohmand858e902010-04-17 15:26:15 +00002901SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002902 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002903 SDValue LHS = Op.getOperand(0);
2904 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002905 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue TrueVal = Op.getOperand(2);
2907 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002908 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002909
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002911 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002912 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002913 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002914 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002915 }
2916
2917 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002918 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002919
Evan Cheng218977b2010-07-13 19:27:42 +00002920 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2921 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002922 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002923 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002924 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002926 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002927 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002928 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002929 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002930 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002931 }
2932 return Result;
2933}
2934
Evan Cheng218977b2010-07-13 19:27:42 +00002935/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2936/// to morph to an integer compare sequence.
2937static bool canChangeToInt(SDValue Op, bool &SeenZero,
2938 const ARMSubtarget *Subtarget) {
2939 SDNode *N = Op.getNode();
2940 if (!N->hasOneUse())
2941 // Otherwise it requires moving the value from fp to integer registers.
2942 return false;
2943 if (!N->getNumValues())
2944 return false;
2945 EVT VT = Op.getValueType();
2946 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2947 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2948 // vmrs are very slow, e.g. cortex-a8.
2949 return false;
2950
2951 if (isFloatingPointZero(Op)) {
2952 SeenZero = true;
2953 return true;
2954 }
2955 return ISD::isNormalLoad(N);
2956}
2957
2958static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2959 if (isFloatingPointZero(Op))
2960 return DAG.getConstant(0, MVT::i32);
2961
2962 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2963 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002964 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002965 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002966 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002967
2968 llvm_unreachable("Unknown VFP cmp argument!");
2969}
2970
2971static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2972 SDValue &RetVal1, SDValue &RetVal2) {
2973 if (isFloatingPointZero(Op)) {
2974 RetVal1 = DAG.getConstant(0, MVT::i32);
2975 RetVal2 = DAG.getConstant(0, MVT::i32);
2976 return;
2977 }
2978
2979 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2980 SDValue Ptr = Ld->getBasePtr();
2981 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2982 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002983 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002984 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002985 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002986
2987 EVT PtrType = Ptr.getValueType();
2988 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2989 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2990 PtrType, Ptr, DAG.getConstant(4, PtrType));
2991 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2992 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002993 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002994 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002995 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002996 return;
2997 }
2998
2999 llvm_unreachable("Unknown VFP cmp argument!");
3000}
3001
3002/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3003/// f32 and even f64 comparisons to integer ones.
3004SDValue
3005ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3006 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003007 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003008 SDValue LHS = Op.getOperand(2);
3009 SDValue RHS = Op.getOperand(3);
3010 SDValue Dest = Op.getOperand(4);
3011 DebugLoc dl = Op.getDebugLoc();
3012
Evan Chengfc501a32012-03-01 23:27:13 +00003013 bool LHSSeenZero = false;
3014 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3015 bool RHSSeenZero = false;
3016 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3017 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003018 // If unsafe fp math optimization is enabled and there are no other uses of
3019 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003020 // to an integer comparison.
3021 if (CC == ISD::SETOEQ)
3022 CC = ISD::SETEQ;
3023 else if (CC == ISD::SETUNE)
3024 CC = ISD::SETNE;
3025
Evan Chengfc501a32012-03-01 23:27:13 +00003026 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003027 SDValue ARMcc;
3028 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003029 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3030 bitcastf32Toi32(LHS, DAG), Mask);
3031 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3032 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003033 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3034 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3035 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3036 Chain, Dest, ARMcc, CCR, Cmp);
3037 }
3038
3039 SDValue LHS1, LHS2;
3040 SDValue RHS1, RHS2;
3041 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3042 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003043 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3044 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003045 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3046 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003047 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003048 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3049 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3050 }
3051
3052 return SDValue();
3053}
3054
3055SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3056 SDValue Chain = Op.getOperand(0);
3057 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3058 SDValue LHS = Op.getOperand(2);
3059 SDValue RHS = Op.getOperand(3);
3060 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003061 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003062
Owen Anderson825b72b2009-08-11 20:47:22 +00003063 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003064 SDValue ARMcc;
3065 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003068 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003069 }
3070
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003072
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003073 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003074 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3075 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3076 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3077 if (Result.getNode())
3078 return Result;
3079 }
3080
Evan Chenga8e29892007-01-19 07:51:42 +00003081 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003082 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003083
Evan Cheng218977b2010-07-13 19:27:42 +00003084 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3085 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003087 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003088 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003089 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003090 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003091 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3092 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003093 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003094 }
3095 return Res;
3096}
3097
Dan Gohmand858e902010-04-17 15:26:15 +00003098SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003099 SDValue Chain = Op.getOperand(0);
3100 SDValue Table = Op.getOperand(1);
3101 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003102 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003103
Owen Andersone50ed302009-08-10 22:56:29 +00003104 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003105 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3106 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003107 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003110 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3111 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003112 if (Subtarget->isThumb2()) {
3113 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3114 // which does another jump to the destination. This also makes it easier
3115 // to translate it to TBB / TBH later.
3116 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003117 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003118 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003119 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003120 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003121 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003122 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003123 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003124 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003125 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003126 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003127 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003128 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003129 MachinePointerInfo::getJumpTable(),
3130 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003131 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003132 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003133 }
Evan Chenga8e29892007-01-19 07:51:42 +00003134}
3135
Eli Friedman14e809c2011-11-09 23:36:02 +00003136static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003137 EVT VT = Op.getValueType();
3138 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003139
James Molloy873fd5f2012-02-20 09:24:05 +00003140 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3141 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3142 return Op;
3143 return DAG.UnrollVectorOp(Op.getNode());
3144 }
3145
3146 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3147 "Invalid type for custom lowering!");
3148 if (VT != MVT::v4i16)
3149 return DAG.UnrollVectorOp(Op.getNode());
3150
3151 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3152 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003153}
3154
Bob Wilson76a312b2010-03-19 22:51:32 +00003155static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003156 EVT VT = Op.getValueType();
3157 if (VT.isVector())
3158 return LowerVectorFP_TO_INT(Op, DAG);
3159
Bob Wilson76a312b2010-03-19 22:51:32 +00003160 DebugLoc dl = Op.getDebugLoc();
3161 unsigned Opc;
3162
3163 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003164 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003165 case ISD::FP_TO_SINT:
3166 Opc = ARMISD::FTOSI;
3167 break;
3168 case ISD::FP_TO_UINT:
3169 Opc = ARMISD::FTOUI;
3170 break;
3171 }
3172 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003173 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003174}
3175
Cameron Zwarich3007d332011-03-29 21:41:55 +00003176static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3177 EVT VT = Op.getValueType();
3178 DebugLoc dl = Op.getDebugLoc();
3179
Eli Friedman14e809c2011-11-09 23:36:02 +00003180 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3181 if (VT.getVectorElementType() == MVT::f32)
3182 return Op;
3183 return DAG.UnrollVectorOp(Op.getNode());
3184 }
3185
Duncan Sands1f6a3292011-08-12 14:54:45 +00003186 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3187 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003188 if (VT != MVT::v4f32)
3189 return DAG.UnrollVectorOp(Op.getNode());
3190
3191 unsigned CastOpc;
3192 unsigned Opc;
3193 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003194 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003195 case ISD::SINT_TO_FP:
3196 CastOpc = ISD::SIGN_EXTEND;
3197 Opc = ISD::SINT_TO_FP;
3198 break;
3199 case ISD::UINT_TO_FP:
3200 CastOpc = ISD::ZERO_EXTEND;
3201 Opc = ISD::UINT_TO_FP;
3202 break;
3203 }
3204
3205 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3206 return DAG.getNode(Opc, dl, VT, Op);
3207}
3208
Bob Wilson76a312b2010-03-19 22:51:32 +00003209static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3210 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003211 if (VT.isVector())
3212 return LowerVectorINT_TO_FP(Op, DAG);
3213
Bob Wilson76a312b2010-03-19 22:51:32 +00003214 DebugLoc dl = Op.getDebugLoc();
3215 unsigned Opc;
3216
3217 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003218 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003219 case ISD::SINT_TO_FP:
3220 Opc = ARMISD::SITOF;
3221 break;
3222 case ISD::UINT_TO_FP:
3223 Opc = ARMISD::UITOF;
3224 break;
3225 }
3226
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003227 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003228 return DAG.getNode(Opc, dl, VT, Op);
3229}
3230
Evan Cheng515fe3a2010-07-08 02:08:50 +00003231SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003232 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003233 SDValue Tmp0 = Op.getOperand(0);
3234 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003235 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003236 EVT VT = Op.getValueType();
3237 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003238 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3239 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3240 bool UseNEON = !InGPR && Subtarget->hasNEON();
3241
3242 if (UseNEON) {
3243 // Use VBSL to copy the sign bit.
3244 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3245 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3246 DAG.getTargetConstant(EncodedVal, MVT::i32));
3247 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3248 if (VT == MVT::f64)
3249 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3250 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3251 DAG.getConstant(32, MVT::i32));
3252 else /*if (VT == MVT::f32)*/
3253 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3254 if (SrcVT == MVT::f32) {
3255 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3256 if (VT == MVT::f64)
3257 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3258 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3259 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003260 } else if (VT == MVT::f32)
3261 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3262 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3263 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003264 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3265 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3266
3267 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3268 MVT::i32);
3269 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3270 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3271 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003272
Evan Chenge573fb32011-02-23 02:24:55 +00003273 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3274 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3275 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003276 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003277 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3278 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3279 DAG.getConstant(0, MVT::i32));
3280 } else {
3281 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3282 }
3283
3284 return Res;
3285 }
Evan Chengc143dd42011-02-11 02:28:55 +00003286
3287 // Bitcast operand 1 to i32.
3288 if (SrcVT == MVT::f64)
3289 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3290 &Tmp1, 1).getValue(1);
3291 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3292
Evan Chenge573fb32011-02-23 02:24:55 +00003293 // Or in the signbit with integer operations.
3294 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3295 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3296 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3297 if (VT == MVT::f32) {
3298 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3299 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3300 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3301 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003302 }
3303
Evan Chenge573fb32011-02-23 02:24:55 +00003304 // f64: Or the high part with signbit and then combine two parts.
3305 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3306 &Tmp0, 1);
3307 SDValue Lo = Tmp0.getValue(0);
3308 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3309 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3310 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003311}
3312
Evan Cheng2457f2c2010-05-22 01:47:14 +00003313SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3314 MachineFunction &MF = DAG.getMachineFunction();
3315 MachineFrameInfo *MFI = MF.getFrameInfo();
3316 MFI->setReturnAddressIsTaken(true);
3317
3318 EVT VT = Op.getValueType();
3319 DebugLoc dl = Op.getDebugLoc();
3320 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3321 if (Depth) {
3322 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3323 SDValue Offset = DAG.getConstant(4, MVT::i32);
3324 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3325 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003326 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003327 }
3328
3329 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003330 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003331 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3332}
3333
Dan Gohmand858e902010-04-17 15:26:15 +00003334SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003335 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3336 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003337
Owen Andersone50ed302009-08-10 22:56:29 +00003338 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003339 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3340 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003341 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003342 ? ARM::R7 : ARM::R11;
3343 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3344 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003345 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3346 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003347 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003348 return FrameAddr;
3349}
3350
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003351/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003352/// expand a bit convert where either the source or destination type is i64 to
3353/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3354/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3355/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003356static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3358 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003359 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003360
Bob Wilson9f3f0612010-04-17 05:30:19 +00003361 // This function is only supposed to be called for i64 types, either as the
3362 // source or destination of the bit convert.
3363 EVT SrcVT = Op.getValueType();
3364 EVT DstVT = N->getValueType(0);
3365 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003366 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003367
Bob Wilson9f3f0612010-04-17 05:30:19 +00003368 // Turn i64->f64 into VMOVDRR.
3369 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3371 DAG.getConstant(0, MVT::i32));
3372 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3373 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003374 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003375 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003376 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003377
Jim Grosbache5165492009-11-09 00:11:35 +00003378 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003379 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3380 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3381 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3382 // Merge the pieces into a single i64 value.
3383 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3384 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003385
Bob Wilson9f3f0612010-04-17 05:30:19 +00003386 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003387}
3388
Bob Wilson5bafff32009-06-22 23:27:02 +00003389/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003390/// Zero vectors are used to represent vector negation and in those cases
3391/// will be implemented with the NEON VNEG instruction. However, VNEG does
3392/// not support i64 elements, so sometimes the zero vectors will need to be
3393/// explicitly constructed. Regardless, use a canonical VMOV to create the
3394/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003395static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003396 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003397 // The canonical modified immediate encoding of a zero vector is....0!
3398 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3399 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3400 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003401 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003402}
3403
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003404/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3405/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003406SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3407 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003408 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3409 EVT VT = Op.getValueType();
3410 unsigned VTBits = VT.getSizeInBits();
3411 DebugLoc dl = Op.getDebugLoc();
3412 SDValue ShOpLo = Op.getOperand(0);
3413 SDValue ShOpHi = Op.getOperand(1);
3414 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003415 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003416 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003417
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003418 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3419
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003420 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3421 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3422 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3423 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3424 DAG.getConstant(VTBits, MVT::i32));
3425 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3426 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003427 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003428
3429 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3430 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003431 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003432 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003433 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003434 CCR, Cmp);
3435
3436 SDValue Ops[2] = { Lo, Hi };
3437 return DAG.getMergeValues(Ops, 2, dl);
3438}
3439
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003440/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3441/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003442SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3443 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003444 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3445 EVT VT = Op.getValueType();
3446 unsigned VTBits = VT.getSizeInBits();
3447 DebugLoc dl = Op.getDebugLoc();
3448 SDValue ShOpLo = Op.getOperand(0);
3449 SDValue ShOpHi = Op.getOperand(1);
3450 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003451 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003452
3453 assert(Op.getOpcode() == ISD::SHL_PARTS);
3454 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3455 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3456 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3457 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3458 DAG.getConstant(VTBits, MVT::i32));
3459 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3460 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3461
3462 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3463 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3464 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003465 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003466 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003467 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003468 CCR, Cmp);
3469
3470 SDValue Ops[2] = { Lo, Hi };
3471 return DAG.getMergeValues(Ops, 2, dl);
3472}
3473
Jim Grosbach4725ca72010-09-08 03:54:02 +00003474SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003475 SelectionDAG &DAG) const {
3476 // The rounding mode is in bits 23:22 of the FPSCR.
3477 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3478 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3479 // so that the shift + and get folded into a bitfield extract.
3480 DebugLoc dl = Op.getDebugLoc();
3481 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3482 DAG.getConstant(Intrinsic::arm_get_fpscr,
3483 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003484 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003485 DAG.getConstant(1U << 22, MVT::i32));
3486 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3487 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003488 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003489 DAG.getConstant(3, MVT::i32));
3490}
3491
Jim Grosbach3482c802010-01-18 19:58:49 +00003492static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3493 const ARMSubtarget *ST) {
3494 EVT VT = N->getValueType(0);
3495 DebugLoc dl = N->getDebugLoc();
3496
3497 if (!ST->hasV6T2Ops())
3498 return SDValue();
3499
3500 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3501 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3502}
3503
Bob Wilson5bafff32009-06-22 23:27:02 +00003504static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3505 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003506 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003507 DebugLoc dl = N->getDebugLoc();
3508
Bob Wilsond5448bb2010-11-18 21:16:28 +00003509 if (!VT.isVector())
3510 return SDValue();
3511
Bob Wilson5bafff32009-06-22 23:27:02 +00003512 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003513 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003514
Bob Wilsond5448bb2010-11-18 21:16:28 +00003515 // Left shifts translate directly to the vshiftu intrinsic.
3516 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003517 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003518 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3519 N->getOperand(0), N->getOperand(1));
3520
3521 assert((N->getOpcode() == ISD::SRA ||
3522 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3523
3524 // NEON uses the same intrinsics for both left and right shifts. For
3525 // right shifts, the shift amounts are negative, so negate the vector of
3526 // shift amounts.
3527 EVT ShiftVT = N->getOperand(1).getValueType();
3528 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3529 getZeroVector(ShiftVT, DAG, dl),
3530 N->getOperand(1));
3531 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3532 Intrinsic::arm_neon_vshifts :
3533 Intrinsic::arm_neon_vshiftu);
3534 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3535 DAG.getConstant(vshiftInt, MVT::i32),
3536 N->getOperand(0), NegatedCount);
3537}
3538
3539static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3540 const ARMSubtarget *ST) {
3541 EVT VT = N->getValueType(0);
3542 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003543
Eli Friedmance392eb2009-08-22 03:13:10 +00003544 // We can get here for a node like i32 = ISD::SHL i32, i64
3545 if (VT != MVT::i64)
3546 return SDValue();
3547
3548 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003549 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003550
Chris Lattner27a6c732007-11-24 07:07:01 +00003551 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3552 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003553 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003554 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003555
Chris Lattner27a6c732007-11-24 07:07:01 +00003556 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003557 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattner27a6c732007-11-24 07:07:01 +00003559 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003561 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003563 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003564
Chris Lattner27a6c732007-11-24 07:07:01 +00003565 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3566 // captures the result into a carry flag.
3567 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003568 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003569
Chris Lattner27a6c732007-11-24 07:07:01 +00003570 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003572
Chris Lattner27a6c732007-11-24 07:07:01 +00003573 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003575}
3576
Bob Wilson5bafff32009-06-22 23:27:02 +00003577static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3578 SDValue TmpOp0, TmpOp1;
3579 bool Invert = false;
3580 bool Swap = false;
3581 unsigned Opc = 0;
3582
3583 SDValue Op0 = Op.getOperand(0);
3584 SDValue Op1 = Op.getOperand(1);
3585 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003586 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003587 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3588 DebugLoc dl = Op.getDebugLoc();
3589
3590 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3591 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003592 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003593 case ISD::SETUNE:
3594 case ISD::SETNE: Invert = true; // Fallthrough
3595 case ISD::SETOEQ:
3596 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3597 case ISD::SETOLT:
3598 case ISD::SETLT: Swap = true; // Fallthrough
3599 case ISD::SETOGT:
3600 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3601 case ISD::SETOLE:
3602 case ISD::SETLE: Swap = true; // Fallthrough
3603 case ISD::SETOGE:
3604 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3605 case ISD::SETUGE: Swap = true; // Fallthrough
3606 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3607 case ISD::SETUGT: Swap = true; // Fallthrough
3608 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3609 case ISD::SETUEQ: Invert = true; // Fallthrough
3610 case ISD::SETONE:
3611 // Expand this to (OLT | OGT).
3612 TmpOp0 = Op0;
3613 TmpOp1 = Op1;
3614 Opc = ISD::OR;
3615 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3616 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3617 break;
3618 case ISD::SETUO: Invert = true; // Fallthrough
3619 case ISD::SETO:
3620 // Expand this to (OLT | OGE).
3621 TmpOp0 = Op0;
3622 TmpOp1 = Op1;
3623 Opc = ISD::OR;
3624 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3625 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3626 break;
3627 }
3628 } else {
3629 // Integer comparisons.
3630 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003631 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003632 case ISD::SETNE: Invert = true;
3633 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3634 case ISD::SETLT: Swap = true;
3635 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3636 case ISD::SETLE: Swap = true;
3637 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3638 case ISD::SETULT: Swap = true;
3639 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3640 case ISD::SETULE: Swap = true;
3641 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3642 }
3643
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003644 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003645 if (Opc == ARMISD::VCEQ) {
3646
3647 SDValue AndOp;
3648 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3649 AndOp = Op0;
3650 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3651 AndOp = Op1;
3652
3653 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003654 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003655 AndOp = AndOp.getOperand(0);
3656
3657 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3658 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003659 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3660 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003661 Invert = !Invert;
3662 }
3663 }
3664 }
3665
3666 if (Swap)
3667 std::swap(Op0, Op1);
3668
Owen Andersonc24cb352010-11-08 23:21:22 +00003669 // If one of the operands is a constant vector zero, attempt to fold the
3670 // comparison to a specialized compare-against-zero form.
3671 SDValue SingleOp;
3672 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3673 SingleOp = Op0;
3674 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3675 if (Opc == ARMISD::VCGE)
3676 Opc = ARMISD::VCLEZ;
3677 else if (Opc == ARMISD::VCGT)
3678 Opc = ARMISD::VCLTZ;
3679 SingleOp = Op1;
3680 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003681
Owen Andersonc24cb352010-11-08 23:21:22 +00003682 SDValue Result;
3683 if (SingleOp.getNode()) {
3684 switch (Opc) {
3685 case ARMISD::VCEQ:
3686 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3687 case ARMISD::VCGE:
3688 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3689 case ARMISD::VCLEZ:
3690 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3691 case ARMISD::VCGT:
3692 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3693 case ARMISD::VCLTZ:
3694 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3695 default:
3696 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3697 }
3698 } else {
3699 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3700 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003701
3702 if (Invert)
3703 Result = DAG.getNOT(dl, Result, VT);
3704
3705 return Result;
3706}
3707
Bob Wilsond3c42842010-06-14 22:19:57 +00003708/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3709/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003710/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003711static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3712 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003713 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003714 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003715
Bob Wilson827b2102010-06-15 19:05:35 +00003716 // SplatBitSize is set to the smallest size that splats the vector, so a
3717 // zero vector will always have SplatBitSize == 8. However, NEON modified
3718 // immediate instructions others than VMOV do not support the 8-bit encoding
3719 // of a zero vector, and the default encoding of zero is supposed to be the
3720 // 32-bit version.
3721 if (SplatBits == 0)
3722 SplatBitSize = 32;
3723
Bob Wilson5bafff32009-06-22 23:27:02 +00003724 switch (SplatBitSize) {
3725 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003726 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003727 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003728 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003729 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003730 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003732 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003733 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003734
3735 case 16:
3736 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003737 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003738 if ((SplatBits & ~0xff) == 0) {
3739 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003740 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003741 Imm = SplatBits;
3742 break;
3743 }
3744 if ((SplatBits & ~0xff00) == 0) {
3745 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003746 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003747 Imm = SplatBits >> 8;
3748 break;
3749 }
3750 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003751
3752 case 32:
3753 // NEON's 32-bit VMOV supports splat values where:
3754 // * only one byte is nonzero, or
3755 // * the least significant byte is 0xff and the second byte is nonzero, or
3756 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003757 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003758 if ((SplatBits & ~0xff) == 0) {
3759 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003760 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003761 Imm = SplatBits;
3762 break;
3763 }
3764 if ((SplatBits & ~0xff00) == 0) {
3765 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003766 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003767 Imm = SplatBits >> 8;
3768 break;
3769 }
3770 if ((SplatBits & ~0xff0000) == 0) {
3771 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003772 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003773 Imm = SplatBits >> 16;
3774 break;
3775 }
3776 if ((SplatBits & ~0xff000000) == 0) {
3777 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003778 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003779 Imm = SplatBits >> 24;
3780 break;
3781 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003782
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003783 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3784 if (type == OtherModImm) return SDValue();
3785
Bob Wilson5bafff32009-06-22 23:27:02 +00003786 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003787 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3788 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003789 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003790 Imm = SplatBits >> 8;
3791 SplatBits |= 0xff;
3792 break;
3793 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003794
3795 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003796 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3797 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003798 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003799 Imm = SplatBits >> 16;
3800 SplatBits |= 0xffff;
3801 break;
3802 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003803
3804 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3805 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3806 // VMOV.I32. A (very) minor optimization would be to replicate the value
3807 // and fall through here to test for a valid 64-bit splat. But, then the
3808 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003809 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003810
3811 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003812 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003813 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003814 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003815 uint64_t BitMask = 0xff;
3816 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003817 unsigned ImmMask = 1;
3818 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003821 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003822 Imm |= ImmMask;
3823 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003826 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003828 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003829 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003830 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003831 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003832 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003833 break;
3834 }
3835
Bob Wilson1a913ed2010-06-11 21:34:50 +00003836 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003837 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003838 }
3839
Bob Wilsoncba270d2010-07-13 21:16:48 +00003840 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3841 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003842}
3843
Lang Hamesc0a9f822012-03-29 21:56:11 +00003844SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3845 const ARMSubtarget *ST) const {
3846 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3847 return SDValue();
3848
3849 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3850 assert(Op.getValueType() == MVT::f32 &&
3851 "ConstantFP custom lowering should only occur for f32.");
3852
3853 // Try splatting with a VMOV.f32...
3854 APFloat FPVal = CFP->getValueAPF();
3855 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3856 if (ImmVal != -1) {
3857 DebugLoc DL = Op.getDebugLoc();
3858 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3859 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3860 NewVal);
3861 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3862 DAG.getConstant(0, MVT::i32));
3863 }
3864
3865 // If that fails, try a VMOV.i32
3866 EVT VMovVT;
3867 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3868 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3869 VMOVModImm);
3870 if (NewVal != SDValue()) {
3871 DebugLoc DL = Op.getDebugLoc();
3872 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3873 NewVal);
3874 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3875 VecConstant);
3876 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3877 DAG.getConstant(0, MVT::i32));
3878 }
3879
3880 // Finally, try a VMVN.i32
3881 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3882 VMVNModImm);
3883 if (NewVal != SDValue()) {
3884 DebugLoc DL = Op.getDebugLoc();
3885 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3886 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3887 VecConstant);
3888 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3889 DAG.getConstant(0, MVT::i32));
3890 }
3891
3892 return SDValue();
3893}
3894
3895
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003896static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003897 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003898 unsigned NumElts = VT.getVectorNumElements();
3899 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003900
3901 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3902 if (M[0] < 0)
3903 return false;
3904
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003905 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003906
3907 // If this is a VEXT shuffle, the immediate value is the index of the first
3908 // element. The other shuffle indices must be the successive elements after
3909 // the first one.
3910 unsigned ExpectedElt = Imm;
3911 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003912 // Increment the expected index. If it wraps around, it may still be
3913 // a VEXT but the source vectors must be swapped.
3914 ExpectedElt += 1;
3915 if (ExpectedElt == NumElts * 2) {
3916 ExpectedElt = 0;
3917 ReverseVEXT = true;
3918 }
3919
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003920 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003921 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003922 return false;
3923 }
3924
3925 // Adjust the index value if the source operands will be swapped.
3926 if (ReverseVEXT)
3927 Imm -= NumElts;
3928
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003929 return true;
3930}
3931
Bob Wilson8bb9e482009-07-26 00:39:34 +00003932/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3933/// instruction with the specified blocksize. (The order of the elements
3934/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003935static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003936 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3937 "Only possible block sizes for VREV are: 16, 32, 64");
3938
Bob Wilson8bb9e482009-07-26 00:39:34 +00003939 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003940 if (EltSz == 64)
3941 return false;
3942
3943 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003944 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003945 // If the first shuffle index is UNDEF, be optimistic.
3946 if (M[0] < 0)
3947 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003948
3949 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3950 return false;
3951
3952 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003953 if (M[i] < 0) continue; // ignore UNDEF indices
3954 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003955 return false;
3956 }
3957
3958 return true;
3959}
3960
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003961static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003962 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3963 // range, then 0 is placed into the resulting vector. So pretty much any mask
3964 // of 8 elements can work here.
3965 return VT == MVT::v8i8 && M.size() == 8;
3966}
3967
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003968static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003969 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3970 if (EltSz == 64)
3971 return false;
3972
Bob Wilsonc692cb72009-08-21 20:54:19 +00003973 unsigned NumElts = VT.getVectorNumElements();
3974 WhichResult = (M[0] == 0 ? 0 : 1);
3975 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003976 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3977 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003978 return false;
3979 }
3980 return true;
3981}
3982
Bob Wilson324f4f12009-12-03 06:40:55 +00003983/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3984/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3985/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003986static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003987 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3988 if (EltSz == 64)
3989 return false;
3990
3991 unsigned NumElts = VT.getVectorNumElements();
3992 WhichResult = (M[0] == 0 ? 0 : 1);
3993 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003994 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3995 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003996 return false;
3997 }
3998 return true;
3999}
4000
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004001static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004002 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4003 if (EltSz == 64)
4004 return false;
4005
Bob Wilsonc692cb72009-08-21 20:54:19 +00004006 unsigned NumElts = VT.getVectorNumElements();
4007 WhichResult = (M[0] == 0 ? 0 : 1);
4008 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004009 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004010 if ((unsigned) M[i] != 2 * i + WhichResult)
4011 return false;
4012 }
4013
4014 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004015 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004016 return false;
4017
4018 return true;
4019}
4020
Bob Wilson324f4f12009-12-03 06:40:55 +00004021/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4022/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4023/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004024static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004025 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4026 if (EltSz == 64)
4027 return false;
4028
4029 unsigned Half = VT.getVectorNumElements() / 2;
4030 WhichResult = (M[0] == 0 ? 0 : 1);
4031 for (unsigned j = 0; j != 2; ++j) {
4032 unsigned Idx = WhichResult;
4033 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004034 int MIdx = M[i + j * Half];
4035 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004036 return false;
4037 Idx += 2;
4038 }
4039 }
4040
4041 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4042 if (VT.is64BitVector() && EltSz == 32)
4043 return false;
4044
4045 return true;
4046}
4047
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004048static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004049 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4050 if (EltSz == 64)
4051 return false;
4052
Bob Wilsonc692cb72009-08-21 20:54:19 +00004053 unsigned NumElts = VT.getVectorNumElements();
4054 WhichResult = (M[0] == 0 ? 0 : 1);
4055 unsigned Idx = WhichResult * NumElts / 2;
4056 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004057 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4058 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004059 return false;
4060 Idx += 1;
4061 }
4062
4063 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004064 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004065 return false;
4066
4067 return true;
4068}
4069
Bob Wilson324f4f12009-12-03 06:40:55 +00004070/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4071/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4072/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004073static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004074 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4075 if (EltSz == 64)
4076 return false;
4077
4078 unsigned NumElts = VT.getVectorNumElements();
4079 WhichResult = (M[0] == 0 ? 0 : 1);
4080 unsigned Idx = WhichResult * NumElts / 2;
4081 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004082 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4083 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004084 return false;
4085 Idx += 1;
4086 }
4087
4088 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4089 if (VT.is64BitVector() && EltSz == 32)
4090 return false;
4091
4092 return true;
4093}
4094
Dale Johannesenf630c712010-07-29 20:10:08 +00004095// If N is an integer constant that can be moved into a register in one
4096// instruction, return an SDValue of such a constant (will become a MOV
4097// instruction). Otherwise return null.
4098static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4099 const ARMSubtarget *ST, DebugLoc dl) {
4100 uint64_t Val;
4101 if (!isa<ConstantSDNode>(N))
4102 return SDValue();
4103 Val = cast<ConstantSDNode>(N)->getZExtValue();
4104
4105 if (ST->isThumb1Only()) {
4106 if (Val <= 255 || ~Val <= 255)
4107 return DAG.getConstant(Val, MVT::i32);
4108 } else {
4109 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4110 return DAG.getConstant(Val, MVT::i32);
4111 }
4112 return SDValue();
4113}
4114
Bob Wilson5bafff32009-06-22 23:27:02 +00004115// If this is a case we can't handle, return null and let the default
4116// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004117SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4118 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004119 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004120 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004121 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004122
4123 APInt SplatBits, SplatUndef;
4124 unsigned SplatBitSize;
4125 bool HasAnyUndefs;
4126 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004127 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004128 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004129 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004130 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004131 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004132 DAG, VmovVT, VT.is128BitVector(),
4133 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004134 if (Val.getNode()) {
4135 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004136 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004137 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004138
4139 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004140 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004141 Val = isNEONModifiedImm(NegatedImm,
4142 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004143 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004144 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004145 if (Val.getNode()) {
4146 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004147 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004148 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004149
4150 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004151 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004152 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004153 if (ImmVal != -1) {
4154 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4155 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4156 }
4157 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004158 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004159 }
4160
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004161 // Scan through the operands to see if only one value is used.
4162 unsigned NumElts = VT.getVectorNumElements();
4163 bool isOnlyLowElement = true;
4164 bool usesOnlyOneValue = true;
4165 bool isConstant = true;
4166 SDValue Value;
4167 for (unsigned i = 0; i < NumElts; ++i) {
4168 SDValue V = Op.getOperand(i);
4169 if (V.getOpcode() == ISD::UNDEF)
4170 continue;
4171 if (i > 0)
4172 isOnlyLowElement = false;
4173 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4174 isConstant = false;
4175
4176 if (!Value.getNode())
4177 Value = V;
4178 else if (V != Value)
4179 usesOnlyOneValue = false;
4180 }
4181
4182 if (!Value.getNode())
4183 return DAG.getUNDEF(VT);
4184
4185 if (isOnlyLowElement)
4186 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4187
Dale Johannesenf630c712010-07-29 20:10:08 +00004188 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4189
Dale Johannesen575cd142010-10-19 20:00:17 +00004190 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4191 // i32 and try again.
4192 if (usesOnlyOneValue && EltSize <= 32) {
4193 if (!isConstant)
4194 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4195 if (VT.getVectorElementType().isFloatingPoint()) {
4196 SmallVector<SDValue, 8> Ops;
4197 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004198 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004199 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004200 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4201 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004202 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4203 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004204 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004205 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004206 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4207 if (Val.getNode())
4208 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004209 }
4210
4211 // If all elements are constants and the case above didn't get hit, fall back
4212 // to the default expansion, which will generate a load from the constant
4213 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004214 if (isConstant)
4215 return SDValue();
4216
Bob Wilson11a1dff2011-01-07 21:37:30 +00004217 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4218 if (NumElts >= 4) {
4219 SDValue shuffle = ReconstructShuffle(Op, DAG);
4220 if (shuffle != SDValue())
4221 return shuffle;
4222 }
4223
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004224 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004225 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4226 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004227 if (EltSize >= 32) {
4228 // Do the expansion with floating-point types, since that is what the VFP
4229 // registers are defined to use, and since i64 is not legal.
4230 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4231 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004232 SmallVector<SDValue, 8> Ops;
4233 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004234 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004235 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004236 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004237 }
4238
4239 return SDValue();
4240}
4241
Bob Wilson11a1dff2011-01-07 21:37:30 +00004242// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004243// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004244SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4245 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004246 DebugLoc dl = Op.getDebugLoc();
4247 EVT VT = Op.getValueType();
4248 unsigned NumElts = VT.getVectorNumElements();
4249
4250 SmallVector<SDValue, 2> SourceVecs;
4251 SmallVector<unsigned, 2> MinElts;
4252 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004253
Bob Wilson11a1dff2011-01-07 21:37:30 +00004254 for (unsigned i = 0; i < NumElts; ++i) {
4255 SDValue V = Op.getOperand(i);
4256 if (V.getOpcode() == ISD::UNDEF)
4257 continue;
4258 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4259 // A shuffle can only come from building a vector from various
4260 // elements of other vectors.
4261 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004262 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4263 VT.getVectorElementType()) {
4264 // This code doesn't know how to handle shuffles where the vector
4265 // element types do not match (this happens because type legalization
4266 // promotes the return type of EXTRACT_VECTOR_ELT).
4267 // FIXME: It might be appropriate to extend this code to handle
4268 // mismatched types.
4269 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004270 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004271
Bob Wilson11a1dff2011-01-07 21:37:30 +00004272 // Record this extraction against the appropriate vector if possible...
4273 SDValue SourceVec = V.getOperand(0);
4274 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4275 bool FoundSource = false;
4276 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4277 if (SourceVecs[j] == SourceVec) {
4278 if (MinElts[j] > EltNo)
4279 MinElts[j] = EltNo;
4280 if (MaxElts[j] < EltNo)
4281 MaxElts[j] = EltNo;
4282 FoundSource = true;
4283 break;
4284 }
4285 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004286
Bob Wilson11a1dff2011-01-07 21:37:30 +00004287 // Or record a new source if not...
4288 if (!FoundSource) {
4289 SourceVecs.push_back(SourceVec);
4290 MinElts.push_back(EltNo);
4291 MaxElts.push_back(EltNo);
4292 }
4293 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004294
Bob Wilson11a1dff2011-01-07 21:37:30 +00004295 // Currently only do something sane when at most two source vectors
4296 // involved.
4297 if (SourceVecs.size() > 2)
4298 return SDValue();
4299
4300 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4301 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004302
Bob Wilson11a1dff2011-01-07 21:37:30 +00004303 // This loop extracts the usage patterns of the source vectors
4304 // and prepares appropriate SDValues for a shuffle if possible.
4305 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4306 if (SourceVecs[i].getValueType() == VT) {
4307 // No VEXT necessary
4308 ShuffleSrcs[i] = SourceVecs[i];
4309 VEXTOffsets[i] = 0;
4310 continue;
4311 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4312 // It probably isn't worth padding out a smaller vector just to
4313 // break it down again in a shuffle.
4314 return SDValue();
4315 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004316
Bob Wilson11a1dff2011-01-07 21:37:30 +00004317 // Since only 64-bit and 128-bit vectors are legal on ARM and
4318 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004319 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4320 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004321
Bob Wilson11a1dff2011-01-07 21:37:30 +00004322 if (MaxElts[i] - MinElts[i] >= NumElts) {
4323 // Span too large for a VEXT to cope
4324 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004325 }
4326
Bob Wilson11a1dff2011-01-07 21:37:30 +00004327 if (MinElts[i] >= NumElts) {
4328 // The extraction can just take the second half
4329 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004330 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4331 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004332 DAG.getIntPtrConstant(NumElts));
4333 } else if (MaxElts[i] < NumElts) {
4334 // The extraction can just take the first half
4335 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004336 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4337 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004338 DAG.getIntPtrConstant(0));
4339 } else {
4340 // An actual VEXT is needed
4341 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004342 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4343 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004344 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004345 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4346 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004347 DAG.getIntPtrConstant(NumElts));
4348 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4349 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4350 }
4351 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004352
Bob Wilson11a1dff2011-01-07 21:37:30 +00004353 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004354
Bob Wilson11a1dff2011-01-07 21:37:30 +00004355 for (unsigned i = 0; i < NumElts; ++i) {
4356 SDValue Entry = Op.getOperand(i);
4357 if (Entry.getOpcode() == ISD::UNDEF) {
4358 Mask.push_back(-1);
4359 continue;
4360 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004361
Bob Wilson11a1dff2011-01-07 21:37:30 +00004362 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004363 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4364 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004365 if (ExtractVec == SourceVecs[0]) {
4366 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4367 } else {
4368 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4369 }
4370 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004371
Bob Wilson11a1dff2011-01-07 21:37:30 +00004372 // Final check before we try to produce nonsense...
4373 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004374 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4375 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004376
Bob Wilson11a1dff2011-01-07 21:37:30 +00004377 return SDValue();
4378}
4379
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004380/// isShuffleMaskLegal - Targets can use this to indicate that they only
4381/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4382/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4383/// are assumed to be legal.
4384bool
4385ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4386 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004387 if (VT.getVectorNumElements() == 4 &&
4388 (VT.is128BitVector() || VT.is64BitVector())) {
4389 unsigned PFIndexes[4];
4390 for (unsigned i = 0; i != 4; ++i) {
4391 if (M[i] < 0)
4392 PFIndexes[i] = 8;
4393 else
4394 PFIndexes[i] = M[i];
4395 }
4396
4397 // Compute the index in the perfect shuffle table.
4398 unsigned PFTableIndex =
4399 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4400 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4401 unsigned Cost = (PFEntry >> 30);
4402
4403 if (Cost <= 4)
4404 return true;
4405 }
4406
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004407 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004408 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004409
Bob Wilson53dd2452010-06-07 23:53:38 +00004410 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4411 return (EltSize >= 32 ||
4412 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004413 isVREVMask(M, VT, 64) ||
4414 isVREVMask(M, VT, 32) ||
4415 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004416 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004417 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004418 isVTRNMask(M, VT, WhichResult) ||
4419 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004420 isVZIPMask(M, VT, WhichResult) ||
4421 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4422 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4423 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004424}
4425
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004426/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4427/// the specified operations to build the shuffle.
4428static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4429 SDValue RHS, SelectionDAG &DAG,
4430 DebugLoc dl) {
4431 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4432 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4433 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4434
4435 enum {
4436 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4437 OP_VREV,
4438 OP_VDUP0,
4439 OP_VDUP1,
4440 OP_VDUP2,
4441 OP_VDUP3,
4442 OP_VEXT1,
4443 OP_VEXT2,
4444 OP_VEXT3,
4445 OP_VUZPL, // VUZP, left result
4446 OP_VUZPR, // VUZP, right result
4447 OP_VZIPL, // VZIP, left result
4448 OP_VZIPR, // VZIP, right result
4449 OP_VTRNL, // VTRN, left result
4450 OP_VTRNR // VTRN, right result
4451 };
4452
4453 if (OpNum == OP_COPY) {
4454 if (LHSID == (1*9+2)*9+3) return LHS;
4455 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4456 return RHS;
4457 }
4458
4459 SDValue OpLHS, OpRHS;
4460 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4461 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4462 EVT VT = OpLHS.getValueType();
4463
4464 switch (OpNum) {
4465 default: llvm_unreachable("Unknown shuffle opcode!");
4466 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004467 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004468 if (VT.getVectorElementType() == MVT::i32 ||
4469 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004470 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4471 // vrev <4 x i16> -> VREV32
4472 if (VT.getVectorElementType() == MVT::i16)
4473 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4474 // vrev <4 x i8> -> VREV16
4475 assert(VT.getVectorElementType() == MVT::i8);
4476 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004477 case OP_VDUP0:
4478 case OP_VDUP1:
4479 case OP_VDUP2:
4480 case OP_VDUP3:
4481 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004482 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004483 case OP_VEXT1:
4484 case OP_VEXT2:
4485 case OP_VEXT3:
4486 return DAG.getNode(ARMISD::VEXT, dl, VT,
4487 OpLHS, OpRHS,
4488 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4489 case OP_VUZPL:
4490 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004491 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004492 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4493 case OP_VZIPL:
4494 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004495 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004496 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4497 case OP_VTRNL:
4498 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004499 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4500 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004501 }
4502}
4503
Bill Wendling69a05a72011-03-14 23:02:38 +00004504static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004505 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004506 SelectionDAG &DAG) {
4507 // Check to see if we can use the VTBL instruction.
4508 SDValue V1 = Op.getOperand(0);
4509 SDValue V2 = Op.getOperand(1);
4510 DebugLoc DL = Op.getDebugLoc();
4511
4512 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004513 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004514 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4515 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4516
4517 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4518 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4519 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4520 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004521
Owen Anderson76706012011-04-05 21:48:57 +00004522 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004523 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4524 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004525}
4526
Bob Wilson5bafff32009-06-22 23:27:02 +00004527static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004528 SDValue V1 = Op.getOperand(0);
4529 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004530 DebugLoc dl = Op.getDebugLoc();
4531 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004532 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004533
Bob Wilson28865062009-08-13 02:13:04 +00004534 // Convert shuffles that are directly supported on NEON to target-specific
4535 // DAG nodes, instead of keeping them as shuffles and matching them again
4536 // during code selection. This is more efficient and avoids the possibility
4537 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004538 // FIXME: floating-point vectors should be canonicalized to integer vectors
4539 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004540 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004541
Bob Wilson53dd2452010-06-07 23:53:38 +00004542 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4543 if (EltSize <= 32) {
4544 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4545 int Lane = SVN->getSplatIndex();
4546 // If this is undef splat, generate it via "just" vdup, if possible.
4547 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004548
Dan Gohman65fd6562011-11-03 21:49:52 +00004549 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004550 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4551 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4552 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004553 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4554 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4555 // reaches it).
4556 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4557 !isa<ConstantSDNode>(V1.getOperand(0))) {
4558 bool IsScalarToVector = true;
4559 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4560 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4561 IsScalarToVector = false;
4562 break;
4563 }
4564 if (IsScalarToVector)
4565 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4566 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004567 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4568 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004569 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004570
4571 bool ReverseVEXT;
4572 unsigned Imm;
4573 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4574 if (ReverseVEXT)
4575 std::swap(V1, V2);
4576 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4577 DAG.getConstant(Imm, MVT::i32));
4578 }
4579
4580 if (isVREVMask(ShuffleMask, VT, 64))
4581 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4582 if (isVREVMask(ShuffleMask, VT, 32))
4583 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4584 if (isVREVMask(ShuffleMask, VT, 16))
4585 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4586
4587 // Check for Neon shuffles that modify both input vectors in place.
4588 // If both results are used, i.e., if there are two shuffles with the same
4589 // source operands and with masks corresponding to both results of one of
4590 // these operations, DAG memoization will ensure that a single node is
4591 // used for both shuffles.
4592 unsigned WhichResult;
4593 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4594 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4595 V1, V2).getValue(WhichResult);
4596 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4597 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4598 V1, V2).getValue(WhichResult);
4599 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4600 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4601 V1, V2).getValue(WhichResult);
4602
4603 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4604 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4605 V1, V1).getValue(WhichResult);
4606 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4607 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4608 V1, V1).getValue(WhichResult);
4609 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4610 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4611 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004612 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004613
Bob Wilsonc692cb72009-08-21 20:54:19 +00004614 // If the shuffle is not directly supported and it has 4 elements, use
4615 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004616 unsigned NumElts = VT.getVectorNumElements();
4617 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004618 unsigned PFIndexes[4];
4619 for (unsigned i = 0; i != 4; ++i) {
4620 if (ShuffleMask[i] < 0)
4621 PFIndexes[i] = 8;
4622 else
4623 PFIndexes[i] = ShuffleMask[i];
4624 }
4625
4626 // Compute the index in the perfect shuffle table.
4627 unsigned PFTableIndex =
4628 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004629 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4630 unsigned Cost = (PFEntry >> 30);
4631
4632 if (Cost <= 4)
4633 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4634 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004635
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004636 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004637 if (EltSize >= 32) {
4638 // Do the expansion with floating-point types, since that is what the VFP
4639 // registers are defined to use, and since i64 is not legal.
4640 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4641 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004642 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4643 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004644 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004645 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004646 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004647 Ops.push_back(DAG.getUNDEF(EltVT));
4648 else
4649 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4650 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4651 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4652 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004653 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004654 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004655 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004656 }
4657
Bill Wendling69a05a72011-03-14 23:02:38 +00004658 if (VT == MVT::v8i8) {
4659 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4660 if (NewOp.getNode())
4661 return NewOp;
4662 }
4663
Bob Wilson22cac0d2009-08-14 05:16:33 +00004664 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004665}
4666
Eli Friedman5c89cb82011-10-24 23:08:52 +00004667static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4668 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4669 SDValue Lane = Op.getOperand(2);
4670 if (!isa<ConstantSDNode>(Lane))
4671 return SDValue();
4672
4673 return Op;
4674}
4675
Bob Wilson5bafff32009-06-22 23:27:02 +00004676static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004677 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004678 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004679 if (!isa<ConstantSDNode>(Lane))
4680 return SDValue();
4681
4682 SDValue Vec = Op.getOperand(0);
4683 if (Op.getValueType() == MVT::i32 &&
4684 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4685 DebugLoc dl = Op.getDebugLoc();
4686 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4687 }
4688
4689 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004690}
4691
Bob Wilsona6d65862009-08-03 20:36:38 +00004692static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4693 // The only time a CONCAT_VECTORS operation can have legal types is when
4694 // two 64-bit vectors are concatenated to a 128-bit vector.
4695 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4696 "unexpected CONCAT_VECTORS");
4697 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004698 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004699 SDValue Op0 = Op.getOperand(0);
4700 SDValue Op1 = Op.getOperand(1);
4701 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004702 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004703 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004704 DAG.getIntPtrConstant(0));
4705 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004707 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004708 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004709 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004710}
4711
Bob Wilson626613d2010-11-23 19:38:38 +00004712/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4713/// element has been zero/sign-extended, depending on the isSigned parameter,
4714/// from an integer type half its size.
4715static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4716 bool isSigned) {
4717 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4718 EVT VT = N->getValueType(0);
4719 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4720 SDNode *BVN = N->getOperand(0).getNode();
4721 if (BVN->getValueType(0) != MVT::v4i32 ||
4722 BVN->getOpcode() != ISD::BUILD_VECTOR)
4723 return false;
4724 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4725 unsigned HiElt = 1 - LoElt;
4726 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4727 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4728 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4729 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4730 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4731 return false;
4732 if (isSigned) {
4733 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4734 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4735 return true;
4736 } else {
4737 if (Hi0->isNullValue() && Hi1->isNullValue())
4738 return true;
4739 }
4740 return false;
4741 }
4742
4743 if (N->getOpcode() != ISD::BUILD_VECTOR)
4744 return false;
4745
4746 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4747 SDNode *Elt = N->getOperand(i).getNode();
4748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4749 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4750 unsigned HalfSize = EltSize / 2;
4751 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004752 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004753 return false;
4754 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004755 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004756 return false;
4757 }
4758 continue;
4759 }
4760 return false;
4761 }
4762
4763 return true;
4764}
4765
4766/// isSignExtended - Check if a node is a vector value that is sign-extended
4767/// or a constant BUILD_VECTOR with sign-extended elements.
4768static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4769 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4770 return true;
4771 if (isExtendedBUILD_VECTOR(N, DAG, true))
4772 return true;
4773 return false;
4774}
4775
4776/// isZeroExtended - Check if a node is a vector value that is zero-extended
4777/// or a constant BUILD_VECTOR with zero-extended elements.
4778static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4779 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4780 return true;
4781 if (isExtendedBUILD_VECTOR(N, DAG, false))
4782 return true;
4783 return false;
4784}
4785
4786/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4787/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004788static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4789 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4790 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004791 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4792 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4793 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004794 LD->isNonTemporal(), LD->isInvariant(),
4795 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004796 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4797 // have been legalized as a BITCAST from v4i32.
4798 if (N->getOpcode() == ISD::BITCAST) {
4799 SDNode *BVN = N->getOperand(0).getNode();
4800 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4801 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4802 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4803 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4804 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4805 }
4806 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4807 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4808 EVT VT = N->getValueType(0);
4809 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4810 unsigned NumElts = VT.getVectorNumElements();
4811 MVT TruncVT = MVT::getIntegerVT(EltSize);
4812 SmallVector<SDValue, 8> Ops;
4813 for (unsigned i = 0; i != NumElts; ++i) {
4814 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4815 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004816 // Element types smaller than 32 bits are not legal, so use i32 elements.
4817 // The values are implicitly truncated so sext vs. zext doesn't matter.
4818 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004819 }
4820 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4821 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004822}
4823
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004824static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4825 unsigned Opcode = N->getOpcode();
4826 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4827 SDNode *N0 = N->getOperand(0).getNode();
4828 SDNode *N1 = N->getOperand(1).getNode();
4829 return N0->hasOneUse() && N1->hasOneUse() &&
4830 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4831 }
4832 return false;
4833}
4834
4835static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4836 unsigned Opcode = N->getOpcode();
4837 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4838 SDNode *N0 = N->getOperand(0).getNode();
4839 SDNode *N1 = N->getOperand(1).getNode();
4840 return N0->hasOneUse() && N1->hasOneUse() &&
4841 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4842 }
4843 return false;
4844}
4845
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004846static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4847 // Multiplications are only custom-lowered for 128-bit vectors so that
4848 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4849 EVT VT = Op.getValueType();
4850 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4851 SDNode *N0 = Op.getOperand(0).getNode();
4852 SDNode *N1 = Op.getOperand(1).getNode();
4853 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004854 bool isMLA = false;
4855 bool isN0SExt = isSignExtended(N0, DAG);
4856 bool isN1SExt = isSignExtended(N1, DAG);
4857 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004858 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004859 else {
4860 bool isN0ZExt = isZeroExtended(N0, DAG);
4861 bool isN1ZExt = isZeroExtended(N1, DAG);
4862 if (isN0ZExt && isN1ZExt)
4863 NewOpc = ARMISD::VMULLu;
4864 else if (isN1SExt || isN1ZExt) {
4865 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4866 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4867 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4868 NewOpc = ARMISD::VMULLs;
4869 isMLA = true;
4870 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4871 NewOpc = ARMISD::VMULLu;
4872 isMLA = true;
4873 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4874 std::swap(N0, N1);
4875 NewOpc = ARMISD::VMULLu;
4876 isMLA = true;
4877 }
4878 }
4879
4880 if (!NewOpc) {
4881 if (VT == MVT::v2i64)
4882 // Fall through to expand this. It is not legal.
4883 return SDValue();
4884 else
4885 // Other vector multiplications are legal.
4886 return Op;
4887 }
4888 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004889
4890 // Legalize to a VMULL instruction.
4891 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004892 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004893 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004894 if (!isMLA) {
4895 Op0 = SkipExtension(N0, DAG);
4896 assert(Op0.getValueType().is64BitVector() &&
4897 Op1.getValueType().is64BitVector() &&
4898 "unexpected types for extended operands to VMULL");
4899 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4900 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004901
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004902 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4903 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4904 // vmull q0, d4, d6
4905 // vmlal q0, d5, d6
4906 // is faster than
4907 // vaddl q0, d4, d5
4908 // vmovl q1, d6
4909 // vmul q0, q0, q1
4910 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4911 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4912 EVT Op1VT = Op1.getValueType();
4913 return DAG.getNode(N0->getOpcode(), DL, VT,
4914 DAG.getNode(NewOpc, DL, VT,
4915 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4916 DAG.getNode(NewOpc, DL, VT,
4917 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004918}
4919
Owen Anderson76706012011-04-05 21:48:57 +00004920static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004921LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4922 // Convert to float
4923 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4924 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4925 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4926 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4927 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4928 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4929 // Get reciprocal estimate.
4930 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004931 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004932 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4933 // Because char has a smaller range than uchar, we can actually get away
4934 // without any newton steps. This requires that we use a weird bias
4935 // of 0xb000, however (again, this has been exhaustively tested).
4936 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4937 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4938 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4939 Y = DAG.getConstant(0xb000, MVT::i32);
4940 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4941 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4942 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4943 // Convert back to short.
4944 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4945 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4946 return X;
4947}
4948
Owen Anderson76706012011-04-05 21:48:57 +00004949static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004950LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4951 SDValue N2;
4952 // Convert to float.
4953 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4954 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4955 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4956 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4957 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4958 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004959
Nate Begeman7973f352011-02-11 20:53:29 +00004960 // Use reciprocal estimate and one refinement step.
4961 // float4 recip = vrecpeq_f32(yf);
4962 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004963 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004964 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004965 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004966 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4967 N1, N2);
4968 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4969 // Because short has a smaller range than ushort, we can actually get away
4970 // with only a single newton step. This requires that we use a weird bias
4971 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004972 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004973 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4974 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004975 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004976 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4977 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4978 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4979 // Convert back to integer and return.
4980 // return vmovn_s32(vcvt_s32_f32(result));
4981 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4982 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4983 return N0;
4984}
4985
4986static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4987 EVT VT = Op.getValueType();
4988 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4989 "unexpected type for custom-lowering ISD::SDIV");
4990
4991 DebugLoc dl = Op.getDebugLoc();
4992 SDValue N0 = Op.getOperand(0);
4993 SDValue N1 = Op.getOperand(1);
4994 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004995
Nate Begeman7973f352011-02-11 20:53:29 +00004996 if (VT == MVT::v8i8) {
4997 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4998 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004999
Nate Begeman7973f352011-02-11 20:53:29 +00005000 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5001 DAG.getIntPtrConstant(4));
5002 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005003 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005004 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5005 DAG.getIntPtrConstant(0));
5006 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5007 DAG.getIntPtrConstant(0));
5008
5009 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5010 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5011
5012 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5013 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005014
Nate Begeman7973f352011-02-11 20:53:29 +00005015 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5016 return N0;
5017 }
5018 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5019}
5020
5021static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5022 EVT VT = Op.getValueType();
5023 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5024 "unexpected type for custom-lowering ISD::UDIV");
5025
5026 DebugLoc dl = Op.getDebugLoc();
5027 SDValue N0 = Op.getOperand(0);
5028 SDValue N1 = Op.getOperand(1);
5029 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005030
Nate Begeman7973f352011-02-11 20:53:29 +00005031 if (VT == MVT::v8i8) {
5032 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5033 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005034
Nate Begeman7973f352011-02-11 20:53:29 +00005035 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5036 DAG.getIntPtrConstant(4));
5037 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005038 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005039 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5040 DAG.getIntPtrConstant(0));
5041 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5042 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005043
Nate Begeman7973f352011-02-11 20:53:29 +00005044 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5045 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005046
Nate Begeman7973f352011-02-11 20:53:29 +00005047 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5048 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005049
5050 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005051 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5052 N0);
5053 return N0;
5054 }
Owen Anderson76706012011-04-05 21:48:57 +00005055
Nate Begeman7973f352011-02-11 20:53:29 +00005056 // v4i16 sdiv ... Convert to float.
5057 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5058 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5059 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5060 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5061 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005062 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005063
5064 // Use reciprocal estimate and two refinement steps.
5065 // float4 recip = vrecpeq_f32(yf);
5066 // recip *= vrecpsq_f32(yf, recip);
5067 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005068 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005069 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005070 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005071 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005072 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005073 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005074 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005075 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005076 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005077 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5078 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5079 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5080 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005081 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005082 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5083 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5084 N1 = DAG.getConstant(2, MVT::i32);
5085 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5086 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5087 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5088 // Convert back to integer and return.
5089 // return vmovn_u32(vcvt_s32_f32(result));
5090 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5091 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5092 return N0;
5093}
5094
Evan Cheng342e3162011-08-30 01:34:54 +00005095static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5096 EVT VT = Op.getNode()->getValueType(0);
5097 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5098
5099 unsigned Opc;
5100 bool ExtraOp = false;
5101 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005102 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005103 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5104 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5105 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5106 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5107 }
5108
5109 if (!ExtraOp)
5110 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5111 Op.getOperand(1));
5112 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5113 Op.getOperand(1), Op.getOperand(2));
5114}
5115
Eli Friedman74bf18c2011-09-15 22:26:18 +00005116static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005117 // Monotonic load/store is legal for all targets
5118 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5119 return Op;
5120
5121 // Aquire/Release load/store is not legal for targets without a
5122 // dmb or equivalent available.
5123 return SDValue();
5124}
5125
5126
Eli Friedman2bdffe42011-08-31 00:31:29 +00005127static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005128ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5129 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005130 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005131 assert (Node->getValueType(0) == MVT::i64 &&
5132 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005133
Eli Friedman4d3f3292011-08-31 17:52:22 +00005134 SmallVector<SDValue, 6> Ops;
5135 Ops.push_back(Node->getOperand(0)); // Chain
5136 Ops.push_back(Node->getOperand(1)); // Ptr
5137 // Low part of Val1
5138 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5139 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5140 // High part of Val1
5141 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5142 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005143 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005144 // High part of Val1
5145 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5146 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5147 // High part of Val2
5148 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5149 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5150 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005151 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5152 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005153 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005154 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005155 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005156 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5157 Results.push_back(Result.getValue(2));
5158}
5159
Dan Gohmand858e902010-04-17 15:26:15 +00005160SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005161 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005162 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005163 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005164 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005165 case ISD::GlobalAddress:
5166 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5167 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005168 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005169 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005170 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5171 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005172 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005173 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005174 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005175 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005176 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005177 case ISD::SINT_TO_FP:
5178 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5179 case ISD::FP_TO_SINT:
5180 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005181 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005182 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005183 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005184 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005185 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005186 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005187 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5188 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005189 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005190 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005191 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005192 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005193 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005194 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005195 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005196 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005197 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005198 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005199 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005200 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005201 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005202 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005203 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005204 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005205 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005206 case ISD::SDIV: return LowerSDIV(Op, DAG);
5207 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005208 case ISD::ADDC:
5209 case ISD::ADDE:
5210 case ISD::SUBC:
5211 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005212 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005213 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005214 }
Evan Chenga8e29892007-01-19 07:51:42 +00005215}
5216
Duncan Sands1607f052008-12-01 11:39:25 +00005217/// ReplaceNodeResults - Replace the results of node with an illegal result
5218/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005219void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5220 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005221 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005222 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005223 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005224 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005225 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005226 case ISD::BITCAST:
5227 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005228 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005229 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005230 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005231 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005232 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005233 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005234 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005235 return;
5236 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005237 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005238 return;
5239 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005240 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005241 return;
5242 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005243 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005244 return;
5245 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005246 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005247 return;
5248 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005249 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005250 return;
5251 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005252 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005253 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005254 case ISD::ATOMIC_CMP_SWAP:
5255 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5256 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005257 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005258 if (Res.getNode())
5259 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005260}
Chris Lattner27a6c732007-11-24 07:07:01 +00005261
Evan Chenga8e29892007-01-19 07:51:42 +00005262//===----------------------------------------------------------------------===//
5263// ARM Scheduler Hooks
5264//===----------------------------------------------------------------------===//
5265
5266MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005267ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5268 MachineBasicBlock *BB,
5269 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005270 unsigned dest = MI->getOperand(0).getReg();
5271 unsigned ptr = MI->getOperand(1).getReg();
5272 unsigned oldval = MI->getOperand(2).getReg();
5273 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005274 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5275 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005276 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005277
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005278 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005279 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5280 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5281 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005282
5283 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005284 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5285 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5286 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005287 }
5288
Jim Grosbach5278eb82009-12-11 01:42:04 +00005289 unsigned ldrOpc, strOpc;
5290 switch (Size) {
5291 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005292 case 1:
5293 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005294 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005295 break;
5296 case 2:
5297 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5298 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5299 break;
5300 case 4:
5301 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5302 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5303 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005304 }
5305
5306 MachineFunction *MF = BB->getParent();
5307 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5308 MachineFunction::iterator It = BB;
5309 ++It; // insert the new blocks after the current block
5310
5311 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5312 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5313 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5314 MF->insert(It, loop1MBB);
5315 MF->insert(It, loop2MBB);
5316 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005317
5318 // Transfer the remainder of BB and its successor edges to exitMBB.
5319 exitMBB->splice(exitMBB->begin(), BB,
5320 llvm::next(MachineBasicBlock::iterator(MI)),
5321 BB->end());
5322 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005323
5324 // thisMBB:
5325 // ...
5326 // fallthrough --> loop1MBB
5327 BB->addSuccessor(loop1MBB);
5328
5329 // loop1MBB:
5330 // ldrex dest, [ptr]
5331 // cmp dest, oldval
5332 // bne exitMBB
5333 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005334 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5335 if (ldrOpc == ARM::t2LDREX)
5336 MIB.addImm(0);
5337 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005338 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005339 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005340 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5341 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005342 BB->addSuccessor(loop2MBB);
5343 BB->addSuccessor(exitMBB);
5344
5345 // loop2MBB:
5346 // strex scratch, newval, [ptr]
5347 // cmp scratch, #0
5348 // bne loop1MBB
5349 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005350 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5351 if (strOpc == ARM::t2STREX)
5352 MIB.addImm(0);
5353 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005354 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005355 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005356 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5357 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005358 BB->addSuccessor(loop1MBB);
5359 BB->addSuccessor(exitMBB);
5360
5361 // exitMBB:
5362 // ...
5363 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005364
Dan Gohman14152b42010-07-06 20:24:04 +00005365 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005366
Jim Grosbach5278eb82009-12-11 01:42:04 +00005367 return BB;
5368}
5369
5370MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005371ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5372 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005373 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5374 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5375
5376 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005377 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005378 MachineFunction::iterator It = BB;
5379 ++It;
5380
5381 unsigned dest = MI->getOperand(0).getReg();
5382 unsigned ptr = MI->getOperand(1).getReg();
5383 unsigned incr = MI->getOperand(2).getReg();
5384 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005385 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005386
5387 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5388 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005389 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5390 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005391 }
5392
Jim Grosbachc3c23542009-12-14 04:22:04 +00005393 unsigned ldrOpc, strOpc;
5394 switch (Size) {
5395 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005396 case 1:
5397 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005398 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005399 break;
5400 case 2:
5401 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5402 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5403 break;
5404 case 4:
5405 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5406 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5407 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005408 }
5409
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005410 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5411 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5412 MF->insert(It, loopMBB);
5413 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005414
5415 // Transfer the remainder of BB and its successor edges to exitMBB.
5416 exitMBB->splice(exitMBB->begin(), BB,
5417 llvm::next(MachineBasicBlock::iterator(MI)),
5418 BB->end());
5419 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005420
Craig Topper420761a2012-04-20 07:30:17 +00005421 const TargetRegisterClass *TRC = isThumb2 ?
5422 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5423 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005424 unsigned scratch = MRI.createVirtualRegister(TRC);
5425 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005426
5427 // thisMBB:
5428 // ...
5429 // fallthrough --> loopMBB
5430 BB->addSuccessor(loopMBB);
5431
5432 // loopMBB:
5433 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005434 // <binop> scratch2, dest, incr
5435 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005436 // cmp scratch, #0
5437 // bne- loopMBB
5438 // fallthrough --> exitMBB
5439 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005440 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5441 if (ldrOpc == ARM::t2LDREX)
5442 MIB.addImm(0);
5443 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005444 if (BinOpcode) {
5445 // operand order needs to go the other way for NAND
5446 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5447 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5448 addReg(incr).addReg(dest)).addReg(0);
5449 else
5450 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5451 addReg(dest).addReg(incr)).addReg(0);
5452 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005453
Jim Grosbachb6aed502011-09-09 18:37:27 +00005454 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5455 if (strOpc == ARM::t2STREX)
5456 MIB.addImm(0);
5457 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005458 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005459 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005460 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5461 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005462
5463 BB->addSuccessor(loopMBB);
5464 BB->addSuccessor(exitMBB);
5465
5466 // exitMBB:
5467 // ...
5468 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005469
Dan Gohman14152b42010-07-06 20:24:04 +00005470 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005471
Jim Grosbachc3c23542009-12-14 04:22:04 +00005472 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005473}
5474
Jim Grosbachf7da8822011-04-26 19:44:18 +00005475MachineBasicBlock *
5476ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5477 MachineBasicBlock *BB,
5478 unsigned Size,
5479 bool signExtend,
5480 ARMCC::CondCodes Cond) const {
5481 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5482
5483 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5484 MachineFunction *MF = BB->getParent();
5485 MachineFunction::iterator It = BB;
5486 ++It;
5487
5488 unsigned dest = MI->getOperand(0).getReg();
5489 unsigned ptr = MI->getOperand(1).getReg();
5490 unsigned incr = MI->getOperand(2).getReg();
5491 unsigned oldval = dest;
5492 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005493 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005494
5495 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5496 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005497 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5498 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005499 }
5500
Jim Grosbachf7da8822011-04-26 19:44:18 +00005501 unsigned ldrOpc, strOpc, extendOpc;
5502 switch (Size) {
5503 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5504 case 1:
5505 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5506 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005507 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005508 break;
5509 case 2:
5510 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5511 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005512 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005513 break;
5514 case 4:
5515 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5516 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5517 extendOpc = 0;
5518 break;
5519 }
5520
5521 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5522 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5523 MF->insert(It, loopMBB);
5524 MF->insert(It, exitMBB);
5525
5526 // Transfer the remainder of BB and its successor edges to exitMBB.
5527 exitMBB->splice(exitMBB->begin(), BB,
5528 llvm::next(MachineBasicBlock::iterator(MI)),
5529 BB->end());
5530 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5531
Craig Topper420761a2012-04-20 07:30:17 +00005532 const TargetRegisterClass *TRC = isThumb2 ?
5533 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5534 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005535 unsigned scratch = MRI.createVirtualRegister(TRC);
5536 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005537
5538 // thisMBB:
5539 // ...
5540 // fallthrough --> loopMBB
5541 BB->addSuccessor(loopMBB);
5542
5543 // loopMBB:
5544 // ldrex dest, ptr
5545 // (sign extend dest, if required)
5546 // cmp dest, incr
5547 // cmov.cond scratch2, dest, incr
5548 // strex scratch, scratch2, ptr
5549 // cmp scratch, #0
5550 // bne- loopMBB
5551 // fallthrough --> exitMBB
5552 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005553 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5554 if (ldrOpc == ARM::t2LDREX)
5555 MIB.addImm(0);
5556 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005557
5558 // Sign extend the value, if necessary.
5559 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005560 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005561 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5562 .addReg(dest)
5563 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005564 }
5565
5566 // Build compare and cmov instructions.
5567 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5568 .addReg(oldval).addReg(incr));
5569 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5570 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5571
Jim Grosbachb6aed502011-09-09 18:37:27 +00005572 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5573 if (strOpc == ARM::t2STREX)
5574 MIB.addImm(0);
5575 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005576 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5577 .addReg(scratch).addImm(0));
5578 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5579 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5580
5581 BB->addSuccessor(loopMBB);
5582 BB->addSuccessor(exitMBB);
5583
5584 // exitMBB:
5585 // ...
5586 BB = exitMBB;
5587
5588 MI->eraseFromParent(); // The instruction is gone now.
5589
5590 return BB;
5591}
5592
Eli Friedman2bdffe42011-08-31 00:31:29 +00005593MachineBasicBlock *
5594ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5595 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005596 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005597 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5598 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5599
5600 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5601 MachineFunction *MF = BB->getParent();
5602 MachineFunction::iterator It = BB;
5603 ++It;
5604
5605 unsigned destlo = MI->getOperand(0).getReg();
5606 unsigned desthi = MI->getOperand(1).getReg();
5607 unsigned ptr = MI->getOperand(2).getReg();
5608 unsigned vallo = MI->getOperand(3).getReg();
5609 unsigned valhi = MI->getOperand(4).getReg();
5610 DebugLoc dl = MI->getDebugLoc();
5611 bool isThumb2 = Subtarget->isThumb2();
5612
5613 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5614 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005615 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5616 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5617 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005618 }
5619
5620 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5621 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5622
5623 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005624 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005625 if (IsCmpxchg) {
5626 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5627 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5628 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005629 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5630 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005631 if (IsCmpxchg) {
5632 MF->insert(It, contBB);
5633 MF->insert(It, cont2BB);
5634 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005635 MF->insert(It, exitMBB);
5636
5637 // Transfer the remainder of BB and its successor edges to exitMBB.
5638 exitMBB->splice(exitMBB->begin(), BB,
5639 llvm::next(MachineBasicBlock::iterator(MI)),
5640 BB->end());
5641 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5642
Craig Topper420761a2012-04-20 07:30:17 +00005643 const TargetRegisterClass *TRC = isThumb2 ?
5644 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5645 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005646 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5647
5648 // thisMBB:
5649 // ...
5650 // fallthrough --> loopMBB
5651 BB->addSuccessor(loopMBB);
5652
5653 // loopMBB:
5654 // ldrexd r2, r3, ptr
5655 // <binopa> r0, r2, incr
5656 // <binopb> r1, r3, incr
5657 // strexd storesuccess, r0, r1, ptr
5658 // cmp storesuccess, #0
5659 // bne- loopMBB
5660 // fallthrough --> exitMBB
5661 //
5662 // Note that the registers are explicitly specified because there is not any
5663 // way to force the register allocator to allocate a register pair.
5664 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005665 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005666 // need to properly enforce the restriction that the two output registers
5667 // for ldrexd must be different.
5668 BB = loopMBB;
5669 // Load
5670 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5671 .addReg(ARM::R2, RegState::Define)
5672 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5673 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5674 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5675 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005676
5677 if (IsCmpxchg) {
5678 // Add early exit
5679 for (unsigned i = 0; i < 2; i++) {
5680 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5681 ARM::CMPrr))
5682 .addReg(i == 0 ? destlo : desthi)
5683 .addReg(i == 0 ? vallo : valhi));
5684 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5685 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5686 BB->addSuccessor(exitMBB);
5687 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5688 BB = (i == 0 ? contBB : cont2BB);
5689 }
5690
5691 // Copy to physregs for strexd
5692 unsigned setlo = MI->getOperand(5).getReg();
5693 unsigned sethi = MI->getOperand(6).getReg();
5694 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5695 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5696 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005697 // Perform binary operation
5698 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5699 .addReg(destlo).addReg(vallo))
5700 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5701 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5702 .addReg(desthi).addReg(valhi)).addReg(0);
5703 } else {
5704 // Copy to physregs for strexd
5705 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5706 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5707 }
5708
5709 // Store
5710 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5711 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5712 // Cmp+jump
5713 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5714 .addReg(storesuccess).addImm(0));
5715 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5716 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5717
5718 BB->addSuccessor(loopMBB);
5719 BB->addSuccessor(exitMBB);
5720
5721 // exitMBB:
5722 // ...
5723 BB = exitMBB;
5724
5725 MI->eraseFromParent(); // The instruction is gone now.
5726
5727 return BB;
5728}
5729
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005730/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5731/// registers the function context.
5732void ARMTargetLowering::
5733SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5734 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005735 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5736 DebugLoc dl = MI->getDebugLoc();
5737 MachineFunction *MF = MBB->getParent();
5738 MachineRegisterInfo *MRI = &MF->getRegInfo();
5739 MachineConstantPool *MCP = MF->getConstantPool();
5740 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5741 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005742
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005743 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005744 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005745
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005746 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005747 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005748 ARMConstantPoolValue *CPV =
5749 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5750 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5751
Craig Topper420761a2012-04-20 07:30:17 +00005752 const TargetRegisterClass *TRC = isThumb ?
5753 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5754 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005755
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005756 // Grab constant pool and fixed stack memory operands.
5757 MachineMemOperand *CPMMO =
5758 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5759 MachineMemOperand::MOLoad, 4, 4);
5760
5761 MachineMemOperand *FIMMOSt =
5762 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5763 MachineMemOperand::MOStore, 4, 4);
5764
5765 // Load the address of the dispatch MBB into the jump buffer.
5766 if (isThumb2) {
5767 // Incoming value: jbuf
5768 // ldr.n r5, LCPI1_1
5769 // orr r5, r5, #1
5770 // add r5, pc
5771 // str r5, [$jbuf, #+4] ; &jbuf[1]
5772 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5773 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5774 .addConstantPoolIndex(CPI)
5775 .addMemOperand(CPMMO));
5776 // Set the low bit because of thumb mode.
5777 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5778 AddDefaultCC(
5779 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5780 .addReg(NewVReg1, RegState::Kill)
5781 .addImm(0x01)));
5782 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5783 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5784 .addReg(NewVReg2, RegState::Kill)
5785 .addImm(PCLabelId);
5786 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5787 .addReg(NewVReg3, RegState::Kill)
5788 .addFrameIndex(FI)
5789 .addImm(36) // &jbuf[1] :: pc
5790 .addMemOperand(FIMMOSt));
5791 } else if (isThumb) {
5792 // Incoming value: jbuf
5793 // ldr.n r1, LCPI1_4
5794 // add r1, pc
5795 // mov r2, #1
5796 // orrs r1, r2
5797 // add r2, $jbuf, #+4 ; &jbuf[1]
5798 // str r1, [r2]
5799 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5800 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5801 .addConstantPoolIndex(CPI)
5802 .addMemOperand(CPMMO));
5803 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5804 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5805 .addReg(NewVReg1, RegState::Kill)
5806 .addImm(PCLabelId);
5807 // Set the low bit because of thumb mode.
5808 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5809 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5810 .addReg(ARM::CPSR, RegState::Define)
5811 .addImm(1));
5812 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5813 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5814 .addReg(ARM::CPSR, RegState::Define)
5815 .addReg(NewVReg2, RegState::Kill)
5816 .addReg(NewVReg3, RegState::Kill));
5817 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5818 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5819 .addFrameIndex(FI)
5820 .addImm(36)); // &jbuf[1] :: pc
5821 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5822 .addReg(NewVReg4, RegState::Kill)
5823 .addReg(NewVReg5, RegState::Kill)
5824 .addImm(0)
5825 .addMemOperand(FIMMOSt));
5826 } else {
5827 // Incoming value: jbuf
5828 // ldr r1, LCPI1_1
5829 // add r1, pc, r1
5830 // str r1, [$jbuf, #+4] ; &jbuf[1]
5831 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5832 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5833 .addConstantPoolIndex(CPI)
5834 .addImm(0)
5835 .addMemOperand(CPMMO));
5836 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5837 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5838 .addReg(NewVReg1, RegState::Kill)
5839 .addImm(PCLabelId));
5840 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5841 .addReg(NewVReg2, RegState::Kill)
5842 .addFrameIndex(FI)
5843 .addImm(36) // &jbuf[1] :: pc
5844 .addMemOperand(FIMMOSt));
5845 }
5846}
5847
5848MachineBasicBlock *ARMTargetLowering::
5849EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5850 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5851 DebugLoc dl = MI->getDebugLoc();
5852 MachineFunction *MF = MBB->getParent();
5853 MachineRegisterInfo *MRI = &MF->getRegInfo();
5854 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5855 MachineFrameInfo *MFI = MF->getFrameInfo();
5856 int FI = MFI->getFunctionContextIndex();
5857
Craig Topper420761a2012-04-20 07:30:17 +00005858 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5859 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005860 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005861
Bill Wendling04f15b42011-10-06 21:29:56 +00005862 // Get a mapping of the call site numbers to all of the landing pads they're
5863 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005864 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5865 unsigned MaxCSNum = 0;
5866 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005867 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5868 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005869 if (!BB->isLandingPad()) continue;
5870
5871 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5872 // pad.
5873 for (MachineBasicBlock::iterator
5874 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5875 if (!II->isEHLabel()) continue;
5876
5877 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005878 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005879
Bill Wendling5cbef192011-10-05 23:28:57 +00005880 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5881 for (SmallVectorImpl<unsigned>::iterator
5882 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5883 CSI != CSE; ++CSI) {
5884 CallSiteNumToLPad[*CSI].push_back(BB);
5885 MaxCSNum = std::max(MaxCSNum, *CSI);
5886 }
Bill Wendling2a850152011-10-05 00:02:33 +00005887 break;
5888 }
5889 }
5890
5891 // Get an ordered list of the machine basic blocks for the jump table.
5892 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005893 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005894 LPadList.reserve(CallSiteNumToLPad.size());
5895 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5896 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5897 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005898 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005899 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005900 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5901 }
Bill Wendling2a850152011-10-05 00:02:33 +00005902 }
5903
Bill Wendling5cbef192011-10-05 23:28:57 +00005904 assert(!LPadList.empty() &&
5905 "No landing pad destinations for the dispatch jump table!");
5906
Bill Wendling04f15b42011-10-06 21:29:56 +00005907 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005908 MachineJumpTableInfo *JTI =
5909 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5910 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5911 unsigned UId = AFI->createJumpTableUId();
5912
Bill Wendling04f15b42011-10-06 21:29:56 +00005913 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005914
5915 // Shove the dispatch's address into the return slot in the function context.
5916 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5917 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005918
Bill Wendlingbb734682011-10-05 00:39:32 +00005919 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005920 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005921 DispatchBB->addSuccessor(TrapBB);
5922
5923 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5924 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005925
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005926 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005927 MF->insert(MF->end(), DispatchBB);
5928 MF->insert(MF->end(), DispContBB);
5929 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005930
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005931 // Insert code into the entry block that creates and registers the function
5932 // context.
5933 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5934
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005935 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005936 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005937 MachineMemOperand::MOLoad |
5938 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005939
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005940 if (AFI->isThumb1OnlyFunction())
5941 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5942 else if (!Subtarget->hasVFP2())
5943 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005944 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005945 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005946
Bill Wendling952cb502011-10-18 22:49:07 +00005947 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005948 if (Subtarget->isThumb2()) {
5949 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5950 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5951 .addFrameIndex(FI)
5952 .addImm(4)
5953 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005954
Bill Wendling952cb502011-10-18 22:49:07 +00005955 if (NumLPads < 256) {
5956 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5957 .addReg(NewVReg1)
5958 .addImm(LPadList.size()));
5959 } else {
5960 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5961 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005962 .addImm(NumLPads & 0xFFFF));
5963
5964 unsigned VReg2 = VReg1;
5965 if ((NumLPads & 0xFFFF0000) != 0) {
5966 VReg2 = MRI->createVirtualRegister(TRC);
5967 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5968 .addReg(VReg1)
5969 .addImm(NumLPads >> 16));
5970 }
5971
Bill Wendling952cb502011-10-18 22:49:07 +00005972 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5973 .addReg(NewVReg1)
5974 .addReg(VReg2));
5975 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005976
Bill Wendling95ce2e92011-10-06 22:53:00 +00005977 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5978 .addMBB(TrapBB)
5979 .addImm(ARMCC::HI)
5980 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005981
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005982 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5983 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005984 .addJumpTableIndex(MJTI)
5985 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005986
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005987 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005988 AddDefaultCC(
5989 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005990 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5991 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005992 .addReg(NewVReg1)
5993 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5994
5995 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005996 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005997 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005998 .addJumpTableIndex(MJTI)
5999 .addImm(UId);
6000 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006001 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6002 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6003 .addFrameIndex(FI)
6004 .addImm(1)
6005 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006006
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006007 if (NumLPads < 256) {
6008 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6009 .addReg(NewVReg1)
6010 .addImm(NumLPads));
6011 } else {
6012 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006013 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6014 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6015
6016 // MachineConstantPool wants an explicit alignment.
6017 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6018 if (Align == 0)
6019 Align = getTargetData()->getTypeAllocSize(C->getType());
6020 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006021
6022 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6023 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6024 .addReg(VReg1, RegState::Define)
6025 .addConstantPoolIndex(Idx));
6026 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6027 .addReg(NewVReg1)
6028 .addReg(VReg1));
6029 }
6030
Bill Wendling083a8eb2011-10-06 23:37:36 +00006031 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6032 .addMBB(TrapBB)
6033 .addImm(ARMCC::HI)
6034 .addReg(ARM::CPSR);
6035
6036 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6037 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6038 .addReg(ARM::CPSR, RegState::Define)
6039 .addReg(NewVReg1)
6040 .addImm(2));
6041
6042 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006043 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006044 .addJumpTableIndex(MJTI)
6045 .addImm(UId));
6046
6047 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6048 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6049 .addReg(ARM::CPSR, RegState::Define)
6050 .addReg(NewVReg2, RegState::Kill)
6051 .addReg(NewVReg3));
6052
6053 MachineMemOperand *JTMMOLd =
6054 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6055 MachineMemOperand::MOLoad, 4, 4);
6056
6057 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6058 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6059 .addReg(NewVReg4, RegState::Kill)
6060 .addImm(0)
6061 .addMemOperand(JTMMOLd));
6062
6063 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6064 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6065 .addReg(ARM::CPSR, RegState::Define)
6066 .addReg(NewVReg5, RegState::Kill)
6067 .addReg(NewVReg3));
6068
6069 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6070 .addReg(NewVReg6, RegState::Kill)
6071 .addJumpTableIndex(MJTI)
6072 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006073 } else {
6074 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6075 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6076 .addFrameIndex(FI)
6077 .addImm(4)
6078 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006079
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006080 if (NumLPads < 256) {
6081 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6082 .addReg(NewVReg1)
6083 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006084 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006085 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6086 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006087 .addImm(NumLPads & 0xFFFF));
6088
6089 unsigned VReg2 = VReg1;
6090 if ((NumLPads & 0xFFFF0000) != 0) {
6091 VReg2 = MRI->createVirtualRegister(TRC);
6092 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6093 .addReg(VReg1)
6094 .addImm(NumLPads >> 16));
6095 }
6096
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006097 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6098 .addReg(NewVReg1)
6099 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006100 } else {
6101 MachineConstantPool *ConstantPool = MF->getConstantPool();
6102 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6103 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6104
6105 // MachineConstantPool wants an explicit alignment.
6106 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6107 if (Align == 0)
6108 Align = getTargetData()->getTypeAllocSize(C->getType());
6109 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6110
6111 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6112 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6113 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006114 .addConstantPoolIndex(Idx)
6115 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006116 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6117 .addReg(NewVReg1)
6118 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006119 }
6120
Bill Wendling95ce2e92011-10-06 22:53:00 +00006121 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6122 .addMBB(TrapBB)
6123 .addImm(ARMCC::HI)
6124 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006125
Bill Wendling564392b2011-10-18 22:11:18 +00006126 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006127 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006128 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006129 .addReg(NewVReg1)
6130 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006131 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6132 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006133 .addJumpTableIndex(MJTI)
6134 .addImm(UId));
6135
6136 MachineMemOperand *JTMMOLd =
6137 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6138 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006139 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006140 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006141 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6142 .addReg(NewVReg3, RegState::Kill)
6143 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006144 .addImm(0)
6145 .addMemOperand(JTMMOLd));
6146
6147 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006148 .addReg(NewVReg5, RegState::Kill)
6149 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006150 .addJumpTableIndex(MJTI)
6151 .addImm(UId);
6152 }
Bill Wendling2a850152011-10-05 00:02:33 +00006153
Bill Wendlingbb734682011-10-05 00:39:32 +00006154 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006155 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006156 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006157 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6158 MachineBasicBlock *CurMBB = *I;
6159 if (PrevMBB != CurMBB)
6160 DispContBB->addSuccessor(CurMBB);
6161 PrevMBB = CurMBB;
6162 }
6163
Bill Wendling24bb9252011-10-17 05:25:09 +00006164 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006165 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6166 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006167 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006168 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006169 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6170 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6171 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006172
6173 // Remove the landing pad successor from the invoke block and replace it
6174 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006175 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6176 BB->succ_end());
6177 while (!Successors.empty()) {
6178 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006179 if (SMBB->isLandingPad()) {
6180 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006181 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006182 }
6183 }
6184
6185 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006186
6187 // Find the invoke call and mark all of the callee-saved registers as
6188 // 'implicit defined' so that they're spilled. This prevents code from
6189 // moving instructions to before the EH block, where they will never be
6190 // executed.
6191 for (MachineBasicBlock::reverse_iterator
6192 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006193 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006194
6195 DenseMap<unsigned, bool> DefRegs;
6196 for (MachineInstr::mop_iterator
6197 OI = II->operands_begin(), OE = II->operands_end();
6198 OI != OE; ++OI) {
6199 if (!OI->isReg()) continue;
6200 DefRegs[OI->getReg()] = true;
6201 }
6202
6203 MachineInstrBuilder MIB(&*II);
6204
Bill Wendling5d798592011-10-14 23:55:44 +00006205 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006206 unsigned Reg = SavedRegs[i];
6207 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006208 !ARM::tGPRRegClass.contains(Reg) &&
6209 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006210 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006211 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006212 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006213 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006214 continue;
6215 if (!DefRegs[Reg])
6216 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006217 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006218
6219 break;
6220 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006221 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006222
Bill Wendlingf7b02072011-10-18 18:30:49 +00006223 // Mark all former landing pads as non-landing pads. The dispatch is the only
6224 // landing pad now.
6225 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6226 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6227 (*I)->setIsLandingPad(false);
6228
Bill Wendlingbb734682011-10-05 00:39:32 +00006229 // The instruction is gone now.
6230 MI->eraseFromParent();
6231
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006232 return MBB;
6233}
6234
Evan Cheng218977b2010-07-13 19:27:42 +00006235static
6236MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6237 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6238 E = MBB->succ_end(); I != E; ++I)
6239 if (*I != Succ)
6240 return *I;
6241 llvm_unreachable("Expecting a BB with two successors!");
6242}
6243
Manman Ren68f25572012-06-01 19:33:18 +00006244MachineBasicBlock *ARMTargetLowering::
6245EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6246 // This pseudo instruction has 3 operands: dst, src, size
6247 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6248 // Otherwise, we will generate unrolled scalar copies.
6249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6251 MachineFunction::iterator It = BB;
6252 ++It;
6253
6254 unsigned dest = MI->getOperand(0).getReg();
6255 unsigned src = MI->getOperand(1).getReg();
6256 unsigned SizeVal = MI->getOperand(2).getImm();
6257 unsigned Align = MI->getOperand(3).getImm();
6258 DebugLoc dl = MI->getDebugLoc();
6259
6260 bool isThumb2 = Subtarget->isThumb2();
6261 MachineFunction *MF = BB->getParent();
6262 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006263 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006264
6265 const TargetRegisterClass *TRC = isThumb2 ?
6266 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6267 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006268 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006269
6270 if (Align & 1) {
6271 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6272 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6273 UnitSize = 1;
6274 } else if (Align & 2) {
6275 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6276 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6277 UnitSize = 2;
6278 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006279 // Check whether we can use NEON instructions.
6280 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6281 Subtarget->hasNEON()) {
6282 if ((Align % 16 == 0) && SizeVal >= 16) {
6283 ldrOpc = ARM::VLD1q32wb_fixed;
6284 strOpc = ARM::VST1q32wb_fixed;
6285 UnitSize = 16;
6286 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6287 }
6288 else if ((Align % 8 == 0) && SizeVal >= 8) {
6289 ldrOpc = ARM::VLD1d32wb_fixed;
6290 strOpc = ARM::VST1d32wb_fixed;
6291 UnitSize = 8;
6292 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6293 }
6294 }
6295 // Can't use NEON instructions.
6296 if (UnitSize == 0) {
6297 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6298 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6299 UnitSize = 4;
6300 }
Manman Ren68f25572012-06-01 19:33:18 +00006301 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006302
Manman Ren68f25572012-06-01 19:33:18 +00006303 unsigned BytesLeft = SizeVal % UnitSize;
6304 unsigned LoopSize = SizeVal - BytesLeft;
6305
6306 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6307 // Use LDR and STR to copy.
6308 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6309 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6310 unsigned srcIn = src;
6311 unsigned destIn = dest;
6312 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006313 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006314 unsigned srcOut = MRI.createVirtualRegister(TRC);
6315 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006316 if (UnitSize >= 8) {
6317 AddDefaultPred(BuildMI(*BB, MI, dl,
6318 TII->get(ldrOpc), scratch)
6319 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6320
6321 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6322 .addReg(destIn).addImm(0).addReg(scratch));
6323 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006324 AddDefaultPred(BuildMI(*BB, MI, dl,
6325 TII->get(ldrOpc), scratch)
6326 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6327
6328 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6329 .addReg(scratch).addReg(destIn)
6330 .addImm(UnitSize));
6331 } else {
6332 AddDefaultPred(BuildMI(*BB, MI, dl,
6333 TII->get(ldrOpc), scratch)
6334 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6335 .addImm(UnitSize));
6336
6337 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6338 .addReg(scratch).addReg(destIn)
6339 .addReg(0).addImm(UnitSize));
6340 }
6341 srcIn = srcOut;
6342 destIn = destOut;
6343 }
6344
6345 // Handle the leftover bytes with LDRB and STRB.
6346 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6347 // [destOut] = STRB_POST(scratch, destIn, 1)
6348 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6349 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6350 for (unsigned i = 0; i < BytesLeft; i++) {
6351 unsigned scratch = MRI.createVirtualRegister(TRC);
6352 unsigned srcOut = MRI.createVirtualRegister(TRC);
6353 unsigned destOut = MRI.createVirtualRegister(TRC);
6354 if (isThumb2) {
6355 AddDefaultPred(BuildMI(*BB, MI, dl,
6356 TII->get(ldrOpc),scratch)
6357 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6358
6359 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6360 .addReg(scratch).addReg(destIn)
6361 .addReg(0).addImm(1));
6362 } else {
6363 AddDefaultPred(BuildMI(*BB, MI, dl,
6364 TII->get(ldrOpc),scratch)
6365 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6366
6367 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6368 .addReg(scratch).addReg(destIn)
6369 .addReg(0).addImm(1));
6370 }
6371 srcIn = srcOut;
6372 destIn = destOut;
6373 }
6374 MI->eraseFromParent(); // The instruction is gone now.
6375 return BB;
6376 }
6377
6378 // Expand the pseudo op to a loop.
6379 // thisMBB:
6380 // ...
6381 // movw varEnd, # --> with thumb2
6382 // movt varEnd, #
6383 // ldrcp varEnd, idx --> without thumb2
6384 // fallthrough --> loopMBB
6385 // loopMBB:
6386 // PHI varPhi, varEnd, varLoop
6387 // PHI srcPhi, src, srcLoop
6388 // PHI destPhi, dst, destLoop
6389 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6390 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6391 // subs varLoop, varPhi, #UnitSize
6392 // bne loopMBB
6393 // fallthrough --> exitMBB
6394 // exitMBB:
6395 // epilogue to handle left-over bytes
6396 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6397 // [destOut] = STRB_POST(scratch, destLoop, 1)
6398 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6399 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6400 MF->insert(It, loopMBB);
6401 MF->insert(It, exitMBB);
6402
6403 // Transfer the remainder of BB and its successor edges to exitMBB.
6404 exitMBB->splice(exitMBB->begin(), BB,
6405 llvm::next(MachineBasicBlock::iterator(MI)),
6406 BB->end());
6407 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6408
6409 // Load an immediate to varEnd.
6410 unsigned varEnd = MRI.createVirtualRegister(TRC);
6411 if (isThumb2) {
6412 unsigned VReg1 = varEnd;
6413 if ((LoopSize & 0xFFFF0000) != 0)
6414 VReg1 = MRI.createVirtualRegister(TRC);
6415 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6416 .addImm(LoopSize & 0xFFFF));
6417
6418 if ((LoopSize & 0xFFFF0000) != 0)
6419 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6420 .addReg(VReg1)
6421 .addImm(LoopSize >> 16));
6422 } else {
6423 MachineConstantPool *ConstantPool = MF->getConstantPool();
6424 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6425 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6426
6427 // MachineConstantPool wants an explicit alignment.
6428 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6429 if (Align == 0)
6430 Align = getTargetData()->getTypeAllocSize(C->getType());
6431 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6432
6433 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6434 .addReg(varEnd, RegState::Define)
6435 .addConstantPoolIndex(Idx)
6436 .addImm(0));
6437 }
6438 BB->addSuccessor(loopMBB);
6439
6440 // Generate the loop body:
6441 // varPhi = PHI(varLoop, varEnd)
6442 // srcPhi = PHI(srcLoop, src)
6443 // destPhi = PHI(destLoop, dst)
6444 MachineBasicBlock *entryBB = BB;
6445 BB = loopMBB;
6446 unsigned varLoop = MRI.createVirtualRegister(TRC);
6447 unsigned varPhi = MRI.createVirtualRegister(TRC);
6448 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6449 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6450 unsigned destLoop = MRI.createVirtualRegister(TRC);
6451 unsigned destPhi = MRI.createVirtualRegister(TRC);
6452
6453 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6454 .addReg(varLoop).addMBB(loopMBB)
6455 .addReg(varEnd).addMBB(entryBB);
6456 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6457 .addReg(srcLoop).addMBB(loopMBB)
6458 .addReg(src).addMBB(entryBB);
6459 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6460 .addReg(destLoop).addMBB(loopMBB)
6461 .addReg(dest).addMBB(entryBB);
6462
6463 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6464 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006465 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6466 if (UnitSize >= 8) {
6467 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6468 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6469
6470 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6471 .addReg(destPhi).addImm(0).addReg(scratch));
6472 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006473 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6474 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6475
6476 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6477 .addReg(scratch).addReg(destPhi)
6478 .addImm(UnitSize));
6479 } else {
6480 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6481 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6482 .addImm(UnitSize));
6483
6484 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6485 .addReg(scratch).addReg(destPhi)
6486 .addReg(0).addImm(UnitSize));
6487 }
6488
6489 // Decrement loop variable by UnitSize.
6490 MachineInstrBuilder MIB = BuildMI(BB, dl,
6491 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6492 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6493 MIB->getOperand(5).setReg(ARM::CPSR);
6494 MIB->getOperand(5).setIsDef(true);
6495
6496 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6497 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6498
6499 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6500 BB->addSuccessor(loopMBB);
6501 BB->addSuccessor(exitMBB);
6502
6503 // Add epilogue to handle BytesLeft.
6504 BB = exitMBB;
6505 MachineInstr *StartOfExit = exitMBB->begin();
6506 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6507 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6508
6509 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6510 // [destOut] = STRB_POST(scratch, destLoop, 1)
6511 unsigned srcIn = srcLoop;
6512 unsigned destIn = destLoop;
6513 for (unsigned i = 0; i < BytesLeft; i++) {
6514 unsigned scratch = MRI.createVirtualRegister(TRC);
6515 unsigned srcOut = MRI.createVirtualRegister(TRC);
6516 unsigned destOut = MRI.createVirtualRegister(TRC);
6517 if (isThumb2) {
6518 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6519 TII->get(ldrOpc),scratch)
6520 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6521
6522 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6523 .addReg(scratch).addReg(destIn)
6524 .addImm(1));
6525 } else {
6526 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6527 TII->get(ldrOpc),scratch)
6528 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6529
6530 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6531 .addReg(scratch).addReg(destIn)
6532 .addReg(0).addImm(1));
6533 }
6534 srcIn = srcOut;
6535 destIn = destOut;
6536 }
6537
6538 MI->eraseFromParent(); // The instruction is gone now.
6539 return BB;
6540}
6541
Jim Grosbache801dc42009-12-12 01:40:06 +00006542MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006543ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006544 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006545 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006546 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006547 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006548 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006549 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006550 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006551 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006552 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006553 // The Thumb2 pre-indexed stores have the same MI operands, they just
6554 // define them differently in the .td files from the isel patterns, so
6555 // they need pseudos.
6556 case ARM::t2STR_preidx:
6557 MI->setDesc(TII->get(ARM::t2STR_PRE));
6558 return BB;
6559 case ARM::t2STRB_preidx:
6560 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6561 return BB;
6562 case ARM::t2STRH_preidx:
6563 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6564 return BB;
6565
Jim Grosbach19dec202011-08-05 20:35:44 +00006566 case ARM::STRi_preidx:
6567 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006568 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006569 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6570 // Decode the offset.
6571 unsigned Offset = MI->getOperand(4).getImm();
6572 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6573 Offset = ARM_AM::getAM2Offset(Offset);
6574 if (isSub)
6575 Offset = -Offset;
6576
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006577 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006578 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006579 .addOperand(MI->getOperand(0)) // Rn_wb
6580 .addOperand(MI->getOperand(1)) // Rt
6581 .addOperand(MI->getOperand(2)) // Rn
6582 .addImm(Offset) // offset (skip GPR==zero_reg)
6583 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006584 .addOperand(MI->getOperand(6))
6585 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006586 MI->eraseFromParent();
6587 return BB;
6588 }
6589 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006590 case ARM::STRBr_preidx:
6591 case ARM::STRH_preidx: {
6592 unsigned NewOpc;
6593 switch (MI->getOpcode()) {
6594 default: llvm_unreachable("unexpected opcode!");
6595 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6596 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6597 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6598 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006599 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6600 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6601 MIB.addOperand(MI->getOperand(i));
6602 MI->eraseFromParent();
6603 return BB;
6604 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006605 case ARM::ATOMIC_LOAD_ADD_I8:
6606 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6607 case ARM::ATOMIC_LOAD_ADD_I16:
6608 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6609 case ARM::ATOMIC_LOAD_ADD_I32:
6610 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006611
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006612 case ARM::ATOMIC_LOAD_AND_I8:
6613 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6614 case ARM::ATOMIC_LOAD_AND_I16:
6615 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6616 case ARM::ATOMIC_LOAD_AND_I32:
6617 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006618
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006619 case ARM::ATOMIC_LOAD_OR_I8:
6620 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6621 case ARM::ATOMIC_LOAD_OR_I16:
6622 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6623 case ARM::ATOMIC_LOAD_OR_I32:
6624 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006625
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006626 case ARM::ATOMIC_LOAD_XOR_I8:
6627 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6628 case ARM::ATOMIC_LOAD_XOR_I16:
6629 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6630 case ARM::ATOMIC_LOAD_XOR_I32:
6631 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006632
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006633 case ARM::ATOMIC_LOAD_NAND_I8:
6634 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6635 case ARM::ATOMIC_LOAD_NAND_I16:
6636 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6637 case ARM::ATOMIC_LOAD_NAND_I32:
6638 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006639
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006640 case ARM::ATOMIC_LOAD_SUB_I8:
6641 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6642 case ARM::ATOMIC_LOAD_SUB_I16:
6643 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6644 case ARM::ATOMIC_LOAD_SUB_I32:
6645 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006646
Jim Grosbachf7da8822011-04-26 19:44:18 +00006647 case ARM::ATOMIC_LOAD_MIN_I8:
6648 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6649 case ARM::ATOMIC_LOAD_MIN_I16:
6650 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6651 case ARM::ATOMIC_LOAD_MIN_I32:
6652 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6653
6654 case ARM::ATOMIC_LOAD_MAX_I8:
6655 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6656 case ARM::ATOMIC_LOAD_MAX_I16:
6657 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6658 case ARM::ATOMIC_LOAD_MAX_I32:
6659 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6660
6661 case ARM::ATOMIC_LOAD_UMIN_I8:
6662 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6663 case ARM::ATOMIC_LOAD_UMIN_I16:
6664 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6665 case ARM::ATOMIC_LOAD_UMIN_I32:
6666 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6667
6668 case ARM::ATOMIC_LOAD_UMAX_I8:
6669 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6670 case ARM::ATOMIC_LOAD_UMAX_I16:
6671 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6672 case ARM::ATOMIC_LOAD_UMAX_I32:
6673 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6674
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006675 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6676 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6677 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006678
6679 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6680 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6681 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006682
Eli Friedman2bdffe42011-08-31 00:31:29 +00006683
6684 case ARM::ATOMADD6432:
6685 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006686 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6687 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006688 case ARM::ATOMSUB6432:
6689 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006690 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6691 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006692 case ARM::ATOMOR6432:
6693 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006694 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006695 case ARM::ATOMXOR6432:
6696 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006697 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006698 case ARM::ATOMAND6432:
6699 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006700 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006701 case ARM::ATOMSWAP6432:
6702 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006703 case ARM::ATOMCMPXCHG6432:
6704 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6705 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6706 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006707
Evan Cheng007ea272009-08-12 05:17:19 +00006708 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006709 // To "insert" a SELECT_CC instruction, we actually have to insert the
6710 // diamond control-flow pattern. The incoming instruction knows the
6711 // destination vreg to set, the condition code register to branch on, the
6712 // true/false values to select between, and a branch opcode to use.
6713 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006714 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006715 ++It;
6716
6717 // thisMBB:
6718 // ...
6719 // TrueVal = ...
6720 // cmpTY ccX, r1, r2
6721 // bCC copy1MBB
6722 // fallthrough --> copy0MBB
6723 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006724 MachineFunction *F = BB->getParent();
6725 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6726 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006727 F->insert(It, copy0MBB);
6728 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006729
6730 // Transfer the remainder of BB and its successor edges to sinkMBB.
6731 sinkMBB->splice(sinkMBB->begin(), BB,
6732 llvm::next(MachineBasicBlock::iterator(MI)),
6733 BB->end());
6734 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6735
Dan Gohman258c58c2010-07-06 15:49:48 +00006736 BB->addSuccessor(copy0MBB);
6737 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006738
Dan Gohman14152b42010-07-06 20:24:04 +00006739 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6740 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6741
Evan Chenga8e29892007-01-19 07:51:42 +00006742 // copy0MBB:
6743 // %FalseValue = ...
6744 // # fallthrough to sinkMBB
6745 BB = copy0MBB;
6746
6747 // Update machine-CFG edges
6748 BB->addSuccessor(sinkMBB);
6749
6750 // sinkMBB:
6751 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6752 // ...
6753 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006754 BuildMI(*BB, BB->begin(), dl,
6755 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006756 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6757 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6758
Dan Gohman14152b42010-07-06 20:24:04 +00006759 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006760 return BB;
6761 }
Evan Cheng86198642009-08-07 00:34:42 +00006762
Evan Cheng218977b2010-07-13 19:27:42 +00006763 case ARM::BCCi64:
6764 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006765 // If there is an unconditional branch to the other successor, remove it.
6766 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006767
Evan Cheng218977b2010-07-13 19:27:42 +00006768 // Compare both parts that make up the double comparison separately for
6769 // equality.
6770 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6771
6772 unsigned LHS1 = MI->getOperand(1).getReg();
6773 unsigned LHS2 = MI->getOperand(2).getReg();
6774 if (RHSisZero) {
6775 AddDefaultPred(BuildMI(BB, dl,
6776 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6777 .addReg(LHS1).addImm(0));
6778 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6779 .addReg(LHS2).addImm(0)
6780 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6781 } else {
6782 unsigned RHS1 = MI->getOperand(3).getReg();
6783 unsigned RHS2 = MI->getOperand(4).getReg();
6784 AddDefaultPred(BuildMI(BB, dl,
6785 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6786 .addReg(LHS1).addReg(RHS1));
6787 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6788 .addReg(LHS2).addReg(RHS2)
6789 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6790 }
6791
6792 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6793 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6794 if (MI->getOperand(0).getImm() == ARMCC::NE)
6795 std::swap(destMBB, exitMBB);
6796
6797 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6798 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006799 if (isThumb2)
6800 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6801 else
6802 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006803
6804 MI->eraseFromParent(); // The pseudo instruction is gone now.
6805 return BB;
6806 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006807
Bill Wendling5bc85282011-10-17 20:37:20 +00006808 case ARM::Int_eh_sjlj_setjmp:
6809 case ARM::Int_eh_sjlj_setjmp_nofp:
6810 case ARM::tInt_eh_sjlj_setjmp:
6811 case ARM::t2Int_eh_sjlj_setjmp:
6812 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6813 EmitSjLjDispatchBlock(MI, BB);
6814 return BB;
6815
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006816 case ARM::ABS:
6817 case ARM::t2ABS: {
6818 // To insert an ABS instruction, we have to insert the
6819 // diamond control-flow pattern. The incoming instruction knows the
6820 // source vreg to test against 0, the destination vreg to set,
6821 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006822 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006823 // It transforms
6824 // V1 = ABS V0
6825 // into
6826 // V2 = MOVS V0
6827 // BCC (branch to SinkBB if V0 >= 0)
6828 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006829 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006830 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6831 MachineFunction::iterator BBI = BB;
6832 ++BBI;
6833 MachineFunction *Fn = BB->getParent();
6834 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6835 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6836 Fn->insert(BBI, RSBBB);
6837 Fn->insert(BBI, SinkBB);
6838
6839 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6840 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6841 bool isThumb2 = Subtarget->isThumb2();
6842 MachineRegisterInfo &MRI = Fn->getRegInfo();
6843 // In Thumb mode S must not be specified if source register is the SP or
6844 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006845 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6846 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6847 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006848
6849 // Transfer the remainder of BB and its successor edges to sinkMBB.
6850 SinkBB->splice(SinkBB->begin(), BB,
6851 llvm::next(MachineBasicBlock::iterator(MI)),
6852 BB->end());
6853 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6854
6855 BB->addSuccessor(RSBBB);
6856 BB->addSuccessor(SinkBB);
6857
6858 // fall through to SinkMBB
6859 RSBBB->addSuccessor(SinkBB);
6860
Manman Ren307473d2012-06-15 21:32:12 +00006861 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006862 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006863 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6864 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006865
6866 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006867 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006868 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6869 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6870
6871 // insert rsbri in RSBBB
6872 // Note: BCC and rsbri will be converted into predicated rsbmi
6873 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006874 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006875 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006876 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006877 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6878
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006879 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006880 // reuse ABSDstReg to not change uses of ABS instruction
6881 BuildMI(*SinkBB, SinkBB->begin(), dl,
6882 TII->get(ARM::PHI), ABSDstReg)
6883 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006884 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006885
6886 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006887 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006888
6889 // return last added BB
6890 return SinkBB;
6891 }
Manman Ren68f25572012-06-01 19:33:18 +00006892 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006893 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006894 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006895 }
6896}
6897
Evan Cheng37fefc22011-08-30 19:09:48 +00006898void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6899 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006900 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006901 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6902 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6903 return;
6904 }
6905
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006906 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006907 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6908 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6909 // operand is still set to noreg. If needed, set the optional operand's
6910 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006911 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006912 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006913
Andrew Trick3be654f2011-09-21 02:20:46 +00006914 // Rename pseudo opcodes.
6915 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6916 if (NewOpc) {
6917 const ARMBaseInstrInfo *TII =
6918 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006919 MCID = &TII->get(NewOpc);
6920
6921 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6922 "converted opcode should be the same except for cc_out");
6923
6924 MI->setDesc(*MCID);
6925
6926 // Add the optional cc_out operand
6927 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006928 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006929 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006930
6931 // Any ARM instruction that sets the 's' bit should specify an optional
6932 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006933 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006934 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006935 return;
6936 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006937 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6938 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006939 bool definesCPSR = false;
6940 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006941 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006942 i != e; ++i) {
6943 const MachineOperand &MO = MI->getOperand(i);
6944 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6945 definesCPSR = true;
6946 if (MO.isDead())
6947 deadCPSR = true;
6948 MI->RemoveOperand(i);
6949 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006950 }
6951 }
Andrew Trick4815d562011-09-20 03:17:40 +00006952 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006953 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006954 return;
6955 }
6956 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006957 if (deadCPSR) {
6958 assert(!MI->getOperand(ccOutIdx).getReg() &&
6959 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006960 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006961 }
Andrew Trick4815d562011-09-20 03:17:40 +00006962
Andrew Trick3be654f2011-09-21 02:20:46 +00006963 // If this instruction was defined with an optional CPSR def and its dag node
6964 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006965 MachineOperand &MO = MI->getOperand(ccOutIdx);
6966 MO.setReg(ARM::CPSR);
6967 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006968}
6969
Evan Chenga8e29892007-01-19 07:51:42 +00006970//===----------------------------------------------------------------------===//
6971// ARM Optimization Hooks
6972//===----------------------------------------------------------------------===//
6973
Chris Lattnerd1980a52009-03-12 06:52:53 +00006974static
6975SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6976 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006977 SelectionDAG &DAG = DCI.DAG;
6978 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006979 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006980 unsigned Opc = N->getOpcode();
6981 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6982 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6983 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6984 ISD::CondCode CC = ISD::SETCC_INVALID;
6985
6986 if (isSlctCC) {
6987 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6988 } else {
6989 SDValue CCOp = Slct.getOperand(0);
6990 if (CCOp.getOpcode() == ISD::SETCC)
6991 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6992 }
6993
6994 bool DoXform = false;
6995 bool InvCC = false;
6996 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6997 "Bad input!");
6998
6999 if (LHS.getOpcode() == ISD::Constant &&
7000 cast<ConstantSDNode>(LHS)->isNullValue()) {
7001 DoXform = true;
7002 } else if (CC != ISD::SETCC_INVALID &&
7003 RHS.getOpcode() == ISD::Constant &&
7004 cast<ConstantSDNode>(RHS)->isNullValue()) {
7005 std::swap(LHS, RHS);
7006 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007007 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00007008 Op0.getOperand(0).getValueType();
7009 bool isInt = OpVT.isInteger();
7010 CC = ISD::getSetCCInverse(CC, isInt);
7011
7012 if (!TLI.isCondCodeLegal(CC, OpVT))
7013 return SDValue(); // Inverse operator isn't legal.
7014
7015 DoXform = true;
7016 InvCC = true;
7017 }
7018
7019 if (DoXform) {
7020 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
7021 if (isSlctCC)
7022 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
7023 Slct.getOperand(0), Slct.getOperand(1), CC);
7024 SDValue CCOp = Slct.getOperand(0);
7025 if (InvCC)
7026 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
7027 CCOp.getOperand(0), CCOp.getOperand(1), CC);
7028 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
7029 CCOp, OtherOp, Result);
7030 }
7031 return SDValue();
7032}
7033
Eric Christopherfa6f5912011-06-29 21:10:36 +00007034// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007035// (only after legalization).
7036static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7037 TargetLowering::DAGCombinerInfo &DCI,
7038 const ARMSubtarget *Subtarget) {
7039
7040 // Only perform optimization if after legalize, and if NEON is available. We
7041 // also expected both operands to be BUILD_VECTORs.
7042 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7043 || N0.getOpcode() != ISD::BUILD_VECTOR
7044 || N1.getOpcode() != ISD::BUILD_VECTOR)
7045 return SDValue();
7046
7047 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7048 EVT VT = N->getValueType(0);
7049 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7050 return SDValue();
7051
7052 // Check that the vector operands are of the right form.
7053 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7054 // operands, where N is the size of the formed vector.
7055 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7056 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007057
7058 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007059 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007060 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007061 SDValue Vec = N0->getOperand(0)->getOperand(0);
7062 SDNode *V = Vec.getNode();
7063 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007064
Eric Christopherfa6f5912011-06-29 21:10:36 +00007065 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007066 // check to see if each of their operands are an EXTRACT_VECTOR with
7067 // the same vector and appropriate index.
7068 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7069 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7070 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007071
Tanya Lattner189531f2011-06-14 23:48:48 +00007072 SDValue ExtVec0 = N0->getOperand(i);
7073 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007074
Tanya Lattner189531f2011-06-14 23:48:48 +00007075 // First operand is the vector, verify its the same.
7076 if (V != ExtVec0->getOperand(0).getNode() ||
7077 V != ExtVec1->getOperand(0).getNode())
7078 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007079
Tanya Lattner189531f2011-06-14 23:48:48 +00007080 // Second is the constant, verify its correct.
7081 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7082 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007083
Tanya Lattner189531f2011-06-14 23:48:48 +00007084 // For the constant, we want to see all the even or all the odd.
7085 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7086 || C1->getZExtValue() != nextIndex+1)
7087 return SDValue();
7088
7089 // Increment index.
7090 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007091 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007092 return SDValue();
7093 }
7094
7095 // Create VPADDL node.
7096 SelectionDAG &DAG = DCI.DAG;
7097 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007098
7099 // Build operand list.
7100 SmallVector<SDValue, 8> Ops;
7101 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7102 TLI.getPointerTy()));
7103
7104 // Input is the vector.
7105 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007106
Tanya Lattner189531f2011-06-14 23:48:48 +00007107 // Get widened type and narrowed type.
7108 MVT widenType;
7109 unsigned numElem = VT.getVectorNumElements();
7110 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7111 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7112 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7113 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7114 default:
Craig Topperbc219812012-02-07 02:50:20 +00007115 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007116 }
7117
7118 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7119 widenType, &Ops[0], Ops.size());
7120 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7121}
7122
Bob Wilson3d5792a2010-07-29 20:34:14 +00007123/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7124/// operands N0 and N1. This is a helper for PerformADDCombine that is
7125/// called with the default operands, and if that fails, with commuted
7126/// operands.
7127static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007128 TargetLowering::DAGCombinerInfo &DCI,
7129 const ARMSubtarget *Subtarget){
7130
7131 // Attempt to create vpaddl for this add.
7132 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7133 if (Result.getNode())
7134 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007135
Chris Lattnerd1980a52009-03-12 06:52:53 +00007136 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7137 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7138 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7139 if (Result.getNode()) return Result;
7140 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007141 return SDValue();
7142}
7143
Bob Wilson3d5792a2010-07-29 20:34:14 +00007144/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7145///
7146static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007147 TargetLowering::DAGCombinerInfo &DCI,
7148 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007149 SDValue N0 = N->getOperand(0);
7150 SDValue N1 = N->getOperand(1);
7151
7152 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007153 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007154 if (Result.getNode())
7155 return Result;
7156
7157 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007158 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007159}
7160
Chris Lattnerd1980a52009-03-12 06:52:53 +00007161/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007162///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007163static SDValue PerformSUBCombine(SDNode *N,
7164 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007165 SDValue N0 = N->getOperand(0);
7166 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007167
Chris Lattnerd1980a52009-03-12 06:52:53 +00007168 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7169 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7170 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7171 if (Result.getNode()) return Result;
7172 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007173
Chris Lattnerd1980a52009-03-12 06:52:53 +00007174 return SDValue();
7175}
7176
Evan Cheng463d3582011-03-31 19:38:48 +00007177/// PerformVMULCombine
7178/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7179/// special multiplier accumulator forwarding.
7180/// vmul d3, d0, d2
7181/// vmla d3, d1, d2
7182/// is faster than
7183/// vadd d3, d0, d1
7184/// vmul d3, d3, d2
7185static SDValue PerformVMULCombine(SDNode *N,
7186 TargetLowering::DAGCombinerInfo &DCI,
7187 const ARMSubtarget *Subtarget) {
7188 if (!Subtarget->hasVMLxForwarding())
7189 return SDValue();
7190
7191 SelectionDAG &DAG = DCI.DAG;
7192 SDValue N0 = N->getOperand(0);
7193 SDValue N1 = N->getOperand(1);
7194 unsigned Opcode = N0.getOpcode();
7195 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7196 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007197 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007198 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7199 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7200 return SDValue();
7201 std::swap(N0, N1);
7202 }
7203
7204 EVT VT = N->getValueType(0);
7205 DebugLoc DL = N->getDebugLoc();
7206 SDValue N00 = N0->getOperand(0);
7207 SDValue N01 = N0->getOperand(1);
7208 return DAG.getNode(Opcode, DL, VT,
7209 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7210 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7211}
7212
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007213static SDValue PerformMULCombine(SDNode *N,
7214 TargetLowering::DAGCombinerInfo &DCI,
7215 const ARMSubtarget *Subtarget) {
7216 SelectionDAG &DAG = DCI.DAG;
7217
7218 if (Subtarget->isThumb1Only())
7219 return SDValue();
7220
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007221 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7222 return SDValue();
7223
7224 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007225 if (VT.is64BitVector() || VT.is128BitVector())
7226 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007227 if (VT != MVT::i32)
7228 return SDValue();
7229
7230 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7231 if (!C)
7232 return SDValue();
7233
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007234 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007235 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007236
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007237 ShiftAmt = ShiftAmt & (32 - 1);
7238 SDValue V = N->getOperand(0);
7239 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007240
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007241 SDValue Res;
7242 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007243
7244 if (MulAmt >= 0) {
7245 if (isPowerOf2_32(MulAmt - 1)) {
7246 // (mul x, 2^N + 1) => (add (shl x, N), x)
7247 Res = DAG.getNode(ISD::ADD, DL, VT,
7248 V,
7249 DAG.getNode(ISD::SHL, DL, VT,
7250 V,
7251 DAG.getConstant(Log2_32(MulAmt - 1),
7252 MVT::i32)));
7253 } else if (isPowerOf2_32(MulAmt + 1)) {
7254 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7255 Res = DAG.getNode(ISD::SUB, DL, VT,
7256 DAG.getNode(ISD::SHL, DL, VT,
7257 V,
7258 DAG.getConstant(Log2_32(MulAmt + 1),
7259 MVT::i32)),
7260 V);
7261 } else
7262 return SDValue();
7263 } else {
7264 uint64_t MulAmtAbs = -MulAmt;
7265 if (isPowerOf2_32(MulAmtAbs + 1)) {
7266 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7267 Res = DAG.getNode(ISD::SUB, DL, VT,
7268 V,
7269 DAG.getNode(ISD::SHL, DL, VT,
7270 V,
7271 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7272 MVT::i32)));
7273 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7274 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7275 Res = DAG.getNode(ISD::ADD, DL, VT,
7276 V,
7277 DAG.getNode(ISD::SHL, DL, VT,
7278 V,
7279 DAG.getConstant(Log2_32(MulAmtAbs-1),
7280 MVT::i32)));
7281 Res = DAG.getNode(ISD::SUB, DL, VT,
7282 DAG.getConstant(0, MVT::i32),Res);
7283
7284 } else
7285 return SDValue();
7286 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007287
7288 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007289 Res = DAG.getNode(ISD::SHL, DL, VT,
7290 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007291
7292 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007293 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007294 return SDValue();
7295}
7296
Evan Chengc892aeb2012-02-23 01:19:06 +00007297static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7298 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7299 return false;
7300
7301 SDValue FalseVal = N.getOperand(0);
7302 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7303 if (!C)
7304 return false;
7305 if (AllOnes)
7306 return C->isAllOnesValue();
7307 return C->isNullValue();
7308}
7309
7310/// formConditionalOp - Combine an operation with a conditional move operand
7311/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7312/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7313static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7314 bool Commutable) {
7315 SDValue N0 = N->getOperand(0);
7316 SDValue N1 = N->getOperand(1);
7317
7318 bool isAND = N->getOpcode() == ISD::AND;
7319 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7320 if (!isCand && Commutable) {
7321 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7322 if (isCand)
7323 std::swap(N0, N1);
7324 }
7325 if (!isCand)
7326 return SDValue();
7327
7328 unsigned Opc = 0;
7329 switch (N->getOpcode()) {
7330 default: llvm_unreachable("Unexpected node");
7331 case ISD::AND: Opc = ARMISD::CAND; break;
7332 case ISD::OR: Opc = ARMISD::COR; break;
7333 case ISD::XOR: Opc = ARMISD::CXOR; break;
7334 }
7335 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7336 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7337 N1.getOperand(4));
7338}
7339
Owen Anderson080c0922010-11-05 19:27:46 +00007340static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007341 TargetLowering::DAGCombinerInfo &DCI,
7342 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007343
Owen Anderson080c0922010-11-05 19:27:46 +00007344 // Attempt to use immediate-form VBIC
7345 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7346 DebugLoc dl = N->getDebugLoc();
7347 EVT VT = N->getValueType(0);
7348 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007349
Tanya Lattner0433b212011-04-07 15:24:20 +00007350 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7351 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007352
Owen Anderson080c0922010-11-05 19:27:46 +00007353 APInt SplatBits, SplatUndef;
7354 unsigned SplatBitSize;
7355 bool HasAnyUndefs;
7356 if (BVN &&
7357 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7358 if (SplatBitSize <= 64) {
7359 EVT VbicVT;
7360 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7361 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007362 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007363 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007364 if (Val.getNode()) {
7365 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007366 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007367 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007368 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007369 }
7370 }
7371 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007372
Evan Chengc892aeb2012-02-23 01:19:06 +00007373 if (!Subtarget->isThumb1Only()) {
7374 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7375 SDValue CAND = formConditionalOp(N, DAG, true);
7376 if (CAND.getNode())
7377 return CAND;
7378 }
7379
Owen Anderson080c0922010-11-05 19:27:46 +00007380 return SDValue();
7381}
7382
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007383/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7384static SDValue PerformORCombine(SDNode *N,
7385 TargetLowering::DAGCombinerInfo &DCI,
7386 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007387 // Attempt to use immediate-form VORR
7388 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7389 DebugLoc dl = N->getDebugLoc();
7390 EVT VT = N->getValueType(0);
7391 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007392
Tanya Lattner0433b212011-04-07 15:24:20 +00007393 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7394 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007395
Owen Anderson60f48702010-11-03 23:15:26 +00007396 APInt SplatBits, SplatUndef;
7397 unsigned SplatBitSize;
7398 bool HasAnyUndefs;
7399 if (BVN && Subtarget->hasNEON() &&
7400 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7401 if (SplatBitSize <= 64) {
7402 EVT VorrVT;
7403 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7404 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007405 DAG, VorrVT, VT.is128BitVector(),
7406 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007407 if (Val.getNode()) {
7408 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007409 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007410 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007411 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007412 }
7413 }
7414 }
7415
Evan Chengc892aeb2012-02-23 01:19:06 +00007416 if (!Subtarget->isThumb1Only()) {
7417 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7418 SDValue COR = formConditionalOp(N, DAG, true);
7419 if (COR.getNode())
7420 return COR;
7421 }
7422
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007423 SDValue N0 = N->getOperand(0);
7424 if (N0.getOpcode() != ISD::AND)
7425 return SDValue();
7426 SDValue N1 = N->getOperand(1);
7427
7428 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7429 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7430 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7431 APInt SplatUndef;
7432 unsigned SplatBitSize;
7433 bool HasAnyUndefs;
7434
7435 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7436 APInt SplatBits0;
7437 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7438 HasAnyUndefs) && !HasAnyUndefs) {
7439 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7440 APInt SplatBits1;
7441 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7442 HasAnyUndefs) && !HasAnyUndefs &&
7443 SplatBits0 == ~SplatBits1) {
7444 // Canonicalize the vector type to make instruction selection simpler.
7445 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7446 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7447 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007448 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007449 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7450 }
7451 }
7452 }
7453
Jim Grosbach54238562010-07-17 03:30:54 +00007454 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7455 // reasonable.
7456
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007457 // BFI is only available on V6T2+
7458 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7459 return SDValue();
7460
Jim Grosbach54238562010-07-17 03:30:54 +00007461 DebugLoc DL = N->getDebugLoc();
7462 // 1) or (and A, mask), val => ARMbfi A, val, mask
7463 // iff (val & mask) == val
7464 //
7465 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7466 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007467 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007468 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007469 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007470 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007471
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007472 if (VT != MVT::i32)
7473 return SDValue();
7474
Evan Cheng30fb13f2010-12-13 20:32:54 +00007475 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007476
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007477 // The value and the mask need to be constants so we can verify this is
7478 // actually a bitfield set. If the mask is 0xffff, we can do better
7479 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007480 SDValue MaskOp = N0.getOperand(1);
7481 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7482 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007483 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007484 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007485 if (Mask == 0xffff)
7486 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007487 SDValue Res;
7488 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007489 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7490 if (N1C) {
7491 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007492 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007493 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007494
Evan Chenga9688c42010-12-11 04:11:38 +00007495 if (ARM::isBitFieldInvertedMask(Mask)) {
7496 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007497
Evan Cheng30fb13f2010-12-13 20:32:54 +00007498 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007499 DAG.getConstant(Val, MVT::i32),
7500 DAG.getConstant(Mask, MVT::i32));
7501
7502 // Do not add new nodes to DAG combiner worklist.
7503 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007504 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007505 }
Jim Grosbach54238562010-07-17 03:30:54 +00007506 } else if (N1.getOpcode() == ISD::AND) {
7507 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007508 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7509 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007510 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007511 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007512
Eric Christopher29aeed12011-03-26 01:21:03 +00007513 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7514 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007515 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007516 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007517 // The pack halfword instruction works better for masks that fit it,
7518 // so use that when it's available.
7519 if (Subtarget->hasT2ExtractPack() &&
7520 (Mask == 0xffff || Mask == 0xffff0000))
7521 return SDValue();
7522 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007523 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007524 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007525 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007526 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007527 DAG.getConstant(Mask, MVT::i32));
7528 // Do not add new nodes to DAG combiner worklist.
7529 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007530 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007531 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007532 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007533 // The pack halfword instruction works better for masks that fit it,
7534 // so use that when it's available.
7535 if (Subtarget->hasT2ExtractPack() &&
7536 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7537 return SDValue();
7538 // 2b
7539 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007540 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007541 DAG.getConstant(lsb, MVT::i32));
7542 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007543 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007544 // Do not add new nodes to DAG combiner worklist.
7545 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007546 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007547 }
7548 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007549
Evan Cheng30fb13f2010-12-13 20:32:54 +00007550 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7551 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7552 ARM::isBitFieldInvertedMask(~Mask)) {
7553 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7554 // where lsb(mask) == #shamt and masked bits of B are known zero.
7555 SDValue ShAmt = N00.getOperand(1);
7556 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7557 unsigned LSB = CountTrailingZeros_32(Mask);
7558 if (ShAmtC != LSB)
7559 return SDValue();
7560
7561 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7562 DAG.getConstant(~Mask, MVT::i32));
7563
7564 // Do not add new nodes to DAG combiner worklist.
7565 DCI.CombineTo(N, Res, false);
7566 }
7567
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007568 return SDValue();
7569}
7570
Evan Chengc892aeb2012-02-23 01:19:06 +00007571static SDValue PerformXORCombine(SDNode *N,
7572 TargetLowering::DAGCombinerInfo &DCI,
7573 const ARMSubtarget *Subtarget) {
7574 EVT VT = N->getValueType(0);
7575 SelectionDAG &DAG = DCI.DAG;
7576
7577 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7578 return SDValue();
7579
7580 if (!Subtarget->isThumb1Only()) {
7581 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7582 SDValue CXOR = formConditionalOp(N, DAG, true);
7583 if (CXOR.getNode())
7584 return CXOR;
7585 }
7586
7587 return SDValue();
7588}
7589
Evan Chengbf188ae2011-06-15 01:12:31 +00007590/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7591/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007592static SDValue PerformBFICombine(SDNode *N,
7593 TargetLowering::DAGCombinerInfo &DCI) {
7594 SDValue N1 = N->getOperand(1);
7595 if (N1.getOpcode() == ISD::AND) {
7596 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7597 if (!N11C)
7598 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007599 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7600 unsigned LSB = CountTrailingZeros_32(~InvMask);
7601 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7602 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007603 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007604 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007605 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7606 N->getOperand(0), N1.getOperand(0),
7607 N->getOperand(2));
7608 }
7609 return SDValue();
7610}
7611
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007612/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7613/// ARMISD::VMOVRRD.
7614static SDValue PerformVMOVRRDCombine(SDNode *N,
7615 TargetLowering::DAGCombinerInfo &DCI) {
7616 // vmovrrd(vmovdrr x, y) -> x,y
7617 SDValue InDouble = N->getOperand(0);
7618 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7619 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007620
7621 // vmovrrd(load f64) -> (load i32), (load i32)
7622 SDNode *InNode = InDouble.getNode();
7623 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7624 InNode->getValueType(0) == MVT::f64 &&
7625 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7626 !cast<LoadSDNode>(InNode)->isVolatile()) {
7627 // TODO: Should this be done for non-FrameIndex operands?
7628 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7629
7630 SelectionDAG &DAG = DCI.DAG;
7631 DebugLoc DL = LD->getDebugLoc();
7632 SDValue BasePtr = LD->getBasePtr();
7633 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7634 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007635 LD->isNonTemporal(), LD->isInvariant(),
7636 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007637
7638 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7639 DAG.getConstant(4, MVT::i32));
7640 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7641 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007642 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007643 std::min(4U, LD->getAlignment() / 2));
7644
7645 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7646 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7647 DCI.RemoveFromWorklist(LD);
7648 DAG.DeleteNode(LD);
7649 return Result;
7650 }
7651
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007652 return SDValue();
7653}
7654
7655/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7656/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7657static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7658 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7659 SDValue Op0 = N->getOperand(0);
7660 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007661 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007662 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007663 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007664 Op1 = Op1.getOperand(0);
7665 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7666 Op0.getNode() == Op1.getNode() &&
7667 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007668 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007669 N->getValueType(0), Op0.getOperand(0));
7670 return SDValue();
7671}
7672
Bob Wilson31600902010-12-21 06:43:19 +00007673/// PerformSTORECombine - Target-specific dag combine xforms for
7674/// ISD::STORE.
7675static SDValue PerformSTORECombine(SDNode *N,
7676 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007677 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007678 if (St->isVolatile())
7679 return SDValue();
7680
Andrew Trick49b446f2012-07-18 18:34:24 +00007681 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007682 // pack all of the elements in one place. Next, store to memory in fewer
7683 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007684 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007685 EVT VT = StVal.getValueType();
7686 if (St->isTruncatingStore() && VT.isVector()) {
7687 SelectionDAG &DAG = DCI.DAG;
7688 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7689 EVT StVT = St->getMemoryVT();
7690 unsigned NumElems = VT.getVectorNumElements();
7691 assert(StVT != VT && "Cannot truncate to the same type");
7692 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7693 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7694
7695 // From, To sizes and ElemCount must be pow of two
7696 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7697
7698 // We are going to use the original vector elt for storing.
7699 // Accumulated smaller vector elements must be a multiple of the store size.
7700 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7701
7702 unsigned SizeRatio = FromEltSz / ToEltSz;
7703 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7704
7705 // Create a type on which we perform the shuffle.
7706 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7707 NumElems*SizeRatio);
7708 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7709
7710 DebugLoc DL = St->getDebugLoc();
7711 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7712 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7713 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7714
7715 // Can't shuffle using an illegal type.
7716 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7717
7718 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7719 DAG.getUNDEF(WideVec.getValueType()),
7720 ShuffleVec.data());
7721 // At this point all of the data is stored at the bottom of the
7722 // register. We now need to save it to mem.
7723
7724 // Find the largest store unit
7725 MVT StoreType = MVT::i8;
7726 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7727 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7728 MVT Tp = (MVT::SimpleValueType)tp;
7729 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7730 StoreType = Tp;
7731 }
7732 // Didn't find a legal store type.
7733 if (!TLI.isTypeLegal(StoreType))
7734 return SDValue();
7735
7736 // Bitcast the original vector into a vector of store-size units
7737 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7738 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7739 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7740 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7741 SmallVector<SDValue, 8> Chains;
7742 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7743 TLI.getPointerTy());
7744 SDValue BasePtr = St->getBasePtr();
7745
7746 // Perform one or more big stores into memory.
7747 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7748 for (unsigned I = 0; I < E; I++) {
7749 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7750 StoreType, ShuffWide,
7751 DAG.getIntPtrConstant(I));
7752 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7753 St->getPointerInfo(), St->isVolatile(),
7754 St->isNonTemporal(), St->getAlignment());
7755 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7756 Increment);
7757 Chains.push_back(Ch);
7758 }
7759 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7760 Chains.size());
7761 }
7762
7763 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007764 return SDValue();
7765
Chad Rosier96b66d62012-04-09 19:38:15 +00007766 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7767 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007768 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007769 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007770 SelectionDAG &DAG = DCI.DAG;
7771 DebugLoc DL = St->getDebugLoc();
7772 SDValue BasePtr = St->getBasePtr();
7773 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7774 StVal.getNode()->getOperand(0), BasePtr,
7775 St->getPointerInfo(), St->isVolatile(),
7776 St->isNonTemporal(), St->getAlignment());
7777
7778 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7779 DAG.getConstant(4, MVT::i32));
7780 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7781 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7782 St->isNonTemporal(),
7783 std::min(4U, St->getAlignment() / 2));
7784 }
7785
7786 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007787 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7788 return SDValue();
7789
Chad Rosier96b66d62012-04-09 19:38:15 +00007790 // Bitcast an i64 store extracted from a vector to f64.
7791 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007792 SelectionDAG &DAG = DCI.DAG;
7793 DebugLoc dl = StVal.getDebugLoc();
7794 SDValue IntVec = StVal.getOperand(0);
7795 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7796 IntVec.getValueType().getVectorNumElements());
7797 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7798 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7799 Vec, StVal.getOperand(1));
7800 dl = N->getDebugLoc();
7801 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7802 // Make the DAGCombiner fold the bitcasts.
7803 DCI.AddToWorklist(Vec.getNode());
7804 DCI.AddToWorklist(ExtElt.getNode());
7805 DCI.AddToWorklist(V.getNode());
7806 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7807 St->getPointerInfo(), St->isVolatile(),
7808 St->isNonTemporal(), St->getAlignment(),
7809 St->getTBAAInfo());
7810}
7811
7812/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7813/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7814/// i64 vector to have f64 elements, since the value can then be loaded
7815/// directly into a VFP register.
7816static bool hasNormalLoadOperand(SDNode *N) {
7817 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7818 for (unsigned i = 0; i < NumElts; ++i) {
7819 SDNode *Elt = N->getOperand(i).getNode();
7820 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7821 return true;
7822 }
7823 return false;
7824}
7825
Bob Wilson75f02882010-09-17 22:59:05 +00007826/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7827/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007828static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7829 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007830 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7831 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7832 // into a pair of GPRs, which is fine when the value is used as a scalar,
7833 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007834 SelectionDAG &DAG = DCI.DAG;
7835 if (N->getNumOperands() == 2) {
7836 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7837 if (RV.getNode())
7838 return RV;
7839 }
Bob Wilson75f02882010-09-17 22:59:05 +00007840
Bob Wilson31600902010-12-21 06:43:19 +00007841 // Load i64 elements as f64 values so that type legalization does not split
7842 // them up into i32 values.
7843 EVT VT = N->getValueType(0);
7844 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7845 return SDValue();
7846 DebugLoc dl = N->getDebugLoc();
7847 SmallVector<SDValue, 8> Ops;
7848 unsigned NumElts = VT.getVectorNumElements();
7849 for (unsigned i = 0; i < NumElts; ++i) {
7850 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7851 Ops.push_back(V);
7852 // Make the DAGCombiner fold the bitcast.
7853 DCI.AddToWorklist(V.getNode());
7854 }
7855 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7856 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7857 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7858}
7859
7860/// PerformInsertEltCombine - Target-specific dag combine xforms for
7861/// ISD::INSERT_VECTOR_ELT.
7862static SDValue PerformInsertEltCombine(SDNode *N,
7863 TargetLowering::DAGCombinerInfo &DCI) {
7864 // Bitcast an i64 load inserted into a vector to f64.
7865 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7866 EVT VT = N->getValueType(0);
7867 SDNode *Elt = N->getOperand(1).getNode();
7868 if (VT.getVectorElementType() != MVT::i64 ||
7869 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7870 return SDValue();
7871
7872 SelectionDAG &DAG = DCI.DAG;
7873 DebugLoc dl = N->getDebugLoc();
7874 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7875 VT.getVectorNumElements());
7876 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7877 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7878 // Make the DAGCombiner fold the bitcasts.
7879 DCI.AddToWorklist(Vec.getNode());
7880 DCI.AddToWorklist(V.getNode());
7881 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7882 Vec, V, N->getOperand(2));
7883 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007884}
7885
Bob Wilsonf20700c2010-10-27 20:38:28 +00007886/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7887/// ISD::VECTOR_SHUFFLE.
7888static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7889 // The LLVM shufflevector instruction does not require the shuffle mask
7890 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7891 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7892 // operands do not match the mask length, they are extended by concatenating
7893 // them with undef vectors. That is probably the right thing for other
7894 // targets, but for NEON it is better to concatenate two double-register
7895 // size vector operands into a single quad-register size vector. Do that
7896 // transformation here:
7897 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7898 // shuffle(concat(v1, v2), undef)
7899 SDValue Op0 = N->getOperand(0);
7900 SDValue Op1 = N->getOperand(1);
7901 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7902 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7903 Op0.getNumOperands() != 2 ||
7904 Op1.getNumOperands() != 2)
7905 return SDValue();
7906 SDValue Concat0Op1 = Op0.getOperand(1);
7907 SDValue Concat1Op1 = Op1.getOperand(1);
7908 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7909 Concat1Op1.getOpcode() != ISD::UNDEF)
7910 return SDValue();
7911 // Skip the transformation if any of the types are illegal.
7912 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7913 EVT VT = N->getValueType(0);
7914 if (!TLI.isTypeLegal(VT) ||
7915 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7916 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7917 return SDValue();
7918
7919 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7920 Op0.getOperand(0), Op1.getOperand(0));
7921 // Translate the shuffle mask.
7922 SmallVector<int, 16> NewMask;
7923 unsigned NumElts = VT.getVectorNumElements();
7924 unsigned HalfElts = NumElts/2;
7925 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7926 for (unsigned n = 0; n < NumElts; ++n) {
7927 int MaskElt = SVN->getMaskElt(n);
7928 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007929 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007930 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007931 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007932 NewElt = HalfElts + MaskElt - NumElts;
7933 NewMask.push_back(NewElt);
7934 }
7935 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7936 DAG.getUNDEF(VT), NewMask.data());
7937}
7938
Bob Wilson1c3ef902011-02-07 17:43:21 +00007939/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7940/// NEON load/store intrinsics to merge base address updates.
7941static SDValue CombineBaseUpdate(SDNode *N,
7942 TargetLowering::DAGCombinerInfo &DCI) {
7943 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7944 return SDValue();
7945
7946 SelectionDAG &DAG = DCI.DAG;
7947 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7948 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7949 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7950 SDValue Addr = N->getOperand(AddrOpIdx);
7951
7952 // Search for a use of the address operand that is an increment.
7953 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7954 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7955 SDNode *User = *UI;
7956 if (User->getOpcode() != ISD::ADD ||
7957 UI.getUse().getResNo() != Addr.getResNo())
7958 continue;
7959
7960 // Check that the add is independent of the load/store. Otherwise, folding
7961 // it would create a cycle.
7962 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7963 continue;
7964
7965 // Find the new opcode for the updating load/store.
7966 bool isLoad = true;
7967 bool isLaneOp = false;
7968 unsigned NewOpc = 0;
7969 unsigned NumVecs = 0;
7970 if (isIntrinsic) {
7971 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7972 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007973 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007974 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7975 NumVecs = 1; break;
7976 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7977 NumVecs = 2; break;
7978 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7979 NumVecs = 3; break;
7980 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7981 NumVecs = 4; break;
7982 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7983 NumVecs = 2; isLaneOp = true; break;
7984 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7985 NumVecs = 3; isLaneOp = true; break;
7986 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7987 NumVecs = 4; isLaneOp = true; break;
7988 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7989 NumVecs = 1; isLoad = false; break;
7990 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7991 NumVecs = 2; isLoad = false; break;
7992 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7993 NumVecs = 3; isLoad = false; break;
7994 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7995 NumVecs = 4; isLoad = false; break;
7996 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7997 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7998 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7999 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8000 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8001 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8002 }
8003 } else {
8004 isLaneOp = true;
8005 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008006 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008007 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8008 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8009 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8010 }
8011 }
8012
8013 // Find the size of memory referenced by the load/store.
8014 EVT VecTy;
8015 if (isLoad)
8016 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008017 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008018 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8019 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8020 if (isLaneOp)
8021 NumBytes /= VecTy.getVectorNumElements();
8022
8023 // If the increment is a constant, it must match the memory ref size.
8024 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8025 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8026 uint64_t IncVal = CInc->getZExtValue();
8027 if (IncVal != NumBytes)
8028 continue;
8029 } else if (NumBytes >= 3 * 16) {
8030 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8031 // separate instructions that make it harder to use a non-constant update.
8032 continue;
8033 }
8034
8035 // Create the new updating load/store node.
8036 EVT Tys[6];
8037 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8038 unsigned n;
8039 for (n = 0; n < NumResultVecs; ++n)
8040 Tys[n] = VecTy;
8041 Tys[n++] = MVT::i32;
8042 Tys[n] = MVT::Other;
8043 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8044 SmallVector<SDValue, 8> Ops;
8045 Ops.push_back(N->getOperand(0)); // incoming chain
8046 Ops.push_back(N->getOperand(AddrOpIdx));
8047 Ops.push_back(Inc);
8048 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8049 Ops.push_back(N->getOperand(i));
8050 }
8051 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8052 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8053 Ops.data(), Ops.size(),
8054 MemInt->getMemoryVT(),
8055 MemInt->getMemOperand());
8056
8057 // Update the uses.
8058 std::vector<SDValue> NewResults;
8059 for (unsigned i = 0; i < NumResultVecs; ++i) {
8060 NewResults.push_back(SDValue(UpdN.getNode(), i));
8061 }
8062 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8063 DCI.CombineTo(N, NewResults);
8064 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8065
8066 break;
Owen Anderson76706012011-04-05 21:48:57 +00008067 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008068 return SDValue();
8069}
8070
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008071/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8072/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8073/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8074/// return true.
8075static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8076 SelectionDAG &DAG = DCI.DAG;
8077 EVT VT = N->getValueType(0);
8078 // vldN-dup instructions only support 64-bit vectors for N > 1.
8079 if (!VT.is64BitVector())
8080 return false;
8081
8082 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8083 SDNode *VLD = N->getOperand(0).getNode();
8084 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8085 return false;
8086 unsigned NumVecs = 0;
8087 unsigned NewOpc = 0;
8088 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8089 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8090 NumVecs = 2;
8091 NewOpc = ARMISD::VLD2DUP;
8092 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8093 NumVecs = 3;
8094 NewOpc = ARMISD::VLD3DUP;
8095 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8096 NumVecs = 4;
8097 NewOpc = ARMISD::VLD4DUP;
8098 } else {
8099 return false;
8100 }
8101
8102 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8103 // numbers match the load.
8104 unsigned VLDLaneNo =
8105 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8106 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8107 UI != UE; ++UI) {
8108 // Ignore uses of the chain result.
8109 if (UI.getUse().getResNo() == NumVecs)
8110 continue;
8111 SDNode *User = *UI;
8112 if (User->getOpcode() != ARMISD::VDUPLANE ||
8113 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8114 return false;
8115 }
8116
8117 // Create the vldN-dup node.
8118 EVT Tys[5];
8119 unsigned n;
8120 for (n = 0; n < NumVecs; ++n)
8121 Tys[n] = VT;
8122 Tys[n] = MVT::Other;
8123 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8124 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8125 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8126 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8127 Ops, 2, VLDMemInt->getMemoryVT(),
8128 VLDMemInt->getMemOperand());
8129
8130 // Update the uses.
8131 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8132 UI != UE; ++UI) {
8133 unsigned ResNo = UI.getUse().getResNo();
8134 // Ignore uses of the chain result.
8135 if (ResNo == NumVecs)
8136 continue;
8137 SDNode *User = *UI;
8138 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8139 }
8140
8141 // Now the vldN-lane intrinsic is dead except for its chain result.
8142 // Update uses of the chain.
8143 std::vector<SDValue> VLDDupResults;
8144 for (unsigned n = 0; n < NumVecs; ++n)
8145 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8146 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8147 DCI.CombineTo(VLD, VLDDupResults);
8148
8149 return true;
8150}
8151
Bob Wilson9e82bf12010-07-14 01:22:12 +00008152/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8153/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008154static SDValue PerformVDUPLANECombine(SDNode *N,
8155 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008156 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008157
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008158 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8159 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8160 if (CombineVLDDUP(N, DCI))
8161 return SDValue(N, 0);
8162
8163 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8164 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008165 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008166 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008167 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008168 return SDValue();
8169
8170 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8171 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8172 // The canonical VMOV for a zero vector uses a 32-bit element size.
8173 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8174 unsigned EltBits;
8175 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8176 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008177 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008178 if (EltSize > VT.getVectorElementType().getSizeInBits())
8179 return SDValue();
8180
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008181 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008182}
8183
Eric Christopherfa6f5912011-06-29 21:10:36 +00008184// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008185// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8186static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8187{
Chad Rosier118c9a02011-06-28 17:26:57 +00008188 integerPart cN;
8189 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008190 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8191 I != E; I++) {
8192 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8193 if (!C)
8194 return false;
8195
Eric Christopherfa6f5912011-06-29 21:10:36 +00008196 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008197 APFloat APF = C->getValueAPF();
8198 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8199 != APFloat::opOK || !isExact)
8200 return false;
8201
8202 c0 = (I == 0) ? cN : c0;
8203 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8204 return false;
8205 }
8206 C = c0;
8207 return true;
8208}
8209
8210/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8211/// can replace combinations of VMUL and VCVT (floating-point to integer)
8212/// when the VMUL has a constant operand that is a power of 2.
8213///
8214/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8215/// vmul.f32 d16, d17, d16
8216/// vcvt.s32.f32 d16, d16
8217/// becomes:
8218/// vcvt.s32.f32 d16, d16, #3
8219static SDValue PerformVCVTCombine(SDNode *N,
8220 TargetLowering::DAGCombinerInfo &DCI,
8221 const ARMSubtarget *Subtarget) {
8222 SelectionDAG &DAG = DCI.DAG;
8223 SDValue Op = N->getOperand(0);
8224
8225 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8226 Op.getOpcode() != ISD::FMUL)
8227 return SDValue();
8228
8229 uint64_t C;
8230 SDValue N0 = Op->getOperand(0);
8231 SDValue ConstVec = Op->getOperand(1);
8232 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8233
Eric Christopherfa6f5912011-06-29 21:10:36 +00008234 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008235 !isConstVecPow2(ConstVec, isSigned, C))
8236 return SDValue();
8237
8238 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8239 Intrinsic::arm_neon_vcvtfp2fxu;
8240 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8241 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008242 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008243 DAG.getConstant(Log2_64(C), MVT::i32));
8244}
8245
8246/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8247/// can replace combinations of VCVT (integer to floating-point) and VDIV
8248/// when the VDIV has a constant operand that is a power of 2.
8249///
8250/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8251/// vcvt.f32.s32 d16, d16
8252/// vdiv.f32 d16, d17, d16
8253/// becomes:
8254/// vcvt.f32.s32 d16, d16, #3
8255static SDValue PerformVDIVCombine(SDNode *N,
8256 TargetLowering::DAGCombinerInfo &DCI,
8257 const ARMSubtarget *Subtarget) {
8258 SelectionDAG &DAG = DCI.DAG;
8259 SDValue Op = N->getOperand(0);
8260 unsigned OpOpcode = Op.getNode()->getOpcode();
8261
8262 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8263 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8264 return SDValue();
8265
8266 uint64_t C;
8267 SDValue ConstVec = N->getOperand(1);
8268 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8269
8270 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8271 !isConstVecPow2(ConstVec, isSigned, C))
8272 return SDValue();
8273
Eric Christopherfa6f5912011-06-29 21:10:36 +00008274 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008275 Intrinsic::arm_neon_vcvtfxu2fp;
8276 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8277 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008278 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008279 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8280}
8281
8282/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008283/// operand of a vector shift operation, where all the elements of the
8284/// build_vector must have the same constant integer value.
8285static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8286 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008287 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008288 Op = Op.getOperand(0);
8289 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8290 APInt SplatBits, SplatUndef;
8291 unsigned SplatBitSize;
8292 bool HasAnyUndefs;
8293 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8294 HasAnyUndefs, ElementBits) ||
8295 SplatBitSize > ElementBits)
8296 return false;
8297 Cnt = SplatBits.getSExtValue();
8298 return true;
8299}
8300
8301/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8302/// operand of a vector shift left operation. That value must be in the range:
8303/// 0 <= Value < ElementBits for a left shift; or
8304/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008305static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008306 assert(VT.isVector() && "vector shift count is not a vector type");
8307 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8308 if (! getVShiftImm(Op, ElementBits, Cnt))
8309 return false;
8310 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8311}
8312
8313/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8314/// operand of a vector shift right operation. For a shift opcode, the value
8315/// is positive, but for an intrinsic the value count must be negative. The
8316/// absolute value must be in the range:
8317/// 1 <= |Value| <= ElementBits for a right shift; or
8318/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008319static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008320 int64_t &Cnt) {
8321 assert(VT.isVector() && "vector shift count is not a vector type");
8322 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8323 if (! getVShiftImm(Op, ElementBits, Cnt))
8324 return false;
8325 if (isIntrinsic)
8326 Cnt = -Cnt;
8327 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8328}
8329
8330/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8331static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8332 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8333 switch (IntNo) {
8334 default:
8335 // Don't do anything for most intrinsics.
8336 break;
8337
8338 // Vector shifts: check for immediate versions and lower them.
8339 // Note: This is done during DAG combining instead of DAG legalizing because
8340 // the build_vectors for 64-bit vector element shift counts are generally
8341 // not legal, and it is hard to see their values after they get legalized to
8342 // loads from a constant pool.
8343 case Intrinsic::arm_neon_vshifts:
8344 case Intrinsic::arm_neon_vshiftu:
8345 case Intrinsic::arm_neon_vshiftls:
8346 case Intrinsic::arm_neon_vshiftlu:
8347 case Intrinsic::arm_neon_vshiftn:
8348 case Intrinsic::arm_neon_vrshifts:
8349 case Intrinsic::arm_neon_vrshiftu:
8350 case Intrinsic::arm_neon_vrshiftn:
8351 case Intrinsic::arm_neon_vqshifts:
8352 case Intrinsic::arm_neon_vqshiftu:
8353 case Intrinsic::arm_neon_vqshiftsu:
8354 case Intrinsic::arm_neon_vqshiftns:
8355 case Intrinsic::arm_neon_vqshiftnu:
8356 case Intrinsic::arm_neon_vqshiftnsu:
8357 case Intrinsic::arm_neon_vqrshiftns:
8358 case Intrinsic::arm_neon_vqrshiftnu:
8359 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008360 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008361 int64_t Cnt;
8362 unsigned VShiftOpc = 0;
8363
8364 switch (IntNo) {
8365 case Intrinsic::arm_neon_vshifts:
8366 case Intrinsic::arm_neon_vshiftu:
8367 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8368 VShiftOpc = ARMISD::VSHL;
8369 break;
8370 }
8371 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8372 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8373 ARMISD::VSHRs : ARMISD::VSHRu);
8374 break;
8375 }
8376 return SDValue();
8377
8378 case Intrinsic::arm_neon_vshiftls:
8379 case Intrinsic::arm_neon_vshiftlu:
8380 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8381 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008382 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008383
8384 case Intrinsic::arm_neon_vrshifts:
8385 case Intrinsic::arm_neon_vrshiftu:
8386 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8387 break;
8388 return SDValue();
8389
8390 case Intrinsic::arm_neon_vqshifts:
8391 case Intrinsic::arm_neon_vqshiftu:
8392 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8393 break;
8394 return SDValue();
8395
8396 case Intrinsic::arm_neon_vqshiftsu:
8397 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8398 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008399 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008400
8401 case Intrinsic::arm_neon_vshiftn:
8402 case Intrinsic::arm_neon_vrshiftn:
8403 case Intrinsic::arm_neon_vqshiftns:
8404 case Intrinsic::arm_neon_vqshiftnu:
8405 case Intrinsic::arm_neon_vqshiftnsu:
8406 case Intrinsic::arm_neon_vqrshiftns:
8407 case Intrinsic::arm_neon_vqrshiftnu:
8408 case Intrinsic::arm_neon_vqrshiftnsu:
8409 // Narrowing shifts require an immediate right shift.
8410 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8411 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008412 llvm_unreachable("invalid shift count for narrowing vector shift "
8413 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008414
8415 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008416 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008417 }
8418
8419 switch (IntNo) {
8420 case Intrinsic::arm_neon_vshifts:
8421 case Intrinsic::arm_neon_vshiftu:
8422 // Opcode already set above.
8423 break;
8424 case Intrinsic::arm_neon_vshiftls:
8425 case Intrinsic::arm_neon_vshiftlu:
8426 if (Cnt == VT.getVectorElementType().getSizeInBits())
8427 VShiftOpc = ARMISD::VSHLLi;
8428 else
8429 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8430 ARMISD::VSHLLs : ARMISD::VSHLLu);
8431 break;
8432 case Intrinsic::arm_neon_vshiftn:
8433 VShiftOpc = ARMISD::VSHRN; break;
8434 case Intrinsic::arm_neon_vrshifts:
8435 VShiftOpc = ARMISD::VRSHRs; break;
8436 case Intrinsic::arm_neon_vrshiftu:
8437 VShiftOpc = ARMISD::VRSHRu; break;
8438 case Intrinsic::arm_neon_vrshiftn:
8439 VShiftOpc = ARMISD::VRSHRN; break;
8440 case Intrinsic::arm_neon_vqshifts:
8441 VShiftOpc = ARMISD::VQSHLs; break;
8442 case Intrinsic::arm_neon_vqshiftu:
8443 VShiftOpc = ARMISD::VQSHLu; break;
8444 case Intrinsic::arm_neon_vqshiftsu:
8445 VShiftOpc = ARMISD::VQSHLsu; break;
8446 case Intrinsic::arm_neon_vqshiftns:
8447 VShiftOpc = ARMISD::VQSHRNs; break;
8448 case Intrinsic::arm_neon_vqshiftnu:
8449 VShiftOpc = ARMISD::VQSHRNu; break;
8450 case Intrinsic::arm_neon_vqshiftnsu:
8451 VShiftOpc = ARMISD::VQSHRNsu; break;
8452 case Intrinsic::arm_neon_vqrshiftns:
8453 VShiftOpc = ARMISD::VQRSHRNs; break;
8454 case Intrinsic::arm_neon_vqrshiftnu:
8455 VShiftOpc = ARMISD::VQRSHRNu; break;
8456 case Intrinsic::arm_neon_vqrshiftnsu:
8457 VShiftOpc = ARMISD::VQRSHRNsu; break;
8458 }
8459
8460 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008461 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008462 }
8463
8464 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008465 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008466 int64_t Cnt;
8467 unsigned VShiftOpc = 0;
8468
8469 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8470 VShiftOpc = ARMISD::VSLI;
8471 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8472 VShiftOpc = ARMISD::VSRI;
8473 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008474 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008475 }
8476
8477 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8478 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008479 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008480 }
8481
8482 case Intrinsic::arm_neon_vqrshifts:
8483 case Intrinsic::arm_neon_vqrshiftu:
8484 // No immediate versions of these to check for.
8485 break;
8486 }
8487
8488 return SDValue();
8489}
8490
8491/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8492/// lowers them. As with the vector shift intrinsics, this is done during DAG
8493/// combining instead of DAG legalizing because the build_vectors for 64-bit
8494/// vector element shift counts are generally not legal, and it is hard to see
8495/// their values after they get legalized to loads from a constant pool.
8496static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8497 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008498 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008499 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8500 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8501 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8502 SDValue N1 = N->getOperand(1);
8503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8504 SDValue N0 = N->getOperand(0);
8505 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8506 DAG.MaskedValueIsZero(N0.getOperand(0),
8507 APInt::getHighBitsSet(32, 16)))
8508 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8509 }
8510 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008511
8512 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008513 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8514 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008515 return SDValue();
8516
8517 assert(ST->hasNEON() && "unexpected vector shift");
8518 int64_t Cnt;
8519
8520 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008521 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008522
8523 case ISD::SHL:
8524 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8525 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008526 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008527 break;
8528
8529 case ISD::SRA:
8530 case ISD::SRL:
8531 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8532 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8533 ARMISD::VSHRs : ARMISD::VSHRu);
8534 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008535 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008536 }
8537 }
8538 return SDValue();
8539}
8540
8541/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8542/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8543static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8544 const ARMSubtarget *ST) {
8545 SDValue N0 = N->getOperand(0);
8546
8547 // Check for sign- and zero-extensions of vector extract operations of 8-
8548 // and 16-bit vector elements. NEON supports these directly. They are
8549 // handled during DAG combining because type legalization will promote them
8550 // to 32-bit types and it is messy to recognize the operations after that.
8551 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8552 SDValue Vec = N0.getOperand(0);
8553 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008554 EVT VT = N->getValueType(0);
8555 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008556 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8557
Owen Anderson825b72b2009-08-11 20:47:22 +00008558 if (VT == MVT::i32 &&
8559 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008560 TLI.isTypeLegal(Vec.getValueType()) &&
8561 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008562
8563 unsigned Opc = 0;
8564 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008565 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008566 case ISD::SIGN_EXTEND:
8567 Opc = ARMISD::VGETLANEs;
8568 break;
8569 case ISD::ZERO_EXTEND:
8570 case ISD::ANY_EXTEND:
8571 Opc = ARMISD::VGETLANEu;
8572 break;
8573 }
8574 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8575 }
8576 }
8577
8578 return SDValue();
8579}
8580
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008581/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8582/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8583static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8584 const ARMSubtarget *ST) {
8585 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008586 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008587 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8588 // a NaN; only do the transformation when it matches that behavior.
8589
8590 // For now only do this when using NEON for FP operations; if using VFP, it
8591 // is not obvious that the benefit outweighs the cost of switching to the
8592 // NEON pipeline.
8593 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8594 N->getValueType(0) != MVT::f32)
8595 return SDValue();
8596
8597 SDValue CondLHS = N->getOperand(0);
8598 SDValue CondRHS = N->getOperand(1);
8599 SDValue LHS = N->getOperand(2);
8600 SDValue RHS = N->getOperand(3);
8601 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8602
8603 unsigned Opcode = 0;
8604 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008605 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008606 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008607 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008608 IsReversed = true ; // x CC y ? y : x
8609 } else {
8610 return SDValue();
8611 }
8612
Bob Wilsone742bb52010-02-24 22:15:53 +00008613 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008614 switch (CC) {
8615 default: break;
8616 case ISD::SETOLT:
8617 case ISD::SETOLE:
8618 case ISD::SETLT:
8619 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008620 case ISD::SETULT:
8621 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008622 // If LHS is NaN, an ordered comparison will be false and the result will
8623 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8624 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8625 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8626 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8627 break;
8628 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8629 // will return -0, so vmin can only be used for unsafe math or if one of
8630 // the operands is known to be nonzero.
8631 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008632 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008633 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8634 break;
8635 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008636 break;
8637
8638 case ISD::SETOGT:
8639 case ISD::SETOGE:
8640 case ISD::SETGT:
8641 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008642 case ISD::SETUGT:
8643 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008644 // If LHS is NaN, an ordered comparison will be false and the result will
8645 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8646 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8647 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8648 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8649 break;
8650 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8651 // will return +0, so vmax can only be used for unsafe math or if one of
8652 // the operands is known to be nonzero.
8653 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008654 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008655 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8656 break;
8657 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008658 break;
8659 }
8660
8661 if (!Opcode)
8662 return SDValue();
8663 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8664}
8665
Evan Chenge721f5c2011-07-13 00:42:17 +00008666/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8667SDValue
8668ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8669 SDValue Cmp = N->getOperand(4);
8670 if (Cmp.getOpcode() != ARMISD::CMPZ)
8671 // Only looking at EQ and NE cases.
8672 return SDValue();
8673
8674 EVT VT = N->getValueType(0);
8675 DebugLoc dl = N->getDebugLoc();
8676 SDValue LHS = Cmp.getOperand(0);
8677 SDValue RHS = Cmp.getOperand(1);
8678 SDValue FalseVal = N->getOperand(0);
8679 SDValue TrueVal = N->getOperand(1);
8680 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008681 ARMCC::CondCodes CC =
8682 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008683
8684 // Simplify
8685 // mov r1, r0
8686 // cmp r1, x
8687 // mov r0, y
8688 // moveq r0, x
8689 // to
8690 // cmp r0, x
8691 // movne r0, y
8692 //
8693 // mov r1, r0
8694 // cmp r1, x
8695 // mov r0, x
8696 // movne r0, y
8697 // to
8698 // cmp r0, x
8699 // movne r0, y
8700 /// FIXME: Turn this into a target neutral optimization?
8701 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008702 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008703 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8704 N->getOperand(3), Cmp);
8705 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8706 SDValue ARMcc;
8707 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8708 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8709 N->getOperand(3), NewCmp);
8710 }
8711
8712 if (Res.getNode()) {
8713 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008714 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008715 // Capture demanded bits information that would be otherwise lost.
8716 if (KnownZero == 0xfffffffe)
8717 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8718 DAG.getValueType(MVT::i1));
8719 else if (KnownZero == 0xffffff00)
8720 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8721 DAG.getValueType(MVT::i8));
8722 else if (KnownZero == 0xffff0000)
8723 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8724 DAG.getValueType(MVT::i16));
8725 }
8726
8727 return Res;
8728}
8729
Dan Gohman475871a2008-07-27 21:46:04 +00008730SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008731 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008732 switch (N->getOpcode()) {
8733 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008734 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008735 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008736 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008737 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008738 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8739 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008740 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008741 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008742 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008743 case ISD::STORE: return PerformSTORECombine(N, DCI);
8744 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8745 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008746 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008747 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008748 case ISD::FP_TO_SINT:
8749 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8750 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008751 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008752 case ISD::SHL:
8753 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008754 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008755 case ISD::SIGN_EXTEND:
8756 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008757 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8758 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008759 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008760 case ARMISD::VLD2DUP:
8761 case ARMISD::VLD3DUP:
8762 case ARMISD::VLD4DUP:
8763 return CombineBaseUpdate(N, DCI);
8764 case ISD::INTRINSIC_VOID:
8765 case ISD::INTRINSIC_W_CHAIN:
8766 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8767 case Intrinsic::arm_neon_vld1:
8768 case Intrinsic::arm_neon_vld2:
8769 case Intrinsic::arm_neon_vld3:
8770 case Intrinsic::arm_neon_vld4:
8771 case Intrinsic::arm_neon_vld2lane:
8772 case Intrinsic::arm_neon_vld3lane:
8773 case Intrinsic::arm_neon_vld4lane:
8774 case Intrinsic::arm_neon_vst1:
8775 case Intrinsic::arm_neon_vst2:
8776 case Intrinsic::arm_neon_vst3:
8777 case Intrinsic::arm_neon_vst4:
8778 case Intrinsic::arm_neon_vst2lane:
8779 case Intrinsic::arm_neon_vst3lane:
8780 case Intrinsic::arm_neon_vst4lane:
8781 return CombineBaseUpdate(N, DCI);
8782 default: break;
8783 }
8784 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008785 }
Dan Gohman475871a2008-07-27 21:46:04 +00008786 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008787}
8788
Evan Cheng31959b12011-02-02 01:06:55 +00008789bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8790 EVT VT) const {
8791 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8792}
8793
Bill Wendlingaf566342009-08-15 21:21:19 +00008794bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008795 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008796 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008797
8798 switch (VT.getSimpleVT().SimpleTy) {
8799 default:
8800 return false;
8801 case MVT::i8:
8802 case MVT::i16:
8803 case MVT::i32:
8804 return true;
8805 // FIXME: VLD1 etc with standard alignment is legal.
8806 }
8807}
8808
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008809static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8810 unsigned AlignCheck) {
8811 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8812 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8813}
8814
8815EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8816 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008817 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008818 bool MemcpyStrSrc,
8819 MachineFunction &MF) const {
8820 const Function *F = MF.getFunction();
8821
8822 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008823 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008824 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8825 Subtarget->hasNEON()) {
8826 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8827 return MVT::v4i32;
8828 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8829 return MVT::v2i32;
8830 }
8831 }
8832
Lang Hames5207bf22011-11-08 18:56:23 +00008833 // Lowering to i32/i16 if the size permits.
8834 if (Size >= 4) {
8835 return MVT::i32;
8836 } else if (Size >= 2) {
8837 return MVT::i16;
8838 }
8839
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008840 // Let the target-independent logic figure it out.
8841 return MVT::Other;
8842}
8843
Evan Chenge6c835f2009-08-14 20:09:37 +00008844static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8845 if (V < 0)
8846 return false;
8847
8848 unsigned Scale = 1;
8849 switch (VT.getSimpleVT().SimpleTy) {
8850 default: return false;
8851 case MVT::i1:
8852 case MVT::i8:
8853 // Scale == 1;
8854 break;
8855 case MVT::i16:
8856 // Scale == 2;
8857 Scale = 2;
8858 break;
8859 case MVT::i32:
8860 // Scale == 4;
8861 Scale = 4;
8862 break;
8863 }
8864
8865 if ((V & (Scale - 1)) != 0)
8866 return false;
8867 V /= Scale;
8868 return V == (V & ((1LL << 5) - 1));
8869}
8870
8871static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8872 const ARMSubtarget *Subtarget) {
8873 bool isNeg = false;
8874 if (V < 0) {
8875 isNeg = true;
8876 V = - V;
8877 }
8878
8879 switch (VT.getSimpleVT().SimpleTy) {
8880 default: return false;
8881 case MVT::i1:
8882 case MVT::i8:
8883 case MVT::i16:
8884 case MVT::i32:
8885 // + imm12 or - imm8
8886 if (isNeg)
8887 return V == (V & ((1LL << 8) - 1));
8888 return V == (V & ((1LL << 12) - 1));
8889 case MVT::f32:
8890 case MVT::f64:
8891 // Same as ARM mode. FIXME: NEON?
8892 if (!Subtarget->hasVFP2())
8893 return false;
8894 if ((V & 3) != 0)
8895 return false;
8896 V >>= 2;
8897 return V == (V & ((1LL << 8) - 1));
8898 }
8899}
8900
Evan Chengb01fad62007-03-12 23:30:29 +00008901/// isLegalAddressImmediate - Return true if the integer value can be used
8902/// as the offset of the target addressing mode for load / store of the
8903/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008904static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008905 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008906 if (V == 0)
8907 return true;
8908
Evan Cheng65011532009-03-09 19:15:00 +00008909 if (!VT.isSimple())
8910 return false;
8911
Evan Chenge6c835f2009-08-14 20:09:37 +00008912 if (Subtarget->isThumb1Only())
8913 return isLegalT1AddressImmediate(V, VT);
8914 else if (Subtarget->isThumb2())
8915 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008916
Evan Chenge6c835f2009-08-14 20:09:37 +00008917 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008918 if (V < 0)
8919 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008920 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008921 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008922 case MVT::i1:
8923 case MVT::i8:
8924 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008925 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008926 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008927 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008928 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008929 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008930 case MVT::f32:
8931 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008932 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008933 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008934 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008935 return false;
8936 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008937 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008938 }
Evan Chenga8e29892007-01-19 07:51:42 +00008939}
8940
Evan Chenge6c835f2009-08-14 20:09:37 +00008941bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8942 EVT VT) const {
8943 int Scale = AM.Scale;
8944 if (Scale < 0)
8945 return false;
8946
8947 switch (VT.getSimpleVT().SimpleTy) {
8948 default: return false;
8949 case MVT::i1:
8950 case MVT::i8:
8951 case MVT::i16:
8952 case MVT::i32:
8953 if (Scale == 1)
8954 return true;
8955 // r + r << imm
8956 Scale = Scale & ~1;
8957 return Scale == 2 || Scale == 4 || Scale == 8;
8958 case MVT::i64:
8959 // r + r
8960 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8961 return true;
8962 return false;
8963 case MVT::isVoid:
8964 // Note, we allow "void" uses (basically, uses that aren't loads or
8965 // stores), because arm allows folding a scale into many arithmetic
8966 // operations. This should be made more precise and revisited later.
8967
8968 // Allow r << imm, but the imm has to be a multiple of two.
8969 if (Scale & 1) return false;
8970 return isPowerOf2_32(Scale);
8971 }
8972}
8973
Chris Lattner37caf8c2007-04-09 23:33:39 +00008974/// isLegalAddressingMode - Return true if the addressing mode represented
8975/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008976bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008977 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008978 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008979 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008980 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008981
Chris Lattner37caf8c2007-04-09 23:33:39 +00008982 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008983 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008984 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008985
Chris Lattner37caf8c2007-04-09 23:33:39 +00008986 switch (AM.Scale) {
8987 case 0: // no scale reg, must be "r+i" or "r", or "i".
8988 break;
8989 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008990 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008991 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008992 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008993 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008994 // ARM doesn't support any R+R*scale+imm addr modes.
8995 if (AM.BaseOffs)
8996 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008997
Bob Wilson2c7dab12009-04-08 17:55:28 +00008998 if (!VT.isSimple())
8999 return false;
9000
Evan Chenge6c835f2009-08-14 20:09:37 +00009001 if (Subtarget->isThumb2())
9002 return isLegalT2ScaledAddressingMode(AM, VT);
9003
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009004 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009005 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009006 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009007 case MVT::i1:
9008 case MVT::i8:
9009 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009010 if (Scale < 0) Scale = -Scale;
9011 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009012 return true;
9013 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009014 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009015 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009016 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009017 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009018 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009019 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009020 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009021
Owen Anderson825b72b2009-08-11 20:47:22 +00009022 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009023 // Note, we allow "void" uses (basically, uses that aren't loads or
9024 // stores), because arm allows folding a scale into many arithmetic
9025 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009026
Chris Lattner37caf8c2007-04-09 23:33:39 +00009027 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009028 if (Scale & 1) return false;
9029 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009030 }
Evan Chengb01fad62007-03-12 23:30:29 +00009031 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009032 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009033}
9034
Evan Cheng77e47512009-11-11 19:05:52 +00009035/// isLegalICmpImmediate - Return true if the specified immediate is legal
9036/// icmp immediate, that is the target has icmp instructions which can compare
9037/// a register against the immediate without having to materialize the
9038/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009039bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009040 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009041 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009042 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009043 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009044 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009045 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009046 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009047}
9048
Dan Gohmancca82142011-05-03 00:46:49 +00009049/// isLegalAddImmediate - Return true if the specified immediate is legal
9050/// add immediate, that is the target has add instructions which can add
9051/// a register with the immediate without having to materialize the
9052/// immediate into a register.
9053bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
9054 return ARM_AM::getSOImmVal(Imm) != -1;
9055}
9056
Owen Andersone50ed302009-08-10 22:56:29 +00009057static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009058 bool isSEXTLoad, SDValue &Base,
9059 SDValue &Offset, bool &isInc,
9060 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009061 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9062 return false;
9063
Owen Anderson825b72b2009-08-11 20:47:22 +00009064 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009065 // AddressingMode 3
9066 Base = Ptr->getOperand(0);
9067 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009068 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009069 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009070 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009071 isInc = false;
9072 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9073 return true;
9074 }
9075 }
9076 isInc = (Ptr->getOpcode() == ISD::ADD);
9077 Offset = Ptr->getOperand(1);
9078 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009079 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009080 // AddressingMode 2
9081 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009082 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009083 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009084 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009085 isInc = false;
9086 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9087 Base = Ptr->getOperand(0);
9088 return true;
9089 }
9090 }
9091
9092 if (Ptr->getOpcode() == ISD::ADD) {
9093 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009094 ARM_AM::ShiftOpc ShOpcVal=
9095 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009096 if (ShOpcVal != ARM_AM::no_shift) {
9097 Base = Ptr->getOperand(1);
9098 Offset = Ptr->getOperand(0);
9099 } else {
9100 Base = Ptr->getOperand(0);
9101 Offset = Ptr->getOperand(1);
9102 }
9103 return true;
9104 }
9105
9106 isInc = (Ptr->getOpcode() == ISD::ADD);
9107 Base = Ptr->getOperand(0);
9108 Offset = Ptr->getOperand(1);
9109 return true;
9110 }
9111
Jim Grosbache5165492009-11-09 00:11:35 +00009112 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009113 return false;
9114}
9115
Owen Andersone50ed302009-08-10 22:56:29 +00009116static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009117 bool isSEXTLoad, SDValue &Base,
9118 SDValue &Offset, bool &isInc,
9119 SelectionDAG &DAG) {
9120 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9121 return false;
9122
9123 Base = Ptr->getOperand(0);
9124 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9125 int RHSC = (int)RHS->getZExtValue();
9126 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9127 assert(Ptr->getOpcode() == ISD::ADD);
9128 isInc = false;
9129 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9130 return true;
9131 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9132 isInc = Ptr->getOpcode() == ISD::ADD;
9133 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9134 return true;
9135 }
9136 }
9137
9138 return false;
9139}
9140
Evan Chenga8e29892007-01-19 07:51:42 +00009141/// getPreIndexedAddressParts - returns true by value, base pointer and
9142/// offset pointer and addressing mode by reference if the node's address
9143/// can be legally represented as pre-indexed load / store address.
9144bool
Dan Gohman475871a2008-07-27 21:46:04 +00009145ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9146 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009147 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009148 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009149 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009150 return false;
9151
Owen Andersone50ed302009-08-10 22:56:29 +00009152 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009153 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009154 bool isSEXTLoad = false;
9155 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9156 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009157 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009158 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9159 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9160 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009161 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009162 } else
9163 return false;
9164
9165 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009166 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009167 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009168 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9169 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009170 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009171 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009172 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009173 if (!isLegal)
9174 return false;
9175
9176 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9177 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009178}
9179
9180/// getPostIndexedAddressParts - returns true by value, base pointer and
9181/// offset pointer and addressing mode by reference if this node can be
9182/// combined with a load / store to form a post-indexed load / store.
9183bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009184 SDValue &Base,
9185 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009186 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009187 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009188 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009189 return false;
9190
Owen Andersone50ed302009-08-10 22:56:29 +00009191 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009192 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009193 bool isSEXTLoad = false;
9194 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009195 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009196 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009197 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9198 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009199 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009200 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009201 } else
9202 return false;
9203
9204 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009205 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009206 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009207 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009208 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009209 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009210 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9211 isInc, DAG);
9212 if (!isLegal)
9213 return false;
9214
Evan Cheng28dad2a2010-05-18 21:31:17 +00009215 if (Ptr != Base) {
9216 // Swap base ptr and offset to catch more post-index load / store when
9217 // it's legal. In Thumb2 mode, offset must be an immediate.
9218 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9219 !Subtarget->isThumb2())
9220 std::swap(Base, Offset);
9221
9222 // Post-indexed load / store update the base pointer.
9223 if (Ptr != Base)
9224 return false;
9225 }
9226
Evan Chenge88d5ce2009-07-02 07:28:31 +00009227 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9228 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009229}
9230
Dan Gohman475871a2008-07-27 21:46:04 +00009231void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009232 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009233 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009234 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009235 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009236 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009237 switch (Op.getOpcode()) {
9238 default: break;
9239 case ARMISD::CMOV: {
9240 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009241 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009242 if (KnownZero == 0 && KnownOne == 0) return;
9243
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009244 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009245 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009246 KnownZero &= KnownZeroRHS;
9247 KnownOne &= KnownOneRHS;
9248 return;
9249 }
9250 }
9251}
9252
9253//===----------------------------------------------------------------------===//
9254// ARM Inline Assembly Support
9255//===----------------------------------------------------------------------===//
9256
Evan Cheng55d42002011-01-08 01:24:27 +00009257bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9258 // Looking for "rev" which is V6+.
9259 if (!Subtarget->hasV6Ops())
9260 return false;
9261
9262 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9263 std::string AsmStr = IA->getAsmString();
9264 SmallVector<StringRef, 4> AsmPieces;
9265 SplitString(AsmStr, AsmPieces, ";\n");
9266
9267 switch (AsmPieces.size()) {
9268 default: return false;
9269 case 1:
9270 AsmStr = AsmPieces[0];
9271 AsmPieces.clear();
9272 SplitString(AsmStr, AsmPieces, " \t,");
9273
9274 // rev $0, $1
9275 if (AsmPieces.size() == 3 &&
9276 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9277 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009278 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009279 if (Ty && Ty->getBitWidth() == 32)
9280 return IntrinsicLowering::LowerToByteSwap(CI);
9281 }
9282 break;
9283 }
9284
9285 return false;
9286}
9287
Evan Chenga8e29892007-01-19 07:51:42 +00009288/// getConstraintType - Given a constraint letter, return the type of
9289/// constraint it is for this target.
9290ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009291ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9292 if (Constraint.size() == 1) {
9293 switch (Constraint[0]) {
9294 default: break;
9295 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009296 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009297 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009298 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009299 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009300 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009301 // An address with a single base register. Due to the way we
9302 // currently handle addresses it is the same as an 'r' memory constraint.
9303 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009304 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009305 } else if (Constraint.size() == 2) {
9306 switch (Constraint[0]) {
9307 default: break;
9308 // All 'U+' constraints are addresses.
9309 case 'U': return C_Memory;
9310 }
Evan Chenga8e29892007-01-19 07:51:42 +00009311 }
Chris Lattner4234f572007-03-25 02:14:49 +00009312 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009313}
9314
John Thompson44ab89e2010-10-29 17:29:13 +00009315/// Examine constraint type and operand type and determine a weight value.
9316/// This object must already have been set up with the operand type
9317/// and the current alternative constraint selected.
9318TargetLowering::ConstraintWeight
9319ARMTargetLowering::getSingleConstraintMatchWeight(
9320 AsmOperandInfo &info, const char *constraint) const {
9321 ConstraintWeight weight = CW_Invalid;
9322 Value *CallOperandVal = info.CallOperandVal;
9323 // If we don't have a value, we can't do a match,
9324 // but allow it at the lowest weight.
9325 if (CallOperandVal == NULL)
9326 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009327 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009328 // Look at the constraint type.
9329 switch (*constraint) {
9330 default:
9331 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9332 break;
9333 case 'l':
9334 if (type->isIntegerTy()) {
9335 if (Subtarget->isThumb())
9336 weight = CW_SpecificReg;
9337 else
9338 weight = CW_Register;
9339 }
9340 break;
9341 case 'w':
9342 if (type->isFloatingPointTy())
9343 weight = CW_Register;
9344 break;
9345 }
9346 return weight;
9347}
9348
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009349typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9350RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009351ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009352 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009353 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009354 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009355 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009356 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009357 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009358 return RCPair(0U, &ARM::tGPRRegClass);
9359 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009360 case 'h': // High regs or no regs.
9361 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009362 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009363 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009364 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009365 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009366 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009367 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009368 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009369 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009370 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009371 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009372 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009373 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009374 case 'x':
9375 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009376 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009377 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009378 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009379 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009380 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009381 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009382 case 't':
9383 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009384 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009385 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009386 }
9387 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009388 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009389 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009390
Evan Chenga8e29892007-01-19 07:51:42 +00009391 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9392}
9393
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009394/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9395/// vector. If it is invalid, don't add anything to Ops.
9396void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009397 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009398 std::vector<SDValue>&Ops,
9399 SelectionDAG &DAG) const {
9400 SDValue Result(0, 0);
9401
Eric Christopher100c8332011-06-02 23:16:42 +00009402 // Currently only support length 1 constraints.
9403 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009404
Eric Christopher100c8332011-06-02 23:16:42 +00009405 char ConstraintLetter = Constraint[0];
9406 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009407 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009408 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009409 case 'I': case 'J': case 'K': case 'L':
9410 case 'M': case 'N': case 'O':
9411 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9412 if (!C)
9413 return;
9414
9415 int64_t CVal64 = C->getSExtValue();
9416 int CVal = (int) CVal64;
9417 // None of these constraints allow values larger than 32 bits. Check
9418 // that the value fits in an int.
9419 if (CVal != CVal64)
9420 return;
9421
Eric Christopher100c8332011-06-02 23:16:42 +00009422 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009423 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009424 // Constant suitable for movw, must be between 0 and
9425 // 65535.
9426 if (Subtarget->hasV6T2Ops())
9427 if (CVal >= 0 && CVal <= 65535)
9428 break;
9429 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009430 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009431 if (Subtarget->isThumb1Only()) {
9432 // This must be a constant between 0 and 255, for ADD
9433 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009434 if (CVal >= 0 && CVal <= 255)
9435 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009436 } else if (Subtarget->isThumb2()) {
9437 // A constant that can be used as an immediate value in a
9438 // data-processing instruction.
9439 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9440 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009441 } else {
9442 // A constant that can be used as an immediate value in a
9443 // data-processing instruction.
9444 if (ARM_AM::getSOImmVal(CVal) != -1)
9445 break;
9446 }
9447 return;
9448
9449 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009450 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009451 // This must be a constant between -255 and -1, for negated ADD
9452 // immediates. This can be used in GCC with an "n" modifier that
9453 // prints the negated value, for use with SUB instructions. It is
9454 // not useful otherwise but is implemented for compatibility.
9455 if (CVal >= -255 && CVal <= -1)
9456 break;
9457 } else {
9458 // This must be a constant between -4095 and 4095. It is not clear
9459 // what this constraint is intended for. Implemented for
9460 // compatibility with GCC.
9461 if (CVal >= -4095 && CVal <= 4095)
9462 break;
9463 }
9464 return;
9465
9466 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009467 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009468 // A 32-bit value where only one byte has a nonzero value. Exclude
9469 // zero to match GCC. This constraint is used by GCC internally for
9470 // constants that can be loaded with a move/shift combination.
9471 // It is not useful otherwise but is implemented for compatibility.
9472 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9473 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009474 } else if (Subtarget->isThumb2()) {
9475 // A constant whose bitwise inverse can be used as an immediate
9476 // value in a data-processing instruction. This can be used in GCC
9477 // with a "B" modifier that prints the inverted value, for use with
9478 // BIC and MVN instructions. It is not useful otherwise but is
9479 // implemented for compatibility.
9480 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9481 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009482 } else {
9483 // A constant whose bitwise inverse can be used as an immediate
9484 // value in a data-processing instruction. This can be used in GCC
9485 // with a "B" modifier that prints the inverted value, for use with
9486 // BIC and MVN instructions. It is not useful otherwise but is
9487 // implemented for compatibility.
9488 if (ARM_AM::getSOImmVal(~CVal) != -1)
9489 break;
9490 }
9491 return;
9492
9493 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009494 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009495 // This must be a constant between -7 and 7,
9496 // for 3-operand ADD/SUB immediate instructions.
9497 if (CVal >= -7 && CVal < 7)
9498 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009499 } else if (Subtarget->isThumb2()) {
9500 // A constant whose negation can be used as an immediate value in a
9501 // data-processing instruction. This can be used in GCC with an "n"
9502 // modifier that prints the negated value, for use with SUB
9503 // instructions. It is not useful otherwise but is implemented for
9504 // compatibility.
9505 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9506 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009507 } else {
9508 // A constant whose negation can be used as an immediate value in a
9509 // data-processing instruction. This can be used in GCC with an "n"
9510 // modifier that prints the negated value, for use with SUB
9511 // instructions. It is not useful otherwise but is implemented for
9512 // compatibility.
9513 if (ARM_AM::getSOImmVal(-CVal) != -1)
9514 break;
9515 }
9516 return;
9517
9518 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009519 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009520 // This must be a multiple of 4 between 0 and 1020, for
9521 // ADD sp + immediate.
9522 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9523 break;
9524 } else {
9525 // A power of two or a constant between 0 and 32. This is used in
9526 // GCC for the shift amount on shifted register operands, but it is
9527 // useful in general for any shift amounts.
9528 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9529 break;
9530 }
9531 return;
9532
9533 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009534 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009535 // This must be a constant between 0 and 31, for shift amounts.
9536 if (CVal >= 0 && CVal <= 31)
9537 break;
9538 }
9539 return;
9540
9541 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009542 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009543 // This must be a multiple of 4 between -508 and 508, for
9544 // ADD/SUB sp = sp + immediate.
9545 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9546 break;
9547 }
9548 return;
9549 }
9550 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9551 break;
9552 }
9553
9554 if (Result.getNode()) {
9555 Ops.push_back(Result);
9556 return;
9557 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009558 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009559}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009560
9561bool
9562ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9563 // The ARM target isn't yet aware of offsets.
9564 return false;
9565}
Evan Cheng39382422009-10-28 01:44:26 +00009566
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009567bool ARM::isBitFieldInvertedMask(unsigned v) {
9568 if (v == 0xffffffff)
9569 return 0;
9570 // there can be 1's on either or both "outsides", all the "inside"
9571 // bits must be 0's
9572 unsigned int lsb = 0, msb = 31;
9573 while (v & (1 << msb)) --msb;
9574 while (v & (1 << lsb)) ++lsb;
9575 for (unsigned int i = lsb; i <= msb; ++i) {
9576 if (v & (1 << i))
9577 return 0;
9578 }
9579 return 1;
9580}
9581
Evan Cheng39382422009-10-28 01:44:26 +00009582/// isFPImmLegal - Returns true if the target can instruction select the
9583/// specified FP immediate natively. If false, the legalizer will
9584/// materialize the FP immediate as a load from a constant pool.
9585bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9586 if (!Subtarget->hasVFP3())
9587 return false;
9588 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009589 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009590 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009591 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009592 return false;
9593}
Bob Wilson65ffec42010-09-21 17:56:22 +00009594
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009595/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009596/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9597/// specified in the intrinsic calls.
9598bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9599 const CallInst &I,
9600 unsigned Intrinsic) const {
9601 switch (Intrinsic) {
9602 case Intrinsic::arm_neon_vld1:
9603 case Intrinsic::arm_neon_vld2:
9604 case Intrinsic::arm_neon_vld3:
9605 case Intrinsic::arm_neon_vld4:
9606 case Intrinsic::arm_neon_vld2lane:
9607 case Intrinsic::arm_neon_vld3lane:
9608 case Intrinsic::arm_neon_vld4lane: {
9609 Info.opc = ISD::INTRINSIC_W_CHAIN;
9610 // Conservatively set memVT to the entire set of vectors loaded.
9611 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9612 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9613 Info.ptrVal = I.getArgOperand(0);
9614 Info.offset = 0;
9615 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9616 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9617 Info.vol = false; // volatile loads with NEON intrinsics not supported
9618 Info.readMem = true;
9619 Info.writeMem = false;
9620 return true;
9621 }
9622 case Intrinsic::arm_neon_vst1:
9623 case Intrinsic::arm_neon_vst2:
9624 case Intrinsic::arm_neon_vst3:
9625 case Intrinsic::arm_neon_vst4:
9626 case Intrinsic::arm_neon_vst2lane:
9627 case Intrinsic::arm_neon_vst3lane:
9628 case Intrinsic::arm_neon_vst4lane: {
9629 Info.opc = ISD::INTRINSIC_VOID;
9630 // Conservatively set memVT to the entire set of vectors stored.
9631 unsigned NumElts = 0;
9632 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009633 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009634 if (!ArgTy->isVectorTy())
9635 break;
9636 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9637 }
9638 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9639 Info.ptrVal = I.getArgOperand(0);
9640 Info.offset = 0;
9641 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9642 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9643 Info.vol = false; // volatile stores with NEON intrinsics not supported
9644 Info.readMem = false;
9645 Info.writeMem = true;
9646 return true;
9647 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009648 case Intrinsic::arm_strexd: {
9649 Info.opc = ISD::INTRINSIC_W_CHAIN;
9650 Info.memVT = MVT::i64;
9651 Info.ptrVal = I.getArgOperand(2);
9652 Info.offset = 0;
9653 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009654 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009655 Info.readMem = false;
9656 Info.writeMem = true;
9657 return true;
9658 }
9659 case Intrinsic::arm_ldrexd: {
9660 Info.opc = ISD::INTRINSIC_W_CHAIN;
9661 Info.memVT = MVT::i64;
9662 Info.ptrVal = I.getArgOperand(0);
9663 Info.offset = 0;
9664 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009665 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009666 Info.readMem = true;
9667 Info.writeMem = false;
9668 return true;
9669 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009670 default:
9671 break;
9672 }
9673
9674 return false;
9675}