blob: 45287015769d91935b6d40f54fb6971e60d5f0e8 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Craig Topperc1f6f422012-03-17 07:33:42 +000016#include "ARMISelLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000031#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000032#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000033#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000034#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000035#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/MachineBasicBlock.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000043#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000045#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000046#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000047#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000050#include "llvm/Support/raw_ostream.h"
Evan Chenga8e29892007-01-19 07:51:42 +000051using namespace llvm;
52
Dale Johannesen51e28e62010-06-03 21:09:53 +000053STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000054STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren763a75d2012-06-01 02:44:42 +000055STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesen51e28e62010-06-03 21:09:53 +000056
Bob Wilson703af3a2010-08-13 22:43:33 +000057// This option should go away when tail calls fully work.
58static cl::opt<bool>
59EnableARMTailCalls("arm-tail-calls", cl::Hidden,
60 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
61 cl::init(false));
62
Eric Christopher836c6242010-12-15 23:47:29 +000063cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000064EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000065 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000066 cl::init(false));
67
Evan Cheng46df4eb2010-06-16 07:35:02 +000068static cl::opt<bool>
69ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
71 cl::init(true));
72
Benjamin Kramer0861f572011-11-26 23:01:57 +000073namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000074 class ARMCCState : public CCState {
75 public:
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
77 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
78 LLVMContext &C, ParmContext PC)
79 : CCState(CC, isVarArg, MF, TM, locs, C) {
80 assert(((PC == Call) || (PC == Prologue)) &&
81 "ARMCCState users must specify whether their context is call"
82 "or prologue generation.");
83 CallOrPrologue = PC;
84 }
85 };
86}
87
Stuart Hastingsc7315872011-04-20 16:47:52 +000088// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000089static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000090 ARM::R0, ARM::R1, ARM::R2, ARM::R3
91};
92
Owen Andersone50ed302009-08-10 22:56:29 +000093void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
94 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000096 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000097 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
98 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000099
Owen Anderson70671842009-08-10 20:18:46 +0000100 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000101 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000102 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000103 }
104
Owen Andersone50ed302009-08-10 22:56:29 +0000105 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000107 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000108 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000109 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000110 if (ElemTy == MVT::i32) {
111 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
112 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
114 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
115 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000116 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
119 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
120 }
Owen Anderson70671842009-08-10 20:18:46 +0000121 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
122 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000123 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000124 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000125 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
126 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000128 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000129 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
131 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000132 }
133
134 // Promote all bit-wise operations.
135 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000136 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000137 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
138 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000139 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000140 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000141 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000142 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000143 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000144 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000145 }
Bob Wilson16330762009-09-16 00:17:28 +0000146
147 // Neon does not support vector divide/remainder operations.
148 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
149 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
153 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Owen Andersone50ed302009-08-10 22:56:29 +0000156void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000157 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Owen Andersone50ed302009-08-10 22:56:29 +0000161void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000162 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000164}
165
Chris Lattnerf0144122009-07-28 03:13:23 +0000166static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
167 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000168 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000169
Chris Lattner80ec2792009-08-02 00:34:36 +0000170 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000171}
172
Evan Chenga8e29892007-01-19 07:51:42 +0000173ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000174 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000175 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000176 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000177 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000178
Duncan Sands28b77e92011-09-06 19:07:46 +0000179 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
180
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000182 // Uses VFP for Thumb libfuncs if available.
183 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
184 // Single-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
186 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
187 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
188 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000189
Evan Chengb1df8f22007-04-27 08:15:43 +0000190 // Double-precision floating-point arithmetic.
191 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
192 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
193 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
194 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000195
Evan Chengb1df8f22007-04-27 08:15:43 +0000196 // Single-precision comparisons.
197 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
198 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
199 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
200 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
201 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
202 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
203 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
204 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000205
Evan Chengb1df8f22007-04-27 08:15:43 +0000206 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000214
Evan Chengb1df8f22007-04-27 08:15:43 +0000215 // Double-precision comparisons.
216 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
217 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
218 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
219 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
220 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
221 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
222 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
223 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000224
Evan Chengb1df8f22007-04-27 08:15:43 +0000225 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
232 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000233
Evan Chengb1df8f22007-04-27 08:15:43 +0000234 // Floating-point to integer conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
237 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
238 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
239 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
240 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000241
Evan Chengb1df8f22007-04-27 08:15:43 +0000242 // Conversions between floating types.
243 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
244 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
245
246 // Integer to floating-point conversions.
247 // i64 conversions are done via library routines even when generating VFP
248 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000249 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
250 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000251 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
252 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
253 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
254 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
255 }
Evan Chenga8e29892007-01-19 07:51:42 +0000256 }
257
Bob Wilson2f954612009-05-22 17:38:41 +0000258 // These libcalls are not available in 32-bit.
259 setLibcallName(RTLIB::SHL_I128, 0);
260 setLibcallName(RTLIB::SRL_I128, 0);
261 setLibcallName(RTLIB::SRA_I128, 0);
262
Evan Cheng07043272012-02-21 20:46:00 +0000263 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000265 // RTABI chapter 4.1.2, Table 2
266 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
267 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
268 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
269 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
270 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
273 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
274
275 // Double-precision floating-point comparison helper functions
276 // RTABI chapter 4.1.2, Table 3
277 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
278 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
279 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
280 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
281 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
282 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
284 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
285 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
286 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
287 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
288 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
289 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
290 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
291 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
292 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
293 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
300 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
301
302 // Single-precision floating-point arithmetic helper functions
303 // RTABI chapter 4.1.2, Table 4
304 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
305 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
306 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
307 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
308 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
311 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
312
313 // Single-precision floating-point comparison helper functions
314 // RTABI chapter 4.1.2, Table 5
315 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
316 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
317 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
318 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
319 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
320 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
322 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
323 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
324 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
325 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
326 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
327 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
328 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
329 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
330 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
331 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
338 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
339
340 // Floating-point to integer conversions.
341 // RTABI chapter 4.1.2, Table 6
342 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
343 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
345 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
346 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
347 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
348 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
349 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
350 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
357 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
358
359 // Conversions between floating types.
360 // RTABI chapter 4.1.2, Table 7
361 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
362 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
363 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000364 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000365
366 // Integer to floating-point conversions.
367 // RTABI chapter 4.1.2, Table 8
368 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
369 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
370 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
371 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
372 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
373 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
374 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
375 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
384
385 // Long long helper functions
386 // RTABI chapter 4.2, Table 9
387 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000388 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
389 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
390 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
391 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
397
398 // Integer division functions
399 // RTABI chapter 4.3.1
400 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
401 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
402 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000403 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000404 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
405 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
406 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000407 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000408 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000411 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000412 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
413 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000414 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000415 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000416
417 // Memory operations
418 // RTABI chapter 4.3.4
419 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
420 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
421 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000422 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
423 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000425 }
426
Bob Wilson2fef4572011-10-07 16:59:21 +0000427 // Use divmod compiler-rt calls for iOS 5.0 and later.
428 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
429 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
430 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
431 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
432 }
433
David Goodwinf1daf7d2009-07-08 23:10:31 +0000434 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000435 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000436 else
Craig Topper420761a2012-04-20 07:30:17 +0000437 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000438 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
439 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000440 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000441 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000442 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000443
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000445 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000446
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000447 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
448 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
449 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
450 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
451 setTruncStoreAction((MVT::SimpleValueType)VT,
452 (MVT::SimpleValueType)InnerVT, Expand);
453 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
454 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
456 }
457
Lang Hames45b5f882012-03-15 18:49:02 +0000458 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
459
Bob Wilson5bafff32009-06-22 23:27:02 +0000460 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 addDRTypeForNEON(MVT::v2f32);
462 addDRTypeForNEON(MVT::v8i8);
463 addDRTypeForNEON(MVT::v4i16);
464 addDRTypeForNEON(MVT::v2i32);
465 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000466
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 addQRTypeForNEON(MVT::v4f32);
468 addQRTypeForNEON(MVT::v2f64);
469 addQRTypeForNEON(MVT::v16i8);
470 addQRTypeForNEON(MVT::v8i16);
471 addQRTypeForNEON(MVT::v4i32);
472 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000473
Bob Wilson74dc72e2009-09-15 23:55:57 +0000474 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
475 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
477 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
479 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
480 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000481 // FIXME: Code duplication: FDIV and FREM are expanded always, see
482 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
484 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000485 // FIXME: Create unittest.
486 // In another words, find a way when "copysign" appears in DAG with vector
487 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000489 // FIXME: Code duplication: SETCC has custom operation action, see
490 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000491 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000492 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000493 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
494 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
496 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
497 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
499 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
504 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000505 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000506 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
507 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
508 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
510 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000511
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000512 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
513 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
514 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
515 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
516 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
519 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
520 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
521 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000522
Bob Wilson642b3292009-09-16 00:32:15 +0000523 // Neon does not support some operations on v1i64 and v2i64 types.
524 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000525 // Custom handling for some quad-vector types to detect VMULL.
526 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
527 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
528 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000529 // Custom handling for some vector types to avoid expensive expansions
530 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
531 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
532 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
533 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000534 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
535 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000536 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000537 // a destination type that is wider than the source, and nor does
538 // it have a FP_TO_[SU]INT instruction with a narrower destination than
539 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000540 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
541 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000542 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
543 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000544
Bob Wilson1c3ef902011-02-07 17:43:21 +0000545 setTargetDAGCombine(ISD::INTRINSIC_VOID);
546 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000547 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
548 setTargetDAGCombine(ISD::SHL);
549 setTargetDAGCombine(ISD::SRL);
550 setTargetDAGCombine(ISD::SRA);
551 setTargetDAGCombine(ISD::SIGN_EXTEND);
552 setTargetDAGCombine(ISD::ZERO_EXTEND);
553 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000554 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000555 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000556 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000557 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
558 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000559 setTargetDAGCombine(ISD::FP_TO_SINT);
560 setTargetDAGCombine(ISD::FP_TO_UINT);
561 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000562
James Molloy873fd5f2012-02-20 09:24:05 +0000563 // It is legal to extload from v4i8 to v4i16 or v4i32.
564 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
565 MVT::v4i16, MVT::v2i16,
566 MVT::v2i32};
567 for (unsigned i = 0; i < 6; ++i) {
568 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
569 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
570 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
571 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000572 }
573
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000574 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000575
576 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000578
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000579 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000583 if (!Subtarget->isThumb1Only()) {
584 for (unsigned im = (unsigned)ISD::PRE_INC;
585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setIndexedLoadAction(im, MVT::i1, Legal);
587 setIndexedLoadAction(im, MVT::i8, Legal);
588 setIndexedLoadAction(im, MVT::i16, Legal);
589 setIndexedLoadAction(im, MVT::i32, Legal);
590 setIndexedStoreAction(im, MVT::i1, Legal);
591 setIndexedStoreAction(im, MVT::i8, Legal);
592 setIndexedStoreAction(im, MVT::i16, Legal);
593 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595 }
596
597 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000598 setOperationAction(ISD::MUL, MVT::i64, Expand);
599 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000600 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000606 setOperationAction(ISD::MULHS, MVT::i32, Expand);
607
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SRL, MVT::i64, Custom);
612 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Evan Cheng342e3162011-08-30 01:34:54 +0000614 if (!Subtarget->isThumb1Only()) {
615 // FIXME: We should do this for Thumb1 as well.
616 setOperationAction(ISD::ADDC, MVT::i32, Custom);
617 setOperationAction(ISD::ADDE, MVT::i32, Custom);
618 setOperationAction(ISD::SUBC, MVT::i32, Custom);
619 setOperationAction(ISD::SUBE, MVT::i32, Custom);
620 }
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000624 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Chandler Carruth63974b22011-12-13 01:56:10 +0000629 // These just redirect to CTTZ and CTLZ on ARM.
630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
632
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000633 // Only ARMv6 has BSWAP.
634 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000639 // v7M has a hardware divider
640 setOperationAction(ISD::SDIV, MVT::i32, Expand);
641 setOperationAction(ISD::UDIV, MVT::i32, Expand);
642 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::UREM, MVT::i32, Expand);
645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000654 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000655
Evan Chenga8e29892007-01-19 07:51:42 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VASTART, MVT::Other, Custom);
658 setOperationAction(ISD::VAARG, MVT::Other, Expand);
659 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
660 setOperationAction(ISD::VAEND, MVT::Other, Expand);
661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000663
664 if (!Subtarget->isTargetDarwin()) {
665 // Non-Darwin platforms may return values in these registers via the
666 // personality function.
667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
669 setExceptionPointerRegister(ARM::R0);
670 setExceptionSelectorRegister(ARM::R1);
671 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000672
Evan Cheng3a1588a2010-04-15 22:20:34 +0000673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
675 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000676 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000677 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 // membarrier needs custom lowering; the rest are legal and handled
680 // normally.
681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000683 // Custom lowering for 64-bit ops
684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
692 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 } else {
694 // Set them all for expansion, which will force libcalls.
695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
710 // Unordered/Monotonic case.
711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000713 // Since the libcalls include locking, fold in the fences
714 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 }
Evan Chenga8e29892007-01-19 07:51:42 +0000716
Evan Cheng416941d2010-11-04 05:19:35 +0000717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000718
Eli Friedmana2c6f452010-06-26 04:36:50 +0000719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
720 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000723 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
727 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
729 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
732 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000733
734 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000736 if (Subtarget->isTargetDarwin()) {
737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000740 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SETCC, MVT::i32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000745 setOperationAction(ISD::SELECT, MVT::i32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
753 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
756 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000757
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000758 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN, MVT::f64, Expand);
760 setOperationAction(ISD::FSIN, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000769 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::FPOW, MVT::f64, Expand);
771 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000773 if (!Subtarget->hasVFP4()) {
774 setOperationAction(ISD::FMA, MVT::f64, Expand);
775 setOperationAction(ISD::FMA, MVT::f32, Expand);
776 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000777
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000778 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000779 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000780 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
781 if (Subtarget->hasVFP2()) {
782 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
783 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
784 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
785 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
786 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000787 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000788 if (!Subtarget->hasFP16()) {
789 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
790 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000791 }
Evan Cheng110cf482008-04-01 01:50:16 +0000792 }
Evan Chenga8e29892007-01-19 07:51:42 +0000793
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000794 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000795 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000796 setTargetDAGCombine(ISD::ADD);
797 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000798 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000799
Evan Chengc892aeb2012-02-23 01:19:06 +0000800 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000801 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000802 setTargetDAGCombine(ISD::OR);
803 setTargetDAGCombine(ISD::XOR);
804 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000805
Evan Cheng5fb468a2012-02-23 02:58:19 +0000806 if (Subtarget->hasV6Ops())
807 setTargetDAGCombine(ISD::SRL);
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000810
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000811 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
812 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000813 setSchedulingPreference(Sched::RegPressure);
814 else
815 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000816
Evan Cheng05219282011-01-06 06:52:41 +0000817 //// temporary - rewrite interface to use type
818 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000819 maxStoresPerMemset = 16;
820 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000821
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000822 // On ARM arguments smaller than 4 bytes are extended, so all arguments
823 // are at least 4 bytes aligned.
824 setMinStackArgumentAlignment(4);
825
Evan Chengfff606d2010-09-24 19:07:23 +0000826 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000827
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000828 // Prefer likely predicted branches to selects on out-of-order cores.
829 predictableSelectIsExpensive = Subtarget->isCortexA9();
830
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000831 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000832}
833
Andrew Trick32cec0a2011-01-19 02:35:27 +0000834// FIXME: It might make sense to define the representative register class as the
835// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
836// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
837// SPR's representative would be DPR_VFP2. This should work well if register
838// pressure tracking were modified such that a register use would increment the
839// pressure of the register class's representative and all of it's super
840// classes' representatives transitively. We have not implemented this because
841// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000842// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000843// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844std::pair<const TargetRegisterClass*, uint8_t>
845ARMTargetLowering::findRepresentativeClass(EVT VT) const{
846 const TargetRegisterClass *RRC = 0;
847 uint8_t Cost = 1;
848 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000849 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000851 // Use DPR as representative register class for all floating point
852 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
853 // the cost is 1 for both f32 and f64.
854 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000855 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000856 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000857 // When NEON is used for SP, only half of the register file is available
858 // because operations that define both SP and DP results will be constrained
859 // to the VFP2 class (D0-D15). We currently model this constraint prior to
860 // coalescing by double-counting the SP regs. See the FIXME above.
861 if (Subtarget->useNEONForSinglePrecisionFP())
862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
865 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000867 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000870 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000871 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 break;
873 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000874 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000875 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000876 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000877 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000879}
880
Evan Chenga8e29892007-01-19 07:51:42 +0000881const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
882 switch (Opcode) {
883 default: return 0;
884 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000885 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000886 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000887 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
888 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000889 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
891 case ARMISD::tCALL: return "ARMISD::tCALL";
892 case ARMISD::BRCOND: return "ARMISD::BRCOND";
893 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000894 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
896 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
897 case ARMISD::CMP: return "ARMISD::CMP";
David Goodwinc0309b42009-06-29 15:33:01 +0000898 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000899 case ARMISD::CMPFP: return "ARMISD::CMPFP";
900 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000901 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000902 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000903
Evan Chenga8e29892007-01-19 07:51:42 +0000904 case ARMISD::CMOV: return "ARMISD::CMOV";
Evan Chengc892aeb2012-02-23 01:19:06 +0000905 case ARMISD::CAND: return "ARMISD::CAND";
906 case ARMISD::COR: return "ARMISD::COR";
907 case ARMISD::CXOR: return "ARMISD::CXOR";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000908
Jim Grosbach3482c802010-01-18 19:58:49 +0000909 case ARMISD::RBIT: return "ARMISD::RBIT";
910
Bob Wilson76a312b2010-03-19 22:51:32 +0000911 case ARMISD::FTOSI: return "ARMISD::FTOSI";
912 case ARMISD::FTOUI: return "ARMISD::FTOUI";
913 case ARMISD::SITOF: return "ARMISD::SITOF";
914 case ARMISD::UITOF: return "ARMISD::UITOF";
915
Evan Chenga8e29892007-01-19 07:51:42 +0000916 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
917 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
918 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000919
Evan Cheng342e3162011-08-30 01:34:54 +0000920 case ARMISD::ADDC: return "ARMISD::ADDC";
921 case ARMISD::ADDE: return "ARMISD::ADDE";
922 case ARMISD::SUBC: return "ARMISD::SUBC";
923 case ARMISD::SUBE: return "ARMISD::SUBE";
924
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000925 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
926 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000927
Evan Chengc5942082009-10-28 06:55:03 +0000928 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
929 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
930
Dale Johannesen51e28e62010-06-03 21:09:53 +0000931 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000932
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000933 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000934
Evan Cheng86198642009-08-07 00:34:42 +0000935 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
936
Jim Grosbach3728e962009-12-10 00:11:09 +0000937 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000938 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000939
Evan Chengdfed19f2010-11-03 06:34:55 +0000940 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
941
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000943 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000944 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000945 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
946 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000947 case ARMISD::VCGEU: return "ARMISD::VCGEU";
948 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000949 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
950 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000951 case ARMISD::VCGTU: return "ARMISD::VCGTU";
952 case ARMISD::VTST: return "ARMISD::VTST";
953
954 case ARMISD::VSHL: return "ARMISD::VSHL";
955 case ARMISD::VSHRs: return "ARMISD::VSHRs";
956 case ARMISD::VSHRu: return "ARMISD::VSHRu";
957 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
958 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
959 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
960 case ARMISD::VSHRN: return "ARMISD::VSHRN";
961 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
962 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
963 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
964 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
965 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
966 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
967 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
968 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
969 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
970 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
971 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
972 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
973 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
974 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000975 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000976 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000977 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000978 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000979 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000980 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000981 case ARMISD::VREV64: return "ARMISD::VREV64";
982 case ARMISD::VREV32: return "ARMISD::VREV32";
983 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000984 case ARMISD::VZIP: return "ARMISD::VZIP";
985 case ARMISD::VUZP: return "ARMISD::VUZP";
986 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000987 case ARMISD::VTBL1: return "ARMISD::VTBL1";
988 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000989 case ARMISD::VMULLs: return "ARMISD::VMULLs";
990 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000991 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000992 case ARMISD::FMAX: return "ARMISD::FMAX";
993 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000994 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000995 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
996 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000997 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000998 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
999 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1000 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001001 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1002 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1003 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1004 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1005 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1006 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1007 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1008 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1009 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1010 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1011 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1012 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1013 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1014 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1015 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1016 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1017 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001018 }
1019}
1020
Duncan Sands28b77e92011-09-06 19:07:46 +00001021EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1022 if (!VT.isVector()) return getPointerTy();
1023 return VT.changeVectorElementTypeToInteger();
1024}
1025
Evan Cheng06b666c2010-05-15 02:18:07 +00001026/// getRegClassFor - Return the register class that should be used for the
1027/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001028const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001029 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1030 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1031 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001032 if (Subtarget->hasNEON()) {
1033 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001034 return &ARM::QQPRRegClass;
1035 if (VT == MVT::v8i64)
1036 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001037 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001038 return TargetLowering::getRegClassFor(VT);
1039}
1040
Eric Christopherab695882010-07-21 22:26:11 +00001041// Create a fast isel object.
1042FastISel *
1043ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
1044 return ARM::createFastISel(funcInfo);
1045}
1046
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001047/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1048/// be used for loads / stores from the global.
1049unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1050 return (Subtarget->isThumb1Only() ? 127 : 4095);
1051}
1052
Evan Cheng1cc39842010-05-20 23:26:43 +00001053Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001054 unsigned NumVals = N->getNumValues();
1055 if (!NumVals)
1056 return Sched::RegPressure;
1057
1058 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001059 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001060 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001061 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001062 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001063 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001064 }
Evan Chengc10f5432010-05-28 23:25:23 +00001065
1066 if (!N->isMachineOpcode())
1067 return Sched::RegPressure;
1068
1069 // Load are scheduled for latency even if there instruction itinerary
1070 // is not available.
1071 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001072 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001073
Evan Chenge837dea2011-06-28 19:10:37 +00001074 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001075 return Sched::RegPressure;
1076 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001077 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001078 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001079
Evan Cheng1cc39842010-05-20 23:26:43 +00001080 return Sched::RegPressure;
1081}
1082
Evan Chenga8e29892007-01-19 07:51:42 +00001083//===----------------------------------------------------------------------===//
1084// Lowering Code
1085//===----------------------------------------------------------------------===//
1086
Evan Chenga8e29892007-01-19 07:51:42 +00001087/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1088static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1089 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001090 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001091 case ISD::SETNE: return ARMCC::NE;
1092 case ISD::SETEQ: return ARMCC::EQ;
1093 case ISD::SETGT: return ARMCC::GT;
1094 case ISD::SETGE: return ARMCC::GE;
1095 case ISD::SETLT: return ARMCC::LT;
1096 case ISD::SETLE: return ARMCC::LE;
1097 case ISD::SETUGT: return ARMCC::HI;
1098 case ISD::SETUGE: return ARMCC::HS;
1099 case ISD::SETULT: return ARMCC::LO;
1100 case ISD::SETULE: return ARMCC::LS;
1101 }
1102}
1103
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001104/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1105static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001106 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001107 CondCode2 = ARMCC::AL;
1108 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001109 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001110 case ISD::SETEQ:
1111 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1112 case ISD::SETGT:
1113 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1114 case ISD::SETGE:
1115 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1116 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001117 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001118 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1119 case ISD::SETO: CondCode = ARMCC::VC; break;
1120 case ISD::SETUO: CondCode = ARMCC::VS; break;
1121 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1122 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1123 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1124 case ISD::SETLT:
1125 case ISD::SETULT: CondCode = ARMCC::LT; break;
1126 case ISD::SETLE:
1127 case ISD::SETULE: CondCode = ARMCC::LE; break;
1128 case ISD::SETNE:
1129 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1130 }
Evan Chenga8e29892007-01-19 07:51:42 +00001131}
1132
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133//===----------------------------------------------------------------------===//
1134// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001135//===----------------------------------------------------------------------===//
1136
1137#include "ARMGenCallingConv.inc"
1138
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001139/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1140/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001141CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001142 bool Return,
1143 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001144 switch (CC) {
1145 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001146 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001147 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001148 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001149 if (!Subtarget->isAAPCS_ABI())
1150 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1151 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1152 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1153 }
1154 // Fallthrough
1155 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001156 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001157 if (!Subtarget->isAAPCS_ABI())
1158 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1159 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001160 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1161 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001162 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1163 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1164 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001165 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001166 if (!isVarArg)
1167 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1168 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001170 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001171 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001172 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001173 }
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176/// LowerCallResult - Lower the result values of a call into the
1177/// appropriate copies out of appropriate physical registers.
1178SDValue
1179ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001180 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 const SmallVectorImpl<ISD::InputArg> &Ins,
1182 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001183 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 // Assign locations to each value returned by this call.
1186 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001187 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1188 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001190 CCAssignFnForNode(CallConv, /* Return*/ true,
1191 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192
1193 // Copy all of the result registers out of their specified physreg.
1194 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1195 CCValAssign VA = RVLocs[i];
1196
Bob Wilson80915242009-04-25 00:33:20 +00001197 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001199 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001202 Chain = Lo.getValue(1);
1203 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001206 InFlag);
1207 Chain = Hi.getValue(1);
1208 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001209 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001210
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 if (VA.getLocVT() == MVT::v2f64) {
1212 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1213 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1214 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001215
1216 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001218 Chain = Lo.getValue(1);
1219 InFlag = Lo.getValue(2);
1220 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001221 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001222 Chain = Hi.getValue(1);
1223 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001224 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1226 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001227 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001229 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1230 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001231 Chain = Val.getValue(1);
1232 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 }
Bob Wilson80915242009-04-25 00:33:20 +00001234
1235 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001236 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001237 case CCValAssign::Full: break;
1238 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001239 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001240 break;
1241 }
1242
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244 }
1245
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247}
1248
Bob Wilsondee46d72009-04-17 20:35:10 +00001249/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1252 SDValue StackPtr, SDValue Arg,
1253 DebugLoc dl, SelectionDAG &DAG,
1254 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001255 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 unsigned LocMemOffset = VA.getLocMemOffset();
1257 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1258 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001260 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001261 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001262}
1263
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001265 SDValue Chain, SDValue &Arg,
1266 RegsToPassVector &RegsToPass,
1267 CCValAssign &VA, CCValAssign &NextVA,
1268 SDValue &StackPtr,
1269 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001270 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001271
Jim Grosbache5165492009-11-09 00:11:35 +00001272 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1275
1276 if (NextVA.isRegLoc())
1277 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1278 else {
1279 assert(NextVA.isMemLoc());
1280 if (StackPtr.getNode() == 0)
1281 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1284 dl, DAG, NextVA,
1285 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001286 }
1287}
1288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001290/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1291/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001293ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001294 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001295 SelectionDAG &DAG = CLI.DAG;
1296 DebugLoc &dl = CLI.DL;
1297 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1298 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1299 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1300 SDValue Chain = CLI.Chain;
1301 SDValue Callee = CLI.Callee;
1302 bool &isTailCall = CLI.IsTailCall;
1303 CallingConv::ID CallConv = CLI.CallConv;
1304 bool doesNotRet = CLI.DoesNotReturn;
1305 bool isVarArg = CLI.IsVarArg;
1306
Dale Johannesen51e28e62010-06-03 21:09:53 +00001307 MachineFunction &MF = DAG.getMachineFunction();
1308 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1309 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001310 // Disable tail calls if they're not supported.
1311 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001312 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001313 if (isTailCall) {
1314 // Check if it's really possible to do a tail call.
1315 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1316 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001317 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001318 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1319 // detected sibcalls.
1320 if (isTailCall) {
1321 ++NumTailCalls;
1322 IsSibCall = true;
1323 }
1324 }
Evan Chenga8e29892007-01-19 07:51:42 +00001325
Bob Wilson1f595bb2009-04-17 19:07:39 +00001326 // Analyze operands of the call, assigning locations to each operand.
1327 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001328 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1329 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001330 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001331 CCAssignFnForNode(CallConv, /* Return*/ false,
1332 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001333
Bob Wilson1f595bb2009-04-17 19:07:39 +00001334 // Get a count of how many bytes are to be pushed on the stack.
1335 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001336
Dale Johannesen51e28e62010-06-03 21:09:53 +00001337 // For tail calls, memory operands are available in our caller's stack.
1338 if (IsSibCall)
1339 NumBytes = 0;
1340
Evan Chenga8e29892007-01-19 07:51:42 +00001341 // Adjust the stack pointer for the new arguments...
1342 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 if (!IsSibCall)
1344 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001345
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001346 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001347
Bob Wilson5bafff32009-06-22 23:27:02 +00001348 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001350
Bob Wilson1f595bb2009-04-17 19:07:39 +00001351 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001352 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1354 i != e;
1355 ++i, ++realArgIdx) {
1356 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001357 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001359 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001360
Bob Wilson1f595bb2009-04-17 19:07:39 +00001361 // Promote the value if needed.
1362 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001363 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 case CCValAssign::Full: break;
1365 case CCValAssign::SExt:
1366 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1367 break;
1368 case CCValAssign::ZExt:
1369 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1370 break;
1371 case CCValAssign::AExt:
1372 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1373 break;
1374 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001375 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001377 }
1378
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001379 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001380 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001381 if (VA.getLocVT() == MVT::v2f64) {
1382 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1383 DAG.getConstant(0, MVT::i32));
1384 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1385 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001386
Dan Gohman98ca4f22009-08-05 01:29:28 +00001387 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001388 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1389
1390 VA = ArgLocs[++i]; // skip ahead to next loc
1391 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001393 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1394 } else {
1395 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001396
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1398 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001399 }
1400 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001402 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001403 }
1404 } else if (VA.isRegLoc()) {
1405 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001406 } else if (isByVal) {
1407 assert(VA.isMemLoc());
1408 unsigned offset = 0;
1409
1410 // True if this byval aggregate will be split between registers
1411 // and memory.
1412 if (CCInfo.isFirstByValRegValid()) {
1413 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1414 unsigned int i, j;
1415 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1416 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1417 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1418 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1419 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001420 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001421 MemOpChains.push_back(Load.getValue(1));
1422 RegsToPass.push_back(std::make_pair(j, Load));
1423 }
1424 offset = ARM::R4 - CCInfo.getFirstByValReg();
1425 CCInfo.clearFirstByValReg();
1426 }
1427
Manman Ren763a75d2012-06-01 02:44:42 +00001428 if (Flags.getByValSize() - 4*offset > 0) {
1429 unsigned LocMemOffset = VA.getLocMemOffset();
1430 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1431 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1432 StkPtrOff);
1433 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1434 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1435 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1436 MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001437
Manman Ren763a75d2012-06-01 02:44:42 +00001438 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
1439 SDValue Ops[] = { Chain, Dst, Src, SizeNode};
1440 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1441 Ops, array_lengthof(Ops)));
1442 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001443 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001444 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001445
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1447 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001448 }
Evan Chenga8e29892007-01-19 07:51:42 +00001449 }
1450
1451 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001453 &MemOpChains[0], MemOpChains.size());
1454
1455 // Build a sequence of copy-to-reg nodes chained together with token chain
1456 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001458 // Tail call byval lowering might overwrite argument registers so in case of
1459 // tail call optimization the copies to registers are lowered later.
1460 if (!isTailCall)
1461 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1462 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1463 RegsToPass[i].second, InFlag);
1464 InFlag = Chain.getValue(1);
1465 }
Evan Chenga8e29892007-01-19 07:51:42 +00001466
Dale Johannesen51e28e62010-06-03 21:09:53 +00001467 // For tail calls lower the arguments to the 'real' stack slot.
1468 if (isTailCall) {
1469 // Force all the incoming stack arguments to be loaded from the stack
1470 // before any new outgoing arguments are stored to the stack, because the
1471 // outgoing stack slots may alias the incoming argument stack slots, and
1472 // the alias isn't otherwise explicit. This is slightly more conservative
1473 // than necessary, because it means that each store effectively depends
1474 // on every argument instead of just those arguments it would clobber.
1475
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001476 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001477 InFlag = SDValue();
1478 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1479 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1480 RegsToPass[i].second, InFlag);
1481 InFlag = Chain.getValue(1);
1482 }
1483 InFlag =SDValue();
1484 }
1485
Bill Wendling056292f2008-09-16 21:48:12 +00001486 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1487 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1488 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001489 bool isDirect = false;
1490 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001491 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001492 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001493
1494 if (EnableARMLongCalls) {
1495 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1496 && "long-calls with non-static relocation model!");
1497 // Handle a global address or an external symbol. If it's not one of
1498 // those, the target's already in a register, so we don't need to do
1499 // anything extra.
1500 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001501 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001502 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001503 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001504 ARMConstantPoolValue *CPV =
1505 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1506
Jim Grosbache7b52522010-04-14 22:28:31 +00001507 // Get the address of the callee into a register
1508 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1509 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1510 Callee = DAG.getLoad(getPointerTy(), dl,
1511 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001512 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001513 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001514 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1515 const char *Sym = S->getSymbol();
1516
1517 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001518 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001519 ARMConstantPoolValue *CPV =
1520 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1521 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001522 // Get the address of the callee into a register
1523 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1524 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1525 Callee = DAG.getLoad(getPointerTy(), dl,
1526 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001527 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001528 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001529 }
1530 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001531 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001532 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001533 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001534 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001535 getTargetMachine().getRelocationModel() != Reloc::Static;
1536 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001537 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001538 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001539 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001540 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001541 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001542 ARMConstantPoolValue *CPV =
1543 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001544 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001546 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001547 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001548 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001549 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001550 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001551 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001552 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001553 } else {
1554 // On ELF targets for PIC code, direct calls should go through the PLT
1555 unsigned OpFlags = 0;
1556 if (Subtarget->isTargetELF() &&
1557 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1558 OpFlags = ARMII::MO_PLT;
1559 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1560 }
Bill Wendling056292f2008-09-16 21:48:12 +00001561 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001562 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001563 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001564 getTargetMachine().getRelocationModel() != Reloc::Static;
1565 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001566 // tBX takes a register source operand.
1567 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001568 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001569 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001570 ARMConstantPoolValue *CPV =
1571 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1572 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001573 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001575 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001576 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001577 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001578 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001579 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001580 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001581 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001582 } else {
1583 unsigned OpFlags = 0;
1584 // On ELF targets for PIC code, direct calls should go through the PLT
1585 if (Subtarget->isTargetELF() &&
1586 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1587 OpFlags = ARMII::MO_PLT;
1588 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1589 }
Evan Chenga8e29892007-01-19 07:51:42 +00001590 }
1591
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001592 // FIXME: handle tail calls differently.
1593 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001594 if (Subtarget->isThumb()) {
1595 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001596 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001597 else if (doesNotRet && isDirect && !isARMFunc &&
1598 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1599 // "mov lr, pc; b _foo" to avoid confusing the RSP
1600 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001601 else
1602 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1603 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001604 if (!isDirect && !Subtarget->hasV5TOps()) {
1605 CallOpc = ARMISD::CALL_NOLINK;
1606 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1607 // "mov lr, pc; b _foo" to avoid confusing the RSP
1608 CallOpc = ARMISD::CALL_NOLINK;
1609 else
1610 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001611 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001612
Dan Gohman475871a2008-07-27 21:46:04 +00001613 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001614 Ops.push_back(Chain);
1615 Ops.push_back(Callee);
1616
1617 // Add argument registers to the end of the list so that they are known live
1618 // into the call.
1619 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1620 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1621 RegsToPass[i].second.getValueType()));
1622
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001623 // Add a register mask operand representing the call-preserved registers.
1624 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1625 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1626 assert(Mask && "Missing call preserved mask for calling convention");
1627 Ops.push_back(DAG.getRegisterMask(Mask));
1628
Gabor Greifba36cb52008-08-28 21:40:38 +00001629 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001630 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001631
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001632 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001633 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001634 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635
Duncan Sands4bdcb612008-07-02 17:40:58 +00001636 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001637 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001638 InFlag = Chain.getValue(1);
1639
Chris Lattnere563bbc2008-10-11 22:08:30 +00001640 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1641 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001642 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001643 InFlag = Chain.getValue(1);
1644
Bob Wilson1f595bb2009-04-17 19:07:39 +00001645 // Handle result values, copying them out of physregs into vregs that we
1646 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1648 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001649}
1650
Stuart Hastingsf222e592011-02-28 17:17:53 +00001651/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001652/// on the stack. Remember the next parameter register to allocate,
1653/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001654/// this.
1655void
Craig Topperc89c7442012-03-27 07:21:54 +00001656ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001657 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1658 assert((State->getCallOrPrologue() == Prologue ||
1659 State->getCallOrPrologue() == Call) &&
1660 "unhandled ParmContext");
1661 if ((!State->isFirstByValRegValid()) &&
1662 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1663 State->setFirstByValReg(reg);
1664 // At a call site, a byval parameter that is split between
1665 // registers and memory needs its size truncated here. In a
1666 // function prologue, such byval parameters are reassembled in
1667 // memory, and are not truncated.
1668 if (State->getCallOrPrologue() == Call) {
1669 unsigned excess = 4 * (ARM::R4 - reg);
1670 assert(size >= excess && "expected larger existing stack allocation");
1671 size -= excess;
1672 }
1673 }
1674 // Confiscate any remaining parameter registers to preclude their
1675 // assignment to subsequent parameters.
1676 while (State->AllocateReg(GPRArgRegs, 4))
1677 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001678}
1679
Dale Johannesen51e28e62010-06-03 21:09:53 +00001680/// MatchingStackOffset - Return true if the given stack call argument is
1681/// already available in the same position (relatively) of the caller's
1682/// incoming argument stack.
1683static
1684bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1685 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001686 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1688 int FI = INT_MAX;
1689 if (Arg.getOpcode() == ISD::CopyFromReg) {
1690 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001691 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692 return false;
1693 MachineInstr *Def = MRI->getVRegDef(VR);
1694 if (!Def)
1695 return false;
1696 if (!Flags.isByVal()) {
1697 if (!TII->isLoadFromStackSlot(Def, FI))
1698 return false;
1699 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001700 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001701 }
1702 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1703 if (Flags.isByVal())
1704 // ByVal argument is passed in as a pointer but it's now being
1705 // dereferenced. e.g.
1706 // define @foo(%struct.X* %A) {
1707 // tail call @bar(%struct.X* byval %A)
1708 // }
1709 return false;
1710 SDValue Ptr = Ld->getBasePtr();
1711 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1712 if (!FINode)
1713 return false;
1714 FI = FINode->getIndex();
1715 } else
1716 return false;
1717
1718 assert(FI != INT_MAX);
1719 if (!MFI->isFixedObjectIndex(FI))
1720 return false;
1721 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1722}
1723
1724/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1725/// for tail call optimization. Targets which want to do tail call
1726/// optimization should implement this function.
1727bool
1728ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1729 CallingConv::ID CalleeCC,
1730 bool isVarArg,
1731 bool isCalleeStructRet,
1732 bool isCallerStructRet,
1733 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001734 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001735 const SmallVectorImpl<ISD::InputArg> &Ins,
1736 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001737 const Function *CallerF = DAG.getMachineFunction().getFunction();
1738 CallingConv::ID CallerCC = CallerF->getCallingConv();
1739 bool CCMatch = CallerCC == CalleeCC;
1740
1741 // Look for obvious safe cases to perform tail call optimization that do not
1742 // require ABI changes. This is what gcc calls sibcall.
1743
Jim Grosbach7616b642010-06-16 23:45:49 +00001744 // Do not sibcall optimize vararg calls unless the call site is not passing
1745 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001746 if (isVarArg && !Outs.empty())
1747 return false;
1748
1749 // Also avoid sibcall optimization if either caller or callee uses struct
1750 // return semantics.
1751 if (isCalleeStructRet || isCallerStructRet)
1752 return false;
1753
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001754 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001755 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1756 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1757 // support in the assembler and linker to be used. This would need to be
1758 // fixed to fully support tail calls in Thumb1.
1759 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001760 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1761 // LR. This means if we need to reload LR, it takes an extra instructions,
1762 // which outweighs the value of the tail call; but here we don't know yet
1763 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001764 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001765 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001766
1767 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1768 // but we need to make sure there are enough registers; the only valid
1769 // registers are the 4 used for parameters. We don't currently do this
1770 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001771 if (Subtarget->isThumb1Only())
1772 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001773
Dale Johannesen51e28e62010-06-03 21:09:53 +00001774 // If the calling conventions do not match, then we'd better make sure the
1775 // results are returned in the same way as what the caller expects.
1776 if (!CCMatch) {
1777 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001778 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1779 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001780 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1781
1782 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001783 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1784 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001785 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1786
1787 if (RVLocs1.size() != RVLocs2.size())
1788 return false;
1789 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1790 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1791 return false;
1792 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1793 return false;
1794 if (RVLocs1[i].isRegLoc()) {
1795 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1796 return false;
1797 } else {
1798 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1799 return false;
1800 }
1801 }
1802 }
1803
1804 // If the callee takes no arguments then go on to check the results of the
1805 // call.
1806 if (!Outs.empty()) {
1807 // Check if stack adjustment is needed. For now, do not do this if any
1808 // argument is passed on the stack.
1809 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001810 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1811 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001812 CCInfo.AnalyzeCallOperands(Outs,
1813 CCAssignFnForNode(CalleeCC, false, isVarArg));
1814 if (CCInfo.getNextStackOffset()) {
1815 MachineFunction &MF = DAG.getMachineFunction();
1816
1817 // Check if the arguments are already laid out in the right way as
1818 // the caller's fixed stack objects.
1819 MachineFrameInfo *MFI = MF.getFrameInfo();
1820 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001821 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001822 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1823 i != e;
1824 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001825 CCValAssign &VA = ArgLocs[i];
1826 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001827 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001828 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001829 if (VA.getLocInfo() == CCValAssign::Indirect)
1830 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001831 if (VA.needsCustom()) {
1832 // f64 and vector types are split into multiple registers or
1833 // register/stack-slot combinations. The types will not match
1834 // the registers; give up on memory f64 refs until we figure
1835 // out what to do about this.
1836 if (!VA.isRegLoc())
1837 return false;
1838 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001839 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001840 if (RegVT == MVT::v2f64) {
1841 if (!ArgLocs[++i].isRegLoc())
1842 return false;
1843 if (!ArgLocs[++i].isRegLoc())
1844 return false;
1845 }
1846 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001847 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1848 MFI, MRI, TII))
1849 return false;
1850 }
1851 }
1852 }
1853 }
1854
1855 return true;
1856}
1857
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858SDValue
1859ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001860 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001861 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001862 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001863 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001864
Bob Wilsondee46d72009-04-17 20:35:10 +00001865 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001866 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001867
Bob Wilsondee46d72009-04-17 20:35:10 +00001868 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001869 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1870 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001871
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001873 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1874 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001875
1876 // If this is the first return lowered for this function, add
1877 // the regs to the liveout set for the function.
1878 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1879 for (unsigned i = 0; i != RVLocs.size(); ++i)
1880 if (RVLocs[i].isRegLoc())
1881 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001882 }
1883
Bob Wilson1f595bb2009-04-17 19:07:39 +00001884 SDValue Flag;
1885
1886 // Copy the result values into the output registers.
1887 for (unsigned i = 0, realRVLocIdx = 0;
1888 i != RVLocs.size();
1889 ++i, ++realRVLocIdx) {
1890 CCValAssign &VA = RVLocs[i];
1891 assert(VA.isRegLoc() && "Can only return in registers!");
1892
Dan Gohmanc9403652010-07-07 15:54:55 +00001893 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001894
1895 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001896 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001897 case CCValAssign::Full: break;
1898 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001899 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001900 break;
1901 }
1902
Bob Wilson1f595bb2009-04-17 19:07:39 +00001903 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001905 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1907 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001908 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001910
1911 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1912 Flag = Chain.getValue(1);
1913 VA = RVLocs[++i]; // skip ahead to next loc
1914 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1915 HalfGPRs.getValue(1), Flag);
1916 Flag = Chain.getValue(1);
1917 VA = RVLocs[++i]; // skip ahead to next loc
1918
1919 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001920 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1921 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001922 }
1923 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1924 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001925 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001928 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001929 VA = RVLocs[++i]; // skip ahead to next loc
1930 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1931 Flag);
1932 } else
1933 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1934
Bob Wilsondee46d72009-04-17 20:35:10 +00001935 // Guarantee that all emitted copies are
1936 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001937 Flag = Chain.getValue(1);
1938 }
1939
1940 SDValue result;
1941 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001943 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001945
1946 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001947}
1948
Evan Chengbf010eb2012-04-10 01:51:00 +00001949bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001950 if (N->getNumValues() != 1)
1951 return false;
1952 if (!N->hasNUsesOfValue(1, 0))
1953 return false;
1954
Evan Chengbf010eb2012-04-10 01:51:00 +00001955 SDValue TCChain = Chain;
1956 SDNode *Copy = *N->use_begin();
1957 if (Copy->getOpcode() == ISD::CopyToReg) {
1958 // If the copy has a glue operand, we conservatively assume it isn't safe to
1959 // perform a tail call.
1960 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1961 return false;
1962 TCChain = Copy->getOperand(0);
1963 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1964 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001965 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001966 SmallPtrSet<SDNode*, 2> Copies;
1967 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001968 UI != UE; ++UI) {
1969 if (UI->getOpcode() != ISD::CopyToReg)
1970 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001971 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001972 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001973 if (Copies.size() > 2)
1974 return false;
1975
1976 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1977 UI != UE; ++UI) {
1978 SDValue UseChain = UI->getOperand(0);
1979 if (Copies.count(UseChain.getNode()))
1980 // Second CopyToReg
1981 Copy = *UI;
1982 else
1983 // First CopyToReg
1984 TCChain = UseChain;
1985 }
1986 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001987 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001988 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001989 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001990 Copy = *Copy->use_begin();
1991 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001993 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 } else {
1995 return false;
1996 }
1997
Evan Cheng1bf891a2010-12-01 22:59:46 +00001998 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001999 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2000 UI != UE; ++UI) {
2001 if (UI->getOpcode() != ARMISD::RET_FLAG)
2002 return false;
2003 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002004 }
2005
Evan Chengbf010eb2012-04-10 01:51:00 +00002006 if (!HasRet)
2007 return false;
2008
2009 Chain = TCChain;
2010 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002011}
2012
Evan Cheng485fafc2011-03-21 01:19:09 +00002013bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002014 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002015 return false;
2016
2017 if (!CI->isTailCall())
2018 return false;
2019
2020 return !Subtarget->isThumb1Only();
2021}
2022
Bob Wilsonb62d2572009-11-03 00:02:05 +00002023// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2024// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2025// one of the above mentioned nodes. It has to be wrapped because otherwise
2026// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2027// be used to form addressing mode. These wrapped nodes will be selected
2028// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002029static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002030 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002031 // FIXME there is no actual debug info here
2032 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002033 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002035 if (CP->isMachineConstantPoolEntry())
2036 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2037 CP->getAlignment());
2038 else
2039 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2040 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002042}
2043
Jim Grosbache1102ca2010-07-19 17:20:38 +00002044unsigned ARMTargetLowering::getJumpTableEncoding() const {
2045 return MachineJumpTableInfo::EK_Inline;
2046}
2047
Dan Gohmand858e902010-04-17 15:26:15 +00002048SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2049 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002050 MachineFunction &MF = DAG.getMachineFunction();
2051 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2052 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002053 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002054 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002055 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002056 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2057 SDValue CPAddr;
2058 if (RelocM == Reloc::Static) {
2059 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2060 } else {
2061 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002062 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002063 ARMConstantPoolValue *CPV =
2064 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2065 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002066 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2067 }
2068 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2069 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002070 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002071 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002072 if (RelocM == Reloc::Static)
2073 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002074 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002075 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002076}
2077
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002078// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002079SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002080ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002081 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002082 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002083 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002084 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002085 MachineFunction &MF = DAG.getMachineFunction();
2086 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002087 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002088 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002089 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2090 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002091 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002092 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002093 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002094 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002095 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002096 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002097
Evan Chenge7e0d622009-11-06 22:24:13 +00002098 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002099 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002100
2101 // call __tls_get_addr.
2102 ArgListTy Args;
2103 ArgListEntry Entry;
2104 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002105 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002106 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002107 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002108 TargetLowering::CallLoweringInfo CLI(Chain,
2109 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002110 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002111 0, CallingConv::C, /*isTailCall=*/false,
2112 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002113 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002114 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002115 return CallResult.first;
2116}
2117
2118// Lower ISD::GlobalTLSAddress using the "initial exec" or
2119// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002120SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002121ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002122 SelectionDAG &DAG,
2123 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002124 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002125 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002126 SDValue Offset;
2127 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002129 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002130 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002131
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002132 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002133 MachineFunction &MF = DAG.getMachineFunction();
2134 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002135 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002136 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002137 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2138 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002139 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2140 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2141 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002142 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002144 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002145 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002146 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002147 Chain = Offset.getValue(1);
2148
Evan Chenge7e0d622009-11-06 22:24:13 +00002149 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002150 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002151
Evan Cheng9eda6892009-10-31 03:39:36 +00002152 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002153 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002154 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002155 } else {
2156 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002157 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002158 ARMConstantPoolValue *CPV =
2159 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002160 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002161 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002162 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002163 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002164 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002165 }
2166
2167 // The address of the thread local variable is the add of the thread
2168 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002169 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002170}
2171
Dan Gohman475871a2008-07-27 21:46:04 +00002172SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002173ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174 // TODO: implement the "local dynamic" model
2175 assert(Subtarget->isTargetELF() &&
2176 "TLS not implemented for non-ELF targets");
2177 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002178
2179 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2180
2181 switch (model) {
2182 case TLSModel::GeneralDynamic:
2183 case TLSModel::LocalDynamic:
2184 return LowerToTLSGeneralDynamicModel(GA, DAG);
2185 case TLSModel::InitialExec:
2186 case TLSModel::LocalExec:
2187 return LowerToTLSExecModels(GA, DAG, model);
2188 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002189 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002190}
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002193 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002194 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002195 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002196 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002197 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2198 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002199 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002200 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002201 ARMConstantPoolConstant::Create(GV,
2202 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002203 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002204 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002205 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002206 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002207 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002208 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002209 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002210 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002212 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002213 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002214 MachinePointerInfo::getGOT(),
2215 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002216 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002217 }
2218
2219 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002220 // pair. This is always cheaper.
2221 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002222 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002223 // FIXME: Once remat is capable of dealing with instructions with register
2224 // operands, expand this into two nodes.
2225 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2226 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002227 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002228 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2229 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2230 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2231 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002232 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002233 }
2234}
2235
Dan Gohman475871a2008-07-27 21:46:04 +00002236SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002237 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002238 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002239 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002240 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002241 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002242 MachineFunction &MF = DAG.getMachineFunction();
2243 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2244
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002245 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2246 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002247 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002248 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002249 // FIXME: Once remat is capable of dealing with instructions with register
2250 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002251 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002252 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2253 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2254
Evan Cheng53519f02011-01-21 18:55:51 +00002255 unsigned Wrapper = (RelocM == Reloc::PIC_)
2256 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2257 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002258 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002259 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2260 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002261 MachinePointerInfo::getGOT(),
2262 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002263 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002264 }
2265
2266 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002268 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002269 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002270 } else {
2271 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002272 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2273 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002274 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2275 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002276 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002277 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002279
Evan Cheng9eda6892009-10-31 03:39:36 +00002280 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002281 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002282 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002283 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002284
2285 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002286 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002287 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002288 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002289
Evan Cheng63476a82009-09-03 07:04:02 +00002290 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002291 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002292 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002293
2294 return Result;
2295}
2296
Dan Gohman475871a2008-07-27 21:46:04 +00002297SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002298 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002299 assert(Subtarget->isTargetELF() &&
2300 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002301 MachineFunction &MF = DAG.getMachineFunction();
2302 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002303 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002304 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002305 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002306 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002307 ARMConstantPoolValue *CPV =
2308 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2309 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002310 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002311 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002312 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002313 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002314 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002315 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002316 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002317}
2318
Jim Grosbach0e0da732009-05-12 23:59:14 +00002319SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002320ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2321 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002322 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002323 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2324 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002325 Op.getOperand(1), Val);
2326}
2327
2328SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002329ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2330 DebugLoc dl = Op.getDebugLoc();
2331 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2332 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2333}
2334
2335SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002336ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002337 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002338 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002339 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002340 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002341 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002342 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002343 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002344 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2345 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002346 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002347 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002348 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002349 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002350 EVT PtrVT = getPointerTy();
2351 DebugLoc dl = Op.getDebugLoc();
2352 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2353 SDValue CPAddr;
2354 unsigned PCAdj = (RelocM != Reloc::PIC_)
2355 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002356 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002357 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2358 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002359 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002360 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002361 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002362 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002363 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002364 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002365
2366 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002368 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2369 }
2370 return Result;
2371 }
Evan Cheng92e39162011-03-29 23:06:19 +00002372 case Intrinsic::arm_neon_vmulls:
2373 case Intrinsic::arm_neon_vmullu: {
2374 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2375 ? ARMISD::VMULLs : ARMISD::VMULLu;
2376 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2377 Op.getOperand(1), Op.getOperand(2));
2378 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002379 }
2380}
2381
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002382static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002383 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002384 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002385 if (!Subtarget->hasDataBarrier()) {
2386 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2387 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2388 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002389 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002390 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002391 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002392 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002393 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002394
2395 SDValue Op5 = Op.getOperand(5);
2396 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2397 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2398 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2399 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2400
2401 ARM_MB::MemBOpt DMBOpt;
2402 if (isDeviceBarrier)
2403 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2404 else
2405 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2406 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2407 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002408}
2409
Eli Friedman26689ac2011-08-03 21:06:02 +00002410
2411static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2412 const ARMSubtarget *Subtarget) {
2413 // FIXME: handle "fence singlethread" more efficiently.
2414 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002415 if (!Subtarget->hasDataBarrier()) {
2416 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2417 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2418 // here.
2419 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2420 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002421 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002422 DAG.getConstant(0, MVT::i32));
2423 }
2424
Eli Friedman26689ac2011-08-03 21:06:02 +00002425 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002426 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002427}
2428
Evan Chengdfed19f2010-11-03 06:34:55 +00002429static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2430 const ARMSubtarget *Subtarget) {
2431 // ARM pre v5TE and Thumb1 does not have preload instructions.
2432 if (!(Subtarget->isThumb2() ||
2433 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2434 // Just preserve the chain.
2435 return Op.getOperand(0);
2436
2437 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002438 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2439 if (!isRead &&
2440 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2441 // ARMv7 with MP extension has PLDW.
2442 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002443
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002444 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2445 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002446 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002447 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002448 isData = ~isData & 1;
2449 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002450
2451 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002452 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2453 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002454}
2455
Dan Gohman1e93df62010-04-17 14:41:14 +00002456static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2457 MachineFunction &MF = DAG.getMachineFunction();
2458 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2459
Evan Chenga8e29892007-01-19 07:51:42 +00002460 // vastart just stores the address of the VarArgsFrameIndex slot into the
2461 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002462 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002463 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002464 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002465 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002466 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2467 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002468}
2469
Dan Gohman475871a2008-07-27 21:46:04 +00002470SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002471ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2472 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002473 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002474 MachineFunction &MF = DAG.getMachineFunction();
2475 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2476
Craig Topper44d23822012-02-22 05:59:10 +00002477 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002478 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002479 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002480 else
Craig Topper420761a2012-04-20 07:30:17 +00002481 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002482
2483 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002484 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002485 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002486
2487 SDValue ArgValue2;
2488 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002490 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002491
2492 // Create load node to retrieve arguments from the stack.
2493 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002494 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002495 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002496 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002498 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002499 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002500 }
2501
Jim Grosbache5165492009-11-09 00:11:35 +00002502 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002503}
2504
Stuart Hastingsc7315872011-04-20 16:47:52 +00002505void
2506ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2507 unsigned &VARegSize, unsigned &VARegSaveSize)
2508 const {
2509 unsigned NumGPRs;
2510 if (CCInfo.isFirstByValRegValid())
2511 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2512 else {
2513 unsigned int firstUnalloced;
2514 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2515 sizeof(GPRArgRegs) /
2516 sizeof(GPRArgRegs[0]));
2517 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2518 }
2519
2520 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2521 VARegSize = NumGPRs * 4;
2522 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2523}
2524
2525// The remaining GPRs hold either the beginning of variable-argument
2526// data, or the beginning of an aggregate passed by value (usuall
2527// byval). Either way, we allocate stack slots adjacent to the data
2528// provided by our caller, and store the unallocated registers there.
2529// If this is a variadic function, the va_list pointer will begin with
2530// these values; otherwise, this reassembles a (byval) structure that
2531// was split between registers and memory.
2532void
2533ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2534 DebugLoc dl, SDValue &Chain,
2535 unsigned ArgOffset) const {
2536 MachineFunction &MF = DAG.getMachineFunction();
2537 MachineFrameInfo *MFI = MF.getFrameInfo();
2538 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2539 unsigned firstRegToSaveIndex;
2540 if (CCInfo.isFirstByValRegValid())
2541 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2542 else {
2543 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2544 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2545 }
2546
2547 unsigned VARegSize, VARegSaveSize;
2548 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2549 if (VARegSaveSize) {
2550 // If this function is vararg, store any remaining integer argument regs
2551 // to their spots on the stack so that they may be loaded by deferencing
2552 // the result of va_next.
2553 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002554 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2555 ArgOffset + VARegSaveSize
2556 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002557 false));
2558 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2559 getPointerTy());
2560
2561 SmallVector<SDValue, 4> MemOps;
2562 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002563 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002564 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002565 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002566 else
Craig Topper420761a2012-04-20 07:30:17 +00002567 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002568
2569 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2570 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2571 SDValue Store =
2572 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002573 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002574 false, false, 0);
2575 MemOps.push_back(Store);
2576 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2577 DAG.getConstant(4, getPointerTy()));
2578 }
2579 if (!MemOps.empty())
2580 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2581 &MemOps[0], MemOps.size());
2582 } else
2583 // This will point to the next argument passed via stack.
2584 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2585}
2586
Bob Wilson5bafff32009-06-22 23:27:02 +00002587SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002588ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002589 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002590 const SmallVectorImpl<ISD::InputArg>
2591 &Ins,
2592 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002593 SmallVectorImpl<SDValue> &InVals)
2594 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002595 MachineFunction &MF = DAG.getMachineFunction();
2596 MachineFrameInfo *MFI = MF.getFrameInfo();
2597
Bob Wilson1f595bb2009-04-17 19:07:39 +00002598 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2599
2600 // Assign locations to all of the incoming arguments.
2601 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002602 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2603 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002604 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002605 CCAssignFnForNode(CallConv, /* Return*/ false,
2606 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002607
2608 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002609 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002610
Stuart Hastingsf222e592011-02-28 17:17:53 +00002611 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002612 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2613 CCValAssign &VA = ArgLocs[i];
2614
Bob Wilsondee46d72009-04-17 20:35:10 +00002615 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002617 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002618
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002620 // f64 and vector types are split up into multiple registers or
2621 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002623 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002624 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002625 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002626 SDValue ArgValue2;
2627 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002628 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002629 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2630 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002631 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002632 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002633 } else {
2634 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2635 Chain, DAG, dl);
2636 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002637 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2638 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002639 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002641 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2642 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002643 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002644
Bob Wilson5bafff32009-06-22 23:27:02 +00002645 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002646 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002647
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002649 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002650 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002651 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002653 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002655 RC = AFI->isThumb1OnlyFunction() ?
2656 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2657 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002658 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002659 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002660
2661 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002662 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002663 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002664 }
2665
2666 // If this is an 8 or 16-bit value, it is really passed promoted
2667 // to 32 bits. Insert an assert[sz]ext to capture this, then
2668 // truncate to the right size.
2669 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002670 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002671 case CCValAssign::Full: break;
2672 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002673 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002674 break;
2675 case CCValAssign::SExt:
2676 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2677 DAG.getValueType(VA.getValVT()));
2678 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2679 break;
2680 case CCValAssign::ZExt:
2681 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2682 DAG.getValueType(VA.getValVT()));
2683 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2684 break;
2685 }
2686
Dan Gohman98ca4f22009-08-05 01:29:28 +00002687 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002688
2689 } else { // VA.isRegLoc()
2690
2691 // sanity check
2692 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002694
Stuart Hastingsf222e592011-02-28 17:17:53 +00002695 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002696
Stuart Hastingsf222e592011-02-28 17:17:53 +00002697 // Some Ins[] entries become multiple ArgLoc[] entries.
2698 // Process them only once.
2699 if (index != lastInsIndex)
2700 {
2701 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002702 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002703 // This can be changed with more analysis.
2704 // In case of tail call optimization mark all arguments mutable.
2705 // Since they could be overwritten by lowering of arguments in case of
2706 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002707 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002708 unsigned VARegSize, VARegSaveSize;
2709 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2710 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2711 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002712 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002713 int FI = MFI->CreateFixedObject(Bytes,
2714 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002715 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2716 } else {
2717 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2718 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002719
Stuart Hastingsf222e592011-02-28 17:17:53 +00002720 // Create load nodes to retrieve arguments from the stack.
2721 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2722 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2723 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002724 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002725 }
2726 lastInsIndex = index;
2727 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002728 }
2729 }
2730
2731 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002732 if (isVarArg)
2733 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002734
Dan Gohman98ca4f22009-08-05 01:29:28 +00002735 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002736}
2737
2738/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002739static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002740 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002741 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002742 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002743 // Maybe this has already been legalized into the constant pool?
2744 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002746 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002747 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002748 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002749 }
2750 }
2751 return false;
2752}
2753
Evan Chenga8e29892007-01-19 07:51:42 +00002754/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2755/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002756SDValue
2757ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002758 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002759 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002760 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002761 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002762 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002763 // Constant does not fit, try adjusting it by one?
2764 switch (CC) {
2765 default: break;
2766 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002767 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002768 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002769 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002770 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002771 }
2772 break;
2773 case ISD::SETULT:
2774 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002775 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002776 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002777 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002778 }
2779 break;
2780 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002781 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002782 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002783 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002784 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002785 }
2786 break;
2787 case ISD::SETULE:
2788 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002789 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002790 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002791 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002792 }
2793 break;
2794 }
2795 }
2796 }
2797
2798 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002799 ARMISD::NodeType CompareType;
2800 switch (CondCode) {
2801 default:
2802 CompareType = ARMISD::CMP;
2803 break;
2804 case ARMCC::EQ:
2805 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002806 // Uses only Z Flag
2807 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002808 break;
2809 }
Evan Cheng218977b2010-07-13 19:27:42 +00002810 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002811 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002812}
2813
2814/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002815SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002816ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002817 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002818 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002819 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002820 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002822 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2823 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002824}
2825
Bob Wilson79f56c92011-03-08 01:17:20 +00002826/// duplicateCmp - Glue values can have only one use, so this function
2827/// duplicates a comparison node.
2828SDValue
2829ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2830 unsigned Opc = Cmp.getOpcode();
2831 DebugLoc DL = Cmp.getDebugLoc();
2832 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2833 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2834
2835 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2836 Cmp = Cmp.getOperand(0);
2837 Opc = Cmp.getOpcode();
2838 if (Opc == ARMISD::CMPFP)
2839 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2840 else {
2841 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2842 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2843 }
2844 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2845}
2846
Bill Wendlingde2b1512010-08-11 08:43:16 +00002847SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2848 SDValue Cond = Op.getOperand(0);
2849 SDValue SelectTrue = Op.getOperand(1);
2850 SDValue SelectFalse = Op.getOperand(2);
2851 DebugLoc dl = Op.getDebugLoc();
2852
2853 // Convert:
2854 //
2855 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2856 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2857 //
2858 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2859 const ConstantSDNode *CMOVTrue =
2860 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2861 const ConstantSDNode *CMOVFalse =
2862 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2863
2864 if (CMOVTrue && CMOVFalse) {
2865 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2866 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2867
2868 SDValue True;
2869 SDValue False;
2870 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2871 True = SelectTrue;
2872 False = SelectFalse;
2873 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2874 True = SelectFalse;
2875 False = SelectTrue;
2876 }
2877
2878 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002879 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002880 SDValue ARMcc = Cond.getOperand(2);
2881 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002882 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002883 assert(True.getValueType() == VT);
2884 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002885 }
2886 }
2887 }
2888
Dan Gohmandb953892012-02-24 00:09:36 +00002889 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2890 // undefined bits before doing a full-word comparison with zero.
2891 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2892 DAG.getConstant(1, Cond.getValueType()));
2893
Bill Wendlingde2b1512010-08-11 08:43:16 +00002894 return DAG.getSelectCC(dl, Cond,
2895 DAG.getConstant(0, Cond.getValueType()),
2896 SelectTrue, SelectFalse, ISD::SETNE);
2897}
2898
Dan Gohmand858e902010-04-17 15:26:15 +00002899SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002900 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002901 SDValue LHS = Op.getOperand(0);
2902 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002903 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002904 SDValue TrueVal = Op.getOperand(2);
2905 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002906 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002907
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002909 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002911 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002912 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002913 }
2914
2915 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002916 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002917
Evan Cheng218977b2010-07-13 19:27:42 +00002918 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2919 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002920 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002921 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002922 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002923 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002924 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002926 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002927 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002928 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002929 }
2930 return Result;
2931}
2932
Evan Cheng218977b2010-07-13 19:27:42 +00002933/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2934/// to morph to an integer compare sequence.
2935static bool canChangeToInt(SDValue Op, bool &SeenZero,
2936 const ARMSubtarget *Subtarget) {
2937 SDNode *N = Op.getNode();
2938 if (!N->hasOneUse())
2939 // Otherwise it requires moving the value from fp to integer registers.
2940 return false;
2941 if (!N->getNumValues())
2942 return false;
2943 EVT VT = Op.getValueType();
2944 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2945 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2946 // vmrs are very slow, e.g. cortex-a8.
2947 return false;
2948
2949 if (isFloatingPointZero(Op)) {
2950 SeenZero = true;
2951 return true;
2952 }
2953 return ISD::isNormalLoad(N);
2954}
2955
2956static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2957 if (isFloatingPointZero(Op))
2958 return DAG.getConstant(0, MVT::i32);
2959
2960 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2961 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002962 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002963 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002964 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002965
2966 llvm_unreachable("Unknown VFP cmp argument!");
2967}
2968
2969static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2970 SDValue &RetVal1, SDValue &RetVal2) {
2971 if (isFloatingPointZero(Op)) {
2972 RetVal1 = DAG.getConstant(0, MVT::i32);
2973 RetVal2 = DAG.getConstant(0, MVT::i32);
2974 return;
2975 }
2976
2977 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2978 SDValue Ptr = Ld->getBasePtr();
2979 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2980 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002981 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002982 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002983 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002984
2985 EVT PtrType = Ptr.getValueType();
2986 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2987 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2988 PtrType, Ptr, DAG.getConstant(4, PtrType));
2989 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2990 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002991 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002992 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002993 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002994 return;
2995 }
2996
2997 llvm_unreachable("Unknown VFP cmp argument!");
2998}
2999
3000/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3001/// f32 and even f64 comparisons to integer ones.
3002SDValue
3003ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3004 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003005 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003006 SDValue LHS = Op.getOperand(2);
3007 SDValue RHS = Op.getOperand(3);
3008 SDValue Dest = Op.getOperand(4);
3009 DebugLoc dl = Op.getDebugLoc();
3010
Evan Chengfc501a32012-03-01 23:27:13 +00003011 bool LHSSeenZero = false;
3012 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3013 bool RHSSeenZero = false;
3014 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3015 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003016 // If unsafe fp math optimization is enabled and there are no other uses of
3017 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003018 // to an integer comparison.
3019 if (CC == ISD::SETOEQ)
3020 CC = ISD::SETEQ;
3021 else if (CC == ISD::SETUNE)
3022 CC = ISD::SETNE;
3023
Evan Chengfc501a32012-03-01 23:27:13 +00003024 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003025 SDValue ARMcc;
3026 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003027 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3028 bitcastf32Toi32(LHS, DAG), Mask);
3029 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3030 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003031 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3032 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3033 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3034 Chain, Dest, ARMcc, CCR, Cmp);
3035 }
3036
3037 SDValue LHS1, LHS2;
3038 SDValue RHS1, RHS2;
3039 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3040 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003041 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3042 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003043 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3044 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003045 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003046 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3047 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3048 }
3049
3050 return SDValue();
3051}
3052
3053SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3054 SDValue Chain = Op.getOperand(0);
3055 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3056 SDValue LHS = Op.getOperand(2);
3057 SDValue RHS = Op.getOperand(3);
3058 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003059 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003060
Owen Anderson825b72b2009-08-11 20:47:22 +00003061 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003062 SDValue ARMcc;
3063 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003064 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003065 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003066 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003067 }
3068
Owen Anderson825b72b2009-08-11 20:47:22 +00003069 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003070
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003071 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003072 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3073 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3074 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3075 if (Result.getNode())
3076 return Result;
3077 }
3078
Evan Chenga8e29892007-01-19 07:51:42 +00003079 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003080 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003081
Evan Cheng218977b2010-07-13 19:27:42 +00003082 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3083 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003084 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003085 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003086 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003087 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003088 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003089 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3090 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003091 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003092 }
3093 return Res;
3094}
3095
Dan Gohmand858e902010-04-17 15:26:15 +00003096SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue Chain = Op.getOperand(0);
3098 SDValue Table = Op.getOperand(1);
3099 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003100 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003101
Owen Andersone50ed302009-08-10 22:56:29 +00003102 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003103 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3104 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003105 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003106 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003108 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3109 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003110 if (Subtarget->isThumb2()) {
3111 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3112 // which does another jump to the destination. This also makes it easier
3113 // to translate it to TBB / TBH later.
3114 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003115 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003116 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003117 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003118 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003119 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003120 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003121 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003122 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003123 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003125 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003126 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003127 MachinePointerInfo::getJumpTable(),
3128 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003129 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003131 }
Evan Chenga8e29892007-01-19 07:51:42 +00003132}
3133
Eli Friedman14e809c2011-11-09 23:36:02 +00003134static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003135 EVT VT = Op.getValueType();
3136 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003137
James Molloy873fd5f2012-02-20 09:24:05 +00003138 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3139 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3140 return Op;
3141 return DAG.UnrollVectorOp(Op.getNode());
3142 }
3143
3144 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3145 "Invalid type for custom lowering!");
3146 if (VT != MVT::v4i16)
3147 return DAG.UnrollVectorOp(Op.getNode());
3148
3149 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3150 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003151}
3152
Bob Wilson76a312b2010-03-19 22:51:32 +00003153static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003154 EVT VT = Op.getValueType();
3155 if (VT.isVector())
3156 return LowerVectorFP_TO_INT(Op, DAG);
3157
Bob Wilson76a312b2010-03-19 22:51:32 +00003158 DebugLoc dl = Op.getDebugLoc();
3159 unsigned Opc;
3160
3161 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003162 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003163 case ISD::FP_TO_SINT:
3164 Opc = ARMISD::FTOSI;
3165 break;
3166 case ISD::FP_TO_UINT:
3167 Opc = ARMISD::FTOUI;
3168 break;
3169 }
3170 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003171 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003172}
3173
Cameron Zwarich3007d332011-03-29 21:41:55 +00003174static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3175 EVT VT = Op.getValueType();
3176 DebugLoc dl = Op.getDebugLoc();
3177
Eli Friedman14e809c2011-11-09 23:36:02 +00003178 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3179 if (VT.getVectorElementType() == MVT::f32)
3180 return Op;
3181 return DAG.UnrollVectorOp(Op.getNode());
3182 }
3183
Duncan Sands1f6a3292011-08-12 14:54:45 +00003184 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3185 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003186 if (VT != MVT::v4f32)
3187 return DAG.UnrollVectorOp(Op.getNode());
3188
3189 unsigned CastOpc;
3190 unsigned Opc;
3191 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003192 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003193 case ISD::SINT_TO_FP:
3194 CastOpc = ISD::SIGN_EXTEND;
3195 Opc = ISD::SINT_TO_FP;
3196 break;
3197 case ISD::UINT_TO_FP:
3198 CastOpc = ISD::ZERO_EXTEND;
3199 Opc = ISD::UINT_TO_FP;
3200 break;
3201 }
3202
3203 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3204 return DAG.getNode(Opc, dl, VT, Op);
3205}
3206
Bob Wilson76a312b2010-03-19 22:51:32 +00003207static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3208 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003209 if (VT.isVector())
3210 return LowerVectorINT_TO_FP(Op, DAG);
3211
Bob Wilson76a312b2010-03-19 22:51:32 +00003212 DebugLoc dl = Op.getDebugLoc();
3213 unsigned Opc;
3214
3215 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003216 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003217 case ISD::SINT_TO_FP:
3218 Opc = ARMISD::SITOF;
3219 break;
3220 case ISD::UINT_TO_FP:
3221 Opc = ARMISD::UITOF;
3222 break;
3223 }
3224
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003225 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003226 return DAG.getNode(Opc, dl, VT, Op);
3227}
3228
Evan Cheng515fe3a2010-07-08 02:08:50 +00003229SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003230 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003231 SDValue Tmp0 = Op.getOperand(0);
3232 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003233 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003234 EVT VT = Op.getValueType();
3235 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003236 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3237 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3238 bool UseNEON = !InGPR && Subtarget->hasNEON();
3239
3240 if (UseNEON) {
3241 // Use VBSL to copy the sign bit.
3242 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3243 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3244 DAG.getTargetConstant(EncodedVal, MVT::i32));
3245 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3246 if (VT == MVT::f64)
3247 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3248 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3249 DAG.getConstant(32, MVT::i32));
3250 else /*if (VT == MVT::f32)*/
3251 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3252 if (SrcVT == MVT::f32) {
3253 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3254 if (VT == MVT::f64)
3255 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3256 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3257 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003258 } else if (VT == MVT::f32)
3259 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3260 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3261 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003262 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3263 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3264
3265 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3266 MVT::i32);
3267 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3268 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3269 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003270
Evan Chenge573fb32011-02-23 02:24:55 +00003271 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3272 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3273 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003274 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003275 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3276 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3277 DAG.getConstant(0, MVT::i32));
3278 } else {
3279 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3280 }
3281
3282 return Res;
3283 }
Evan Chengc143dd42011-02-11 02:28:55 +00003284
3285 // Bitcast operand 1 to i32.
3286 if (SrcVT == MVT::f64)
3287 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3288 &Tmp1, 1).getValue(1);
3289 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3290
Evan Chenge573fb32011-02-23 02:24:55 +00003291 // Or in the signbit with integer operations.
3292 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3293 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3294 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3295 if (VT == MVT::f32) {
3296 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3297 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3298 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3299 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003300 }
3301
Evan Chenge573fb32011-02-23 02:24:55 +00003302 // f64: Or the high part with signbit and then combine two parts.
3303 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3304 &Tmp0, 1);
3305 SDValue Lo = Tmp0.getValue(0);
3306 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3307 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3308 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003309}
3310
Evan Cheng2457f2c2010-05-22 01:47:14 +00003311SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3312 MachineFunction &MF = DAG.getMachineFunction();
3313 MachineFrameInfo *MFI = MF.getFrameInfo();
3314 MFI->setReturnAddressIsTaken(true);
3315
3316 EVT VT = Op.getValueType();
3317 DebugLoc dl = Op.getDebugLoc();
3318 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3319 if (Depth) {
3320 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3321 SDValue Offset = DAG.getConstant(4, MVT::i32);
3322 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3323 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003324 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003325 }
3326
3327 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003328 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003329 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3330}
3331
Dan Gohmand858e902010-04-17 15:26:15 +00003332SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003333 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3334 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003335
Owen Andersone50ed302009-08-10 22:56:29 +00003336 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003337 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3338 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003339 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003340 ? ARM::R7 : ARM::R11;
3341 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3342 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003343 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3344 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003345 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003346 return FrameAddr;
3347}
3348
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003349/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003350/// expand a bit convert where either the source or destination type is i64 to
3351/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3352/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3353/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003354static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003355 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3356 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003357 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003358
Bob Wilson9f3f0612010-04-17 05:30:19 +00003359 // This function is only supposed to be called for i64 types, either as the
3360 // source or destination of the bit convert.
3361 EVT SrcVT = Op.getValueType();
3362 EVT DstVT = N->getValueType(0);
3363 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003364 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003365
Bob Wilson9f3f0612010-04-17 05:30:19 +00003366 // Turn i64->f64 into VMOVDRR.
3367 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003368 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3369 DAG.getConstant(0, MVT::i32));
3370 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3371 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003372 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003373 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003374 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003375
Jim Grosbache5165492009-11-09 00:11:35 +00003376 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003377 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3378 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3379 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3380 // Merge the pieces into a single i64 value.
3381 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3382 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003383
Bob Wilson9f3f0612010-04-17 05:30:19 +00003384 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003385}
3386
Bob Wilson5bafff32009-06-22 23:27:02 +00003387/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003388/// Zero vectors are used to represent vector negation and in those cases
3389/// will be implemented with the NEON VNEG instruction. However, VNEG does
3390/// not support i64 elements, so sometimes the zero vectors will need to be
3391/// explicitly constructed. Regardless, use a canonical VMOV to create the
3392/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003393static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003394 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003395 // The canonical modified immediate encoding of a zero vector is....0!
3396 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3397 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3398 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003399 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003400}
3401
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003402/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3403/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003404SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3405 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003406 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3407 EVT VT = Op.getValueType();
3408 unsigned VTBits = VT.getSizeInBits();
3409 DebugLoc dl = Op.getDebugLoc();
3410 SDValue ShOpLo = Op.getOperand(0);
3411 SDValue ShOpHi = Op.getOperand(1);
3412 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003413 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003414 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003415
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003416 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3417
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003418 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3419 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3420 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3421 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3422 DAG.getConstant(VTBits, MVT::i32));
3423 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3424 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003425 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003426
3427 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3428 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003429 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003430 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003431 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003432 CCR, Cmp);
3433
3434 SDValue Ops[2] = { Lo, Hi };
3435 return DAG.getMergeValues(Ops, 2, dl);
3436}
3437
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003438/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3439/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003440SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3441 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003442 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3443 EVT VT = Op.getValueType();
3444 unsigned VTBits = VT.getSizeInBits();
3445 DebugLoc dl = Op.getDebugLoc();
3446 SDValue ShOpLo = Op.getOperand(0);
3447 SDValue ShOpHi = Op.getOperand(1);
3448 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003449 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003450
3451 assert(Op.getOpcode() == ISD::SHL_PARTS);
3452 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3453 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3454 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3455 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3456 DAG.getConstant(VTBits, MVT::i32));
3457 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3458 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3459
3460 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3461 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3462 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003463 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003464 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003465 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003466 CCR, Cmp);
3467
3468 SDValue Ops[2] = { Lo, Hi };
3469 return DAG.getMergeValues(Ops, 2, dl);
3470}
3471
Jim Grosbach4725ca72010-09-08 03:54:02 +00003472SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003473 SelectionDAG &DAG) const {
3474 // The rounding mode is in bits 23:22 of the FPSCR.
3475 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3476 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3477 // so that the shift + and get folded into a bitfield extract.
3478 DebugLoc dl = Op.getDebugLoc();
3479 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3480 DAG.getConstant(Intrinsic::arm_get_fpscr,
3481 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003482 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003483 DAG.getConstant(1U << 22, MVT::i32));
3484 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3485 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003486 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003487 DAG.getConstant(3, MVT::i32));
3488}
3489
Jim Grosbach3482c802010-01-18 19:58:49 +00003490static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3491 const ARMSubtarget *ST) {
3492 EVT VT = N->getValueType(0);
3493 DebugLoc dl = N->getDebugLoc();
3494
3495 if (!ST->hasV6T2Ops())
3496 return SDValue();
3497
3498 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3499 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3500}
3501
Bob Wilson5bafff32009-06-22 23:27:02 +00003502static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3503 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003504 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003505 DebugLoc dl = N->getDebugLoc();
3506
Bob Wilsond5448bb2010-11-18 21:16:28 +00003507 if (!VT.isVector())
3508 return SDValue();
3509
Bob Wilson5bafff32009-06-22 23:27:02 +00003510 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003511 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003512
Bob Wilsond5448bb2010-11-18 21:16:28 +00003513 // Left shifts translate directly to the vshiftu intrinsic.
3514 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003515 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003516 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3517 N->getOperand(0), N->getOperand(1));
3518
3519 assert((N->getOpcode() == ISD::SRA ||
3520 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3521
3522 // NEON uses the same intrinsics for both left and right shifts. For
3523 // right shifts, the shift amounts are negative, so negate the vector of
3524 // shift amounts.
3525 EVT ShiftVT = N->getOperand(1).getValueType();
3526 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3527 getZeroVector(ShiftVT, DAG, dl),
3528 N->getOperand(1));
3529 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3530 Intrinsic::arm_neon_vshifts :
3531 Intrinsic::arm_neon_vshiftu);
3532 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3533 DAG.getConstant(vshiftInt, MVT::i32),
3534 N->getOperand(0), NegatedCount);
3535}
3536
3537static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3538 const ARMSubtarget *ST) {
3539 EVT VT = N->getValueType(0);
3540 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003541
Eli Friedmance392eb2009-08-22 03:13:10 +00003542 // We can get here for a node like i32 = ISD::SHL i32, i64
3543 if (VT != MVT::i64)
3544 return SDValue();
3545
3546 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003547 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003548
Chris Lattner27a6c732007-11-24 07:07:01 +00003549 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3550 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003551 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003552 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003553
Chris Lattner27a6c732007-11-24 07:07:01 +00003554 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003555 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003556
Chris Lattner27a6c732007-11-24 07:07:01 +00003557 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003559 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003561 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003562
Chris Lattner27a6c732007-11-24 07:07:01 +00003563 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3564 // captures the result into a carry flag.
3565 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003566 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003567
Chris Lattner27a6c732007-11-24 07:07:01 +00003568 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003570
Chris Lattner27a6c732007-11-24 07:07:01 +00003571 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003573}
3574
Bob Wilson5bafff32009-06-22 23:27:02 +00003575static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3576 SDValue TmpOp0, TmpOp1;
3577 bool Invert = false;
3578 bool Swap = false;
3579 unsigned Opc = 0;
3580
3581 SDValue Op0 = Op.getOperand(0);
3582 SDValue Op1 = Op.getOperand(1);
3583 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003584 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003585 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3586 DebugLoc dl = Op.getDebugLoc();
3587
3588 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3589 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003590 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003591 case ISD::SETUNE:
3592 case ISD::SETNE: Invert = true; // Fallthrough
3593 case ISD::SETOEQ:
3594 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3595 case ISD::SETOLT:
3596 case ISD::SETLT: Swap = true; // Fallthrough
3597 case ISD::SETOGT:
3598 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3599 case ISD::SETOLE:
3600 case ISD::SETLE: Swap = true; // Fallthrough
3601 case ISD::SETOGE:
3602 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3603 case ISD::SETUGE: Swap = true; // Fallthrough
3604 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3605 case ISD::SETUGT: Swap = true; // Fallthrough
3606 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3607 case ISD::SETUEQ: Invert = true; // Fallthrough
3608 case ISD::SETONE:
3609 // Expand this to (OLT | OGT).
3610 TmpOp0 = Op0;
3611 TmpOp1 = Op1;
3612 Opc = ISD::OR;
3613 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3614 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3615 break;
3616 case ISD::SETUO: Invert = true; // Fallthrough
3617 case ISD::SETO:
3618 // Expand this to (OLT | OGE).
3619 TmpOp0 = Op0;
3620 TmpOp1 = Op1;
3621 Opc = ISD::OR;
3622 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3623 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3624 break;
3625 }
3626 } else {
3627 // Integer comparisons.
3628 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003629 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003630 case ISD::SETNE: Invert = true;
3631 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3632 case ISD::SETLT: Swap = true;
3633 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3634 case ISD::SETLE: Swap = true;
3635 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3636 case ISD::SETULT: Swap = true;
3637 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3638 case ISD::SETULE: Swap = true;
3639 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3640 }
3641
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003642 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003643 if (Opc == ARMISD::VCEQ) {
3644
3645 SDValue AndOp;
3646 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3647 AndOp = Op0;
3648 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3649 AndOp = Op1;
3650
3651 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003652 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003653 AndOp = AndOp.getOperand(0);
3654
3655 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3656 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3658 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003659 Invert = !Invert;
3660 }
3661 }
3662 }
3663
3664 if (Swap)
3665 std::swap(Op0, Op1);
3666
Owen Andersonc24cb352010-11-08 23:21:22 +00003667 // If one of the operands is a constant vector zero, attempt to fold the
3668 // comparison to a specialized compare-against-zero form.
3669 SDValue SingleOp;
3670 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3671 SingleOp = Op0;
3672 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3673 if (Opc == ARMISD::VCGE)
3674 Opc = ARMISD::VCLEZ;
3675 else if (Opc == ARMISD::VCGT)
3676 Opc = ARMISD::VCLTZ;
3677 SingleOp = Op1;
3678 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003679
Owen Andersonc24cb352010-11-08 23:21:22 +00003680 SDValue Result;
3681 if (SingleOp.getNode()) {
3682 switch (Opc) {
3683 case ARMISD::VCEQ:
3684 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3685 case ARMISD::VCGE:
3686 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3687 case ARMISD::VCLEZ:
3688 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3689 case ARMISD::VCGT:
3690 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3691 case ARMISD::VCLTZ:
3692 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3693 default:
3694 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3695 }
3696 } else {
3697 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3698 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003699
3700 if (Invert)
3701 Result = DAG.getNOT(dl, Result, VT);
3702
3703 return Result;
3704}
3705
Bob Wilsond3c42842010-06-14 22:19:57 +00003706/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3707/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003708/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003709static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3710 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003711 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003712 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003713
Bob Wilson827b2102010-06-15 19:05:35 +00003714 // SplatBitSize is set to the smallest size that splats the vector, so a
3715 // zero vector will always have SplatBitSize == 8. However, NEON modified
3716 // immediate instructions others than VMOV do not support the 8-bit encoding
3717 // of a zero vector, and the default encoding of zero is supposed to be the
3718 // 32-bit version.
3719 if (SplatBits == 0)
3720 SplatBitSize = 32;
3721
Bob Wilson5bafff32009-06-22 23:27:02 +00003722 switch (SplatBitSize) {
3723 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003724 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003725 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003726 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003727 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003728 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003729 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003730 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003732
3733 case 16:
3734 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003735 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 if ((SplatBits & ~0xff) == 0) {
3737 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003738 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003739 Imm = SplatBits;
3740 break;
3741 }
3742 if ((SplatBits & ~0xff00) == 0) {
3743 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003744 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003745 Imm = SplatBits >> 8;
3746 break;
3747 }
3748 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003749
3750 case 32:
3751 // NEON's 32-bit VMOV supports splat values where:
3752 // * only one byte is nonzero, or
3753 // * the least significant byte is 0xff and the second byte is nonzero, or
3754 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003755 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003756 if ((SplatBits & ~0xff) == 0) {
3757 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003758 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003759 Imm = SplatBits;
3760 break;
3761 }
3762 if ((SplatBits & ~0xff00) == 0) {
3763 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003764 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003765 Imm = SplatBits >> 8;
3766 break;
3767 }
3768 if ((SplatBits & ~0xff0000) == 0) {
3769 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003770 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003771 Imm = SplatBits >> 16;
3772 break;
3773 }
3774 if ((SplatBits & ~0xff000000) == 0) {
3775 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003776 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003777 Imm = SplatBits >> 24;
3778 break;
3779 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003780
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003781 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3782 if (type == OtherModImm) return SDValue();
3783
Bob Wilson5bafff32009-06-22 23:27:02 +00003784 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003785 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3786 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003787 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003788 Imm = SplatBits >> 8;
3789 SplatBits |= 0xff;
3790 break;
3791 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003792
3793 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003794 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3795 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003796 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003797 Imm = SplatBits >> 16;
3798 SplatBits |= 0xffff;
3799 break;
3800 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003801
3802 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3803 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3804 // VMOV.I32. A (very) minor optimization would be to replicate the value
3805 // and fall through here to test for a valid 64-bit splat. But, then the
3806 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003807 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003808
3809 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003810 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003811 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003812 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003813 uint64_t BitMask = 0xff;
3814 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003815 unsigned ImmMask = 1;
3816 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003817 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003818 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 Imm |= ImmMask;
3821 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003822 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003823 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003826 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003828 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003829 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003830 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003831 break;
3832 }
3833
Bob Wilson1a913ed2010-06-11 21:34:50 +00003834 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003835 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003836 }
3837
Bob Wilsoncba270d2010-07-13 21:16:48 +00003838 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3839 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003840}
3841
Lang Hamesc0a9f822012-03-29 21:56:11 +00003842SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3843 const ARMSubtarget *ST) const {
3844 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3845 return SDValue();
3846
3847 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3848 assert(Op.getValueType() == MVT::f32 &&
3849 "ConstantFP custom lowering should only occur for f32.");
3850
3851 // Try splatting with a VMOV.f32...
3852 APFloat FPVal = CFP->getValueAPF();
3853 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3854 if (ImmVal != -1) {
3855 DebugLoc DL = Op.getDebugLoc();
3856 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3857 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3858 NewVal);
3859 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3860 DAG.getConstant(0, MVT::i32));
3861 }
3862
3863 // If that fails, try a VMOV.i32
3864 EVT VMovVT;
3865 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3866 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3867 VMOVModImm);
3868 if (NewVal != SDValue()) {
3869 DebugLoc DL = Op.getDebugLoc();
3870 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3871 NewVal);
3872 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3873 VecConstant);
3874 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3875 DAG.getConstant(0, MVT::i32));
3876 }
3877
3878 // Finally, try a VMVN.i32
3879 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3880 VMVNModImm);
3881 if (NewVal != SDValue()) {
3882 DebugLoc DL = Op.getDebugLoc();
3883 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3884 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3885 VecConstant);
3886 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3887 DAG.getConstant(0, MVT::i32));
3888 }
3889
3890 return SDValue();
3891}
3892
3893
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003894static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003895 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003896 unsigned NumElts = VT.getVectorNumElements();
3897 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003898
3899 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3900 if (M[0] < 0)
3901 return false;
3902
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003903 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003904
3905 // If this is a VEXT shuffle, the immediate value is the index of the first
3906 // element. The other shuffle indices must be the successive elements after
3907 // the first one.
3908 unsigned ExpectedElt = Imm;
3909 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003910 // Increment the expected index. If it wraps around, it may still be
3911 // a VEXT but the source vectors must be swapped.
3912 ExpectedElt += 1;
3913 if (ExpectedElt == NumElts * 2) {
3914 ExpectedElt = 0;
3915 ReverseVEXT = true;
3916 }
3917
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003918 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003919 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003920 return false;
3921 }
3922
3923 // Adjust the index value if the source operands will be swapped.
3924 if (ReverseVEXT)
3925 Imm -= NumElts;
3926
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003927 return true;
3928}
3929
Bob Wilson8bb9e482009-07-26 00:39:34 +00003930/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3931/// instruction with the specified blocksize. (The order of the elements
3932/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003933static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003934 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3935 "Only possible block sizes for VREV are: 16, 32, 64");
3936
Bob Wilson8bb9e482009-07-26 00:39:34 +00003937 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003938 if (EltSz == 64)
3939 return false;
3940
3941 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003942 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003943 // If the first shuffle index is UNDEF, be optimistic.
3944 if (M[0] < 0)
3945 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003946
3947 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3948 return false;
3949
3950 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003951 if (M[i] < 0) continue; // ignore UNDEF indices
3952 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003953 return false;
3954 }
3955
3956 return true;
3957}
3958
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003959static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003960 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3961 // range, then 0 is placed into the resulting vector. So pretty much any mask
3962 // of 8 elements can work here.
3963 return VT == MVT::v8i8 && M.size() == 8;
3964}
3965
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003966static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003967 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3968 if (EltSz == 64)
3969 return false;
3970
Bob Wilsonc692cb72009-08-21 20:54:19 +00003971 unsigned NumElts = VT.getVectorNumElements();
3972 WhichResult = (M[0] == 0 ? 0 : 1);
3973 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003974 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3975 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003976 return false;
3977 }
3978 return true;
3979}
3980
Bob Wilson324f4f12009-12-03 06:40:55 +00003981/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3982/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3983/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003984static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003985 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3986 if (EltSz == 64)
3987 return false;
3988
3989 unsigned NumElts = VT.getVectorNumElements();
3990 WhichResult = (M[0] == 0 ? 0 : 1);
3991 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003992 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3993 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003994 return false;
3995 }
3996 return true;
3997}
3998
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003999static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004000 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4001 if (EltSz == 64)
4002 return false;
4003
Bob Wilsonc692cb72009-08-21 20:54:19 +00004004 unsigned NumElts = VT.getVectorNumElements();
4005 WhichResult = (M[0] == 0 ? 0 : 1);
4006 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004007 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004008 if ((unsigned) M[i] != 2 * i + WhichResult)
4009 return false;
4010 }
4011
4012 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004013 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004014 return false;
4015
4016 return true;
4017}
4018
Bob Wilson324f4f12009-12-03 06:40:55 +00004019/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4020/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4021/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004022static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004023 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4024 if (EltSz == 64)
4025 return false;
4026
4027 unsigned Half = VT.getVectorNumElements() / 2;
4028 WhichResult = (M[0] == 0 ? 0 : 1);
4029 for (unsigned j = 0; j != 2; ++j) {
4030 unsigned Idx = WhichResult;
4031 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004032 int MIdx = M[i + j * Half];
4033 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004034 return false;
4035 Idx += 2;
4036 }
4037 }
4038
4039 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4040 if (VT.is64BitVector() && EltSz == 32)
4041 return false;
4042
4043 return true;
4044}
4045
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004046static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004047 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4048 if (EltSz == 64)
4049 return false;
4050
Bob Wilsonc692cb72009-08-21 20:54:19 +00004051 unsigned NumElts = VT.getVectorNumElements();
4052 WhichResult = (M[0] == 0 ? 0 : 1);
4053 unsigned Idx = WhichResult * NumElts / 2;
4054 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004055 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4056 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004057 return false;
4058 Idx += 1;
4059 }
4060
4061 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004062 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004063 return false;
4064
4065 return true;
4066}
4067
Bob Wilson324f4f12009-12-03 06:40:55 +00004068/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4069/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4070/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004071static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004072 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4073 if (EltSz == 64)
4074 return false;
4075
4076 unsigned NumElts = VT.getVectorNumElements();
4077 WhichResult = (M[0] == 0 ? 0 : 1);
4078 unsigned Idx = WhichResult * NumElts / 2;
4079 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004080 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4081 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004082 return false;
4083 Idx += 1;
4084 }
4085
4086 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4087 if (VT.is64BitVector() && EltSz == 32)
4088 return false;
4089
4090 return true;
4091}
4092
Dale Johannesenf630c712010-07-29 20:10:08 +00004093// If N is an integer constant that can be moved into a register in one
4094// instruction, return an SDValue of such a constant (will become a MOV
4095// instruction). Otherwise return null.
4096static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4097 const ARMSubtarget *ST, DebugLoc dl) {
4098 uint64_t Val;
4099 if (!isa<ConstantSDNode>(N))
4100 return SDValue();
4101 Val = cast<ConstantSDNode>(N)->getZExtValue();
4102
4103 if (ST->isThumb1Only()) {
4104 if (Val <= 255 || ~Val <= 255)
4105 return DAG.getConstant(Val, MVT::i32);
4106 } else {
4107 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4108 return DAG.getConstant(Val, MVT::i32);
4109 }
4110 return SDValue();
4111}
4112
Bob Wilson5bafff32009-06-22 23:27:02 +00004113// If this is a case we can't handle, return null and let the default
4114// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004115SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4116 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004117 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004118 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004119 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004120
4121 APInt SplatBits, SplatUndef;
4122 unsigned SplatBitSize;
4123 bool HasAnyUndefs;
4124 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004125 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004126 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004127 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004128 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004129 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004130 DAG, VmovVT, VT.is128BitVector(),
4131 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004132 if (Val.getNode()) {
4133 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004134 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004135 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004136
4137 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004138 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004139 Val = isNEONModifiedImm(NegatedImm,
4140 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004141 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004142 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004143 if (Val.getNode()) {
4144 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004145 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004146 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004147
4148 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004149 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004150 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004151 if (ImmVal != -1) {
4152 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4153 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4154 }
4155 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004156 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004157 }
4158
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004159 // Scan through the operands to see if only one value is used.
4160 unsigned NumElts = VT.getVectorNumElements();
4161 bool isOnlyLowElement = true;
4162 bool usesOnlyOneValue = true;
4163 bool isConstant = true;
4164 SDValue Value;
4165 for (unsigned i = 0; i < NumElts; ++i) {
4166 SDValue V = Op.getOperand(i);
4167 if (V.getOpcode() == ISD::UNDEF)
4168 continue;
4169 if (i > 0)
4170 isOnlyLowElement = false;
4171 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4172 isConstant = false;
4173
4174 if (!Value.getNode())
4175 Value = V;
4176 else if (V != Value)
4177 usesOnlyOneValue = false;
4178 }
4179
4180 if (!Value.getNode())
4181 return DAG.getUNDEF(VT);
4182
4183 if (isOnlyLowElement)
4184 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4185
Dale Johannesenf630c712010-07-29 20:10:08 +00004186 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4187
Dale Johannesen575cd142010-10-19 20:00:17 +00004188 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4189 // i32 and try again.
4190 if (usesOnlyOneValue && EltSize <= 32) {
4191 if (!isConstant)
4192 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4193 if (VT.getVectorElementType().isFloatingPoint()) {
4194 SmallVector<SDValue, 8> Ops;
4195 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004196 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004197 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004198 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4199 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004200 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4201 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004202 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004203 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004204 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4205 if (Val.getNode())
4206 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004207 }
4208
4209 // If all elements are constants and the case above didn't get hit, fall back
4210 // to the default expansion, which will generate a load from the constant
4211 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004212 if (isConstant)
4213 return SDValue();
4214
Bob Wilson11a1dff2011-01-07 21:37:30 +00004215 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4216 if (NumElts >= 4) {
4217 SDValue shuffle = ReconstructShuffle(Op, DAG);
4218 if (shuffle != SDValue())
4219 return shuffle;
4220 }
4221
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004222 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004223 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4224 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004225 if (EltSize >= 32) {
4226 // Do the expansion with floating-point types, since that is what the VFP
4227 // registers are defined to use, and since i64 is not legal.
4228 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4229 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004230 SmallVector<SDValue, 8> Ops;
4231 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004232 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004233 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004234 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004235 }
4236
4237 return SDValue();
4238}
4239
Bob Wilson11a1dff2011-01-07 21:37:30 +00004240// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004241// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004242SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4243 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004244 DebugLoc dl = Op.getDebugLoc();
4245 EVT VT = Op.getValueType();
4246 unsigned NumElts = VT.getVectorNumElements();
4247
4248 SmallVector<SDValue, 2> SourceVecs;
4249 SmallVector<unsigned, 2> MinElts;
4250 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004251
Bob Wilson11a1dff2011-01-07 21:37:30 +00004252 for (unsigned i = 0; i < NumElts; ++i) {
4253 SDValue V = Op.getOperand(i);
4254 if (V.getOpcode() == ISD::UNDEF)
4255 continue;
4256 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4257 // A shuffle can only come from building a vector from various
4258 // elements of other vectors.
4259 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004260 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4261 VT.getVectorElementType()) {
4262 // This code doesn't know how to handle shuffles where the vector
4263 // element types do not match (this happens because type legalization
4264 // promotes the return type of EXTRACT_VECTOR_ELT).
4265 // FIXME: It might be appropriate to extend this code to handle
4266 // mismatched types.
4267 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004268 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004269
Bob Wilson11a1dff2011-01-07 21:37:30 +00004270 // Record this extraction against the appropriate vector if possible...
4271 SDValue SourceVec = V.getOperand(0);
4272 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4273 bool FoundSource = false;
4274 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4275 if (SourceVecs[j] == SourceVec) {
4276 if (MinElts[j] > EltNo)
4277 MinElts[j] = EltNo;
4278 if (MaxElts[j] < EltNo)
4279 MaxElts[j] = EltNo;
4280 FoundSource = true;
4281 break;
4282 }
4283 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004284
Bob Wilson11a1dff2011-01-07 21:37:30 +00004285 // Or record a new source if not...
4286 if (!FoundSource) {
4287 SourceVecs.push_back(SourceVec);
4288 MinElts.push_back(EltNo);
4289 MaxElts.push_back(EltNo);
4290 }
4291 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004292
Bob Wilson11a1dff2011-01-07 21:37:30 +00004293 // Currently only do something sane when at most two source vectors
4294 // involved.
4295 if (SourceVecs.size() > 2)
4296 return SDValue();
4297
4298 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4299 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004300
Bob Wilson11a1dff2011-01-07 21:37:30 +00004301 // This loop extracts the usage patterns of the source vectors
4302 // and prepares appropriate SDValues for a shuffle if possible.
4303 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4304 if (SourceVecs[i].getValueType() == VT) {
4305 // No VEXT necessary
4306 ShuffleSrcs[i] = SourceVecs[i];
4307 VEXTOffsets[i] = 0;
4308 continue;
4309 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4310 // It probably isn't worth padding out a smaller vector just to
4311 // break it down again in a shuffle.
4312 return SDValue();
4313 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004314
Bob Wilson11a1dff2011-01-07 21:37:30 +00004315 // Since only 64-bit and 128-bit vectors are legal on ARM and
4316 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004317 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4318 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004319
Bob Wilson11a1dff2011-01-07 21:37:30 +00004320 if (MaxElts[i] - MinElts[i] >= NumElts) {
4321 // Span too large for a VEXT to cope
4322 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004323 }
4324
Bob Wilson11a1dff2011-01-07 21:37:30 +00004325 if (MinElts[i] >= NumElts) {
4326 // The extraction can just take the second half
4327 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004328 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4329 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004330 DAG.getIntPtrConstant(NumElts));
4331 } else if (MaxElts[i] < NumElts) {
4332 // The extraction can just take the first half
4333 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004334 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4335 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004336 DAG.getIntPtrConstant(0));
4337 } else {
4338 // An actual VEXT is needed
4339 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004340 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4341 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004342 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004343 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4344 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004345 DAG.getIntPtrConstant(NumElts));
4346 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4347 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4348 }
4349 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004350
Bob Wilson11a1dff2011-01-07 21:37:30 +00004351 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004352
Bob Wilson11a1dff2011-01-07 21:37:30 +00004353 for (unsigned i = 0; i < NumElts; ++i) {
4354 SDValue Entry = Op.getOperand(i);
4355 if (Entry.getOpcode() == ISD::UNDEF) {
4356 Mask.push_back(-1);
4357 continue;
4358 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004359
Bob Wilson11a1dff2011-01-07 21:37:30 +00004360 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004361 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4362 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004363 if (ExtractVec == SourceVecs[0]) {
4364 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4365 } else {
4366 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4367 }
4368 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004369
Bob Wilson11a1dff2011-01-07 21:37:30 +00004370 // Final check before we try to produce nonsense...
4371 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004372 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4373 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004374
Bob Wilson11a1dff2011-01-07 21:37:30 +00004375 return SDValue();
4376}
4377
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004378/// isShuffleMaskLegal - Targets can use this to indicate that they only
4379/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4380/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4381/// are assumed to be legal.
4382bool
4383ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4384 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004385 if (VT.getVectorNumElements() == 4 &&
4386 (VT.is128BitVector() || VT.is64BitVector())) {
4387 unsigned PFIndexes[4];
4388 for (unsigned i = 0; i != 4; ++i) {
4389 if (M[i] < 0)
4390 PFIndexes[i] = 8;
4391 else
4392 PFIndexes[i] = M[i];
4393 }
4394
4395 // Compute the index in the perfect shuffle table.
4396 unsigned PFTableIndex =
4397 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4398 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4399 unsigned Cost = (PFEntry >> 30);
4400
4401 if (Cost <= 4)
4402 return true;
4403 }
4404
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004405 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004406 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004407
Bob Wilson53dd2452010-06-07 23:53:38 +00004408 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4409 return (EltSize >= 32 ||
4410 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004411 isVREVMask(M, VT, 64) ||
4412 isVREVMask(M, VT, 32) ||
4413 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004414 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004415 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004416 isVTRNMask(M, VT, WhichResult) ||
4417 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004418 isVZIPMask(M, VT, WhichResult) ||
4419 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4420 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4421 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004422}
4423
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004424/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4425/// the specified operations to build the shuffle.
4426static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4427 SDValue RHS, SelectionDAG &DAG,
4428 DebugLoc dl) {
4429 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4430 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4431 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4432
4433 enum {
4434 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4435 OP_VREV,
4436 OP_VDUP0,
4437 OP_VDUP1,
4438 OP_VDUP2,
4439 OP_VDUP3,
4440 OP_VEXT1,
4441 OP_VEXT2,
4442 OP_VEXT3,
4443 OP_VUZPL, // VUZP, left result
4444 OP_VUZPR, // VUZP, right result
4445 OP_VZIPL, // VZIP, left result
4446 OP_VZIPR, // VZIP, right result
4447 OP_VTRNL, // VTRN, left result
4448 OP_VTRNR // VTRN, right result
4449 };
4450
4451 if (OpNum == OP_COPY) {
4452 if (LHSID == (1*9+2)*9+3) return LHS;
4453 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4454 return RHS;
4455 }
4456
4457 SDValue OpLHS, OpRHS;
4458 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4459 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4460 EVT VT = OpLHS.getValueType();
4461
4462 switch (OpNum) {
4463 default: llvm_unreachable("Unknown shuffle opcode!");
4464 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004465 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004466 if (VT.getVectorElementType() == MVT::i32 ||
4467 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004468 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4469 // vrev <4 x i16> -> VREV32
4470 if (VT.getVectorElementType() == MVT::i16)
4471 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4472 // vrev <4 x i8> -> VREV16
4473 assert(VT.getVectorElementType() == MVT::i8);
4474 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004475 case OP_VDUP0:
4476 case OP_VDUP1:
4477 case OP_VDUP2:
4478 case OP_VDUP3:
4479 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004480 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004481 case OP_VEXT1:
4482 case OP_VEXT2:
4483 case OP_VEXT3:
4484 return DAG.getNode(ARMISD::VEXT, dl, VT,
4485 OpLHS, OpRHS,
4486 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4487 case OP_VUZPL:
4488 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004489 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004490 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4491 case OP_VZIPL:
4492 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004493 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004494 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4495 case OP_VTRNL:
4496 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004497 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4498 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004499 }
4500}
4501
Bill Wendling69a05a72011-03-14 23:02:38 +00004502static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004503 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004504 SelectionDAG &DAG) {
4505 // Check to see if we can use the VTBL instruction.
4506 SDValue V1 = Op.getOperand(0);
4507 SDValue V2 = Op.getOperand(1);
4508 DebugLoc DL = Op.getDebugLoc();
4509
4510 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004511 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004512 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4513 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4514
4515 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4516 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4517 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4518 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004519
Owen Anderson76706012011-04-05 21:48:57 +00004520 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004521 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4522 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004523}
4524
Bob Wilson5bafff32009-06-22 23:27:02 +00004525static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004526 SDValue V1 = Op.getOperand(0);
4527 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004528 DebugLoc dl = Op.getDebugLoc();
4529 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004530 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004531
Bob Wilson28865062009-08-13 02:13:04 +00004532 // Convert shuffles that are directly supported on NEON to target-specific
4533 // DAG nodes, instead of keeping them as shuffles and matching them again
4534 // during code selection. This is more efficient and avoids the possibility
4535 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004536 // FIXME: floating-point vectors should be canonicalized to integer vectors
4537 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004538 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004539
Bob Wilson53dd2452010-06-07 23:53:38 +00004540 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4541 if (EltSize <= 32) {
4542 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4543 int Lane = SVN->getSplatIndex();
4544 // If this is undef splat, generate it via "just" vdup, if possible.
4545 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004546
Dan Gohman65fd6562011-11-03 21:49:52 +00004547 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004548 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4549 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4550 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004551 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4552 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4553 // reaches it).
4554 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4555 !isa<ConstantSDNode>(V1.getOperand(0))) {
4556 bool IsScalarToVector = true;
4557 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4558 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4559 IsScalarToVector = false;
4560 break;
4561 }
4562 if (IsScalarToVector)
4563 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4564 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004565 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4566 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004567 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004568
4569 bool ReverseVEXT;
4570 unsigned Imm;
4571 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4572 if (ReverseVEXT)
4573 std::swap(V1, V2);
4574 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4575 DAG.getConstant(Imm, MVT::i32));
4576 }
4577
4578 if (isVREVMask(ShuffleMask, VT, 64))
4579 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4580 if (isVREVMask(ShuffleMask, VT, 32))
4581 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4582 if (isVREVMask(ShuffleMask, VT, 16))
4583 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4584
4585 // Check for Neon shuffles that modify both input vectors in place.
4586 // If both results are used, i.e., if there are two shuffles with the same
4587 // source operands and with masks corresponding to both results of one of
4588 // these operations, DAG memoization will ensure that a single node is
4589 // used for both shuffles.
4590 unsigned WhichResult;
4591 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4592 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4593 V1, V2).getValue(WhichResult);
4594 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4595 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4596 V1, V2).getValue(WhichResult);
4597 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4598 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4599 V1, V2).getValue(WhichResult);
4600
4601 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4602 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4603 V1, V1).getValue(WhichResult);
4604 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4605 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4606 V1, V1).getValue(WhichResult);
4607 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4608 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4609 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004610 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004611
Bob Wilsonc692cb72009-08-21 20:54:19 +00004612 // If the shuffle is not directly supported and it has 4 elements, use
4613 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004614 unsigned NumElts = VT.getVectorNumElements();
4615 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004616 unsigned PFIndexes[4];
4617 for (unsigned i = 0; i != 4; ++i) {
4618 if (ShuffleMask[i] < 0)
4619 PFIndexes[i] = 8;
4620 else
4621 PFIndexes[i] = ShuffleMask[i];
4622 }
4623
4624 // Compute the index in the perfect shuffle table.
4625 unsigned PFTableIndex =
4626 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004627 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4628 unsigned Cost = (PFEntry >> 30);
4629
4630 if (Cost <= 4)
4631 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4632 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004633
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004634 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004635 if (EltSize >= 32) {
4636 // Do the expansion with floating-point types, since that is what the VFP
4637 // registers are defined to use, and since i64 is not legal.
4638 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4639 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004640 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4641 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004642 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004643 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004644 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004645 Ops.push_back(DAG.getUNDEF(EltVT));
4646 else
4647 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4648 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4649 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4650 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004651 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004652 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004653 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004654 }
4655
Bill Wendling69a05a72011-03-14 23:02:38 +00004656 if (VT == MVT::v8i8) {
4657 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4658 if (NewOp.getNode())
4659 return NewOp;
4660 }
4661
Bob Wilson22cac0d2009-08-14 05:16:33 +00004662 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004663}
4664
Eli Friedman5c89cb82011-10-24 23:08:52 +00004665static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4666 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4667 SDValue Lane = Op.getOperand(2);
4668 if (!isa<ConstantSDNode>(Lane))
4669 return SDValue();
4670
4671 return Op;
4672}
4673
Bob Wilson5bafff32009-06-22 23:27:02 +00004674static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004675 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004676 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004677 if (!isa<ConstantSDNode>(Lane))
4678 return SDValue();
4679
4680 SDValue Vec = Op.getOperand(0);
4681 if (Op.getValueType() == MVT::i32 &&
4682 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4683 DebugLoc dl = Op.getDebugLoc();
4684 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4685 }
4686
4687 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004688}
4689
Bob Wilsona6d65862009-08-03 20:36:38 +00004690static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4691 // The only time a CONCAT_VECTORS operation can have legal types is when
4692 // two 64-bit vectors are concatenated to a 128-bit vector.
4693 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4694 "unexpected CONCAT_VECTORS");
4695 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004696 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004697 SDValue Op0 = Op.getOperand(0);
4698 SDValue Op1 = Op.getOperand(1);
4699 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004700 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004701 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004702 DAG.getIntPtrConstant(0));
4703 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004705 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004706 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004707 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004708}
4709
Bob Wilson626613d2010-11-23 19:38:38 +00004710/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4711/// element has been zero/sign-extended, depending on the isSigned parameter,
4712/// from an integer type half its size.
4713static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4714 bool isSigned) {
4715 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4716 EVT VT = N->getValueType(0);
4717 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4718 SDNode *BVN = N->getOperand(0).getNode();
4719 if (BVN->getValueType(0) != MVT::v4i32 ||
4720 BVN->getOpcode() != ISD::BUILD_VECTOR)
4721 return false;
4722 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4723 unsigned HiElt = 1 - LoElt;
4724 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4725 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4726 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4727 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4728 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4729 return false;
4730 if (isSigned) {
4731 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4732 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4733 return true;
4734 } else {
4735 if (Hi0->isNullValue() && Hi1->isNullValue())
4736 return true;
4737 }
4738 return false;
4739 }
4740
4741 if (N->getOpcode() != ISD::BUILD_VECTOR)
4742 return false;
4743
4744 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4745 SDNode *Elt = N->getOperand(i).getNode();
4746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4747 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4748 unsigned HalfSize = EltSize / 2;
4749 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004750 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004751 return false;
4752 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004753 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004754 return false;
4755 }
4756 continue;
4757 }
4758 return false;
4759 }
4760
4761 return true;
4762}
4763
4764/// isSignExtended - Check if a node is a vector value that is sign-extended
4765/// or a constant BUILD_VECTOR with sign-extended elements.
4766static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4767 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4768 return true;
4769 if (isExtendedBUILD_VECTOR(N, DAG, true))
4770 return true;
4771 return false;
4772}
4773
4774/// isZeroExtended - Check if a node is a vector value that is zero-extended
4775/// or a constant BUILD_VECTOR with zero-extended elements.
4776static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4777 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4778 return true;
4779 if (isExtendedBUILD_VECTOR(N, DAG, false))
4780 return true;
4781 return false;
4782}
4783
4784/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4785/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004786static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4787 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4788 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004789 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4790 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4791 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004792 LD->isNonTemporal(), LD->isInvariant(),
4793 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004794 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4795 // have been legalized as a BITCAST from v4i32.
4796 if (N->getOpcode() == ISD::BITCAST) {
4797 SDNode *BVN = N->getOperand(0).getNode();
4798 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4799 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4800 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4801 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4802 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4803 }
4804 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4805 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4806 EVT VT = N->getValueType(0);
4807 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4808 unsigned NumElts = VT.getVectorNumElements();
4809 MVT TruncVT = MVT::getIntegerVT(EltSize);
4810 SmallVector<SDValue, 8> Ops;
4811 for (unsigned i = 0; i != NumElts; ++i) {
4812 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4813 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004814 // Element types smaller than 32 bits are not legal, so use i32 elements.
4815 // The values are implicitly truncated so sext vs. zext doesn't matter.
4816 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004817 }
4818 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4819 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004820}
4821
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004822static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4823 unsigned Opcode = N->getOpcode();
4824 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4825 SDNode *N0 = N->getOperand(0).getNode();
4826 SDNode *N1 = N->getOperand(1).getNode();
4827 return N0->hasOneUse() && N1->hasOneUse() &&
4828 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4829 }
4830 return false;
4831}
4832
4833static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4834 unsigned Opcode = N->getOpcode();
4835 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4836 SDNode *N0 = N->getOperand(0).getNode();
4837 SDNode *N1 = N->getOperand(1).getNode();
4838 return N0->hasOneUse() && N1->hasOneUse() &&
4839 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4840 }
4841 return false;
4842}
4843
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004844static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4845 // Multiplications are only custom-lowered for 128-bit vectors so that
4846 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4847 EVT VT = Op.getValueType();
4848 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4849 SDNode *N0 = Op.getOperand(0).getNode();
4850 SDNode *N1 = Op.getOperand(1).getNode();
4851 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004852 bool isMLA = false;
4853 bool isN0SExt = isSignExtended(N0, DAG);
4854 bool isN1SExt = isSignExtended(N1, DAG);
4855 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004856 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004857 else {
4858 bool isN0ZExt = isZeroExtended(N0, DAG);
4859 bool isN1ZExt = isZeroExtended(N1, DAG);
4860 if (isN0ZExt && isN1ZExt)
4861 NewOpc = ARMISD::VMULLu;
4862 else if (isN1SExt || isN1ZExt) {
4863 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4864 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4865 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4866 NewOpc = ARMISD::VMULLs;
4867 isMLA = true;
4868 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4869 NewOpc = ARMISD::VMULLu;
4870 isMLA = true;
4871 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4872 std::swap(N0, N1);
4873 NewOpc = ARMISD::VMULLu;
4874 isMLA = true;
4875 }
4876 }
4877
4878 if (!NewOpc) {
4879 if (VT == MVT::v2i64)
4880 // Fall through to expand this. It is not legal.
4881 return SDValue();
4882 else
4883 // Other vector multiplications are legal.
4884 return Op;
4885 }
4886 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004887
4888 // Legalize to a VMULL instruction.
4889 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004890 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004891 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004892 if (!isMLA) {
4893 Op0 = SkipExtension(N0, DAG);
4894 assert(Op0.getValueType().is64BitVector() &&
4895 Op1.getValueType().is64BitVector() &&
4896 "unexpected types for extended operands to VMULL");
4897 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4898 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004899
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004900 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4901 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4902 // vmull q0, d4, d6
4903 // vmlal q0, d5, d6
4904 // is faster than
4905 // vaddl q0, d4, d5
4906 // vmovl q1, d6
4907 // vmul q0, q0, q1
4908 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4909 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4910 EVT Op1VT = Op1.getValueType();
4911 return DAG.getNode(N0->getOpcode(), DL, VT,
4912 DAG.getNode(NewOpc, DL, VT,
4913 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4914 DAG.getNode(NewOpc, DL, VT,
4915 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004916}
4917
Owen Anderson76706012011-04-05 21:48:57 +00004918static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004919LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4920 // Convert to float
4921 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4922 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4923 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4924 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4925 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4926 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4927 // Get reciprocal estimate.
4928 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004929 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004930 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4931 // Because char has a smaller range than uchar, we can actually get away
4932 // without any newton steps. This requires that we use a weird bias
4933 // of 0xb000, however (again, this has been exhaustively tested).
4934 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4935 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4936 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4937 Y = DAG.getConstant(0xb000, MVT::i32);
4938 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4939 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4940 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4941 // Convert back to short.
4942 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4943 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4944 return X;
4945}
4946
Owen Anderson76706012011-04-05 21:48:57 +00004947static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004948LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4949 SDValue N2;
4950 // Convert to float.
4951 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4952 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4953 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4954 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4955 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4956 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004957
Nate Begeman7973f352011-02-11 20:53:29 +00004958 // Use reciprocal estimate and one refinement step.
4959 // float4 recip = vrecpeq_f32(yf);
4960 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004961 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004962 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004963 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004964 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4965 N1, N2);
4966 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4967 // Because short has a smaller range than ushort, we can actually get away
4968 // with only a single newton step. This requires that we use a weird bias
4969 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004970 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004971 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4972 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004973 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004974 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4975 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4976 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4977 // Convert back to integer and return.
4978 // return vmovn_s32(vcvt_s32_f32(result));
4979 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4980 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4981 return N0;
4982}
4983
4984static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4985 EVT VT = Op.getValueType();
4986 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4987 "unexpected type for custom-lowering ISD::SDIV");
4988
4989 DebugLoc dl = Op.getDebugLoc();
4990 SDValue N0 = Op.getOperand(0);
4991 SDValue N1 = Op.getOperand(1);
4992 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004993
Nate Begeman7973f352011-02-11 20:53:29 +00004994 if (VT == MVT::v8i8) {
4995 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4996 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004997
Nate Begeman7973f352011-02-11 20:53:29 +00004998 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4999 DAG.getIntPtrConstant(4));
5000 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005001 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005002 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5003 DAG.getIntPtrConstant(0));
5004 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5005 DAG.getIntPtrConstant(0));
5006
5007 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5008 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5009
5010 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5011 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005012
Nate Begeman7973f352011-02-11 20:53:29 +00005013 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5014 return N0;
5015 }
5016 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5017}
5018
5019static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5020 EVT VT = Op.getValueType();
5021 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5022 "unexpected type for custom-lowering ISD::UDIV");
5023
5024 DebugLoc dl = Op.getDebugLoc();
5025 SDValue N0 = Op.getOperand(0);
5026 SDValue N1 = Op.getOperand(1);
5027 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005028
Nate Begeman7973f352011-02-11 20:53:29 +00005029 if (VT == MVT::v8i8) {
5030 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5031 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005032
Nate Begeman7973f352011-02-11 20:53:29 +00005033 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5034 DAG.getIntPtrConstant(4));
5035 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005036 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005037 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5038 DAG.getIntPtrConstant(0));
5039 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5040 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005041
Nate Begeman7973f352011-02-11 20:53:29 +00005042 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5043 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005044
Nate Begeman7973f352011-02-11 20:53:29 +00005045 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5046 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005047
5048 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005049 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5050 N0);
5051 return N0;
5052 }
Owen Anderson76706012011-04-05 21:48:57 +00005053
Nate Begeman7973f352011-02-11 20:53:29 +00005054 // v4i16 sdiv ... Convert to float.
5055 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5056 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5057 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5058 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5059 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005060 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005061
5062 // Use reciprocal estimate and two refinement steps.
5063 // float4 recip = vrecpeq_f32(yf);
5064 // recip *= vrecpsq_f32(yf, recip);
5065 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005066 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005067 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005068 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005069 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005070 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005071 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005072 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005073 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005074 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005075 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5076 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5077 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5078 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005079 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005080 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5081 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5082 N1 = DAG.getConstant(2, MVT::i32);
5083 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5084 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5085 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5086 // Convert back to integer and return.
5087 // return vmovn_u32(vcvt_s32_f32(result));
5088 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5089 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5090 return N0;
5091}
5092
Evan Cheng342e3162011-08-30 01:34:54 +00005093static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5094 EVT VT = Op.getNode()->getValueType(0);
5095 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5096
5097 unsigned Opc;
5098 bool ExtraOp = false;
5099 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005100 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005101 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5102 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5103 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5104 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5105 }
5106
5107 if (!ExtraOp)
5108 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5109 Op.getOperand(1));
5110 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5111 Op.getOperand(1), Op.getOperand(2));
5112}
5113
Eli Friedman74bf18c2011-09-15 22:26:18 +00005114static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005115 // Monotonic load/store is legal for all targets
5116 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5117 return Op;
5118
5119 // Aquire/Release load/store is not legal for targets without a
5120 // dmb or equivalent available.
5121 return SDValue();
5122}
5123
5124
Eli Friedman2bdffe42011-08-31 00:31:29 +00005125static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005126ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5127 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005128 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005129 assert (Node->getValueType(0) == MVT::i64 &&
5130 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005131
Eli Friedman4d3f3292011-08-31 17:52:22 +00005132 SmallVector<SDValue, 6> Ops;
5133 Ops.push_back(Node->getOperand(0)); // Chain
5134 Ops.push_back(Node->getOperand(1)); // Ptr
5135 // Low part of Val1
5136 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5137 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5138 // High part of Val1
5139 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5140 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005141 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005142 // High part of Val1
5143 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5144 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5145 // High part of Val2
5146 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5147 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5148 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005149 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5150 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005151 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005152 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005153 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005154 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5155 Results.push_back(Result.getValue(2));
5156}
5157
Dan Gohmand858e902010-04-17 15:26:15 +00005158SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005159 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005160 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005161 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005162 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005163 case ISD::GlobalAddress:
5164 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5165 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005166 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005167 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005168 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5169 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005170 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005171 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005172 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005173 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005174 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005175 case ISD::SINT_TO_FP:
5176 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5177 case ISD::FP_TO_SINT:
5178 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005179 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005180 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005181 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005182 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005183 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005184 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005185 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5186 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005187 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005188 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005189 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005190 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005191 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005192 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005193 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005194 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005195 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005196 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005197 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005198 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005199 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005200 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005201 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005202 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005203 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005204 case ISD::SDIV: return LowerSDIV(Op, DAG);
5205 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005206 case ISD::ADDC:
5207 case ISD::ADDE:
5208 case ISD::SUBC:
5209 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005210 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005211 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005212 }
Evan Chenga8e29892007-01-19 07:51:42 +00005213}
5214
Duncan Sands1607f052008-12-01 11:39:25 +00005215/// ReplaceNodeResults - Replace the results of node with an illegal result
5216/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005217void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5218 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005219 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005220 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005221 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005222 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005223 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005224 case ISD::BITCAST:
5225 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005226 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005227 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005228 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005229 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005230 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005231 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005232 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005233 return;
5234 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005235 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005236 return;
5237 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005238 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005239 return;
5240 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005241 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005242 return;
5243 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005244 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005245 return;
5246 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005247 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005248 return;
5249 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005250 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005251 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005252 case ISD::ATOMIC_CMP_SWAP:
5253 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5254 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005255 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005256 if (Res.getNode())
5257 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005258}
Chris Lattner27a6c732007-11-24 07:07:01 +00005259
Evan Chenga8e29892007-01-19 07:51:42 +00005260//===----------------------------------------------------------------------===//
5261// ARM Scheduler Hooks
5262//===----------------------------------------------------------------------===//
5263
5264MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005265ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5266 MachineBasicBlock *BB,
5267 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005268 unsigned dest = MI->getOperand(0).getReg();
5269 unsigned ptr = MI->getOperand(1).getReg();
5270 unsigned oldval = MI->getOperand(2).getReg();
5271 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005272 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5273 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005274 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005275
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005276 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005277 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5278 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5279 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005280
5281 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005282 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5283 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5284 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005285 }
5286
Jim Grosbach5278eb82009-12-11 01:42:04 +00005287 unsigned ldrOpc, strOpc;
5288 switch (Size) {
5289 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005290 case 1:
5291 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005292 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005293 break;
5294 case 2:
5295 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5296 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5297 break;
5298 case 4:
5299 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5300 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5301 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005302 }
5303
5304 MachineFunction *MF = BB->getParent();
5305 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5306 MachineFunction::iterator It = BB;
5307 ++It; // insert the new blocks after the current block
5308
5309 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5310 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5311 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5312 MF->insert(It, loop1MBB);
5313 MF->insert(It, loop2MBB);
5314 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005315
5316 // Transfer the remainder of BB and its successor edges to exitMBB.
5317 exitMBB->splice(exitMBB->begin(), BB,
5318 llvm::next(MachineBasicBlock::iterator(MI)),
5319 BB->end());
5320 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005321
5322 // thisMBB:
5323 // ...
5324 // fallthrough --> loop1MBB
5325 BB->addSuccessor(loop1MBB);
5326
5327 // loop1MBB:
5328 // ldrex dest, [ptr]
5329 // cmp dest, oldval
5330 // bne exitMBB
5331 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005332 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5333 if (ldrOpc == ARM::t2LDREX)
5334 MIB.addImm(0);
5335 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005336 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005337 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005338 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5339 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005340 BB->addSuccessor(loop2MBB);
5341 BB->addSuccessor(exitMBB);
5342
5343 // loop2MBB:
5344 // strex scratch, newval, [ptr]
5345 // cmp scratch, #0
5346 // bne loop1MBB
5347 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005348 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5349 if (strOpc == ARM::t2STREX)
5350 MIB.addImm(0);
5351 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005352 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005353 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005354 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5355 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005356 BB->addSuccessor(loop1MBB);
5357 BB->addSuccessor(exitMBB);
5358
5359 // exitMBB:
5360 // ...
5361 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005362
Dan Gohman14152b42010-07-06 20:24:04 +00005363 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005364
Jim Grosbach5278eb82009-12-11 01:42:04 +00005365 return BB;
5366}
5367
5368MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005369ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5370 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005371 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5372 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5373
5374 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005375 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005376 MachineFunction::iterator It = BB;
5377 ++It;
5378
5379 unsigned dest = MI->getOperand(0).getReg();
5380 unsigned ptr = MI->getOperand(1).getReg();
5381 unsigned incr = MI->getOperand(2).getReg();
5382 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005383 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005384
5385 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5386 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005387 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5388 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005389 }
5390
Jim Grosbachc3c23542009-12-14 04:22:04 +00005391 unsigned ldrOpc, strOpc;
5392 switch (Size) {
5393 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005394 case 1:
5395 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005396 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005397 break;
5398 case 2:
5399 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5400 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5401 break;
5402 case 4:
5403 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5404 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5405 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005406 }
5407
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005408 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5409 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5410 MF->insert(It, loopMBB);
5411 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005412
5413 // Transfer the remainder of BB and its successor edges to exitMBB.
5414 exitMBB->splice(exitMBB->begin(), BB,
5415 llvm::next(MachineBasicBlock::iterator(MI)),
5416 BB->end());
5417 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005418
Craig Topper420761a2012-04-20 07:30:17 +00005419 const TargetRegisterClass *TRC = isThumb2 ?
5420 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5421 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005422 unsigned scratch = MRI.createVirtualRegister(TRC);
5423 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005424
5425 // thisMBB:
5426 // ...
5427 // fallthrough --> loopMBB
5428 BB->addSuccessor(loopMBB);
5429
5430 // loopMBB:
5431 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005432 // <binop> scratch2, dest, incr
5433 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005434 // cmp scratch, #0
5435 // bne- loopMBB
5436 // fallthrough --> exitMBB
5437 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005438 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5439 if (ldrOpc == ARM::t2LDREX)
5440 MIB.addImm(0);
5441 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005442 if (BinOpcode) {
5443 // operand order needs to go the other way for NAND
5444 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5445 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5446 addReg(incr).addReg(dest)).addReg(0);
5447 else
5448 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5449 addReg(dest).addReg(incr)).addReg(0);
5450 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005451
Jim Grosbachb6aed502011-09-09 18:37:27 +00005452 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5453 if (strOpc == ARM::t2STREX)
5454 MIB.addImm(0);
5455 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005456 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005457 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005458 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5459 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005460
5461 BB->addSuccessor(loopMBB);
5462 BB->addSuccessor(exitMBB);
5463
5464 // exitMBB:
5465 // ...
5466 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005467
Dan Gohman14152b42010-07-06 20:24:04 +00005468 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005469
Jim Grosbachc3c23542009-12-14 04:22:04 +00005470 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005471}
5472
Jim Grosbachf7da8822011-04-26 19:44:18 +00005473MachineBasicBlock *
5474ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5475 MachineBasicBlock *BB,
5476 unsigned Size,
5477 bool signExtend,
5478 ARMCC::CondCodes Cond) const {
5479 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5480
5481 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5482 MachineFunction *MF = BB->getParent();
5483 MachineFunction::iterator It = BB;
5484 ++It;
5485
5486 unsigned dest = MI->getOperand(0).getReg();
5487 unsigned ptr = MI->getOperand(1).getReg();
5488 unsigned incr = MI->getOperand(2).getReg();
5489 unsigned oldval = dest;
5490 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005491 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005492
5493 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5494 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005495 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5496 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005497 }
5498
Jim Grosbachf7da8822011-04-26 19:44:18 +00005499 unsigned ldrOpc, strOpc, extendOpc;
5500 switch (Size) {
5501 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5502 case 1:
5503 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5504 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005505 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005506 break;
5507 case 2:
5508 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5509 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005510 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005511 break;
5512 case 4:
5513 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5514 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5515 extendOpc = 0;
5516 break;
5517 }
5518
5519 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5520 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5521 MF->insert(It, loopMBB);
5522 MF->insert(It, exitMBB);
5523
5524 // Transfer the remainder of BB and its successor edges to exitMBB.
5525 exitMBB->splice(exitMBB->begin(), BB,
5526 llvm::next(MachineBasicBlock::iterator(MI)),
5527 BB->end());
5528 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5529
Craig Topper420761a2012-04-20 07:30:17 +00005530 const TargetRegisterClass *TRC = isThumb2 ?
5531 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5532 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005533 unsigned scratch = MRI.createVirtualRegister(TRC);
5534 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005535
5536 // thisMBB:
5537 // ...
5538 // fallthrough --> loopMBB
5539 BB->addSuccessor(loopMBB);
5540
5541 // loopMBB:
5542 // ldrex dest, ptr
5543 // (sign extend dest, if required)
5544 // cmp dest, incr
5545 // cmov.cond scratch2, dest, incr
5546 // strex scratch, scratch2, ptr
5547 // cmp scratch, #0
5548 // bne- loopMBB
5549 // fallthrough --> exitMBB
5550 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005551 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5552 if (ldrOpc == ARM::t2LDREX)
5553 MIB.addImm(0);
5554 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005555
5556 // Sign extend the value, if necessary.
5557 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005558 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005559 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5560 .addReg(dest)
5561 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005562 }
5563
5564 // Build compare and cmov instructions.
5565 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5566 .addReg(oldval).addReg(incr));
5567 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5568 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5569
Jim Grosbachb6aed502011-09-09 18:37:27 +00005570 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5571 if (strOpc == ARM::t2STREX)
5572 MIB.addImm(0);
5573 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005574 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5575 .addReg(scratch).addImm(0));
5576 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5577 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5578
5579 BB->addSuccessor(loopMBB);
5580 BB->addSuccessor(exitMBB);
5581
5582 // exitMBB:
5583 // ...
5584 BB = exitMBB;
5585
5586 MI->eraseFromParent(); // The instruction is gone now.
5587
5588 return BB;
5589}
5590
Eli Friedman2bdffe42011-08-31 00:31:29 +00005591MachineBasicBlock *
5592ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5593 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005594 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005595 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5596 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5597
5598 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5599 MachineFunction *MF = BB->getParent();
5600 MachineFunction::iterator It = BB;
5601 ++It;
5602
5603 unsigned destlo = MI->getOperand(0).getReg();
5604 unsigned desthi = MI->getOperand(1).getReg();
5605 unsigned ptr = MI->getOperand(2).getReg();
5606 unsigned vallo = MI->getOperand(3).getReg();
5607 unsigned valhi = MI->getOperand(4).getReg();
5608 DebugLoc dl = MI->getDebugLoc();
5609 bool isThumb2 = Subtarget->isThumb2();
5610
5611 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5612 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005613 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5614 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5615 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005616 }
5617
5618 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5619 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5620
5621 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005622 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005623 if (IsCmpxchg) {
5624 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5625 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5626 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005627 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5628 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005629 if (IsCmpxchg) {
5630 MF->insert(It, contBB);
5631 MF->insert(It, cont2BB);
5632 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005633 MF->insert(It, exitMBB);
5634
5635 // Transfer the remainder of BB and its successor edges to exitMBB.
5636 exitMBB->splice(exitMBB->begin(), BB,
5637 llvm::next(MachineBasicBlock::iterator(MI)),
5638 BB->end());
5639 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5640
Craig Topper420761a2012-04-20 07:30:17 +00005641 const TargetRegisterClass *TRC = isThumb2 ?
5642 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5643 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005644 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5645
5646 // thisMBB:
5647 // ...
5648 // fallthrough --> loopMBB
5649 BB->addSuccessor(loopMBB);
5650
5651 // loopMBB:
5652 // ldrexd r2, r3, ptr
5653 // <binopa> r0, r2, incr
5654 // <binopb> r1, r3, incr
5655 // strexd storesuccess, r0, r1, ptr
5656 // cmp storesuccess, #0
5657 // bne- loopMBB
5658 // fallthrough --> exitMBB
5659 //
5660 // Note that the registers are explicitly specified because there is not any
5661 // way to force the register allocator to allocate a register pair.
5662 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005663 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005664 // need to properly enforce the restriction that the two output registers
5665 // for ldrexd must be different.
5666 BB = loopMBB;
5667 // Load
5668 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5669 .addReg(ARM::R2, RegState::Define)
5670 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5671 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5672 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5673 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005674
5675 if (IsCmpxchg) {
5676 // Add early exit
5677 for (unsigned i = 0; i < 2; i++) {
5678 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5679 ARM::CMPrr))
5680 .addReg(i == 0 ? destlo : desthi)
5681 .addReg(i == 0 ? vallo : valhi));
5682 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5683 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5684 BB->addSuccessor(exitMBB);
5685 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5686 BB = (i == 0 ? contBB : cont2BB);
5687 }
5688
5689 // Copy to physregs for strexd
5690 unsigned setlo = MI->getOperand(5).getReg();
5691 unsigned sethi = MI->getOperand(6).getReg();
5692 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5693 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5694 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005695 // Perform binary operation
5696 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5697 .addReg(destlo).addReg(vallo))
5698 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5699 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5700 .addReg(desthi).addReg(valhi)).addReg(0);
5701 } else {
5702 // Copy to physregs for strexd
5703 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5704 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5705 }
5706
5707 // Store
5708 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5709 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5710 // Cmp+jump
5711 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5712 .addReg(storesuccess).addImm(0));
5713 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5714 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5715
5716 BB->addSuccessor(loopMBB);
5717 BB->addSuccessor(exitMBB);
5718
5719 // exitMBB:
5720 // ...
5721 BB = exitMBB;
5722
5723 MI->eraseFromParent(); // The instruction is gone now.
5724
5725 return BB;
5726}
5727
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005728/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5729/// registers the function context.
5730void ARMTargetLowering::
5731SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5732 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005733 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5734 DebugLoc dl = MI->getDebugLoc();
5735 MachineFunction *MF = MBB->getParent();
5736 MachineRegisterInfo *MRI = &MF->getRegInfo();
5737 MachineConstantPool *MCP = MF->getConstantPool();
5738 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5739 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005740
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005741 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005742 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005743
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005744 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005745 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005746 ARMConstantPoolValue *CPV =
5747 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5748 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5749
Craig Topper420761a2012-04-20 07:30:17 +00005750 const TargetRegisterClass *TRC = isThumb ?
5751 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5752 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005753
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005754 // Grab constant pool and fixed stack memory operands.
5755 MachineMemOperand *CPMMO =
5756 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5757 MachineMemOperand::MOLoad, 4, 4);
5758
5759 MachineMemOperand *FIMMOSt =
5760 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5761 MachineMemOperand::MOStore, 4, 4);
5762
5763 // Load the address of the dispatch MBB into the jump buffer.
5764 if (isThumb2) {
5765 // Incoming value: jbuf
5766 // ldr.n r5, LCPI1_1
5767 // orr r5, r5, #1
5768 // add r5, pc
5769 // str r5, [$jbuf, #+4] ; &jbuf[1]
5770 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5771 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5772 .addConstantPoolIndex(CPI)
5773 .addMemOperand(CPMMO));
5774 // Set the low bit because of thumb mode.
5775 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5776 AddDefaultCC(
5777 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5778 .addReg(NewVReg1, RegState::Kill)
5779 .addImm(0x01)));
5780 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5781 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5782 .addReg(NewVReg2, RegState::Kill)
5783 .addImm(PCLabelId);
5784 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5785 .addReg(NewVReg3, RegState::Kill)
5786 .addFrameIndex(FI)
5787 .addImm(36) // &jbuf[1] :: pc
5788 .addMemOperand(FIMMOSt));
5789 } else if (isThumb) {
5790 // Incoming value: jbuf
5791 // ldr.n r1, LCPI1_4
5792 // add r1, pc
5793 // mov r2, #1
5794 // orrs r1, r2
5795 // add r2, $jbuf, #+4 ; &jbuf[1]
5796 // str r1, [r2]
5797 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5798 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5799 .addConstantPoolIndex(CPI)
5800 .addMemOperand(CPMMO));
5801 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5802 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5803 .addReg(NewVReg1, RegState::Kill)
5804 .addImm(PCLabelId);
5805 // Set the low bit because of thumb mode.
5806 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5807 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5808 .addReg(ARM::CPSR, RegState::Define)
5809 .addImm(1));
5810 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5811 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5812 .addReg(ARM::CPSR, RegState::Define)
5813 .addReg(NewVReg2, RegState::Kill)
5814 .addReg(NewVReg3, RegState::Kill));
5815 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5816 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5817 .addFrameIndex(FI)
5818 .addImm(36)); // &jbuf[1] :: pc
5819 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5820 .addReg(NewVReg4, RegState::Kill)
5821 .addReg(NewVReg5, RegState::Kill)
5822 .addImm(0)
5823 .addMemOperand(FIMMOSt));
5824 } else {
5825 // Incoming value: jbuf
5826 // ldr r1, LCPI1_1
5827 // add r1, pc, r1
5828 // str r1, [$jbuf, #+4] ; &jbuf[1]
5829 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5830 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5831 .addConstantPoolIndex(CPI)
5832 .addImm(0)
5833 .addMemOperand(CPMMO));
5834 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5835 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5836 .addReg(NewVReg1, RegState::Kill)
5837 .addImm(PCLabelId));
5838 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5839 .addReg(NewVReg2, RegState::Kill)
5840 .addFrameIndex(FI)
5841 .addImm(36) // &jbuf[1] :: pc
5842 .addMemOperand(FIMMOSt));
5843 }
5844}
5845
5846MachineBasicBlock *ARMTargetLowering::
5847EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5848 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5849 DebugLoc dl = MI->getDebugLoc();
5850 MachineFunction *MF = MBB->getParent();
5851 MachineRegisterInfo *MRI = &MF->getRegInfo();
5852 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5853 MachineFrameInfo *MFI = MF->getFrameInfo();
5854 int FI = MFI->getFunctionContextIndex();
5855
Craig Topper420761a2012-04-20 07:30:17 +00005856 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5857 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005858 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005859
Bill Wendling04f15b42011-10-06 21:29:56 +00005860 // Get a mapping of the call site numbers to all of the landing pads they're
5861 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005862 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5863 unsigned MaxCSNum = 0;
5864 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005865 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5866 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005867 if (!BB->isLandingPad()) continue;
5868
5869 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5870 // pad.
5871 for (MachineBasicBlock::iterator
5872 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5873 if (!II->isEHLabel()) continue;
5874
5875 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005876 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005877
Bill Wendling5cbef192011-10-05 23:28:57 +00005878 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5879 for (SmallVectorImpl<unsigned>::iterator
5880 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5881 CSI != CSE; ++CSI) {
5882 CallSiteNumToLPad[*CSI].push_back(BB);
5883 MaxCSNum = std::max(MaxCSNum, *CSI);
5884 }
Bill Wendling2a850152011-10-05 00:02:33 +00005885 break;
5886 }
5887 }
5888
5889 // Get an ordered list of the machine basic blocks for the jump table.
5890 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005891 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005892 LPadList.reserve(CallSiteNumToLPad.size());
5893 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5894 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5895 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005896 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005897 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005898 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5899 }
Bill Wendling2a850152011-10-05 00:02:33 +00005900 }
5901
Bill Wendling5cbef192011-10-05 23:28:57 +00005902 assert(!LPadList.empty() &&
5903 "No landing pad destinations for the dispatch jump table!");
5904
Bill Wendling04f15b42011-10-06 21:29:56 +00005905 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005906 MachineJumpTableInfo *JTI =
5907 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5908 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5909 unsigned UId = AFI->createJumpTableUId();
5910
Bill Wendling04f15b42011-10-06 21:29:56 +00005911 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005912
5913 // Shove the dispatch's address into the return slot in the function context.
5914 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5915 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005916
Bill Wendlingbb734682011-10-05 00:39:32 +00005917 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005918 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005919 DispatchBB->addSuccessor(TrapBB);
5920
5921 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5922 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005923
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005924 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005925 MF->insert(MF->end(), DispatchBB);
5926 MF->insert(MF->end(), DispContBB);
5927 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005928
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005929 // Insert code into the entry block that creates and registers the function
5930 // context.
5931 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5932
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005933 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005934 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005935 MachineMemOperand::MOLoad |
5936 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005937
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005938 if (AFI->isThumb1OnlyFunction())
5939 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5940 else if (!Subtarget->hasVFP2())
5941 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005942 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005943 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005944
Bill Wendling952cb502011-10-18 22:49:07 +00005945 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005946 if (Subtarget->isThumb2()) {
5947 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5948 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5949 .addFrameIndex(FI)
5950 .addImm(4)
5951 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005952
Bill Wendling952cb502011-10-18 22:49:07 +00005953 if (NumLPads < 256) {
5954 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5955 .addReg(NewVReg1)
5956 .addImm(LPadList.size()));
5957 } else {
5958 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5959 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005960 .addImm(NumLPads & 0xFFFF));
5961
5962 unsigned VReg2 = VReg1;
5963 if ((NumLPads & 0xFFFF0000) != 0) {
5964 VReg2 = MRI->createVirtualRegister(TRC);
5965 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5966 .addReg(VReg1)
5967 .addImm(NumLPads >> 16));
5968 }
5969
Bill Wendling952cb502011-10-18 22:49:07 +00005970 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5971 .addReg(NewVReg1)
5972 .addReg(VReg2));
5973 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005974
Bill Wendling95ce2e92011-10-06 22:53:00 +00005975 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5976 .addMBB(TrapBB)
5977 .addImm(ARMCC::HI)
5978 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005979
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005980 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5981 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005982 .addJumpTableIndex(MJTI)
5983 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005984
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005985 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005986 AddDefaultCC(
5987 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005988 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5989 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005990 .addReg(NewVReg1)
5991 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5992
5993 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005994 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005995 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005996 .addJumpTableIndex(MJTI)
5997 .addImm(UId);
5998 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005999 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6000 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6001 .addFrameIndex(FI)
6002 .addImm(1)
6003 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006004
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006005 if (NumLPads < 256) {
6006 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6007 .addReg(NewVReg1)
6008 .addImm(NumLPads));
6009 } else {
6010 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006011 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6012 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6013
6014 // MachineConstantPool wants an explicit alignment.
6015 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6016 if (Align == 0)
6017 Align = getTargetData()->getTypeAllocSize(C->getType());
6018 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006019
6020 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6021 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6022 .addReg(VReg1, RegState::Define)
6023 .addConstantPoolIndex(Idx));
6024 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6025 .addReg(NewVReg1)
6026 .addReg(VReg1));
6027 }
6028
Bill Wendling083a8eb2011-10-06 23:37:36 +00006029 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6030 .addMBB(TrapBB)
6031 .addImm(ARMCC::HI)
6032 .addReg(ARM::CPSR);
6033
6034 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6035 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6036 .addReg(ARM::CPSR, RegState::Define)
6037 .addReg(NewVReg1)
6038 .addImm(2));
6039
6040 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006041 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006042 .addJumpTableIndex(MJTI)
6043 .addImm(UId));
6044
6045 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6046 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6047 .addReg(ARM::CPSR, RegState::Define)
6048 .addReg(NewVReg2, RegState::Kill)
6049 .addReg(NewVReg3));
6050
6051 MachineMemOperand *JTMMOLd =
6052 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6053 MachineMemOperand::MOLoad, 4, 4);
6054
6055 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6056 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6057 .addReg(NewVReg4, RegState::Kill)
6058 .addImm(0)
6059 .addMemOperand(JTMMOLd));
6060
6061 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6062 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6063 .addReg(ARM::CPSR, RegState::Define)
6064 .addReg(NewVReg5, RegState::Kill)
6065 .addReg(NewVReg3));
6066
6067 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6068 .addReg(NewVReg6, RegState::Kill)
6069 .addJumpTableIndex(MJTI)
6070 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006071 } else {
6072 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6073 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6074 .addFrameIndex(FI)
6075 .addImm(4)
6076 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006077
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006078 if (NumLPads < 256) {
6079 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6080 .addReg(NewVReg1)
6081 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006082 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006083 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6084 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006085 .addImm(NumLPads & 0xFFFF));
6086
6087 unsigned VReg2 = VReg1;
6088 if ((NumLPads & 0xFFFF0000) != 0) {
6089 VReg2 = MRI->createVirtualRegister(TRC);
6090 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6091 .addReg(VReg1)
6092 .addImm(NumLPads >> 16));
6093 }
6094
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006095 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6096 .addReg(NewVReg1)
6097 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006098 } else {
6099 MachineConstantPool *ConstantPool = MF->getConstantPool();
6100 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6101 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6102
6103 // MachineConstantPool wants an explicit alignment.
6104 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6105 if (Align == 0)
6106 Align = getTargetData()->getTypeAllocSize(C->getType());
6107 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6108
6109 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6110 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6111 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006112 .addConstantPoolIndex(Idx)
6113 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006114 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6115 .addReg(NewVReg1)
6116 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006117 }
6118
Bill Wendling95ce2e92011-10-06 22:53:00 +00006119 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6120 .addMBB(TrapBB)
6121 .addImm(ARMCC::HI)
6122 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006123
Bill Wendling564392b2011-10-18 22:11:18 +00006124 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006125 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006126 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006127 .addReg(NewVReg1)
6128 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006129 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6130 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006131 .addJumpTableIndex(MJTI)
6132 .addImm(UId));
6133
6134 MachineMemOperand *JTMMOLd =
6135 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6136 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006137 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006138 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006139 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6140 .addReg(NewVReg3, RegState::Kill)
6141 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006142 .addImm(0)
6143 .addMemOperand(JTMMOLd));
6144
6145 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006146 .addReg(NewVReg5, RegState::Kill)
6147 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006148 .addJumpTableIndex(MJTI)
6149 .addImm(UId);
6150 }
Bill Wendling2a850152011-10-05 00:02:33 +00006151
Bill Wendlingbb734682011-10-05 00:39:32 +00006152 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006153 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006154 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006155 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6156 MachineBasicBlock *CurMBB = *I;
6157 if (PrevMBB != CurMBB)
6158 DispContBB->addSuccessor(CurMBB);
6159 PrevMBB = CurMBB;
6160 }
6161
Bill Wendling24bb9252011-10-17 05:25:09 +00006162 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006163 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6164 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006165 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006166 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006167 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6168 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6169 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006170
6171 // Remove the landing pad successor from the invoke block and replace it
6172 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006173 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6174 BB->succ_end());
6175 while (!Successors.empty()) {
6176 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006177 if (SMBB->isLandingPad()) {
6178 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006179 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006180 }
6181 }
6182
6183 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006184
6185 // Find the invoke call and mark all of the callee-saved registers as
6186 // 'implicit defined' so that they're spilled. This prevents code from
6187 // moving instructions to before the EH block, where they will never be
6188 // executed.
6189 for (MachineBasicBlock::reverse_iterator
6190 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006191 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006192
6193 DenseMap<unsigned, bool> DefRegs;
6194 for (MachineInstr::mop_iterator
6195 OI = II->operands_begin(), OE = II->operands_end();
6196 OI != OE; ++OI) {
6197 if (!OI->isReg()) continue;
6198 DefRegs[OI->getReg()] = true;
6199 }
6200
6201 MachineInstrBuilder MIB(&*II);
6202
Bill Wendling5d798592011-10-14 23:55:44 +00006203 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006204 unsigned Reg = SavedRegs[i];
6205 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006206 !ARM::tGPRRegClass.contains(Reg) &&
6207 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006208 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006209 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006210 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006211 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006212 continue;
6213 if (!DefRegs[Reg])
6214 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006215 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006216
6217 break;
6218 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006219 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006220
Bill Wendlingf7b02072011-10-18 18:30:49 +00006221 // Mark all former landing pads as non-landing pads. The dispatch is the only
6222 // landing pad now.
6223 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6224 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6225 (*I)->setIsLandingPad(false);
6226
Bill Wendlingbb734682011-10-05 00:39:32 +00006227 // The instruction is gone now.
6228 MI->eraseFromParent();
6229
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006230 return MBB;
6231}
6232
Evan Cheng218977b2010-07-13 19:27:42 +00006233static
6234MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6235 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6236 E = MBB->succ_end(); I != E; ++I)
6237 if (*I != Succ)
6238 return *I;
6239 llvm_unreachable("Expecting a BB with two successors!");
6240}
6241
Jim Grosbache801dc42009-12-12 01:40:06 +00006242MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006243ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006244 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006245 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006246 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006247 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006248 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006249 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006250 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006251 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006252 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006253 // The Thumb2 pre-indexed stores have the same MI operands, they just
6254 // define them differently in the .td files from the isel patterns, so
6255 // they need pseudos.
6256 case ARM::t2STR_preidx:
6257 MI->setDesc(TII->get(ARM::t2STR_PRE));
6258 return BB;
6259 case ARM::t2STRB_preidx:
6260 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6261 return BB;
6262 case ARM::t2STRH_preidx:
6263 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6264 return BB;
6265
Jim Grosbach19dec202011-08-05 20:35:44 +00006266 case ARM::STRi_preidx:
6267 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006268 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006269 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6270 // Decode the offset.
6271 unsigned Offset = MI->getOperand(4).getImm();
6272 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6273 Offset = ARM_AM::getAM2Offset(Offset);
6274 if (isSub)
6275 Offset = -Offset;
6276
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006277 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006278 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006279 .addOperand(MI->getOperand(0)) // Rn_wb
6280 .addOperand(MI->getOperand(1)) // Rt
6281 .addOperand(MI->getOperand(2)) // Rn
6282 .addImm(Offset) // offset (skip GPR==zero_reg)
6283 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006284 .addOperand(MI->getOperand(6))
6285 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006286 MI->eraseFromParent();
6287 return BB;
6288 }
6289 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006290 case ARM::STRBr_preidx:
6291 case ARM::STRH_preidx: {
6292 unsigned NewOpc;
6293 switch (MI->getOpcode()) {
6294 default: llvm_unreachable("unexpected opcode!");
6295 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6296 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6297 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6298 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006299 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6300 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6301 MIB.addOperand(MI->getOperand(i));
6302 MI->eraseFromParent();
6303 return BB;
6304 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006305 case ARM::ATOMIC_LOAD_ADD_I8:
6306 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6307 case ARM::ATOMIC_LOAD_ADD_I16:
6308 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6309 case ARM::ATOMIC_LOAD_ADD_I32:
6310 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006311
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006312 case ARM::ATOMIC_LOAD_AND_I8:
6313 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6314 case ARM::ATOMIC_LOAD_AND_I16:
6315 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6316 case ARM::ATOMIC_LOAD_AND_I32:
6317 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006318
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006319 case ARM::ATOMIC_LOAD_OR_I8:
6320 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6321 case ARM::ATOMIC_LOAD_OR_I16:
6322 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6323 case ARM::ATOMIC_LOAD_OR_I32:
6324 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006325
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006326 case ARM::ATOMIC_LOAD_XOR_I8:
6327 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6328 case ARM::ATOMIC_LOAD_XOR_I16:
6329 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6330 case ARM::ATOMIC_LOAD_XOR_I32:
6331 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006332
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006333 case ARM::ATOMIC_LOAD_NAND_I8:
6334 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6335 case ARM::ATOMIC_LOAD_NAND_I16:
6336 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6337 case ARM::ATOMIC_LOAD_NAND_I32:
6338 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006339
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006340 case ARM::ATOMIC_LOAD_SUB_I8:
6341 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6342 case ARM::ATOMIC_LOAD_SUB_I16:
6343 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6344 case ARM::ATOMIC_LOAD_SUB_I32:
6345 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006346
Jim Grosbachf7da8822011-04-26 19:44:18 +00006347 case ARM::ATOMIC_LOAD_MIN_I8:
6348 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6349 case ARM::ATOMIC_LOAD_MIN_I16:
6350 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6351 case ARM::ATOMIC_LOAD_MIN_I32:
6352 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6353
6354 case ARM::ATOMIC_LOAD_MAX_I8:
6355 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6356 case ARM::ATOMIC_LOAD_MAX_I16:
6357 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6358 case ARM::ATOMIC_LOAD_MAX_I32:
6359 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6360
6361 case ARM::ATOMIC_LOAD_UMIN_I8:
6362 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6363 case ARM::ATOMIC_LOAD_UMIN_I16:
6364 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6365 case ARM::ATOMIC_LOAD_UMIN_I32:
6366 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6367
6368 case ARM::ATOMIC_LOAD_UMAX_I8:
6369 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6370 case ARM::ATOMIC_LOAD_UMAX_I16:
6371 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6372 case ARM::ATOMIC_LOAD_UMAX_I32:
6373 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6374
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006375 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6376 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6377 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006378
6379 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6380 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6381 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006382
Eli Friedman2bdffe42011-08-31 00:31:29 +00006383
6384 case ARM::ATOMADD6432:
6385 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006386 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6387 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006388 case ARM::ATOMSUB6432:
6389 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006390 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6391 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006392 case ARM::ATOMOR6432:
6393 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006394 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006395 case ARM::ATOMXOR6432:
6396 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006397 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006398 case ARM::ATOMAND6432:
6399 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006400 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006401 case ARM::ATOMSWAP6432:
6402 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006403 case ARM::ATOMCMPXCHG6432:
6404 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6405 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6406 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006407
Evan Cheng007ea272009-08-12 05:17:19 +00006408 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006409 // To "insert" a SELECT_CC instruction, we actually have to insert the
6410 // diamond control-flow pattern. The incoming instruction knows the
6411 // destination vreg to set, the condition code register to branch on, the
6412 // true/false values to select between, and a branch opcode to use.
6413 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006414 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006415 ++It;
6416
6417 // thisMBB:
6418 // ...
6419 // TrueVal = ...
6420 // cmpTY ccX, r1, r2
6421 // bCC copy1MBB
6422 // fallthrough --> copy0MBB
6423 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006424 MachineFunction *F = BB->getParent();
6425 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6426 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006427 F->insert(It, copy0MBB);
6428 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006429
6430 // Transfer the remainder of BB and its successor edges to sinkMBB.
6431 sinkMBB->splice(sinkMBB->begin(), BB,
6432 llvm::next(MachineBasicBlock::iterator(MI)),
6433 BB->end());
6434 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6435
Dan Gohman258c58c2010-07-06 15:49:48 +00006436 BB->addSuccessor(copy0MBB);
6437 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006438
Dan Gohman14152b42010-07-06 20:24:04 +00006439 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6440 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6441
Evan Chenga8e29892007-01-19 07:51:42 +00006442 // copy0MBB:
6443 // %FalseValue = ...
6444 // # fallthrough to sinkMBB
6445 BB = copy0MBB;
6446
6447 // Update machine-CFG edges
6448 BB->addSuccessor(sinkMBB);
6449
6450 // sinkMBB:
6451 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6452 // ...
6453 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006454 BuildMI(*BB, BB->begin(), dl,
6455 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006456 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6457 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6458
Dan Gohman14152b42010-07-06 20:24:04 +00006459 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006460 return BB;
6461 }
Evan Cheng86198642009-08-07 00:34:42 +00006462
Evan Cheng218977b2010-07-13 19:27:42 +00006463 case ARM::BCCi64:
6464 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006465 // If there is an unconditional branch to the other successor, remove it.
6466 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006467
Evan Cheng218977b2010-07-13 19:27:42 +00006468 // Compare both parts that make up the double comparison separately for
6469 // equality.
6470 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6471
6472 unsigned LHS1 = MI->getOperand(1).getReg();
6473 unsigned LHS2 = MI->getOperand(2).getReg();
6474 if (RHSisZero) {
6475 AddDefaultPred(BuildMI(BB, dl,
6476 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6477 .addReg(LHS1).addImm(0));
6478 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6479 .addReg(LHS2).addImm(0)
6480 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6481 } else {
6482 unsigned RHS1 = MI->getOperand(3).getReg();
6483 unsigned RHS2 = MI->getOperand(4).getReg();
6484 AddDefaultPred(BuildMI(BB, dl,
6485 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6486 .addReg(LHS1).addReg(RHS1));
6487 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6488 .addReg(LHS2).addReg(RHS2)
6489 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6490 }
6491
6492 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6493 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6494 if (MI->getOperand(0).getImm() == ARMCC::NE)
6495 std::swap(destMBB, exitMBB);
6496
6497 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6498 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006499 if (isThumb2)
6500 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6501 else
6502 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006503
6504 MI->eraseFromParent(); // The pseudo instruction is gone now.
6505 return BB;
6506 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006507
Bill Wendling5bc85282011-10-17 20:37:20 +00006508 case ARM::Int_eh_sjlj_setjmp:
6509 case ARM::Int_eh_sjlj_setjmp_nofp:
6510 case ARM::tInt_eh_sjlj_setjmp:
6511 case ARM::t2Int_eh_sjlj_setjmp:
6512 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6513 EmitSjLjDispatchBlock(MI, BB);
6514 return BB;
6515
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006516 case ARM::ABS:
6517 case ARM::t2ABS: {
6518 // To insert an ABS instruction, we have to insert the
6519 // diamond control-flow pattern. The incoming instruction knows the
6520 // source vreg to test against 0, the destination vreg to set,
6521 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006522 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006523 // It transforms
6524 // V1 = ABS V0
6525 // into
6526 // V2 = MOVS V0
6527 // BCC (branch to SinkBB if V0 >= 0)
6528 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006529 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006530 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6531 MachineFunction::iterator BBI = BB;
6532 ++BBI;
6533 MachineFunction *Fn = BB->getParent();
6534 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6535 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6536 Fn->insert(BBI, RSBBB);
6537 Fn->insert(BBI, SinkBB);
6538
6539 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6540 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6541 bool isThumb2 = Subtarget->isThumb2();
6542 MachineRegisterInfo &MRI = Fn->getRegInfo();
6543 // In Thumb mode S must not be specified if source register is the SP or
6544 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006545 unsigned NewMovDstReg = MRI.createVirtualRegister(isThumb2 ?
6546 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6547 (const TargetRegisterClass*)&ARM::GPRRegClass);
6548 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6549 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6550 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006551
6552 // Transfer the remainder of BB and its successor edges to sinkMBB.
6553 SinkBB->splice(SinkBB->begin(), BB,
6554 llvm::next(MachineBasicBlock::iterator(MI)),
6555 BB->end());
6556 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6557
6558 BB->addSuccessor(RSBBB);
6559 BB->addSuccessor(SinkBB);
6560
6561 // fall through to SinkMBB
6562 RSBBB->addSuccessor(SinkBB);
6563
6564 // insert a movs at the end of BB
6565 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6566 NewMovDstReg)
6567 .addReg(ABSSrcReg, RegState::Kill)
6568 .addImm((unsigned)ARMCC::AL).addReg(0)
6569 .addReg(ARM::CPSR, RegState::Define);
6570
6571 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006572 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006573 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6574 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6575
6576 // insert rsbri in RSBBB
6577 // Note: BCC and rsbri will be converted into predicated rsbmi
6578 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006579 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006580 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6581 .addReg(NewMovDstReg, RegState::Kill)
6582 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6583
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006584 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006585 // reuse ABSDstReg to not change uses of ABS instruction
6586 BuildMI(*SinkBB, SinkBB->begin(), dl,
6587 TII->get(ARM::PHI), ABSDstReg)
6588 .addReg(NewRsbDstReg).addMBB(RSBBB)
6589 .addReg(NewMovDstReg).addMBB(BB);
6590
6591 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006592 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006593
6594 // return last added BB
6595 return SinkBB;
6596 }
Manman Ren763a75d2012-06-01 02:44:42 +00006597 case ARM::COPY_STRUCT_BYVAL_I32: {
6598 ++NumLoopByVals;
6599 // This pseudo instruction has 3 operands: dst, src, size
6600 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6601 // Otherwise, we will generate unrolled scalar copies.
6602 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6603 MachineFunction::iterator It = BB;
6604 ++It;
6605
6606 unsigned dest = MI->getOperand(0).getReg();
6607 unsigned src = MI->getOperand(1).getReg();
6608 unsigned size = MI->getOperand(2).getImm();
6609 DebugLoc dl = MI->getDebugLoc();
6610 unsigned BytesLeft = size & 3;
6611 unsigned LoopSize = size - BytesLeft;
6612
6613 bool isThumb2 = Subtarget->isThumb2();
6614 MachineFunction *MF = BB->getParent();
6615 MachineRegisterInfo &MRI = MF->getRegInfo();
6616 unsigned ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6617 unsigned strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6618
6619 const TargetRegisterClass *TRC = isThumb2 ?
6620 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6621 (const TargetRegisterClass*)&ARM::GPRRegClass;
6622
6623 if (size <= Subtarget->getMaxInlineSizeThreshold()) {
6624 // Use LDR and STR to copy.
6625 // [scratch, srcOut] = LDR_POST(srcIn, 4)
6626 // [destOut] = STR_POST(scratch, destIn, 4)
6627 unsigned srcIn = src;
6628 unsigned destIn = dest;
6629 for (unsigned i = 0; i < LoopSize; i+=4) {
6630 unsigned scratch = MRI.createVirtualRegister(TRC);
6631 unsigned srcOut = MRI.createVirtualRegister(TRC);
6632 unsigned destOut = MRI.createVirtualRegister(TRC);
6633 if (isThumb2) {
6634 AddDefaultPred(BuildMI(*BB, MI, dl,
6635 TII->get(ldrOpc), scratch)
6636 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(4));
6637
6638 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6639 .addReg(scratch).addReg(destIn)
6640 .addImm(4));
6641 } else {
6642 AddDefaultPred(BuildMI(*BB, MI, dl,
6643 TII->get(ldrOpc), scratch)
6644 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(4));
6645
6646 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6647 .addReg(scratch).addReg(destIn)
6648 .addReg(0).addImm(4));
6649 }
6650 srcIn = srcOut;
6651 destIn = destOut;
6652 }
6653
6654 // Handle the leftover bytes with LDRB and STRB.
6655 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6656 // [destOut] = STRB_POST(scratch, destIn, 1)
6657 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6658 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6659 for (unsigned i = 0; i < BytesLeft; i++) {
6660 unsigned scratch = MRI.createVirtualRegister(TRC);
6661 unsigned srcOut = MRI.createVirtualRegister(TRC);
6662 unsigned destOut = MRI.createVirtualRegister(TRC);
6663 if (isThumb2) {
6664 AddDefaultPred(BuildMI(*BB, MI, dl,
6665 TII->get(ldrOpc),scratch)
6666 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6667
6668 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6669 .addReg(scratch).addReg(destIn)
6670 .addReg(0).addImm(1));
6671 } else {
6672 AddDefaultPred(BuildMI(*BB, MI, dl,
6673 TII->get(ldrOpc),scratch)
6674 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6675
6676 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6677 .addReg(scratch).addReg(destIn)
6678 .addReg(0).addImm(1));
6679 }
6680 srcIn = srcOut;
6681 destIn = destOut;
6682 }
6683 MI->eraseFromParent(); // The instruction is gone now.
6684 return BB;
6685 }
6686
6687 // Expand the pseudo op to a loop.
6688 // thisMBB:
6689 // ...
6690 // movw varEnd, # --> with thumb2
6691 // movt varEnd, #
6692 // ldrcp varEnd, idx --> without thumb2
6693 // fallthrough --> loopMBB
6694 // loopMBB:
6695 // PHI varPhi, varEnd, varLoop
6696 // PHI srcPhi, src, srcLoop
6697 // PHI destPhi, dst, destLoop
6698 // [scratch, srcLoop] = LDR_POST(srcPhi, 4)
6699 // [destLoop] = STR_POST(scratch, destPhi, 4)
6700 // subs varLoop, varPhi, #4
6701 // bne loopMBB
6702 // fallthrough --> exitMBB
6703 // exitMBB:
6704 // epilogue to handle left-over bytes
6705 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6706 // [destOut] = STRB_POST(scratch, destLoop, 1)
6707 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6708 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6709 MF->insert(It, loopMBB);
6710 MF->insert(It, exitMBB);
6711
6712 // Transfer the remainder of BB and its successor edges to exitMBB.
6713 exitMBB->splice(exitMBB->begin(), BB,
6714 llvm::next(MachineBasicBlock::iterator(MI)),
6715 BB->end());
6716 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6717
6718 // Load an immediate to varEnd.
6719 unsigned varEnd = MRI.createVirtualRegister(TRC);
6720 if (isThumb2) {
6721 unsigned VReg1 = varEnd;
6722 if ((LoopSize & 0xFFFF0000) != 0)
6723 VReg1 = MRI.createVirtualRegister(TRC);
6724 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6725 .addImm(LoopSize & 0xFFFF));
6726
6727 if ((LoopSize & 0xFFFF0000) != 0)
6728 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6729 .addReg(VReg1)
6730 .addImm(LoopSize >> 16));
6731 } else {
6732 MachineConstantPool *ConstantPool = MF->getConstantPool();
6733 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6734 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6735
6736 // MachineConstantPool wants an explicit alignment.
6737 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6738 if (Align == 0)
6739 Align = getTargetData()->getTypeAllocSize(C->getType());
6740 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6741
6742 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6743 .addReg(varEnd, RegState::Define)
6744 .addConstantPoolIndex(Idx)
6745 .addImm(0));
6746 }
6747 BB->addSuccessor(loopMBB);
6748
6749 // Generate the loop body:
6750 // varPhi = PHI(varLoop, varEnd)
6751 // srcPhi = PHI(srcLoop, src)
6752 // destPhi = PHI(destLoop, dst)
6753 MachineBasicBlock *entryBB = BB;
6754 BB = loopMBB;
6755 unsigned varLoop = MRI.createVirtualRegister(TRC);
6756 unsigned varPhi = MRI.createVirtualRegister(TRC);
6757 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6758 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6759 unsigned destLoop = MRI.createVirtualRegister(TRC);
6760 unsigned destPhi = MRI.createVirtualRegister(TRC);
6761
6762 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6763 .addReg(varLoop).addMBB(loopMBB)
6764 .addReg(varEnd).addMBB(entryBB);
6765 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6766 .addReg(srcLoop).addMBB(loopMBB)
6767 .addReg(src).addMBB(entryBB);
6768 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6769 .addReg(destLoop).addMBB(loopMBB)
6770 .addReg(dest).addMBB(entryBB);
6771
6772 // [scratch, srcLoop] = LDR_POST(srcPhi, 4)
6773 // [destLoop] = STR_POST(scratch, destPhi, 4)
6774 unsigned scratch = MRI.createVirtualRegister(TRC);
6775 if (isThumb2) {
6776 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6777 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(4));
6778
6779 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6780 .addReg(scratch).addReg(destPhi)
6781 .addImm(4));
6782 } else {
6783 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6784 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0).addImm(4));
6785
6786 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6787 .addReg(scratch).addReg(destPhi)
6788 .addReg(0).addImm(4));
6789 }
6790
6791 // Decrement loop variable by 4.
6792 MachineInstrBuilder MIB = BuildMI(BB, dl,
6793 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6794 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(4)));
6795 MIB->getOperand(5).setReg(ARM::CPSR);
6796 MIB->getOperand(5).setIsDef(true);
6797
6798 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6799 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6800
6801 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6802 BB->addSuccessor(loopMBB);
6803 BB->addSuccessor(exitMBB);
6804
6805 // Add epilogue to handle BytesLeft.
6806 BB = exitMBB;
6807 MachineInstr *StartOfExit = exitMBB->begin();
6808 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6809 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6810
6811 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6812 // [destOut] = STRB_POST(scratch, destLoop, 1)
6813 unsigned srcIn = srcLoop;
6814 unsigned destIn = destLoop;
6815 for (unsigned i = 0; i < BytesLeft; i++) {
6816 unsigned scratch = MRI.createVirtualRegister(TRC);
6817 unsigned srcOut = MRI.createVirtualRegister(TRC);
6818 unsigned destOut = MRI.createVirtualRegister(TRC);
6819 if (isThumb2) {
6820 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6821 TII->get(ldrOpc),scratch)
6822 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6823
6824 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6825 .addReg(scratch).addReg(destIn)
6826 .addImm(1));
6827 } else {
6828 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6829 TII->get(ldrOpc),scratch)
6830 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6831
6832 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6833 .addReg(scratch).addReg(destIn)
6834 .addReg(0).addImm(1));
6835 }
6836 srcIn = srcOut;
6837 destIn = destOut;
6838 }
6839
6840 MI->eraseFromParent(); // The instruction is gone now.
6841 return BB;
6842 }
Evan Chenga8e29892007-01-19 07:51:42 +00006843 }
6844}
6845
Evan Cheng37fefc22011-08-30 19:09:48 +00006846void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6847 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006848 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006849 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6850 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6851 return;
6852 }
6853
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006854 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006855 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6856 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6857 // operand is still set to noreg. If needed, set the optional operand's
6858 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006859 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006860 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006861
Andrew Trick3be654f2011-09-21 02:20:46 +00006862 // Rename pseudo opcodes.
6863 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6864 if (NewOpc) {
6865 const ARMBaseInstrInfo *TII =
6866 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006867 MCID = &TII->get(NewOpc);
6868
6869 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6870 "converted opcode should be the same except for cc_out");
6871
6872 MI->setDesc(*MCID);
6873
6874 // Add the optional cc_out operand
6875 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006876 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006877 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006878
6879 // Any ARM instruction that sets the 's' bit should specify an optional
6880 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006881 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006882 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006883 return;
6884 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006885 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6886 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006887 bool definesCPSR = false;
6888 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006889 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006890 i != e; ++i) {
6891 const MachineOperand &MO = MI->getOperand(i);
6892 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6893 definesCPSR = true;
6894 if (MO.isDead())
6895 deadCPSR = true;
6896 MI->RemoveOperand(i);
6897 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006898 }
6899 }
Andrew Trick4815d562011-09-20 03:17:40 +00006900 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006901 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006902 return;
6903 }
6904 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006905 if (deadCPSR) {
6906 assert(!MI->getOperand(ccOutIdx).getReg() &&
6907 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006908 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006909 }
Andrew Trick4815d562011-09-20 03:17:40 +00006910
Andrew Trick3be654f2011-09-21 02:20:46 +00006911 // If this instruction was defined with an optional CPSR def and its dag node
6912 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006913 MachineOperand &MO = MI->getOperand(ccOutIdx);
6914 MO.setReg(ARM::CPSR);
6915 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006916}
6917
Evan Chenga8e29892007-01-19 07:51:42 +00006918//===----------------------------------------------------------------------===//
6919// ARM Optimization Hooks
6920//===----------------------------------------------------------------------===//
6921
Chris Lattnerd1980a52009-03-12 06:52:53 +00006922static
6923SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6924 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006925 SelectionDAG &DAG = DCI.DAG;
6926 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006927 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006928 unsigned Opc = N->getOpcode();
6929 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6930 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6931 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6932 ISD::CondCode CC = ISD::SETCC_INVALID;
6933
6934 if (isSlctCC) {
6935 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6936 } else {
6937 SDValue CCOp = Slct.getOperand(0);
6938 if (CCOp.getOpcode() == ISD::SETCC)
6939 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6940 }
6941
6942 bool DoXform = false;
6943 bool InvCC = false;
6944 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6945 "Bad input!");
6946
6947 if (LHS.getOpcode() == ISD::Constant &&
6948 cast<ConstantSDNode>(LHS)->isNullValue()) {
6949 DoXform = true;
6950 } else if (CC != ISD::SETCC_INVALID &&
6951 RHS.getOpcode() == ISD::Constant &&
6952 cast<ConstantSDNode>(RHS)->isNullValue()) {
6953 std::swap(LHS, RHS);
6954 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006955 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006956 Op0.getOperand(0).getValueType();
6957 bool isInt = OpVT.isInteger();
6958 CC = ISD::getSetCCInverse(CC, isInt);
6959
6960 if (!TLI.isCondCodeLegal(CC, OpVT))
6961 return SDValue(); // Inverse operator isn't legal.
6962
6963 DoXform = true;
6964 InvCC = true;
6965 }
6966
6967 if (DoXform) {
6968 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6969 if (isSlctCC)
6970 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6971 Slct.getOperand(0), Slct.getOperand(1), CC);
6972 SDValue CCOp = Slct.getOperand(0);
6973 if (InvCC)
6974 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6975 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6976 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6977 CCOp, OtherOp, Result);
6978 }
6979 return SDValue();
6980}
6981
Eric Christopherfa6f5912011-06-29 21:10:36 +00006982// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006983// (only after legalization).
6984static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6985 TargetLowering::DAGCombinerInfo &DCI,
6986 const ARMSubtarget *Subtarget) {
6987
6988 // Only perform optimization if after legalize, and if NEON is available. We
6989 // also expected both operands to be BUILD_VECTORs.
6990 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6991 || N0.getOpcode() != ISD::BUILD_VECTOR
6992 || N1.getOpcode() != ISD::BUILD_VECTOR)
6993 return SDValue();
6994
6995 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6996 EVT VT = N->getValueType(0);
6997 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6998 return SDValue();
6999
7000 // Check that the vector operands are of the right form.
7001 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7002 // operands, where N is the size of the formed vector.
7003 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7004 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007005
7006 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007007 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007008 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007009 SDValue Vec = N0->getOperand(0)->getOperand(0);
7010 SDNode *V = Vec.getNode();
7011 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007012
Eric Christopherfa6f5912011-06-29 21:10:36 +00007013 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007014 // check to see if each of their operands are an EXTRACT_VECTOR with
7015 // the same vector and appropriate index.
7016 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7017 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7018 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007019
Tanya Lattner189531f2011-06-14 23:48:48 +00007020 SDValue ExtVec0 = N0->getOperand(i);
7021 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007022
Tanya Lattner189531f2011-06-14 23:48:48 +00007023 // First operand is the vector, verify its the same.
7024 if (V != ExtVec0->getOperand(0).getNode() ||
7025 V != ExtVec1->getOperand(0).getNode())
7026 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007027
Tanya Lattner189531f2011-06-14 23:48:48 +00007028 // Second is the constant, verify its correct.
7029 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7030 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007031
Tanya Lattner189531f2011-06-14 23:48:48 +00007032 // For the constant, we want to see all the even or all the odd.
7033 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7034 || C1->getZExtValue() != nextIndex+1)
7035 return SDValue();
7036
7037 // Increment index.
7038 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007039 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007040 return SDValue();
7041 }
7042
7043 // Create VPADDL node.
7044 SelectionDAG &DAG = DCI.DAG;
7045 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007046
7047 // Build operand list.
7048 SmallVector<SDValue, 8> Ops;
7049 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7050 TLI.getPointerTy()));
7051
7052 // Input is the vector.
7053 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007054
Tanya Lattner189531f2011-06-14 23:48:48 +00007055 // Get widened type and narrowed type.
7056 MVT widenType;
7057 unsigned numElem = VT.getVectorNumElements();
7058 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7059 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7060 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7061 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7062 default:
Craig Topperbc219812012-02-07 02:50:20 +00007063 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007064 }
7065
7066 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7067 widenType, &Ops[0], Ops.size());
7068 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7069}
7070
Bob Wilson3d5792a2010-07-29 20:34:14 +00007071/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7072/// operands N0 and N1. This is a helper for PerformADDCombine that is
7073/// called with the default operands, and if that fails, with commuted
7074/// operands.
7075static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007076 TargetLowering::DAGCombinerInfo &DCI,
7077 const ARMSubtarget *Subtarget){
7078
7079 // Attempt to create vpaddl for this add.
7080 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7081 if (Result.getNode())
7082 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007083
Chris Lattnerd1980a52009-03-12 06:52:53 +00007084 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7085 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7086 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7087 if (Result.getNode()) return Result;
7088 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007089 return SDValue();
7090}
7091
Bob Wilson3d5792a2010-07-29 20:34:14 +00007092/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7093///
7094static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007095 TargetLowering::DAGCombinerInfo &DCI,
7096 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007097 SDValue N0 = N->getOperand(0);
7098 SDValue N1 = N->getOperand(1);
7099
7100 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007101 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007102 if (Result.getNode())
7103 return Result;
7104
7105 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007106 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007107}
7108
Chris Lattnerd1980a52009-03-12 06:52:53 +00007109/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007110///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007111static SDValue PerformSUBCombine(SDNode *N,
7112 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007113 SDValue N0 = N->getOperand(0);
7114 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007115
Chris Lattnerd1980a52009-03-12 06:52:53 +00007116 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7117 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7118 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7119 if (Result.getNode()) return Result;
7120 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007121
Chris Lattnerd1980a52009-03-12 06:52:53 +00007122 return SDValue();
7123}
7124
Evan Cheng463d3582011-03-31 19:38:48 +00007125/// PerformVMULCombine
7126/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7127/// special multiplier accumulator forwarding.
7128/// vmul d3, d0, d2
7129/// vmla d3, d1, d2
7130/// is faster than
7131/// vadd d3, d0, d1
7132/// vmul d3, d3, d2
7133static SDValue PerformVMULCombine(SDNode *N,
7134 TargetLowering::DAGCombinerInfo &DCI,
7135 const ARMSubtarget *Subtarget) {
7136 if (!Subtarget->hasVMLxForwarding())
7137 return SDValue();
7138
7139 SelectionDAG &DAG = DCI.DAG;
7140 SDValue N0 = N->getOperand(0);
7141 SDValue N1 = N->getOperand(1);
7142 unsigned Opcode = N0.getOpcode();
7143 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7144 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007145 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007146 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7147 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7148 return SDValue();
7149 std::swap(N0, N1);
7150 }
7151
7152 EVT VT = N->getValueType(0);
7153 DebugLoc DL = N->getDebugLoc();
7154 SDValue N00 = N0->getOperand(0);
7155 SDValue N01 = N0->getOperand(1);
7156 return DAG.getNode(Opcode, DL, VT,
7157 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7158 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7159}
7160
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007161static SDValue PerformMULCombine(SDNode *N,
7162 TargetLowering::DAGCombinerInfo &DCI,
7163 const ARMSubtarget *Subtarget) {
7164 SelectionDAG &DAG = DCI.DAG;
7165
7166 if (Subtarget->isThumb1Only())
7167 return SDValue();
7168
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007169 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7170 return SDValue();
7171
7172 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007173 if (VT.is64BitVector() || VT.is128BitVector())
7174 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007175 if (VT != MVT::i32)
7176 return SDValue();
7177
7178 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7179 if (!C)
7180 return SDValue();
7181
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007182 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007183 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007184
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007185 ShiftAmt = ShiftAmt & (32 - 1);
7186 SDValue V = N->getOperand(0);
7187 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007188
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007189 SDValue Res;
7190 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007191
7192 if (MulAmt >= 0) {
7193 if (isPowerOf2_32(MulAmt - 1)) {
7194 // (mul x, 2^N + 1) => (add (shl x, N), x)
7195 Res = DAG.getNode(ISD::ADD, DL, VT,
7196 V,
7197 DAG.getNode(ISD::SHL, DL, VT,
7198 V,
7199 DAG.getConstant(Log2_32(MulAmt - 1),
7200 MVT::i32)));
7201 } else if (isPowerOf2_32(MulAmt + 1)) {
7202 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7203 Res = DAG.getNode(ISD::SUB, DL, VT,
7204 DAG.getNode(ISD::SHL, DL, VT,
7205 V,
7206 DAG.getConstant(Log2_32(MulAmt + 1),
7207 MVT::i32)),
7208 V);
7209 } else
7210 return SDValue();
7211 } else {
7212 uint64_t MulAmtAbs = -MulAmt;
7213 if (isPowerOf2_32(MulAmtAbs + 1)) {
7214 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7215 Res = DAG.getNode(ISD::SUB, DL, VT,
7216 V,
7217 DAG.getNode(ISD::SHL, DL, VT,
7218 V,
7219 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7220 MVT::i32)));
7221 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7222 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7223 Res = DAG.getNode(ISD::ADD, DL, VT,
7224 V,
7225 DAG.getNode(ISD::SHL, DL, VT,
7226 V,
7227 DAG.getConstant(Log2_32(MulAmtAbs-1),
7228 MVT::i32)));
7229 Res = DAG.getNode(ISD::SUB, DL, VT,
7230 DAG.getConstant(0, MVT::i32),Res);
7231
7232 } else
7233 return SDValue();
7234 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007235
7236 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007237 Res = DAG.getNode(ISD::SHL, DL, VT,
7238 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007239
7240 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007241 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007242 return SDValue();
7243}
7244
Evan Chengc892aeb2012-02-23 01:19:06 +00007245static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
7246 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
7247 return false;
7248
7249 SDValue FalseVal = N.getOperand(0);
7250 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
7251 if (!C)
7252 return false;
7253 if (AllOnes)
7254 return C->isAllOnesValue();
7255 return C->isNullValue();
7256}
7257
7258/// formConditionalOp - Combine an operation with a conditional move operand
7259/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7260/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7261static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7262 bool Commutable) {
7263 SDValue N0 = N->getOperand(0);
7264 SDValue N1 = N->getOperand(1);
7265
7266 bool isAND = N->getOpcode() == ISD::AND;
7267 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7268 if (!isCand && Commutable) {
7269 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7270 if (isCand)
7271 std::swap(N0, N1);
7272 }
7273 if (!isCand)
7274 return SDValue();
7275
7276 unsigned Opc = 0;
7277 switch (N->getOpcode()) {
7278 default: llvm_unreachable("Unexpected node");
7279 case ISD::AND: Opc = ARMISD::CAND; break;
7280 case ISD::OR: Opc = ARMISD::COR; break;
7281 case ISD::XOR: Opc = ARMISD::CXOR; break;
7282 }
7283 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7284 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7285 N1.getOperand(4));
7286}
7287
Owen Anderson080c0922010-11-05 19:27:46 +00007288static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007289 TargetLowering::DAGCombinerInfo &DCI,
7290 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007291
Owen Anderson080c0922010-11-05 19:27:46 +00007292 // Attempt to use immediate-form VBIC
7293 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7294 DebugLoc dl = N->getDebugLoc();
7295 EVT VT = N->getValueType(0);
7296 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007297
Tanya Lattner0433b212011-04-07 15:24:20 +00007298 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7299 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007300
Owen Anderson080c0922010-11-05 19:27:46 +00007301 APInt SplatBits, SplatUndef;
7302 unsigned SplatBitSize;
7303 bool HasAnyUndefs;
7304 if (BVN &&
7305 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7306 if (SplatBitSize <= 64) {
7307 EVT VbicVT;
7308 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7309 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007310 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007311 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007312 if (Val.getNode()) {
7313 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007314 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007315 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007316 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007317 }
7318 }
7319 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007320
Evan Chengc892aeb2012-02-23 01:19:06 +00007321 if (!Subtarget->isThumb1Only()) {
7322 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7323 SDValue CAND = formConditionalOp(N, DAG, true);
7324 if (CAND.getNode())
7325 return CAND;
7326 }
7327
Owen Anderson080c0922010-11-05 19:27:46 +00007328 return SDValue();
7329}
7330
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007331/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7332static SDValue PerformORCombine(SDNode *N,
7333 TargetLowering::DAGCombinerInfo &DCI,
7334 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007335 // Attempt to use immediate-form VORR
7336 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7337 DebugLoc dl = N->getDebugLoc();
7338 EVT VT = N->getValueType(0);
7339 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007340
Tanya Lattner0433b212011-04-07 15:24:20 +00007341 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7342 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007343
Owen Anderson60f48702010-11-03 23:15:26 +00007344 APInt SplatBits, SplatUndef;
7345 unsigned SplatBitSize;
7346 bool HasAnyUndefs;
7347 if (BVN && Subtarget->hasNEON() &&
7348 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7349 if (SplatBitSize <= 64) {
7350 EVT VorrVT;
7351 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7352 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007353 DAG, VorrVT, VT.is128BitVector(),
7354 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007355 if (Val.getNode()) {
7356 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007357 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007358 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007359 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007360 }
7361 }
7362 }
7363
Evan Chengc892aeb2012-02-23 01:19:06 +00007364 if (!Subtarget->isThumb1Only()) {
7365 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7366 SDValue COR = formConditionalOp(N, DAG, true);
7367 if (COR.getNode())
7368 return COR;
7369 }
7370
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007371 SDValue N0 = N->getOperand(0);
7372 if (N0.getOpcode() != ISD::AND)
7373 return SDValue();
7374 SDValue N1 = N->getOperand(1);
7375
7376 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7377 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7378 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7379 APInt SplatUndef;
7380 unsigned SplatBitSize;
7381 bool HasAnyUndefs;
7382
7383 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7384 APInt SplatBits0;
7385 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7386 HasAnyUndefs) && !HasAnyUndefs) {
7387 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7388 APInt SplatBits1;
7389 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7390 HasAnyUndefs) && !HasAnyUndefs &&
7391 SplatBits0 == ~SplatBits1) {
7392 // Canonicalize the vector type to make instruction selection simpler.
7393 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7394 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7395 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007396 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007397 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7398 }
7399 }
7400 }
7401
Jim Grosbach54238562010-07-17 03:30:54 +00007402 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7403 // reasonable.
7404
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007405 // BFI is only available on V6T2+
7406 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7407 return SDValue();
7408
Jim Grosbach54238562010-07-17 03:30:54 +00007409 DebugLoc DL = N->getDebugLoc();
7410 // 1) or (and A, mask), val => ARMbfi A, val, mask
7411 // iff (val & mask) == val
7412 //
7413 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7414 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007415 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007416 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007417 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007418 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007419
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007420 if (VT != MVT::i32)
7421 return SDValue();
7422
Evan Cheng30fb13f2010-12-13 20:32:54 +00007423 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007424
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007425 // The value and the mask need to be constants so we can verify this is
7426 // actually a bitfield set. If the mask is 0xffff, we can do better
7427 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007428 SDValue MaskOp = N0.getOperand(1);
7429 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7430 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007431 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007432 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007433 if (Mask == 0xffff)
7434 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007435 SDValue Res;
7436 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007437 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7438 if (N1C) {
7439 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007440 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007441 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007442
Evan Chenga9688c42010-12-11 04:11:38 +00007443 if (ARM::isBitFieldInvertedMask(Mask)) {
7444 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007445
Evan Cheng30fb13f2010-12-13 20:32:54 +00007446 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007447 DAG.getConstant(Val, MVT::i32),
7448 DAG.getConstant(Mask, MVT::i32));
7449
7450 // Do not add new nodes to DAG combiner worklist.
7451 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007452 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007453 }
Jim Grosbach54238562010-07-17 03:30:54 +00007454 } else if (N1.getOpcode() == ISD::AND) {
7455 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007456 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7457 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007458 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007459 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007460
Eric Christopher29aeed12011-03-26 01:21:03 +00007461 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7462 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007463 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007464 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007465 // The pack halfword instruction works better for masks that fit it,
7466 // so use that when it's available.
7467 if (Subtarget->hasT2ExtractPack() &&
7468 (Mask == 0xffff || Mask == 0xffff0000))
7469 return SDValue();
7470 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007471 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007472 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007473 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007474 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007475 DAG.getConstant(Mask, MVT::i32));
7476 // Do not add new nodes to DAG combiner worklist.
7477 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007478 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007479 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007480 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007481 // The pack halfword instruction works better for masks that fit it,
7482 // so use that when it's available.
7483 if (Subtarget->hasT2ExtractPack() &&
7484 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7485 return SDValue();
7486 // 2b
7487 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007488 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007489 DAG.getConstant(lsb, MVT::i32));
7490 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007491 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007492 // Do not add new nodes to DAG combiner worklist.
7493 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007494 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007495 }
7496 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007497
Evan Cheng30fb13f2010-12-13 20:32:54 +00007498 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7499 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7500 ARM::isBitFieldInvertedMask(~Mask)) {
7501 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7502 // where lsb(mask) == #shamt and masked bits of B are known zero.
7503 SDValue ShAmt = N00.getOperand(1);
7504 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7505 unsigned LSB = CountTrailingZeros_32(Mask);
7506 if (ShAmtC != LSB)
7507 return SDValue();
7508
7509 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7510 DAG.getConstant(~Mask, MVT::i32));
7511
7512 // Do not add new nodes to DAG combiner worklist.
7513 DCI.CombineTo(N, Res, false);
7514 }
7515
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007516 return SDValue();
7517}
7518
Evan Chengc892aeb2012-02-23 01:19:06 +00007519static SDValue PerformXORCombine(SDNode *N,
7520 TargetLowering::DAGCombinerInfo &DCI,
7521 const ARMSubtarget *Subtarget) {
7522 EVT VT = N->getValueType(0);
7523 SelectionDAG &DAG = DCI.DAG;
7524
7525 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7526 return SDValue();
7527
7528 if (!Subtarget->isThumb1Only()) {
7529 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7530 SDValue CXOR = formConditionalOp(N, DAG, true);
7531 if (CXOR.getNode())
7532 return CXOR;
7533 }
7534
7535 return SDValue();
7536}
7537
Evan Chengbf188ae2011-06-15 01:12:31 +00007538/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7539/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007540static SDValue PerformBFICombine(SDNode *N,
7541 TargetLowering::DAGCombinerInfo &DCI) {
7542 SDValue N1 = N->getOperand(1);
7543 if (N1.getOpcode() == ISD::AND) {
7544 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7545 if (!N11C)
7546 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007547 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7548 unsigned LSB = CountTrailingZeros_32(~InvMask);
7549 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7550 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007551 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007552 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007553 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7554 N->getOperand(0), N1.getOperand(0),
7555 N->getOperand(2));
7556 }
7557 return SDValue();
7558}
7559
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007560/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7561/// ARMISD::VMOVRRD.
7562static SDValue PerformVMOVRRDCombine(SDNode *N,
7563 TargetLowering::DAGCombinerInfo &DCI) {
7564 // vmovrrd(vmovdrr x, y) -> x,y
7565 SDValue InDouble = N->getOperand(0);
7566 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7567 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007568
7569 // vmovrrd(load f64) -> (load i32), (load i32)
7570 SDNode *InNode = InDouble.getNode();
7571 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7572 InNode->getValueType(0) == MVT::f64 &&
7573 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7574 !cast<LoadSDNode>(InNode)->isVolatile()) {
7575 // TODO: Should this be done for non-FrameIndex operands?
7576 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7577
7578 SelectionDAG &DAG = DCI.DAG;
7579 DebugLoc DL = LD->getDebugLoc();
7580 SDValue BasePtr = LD->getBasePtr();
7581 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7582 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007583 LD->isNonTemporal(), LD->isInvariant(),
7584 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007585
7586 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7587 DAG.getConstant(4, MVT::i32));
7588 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7589 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007590 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007591 std::min(4U, LD->getAlignment() / 2));
7592
7593 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7594 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7595 DCI.RemoveFromWorklist(LD);
7596 DAG.DeleteNode(LD);
7597 return Result;
7598 }
7599
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007600 return SDValue();
7601}
7602
7603/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7604/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7605static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7606 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7607 SDValue Op0 = N->getOperand(0);
7608 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007609 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007610 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007611 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007612 Op1 = Op1.getOperand(0);
7613 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7614 Op0.getNode() == Op1.getNode() &&
7615 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007616 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007617 N->getValueType(0), Op0.getOperand(0));
7618 return SDValue();
7619}
7620
Bob Wilson31600902010-12-21 06:43:19 +00007621/// PerformSTORECombine - Target-specific dag combine xforms for
7622/// ISD::STORE.
7623static SDValue PerformSTORECombine(SDNode *N,
7624 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007625 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007626 if (St->isVolatile())
7627 return SDValue();
7628
7629 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
7630 // pack all of the elements in one place. Next, store to memory in fewer
7631 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007632 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007633 EVT VT = StVal.getValueType();
7634 if (St->isTruncatingStore() && VT.isVector()) {
7635 SelectionDAG &DAG = DCI.DAG;
7636 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7637 EVT StVT = St->getMemoryVT();
7638 unsigned NumElems = VT.getVectorNumElements();
7639 assert(StVT != VT && "Cannot truncate to the same type");
7640 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7641 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7642
7643 // From, To sizes and ElemCount must be pow of two
7644 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7645
7646 // We are going to use the original vector elt for storing.
7647 // Accumulated smaller vector elements must be a multiple of the store size.
7648 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7649
7650 unsigned SizeRatio = FromEltSz / ToEltSz;
7651 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7652
7653 // Create a type on which we perform the shuffle.
7654 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7655 NumElems*SizeRatio);
7656 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7657
7658 DebugLoc DL = St->getDebugLoc();
7659 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7660 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7661 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7662
7663 // Can't shuffle using an illegal type.
7664 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7665
7666 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7667 DAG.getUNDEF(WideVec.getValueType()),
7668 ShuffleVec.data());
7669 // At this point all of the data is stored at the bottom of the
7670 // register. We now need to save it to mem.
7671
7672 // Find the largest store unit
7673 MVT StoreType = MVT::i8;
7674 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7675 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7676 MVT Tp = (MVT::SimpleValueType)tp;
7677 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7678 StoreType = Tp;
7679 }
7680 // Didn't find a legal store type.
7681 if (!TLI.isTypeLegal(StoreType))
7682 return SDValue();
7683
7684 // Bitcast the original vector into a vector of store-size units
7685 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7686 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7687 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7688 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7689 SmallVector<SDValue, 8> Chains;
7690 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7691 TLI.getPointerTy());
7692 SDValue BasePtr = St->getBasePtr();
7693
7694 // Perform one or more big stores into memory.
7695 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7696 for (unsigned I = 0; I < E; I++) {
7697 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7698 StoreType, ShuffWide,
7699 DAG.getIntPtrConstant(I));
7700 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7701 St->getPointerInfo(), St->isVolatile(),
7702 St->isNonTemporal(), St->getAlignment());
7703 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7704 Increment);
7705 Chains.push_back(Ch);
7706 }
7707 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7708 Chains.size());
7709 }
7710
7711 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007712 return SDValue();
7713
Chad Rosier96b66d62012-04-09 19:38:15 +00007714 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7715 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007716 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007717 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007718 SelectionDAG &DAG = DCI.DAG;
7719 DebugLoc DL = St->getDebugLoc();
7720 SDValue BasePtr = St->getBasePtr();
7721 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7722 StVal.getNode()->getOperand(0), BasePtr,
7723 St->getPointerInfo(), St->isVolatile(),
7724 St->isNonTemporal(), St->getAlignment());
7725
7726 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7727 DAG.getConstant(4, MVT::i32));
7728 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7729 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7730 St->isNonTemporal(),
7731 std::min(4U, St->getAlignment() / 2));
7732 }
7733
7734 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007735 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7736 return SDValue();
7737
Chad Rosier96b66d62012-04-09 19:38:15 +00007738 // Bitcast an i64 store extracted from a vector to f64.
7739 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007740 SelectionDAG &DAG = DCI.DAG;
7741 DebugLoc dl = StVal.getDebugLoc();
7742 SDValue IntVec = StVal.getOperand(0);
7743 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7744 IntVec.getValueType().getVectorNumElements());
7745 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7746 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7747 Vec, StVal.getOperand(1));
7748 dl = N->getDebugLoc();
7749 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7750 // Make the DAGCombiner fold the bitcasts.
7751 DCI.AddToWorklist(Vec.getNode());
7752 DCI.AddToWorklist(ExtElt.getNode());
7753 DCI.AddToWorklist(V.getNode());
7754 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7755 St->getPointerInfo(), St->isVolatile(),
7756 St->isNonTemporal(), St->getAlignment(),
7757 St->getTBAAInfo());
7758}
7759
7760/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7761/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7762/// i64 vector to have f64 elements, since the value can then be loaded
7763/// directly into a VFP register.
7764static bool hasNormalLoadOperand(SDNode *N) {
7765 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7766 for (unsigned i = 0; i < NumElts; ++i) {
7767 SDNode *Elt = N->getOperand(i).getNode();
7768 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7769 return true;
7770 }
7771 return false;
7772}
7773
Bob Wilson75f02882010-09-17 22:59:05 +00007774/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7775/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007776static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7777 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007778 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7779 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7780 // into a pair of GPRs, which is fine when the value is used as a scalar,
7781 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007782 SelectionDAG &DAG = DCI.DAG;
7783 if (N->getNumOperands() == 2) {
7784 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7785 if (RV.getNode())
7786 return RV;
7787 }
Bob Wilson75f02882010-09-17 22:59:05 +00007788
Bob Wilson31600902010-12-21 06:43:19 +00007789 // Load i64 elements as f64 values so that type legalization does not split
7790 // them up into i32 values.
7791 EVT VT = N->getValueType(0);
7792 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7793 return SDValue();
7794 DebugLoc dl = N->getDebugLoc();
7795 SmallVector<SDValue, 8> Ops;
7796 unsigned NumElts = VT.getVectorNumElements();
7797 for (unsigned i = 0; i < NumElts; ++i) {
7798 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7799 Ops.push_back(V);
7800 // Make the DAGCombiner fold the bitcast.
7801 DCI.AddToWorklist(V.getNode());
7802 }
7803 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7804 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7805 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7806}
7807
7808/// PerformInsertEltCombine - Target-specific dag combine xforms for
7809/// ISD::INSERT_VECTOR_ELT.
7810static SDValue PerformInsertEltCombine(SDNode *N,
7811 TargetLowering::DAGCombinerInfo &DCI) {
7812 // Bitcast an i64 load inserted into a vector to f64.
7813 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7814 EVT VT = N->getValueType(0);
7815 SDNode *Elt = N->getOperand(1).getNode();
7816 if (VT.getVectorElementType() != MVT::i64 ||
7817 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7818 return SDValue();
7819
7820 SelectionDAG &DAG = DCI.DAG;
7821 DebugLoc dl = N->getDebugLoc();
7822 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7823 VT.getVectorNumElements());
7824 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7825 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7826 // Make the DAGCombiner fold the bitcasts.
7827 DCI.AddToWorklist(Vec.getNode());
7828 DCI.AddToWorklist(V.getNode());
7829 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7830 Vec, V, N->getOperand(2));
7831 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007832}
7833
Bob Wilsonf20700c2010-10-27 20:38:28 +00007834/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7835/// ISD::VECTOR_SHUFFLE.
7836static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7837 // The LLVM shufflevector instruction does not require the shuffle mask
7838 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7839 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7840 // operands do not match the mask length, they are extended by concatenating
7841 // them with undef vectors. That is probably the right thing for other
7842 // targets, but for NEON it is better to concatenate two double-register
7843 // size vector operands into a single quad-register size vector. Do that
7844 // transformation here:
7845 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7846 // shuffle(concat(v1, v2), undef)
7847 SDValue Op0 = N->getOperand(0);
7848 SDValue Op1 = N->getOperand(1);
7849 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7850 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7851 Op0.getNumOperands() != 2 ||
7852 Op1.getNumOperands() != 2)
7853 return SDValue();
7854 SDValue Concat0Op1 = Op0.getOperand(1);
7855 SDValue Concat1Op1 = Op1.getOperand(1);
7856 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7857 Concat1Op1.getOpcode() != ISD::UNDEF)
7858 return SDValue();
7859 // Skip the transformation if any of the types are illegal.
7860 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7861 EVT VT = N->getValueType(0);
7862 if (!TLI.isTypeLegal(VT) ||
7863 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7864 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7865 return SDValue();
7866
7867 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7868 Op0.getOperand(0), Op1.getOperand(0));
7869 // Translate the shuffle mask.
7870 SmallVector<int, 16> NewMask;
7871 unsigned NumElts = VT.getVectorNumElements();
7872 unsigned HalfElts = NumElts/2;
7873 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7874 for (unsigned n = 0; n < NumElts; ++n) {
7875 int MaskElt = SVN->getMaskElt(n);
7876 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007877 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007878 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007879 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007880 NewElt = HalfElts + MaskElt - NumElts;
7881 NewMask.push_back(NewElt);
7882 }
7883 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7884 DAG.getUNDEF(VT), NewMask.data());
7885}
7886
Bob Wilson1c3ef902011-02-07 17:43:21 +00007887/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7888/// NEON load/store intrinsics to merge base address updates.
7889static SDValue CombineBaseUpdate(SDNode *N,
7890 TargetLowering::DAGCombinerInfo &DCI) {
7891 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7892 return SDValue();
7893
7894 SelectionDAG &DAG = DCI.DAG;
7895 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7896 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7897 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7898 SDValue Addr = N->getOperand(AddrOpIdx);
7899
7900 // Search for a use of the address operand that is an increment.
7901 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7902 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7903 SDNode *User = *UI;
7904 if (User->getOpcode() != ISD::ADD ||
7905 UI.getUse().getResNo() != Addr.getResNo())
7906 continue;
7907
7908 // Check that the add is independent of the load/store. Otherwise, folding
7909 // it would create a cycle.
7910 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7911 continue;
7912
7913 // Find the new opcode for the updating load/store.
7914 bool isLoad = true;
7915 bool isLaneOp = false;
7916 unsigned NewOpc = 0;
7917 unsigned NumVecs = 0;
7918 if (isIntrinsic) {
7919 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7920 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007921 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007922 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7923 NumVecs = 1; break;
7924 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7925 NumVecs = 2; break;
7926 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7927 NumVecs = 3; break;
7928 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7929 NumVecs = 4; break;
7930 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7931 NumVecs = 2; isLaneOp = true; break;
7932 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7933 NumVecs = 3; isLaneOp = true; break;
7934 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7935 NumVecs = 4; isLaneOp = true; break;
7936 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7937 NumVecs = 1; isLoad = false; break;
7938 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7939 NumVecs = 2; isLoad = false; break;
7940 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7941 NumVecs = 3; isLoad = false; break;
7942 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7943 NumVecs = 4; isLoad = false; break;
7944 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7945 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7946 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7947 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7948 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7949 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7950 }
7951 } else {
7952 isLaneOp = true;
7953 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007954 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007955 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7956 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7957 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7958 }
7959 }
7960
7961 // Find the size of memory referenced by the load/store.
7962 EVT VecTy;
7963 if (isLoad)
7964 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007965 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007966 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7967 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7968 if (isLaneOp)
7969 NumBytes /= VecTy.getVectorNumElements();
7970
7971 // If the increment is a constant, it must match the memory ref size.
7972 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7973 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7974 uint64_t IncVal = CInc->getZExtValue();
7975 if (IncVal != NumBytes)
7976 continue;
7977 } else if (NumBytes >= 3 * 16) {
7978 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7979 // separate instructions that make it harder to use a non-constant update.
7980 continue;
7981 }
7982
7983 // Create the new updating load/store node.
7984 EVT Tys[6];
7985 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7986 unsigned n;
7987 for (n = 0; n < NumResultVecs; ++n)
7988 Tys[n] = VecTy;
7989 Tys[n++] = MVT::i32;
7990 Tys[n] = MVT::Other;
7991 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7992 SmallVector<SDValue, 8> Ops;
7993 Ops.push_back(N->getOperand(0)); // incoming chain
7994 Ops.push_back(N->getOperand(AddrOpIdx));
7995 Ops.push_back(Inc);
7996 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7997 Ops.push_back(N->getOperand(i));
7998 }
7999 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8000 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8001 Ops.data(), Ops.size(),
8002 MemInt->getMemoryVT(),
8003 MemInt->getMemOperand());
8004
8005 // Update the uses.
8006 std::vector<SDValue> NewResults;
8007 for (unsigned i = 0; i < NumResultVecs; ++i) {
8008 NewResults.push_back(SDValue(UpdN.getNode(), i));
8009 }
8010 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8011 DCI.CombineTo(N, NewResults);
8012 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8013
8014 break;
Owen Anderson76706012011-04-05 21:48:57 +00008015 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008016 return SDValue();
8017}
8018
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008019/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8020/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8021/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8022/// return true.
8023static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8024 SelectionDAG &DAG = DCI.DAG;
8025 EVT VT = N->getValueType(0);
8026 // vldN-dup instructions only support 64-bit vectors for N > 1.
8027 if (!VT.is64BitVector())
8028 return false;
8029
8030 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8031 SDNode *VLD = N->getOperand(0).getNode();
8032 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8033 return false;
8034 unsigned NumVecs = 0;
8035 unsigned NewOpc = 0;
8036 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8037 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8038 NumVecs = 2;
8039 NewOpc = ARMISD::VLD2DUP;
8040 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8041 NumVecs = 3;
8042 NewOpc = ARMISD::VLD3DUP;
8043 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8044 NumVecs = 4;
8045 NewOpc = ARMISD::VLD4DUP;
8046 } else {
8047 return false;
8048 }
8049
8050 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8051 // numbers match the load.
8052 unsigned VLDLaneNo =
8053 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8054 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8055 UI != UE; ++UI) {
8056 // Ignore uses of the chain result.
8057 if (UI.getUse().getResNo() == NumVecs)
8058 continue;
8059 SDNode *User = *UI;
8060 if (User->getOpcode() != ARMISD::VDUPLANE ||
8061 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8062 return false;
8063 }
8064
8065 // Create the vldN-dup node.
8066 EVT Tys[5];
8067 unsigned n;
8068 for (n = 0; n < NumVecs; ++n)
8069 Tys[n] = VT;
8070 Tys[n] = MVT::Other;
8071 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8072 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8073 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8074 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8075 Ops, 2, VLDMemInt->getMemoryVT(),
8076 VLDMemInt->getMemOperand());
8077
8078 // Update the uses.
8079 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8080 UI != UE; ++UI) {
8081 unsigned ResNo = UI.getUse().getResNo();
8082 // Ignore uses of the chain result.
8083 if (ResNo == NumVecs)
8084 continue;
8085 SDNode *User = *UI;
8086 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8087 }
8088
8089 // Now the vldN-lane intrinsic is dead except for its chain result.
8090 // Update uses of the chain.
8091 std::vector<SDValue> VLDDupResults;
8092 for (unsigned n = 0; n < NumVecs; ++n)
8093 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8094 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8095 DCI.CombineTo(VLD, VLDDupResults);
8096
8097 return true;
8098}
8099
Bob Wilson9e82bf12010-07-14 01:22:12 +00008100/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8101/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008102static SDValue PerformVDUPLANECombine(SDNode *N,
8103 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008104 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008105
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008106 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8107 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8108 if (CombineVLDDUP(N, DCI))
8109 return SDValue(N, 0);
8110
8111 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8112 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008113 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008114 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008115 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008116 return SDValue();
8117
8118 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8119 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8120 // The canonical VMOV for a zero vector uses a 32-bit element size.
8121 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8122 unsigned EltBits;
8123 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8124 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008125 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008126 if (EltSize > VT.getVectorElementType().getSizeInBits())
8127 return SDValue();
8128
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008129 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008130}
8131
Eric Christopherfa6f5912011-06-29 21:10:36 +00008132// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008133// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8134static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8135{
Chad Rosier118c9a02011-06-28 17:26:57 +00008136 integerPart cN;
8137 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008138 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8139 I != E; I++) {
8140 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8141 if (!C)
8142 return false;
8143
Eric Christopherfa6f5912011-06-29 21:10:36 +00008144 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008145 APFloat APF = C->getValueAPF();
8146 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8147 != APFloat::opOK || !isExact)
8148 return false;
8149
8150 c0 = (I == 0) ? cN : c0;
8151 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8152 return false;
8153 }
8154 C = c0;
8155 return true;
8156}
8157
8158/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8159/// can replace combinations of VMUL and VCVT (floating-point to integer)
8160/// when the VMUL has a constant operand that is a power of 2.
8161///
8162/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8163/// vmul.f32 d16, d17, d16
8164/// vcvt.s32.f32 d16, d16
8165/// becomes:
8166/// vcvt.s32.f32 d16, d16, #3
8167static SDValue PerformVCVTCombine(SDNode *N,
8168 TargetLowering::DAGCombinerInfo &DCI,
8169 const ARMSubtarget *Subtarget) {
8170 SelectionDAG &DAG = DCI.DAG;
8171 SDValue Op = N->getOperand(0);
8172
8173 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8174 Op.getOpcode() != ISD::FMUL)
8175 return SDValue();
8176
8177 uint64_t C;
8178 SDValue N0 = Op->getOperand(0);
8179 SDValue ConstVec = Op->getOperand(1);
8180 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8181
Eric Christopherfa6f5912011-06-29 21:10:36 +00008182 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008183 !isConstVecPow2(ConstVec, isSigned, C))
8184 return SDValue();
8185
8186 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8187 Intrinsic::arm_neon_vcvtfp2fxu;
8188 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8189 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008190 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008191 DAG.getConstant(Log2_64(C), MVT::i32));
8192}
8193
8194/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8195/// can replace combinations of VCVT (integer to floating-point) and VDIV
8196/// when the VDIV has a constant operand that is a power of 2.
8197///
8198/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8199/// vcvt.f32.s32 d16, d16
8200/// vdiv.f32 d16, d17, d16
8201/// becomes:
8202/// vcvt.f32.s32 d16, d16, #3
8203static SDValue PerformVDIVCombine(SDNode *N,
8204 TargetLowering::DAGCombinerInfo &DCI,
8205 const ARMSubtarget *Subtarget) {
8206 SelectionDAG &DAG = DCI.DAG;
8207 SDValue Op = N->getOperand(0);
8208 unsigned OpOpcode = Op.getNode()->getOpcode();
8209
8210 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8211 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8212 return SDValue();
8213
8214 uint64_t C;
8215 SDValue ConstVec = N->getOperand(1);
8216 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8217
8218 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8219 !isConstVecPow2(ConstVec, isSigned, C))
8220 return SDValue();
8221
Eric Christopherfa6f5912011-06-29 21:10:36 +00008222 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008223 Intrinsic::arm_neon_vcvtfxu2fp;
8224 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8225 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008226 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008227 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8228}
8229
8230/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008231/// operand of a vector shift operation, where all the elements of the
8232/// build_vector must have the same constant integer value.
8233static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8234 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008235 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008236 Op = Op.getOperand(0);
8237 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8238 APInt SplatBits, SplatUndef;
8239 unsigned SplatBitSize;
8240 bool HasAnyUndefs;
8241 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8242 HasAnyUndefs, ElementBits) ||
8243 SplatBitSize > ElementBits)
8244 return false;
8245 Cnt = SplatBits.getSExtValue();
8246 return true;
8247}
8248
8249/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8250/// operand of a vector shift left operation. That value must be in the range:
8251/// 0 <= Value < ElementBits for a left shift; or
8252/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008253static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008254 assert(VT.isVector() && "vector shift count is not a vector type");
8255 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8256 if (! getVShiftImm(Op, ElementBits, Cnt))
8257 return false;
8258 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8259}
8260
8261/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8262/// operand of a vector shift right operation. For a shift opcode, the value
8263/// is positive, but for an intrinsic the value count must be negative. The
8264/// absolute value must be in the range:
8265/// 1 <= |Value| <= ElementBits for a right shift; or
8266/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008267static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008268 int64_t &Cnt) {
8269 assert(VT.isVector() && "vector shift count is not a vector type");
8270 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8271 if (! getVShiftImm(Op, ElementBits, Cnt))
8272 return false;
8273 if (isIntrinsic)
8274 Cnt = -Cnt;
8275 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8276}
8277
8278/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8279static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8280 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8281 switch (IntNo) {
8282 default:
8283 // Don't do anything for most intrinsics.
8284 break;
8285
8286 // Vector shifts: check for immediate versions and lower them.
8287 // Note: This is done during DAG combining instead of DAG legalizing because
8288 // the build_vectors for 64-bit vector element shift counts are generally
8289 // not legal, and it is hard to see their values after they get legalized to
8290 // loads from a constant pool.
8291 case Intrinsic::arm_neon_vshifts:
8292 case Intrinsic::arm_neon_vshiftu:
8293 case Intrinsic::arm_neon_vshiftls:
8294 case Intrinsic::arm_neon_vshiftlu:
8295 case Intrinsic::arm_neon_vshiftn:
8296 case Intrinsic::arm_neon_vrshifts:
8297 case Intrinsic::arm_neon_vrshiftu:
8298 case Intrinsic::arm_neon_vrshiftn:
8299 case Intrinsic::arm_neon_vqshifts:
8300 case Intrinsic::arm_neon_vqshiftu:
8301 case Intrinsic::arm_neon_vqshiftsu:
8302 case Intrinsic::arm_neon_vqshiftns:
8303 case Intrinsic::arm_neon_vqshiftnu:
8304 case Intrinsic::arm_neon_vqshiftnsu:
8305 case Intrinsic::arm_neon_vqrshiftns:
8306 case Intrinsic::arm_neon_vqrshiftnu:
8307 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008308 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008309 int64_t Cnt;
8310 unsigned VShiftOpc = 0;
8311
8312 switch (IntNo) {
8313 case Intrinsic::arm_neon_vshifts:
8314 case Intrinsic::arm_neon_vshiftu:
8315 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8316 VShiftOpc = ARMISD::VSHL;
8317 break;
8318 }
8319 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8320 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8321 ARMISD::VSHRs : ARMISD::VSHRu);
8322 break;
8323 }
8324 return SDValue();
8325
8326 case Intrinsic::arm_neon_vshiftls:
8327 case Intrinsic::arm_neon_vshiftlu:
8328 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8329 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008330 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008331
8332 case Intrinsic::arm_neon_vrshifts:
8333 case Intrinsic::arm_neon_vrshiftu:
8334 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8335 break;
8336 return SDValue();
8337
8338 case Intrinsic::arm_neon_vqshifts:
8339 case Intrinsic::arm_neon_vqshiftu:
8340 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8341 break;
8342 return SDValue();
8343
8344 case Intrinsic::arm_neon_vqshiftsu:
8345 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8346 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008347 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008348
8349 case Intrinsic::arm_neon_vshiftn:
8350 case Intrinsic::arm_neon_vrshiftn:
8351 case Intrinsic::arm_neon_vqshiftns:
8352 case Intrinsic::arm_neon_vqshiftnu:
8353 case Intrinsic::arm_neon_vqshiftnsu:
8354 case Intrinsic::arm_neon_vqrshiftns:
8355 case Intrinsic::arm_neon_vqrshiftnu:
8356 case Intrinsic::arm_neon_vqrshiftnsu:
8357 // Narrowing shifts require an immediate right shift.
8358 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8359 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008360 llvm_unreachable("invalid shift count for narrowing vector shift "
8361 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008362
8363 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008364 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008365 }
8366
8367 switch (IntNo) {
8368 case Intrinsic::arm_neon_vshifts:
8369 case Intrinsic::arm_neon_vshiftu:
8370 // Opcode already set above.
8371 break;
8372 case Intrinsic::arm_neon_vshiftls:
8373 case Intrinsic::arm_neon_vshiftlu:
8374 if (Cnt == VT.getVectorElementType().getSizeInBits())
8375 VShiftOpc = ARMISD::VSHLLi;
8376 else
8377 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8378 ARMISD::VSHLLs : ARMISD::VSHLLu);
8379 break;
8380 case Intrinsic::arm_neon_vshiftn:
8381 VShiftOpc = ARMISD::VSHRN; break;
8382 case Intrinsic::arm_neon_vrshifts:
8383 VShiftOpc = ARMISD::VRSHRs; break;
8384 case Intrinsic::arm_neon_vrshiftu:
8385 VShiftOpc = ARMISD::VRSHRu; break;
8386 case Intrinsic::arm_neon_vrshiftn:
8387 VShiftOpc = ARMISD::VRSHRN; break;
8388 case Intrinsic::arm_neon_vqshifts:
8389 VShiftOpc = ARMISD::VQSHLs; break;
8390 case Intrinsic::arm_neon_vqshiftu:
8391 VShiftOpc = ARMISD::VQSHLu; break;
8392 case Intrinsic::arm_neon_vqshiftsu:
8393 VShiftOpc = ARMISD::VQSHLsu; break;
8394 case Intrinsic::arm_neon_vqshiftns:
8395 VShiftOpc = ARMISD::VQSHRNs; break;
8396 case Intrinsic::arm_neon_vqshiftnu:
8397 VShiftOpc = ARMISD::VQSHRNu; break;
8398 case Intrinsic::arm_neon_vqshiftnsu:
8399 VShiftOpc = ARMISD::VQSHRNsu; break;
8400 case Intrinsic::arm_neon_vqrshiftns:
8401 VShiftOpc = ARMISD::VQRSHRNs; break;
8402 case Intrinsic::arm_neon_vqrshiftnu:
8403 VShiftOpc = ARMISD::VQRSHRNu; break;
8404 case Intrinsic::arm_neon_vqrshiftnsu:
8405 VShiftOpc = ARMISD::VQRSHRNsu; break;
8406 }
8407
8408 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008409 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008410 }
8411
8412 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008413 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008414 int64_t Cnt;
8415 unsigned VShiftOpc = 0;
8416
8417 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8418 VShiftOpc = ARMISD::VSLI;
8419 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8420 VShiftOpc = ARMISD::VSRI;
8421 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008422 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008423 }
8424
8425 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8426 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008427 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008428 }
8429
8430 case Intrinsic::arm_neon_vqrshifts:
8431 case Intrinsic::arm_neon_vqrshiftu:
8432 // No immediate versions of these to check for.
8433 break;
8434 }
8435
8436 return SDValue();
8437}
8438
8439/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8440/// lowers them. As with the vector shift intrinsics, this is done during DAG
8441/// combining instead of DAG legalizing because the build_vectors for 64-bit
8442/// vector element shift counts are generally not legal, and it is hard to see
8443/// their values after they get legalized to loads from a constant pool.
8444static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8445 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008446 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008447 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8448 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8449 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8450 SDValue N1 = N->getOperand(1);
8451 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8452 SDValue N0 = N->getOperand(0);
8453 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8454 DAG.MaskedValueIsZero(N0.getOperand(0),
8455 APInt::getHighBitsSet(32, 16)))
8456 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8457 }
8458 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008459
8460 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008461 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8462 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008463 return SDValue();
8464
8465 assert(ST->hasNEON() && "unexpected vector shift");
8466 int64_t Cnt;
8467
8468 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008469 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008470
8471 case ISD::SHL:
8472 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8473 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008474 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008475 break;
8476
8477 case ISD::SRA:
8478 case ISD::SRL:
8479 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8480 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8481 ARMISD::VSHRs : ARMISD::VSHRu);
8482 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008483 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008484 }
8485 }
8486 return SDValue();
8487}
8488
8489/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8490/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8491static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8492 const ARMSubtarget *ST) {
8493 SDValue N0 = N->getOperand(0);
8494
8495 // Check for sign- and zero-extensions of vector extract operations of 8-
8496 // and 16-bit vector elements. NEON supports these directly. They are
8497 // handled during DAG combining because type legalization will promote them
8498 // to 32-bit types and it is messy to recognize the operations after that.
8499 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8500 SDValue Vec = N0.getOperand(0);
8501 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008502 EVT VT = N->getValueType(0);
8503 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008504 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8505
Owen Anderson825b72b2009-08-11 20:47:22 +00008506 if (VT == MVT::i32 &&
8507 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008508 TLI.isTypeLegal(Vec.getValueType()) &&
8509 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008510
8511 unsigned Opc = 0;
8512 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008513 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008514 case ISD::SIGN_EXTEND:
8515 Opc = ARMISD::VGETLANEs;
8516 break;
8517 case ISD::ZERO_EXTEND:
8518 case ISD::ANY_EXTEND:
8519 Opc = ARMISD::VGETLANEu;
8520 break;
8521 }
8522 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8523 }
8524 }
8525
8526 return SDValue();
8527}
8528
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008529/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8530/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8531static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8532 const ARMSubtarget *ST) {
8533 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008534 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008535 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8536 // a NaN; only do the transformation when it matches that behavior.
8537
8538 // For now only do this when using NEON for FP operations; if using VFP, it
8539 // is not obvious that the benefit outweighs the cost of switching to the
8540 // NEON pipeline.
8541 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8542 N->getValueType(0) != MVT::f32)
8543 return SDValue();
8544
8545 SDValue CondLHS = N->getOperand(0);
8546 SDValue CondRHS = N->getOperand(1);
8547 SDValue LHS = N->getOperand(2);
8548 SDValue RHS = N->getOperand(3);
8549 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8550
8551 unsigned Opcode = 0;
8552 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008553 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008554 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008555 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008556 IsReversed = true ; // x CC y ? y : x
8557 } else {
8558 return SDValue();
8559 }
8560
Bob Wilsone742bb52010-02-24 22:15:53 +00008561 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008562 switch (CC) {
8563 default: break;
8564 case ISD::SETOLT:
8565 case ISD::SETOLE:
8566 case ISD::SETLT:
8567 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008568 case ISD::SETULT:
8569 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008570 // If LHS is NaN, an ordered comparison will be false and the result will
8571 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8572 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8573 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8574 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8575 break;
8576 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8577 // will return -0, so vmin can only be used for unsafe math or if one of
8578 // the operands is known to be nonzero.
8579 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008580 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008581 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8582 break;
8583 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008584 break;
8585
8586 case ISD::SETOGT:
8587 case ISD::SETOGE:
8588 case ISD::SETGT:
8589 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008590 case ISD::SETUGT:
8591 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008592 // If LHS is NaN, an ordered comparison will be false and the result will
8593 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8594 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8595 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8596 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8597 break;
8598 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8599 // will return +0, so vmax can only be used for unsafe math or if one of
8600 // the operands is known to be nonzero.
8601 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008602 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008603 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8604 break;
8605 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008606 break;
8607 }
8608
8609 if (!Opcode)
8610 return SDValue();
8611 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8612}
8613
Evan Chenge721f5c2011-07-13 00:42:17 +00008614/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8615SDValue
8616ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8617 SDValue Cmp = N->getOperand(4);
8618 if (Cmp.getOpcode() != ARMISD::CMPZ)
8619 // Only looking at EQ and NE cases.
8620 return SDValue();
8621
8622 EVT VT = N->getValueType(0);
8623 DebugLoc dl = N->getDebugLoc();
8624 SDValue LHS = Cmp.getOperand(0);
8625 SDValue RHS = Cmp.getOperand(1);
8626 SDValue FalseVal = N->getOperand(0);
8627 SDValue TrueVal = N->getOperand(1);
8628 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008629 ARMCC::CondCodes CC =
8630 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008631
8632 // Simplify
8633 // mov r1, r0
8634 // cmp r1, x
8635 // mov r0, y
8636 // moveq r0, x
8637 // to
8638 // cmp r0, x
8639 // movne r0, y
8640 //
8641 // mov r1, r0
8642 // cmp r1, x
8643 // mov r0, x
8644 // movne r0, y
8645 // to
8646 // cmp r0, x
8647 // movne r0, y
8648 /// FIXME: Turn this into a target neutral optimization?
8649 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008650 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008651 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8652 N->getOperand(3), Cmp);
8653 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8654 SDValue ARMcc;
8655 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8656 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8657 N->getOperand(3), NewCmp);
8658 }
8659
8660 if (Res.getNode()) {
8661 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008662 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008663 // Capture demanded bits information that would be otherwise lost.
8664 if (KnownZero == 0xfffffffe)
8665 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8666 DAG.getValueType(MVT::i1));
8667 else if (KnownZero == 0xffffff00)
8668 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8669 DAG.getValueType(MVT::i8));
8670 else if (KnownZero == 0xffff0000)
8671 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8672 DAG.getValueType(MVT::i16));
8673 }
8674
8675 return Res;
8676}
8677
Dan Gohman475871a2008-07-27 21:46:04 +00008678SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008679 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008680 switch (N->getOpcode()) {
8681 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008682 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008683 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008684 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008685 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008686 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8687 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008688 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008689 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008690 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008691 case ISD::STORE: return PerformSTORECombine(N, DCI);
8692 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8693 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008694 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008695 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008696 case ISD::FP_TO_SINT:
8697 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8698 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008699 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008700 case ISD::SHL:
8701 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008702 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008703 case ISD::SIGN_EXTEND:
8704 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008705 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8706 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008707 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008708 case ARMISD::VLD2DUP:
8709 case ARMISD::VLD3DUP:
8710 case ARMISD::VLD4DUP:
8711 return CombineBaseUpdate(N, DCI);
8712 case ISD::INTRINSIC_VOID:
8713 case ISD::INTRINSIC_W_CHAIN:
8714 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8715 case Intrinsic::arm_neon_vld1:
8716 case Intrinsic::arm_neon_vld2:
8717 case Intrinsic::arm_neon_vld3:
8718 case Intrinsic::arm_neon_vld4:
8719 case Intrinsic::arm_neon_vld2lane:
8720 case Intrinsic::arm_neon_vld3lane:
8721 case Intrinsic::arm_neon_vld4lane:
8722 case Intrinsic::arm_neon_vst1:
8723 case Intrinsic::arm_neon_vst2:
8724 case Intrinsic::arm_neon_vst3:
8725 case Intrinsic::arm_neon_vst4:
8726 case Intrinsic::arm_neon_vst2lane:
8727 case Intrinsic::arm_neon_vst3lane:
8728 case Intrinsic::arm_neon_vst4lane:
8729 return CombineBaseUpdate(N, DCI);
8730 default: break;
8731 }
8732 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008733 }
Dan Gohman475871a2008-07-27 21:46:04 +00008734 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008735}
8736
Evan Cheng31959b12011-02-02 01:06:55 +00008737bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8738 EVT VT) const {
8739 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8740}
8741
Bill Wendlingaf566342009-08-15 21:21:19 +00008742bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008743 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008744 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008745
8746 switch (VT.getSimpleVT().SimpleTy) {
8747 default:
8748 return false;
8749 case MVT::i8:
8750 case MVT::i16:
8751 case MVT::i32:
8752 return true;
8753 // FIXME: VLD1 etc with standard alignment is legal.
8754 }
8755}
8756
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008757static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8758 unsigned AlignCheck) {
8759 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8760 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8761}
8762
8763EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8764 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008765 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008766 bool MemcpyStrSrc,
8767 MachineFunction &MF) const {
8768 const Function *F = MF.getFunction();
8769
8770 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008771 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008772 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8773 Subtarget->hasNEON()) {
8774 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8775 return MVT::v4i32;
8776 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8777 return MVT::v2i32;
8778 }
8779 }
8780
Lang Hames5207bf22011-11-08 18:56:23 +00008781 // Lowering to i32/i16 if the size permits.
8782 if (Size >= 4) {
8783 return MVT::i32;
8784 } else if (Size >= 2) {
8785 return MVT::i16;
8786 }
8787
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008788 // Let the target-independent logic figure it out.
8789 return MVT::Other;
8790}
8791
Evan Chenge6c835f2009-08-14 20:09:37 +00008792static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8793 if (V < 0)
8794 return false;
8795
8796 unsigned Scale = 1;
8797 switch (VT.getSimpleVT().SimpleTy) {
8798 default: return false;
8799 case MVT::i1:
8800 case MVT::i8:
8801 // Scale == 1;
8802 break;
8803 case MVT::i16:
8804 // Scale == 2;
8805 Scale = 2;
8806 break;
8807 case MVT::i32:
8808 // Scale == 4;
8809 Scale = 4;
8810 break;
8811 }
8812
8813 if ((V & (Scale - 1)) != 0)
8814 return false;
8815 V /= Scale;
8816 return V == (V & ((1LL << 5) - 1));
8817}
8818
8819static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8820 const ARMSubtarget *Subtarget) {
8821 bool isNeg = false;
8822 if (V < 0) {
8823 isNeg = true;
8824 V = - V;
8825 }
8826
8827 switch (VT.getSimpleVT().SimpleTy) {
8828 default: return false;
8829 case MVT::i1:
8830 case MVT::i8:
8831 case MVT::i16:
8832 case MVT::i32:
8833 // + imm12 or - imm8
8834 if (isNeg)
8835 return V == (V & ((1LL << 8) - 1));
8836 return V == (V & ((1LL << 12) - 1));
8837 case MVT::f32:
8838 case MVT::f64:
8839 // Same as ARM mode. FIXME: NEON?
8840 if (!Subtarget->hasVFP2())
8841 return false;
8842 if ((V & 3) != 0)
8843 return false;
8844 V >>= 2;
8845 return V == (V & ((1LL << 8) - 1));
8846 }
8847}
8848
Evan Chengb01fad62007-03-12 23:30:29 +00008849/// isLegalAddressImmediate - Return true if the integer value can be used
8850/// as the offset of the target addressing mode for load / store of the
8851/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008852static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008853 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008854 if (V == 0)
8855 return true;
8856
Evan Cheng65011532009-03-09 19:15:00 +00008857 if (!VT.isSimple())
8858 return false;
8859
Evan Chenge6c835f2009-08-14 20:09:37 +00008860 if (Subtarget->isThumb1Only())
8861 return isLegalT1AddressImmediate(V, VT);
8862 else if (Subtarget->isThumb2())
8863 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008864
Evan Chenge6c835f2009-08-14 20:09:37 +00008865 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008866 if (V < 0)
8867 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008868 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008869 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008870 case MVT::i1:
8871 case MVT::i8:
8872 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008873 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008874 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008875 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008876 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008877 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008878 case MVT::f32:
8879 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008880 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008881 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008882 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008883 return false;
8884 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008885 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008886 }
Evan Chenga8e29892007-01-19 07:51:42 +00008887}
8888
Evan Chenge6c835f2009-08-14 20:09:37 +00008889bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8890 EVT VT) const {
8891 int Scale = AM.Scale;
8892 if (Scale < 0)
8893 return false;
8894
8895 switch (VT.getSimpleVT().SimpleTy) {
8896 default: return false;
8897 case MVT::i1:
8898 case MVT::i8:
8899 case MVT::i16:
8900 case MVT::i32:
8901 if (Scale == 1)
8902 return true;
8903 // r + r << imm
8904 Scale = Scale & ~1;
8905 return Scale == 2 || Scale == 4 || Scale == 8;
8906 case MVT::i64:
8907 // r + r
8908 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8909 return true;
8910 return false;
8911 case MVT::isVoid:
8912 // Note, we allow "void" uses (basically, uses that aren't loads or
8913 // stores), because arm allows folding a scale into many arithmetic
8914 // operations. This should be made more precise and revisited later.
8915
8916 // Allow r << imm, but the imm has to be a multiple of two.
8917 if (Scale & 1) return false;
8918 return isPowerOf2_32(Scale);
8919 }
8920}
8921
Chris Lattner37caf8c2007-04-09 23:33:39 +00008922/// isLegalAddressingMode - Return true if the addressing mode represented
8923/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008924bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008925 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008926 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008927 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008928 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008929
Chris Lattner37caf8c2007-04-09 23:33:39 +00008930 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008931 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008932 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008933
Chris Lattner37caf8c2007-04-09 23:33:39 +00008934 switch (AM.Scale) {
8935 case 0: // no scale reg, must be "r+i" or "r", or "i".
8936 break;
8937 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008938 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008939 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008940 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008941 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008942 // ARM doesn't support any R+R*scale+imm addr modes.
8943 if (AM.BaseOffs)
8944 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008945
Bob Wilson2c7dab12009-04-08 17:55:28 +00008946 if (!VT.isSimple())
8947 return false;
8948
Evan Chenge6c835f2009-08-14 20:09:37 +00008949 if (Subtarget->isThumb2())
8950 return isLegalT2ScaledAddressingMode(AM, VT);
8951
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008952 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008953 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008954 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008955 case MVT::i1:
8956 case MVT::i8:
8957 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008958 if (Scale < 0) Scale = -Scale;
8959 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008960 return true;
8961 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008962 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008963 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008964 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008965 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008966 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008967 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008968 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008969
Owen Anderson825b72b2009-08-11 20:47:22 +00008970 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008971 // Note, we allow "void" uses (basically, uses that aren't loads or
8972 // stores), because arm allows folding a scale into many arithmetic
8973 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008974
Chris Lattner37caf8c2007-04-09 23:33:39 +00008975 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008976 if (Scale & 1) return false;
8977 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008978 }
Evan Chengb01fad62007-03-12 23:30:29 +00008979 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008980 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008981}
8982
Evan Cheng77e47512009-11-11 19:05:52 +00008983/// isLegalICmpImmediate - Return true if the specified immediate is legal
8984/// icmp immediate, that is the target has icmp instructions which can compare
8985/// a register against the immediate without having to materialize the
8986/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008987bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00008988 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00008989 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00008990 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00008991 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00008992 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00008993 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00008994 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008995}
8996
Dan Gohmancca82142011-05-03 00:46:49 +00008997/// isLegalAddImmediate - Return true if the specified immediate is legal
8998/// add immediate, that is the target has add instructions which can add
8999/// a register with the immediate without having to materialize the
9000/// immediate into a register.
9001bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
9002 return ARM_AM::getSOImmVal(Imm) != -1;
9003}
9004
Owen Andersone50ed302009-08-10 22:56:29 +00009005static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009006 bool isSEXTLoad, SDValue &Base,
9007 SDValue &Offset, bool &isInc,
9008 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009009 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9010 return false;
9011
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009013 // AddressingMode 3
9014 Base = Ptr->getOperand(0);
9015 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009016 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009017 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009018 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009019 isInc = false;
9020 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9021 return true;
9022 }
9023 }
9024 isInc = (Ptr->getOpcode() == ISD::ADD);
9025 Offset = Ptr->getOperand(1);
9026 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009027 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009028 // AddressingMode 2
9029 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009030 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009031 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009032 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009033 isInc = false;
9034 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9035 Base = Ptr->getOperand(0);
9036 return true;
9037 }
9038 }
9039
9040 if (Ptr->getOpcode() == ISD::ADD) {
9041 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009042 ARM_AM::ShiftOpc ShOpcVal=
9043 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009044 if (ShOpcVal != ARM_AM::no_shift) {
9045 Base = Ptr->getOperand(1);
9046 Offset = Ptr->getOperand(0);
9047 } else {
9048 Base = Ptr->getOperand(0);
9049 Offset = Ptr->getOperand(1);
9050 }
9051 return true;
9052 }
9053
9054 isInc = (Ptr->getOpcode() == ISD::ADD);
9055 Base = Ptr->getOperand(0);
9056 Offset = Ptr->getOperand(1);
9057 return true;
9058 }
9059
Jim Grosbache5165492009-11-09 00:11:35 +00009060 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009061 return false;
9062}
9063
Owen Andersone50ed302009-08-10 22:56:29 +00009064static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009065 bool isSEXTLoad, SDValue &Base,
9066 SDValue &Offset, bool &isInc,
9067 SelectionDAG &DAG) {
9068 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9069 return false;
9070
9071 Base = Ptr->getOperand(0);
9072 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9073 int RHSC = (int)RHS->getZExtValue();
9074 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9075 assert(Ptr->getOpcode() == ISD::ADD);
9076 isInc = false;
9077 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9078 return true;
9079 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9080 isInc = Ptr->getOpcode() == ISD::ADD;
9081 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9082 return true;
9083 }
9084 }
9085
9086 return false;
9087}
9088
Evan Chenga8e29892007-01-19 07:51:42 +00009089/// getPreIndexedAddressParts - returns true by value, base pointer and
9090/// offset pointer and addressing mode by reference if the node's address
9091/// can be legally represented as pre-indexed load / store address.
9092bool
Dan Gohman475871a2008-07-27 21:46:04 +00009093ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9094 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009095 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009096 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009097 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009098 return false;
9099
Owen Andersone50ed302009-08-10 22:56:29 +00009100 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009101 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009102 bool isSEXTLoad = false;
9103 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9104 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009105 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009106 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9107 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9108 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009109 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009110 } else
9111 return false;
9112
9113 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009114 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009115 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009116 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9117 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009118 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009119 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009120 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009121 if (!isLegal)
9122 return false;
9123
9124 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9125 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009126}
9127
9128/// getPostIndexedAddressParts - returns true by value, base pointer and
9129/// offset pointer and addressing mode by reference if this node can be
9130/// combined with a load / store to form a post-indexed load / store.
9131bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009132 SDValue &Base,
9133 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009134 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009135 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009136 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009137 return false;
9138
Owen Andersone50ed302009-08-10 22:56:29 +00009139 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009140 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009141 bool isSEXTLoad = false;
9142 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009143 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009144 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009145 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9146 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009147 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009148 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009149 } else
9150 return false;
9151
9152 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009153 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009154 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009155 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009156 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009157 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009158 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9159 isInc, DAG);
9160 if (!isLegal)
9161 return false;
9162
Evan Cheng28dad2a2010-05-18 21:31:17 +00009163 if (Ptr != Base) {
9164 // Swap base ptr and offset to catch more post-index load / store when
9165 // it's legal. In Thumb2 mode, offset must be an immediate.
9166 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9167 !Subtarget->isThumb2())
9168 std::swap(Base, Offset);
9169
9170 // Post-indexed load / store update the base pointer.
9171 if (Ptr != Base)
9172 return false;
9173 }
9174
Evan Chenge88d5ce2009-07-02 07:28:31 +00009175 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9176 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009177}
9178
Dan Gohman475871a2008-07-27 21:46:04 +00009179void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009180 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009181 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009182 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009183 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009184 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009185 switch (Op.getOpcode()) {
9186 default: break;
9187 case ARMISD::CMOV: {
9188 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009189 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009190 if (KnownZero == 0 && KnownOne == 0) return;
9191
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009192 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009193 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009194 KnownZero &= KnownZeroRHS;
9195 KnownOne &= KnownOneRHS;
9196 return;
9197 }
9198 }
9199}
9200
9201//===----------------------------------------------------------------------===//
9202// ARM Inline Assembly Support
9203//===----------------------------------------------------------------------===//
9204
Evan Cheng55d42002011-01-08 01:24:27 +00009205bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9206 // Looking for "rev" which is V6+.
9207 if (!Subtarget->hasV6Ops())
9208 return false;
9209
9210 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9211 std::string AsmStr = IA->getAsmString();
9212 SmallVector<StringRef, 4> AsmPieces;
9213 SplitString(AsmStr, AsmPieces, ";\n");
9214
9215 switch (AsmPieces.size()) {
9216 default: return false;
9217 case 1:
9218 AsmStr = AsmPieces[0];
9219 AsmPieces.clear();
9220 SplitString(AsmStr, AsmPieces, " \t,");
9221
9222 // rev $0, $1
9223 if (AsmPieces.size() == 3 &&
9224 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9225 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009226 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009227 if (Ty && Ty->getBitWidth() == 32)
9228 return IntrinsicLowering::LowerToByteSwap(CI);
9229 }
9230 break;
9231 }
9232
9233 return false;
9234}
9235
Evan Chenga8e29892007-01-19 07:51:42 +00009236/// getConstraintType - Given a constraint letter, return the type of
9237/// constraint it is for this target.
9238ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009239ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9240 if (Constraint.size() == 1) {
9241 switch (Constraint[0]) {
9242 default: break;
9243 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009244 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009245 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009246 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009247 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009248 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009249 // An address with a single base register. Due to the way we
9250 // currently handle addresses it is the same as an 'r' memory constraint.
9251 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009252 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009253 } else if (Constraint.size() == 2) {
9254 switch (Constraint[0]) {
9255 default: break;
9256 // All 'U+' constraints are addresses.
9257 case 'U': return C_Memory;
9258 }
Evan Chenga8e29892007-01-19 07:51:42 +00009259 }
Chris Lattner4234f572007-03-25 02:14:49 +00009260 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009261}
9262
John Thompson44ab89e2010-10-29 17:29:13 +00009263/// Examine constraint type and operand type and determine a weight value.
9264/// This object must already have been set up with the operand type
9265/// and the current alternative constraint selected.
9266TargetLowering::ConstraintWeight
9267ARMTargetLowering::getSingleConstraintMatchWeight(
9268 AsmOperandInfo &info, const char *constraint) const {
9269 ConstraintWeight weight = CW_Invalid;
9270 Value *CallOperandVal = info.CallOperandVal;
9271 // If we don't have a value, we can't do a match,
9272 // but allow it at the lowest weight.
9273 if (CallOperandVal == NULL)
9274 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009275 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009276 // Look at the constraint type.
9277 switch (*constraint) {
9278 default:
9279 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9280 break;
9281 case 'l':
9282 if (type->isIntegerTy()) {
9283 if (Subtarget->isThumb())
9284 weight = CW_SpecificReg;
9285 else
9286 weight = CW_Register;
9287 }
9288 break;
9289 case 'w':
9290 if (type->isFloatingPointTy())
9291 weight = CW_Register;
9292 break;
9293 }
9294 return weight;
9295}
9296
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009297typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9298RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009299ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009300 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009301 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009302 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009303 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009304 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009305 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009306 return RCPair(0U, &ARM::tGPRRegClass);
9307 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009308 case 'h': // High regs or no regs.
9309 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009310 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009311 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009312 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009313 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009314 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009315 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009316 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009317 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009318 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009319 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009320 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009321 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009322 case 'x':
9323 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009324 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009325 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009326 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009327 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009328 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009329 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009330 case 't':
9331 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009332 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009333 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009334 }
9335 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009336 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009337 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009338
Evan Chenga8e29892007-01-19 07:51:42 +00009339 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9340}
9341
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009342/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9343/// vector. If it is invalid, don't add anything to Ops.
9344void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009345 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009346 std::vector<SDValue>&Ops,
9347 SelectionDAG &DAG) const {
9348 SDValue Result(0, 0);
9349
Eric Christopher100c8332011-06-02 23:16:42 +00009350 // Currently only support length 1 constraints.
9351 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009352
Eric Christopher100c8332011-06-02 23:16:42 +00009353 char ConstraintLetter = Constraint[0];
9354 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009355 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009356 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009357 case 'I': case 'J': case 'K': case 'L':
9358 case 'M': case 'N': case 'O':
9359 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9360 if (!C)
9361 return;
9362
9363 int64_t CVal64 = C->getSExtValue();
9364 int CVal = (int) CVal64;
9365 // None of these constraints allow values larger than 32 bits. Check
9366 // that the value fits in an int.
9367 if (CVal != CVal64)
9368 return;
9369
Eric Christopher100c8332011-06-02 23:16:42 +00009370 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009371 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009372 // Constant suitable for movw, must be between 0 and
9373 // 65535.
9374 if (Subtarget->hasV6T2Ops())
9375 if (CVal >= 0 && CVal <= 65535)
9376 break;
9377 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009378 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009379 if (Subtarget->isThumb1Only()) {
9380 // This must be a constant between 0 and 255, for ADD
9381 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009382 if (CVal >= 0 && CVal <= 255)
9383 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009384 } else if (Subtarget->isThumb2()) {
9385 // A constant that can be used as an immediate value in a
9386 // data-processing instruction.
9387 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9388 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009389 } else {
9390 // A constant that can be used as an immediate value in a
9391 // data-processing instruction.
9392 if (ARM_AM::getSOImmVal(CVal) != -1)
9393 break;
9394 }
9395 return;
9396
9397 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009398 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009399 // This must be a constant between -255 and -1, for negated ADD
9400 // immediates. This can be used in GCC with an "n" modifier that
9401 // prints the negated value, for use with SUB instructions. It is
9402 // not useful otherwise but is implemented for compatibility.
9403 if (CVal >= -255 && CVal <= -1)
9404 break;
9405 } else {
9406 // This must be a constant between -4095 and 4095. It is not clear
9407 // what this constraint is intended for. Implemented for
9408 // compatibility with GCC.
9409 if (CVal >= -4095 && CVal <= 4095)
9410 break;
9411 }
9412 return;
9413
9414 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009415 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009416 // A 32-bit value where only one byte has a nonzero value. Exclude
9417 // zero to match GCC. This constraint is used by GCC internally for
9418 // constants that can be loaded with a move/shift combination.
9419 // It is not useful otherwise but is implemented for compatibility.
9420 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9421 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009422 } else if (Subtarget->isThumb2()) {
9423 // A constant whose bitwise inverse can be used as an immediate
9424 // value in a data-processing instruction. This can be used in GCC
9425 // with a "B" modifier that prints the inverted value, for use with
9426 // BIC and MVN instructions. It is not useful otherwise but is
9427 // implemented for compatibility.
9428 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9429 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009430 } else {
9431 // A constant whose bitwise inverse can be used as an immediate
9432 // value in a data-processing instruction. This can be used in GCC
9433 // with a "B" modifier that prints the inverted value, for use with
9434 // BIC and MVN instructions. It is not useful otherwise but is
9435 // implemented for compatibility.
9436 if (ARM_AM::getSOImmVal(~CVal) != -1)
9437 break;
9438 }
9439 return;
9440
9441 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009442 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009443 // This must be a constant between -7 and 7,
9444 // for 3-operand ADD/SUB immediate instructions.
9445 if (CVal >= -7 && CVal < 7)
9446 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009447 } else if (Subtarget->isThumb2()) {
9448 // A constant whose negation can be used as an immediate value in a
9449 // data-processing instruction. This can be used in GCC with an "n"
9450 // modifier that prints the negated value, for use with SUB
9451 // instructions. It is not useful otherwise but is implemented for
9452 // compatibility.
9453 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9454 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009455 } else {
9456 // A constant whose negation can be used as an immediate value in a
9457 // data-processing instruction. This can be used in GCC with an "n"
9458 // modifier that prints the negated value, for use with SUB
9459 // instructions. It is not useful otherwise but is implemented for
9460 // compatibility.
9461 if (ARM_AM::getSOImmVal(-CVal) != -1)
9462 break;
9463 }
9464 return;
9465
9466 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009467 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009468 // This must be a multiple of 4 between 0 and 1020, for
9469 // ADD sp + immediate.
9470 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9471 break;
9472 } else {
9473 // A power of two or a constant between 0 and 32. This is used in
9474 // GCC for the shift amount on shifted register operands, but it is
9475 // useful in general for any shift amounts.
9476 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9477 break;
9478 }
9479 return;
9480
9481 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009482 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009483 // This must be a constant between 0 and 31, for shift amounts.
9484 if (CVal >= 0 && CVal <= 31)
9485 break;
9486 }
9487 return;
9488
9489 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009490 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009491 // This must be a multiple of 4 between -508 and 508, for
9492 // ADD/SUB sp = sp + immediate.
9493 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9494 break;
9495 }
9496 return;
9497 }
9498 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9499 break;
9500 }
9501
9502 if (Result.getNode()) {
9503 Ops.push_back(Result);
9504 return;
9505 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009506 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009507}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009508
9509bool
9510ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9511 // The ARM target isn't yet aware of offsets.
9512 return false;
9513}
Evan Cheng39382422009-10-28 01:44:26 +00009514
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009515bool ARM::isBitFieldInvertedMask(unsigned v) {
9516 if (v == 0xffffffff)
9517 return 0;
9518 // there can be 1's on either or both "outsides", all the "inside"
9519 // bits must be 0's
9520 unsigned int lsb = 0, msb = 31;
9521 while (v & (1 << msb)) --msb;
9522 while (v & (1 << lsb)) ++lsb;
9523 for (unsigned int i = lsb; i <= msb; ++i) {
9524 if (v & (1 << i))
9525 return 0;
9526 }
9527 return 1;
9528}
9529
Evan Cheng39382422009-10-28 01:44:26 +00009530/// isFPImmLegal - Returns true if the target can instruction select the
9531/// specified FP immediate natively. If false, the legalizer will
9532/// materialize the FP immediate as a load from a constant pool.
9533bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9534 if (!Subtarget->hasVFP3())
9535 return false;
9536 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009537 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009538 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009539 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009540 return false;
9541}
Bob Wilson65ffec42010-09-21 17:56:22 +00009542
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009543/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009544/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9545/// specified in the intrinsic calls.
9546bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9547 const CallInst &I,
9548 unsigned Intrinsic) const {
9549 switch (Intrinsic) {
9550 case Intrinsic::arm_neon_vld1:
9551 case Intrinsic::arm_neon_vld2:
9552 case Intrinsic::arm_neon_vld3:
9553 case Intrinsic::arm_neon_vld4:
9554 case Intrinsic::arm_neon_vld2lane:
9555 case Intrinsic::arm_neon_vld3lane:
9556 case Intrinsic::arm_neon_vld4lane: {
9557 Info.opc = ISD::INTRINSIC_W_CHAIN;
9558 // Conservatively set memVT to the entire set of vectors loaded.
9559 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9560 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9561 Info.ptrVal = I.getArgOperand(0);
9562 Info.offset = 0;
9563 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9564 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9565 Info.vol = false; // volatile loads with NEON intrinsics not supported
9566 Info.readMem = true;
9567 Info.writeMem = false;
9568 return true;
9569 }
9570 case Intrinsic::arm_neon_vst1:
9571 case Intrinsic::arm_neon_vst2:
9572 case Intrinsic::arm_neon_vst3:
9573 case Intrinsic::arm_neon_vst4:
9574 case Intrinsic::arm_neon_vst2lane:
9575 case Intrinsic::arm_neon_vst3lane:
9576 case Intrinsic::arm_neon_vst4lane: {
9577 Info.opc = ISD::INTRINSIC_VOID;
9578 // Conservatively set memVT to the entire set of vectors stored.
9579 unsigned NumElts = 0;
9580 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009581 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009582 if (!ArgTy->isVectorTy())
9583 break;
9584 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9585 }
9586 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9587 Info.ptrVal = I.getArgOperand(0);
9588 Info.offset = 0;
9589 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9590 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9591 Info.vol = false; // volatile stores with NEON intrinsics not supported
9592 Info.readMem = false;
9593 Info.writeMem = true;
9594 return true;
9595 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009596 case Intrinsic::arm_strexd: {
9597 Info.opc = ISD::INTRINSIC_W_CHAIN;
9598 Info.memVT = MVT::i64;
9599 Info.ptrVal = I.getArgOperand(2);
9600 Info.offset = 0;
9601 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009602 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009603 Info.readMem = false;
9604 Info.writeMem = true;
9605 return true;
9606 }
9607 case Intrinsic::arm_ldrexd: {
9608 Info.opc = ISD::INTRINSIC_W_CHAIN;
9609 Info.memVT = MVT::i64;
9610 Info.ptrVal = I.getArgOperand(0);
9611 Info.offset = 0;
9612 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009613 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009614 Info.readMem = true;
9615 Info.writeMem = false;
9616 return true;
9617 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009618 default:
9619 break;
9620 }
9621
9622 return false;
9623}