blob: 62c758931e1e8a3a1bf50960d889e786f47d99ce [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
Craig Topper0faf46c2012-08-12 03:16:37 +000093void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
94 MVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +000096 setOperationAction(ISD::LOAD, VT, Promote);
97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson5bafff32009-06-22 23:27:02 +000098
Craig Topper0faf46c2012-08-12 03:16:37 +000099 setOperationAction(ISD::STORE, VT, Promote);
100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
102
Craig Topper0faf46c2012-08-12 03:16:37 +0000103 MVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper0faf46c2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SETCC, VT, Custom);
106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000108 if (ElemTy == MVT::i32) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
110 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
111 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
112 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000113 } else {
Craig Topper0faf46c2012-08-12 03:16:37 +0000114 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
115 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
116 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
117 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000118 }
Craig Topper0faf46c2012-08-12 03:16:37 +0000119 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
120 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
121 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
123 setOperationAction(ISD::SELECT, VT, Expand);
124 setOperationAction(ISD::SELECT_CC, VT, Expand);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000126 if (VT.isInteger()) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000127 setOperationAction(ISD::SHL, VT, Custom);
128 setOperationAction(ISD::SRA, VT, Custom);
129 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 }
131
132 // Promote all bit-wise operations.
133 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000134 setOperationAction(ISD::AND, VT, Promote);
135 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
136 setOperationAction(ISD::OR, VT, Promote);
137 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
138 setOperationAction(ISD::XOR, VT, Promote);
139 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140 }
Bob Wilson16330762009-09-16 00:17:28 +0000141
142 // Neon does not support vector divide/remainder operations.
Craig Topper0faf46c2012-08-12 03:16:37 +0000143 setOperationAction(ISD::SDIV, VT, Expand);
144 setOperationAction(ISD::UDIV, VT, Expand);
145 setOperationAction(ISD::FDIV, VT, Expand);
146 setOperationAction(ISD::SREM, VT, Expand);
147 setOperationAction(ISD::UREM, VT, Expand);
148 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000149}
150
Craig Topper0faf46c2012-08-12 03:16:37 +0000151void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000152 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Craig Topper0faf46c2012-08-12 03:16:37 +0000156void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000157 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Chris Lattnerf0144122009-07-28 03:13:23 +0000161static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
162 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000163 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000164
Chris Lattner80ec2792009-08-02 00:34:36 +0000165 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Evan Chenga8e29892007-01-19 07:51:42 +0000168ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000170 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000171 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000172 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000173
Duncan Sands28b77e92011-09-06 19:07:46 +0000174 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Uses VFP for Thumb libfuncs if available.
178 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
179 // Single-precision floating-point arithmetic.
180 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
181 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
182 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
183 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Double-precision floating-point arithmetic.
186 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
187 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
188 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
189 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Single-precision comparisons.
192 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
193 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
194 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
195 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
196 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
197 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
198 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
199 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000200
Evan Chengb1df8f22007-04-27 08:15:43 +0000201 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000209
Evan Chengb1df8f22007-04-27 08:15:43 +0000210 // Double-precision comparisons.
211 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
212 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
213 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
214 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
215 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
216 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
217 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
218 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000219
Evan Chengb1df8f22007-04-27 08:15:43 +0000220 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000228
Evan Chengb1df8f22007-04-27 08:15:43 +0000229 // Floating-point to integer conversions.
230 // i64 conversions are done via library routines even when generating VFP
231 // instructions, so use the same ones.
232 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
233 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
234 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
235 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000236
Evan Chengb1df8f22007-04-27 08:15:43 +0000237 // Conversions between floating types.
238 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
239 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
240
241 // Integer to floating-point conversions.
242 // i64 conversions are done via library routines even when generating VFP
243 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000244 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
245 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000246 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
247 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
248 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
249 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
250 }
Evan Chenga8e29892007-01-19 07:51:42 +0000251 }
252
Bob Wilson2f954612009-05-22 17:38:41 +0000253 // These libcalls are not available in 32-bit.
254 setLibcallName(RTLIB::SHL_I128, 0);
255 setLibcallName(RTLIB::SRL_I128, 0);
256 setLibcallName(RTLIB::SRA_I128, 0);
257
Evan Cheng07043272012-02-21 20:46:00 +0000258 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000259 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000260 // RTABI chapter 4.1.2, Table 2
261 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
262 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
263 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
264 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
265 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
266 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
268 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
269
270 // Double-precision floating-point comparison helper functions
271 // RTABI chapter 4.1.2, Table 3
272 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
273 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
274 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
275 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
276 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
277 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
278 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
279 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
280 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
281 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
282 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
283 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
284 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
285 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
286 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
287 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
288 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
296
297 // Single-precision floating-point arithmetic helper functions
298 // RTABI chapter 4.1.2, Table 4
299 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
300 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
301 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
302 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
303 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
304 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
306 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
307
308 // Single-precision floating-point comparison helper functions
309 // RTABI chapter 4.1.2, Table 5
310 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
311 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
312 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
313 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
314 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
315 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
316 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
317 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
318 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
319 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
320 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
321 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
322 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
323 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
324 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
325 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
326 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
334
335 // Floating-point to integer conversions.
336 // RTABI chapter 4.1.2, Table 6
337 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
338 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
339 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
340 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
341 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
342 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
343 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
344 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
345 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
353
354 // Conversions between floating types.
355 // RTABI chapter 4.1.2, Table 7
356 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
357 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
358 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000360
361 // Integer to floating-point conversions.
362 // RTABI chapter 4.1.2, Table 8
363 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
364 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
365 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
366 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
367 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
368 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
369 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
370 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
371 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
379
380 // Long long helper functions
381 // RTABI chapter 4.2, Table 9
382 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000383 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
384 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
385 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
386 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
392
393 // Integer division functions
394 // RTABI chapter 4.3.1
395 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
396 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
397 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000398 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000399 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
400 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
401 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000402 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000403 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
404 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000406 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000407 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
408 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000409 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000410 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000411
412 // Memory operations
413 // RTABI chapter 4.3.4
414 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
415 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
416 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000417 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
Bob Wilson2fef4572011-10-07 16:59:21 +0000422 // Use divmod compiler-rt calls for iOS 5.0 and later.
423 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
424 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
425 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
426 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
427 }
428
David Goodwinf1daf7d2009-07-08 23:10:31 +0000429 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000430 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000431 else
Craig Topper420761a2012-04-20 07:30:17 +0000432 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000433 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
434 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000435 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000436 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000437 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000438
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000440 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000441
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000442 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
443 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
444 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
446 setTruncStoreAction((MVT::SimpleValueType)VT,
447 (MVT::SimpleValueType)InnerVT, Expand);
448 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
449 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 }
452
Lang Hames45b5f882012-03-15 18:49:02 +0000453 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000471 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
472 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
474 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
475 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // FIXME: Code duplication: FDIV and FREM are expanded always, see
477 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
479 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Create unittest.
481 // In another words, find a way when "copysign" appears in DAG with vector
482 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Code duplication: SETCC has custom operation action, see
485 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000486 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000487 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
489 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
491 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
492 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
493 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
494 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
498 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
499 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000500 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000501 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
502 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
503 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
504 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
505 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000506
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000507 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
508 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
509 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000517
Bob Wilson642b3292009-09-16 00:32:15 +0000518 // Neon does not support some operations on v1i64 and v2i64 types.
519 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000520 // Custom handling for some quad-vector types to detect VMULL.
521 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
522 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
523 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000524 // Custom handling for some vector types to avoid expensive expansions
525 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
526 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
527 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
528 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000529 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
530 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000531 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000532 // a destination type that is wider than the source, and nor does
533 // it have a FP_TO_[SU]INT instruction with a narrower destination than
534 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000535 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
536 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000537 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
538 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000539
Bob Wilson1c3ef902011-02-07 17:43:21 +0000540 setTargetDAGCombine(ISD::INTRINSIC_VOID);
541 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000542 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
543 setTargetDAGCombine(ISD::SHL);
544 setTargetDAGCombine(ISD::SRL);
545 setTargetDAGCombine(ISD::SRA);
546 setTargetDAGCombine(ISD::SIGN_EXTEND);
547 setTargetDAGCombine(ISD::ZERO_EXTEND);
548 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000549 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000550 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000551 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000552 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
553 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000554 setTargetDAGCombine(ISD::FP_TO_SINT);
555 setTargetDAGCombine(ISD::FP_TO_UINT);
556 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000557
James Molloy873fd5f2012-02-20 09:24:05 +0000558 // It is legal to extload from v4i8 to v4i16 or v4i32.
559 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
560 MVT::v4i16, MVT::v2i16,
561 MVT::v2i32};
562 for (unsigned i = 0; i < 6; ++i) {
563 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
564 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
565 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
566 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000567 }
568
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000569 // ARM and Thumb2 support UMLAL/SMLAL.
570 if (!Subtarget->isThumb1Only())
571 setTargetDAGCombine(ISD::ADDC);
572
573
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000574 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000575
576 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000578
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000579 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000581
Evan Chenga8e29892007-01-19 07:51:42 +0000582 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000583 if (!Subtarget->isThumb1Only()) {
584 for (unsigned im = (unsigned)ISD::PRE_INC;
585 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setIndexedLoadAction(im, MVT::i1, Legal);
587 setIndexedLoadAction(im, MVT::i8, Legal);
588 setIndexedLoadAction(im, MVT::i16, Legal);
589 setIndexedLoadAction(im, MVT::i32, Legal);
590 setIndexedStoreAction(im, MVT::i1, Legal);
591 setIndexedStoreAction(im, MVT::i8, Legal);
592 setIndexedStoreAction(im, MVT::i16, Legal);
593 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000594 }
Evan Chenga8e29892007-01-19 07:51:42 +0000595 }
596
597 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000598 setOperationAction(ISD::MUL, MVT::i64, Expand);
599 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000600 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000601 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
602 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000603 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000604 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
605 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000606 setOperationAction(ISD::MULHS, MVT::i32, Expand);
607
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000608 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000609 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000610 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000611 setOperationAction(ISD::SRL, MVT::i64, Custom);
612 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000613
Evan Cheng342e3162011-08-30 01:34:54 +0000614 if (!Subtarget->isThumb1Only()) {
615 // FIXME: We should do this for Thumb1 as well.
616 setOperationAction(ISD::ADDC, MVT::i32, Custom);
617 setOperationAction(ISD::ADDE, MVT::i32, Custom);
618 setOperationAction(ISD::SUBC, MVT::i32, Custom);
619 setOperationAction(ISD::SUBE, MVT::i32, Custom);
620 }
621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000624 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000626 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000628
Chandler Carruth63974b22011-12-13 01:56:10 +0000629 // These just redirect to CTTZ and CTLZ on ARM.
630 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
631 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
632
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000633 // Only ARMv6 has BSWAP.
634 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000638 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000639 // v7M has a hardware divider
640 setOperationAction(ISD::SDIV, MVT::i32, Expand);
641 setOperationAction(ISD::UDIV, MVT::i32, Expand);
642 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::UREM, MVT::i32, Expand);
645 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
646 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
649 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
650 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
651 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000652 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000653
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000654 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000655
Evan Chenga8e29892007-01-19 07:51:42 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VASTART, MVT::Other, Custom);
658 setOperationAction(ISD::VAARG, MVT::Other, Expand);
659 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
660 setOperationAction(ISD::VAEND, MVT::Other, Expand);
661 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
662 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000663
664 if (!Subtarget->isTargetDarwin()) {
665 // Non-Darwin platforms may return values in these registers via the
666 // personality function.
667 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
668 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
669 setExceptionPointerRegister(ARM::R0);
670 setExceptionSelectorRegister(ARM::R1);
671 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000672
Evan Cheng3a1588a2010-04-15 22:20:34 +0000673 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000674 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
675 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000676 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000677 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000678 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000679 // membarrier needs custom lowering; the rest are legal and handled
680 // normally.
681 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000682 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000683 // Custom lowering for 64-bit ops
684 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
689 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000690 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000691 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
692 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000693 } else {
694 // Set them all for expansion, which will force libcalls.
695 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000696 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000704 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000708 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000709 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
710 // Unordered/Monotonic case.
711 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
712 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000713 // Since the libcalls include locking, fold in the fences
714 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 }
Evan Chenga8e29892007-01-19 07:51:42 +0000716
Evan Cheng416941d2010-11-04 05:19:35 +0000717 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000718
Eli Friedmana2c6f452010-06-26 04:36:50 +0000719 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
720 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
722 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000723 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000726 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
727 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000728 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
729 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000730 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000731 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
732 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000733
734 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000736 if (Subtarget->isTargetDarwin()) {
737 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
738 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000739 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000740 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SETCC, MVT::i32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f32, Expand);
744 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000745 setOperationAction(ISD::SELECT, MVT::i32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f32, Custom);
747 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000751
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
753 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
755 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
756 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000757
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000758 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN, MVT::f64, Expand);
760 setOperationAction(ISD::FSIN, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f32, Expand);
762 setOperationAction(ISD::FCOS, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f64, Expand);
764 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
768 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000769 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::FPOW, MVT::f64, Expand);
771 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000772
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000773 if (!Subtarget->hasVFP4()) {
774 setOperationAction(ISD::FMA, MVT::f64, Expand);
775 setOperationAction(ISD::FMA, MVT::f32, Expand);
776 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000777
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000778 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000779 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000780 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
781 if (Subtarget->hasVFP2()) {
782 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
783 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
784 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
785 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
786 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000787 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000788 if (!Subtarget->hasFP16()) {
789 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
790 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000791 }
Evan Cheng110cf482008-04-01 01:50:16 +0000792 }
Evan Chenga8e29892007-01-19 07:51:42 +0000793
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000794 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000795 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000796 setTargetDAGCombine(ISD::ADD);
797 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000798 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000799
Evan Chengc892aeb2012-02-23 01:19:06 +0000800 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000801 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000802 setTargetDAGCombine(ISD::OR);
803 setTargetDAGCombine(ISD::XOR);
804 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000805
Evan Cheng5fb468a2012-02-23 02:58:19 +0000806 if (Subtarget->hasV6Ops())
807 setTargetDAGCombine(ISD::SRL);
808
Evan Chenga8e29892007-01-19 07:51:42 +0000809 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000810
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000811 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
812 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000813 setSchedulingPreference(Sched::RegPressure);
814 else
815 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000816
Evan Cheng05219282011-01-06 06:52:41 +0000817 //// temporary - rewrite interface to use type
818 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000819 maxStoresPerMemset = 16;
820 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000821
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000822 // On ARM arguments smaller than 4 bytes are extended, so all arguments
823 // are at least 4 bytes aligned.
824 setMinStackArgumentAlignment(4);
825
Evan Chengfff606d2010-09-24 19:07:23 +0000826 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000827
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000828 // Prefer likely predicted branches to selects on out-of-order cores.
829 predictableSelectIsExpensive = Subtarget->isCortexA9();
830
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000831 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000832}
833
Andrew Trick32cec0a2011-01-19 02:35:27 +0000834// FIXME: It might make sense to define the representative register class as the
835// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
836// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
837// SPR's representative would be DPR_VFP2. This should work well if register
838// pressure tracking were modified such that a register use would increment the
839// pressure of the register class's representative and all of it's super
840// classes' representatives transitively. We have not implemented this because
841// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000842// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000843// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000844std::pair<const TargetRegisterClass*, uint8_t>
845ARMTargetLowering::findRepresentativeClass(EVT VT) const{
846 const TargetRegisterClass *RRC = 0;
847 uint8_t Cost = 1;
848 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000849 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000851 // Use DPR as representative register class for all floating point
852 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
853 // the cost is 1 for both f32 and f64.
854 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000855 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000856 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000857 // When NEON is used for SP, only half of the register file is available
858 // because operations that define both SP and DP results will be constrained
859 // to the VFP2 class (D0-D15). We currently model this constraint prior to
860 // coalescing by double-counting the SP regs. See the FIXME above.
861 if (Subtarget->useNEONForSinglePrecisionFP())
862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
865 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000867 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000870 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000871 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000872 break;
873 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000874 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000875 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000876 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000877 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000879}
880
Evan Chenga8e29892007-01-19 07:51:42 +0000881const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
882 switch (Opcode) {
883 default: return 0;
884 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000885 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000886 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000887 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
888 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000889 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
891 case ARMISD::tCALL: return "ARMISD::tCALL";
892 case ARMISD::BRCOND: return "ARMISD::BRCOND";
893 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000894 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000895 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
896 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
897 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000898 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000899 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::CMPFP: return "ARMISD::CMPFP";
901 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000902 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000903 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000904
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000906
Jim Grosbach3482c802010-01-18 19:58:49 +0000907 case ARMISD::RBIT: return "ARMISD::RBIT";
908
Bob Wilson76a312b2010-03-19 22:51:32 +0000909 case ARMISD::FTOSI: return "ARMISD::FTOSI";
910 case ARMISD::FTOUI: return "ARMISD::FTOUI";
911 case ARMISD::SITOF: return "ARMISD::SITOF";
912 case ARMISD::UITOF: return "ARMISD::UITOF";
913
Evan Chenga8e29892007-01-19 07:51:42 +0000914 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
915 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
916 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000917
Evan Cheng342e3162011-08-30 01:34:54 +0000918 case ARMISD::ADDC: return "ARMISD::ADDC";
919 case ARMISD::ADDE: return "ARMISD::ADDE";
920 case ARMISD::SUBC: return "ARMISD::SUBC";
921 case ARMISD::SUBE: return "ARMISD::SUBE";
922
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000923 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
924 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000925
Evan Chengc5942082009-10-28 06:55:03 +0000926 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
927 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
928
Dale Johannesen51e28e62010-06-03 21:09:53 +0000929 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000930
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000931 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000932
Evan Cheng86198642009-08-07 00:34:42 +0000933 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
934
Jim Grosbach3728e962009-12-10 00:11:09 +0000935 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000936 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000937
Evan Chengdfed19f2010-11-03 06:34:55 +0000938 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
939
Bob Wilson5bafff32009-06-22 23:27:02 +0000940 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000941 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000943 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
944 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000945 case ARMISD::VCGEU: return "ARMISD::VCGEU";
946 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000947 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
948 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000949 case ARMISD::VCGTU: return "ARMISD::VCGTU";
950 case ARMISD::VTST: return "ARMISD::VTST";
951
952 case ARMISD::VSHL: return "ARMISD::VSHL";
953 case ARMISD::VSHRs: return "ARMISD::VSHRs";
954 case ARMISD::VSHRu: return "ARMISD::VSHRu";
955 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
956 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
957 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
958 case ARMISD::VSHRN: return "ARMISD::VSHRN";
959 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
960 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
961 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
962 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
963 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
964 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
965 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
966 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
967 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
968 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
969 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
970 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
971 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
972 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000973 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000974 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000975 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000976 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000977 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000978 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000979 case ARMISD::VREV64: return "ARMISD::VREV64";
980 case ARMISD::VREV32: return "ARMISD::VREV32";
981 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000982 case ARMISD::VZIP: return "ARMISD::VZIP";
983 case ARMISD::VUZP: return "ARMISD::VUZP";
984 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000985 case ARMISD::VTBL1: return "ARMISD::VTBL1";
986 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000987 case ARMISD::VMULLs: return "ARMISD::VMULLs";
988 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000989 case ARMISD::UMLAL: return "ARMISD::UMLAL";
990 case ARMISD::SMLAL: return "ARMISD::SMLAL";
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 *
Bob Wilsond49edb72012-08-03 04:06:28 +00001043ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1044 const TargetLibraryInfo *libInfo) const {
1045 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001046}
1047
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001048/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1049/// be used for loads / stores from the global.
1050unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1051 return (Subtarget->isThumb1Only() ? 127 : 4095);
1052}
1053
Evan Cheng1cc39842010-05-20 23:26:43 +00001054Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001055 unsigned NumVals = N->getNumValues();
1056 if (!NumVals)
1057 return Sched::RegPressure;
1058
1059 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001060 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001061 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001062 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001063 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001064 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001065 }
Evan Chengc10f5432010-05-28 23:25:23 +00001066
1067 if (!N->isMachineOpcode())
1068 return Sched::RegPressure;
1069
1070 // Load are scheduled for latency even if there instruction itinerary
1071 // is not available.
1072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001073 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001074
Evan Chenge837dea2011-06-28 19:10:37 +00001075 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001076 return Sched::RegPressure;
1077 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001078 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001079 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001080
Evan Cheng1cc39842010-05-20 23:26:43 +00001081 return Sched::RegPressure;
1082}
1083
Evan Chenga8e29892007-01-19 07:51:42 +00001084//===----------------------------------------------------------------------===//
1085// Lowering Code
1086//===----------------------------------------------------------------------===//
1087
Evan Chenga8e29892007-01-19 07:51:42 +00001088/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1089static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1090 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001091 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001092 case ISD::SETNE: return ARMCC::NE;
1093 case ISD::SETEQ: return ARMCC::EQ;
1094 case ISD::SETGT: return ARMCC::GT;
1095 case ISD::SETGE: return ARMCC::GE;
1096 case ISD::SETLT: return ARMCC::LT;
1097 case ISD::SETLE: return ARMCC::LE;
1098 case ISD::SETUGT: return ARMCC::HI;
1099 case ISD::SETUGE: return ARMCC::HS;
1100 case ISD::SETULT: return ARMCC::LO;
1101 case ISD::SETULE: return ARMCC::LS;
1102 }
1103}
1104
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001105/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1106static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001107 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001108 CondCode2 = ARMCC::AL;
1109 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001110 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001111 case ISD::SETEQ:
1112 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1113 case ISD::SETGT:
1114 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1115 case ISD::SETGE:
1116 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1117 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001118 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001119 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1120 case ISD::SETO: CondCode = ARMCC::VC; break;
1121 case ISD::SETUO: CondCode = ARMCC::VS; break;
1122 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1123 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1124 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1125 case ISD::SETLT:
1126 case ISD::SETULT: CondCode = ARMCC::LT; break;
1127 case ISD::SETLE:
1128 case ISD::SETULE: CondCode = ARMCC::LE; break;
1129 case ISD::SETNE:
1130 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1131 }
Evan Chenga8e29892007-01-19 07:51:42 +00001132}
1133
Bob Wilson1f595bb2009-04-17 19:07:39 +00001134//===----------------------------------------------------------------------===//
1135// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001136//===----------------------------------------------------------------------===//
1137
1138#include "ARMGenCallingConv.inc"
1139
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001140/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1141/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001142CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001143 bool Return,
1144 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001145 switch (CC) {
1146 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001147 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001148 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001149 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001150 if (!Subtarget->isAAPCS_ABI())
1151 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1152 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1153 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1154 }
1155 // Fallthrough
1156 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001157 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001158 if (!Subtarget->isAAPCS_ABI())
1159 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1160 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001161 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1162 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001163 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1164 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1165 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001166 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001167 if (!isVarArg)
1168 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1169 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001170 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001171 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001172 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001173 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001174 case CallingConv::GHC:
1175 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001176 }
1177}
1178
Dan Gohman98ca4f22009-08-05 01:29:28 +00001179/// LowerCallResult - Lower the result values of a call into the
1180/// appropriate copies out of appropriate physical registers.
1181SDValue
1182ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001183 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184 const SmallVectorImpl<ISD::InputArg> &Ins,
1185 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001186 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187
Bob Wilson1f595bb2009-04-17 19:07:39 +00001188 // Assign locations to each value returned by this call.
1189 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001190 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1191 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001193 CCAssignFnForNode(CallConv, /* Return*/ true,
1194 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001195
1196 // Copy all of the result registers out of their specified physreg.
1197 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1198 CCValAssign VA = RVLocs[i];
1199
Bob Wilson80915242009-04-25 00:33:20 +00001200 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001202 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001203 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001205 Chain = Lo.getValue(1);
1206 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001208 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001209 InFlag);
1210 Chain = Hi.getValue(1);
1211 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001212 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001213
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 if (VA.getLocVT() == MVT::v2f64) {
1215 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1216 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1217 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001218
1219 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001221 Chain = Lo.getValue(1);
1222 InFlag = Lo.getValue(2);
1223 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001224 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001225 Chain = Hi.getValue(1);
1226 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001227 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1229 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001230 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001231 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001232 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1233 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001234 Chain = Val.getValue(1);
1235 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 }
Bob Wilson80915242009-04-25 00:33:20 +00001237
1238 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001239 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001240 case CCValAssign::Full: break;
1241 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001242 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001243 break;
1244 }
1245
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247 }
1248
Dan Gohman98ca4f22009-08-05 01:29:28 +00001249 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250}
1251
Bob Wilsondee46d72009-04-17 20:35:10 +00001252/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001254ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1255 SDValue StackPtr, SDValue Arg,
1256 DebugLoc dl, SelectionDAG &DAG,
1257 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001258 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259 unsigned LocMemOffset = VA.getLocMemOffset();
1260 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1261 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001263 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001264 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001265}
1266
Dan Gohman98ca4f22009-08-05 01:29:28 +00001267void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001268 SDValue Chain, SDValue &Arg,
1269 RegsToPassVector &RegsToPass,
1270 CCValAssign &VA, CCValAssign &NextVA,
1271 SDValue &StackPtr,
1272 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001273 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001274
Jim Grosbache5165492009-11-09 00:11:35 +00001275 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001277 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1278
1279 if (NextVA.isRegLoc())
1280 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1281 else {
1282 assert(NextVA.isMemLoc());
1283 if (StackPtr.getNode() == 0)
1284 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1285
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1287 dl, DAG, NextVA,
1288 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001289 }
1290}
1291
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001293/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1294/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001295SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001296ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001297 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001298 SelectionDAG &DAG = CLI.DAG;
1299 DebugLoc &dl = CLI.DL;
1300 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1301 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1302 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1303 SDValue Chain = CLI.Chain;
1304 SDValue Callee = CLI.Callee;
1305 bool &isTailCall = CLI.IsTailCall;
1306 CallingConv::ID CallConv = CLI.CallConv;
1307 bool doesNotRet = CLI.DoesNotReturn;
1308 bool isVarArg = CLI.IsVarArg;
1309
Dale Johannesen51e28e62010-06-03 21:09:53 +00001310 MachineFunction &MF = DAG.getMachineFunction();
1311 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1312 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001313 // Disable tail calls if they're not supported.
1314 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001315 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001316 if (isTailCall) {
1317 // Check if it's really possible to do a tail call.
1318 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1319 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001320 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001321 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1322 // detected sibcalls.
1323 if (isTailCall) {
1324 ++NumTailCalls;
1325 IsSibCall = true;
1326 }
1327 }
Evan Chenga8e29892007-01-19 07:51:42 +00001328
Bob Wilson1f595bb2009-04-17 19:07:39 +00001329 // Analyze operands of the call, assigning locations to each operand.
1330 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001331 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1332 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001334 CCAssignFnForNode(CallConv, /* Return*/ false,
1335 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001336
Bob Wilson1f595bb2009-04-17 19:07:39 +00001337 // Get a count of how many bytes are to be pushed on the stack.
1338 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001339
Dale Johannesen51e28e62010-06-03 21:09:53 +00001340 // For tail calls, memory operands are available in our caller's stack.
1341 if (IsSibCall)
1342 NumBytes = 0;
1343
Evan Chenga8e29892007-01-19 07:51:42 +00001344 // Adjust the stack pointer for the new arguments...
1345 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001346 if (!IsSibCall)
1347 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001348
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001349 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001350
Bob Wilson5bafff32009-06-22 23:27:02 +00001351 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001353
Bob Wilson1f595bb2009-04-17 19:07:39 +00001354 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001355 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001356 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1357 i != e;
1358 ++i, ++realArgIdx) {
1359 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001360 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001362 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001363
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 // Promote the value if needed.
1365 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001366 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001367 case CCValAssign::Full: break;
1368 case CCValAssign::SExt:
1369 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1370 break;
1371 case CCValAssign::ZExt:
1372 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1373 break;
1374 case CCValAssign::AExt:
1375 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1376 break;
1377 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001379 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001380 }
1381
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001382 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001383 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001384 if (VA.getLocVT() == MVT::v2f64) {
1385 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1386 DAG.getConstant(0, MVT::i32));
1387 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1388 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001389
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001391 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1392
1393 VA = ArgLocs[++i]; // skip ahead to next loc
1394 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001396 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1397 } else {
1398 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001399
Dan Gohman98ca4f22009-08-05 01:29:28 +00001400 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1401 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001402 }
1403 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001404 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001405 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001406 }
1407 } else if (VA.isRegLoc()) {
1408 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001409 } else if (isByVal) {
1410 assert(VA.isMemLoc());
1411 unsigned offset = 0;
1412
1413 // True if this byval aggregate will be split between registers
1414 // and memory.
1415 if (CCInfo.isFirstByValRegValid()) {
1416 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1417 unsigned int i, j;
1418 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1419 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1420 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1421 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1422 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001423 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001424 MemOpChains.push_back(Load.getValue(1));
1425 RegsToPass.push_back(std::make_pair(j, Load));
1426 }
1427 offset = ARM::R4 - CCInfo.getFirstByValReg();
1428 CCInfo.clearFirstByValReg();
1429 }
1430
Manman Ren763a75d2012-06-01 02:44:42 +00001431 if (Flags.getByValSize() - 4*offset > 0) {
1432 unsigned LocMemOffset = VA.getLocMemOffset();
1433 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1434 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1435 StkPtrOff);
1436 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1437 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1438 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1439 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001440 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001441
Manman Ren763a75d2012-06-01 02:44:42 +00001442 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001443 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001444 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1445 Ops, array_lengthof(Ops)));
1446 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001447 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001448 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001449
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1451 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001452 }
Evan Chenga8e29892007-01-19 07:51:42 +00001453 }
1454
1455 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001456 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001457 &MemOpChains[0], MemOpChains.size());
1458
1459 // Build a sequence of copy-to-reg nodes chained together with token chain
1460 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001461 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001462 // Tail call byval lowering might overwrite argument registers so in case of
1463 // tail call optimization the copies to registers are lowered later.
1464 if (!isTailCall)
1465 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1466 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1467 RegsToPass[i].second, InFlag);
1468 InFlag = Chain.getValue(1);
1469 }
Evan Chenga8e29892007-01-19 07:51:42 +00001470
Dale Johannesen51e28e62010-06-03 21:09:53 +00001471 // For tail calls lower the arguments to the 'real' stack slot.
1472 if (isTailCall) {
1473 // Force all the incoming stack arguments to be loaded from the stack
1474 // before any new outgoing arguments are stored to the stack, because the
1475 // outgoing stack slots may alias the incoming argument stack slots, and
1476 // the alias isn't otherwise explicit. This is slightly more conservative
1477 // than necessary, because it means that each store effectively depends
1478 // on every argument instead of just those arguments it would clobber.
1479
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001480 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001481 InFlag = SDValue();
1482 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1483 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1484 RegsToPass[i].second, InFlag);
1485 InFlag = Chain.getValue(1);
1486 }
1487 InFlag =SDValue();
1488 }
1489
Bill Wendling056292f2008-09-16 21:48:12 +00001490 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1491 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1492 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001493 bool isDirect = false;
1494 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001495 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001496 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001497
1498 if (EnableARMLongCalls) {
1499 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1500 && "long-calls with non-static relocation model!");
1501 // Handle a global address or an external symbol. If it's not one of
1502 // those, the target's already in a register, so we don't need to do
1503 // anything extra.
1504 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001505 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001506 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001507 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001508 ARMConstantPoolValue *CPV =
1509 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1510
Jim Grosbache7b52522010-04-14 22:28:31 +00001511 // Get the address of the callee into a register
1512 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1513 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1514 Callee = DAG.getLoad(getPointerTy(), dl,
1515 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001516 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001517 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001518 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1519 const char *Sym = S->getSymbol();
1520
1521 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001522 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001523 ARMConstantPoolValue *CPV =
1524 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1525 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001526 // Get the address of the callee into a register
1527 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1528 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1529 Callee = DAG.getLoad(getPointerTy(), dl,
1530 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001531 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001532 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001533 }
1534 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001535 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001536 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001537 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001538 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001539 getTargetMachine().getRelocationModel() != Reloc::Static;
1540 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001541 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001542 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001543 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001544 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001545 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001546 ARMConstantPoolValue *CPV =
1547 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001548 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001549 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001550 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001551 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001552 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001553 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001554 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001555 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001556 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001557 } else {
1558 // On ELF targets for PIC code, direct calls should go through the PLT
1559 unsigned OpFlags = 0;
1560 if (Subtarget->isTargetELF() &&
1561 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1562 OpFlags = ARMII::MO_PLT;
1563 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1564 }
Bill Wendling056292f2008-09-16 21:48:12 +00001565 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001566 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001567 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001568 getTargetMachine().getRelocationModel() != Reloc::Static;
1569 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001570 // tBX takes a register source operand.
1571 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001572 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001573 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001574 ARMConstantPoolValue *CPV =
1575 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1576 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001577 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001579 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001580 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001581 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001582 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001583 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001584 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001585 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001586 } else {
1587 unsigned OpFlags = 0;
1588 // On ELF targets for PIC code, direct calls should go through the PLT
1589 if (Subtarget->isTargetELF() &&
1590 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1591 OpFlags = ARMII::MO_PLT;
1592 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1593 }
Evan Chenga8e29892007-01-19 07:51:42 +00001594 }
1595
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001596 // FIXME: handle tail calls differently.
1597 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001598 if (Subtarget->isThumb()) {
1599 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001600 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001601 else if (doesNotRet && isDirect && !isARMFunc &&
1602 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1603 // "mov lr, pc; b _foo" to avoid confusing the RSP
1604 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001605 else
1606 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1607 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001608 if (!isDirect && !Subtarget->hasV5TOps()) {
1609 CallOpc = ARMISD::CALL_NOLINK;
1610 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1611 // "mov lr, pc; b _foo" to avoid confusing the RSP
1612 CallOpc = ARMISD::CALL_NOLINK;
1613 else
1614 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001615 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001616
Dan Gohman475871a2008-07-27 21:46:04 +00001617 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001618 Ops.push_back(Chain);
1619 Ops.push_back(Callee);
1620
1621 // Add argument registers to the end of the list so that they are known live
1622 // into the call.
1623 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1624 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1625 RegsToPass[i].second.getValueType()));
1626
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001627 // Add a register mask operand representing the call-preserved registers.
1628 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1629 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1630 assert(Mask && "Missing call preserved mask for calling convention");
1631 Ops.push_back(DAG.getRegisterMask(Mask));
1632
Gabor Greifba36cb52008-08-28 21:40:38 +00001633 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001634 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001636 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001637 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001638 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001639
Duncan Sands4bdcb612008-07-02 17:40:58 +00001640 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001641 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001642 InFlag = Chain.getValue(1);
1643
Chris Lattnere563bbc2008-10-11 22:08:30 +00001644 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1645 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001647 InFlag = Chain.getValue(1);
1648
Bob Wilson1f595bb2009-04-17 19:07:39 +00001649 // Handle result values, copying them out of physregs into vregs that we
1650 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001651 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1652 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001653}
1654
Stuart Hastingsf222e592011-02-28 17:17:53 +00001655/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001656/// on the stack. Remember the next parameter register to allocate,
1657/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001658/// this.
1659void
Craig Topperc89c7442012-03-27 07:21:54 +00001660ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001661 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1662 assert((State->getCallOrPrologue() == Prologue ||
1663 State->getCallOrPrologue() == Call) &&
1664 "unhandled ParmContext");
1665 if ((!State->isFirstByValRegValid()) &&
1666 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1667 State->setFirstByValReg(reg);
1668 // At a call site, a byval parameter that is split between
1669 // registers and memory needs its size truncated here. In a
1670 // function prologue, such byval parameters are reassembled in
1671 // memory, and are not truncated.
1672 if (State->getCallOrPrologue() == Call) {
1673 unsigned excess = 4 * (ARM::R4 - reg);
1674 assert(size >= excess && "expected larger existing stack allocation");
1675 size -= excess;
1676 }
1677 }
1678 // Confiscate any remaining parameter registers to preclude their
1679 // assignment to subsequent parameters.
1680 while (State->AllocateReg(GPRArgRegs, 4))
1681 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001682}
1683
Dale Johannesen51e28e62010-06-03 21:09:53 +00001684/// MatchingStackOffset - Return true if the given stack call argument is
1685/// already available in the same position (relatively) of the caller's
1686/// incoming argument stack.
1687static
1688bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1689 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001690 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001691 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1692 int FI = INT_MAX;
1693 if (Arg.getOpcode() == ISD::CopyFromReg) {
1694 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001695 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001696 return false;
1697 MachineInstr *Def = MRI->getVRegDef(VR);
1698 if (!Def)
1699 return false;
1700 if (!Flags.isByVal()) {
1701 if (!TII->isLoadFromStackSlot(Def, FI))
1702 return false;
1703 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001704 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001705 }
1706 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1707 if (Flags.isByVal())
1708 // ByVal argument is passed in as a pointer but it's now being
1709 // dereferenced. e.g.
1710 // define @foo(%struct.X* %A) {
1711 // tail call @bar(%struct.X* byval %A)
1712 // }
1713 return false;
1714 SDValue Ptr = Ld->getBasePtr();
1715 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1716 if (!FINode)
1717 return false;
1718 FI = FINode->getIndex();
1719 } else
1720 return false;
1721
1722 assert(FI != INT_MAX);
1723 if (!MFI->isFixedObjectIndex(FI))
1724 return false;
1725 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1726}
1727
1728/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1729/// for tail call optimization. Targets which want to do tail call
1730/// optimization should implement this function.
1731bool
1732ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1733 CallingConv::ID CalleeCC,
1734 bool isVarArg,
1735 bool isCalleeStructRet,
1736 bool isCallerStructRet,
1737 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001738 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001739 const SmallVectorImpl<ISD::InputArg> &Ins,
1740 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001741 const Function *CallerF = DAG.getMachineFunction().getFunction();
1742 CallingConv::ID CallerCC = CallerF->getCallingConv();
1743 bool CCMatch = CallerCC == CalleeCC;
1744
1745 // Look for obvious safe cases to perform tail call optimization that do not
1746 // require ABI changes. This is what gcc calls sibcall.
1747
Jim Grosbach7616b642010-06-16 23:45:49 +00001748 // Do not sibcall optimize vararg calls unless the call site is not passing
1749 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001750 if (isVarArg && !Outs.empty())
1751 return false;
1752
1753 // Also avoid sibcall optimization if either caller or callee uses struct
1754 // return semantics.
1755 if (isCalleeStructRet || isCallerStructRet)
1756 return false;
1757
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001758 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001759 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1760 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1761 // support in the assembler and linker to be used. This would need to be
1762 // fixed to fully support tail calls in Thumb1.
1763 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001764 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1765 // LR. This means if we need to reload LR, it takes an extra instructions,
1766 // which outweighs the value of the tail call; but here we don't know yet
1767 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001768 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001769 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001770
1771 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1772 // but we need to make sure there are enough registers; the only valid
1773 // registers are the 4 used for parameters. We don't currently do this
1774 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001775 if (Subtarget->isThumb1Only())
1776 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001777
Dale Johannesen51e28e62010-06-03 21:09:53 +00001778 // If the calling conventions do not match, then we'd better make sure the
1779 // results are returned in the same way as what the caller expects.
1780 if (!CCMatch) {
1781 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001782 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1783 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001784 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1785
1786 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001787 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1788 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001789 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1790
1791 if (RVLocs1.size() != RVLocs2.size())
1792 return false;
1793 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1794 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1795 return false;
1796 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1797 return false;
1798 if (RVLocs1[i].isRegLoc()) {
1799 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1800 return false;
1801 } else {
1802 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1803 return false;
1804 }
1805 }
1806 }
1807
1808 // If the callee takes no arguments then go on to check the results of the
1809 // call.
1810 if (!Outs.empty()) {
1811 // Check if stack adjustment is needed. For now, do not do this if any
1812 // argument is passed on the stack.
1813 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001814 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1815 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001816 CCInfo.AnalyzeCallOperands(Outs,
1817 CCAssignFnForNode(CalleeCC, false, isVarArg));
1818 if (CCInfo.getNextStackOffset()) {
1819 MachineFunction &MF = DAG.getMachineFunction();
1820
1821 // Check if the arguments are already laid out in the right way as
1822 // the caller's fixed stack objects.
1823 MachineFrameInfo *MFI = MF.getFrameInfo();
1824 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001825 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001826 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1827 i != e;
1828 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001829 CCValAssign &VA = ArgLocs[i];
1830 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001831 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001832 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001833 if (VA.getLocInfo() == CCValAssign::Indirect)
1834 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001835 if (VA.needsCustom()) {
1836 // f64 and vector types are split into multiple registers or
1837 // register/stack-slot combinations. The types will not match
1838 // the registers; give up on memory f64 refs until we figure
1839 // out what to do about this.
1840 if (!VA.isRegLoc())
1841 return false;
1842 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001843 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001844 if (RegVT == MVT::v2f64) {
1845 if (!ArgLocs[++i].isRegLoc())
1846 return false;
1847 if (!ArgLocs[++i].isRegLoc())
1848 return false;
1849 }
1850 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001851 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1852 MFI, MRI, TII))
1853 return false;
1854 }
1855 }
1856 }
1857 }
1858
1859 return true;
1860}
1861
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862SDValue
1863ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001864 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001866 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001867 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001868
Bob Wilsondee46d72009-04-17 20:35:10 +00001869 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001870 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001871
Bob Wilsondee46d72009-04-17 20:35:10 +00001872 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001873 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1874 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001875
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001877 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1878 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001879
1880 // If this is the first return lowered for this function, add
1881 // the regs to the liveout set for the function.
1882 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1883 for (unsigned i = 0; i != RVLocs.size(); ++i)
1884 if (RVLocs[i].isRegLoc())
1885 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001886 }
1887
Bob Wilson1f595bb2009-04-17 19:07:39 +00001888 SDValue Flag;
1889
1890 // Copy the result values into the output registers.
1891 for (unsigned i = 0, realRVLocIdx = 0;
1892 i != RVLocs.size();
1893 ++i, ++realRVLocIdx) {
1894 CCValAssign &VA = RVLocs[i];
1895 assert(VA.isRegLoc() && "Can only return in registers!");
1896
Dan Gohmanc9403652010-07-07 15:54:55 +00001897 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001898
1899 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001900 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001901 case CCValAssign::Full: break;
1902 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001903 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001904 break;
1905 }
1906
Bob Wilson1f595bb2009-04-17 19:07:39 +00001907 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001908 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001909 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1911 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001912 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001914
1915 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1916 Flag = Chain.getValue(1);
1917 VA = RVLocs[++i]; // skip ahead to next loc
1918 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1919 HalfGPRs.getValue(1), Flag);
1920 Flag = Chain.getValue(1);
1921 VA = RVLocs[++i]; // skip ahead to next loc
1922
1923 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1925 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001926 }
1927 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1928 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001929 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001931 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001932 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001933 VA = RVLocs[++i]; // skip ahead to next loc
1934 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1935 Flag);
1936 } else
1937 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1938
Bob Wilsondee46d72009-04-17 20:35:10 +00001939 // Guarantee that all emitted copies are
1940 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001941 Flag = Chain.getValue(1);
1942 }
1943
1944 SDValue result;
1945 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001947 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001949
1950 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001951}
1952
Evan Chengbf010eb2012-04-10 01:51:00 +00001953bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001954 if (N->getNumValues() != 1)
1955 return false;
1956 if (!N->hasNUsesOfValue(1, 0))
1957 return false;
1958
Evan Chengbf010eb2012-04-10 01:51:00 +00001959 SDValue TCChain = Chain;
1960 SDNode *Copy = *N->use_begin();
1961 if (Copy->getOpcode() == ISD::CopyToReg) {
1962 // If the copy has a glue operand, we conservatively assume it isn't safe to
1963 // perform a tail call.
1964 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1965 return false;
1966 TCChain = Copy->getOperand(0);
1967 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1968 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001969 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001970 SmallPtrSet<SDNode*, 2> Copies;
1971 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001972 UI != UE; ++UI) {
1973 if (UI->getOpcode() != ISD::CopyToReg)
1974 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001975 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001976 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001977 if (Copies.size() > 2)
1978 return false;
1979
1980 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1981 UI != UE; ++UI) {
1982 SDValue UseChain = UI->getOperand(0);
1983 if (Copies.count(UseChain.getNode()))
1984 // Second CopyToReg
1985 Copy = *UI;
1986 else
1987 // First CopyToReg
1988 TCChain = UseChain;
1989 }
1990 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001991 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001992 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001993 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001994 Copy = *Copy->use_begin();
1995 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001996 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001997 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001998 } else {
1999 return false;
2000 }
2001
Evan Cheng1bf891a2010-12-01 22:59:46 +00002002 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002003 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2004 UI != UE; ++UI) {
2005 if (UI->getOpcode() != ARMISD::RET_FLAG)
2006 return false;
2007 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002008 }
2009
Evan Chengbf010eb2012-04-10 01:51:00 +00002010 if (!HasRet)
2011 return false;
2012
2013 Chain = TCChain;
2014 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002015}
2016
Evan Cheng485fafc2011-03-21 01:19:09 +00002017bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002018 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002019 return false;
2020
2021 if (!CI->isTailCall())
2022 return false;
2023
2024 return !Subtarget->isThumb1Only();
2025}
2026
Bob Wilsonb62d2572009-11-03 00:02:05 +00002027// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2028// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2029// one of the above mentioned nodes. It has to be wrapped because otherwise
2030// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2031// be used to form addressing mode. These wrapped nodes will be selected
2032// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002033static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002034 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002035 // FIXME there is no actual debug info here
2036 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002037 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002038 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002039 if (CP->isMachineConstantPoolEntry())
2040 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2041 CP->getAlignment());
2042 else
2043 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2044 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002045 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002046}
2047
Jim Grosbache1102ca2010-07-19 17:20:38 +00002048unsigned ARMTargetLowering::getJumpTableEncoding() const {
2049 return MachineJumpTableInfo::EK_Inline;
2050}
2051
Dan Gohmand858e902010-04-17 15:26:15 +00002052SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2053 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002054 MachineFunction &MF = DAG.getMachineFunction();
2055 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2056 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002057 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002058 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002059 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002060 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2061 SDValue CPAddr;
2062 if (RelocM == Reloc::Static) {
2063 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2064 } else {
2065 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002066 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002067 ARMConstantPoolValue *CPV =
2068 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2069 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002070 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2071 }
2072 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2073 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002074 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002075 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002076 if (RelocM == Reloc::Static)
2077 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002078 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002079 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002080}
2081
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002082// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002083SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002084ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002085 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002086 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002087 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002088 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002089 MachineFunction &MF = DAG.getMachineFunction();
2090 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002091 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002092 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002093 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2094 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002095 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002096 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002097 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002098 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002099 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002100 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002101
Evan Chenge7e0d622009-11-06 22:24:13 +00002102 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002103 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002104
2105 // call __tls_get_addr.
2106 ArgListTy Args;
2107 ArgListEntry Entry;
2108 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002109 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002110 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002111 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002112 TargetLowering::CallLoweringInfo CLI(Chain,
2113 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002114 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002115 0, CallingConv::C, /*isTailCall=*/false,
2116 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002117 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002118 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002119 return CallResult.first;
2120}
2121
2122// Lower ISD::GlobalTLSAddress using the "initial exec" or
2123// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002124SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002125ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002126 SelectionDAG &DAG,
2127 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002128 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002129 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue Offset;
2131 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002132 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002134 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002135
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002136 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002137 MachineFunction &MF = DAG.getMachineFunction();
2138 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002139 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002140 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002141 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2142 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002143 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2144 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2145 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002146 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002148 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002149 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002150 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002151 Chain = Offset.getValue(1);
2152
Evan Chenge7e0d622009-11-06 22:24:13 +00002153 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002154 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002155
Evan Cheng9eda6892009-10-31 03:39:36 +00002156 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002157 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002158 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159 } else {
2160 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002161 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002162 ARMConstantPoolValue *CPV =
2163 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002164 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002165 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002166 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002167 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002168 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002169 }
2170
2171 // The address of the thread local variable is the add of the thread
2172 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002173 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174}
2175
Dan Gohman475871a2008-07-27 21:46:04 +00002176SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002177ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002178 // TODO: implement the "local dynamic" model
2179 assert(Subtarget->isTargetELF() &&
2180 "TLS not implemented for non-ELF targets");
2181 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002182
2183 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2184
2185 switch (model) {
2186 case TLSModel::GeneralDynamic:
2187 case TLSModel::LocalDynamic:
2188 return LowerToTLSGeneralDynamicModel(GA, DAG);
2189 case TLSModel::InitialExec:
2190 case TLSModel::LocalExec:
2191 return LowerToTLSExecModels(GA, DAG, model);
2192 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002193 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002194}
2195
Dan Gohman475871a2008-07-27 21:46:04 +00002196SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002197 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002198 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002199 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002200 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002201 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2202 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002203 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002204 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002205 ARMConstantPoolConstant::Create(GV,
2206 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002207 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002208 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002209 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002210 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002211 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002212 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002213 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002214 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002215 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002216 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002217 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002218 MachinePointerInfo::getGOT(),
2219 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002220 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002221 }
2222
2223 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002224 // pair. This is always cheaper.
2225 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002226 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002227 // FIXME: Once remat is capable of dealing with instructions with register
2228 // operands, expand this into two nodes.
2229 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2230 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002231 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002232 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2233 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2234 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2235 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002236 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002237 }
2238}
2239
Dan Gohman475871a2008-07-27 21:46:04 +00002240SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002241 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002242 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002243 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002244 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002245 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002246 MachineFunction &MF = DAG.getMachineFunction();
2247 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2248
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002249 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2250 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002251 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002252 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002253 // FIXME: Once remat is capable of dealing with instructions with register
2254 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002255 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002256 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2257 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2258
Evan Cheng53519f02011-01-21 18:55:51 +00002259 unsigned Wrapper = (RelocM == Reloc::PIC_)
2260 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2261 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002262 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002263 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2264 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002265 MachinePointerInfo::getGOT(),
2266 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002267 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002268 }
2269
2270 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002271 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002272 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002273 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002274 } else {
2275 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002276 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2277 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002278 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2279 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002280 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002281 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002282 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002283
Evan Cheng9eda6892009-10-31 03:39:36 +00002284 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002285 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002286 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002287 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002288
2289 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002290 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002291 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002292 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002293
Evan Cheng63476a82009-09-03 07:04:02 +00002294 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002295 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002296 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002297
2298 return Result;
2299}
2300
Dan Gohman475871a2008-07-27 21:46:04 +00002301SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002302 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002303 assert(Subtarget->isTargetELF() &&
2304 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002305 MachineFunction &MF = DAG.getMachineFunction();
2306 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002307 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002308 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002309 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002310 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002311 ARMConstantPoolValue *CPV =
2312 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2313 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002314 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002315 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002316 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002317 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002318 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002319 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002320 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002321}
2322
Jim Grosbach0e0da732009-05-12 23:59:14 +00002323SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002324ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2325 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002326 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002327 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2328 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002329 Op.getOperand(1), Val);
2330}
2331
2332SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002333ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2334 DebugLoc dl = Op.getDebugLoc();
2335 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2336 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2337}
2338
2339SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002340ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002341 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002342 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002343 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002344 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002345 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002346 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002347 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002348 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2349 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002350 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002351 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002352 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002353 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002354 EVT PtrVT = getPointerTy();
2355 DebugLoc dl = Op.getDebugLoc();
2356 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2357 SDValue CPAddr;
2358 unsigned PCAdj = (RelocM != Reloc::PIC_)
2359 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002360 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002361 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2362 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002363 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002364 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002365 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002366 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002367 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002368 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002369
2370 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002371 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002372 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2373 }
2374 return Result;
2375 }
Evan Cheng92e39162011-03-29 23:06:19 +00002376 case Intrinsic::arm_neon_vmulls:
2377 case Intrinsic::arm_neon_vmullu: {
2378 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2379 ? ARMISD::VMULLs : ARMISD::VMULLu;
2380 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2381 Op.getOperand(1), Op.getOperand(2));
2382 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002383 }
2384}
2385
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002386static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002387 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002388 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002389 if (!Subtarget->hasDataBarrier()) {
2390 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2391 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2392 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002393 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002394 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002395 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002396 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002397 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002398
2399 SDValue Op5 = Op.getOperand(5);
2400 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2401 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2402 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2403 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2404
2405 ARM_MB::MemBOpt DMBOpt;
2406 if (isDeviceBarrier)
2407 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2408 else
2409 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2410 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2411 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002412}
2413
Eli Friedman26689ac2011-08-03 21:06:02 +00002414
2415static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2416 const ARMSubtarget *Subtarget) {
2417 // FIXME: handle "fence singlethread" more efficiently.
2418 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002419 if (!Subtarget->hasDataBarrier()) {
2420 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2421 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2422 // here.
2423 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2424 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002425 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002426 DAG.getConstant(0, MVT::i32));
2427 }
2428
Eli Friedman26689ac2011-08-03 21:06:02 +00002429 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002430 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002431}
2432
Evan Chengdfed19f2010-11-03 06:34:55 +00002433static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2434 const ARMSubtarget *Subtarget) {
2435 // ARM pre v5TE and Thumb1 does not have preload instructions.
2436 if (!(Subtarget->isThumb2() ||
2437 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2438 // Just preserve the chain.
2439 return Op.getOperand(0);
2440
2441 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002442 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2443 if (!isRead &&
2444 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2445 // ARMv7 with MP extension has PLDW.
2446 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002447
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002448 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2449 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002450 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002451 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002452 isData = ~isData & 1;
2453 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002454
2455 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002456 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2457 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002458}
2459
Dan Gohman1e93df62010-04-17 14:41:14 +00002460static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2461 MachineFunction &MF = DAG.getMachineFunction();
2462 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2463
Evan Chenga8e29892007-01-19 07:51:42 +00002464 // vastart just stores the address of the VarArgsFrameIndex slot into the
2465 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002466 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002467 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002468 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002469 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002470 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2471 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002472}
2473
Dan Gohman475871a2008-07-27 21:46:04 +00002474SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002475ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2476 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002477 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002478 MachineFunction &MF = DAG.getMachineFunction();
2479 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2480
Craig Topper44d23822012-02-22 05:59:10 +00002481 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002482 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002483 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002484 else
Craig Topper420761a2012-04-20 07:30:17 +00002485 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002486
2487 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002488 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002489 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002490
2491 SDValue ArgValue2;
2492 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002493 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002494 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002495
2496 // Create load node to retrieve arguments from the stack.
2497 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002498 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002499 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002500 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002502 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002503 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002504 }
2505
Jim Grosbache5165492009-11-09 00:11:35 +00002506 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002507}
2508
Stuart Hastingsc7315872011-04-20 16:47:52 +00002509void
2510ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2511 unsigned &VARegSize, unsigned &VARegSaveSize)
2512 const {
2513 unsigned NumGPRs;
2514 if (CCInfo.isFirstByValRegValid())
2515 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2516 else {
2517 unsigned int firstUnalloced;
2518 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2519 sizeof(GPRArgRegs) /
2520 sizeof(GPRArgRegs[0]));
2521 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2522 }
2523
2524 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2525 VARegSize = NumGPRs * 4;
2526 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2527}
2528
2529// The remaining GPRs hold either the beginning of variable-argument
2530// data, or the beginning of an aggregate passed by value (usuall
2531// byval). Either way, we allocate stack slots adjacent to the data
2532// provided by our caller, and store the unallocated registers there.
2533// If this is a variadic function, the va_list pointer will begin with
2534// these values; otherwise, this reassembles a (byval) structure that
2535// was split between registers and memory.
2536void
2537ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2538 DebugLoc dl, SDValue &Chain,
2539 unsigned ArgOffset) const {
2540 MachineFunction &MF = DAG.getMachineFunction();
2541 MachineFrameInfo *MFI = MF.getFrameInfo();
2542 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2543 unsigned firstRegToSaveIndex;
2544 if (CCInfo.isFirstByValRegValid())
2545 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2546 else {
2547 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2548 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2549 }
2550
2551 unsigned VARegSize, VARegSaveSize;
2552 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2553 if (VARegSaveSize) {
2554 // If this function is vararg, store any remaining integer argument regs
2555 // to their spots on the stack so that they may be loaded by deferencing
2556 // the result of va_next.
2557 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002558 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2559 ArgOffset + VARegSaveSize
2560 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002561 false));
2562 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2563 getPointerTy());
2564
2565 SmallVector<SDValue, 4> MemOps;
2566 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002567 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002568 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002569 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002570 else
Craig Topper420761a2012-04-20 07:30:17 +00002571 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002572
2573 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2574 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2575 SDValue Store =
2576 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002577 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002578 false, false, 0);
2579 MemOps.push_back(Store);
2580 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2581 DAG.getConstant(4, getPointerTy()));
2582 }
2583 if (!MemOps.empty())
2584 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2585 &MemOps[0], MemOps.size());
2586 } else
2587 // This will point to the next argument passed via stack.
2588 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2589}
2590
Bob Wilson5bafff32009-06-22 23:27:02 +00002591SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002592ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002593 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002594 const SmallVectorImpl<ISD::InputArg>
2595 &Ins,
2596 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002597 SmallVectorImpl<SDValue> &InVals)
2598 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002599 MachineFunction &MF = DAG.getMachineFunction();
2600 MachineFrameInfo *MFI = MF.getFrameInfo();
2601
Bob Wilson1f595bb2009-04-17 19:07:39 +00002602 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2603
2604 // Assign locations to all of the incoming arguments.
2605 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002606 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2607 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002608 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002609 CCAssignFnForNode(CallConv, /* Return*/ false,
2610 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002611
2612 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002613 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002614
Stuart Hastingsf222e592011-02-28 17:17:53 +00002615 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2617 CCValAssign &VA = ArgLocs[i];
2618
Bob Wilsondee46d72009-04-17 20:35:10 +00002619 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002621 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002622
Bob Wilson1f595bb2009-04-17 19:07:39 +00002623 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002624 // f64 and vector types are split up into multiple registers or
2625 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002626 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002627 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002628 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002629 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002630 SDValue ArgValue2;
2631 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002632 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002633 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2634 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002635 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002636 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002637 } else {
2638 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2639 Chain, DAG, dl);
2640 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2642 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002643 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002644 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002645 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2646 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002647 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002648
Bob Wilson5bafff32009-06-22 23:27:02 +00002649 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002650 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002651
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002653 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002655 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002656 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002657 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002658 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002659 RC = AFI->isThumb1OnlyFunction() ?
2660 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2661 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002662 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002663 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002664
2665 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002666 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002667 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002668 }
2669
2670 // If this is an 8 or 16-bit value, it is really passed promoted
2671 // to 32 bits. Insert an assert[sz]ext to capture this, then
2672 // truncate to the right size.
2673 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002674 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002675 case CCValAssign::Full: break;
2676 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002677 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002678 break;
2679 case CCValAssign::SExt:
2680 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2681 DAG.getValueType(VA.getValVT()));
2682 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2683 break;
2684 case CCValAssign::ZExt:
2685 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2686 DAG.getValueType(VA.getValVT()));
2687 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2688 break;
2689 }
2690
Dan Gohman98ca4f22009-08-05 01:29:28 +00002691 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002692
2693 } else { // VA.isRegLoc()
2694
2695 // sanity check
2696 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002697 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002698
Stuart Hastingsf222e592011-02-28 17:17:53 +00002699 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002700
Stuart Hastingsf222e592011-02-28 17:17:53 +00002701 // Some Ins[] entries become multiple ArgLoc[] entries.
2702 // Process them only once.
2703 if (index != lastInsIndex)
2704 {
2705 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002706 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002707 // This can be changed with more analysis.
2708 // In case of tail call optimization mark all arguments mutable.
2709 // Since they could be overwritten by lowering of arguments in case of
2710 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002711 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002712 unsigned VARegSize, VARegSaveSize;
2713 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2714 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2715 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002716 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002717 int FI = MFI->CreateFixedObject(Bytes,
2718 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002719 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2720 } else {
2721 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2722 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002723
Stuart Hastingsf222e592011-02-28 17:17:53 +00002724 // Create load nodes to retrieve arguments from the stack.
2725 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2726 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2727 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002728 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002729 }
2730 lastInsIndex = index;
2731 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002732 }
2733 }
2734
2735 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002736 if (isVarArg)
2737 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002738
Dan Gohman98ca4f22009-08-05 01:29:28 +00002739 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002740}
2741
2742/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002743static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002744 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002745 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002746 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002747 // Maybe this has already been legalized into the constant pool?
2748 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002749 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002750 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002751 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002752 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002753 }
2754 }
2755 return false;
2756}
2757
Evan Chenga8e29892007-01-19 07:51:42 +00002758/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2759/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002760SDValue
2761ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002762 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002763 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002764 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002765 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002766 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002767 // Constant does not fit, try adjusting it by one?
2768 switch (CC) {
2769 default: break;
2770 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002771 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002772 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002773 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002774 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002775 }
2776 break;
2777 case ISD::SETULT:
2778 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002779 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002780 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002781 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002782 }
2783 break;
2784 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002785 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002786 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002787 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002788 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002789 }
2790 break;
2791 case ISD::SETULE:
2792 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002793 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002794 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002795 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002796 }
2797 break;
2798 }
2799 }
2800 }
2801
2802 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002803 ARMISD::NodeType CompareType;
2804 switch (CondCode) {
2805 default:
2806 CompareType = ARMISD::CMP;
2807 break;
2808 case ARMCC::EQ:
2809 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002810 // Uses only Z Flag
2811 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002812 break;
2813 }
Evan Cheng218977b2010-07-13 19:27:42 +00002814 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002815 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002816}
2817
2818/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002819SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002820ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002821 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002822 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002823 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002824 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002825 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002826 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2827 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002828}
2829
Bob Wilson79f56c92011-03-08 01:17:20 +00002830/// duplicateCmp - Glue values can have only one use, so this function
2831/// duplicates a comparison node.
2832SDValue
2833ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2834 unsigned Opc = Cmp.getOpcode();
2835 DebugLoc DL = Cmp.getDebugLoc();
2836 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2837 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2838
2839 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2840 Cmp = Cmp.getOperand(0);
2841 Opc = Cmp.getOpcode();
2842 if (Opc == ARMISD::CMPFP)
2843 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2844 else {
2845 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2846 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2847 }
2848 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2849}
2850
Bill Wendlingde2b1512010-08-11 08:43:16 +00002851SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2852 SDValue Cond = Op.getOperand(0);
2853 SDValue SelectTrue = Op.getOperand(1);
2854 SDValue SelectFalse = Op.getOperand(2);
2855 DebugLoc dl = Op.getDebugLoc();
2856
2857 // Convert:
2858 //
2859 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2860 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2861 //
2862 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2863 const ConstantSDNode *CMOVTrue =
2864 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2865 const ConstantSDNode *CMOVFalse =
2866 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2867
2868 if (CMOVTrue && CMOVFalse) {
2869 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2870 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2871
2872 SDValue True;
2873 SDValue False;
2874 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2875 True = SelectTrue;
2876 False = SelectFalse;
2877 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2878 True = SelectFalse;
2879 False = SelectTrue;
2880 }
2881
2882 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002883 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002884 SDValue ARMcc = Cond.getOperand(2);
2885 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002886 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002887 assert(True.getValueType() == VT);
2888 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002889 }
2890 }
2891 }
2892
Dan Gohmandb953892012-02-24 00:09:36 +00002893 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2894 // undefined bits before doing a full-word comparison with zero.
2895 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2896 DAG.getConstant(1, Cond.getValueType()));
2897
Bill Wendlingde2b1512010-08-11 08:43:16 +00002898 return DAG.getSelectCC(dl, Cond,
2899 DAG.getConstant(0, Cond.getValueType()),
2900 SelectTrue, SelectFalse, ISD::SETNE);
2901}
2902
Dan Gohmand858e902010-04-17 15:26:15 +00002903SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002904 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue LHS = Op.getOperand(0);
2906 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002907 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SDValue TrueVal = Op.getOperand(2);
2909 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002910 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002911
Owen Anderson825b72b2009-08-11 20:47:22 +00002912 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002913 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002914 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002915 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002916 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002917 }
2918
2919 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002920 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002921
Evan Cheng218977b2010-07-13 19:27:42 +00002922 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2923 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002924 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002925 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002926 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002927 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002928 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002929 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002930 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002931 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002932 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002933 }
2934 return Result;
2935}
2936
Evan Cheng218977b2010-07-13 19:27:42 +00002937/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2938/// to morph to an integer compare sequence.
2939static bool canChangeToInt(SDValue Op, bool &SeenZero,
2940 const ARMSubtarget *Subtarget) {
2941 SDNode *N = Op.getNode();
2942 if (!N->hasOneUse())
2943 // Otherwise it requires moving the value from fp to integer registers.
2944 return false;
2945 if (!N->getNumValues())
2946 return false;
2947 EVT VT = Op.getValueType();
2948 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2949 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2950 // vmrs are very slow, e.g. cortex-a8.
2951 return false;
2952
2953 if (isFloatingPointZero(Op)) {
2954 SeenZero = true;
2955 return true;
2956 }
2957 return ISD::isNormalLoad(N);
2958}
2959
2960static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2961 if (isFloatingPointZero(Op))
2962 return DAG.getConstant(0, MVT::i32);
2963
2964 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2965 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002966 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002967 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002968 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002969
2970 llvm_unreachable("Unknown VFP cmp argument!");
2971}
2972
2973static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2974 SDValue &RetVal1, SDValue &RetVal2) {
2975 if (isFloatingPointZero(Op)) {
2976 RetVal1 = DAG.getConstant(0, MVT::i32);
2977 RetVal2 = DAG.getConstant(0, MVT::i32);
2978 return;
2979 }
2980
2981 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2982 SDValue Ptr = Ld->getBasePtr();
2983 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2984 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002985 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002986 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002987 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002988
2989 EVT PtrType = Ptr.getValueType();
2990 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2991 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2992 PtrType, Ptr, DAG.getConstant(4, PtrType));
2993 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2994 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002995 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002996 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002997 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002998 return;
2999 }
3000
3001 llvm_unreachable("Unknown VFP cmp argument!");
3002}
3003
3004/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3005/// f32 and even f64 comparisons to integer ones.
3006SDValue
3007ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3008 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003009 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003010 SDValue LHS = Op.getOperand(2);
3011 SDValue RHS = Op.getOperand(3);
3012 SDValue Dest = Op.getOperand(4);
3013 DebugLoc dl = Op.getDebugLoc();
3014
Evan Chengfc501a32012-03-01 23:27:13 +00003015 bool LHSSeenZero = false;
3016 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3017 bool RHSSeenZero = false;
3018 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3019 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003020 // If unsafe fp math optimization is enabled and there are no other uses of
3021 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003022 // to an integer comparison.
3023 if (CC == ISD::SETOEQ)
3024 CC = ISD::SETEQ;
3025 else if (CC == ISD::SETUNE)
3026 CC = ISD::SETNE;
3027
Evan Chengfc501a32012-03-01 23:27:13 +00003028 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003029 SDValue ARMcc;
3030 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003031 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3032 bitcastf32Toi32(LHS, DAG), Mask);
3033 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3034 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003035 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3036 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3037 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3038 Chain, Dest, ARMcc, CCR, Cmp);
3039 }
3040
3041 SDValue LHS1, LHS2;
3042 SDValue RHS1, RHS2;
3043 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3044 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003045 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3046 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003047 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3048 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003049 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003050 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3051 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3052 }
3053
3054 return SDValue();
3055}
3056
3057SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3058 SDValue Chain = Op.getOperand(0);
3059 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3060 SDValue LHS = Op.getOperand(2);
3061 SDValue RHS = Op.getOperand(3);
3062 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003063 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003064
Owen Anderson825b72b2009-08-11 20:47:22 +00003065 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003066 SDValue ARMcc;
3067 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003069 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003070 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003071 }
3072
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003074
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003075 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003076 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3077 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3078 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3079 if (Result.getNode())
3080 return Result;
3081 }
3082
Evan Chenga8e29892007-01-19 07:51:42 +00003083 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003084 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003085
Evan Cheng218977b2010-07-13 19:27:42 +00003086 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3087 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003089 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003090 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003091 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003092 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003093 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3094 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003095 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003096 }
3097 return Res;
3098}
3099
Dan Gohmand858e902010-04-17 15:26:15 +00003100SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003101 SDValue Chain = Op.getOperand(0);
3102 SDValue Table = Op.getOperand(1);
3103 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003104 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003105
Owen Andersone50ed302009-08-10 22:56:29 +00003106 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003107 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3108 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003109 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003110 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003112 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3113 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003114 if (Subtarget->isThumb2()) {
3115 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3116 // which does another jump to the destination. This also makes it easier
3117 // to translate it to TBB / TBH later.
3118 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003120 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003121 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003122 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003123 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003124 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003125 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003126 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003127 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003128 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003129 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003130 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003131 MachinePointerInfo::getJumpTable(),
3132 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003133 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003135 }
Evan Chenga8e29892007-01-19 07:51:42 +00003136}
3137
Eli Friedman14e809c2011-11-09 23:36:02 +00003138static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003139 EVT VT = Op.getValueType();
3140 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003141
James Molloy873fd5f2012-02-20 09:24:05 +00003142 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3143 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3144 return Op;
3145 return DAG.UnrollVectorOp(Op.getNode());
3146 }
3147
3148 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3149 "Invalid type for custom lowering!");
3150 if (VT != MVT::v4i16)
3151 return DAG.UnrollVectorOp(Op.getNode());
3152
3153 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3154 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003155}
3156
Bob Wilson76a312b2010-03-19 22:51:32 +00003157static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003158 EVT VT = Op.getValueType();
3159 if (VT.isVector())
3160 return LowerVectorFP_TO_INT(Op, DAG);
3161
Bob Wilson76a312b2010-03-19 22:51:32 +00003162 DebugLoc dl = Op.getDebugLoc();
3163 unsigned Opc;
3164
3165 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003166 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003167 case ISD::FP_TO_SINT:
3168 Opc = ARMISD::FTOSI;
3169 break;
3170 case ISD::FP_TO_UINT:
3171 Opc = ARMISD::FTOUI;
3172 break;
3173 }
3174 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003175 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003176}
3177
Cameron Zwarich3007d332011-03-29 21:41:55 +00003178static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3179 EVT VT = Op.getValueType();
3180 DebugLoc dl = Op.getDebugLoc();
3181
Eli Friedman14e809c2011-11-09 23:36:02 +00003182 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3183 if (VT.getVectorElementType() == MVT::f32)
3184 return Op;
3185 return DAG.UnrollVectorOp(Op.getNode());
3186 }
3187
Duncan Sands1f6a3292011-08-12 14:54:45 +00003188 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3189 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003190 if (VT != MVT::v4f32)
3191 return DAG.UnrollVectorOp(Op.getNode());
3192
3193 unsigned CastOpc;
3194 unsigned Opc;
3195 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003196 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003197 case ISD::SINT_TO_FP:
3198 CastOpc = ISD::SIGN_EXTEND;
3199 Opc = ISD::SINT_TO_FP;
3200 break;
3201 case ISD::UINT_TO_FP:
3202 CastOpc = ISD::ZERO_EXTEND;
3203 Opc = ISD::UINT_TO_FP;
3204 break;
3205 }
3206
3207 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3208 return DAG.getNode(Opc, dl, VT, Op);
3209}
3210
Bob Wilson76a312b2010-03-19 22:51:32 +00003211static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3212 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003213 if (VT.isVector())
3214 return LowerVectorINT_TO_FP(Op, DAG);
3215
Bob Wilson76a312b2010-03-19 22:51:32 +00003216 DebugLoc dl = Op.getDebugLoc();
3217 unsigned Opc;
3218
3219 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003220 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003221 case ISD::SINT_TO_FP:
3222 Opc = ARMISD::SITOF;
3223 break;
3224 case ISD::UINT_TO_FP:
3225 Opc = ARMISD::UITOF;
3226 break;
3227 }
3228
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003229 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003230 return DAG.getNode(Opc, dl, VT, Op);
3231}
3232
Evan Cheng515fe3a2010-07-08 02:08:50 +00003233SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003234 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003235 SDValue Tmp0 = Op.getOperand(0);
3236 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003237 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003238 EVT VT = Op.getValueType();
3239 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003240 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3241 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3242 bool UseNEON = !InGPR && Subtarget->hasNEON();
3243
3244 if (UseNEON) {
3245 // Use VBSL to copy the sign bit.
3246 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3247 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3248 DAG.getTargetConstant(EncodedVal, MVT::i32));
3249 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3250 if (VT == MVT::f64)
3251 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3252 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3253 DAG.getConstant(32, MVT::i32));
3254 else /*if (VT == MVT::f32)*/
3255 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3256 if (SrcVT == MVT::f32) {
3257 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3258 if (VT == MVT::f64)
3259 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3260 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3261 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003262 } else if (VT == MVT::f32)
3263 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3264 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3265 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003266 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3267 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3268
3269 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3270 MVT::i32);
3271 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3272 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3273 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003274
Evan Chenge573fb32011-02-23 02:24:55 +00003275 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3276 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3277 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003278 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003279 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3280 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3281 DAG.getConstant(0, MVT::i32));
3282 } else {
3283 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3284 }
3285
3286 return Res;
3287 }
Evan Chengc143dd42011-02-11 02:28:55 +00003288
3289 // Bitcast operand 1 to i32.
3290 if (SrcVT == MVT::f64)
3291 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3292 &Tmp1, 1).getValue(1);
3293 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3294
Evan Chenge573fb32011-02-23 02:24:55 +00003295 // Or in the signbit with integer operations.
3296 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3297 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3298 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3299 if (VT == MVT::f32) {
3300 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3301 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3302 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3303 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003304 }
3305
Evan Chenge573fb32011-02-23 02:24:55 +00003306 // f64: Or the high part with signbit and then combine two parts.
3307 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3308 &Tmp0, 1);
3309 SDValue Lo = Tmp0.getValue(0);
3310 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3311 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3312 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003313}
3314
Evan Cheng2457f2c2010-05-22 01:47:14 +00003315SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3316 MachineFunction &MF = DAG.getMachineFunction();
3317 MachineFrameInfo *MFI = MF.getFrameInfo();
3318 MFI->setReturnAddressIsTaken(true);
3319
3320 EVT VT = Op.getValueType();
3321 DebugLoc dl = Op.getDebugLoc();
3322 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3323 if (Depth) {
3324 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3325 SDValue Offset = DAG.getConstant(4, MVT::i32);
3326 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3327 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003328 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003329 }
3330
3331 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003332 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003333 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3334}
3335
Dan Gohmand858e902010-04-17 15:26:15 +00003336SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003337 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3338 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003339
Owen Andersone50ed302009-08-10 22:56:29 +00003340 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003341 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3342 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003343 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003344 ? ARM::R7 : ARM::R11;
3345 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3346 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003347 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3348 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003349 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003350 return FrameAddr;
3351}
3352
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003353/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003354/// expand a bit convert where either the source or destination type is i64 to
3355/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3356/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3357/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003358static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003359 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3360 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003361 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003362
Bob Wilson9f3f0612010-04-17 05:30:19 +00003363 // This function is only supposed to be called for i64 types, either as the
3364 // source or destination of the bit convert.
3365 EVT SrcVT = Op.getValueType();
3366 EVT DstVT = N->getValueType(0);
3367 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003368 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003369
Bob Wilson9f3f0612010-04-17 05:30:19 +00003370 // Turn i64->f64 into VMOVDRR.
3371 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3373 DAG.getConstant(0, MVT::i32));
3374 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3375 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003376 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003377 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003378 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003379
Jim Grosbache5165492009-11-09 00:11:35 +00003380 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003381 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3382 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3383 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3384 // Merge the pieces into a single i64 value.
3385 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3386 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003387
Bob Wilson9f3f0612010-04-17 05:30:19 +00003388 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003389}
3390
Bob Wilson5bafff32009-06-22 23:27:02 +00003391/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003392/// Zero vectors are used to represent vector negation and in those cases
3393/// will be implemented with the NEON VNEG instruction. However, VNEG does
3394/// not support i64 elements, so sometimes the zero vectors will need to be
3395/// explicitly constructed. Regardless, use a canonical VMOV to create the
3396/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003397static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003398 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003399 // The canonical modified immediate encoding of a zero vector is....0!
3400 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3401 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3402 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003403 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003404}
3405
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003406/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3407/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003408SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3409 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003410 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3411 EVT VT = Op.getValueType();
3412 unsigned VTBits = VT.getSizeInBits();
3413 DebugLoc dl = Op.getDebugLoc();
3414 SDValue ShOpLo = Op.getOperand(0);
3415 SDValue ShOpHi = Op.getOperand(1);
3416 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003417 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003418 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003419
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003420 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3421
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003422 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3423 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3424 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3425 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3426 DAG.getConstant(VTBits, MVT::i32));
3427 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3428 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003429 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003430
3431 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3432 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003433 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003434 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003435 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003436 CCR, Cmp);
3437
3438 SDValue Ops[2] = { Lo, Hi };
3439 return DAG.getMergeValues(Ops, 2, dl);
3440}
3441
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003442/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3443/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003444SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3445 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003446 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3447 EVT VT = Op.getValueType();
3448 unsigned VTBits = VT.getSizeInBits();
3449 DebugLoc dl = Op.getDebugLoc();
3450 SDValue ShOpLo = Op.getOperand(0);
3451 SDValue ShOpHi = Op.getOperand(1);
3452 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003453 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003454
3455 assert(Op.getOpcode() == ISD::SHL_PARTS);
3456 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3457 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3458 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3459 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3460 DAG.getConstant(VTBits, MVT::i32));
3461 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3462 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3463
3464 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3465 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3466 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003467 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003468 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003469 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003470 CCR, Cmp);
3471
3472 SDValue Ops[2] = { Lo, Hi };
3473 return DAG.getMergeValues(Ops, 2, dl);
3474}
3475
Jim Grosbach4725ca72010-09-08 03:54:02 +00003476SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003477 SelectionDAG &DAG) const {
3478 // The rounding mode is in bits 23:22 of the FPSCR.
3479 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3480 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3481 // so that the shift + and get folded into a bitfield extract.
3482 DebugLoc dl = Op.getDebugLoc();
3483 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3484 DAG.getConstant(Intrinsic::arm_get_fpscr,
3485 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003486 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003487 DAG.getConstant(1U << 22, MVT::i32));
3488 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3489 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003490 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003491 DAG.getConstant(3, MVT::i32));
3492}
3493
Jim Grosbach3482c802010-01-18 19:58:49 +00003494static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3495 const ARMSubtarget *ST) {
3496 EVT VT = N->getValueType(0);
3497 DebugLoc dl = N->getDebugLoc();
3498
3499 if (!ST->hasV6T2Ops())
3500 return SDValue();
3501
3502 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3503 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3504}
3505
Bob Wilson5bafff32009-06-22 23:27:02 +00003506static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3507 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003508 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003509 DebugLoc dl = N->getDebugLoc();
3510
Bob Wilsond5448bb2010-11-18 21:16:28 +00003511 if (!VT.isVector())
3512 return SDValue();
3513
Bob Wilson5bafff32009-06-22 23:27:02 +00003514 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003515 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003516
Bob Wilsond5448bb2010-11-18 21:16:28 +00003517 // Left shifts translate directly to the vshiftu intrinsic.
3518 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003519 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003520 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3521 N->getOperand(0), N->getOperand(1));
3522
3523 assert((N->getOpcode() == ISD::SRA ||
3524 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3525
3526 // NEON uses the same intrinsics for both left and right shifts. For
3527 // right shifts, the shift amounts are negative, so negate the vector of
3528 // shift amounts.
3529 EVT ShiftVT = N->getOperand(1).getValueType();
3530 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3531 getZeroVector(ShiftVT, DAG, dl),
3532 N->getOperand(1));
3533 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3534 Intrinsic::arm_neon_vshifts :
3535 Intrinsic::arm_neon_vshiftu);
3536 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3537 DAG.getConstant(vshiftInt, MVT::i32),
3538 N->getOperand(0), NegatedCount);
3539}
3540
3541static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3542 const ARMSubtarget *ST) {
3543 EVT VT = N->getValueType(0);
3544 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003545
Eli Friedmance392eb2009-08-22 03:13:10 +00003546 // We can get here for a node like i32 = ISD::SHL i32, i64
3547 if (VT != MVT::i64)
3548 return SDValue();
3549
3550 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003551 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003552
Chris Lattner27a6c732007-11-24 07:07:01 +00003553 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3554 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003555 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003556 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003557
Chris Lattner27a6c732007-11-24 07:07:01 +00003558 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003559 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003560
Chris Lattner27a6c732007-11-24 07:07:01 +00003561 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003563 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003565 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003566
Chris Lattner27a6c732007-11-24 07:07:01 +00003567 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3568 // captures the result into a carry flag.
3569 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003570 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003571
Chris Lattner27a6c732007-11-24 07:07:01 +00003572 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003574
Chris Lattner27a6c732007-11-24 07:07:01 +00003575 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003577}
3578
Bob Wilson5bafff32009-06-22 23:27:02 +00003579static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3580 SDValue TmpOp0, TmpOp1;
3581 bool Invert = false;
3582 bool Swap = false;
3583 unsigned Opc = 0;
3584
3585 SDValue Op0 = Op.getOperand(0);
3586 SDValue Op1 = Op.getOperand(1);
3587 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003588 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003589 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3590 DebugLoc dl = Op.getDebugLoc();
3591
3592 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3593 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003594 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003595 case ISD::SETUNE:
3596 case ISD::SETNE: Invert = true; // Fallthrough
3597 case ISD::SETOEQ:
3598 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3599 case ISD::SETOLT:
3600 case ISD::SETLT: Swap = true; // Fallthrough
3601 case ISD::SETOGT:
3602 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3603 case ISD::SETOLE:
3604 case ISD::SETLE: Swap = true; // Fallthrough
3605 case ISD::SETOGE:
3606 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3607 case ISD::SETUGE: Swap = true; // Fallthrough
3608 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3609 case ISD::SETUGT: Swap = true; // Fallthrough
3610 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3611 case ISD::SETUEQ: Invert = true; // Fallthrough
3612 case ISD::SETONE:
3613 // Expand this to (OLT | OGT).
3614 TmpOp0 = Op0;
3615 TmpOp1 = Op1;
3616 Opc = ISD::OR;
3617 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3618 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3619 break;
3620 case ISD::SETUO: Invert = true; // Fallthrough
3621 case ISD::SETO:
3622 // Expand this to (OLT | OGE).
3623 TmpOp0 = Op0;
3624 TmpOp1 = Op1;
3625 Opc = ISD::OR;
3626 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3627 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3628 break;
3629 }
3630 } else {
3631 // Integer comparisons.
3632 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003633 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003634 case ISD::SETNE: Invert = true;
3635 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3636 case ISD::SETLT: Swap = true;
3637 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3638 case ISD::SETLE: Swap = true;
3639 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3640 case ISD::SETULT: Swap = true;
3641 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3642 case ISD::SETULE: Swap = true;
3643 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3644 }
3645
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003646 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003647 if (Opc == ARMISD::VCEQ) {
3648
3649 SDValue AndOp;
3650 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3651 AndOp = Op0;
3652 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3653 AndOp = Op1;
3654
3655 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003656 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003657 AndOp = AndOp.getOperand(0);
3658
3659 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3660 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003661 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3662 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003663 Invert = !Invert;
3664 }
3665 }
3666 }
3667
3668 if (Swap)
3669 std::swap(Op0, Op1);
3670
Owen Andersonc24cb352010-11-08 23:21:22 +00003671 // If one of the operands is a constant vector zero, attempt to fold the
3672 // comparison to a specialized compare-against-zero form.
3673 SDValue SingleOp;
3674 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3675 SingleOp = Op0;
3676 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3677 if (Opc == ARMISD::VCGE)
3678 Opc = ARMISD::VCLEZ;
3679 else if (Opc == ARMISD::VCGT)
3680 Opc = ARMISD::VCLTZ;
3681 SingleOp = Op1;
3682 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003683
Owen Andersonc24cb352010-11-08 23:21:22 +00003684 SDValue Result;
3685 if (SingleOp.getNode()) {
3686 switch (Opc) {
3687 case ARMISD::VCEQ:
3688 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3689 case ARMISD::VCGE:
3690 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3691 case ARMISD::VCLEZ:
3692 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3693 case ARMISD::VCGT:
3694 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3695 case ARMISD::VCLTZ:
3696 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3697 default:
3698 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3699 }
3700 } else {
3701 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3702 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003703
3704 if (Invert)
3705 Result = DAG.getNOT(dl, Result, VT);
3706
3707 return Result;
3708}
3709
Bob Wilsond3c42842010-06-14 22:19:57 +00003710/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3711/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003712/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003713static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3714 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003715 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003716 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003717
Bob Wilson827b2102010-06-15 19:05:35 +00003718 // SplatBitSize is set to the smallest size that splats the vector, so a
3719 // zero vector will always have SplatBitSize == 8. However, NEON modified
3720 // immediate instructions others than VMOV do not support the 8-bit encoding
3721 // of a zero vector, and the default encoding of zero is supposed to be the
3722 // 32-bit version.
3723 if (SplatBits == 0)
3724 SplatBitSize = 32;
3725
Bob Wilson5bafff32009-06-22 23:27:02 +00003726 switch (SplatBitSize) {
3727 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003728 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003729 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003730 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003731 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003732 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003733 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003734 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003735 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003736
3737 case 16:
3738 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003739 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003740 if ((SplatBits & ~0xff) == 0) {
3741 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003742 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003743 Imm = SplatBits;
3744 break;
3745 }
3746 if ((SplatBits & ~0xff00) == 0) {
3747 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003748 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003749 Imm = SplatBits >> 8;
3750 break;
3751 }
3752 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003753
3754 case 32:
3755 // NEON's 32-bit VMOV supports splat values where:
3756 // * only one byte is nonzero, or
3757 // * the least significant byte is 0xff and the second byte is nonzero, or
3758 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003759 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003760 if ((SplatBits & ~0xff) == 0) {
3761 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003762 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003763 Imm = SplatBits;
3764 break;
3765 }
3766 if ((SplatBits & ~0xff00) == 0) {
3767 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003768 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003769 Imm = SplatBits >> 8;
3770 break;
3771 }
3772 if ((SplatBits & ~0xff0000) == 0) {
3773 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003774 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003775 Imm = SplatBits >> 16;
3776 break;
3777 }
3778 if ((SplatBits & ~0xff000000) == 0) {
3779 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003780 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003781 Imm = SplatBits >> 24;
3782 break;
3783 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003784
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003785 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3786 if (type == OtherModImm) return SDValue();
3787
Bob Wilson5bafff32009-06-22 23:27:02 +00003788 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003789 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3790 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003791 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003792 Imm = SplatBits >> 8;
3793 SplatBits |= 0xff;
3794 break;
3795 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003796
3797 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003798 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3799 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003800 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003801 Imm = SplatBits >> 16;
3802 SplatBits |= 0xffff;
3803 break;
3804 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003805
3806 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3807 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3808 // VMOV.I32. A (very) minor optimization would be to replicate the value
3809 // and fall through here to test for a valid 64-bit splat. But, then the
3810 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003811 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003812
3813 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003814 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003815 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003816 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003817 uint64_t BitMask = 0xff;
3818 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003819 unsigned ImmMask = 1;
3820 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003821 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003822 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003823 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003824 Imm |= ImmMask;
3825 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003826 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003828 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003829 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003830 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003831 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003832 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003833 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003834 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003835 break;
3836 }
3837
Bob Wilson1a913ed2010-06-11 21:34:50 +00003838 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003839 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003840 }
3841
Bob Wilsoncba270d2010-07-13 21:16:48 +00003842 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3843 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003844}
3845
Lang Hamesc0a9f822012-03-29 21:56:11 +00003846SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3847 const ARMSubtarget *ST) const {
3848 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3849 return SDValue();
3850
3851 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3852 assert(Op.getValueType() == MVT::f32 &&
3853 "ConstantFP custom lowering should only occur for f32.");
3854
3855 // Try splatting with a VMOV.f32...
3856 APFloat FPVal = CFP->getValueAPF();
3857 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3858 if (ImmVal != -1) {
3859 DebugLoc DL = Op.getDebugLoc();
3860 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3861 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3862 NewVal);
3863 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3864 DAG.getConstant(0, MVT::i32));
3865 }
3866
3867 // If that fails, try a VMOV.i32
3868 EVT VMovVT;
3869 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3870 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3871 VMOVModImm);
3872 if (NewVal != SDValue()) {
3873 DebugLoc DL = Op.getDebugLoc();
3874 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3875 NewVal);
3876 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3877 VecConstant);
3878 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3879 DAG.getConstant(0, MVT::i32));
3880 }
3881
3882 // Finally, try a VMVN.i32
3883 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3884 VMVNModImm);
3885 if (NewVal != SDValue()) {
3886 DebugLoc DL = Op.getDebugLoc();
3887 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3888 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3889 VecConstant);
3890 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3891 DAG.getConstant(0, MVT::i32));
3892 }
3893
3894 return SDValue();
3895}
3896
3897
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003898static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003899 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003900 unsigned NumElts = VT.getVectorNumElements();
3901 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003902
3903 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3904 if (M[0] < 0)
3905 return false;
3906
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003907 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003908
3909 // If this is a VEXT shuffle, the immediate value is the index of the first
3910 // element. The other shuffle indices must be the successive elements after
3911 // the first one.
3912 unsigned ExpectedElt = Imm;
3913 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003914 // Increment the expected index. If it wraps around, it may still be
3915 // a VEXT but the source vectors must be swapped.
3916 ExpectedElt += 1;
3917 if (ExpectedElt == NumElts * 2) {
3918 ExpectedElt = 0;
3919 ReverseVEXT = true;
3920 }
3921
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003922 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003923 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003924 return false;
3925 }
3926
3927 // Adjust the index value if the source operands will be swapped.
3928 if (ReverseVEXT)
3929 Imm -= NumElts;
3930
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003931 return true;
3932}
3933
Bob Wilson8bb9e482009-07-26 00:39:34 +00003934/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3935/// instruction with the specified blocksize. (The order of the elements
3936/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003937static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003938 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3939 "Only possible block sizes for VREV are: 16, 32, 64");
3940
Bob Wilson8bb9e482009-07-26 00:39:34 +00003941 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003942 if (EltSz == 64)
3943 return false;
3944
3945 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003946 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003947 // If the first shuffle index is UNDEF, be optimistic.
3948 if (M[0] < 0)
3949 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003950
3951 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3952 return false;
3953
3954 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003955 if (M[i] < 0) continue; // ignore UNDEF indices
3956 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003957 return false;
3958 }
3959
3960 return true;
3961}
3962
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003963static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003964 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3965 // range, then 0 is placed into the resulting vector. So pretty much any mask
3966 // of 8 elements can work here.
3967 return VT == MVT::v8i8 && M.size() == 8;
3968}
3969
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003970static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003971 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3972 if (EltSz == 64)
3973 return false;
3974
Bob Wilsonc692cb72009-08-21 20:54:19 +00003975 unsigned NumElts = VT.getVectorNumElements();
3976 WhichResult = (M[0] == 0 ? 0 : 1);
3977 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003978 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3979 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003980 return false;
3981 }
3982 return true;
3983}
3984
Bob Wilson324f4f12009-12-03 06:40:55 +00003985/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3986/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3987/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003988static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003989 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3990 if (EltSz == 64)
3991 return false;
3992
3993 unsigned NumElts = VT.getVectorNumElements();
3994 WhichResult = (M[0] == 0 ? 0 : 1);
3995 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003996 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3997 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003998 return false;
3999 }
4000 return true;
4001}
4002
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004003static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004004 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4005 if (EltSz == 64)
4006 return false;
4007
Bob Wilsonc692cb72009-08-21 20:54:19 +00004008 unsigned NumElts = VT.getVectorNumElements();
4009 WhichResult = (M[0] == 0 ? 0 : 1);
4010 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004011 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004012 if ((unsigned) M[i] != 2 * i + WhichResult)
4013 return false;
4014 }
4015
4016 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004017 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004018 return false;
4019
4020 return true;
4021}
4022
Bob Wilson324f4f12009-12-03 06:40:55 +00004023/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4024/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4025/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004026static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004027 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4028 if (EltSz == 64)
4029 return false;
4030
4031 unsigned Half = VT.getVectorNumElements() / 2;
4032 WhichResult = (M[0] == 0 ? 0 : 1);
4033 for (unsigned j = 0; j != 2; ++j) {
4034 unsigned Idx = WhichResult;
4035 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004036 int MIdx = M[i + j * Half];
4037 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004038 return false;
4039 Idx += 2;
4040 }
4041 }
4042
4043 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4044 if (VT.is64BitVector() && EltSz == 32)
4045 return false;
4046
4047 return true;
4048}
4049
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004050static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004051 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4052 if (EltSz == 64)
4053 return false;
4054
Bob Wilsonc692cb72009-08-21 20:54:19 +00004055 unsigned NumElts = VT.getVectorNumElements();
4056 WhichResult = (M[0] == 0 ? 0 : 1);
4057 unsigned Idx = WhichResult * NumElts / 2;
4058 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004059 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4060 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004061 return false;
4062 Idx += 1;
4063 }
4064
4065 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004066 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004067 return false;
4068
4069 return true;
4070}
4071
Bob Wilson324f4f12009-12-03 06:40:55 +00004072/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4073/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4074/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004075static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004076 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4077 if (EltSz == 64)
4078 return false;
4079
4080 unsigned NumElts = VT.getVectorNumElements();
4081 WhichResult = (M[0] == 0 ? 0 : 1);
4082 unsigned Idx = WhichResult * NumElts / 2;
4083 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004084 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4085 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004086 return false;
4087 Idx += 1;
4088 }
4089
4090 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4091 if (VT.is64BitVector() && EltSz == 32)
4092 return false;
4093
4094 return true;
4095}
4096
Dale Johannesenf630c712010-07-29 20:10:08 +00004097// If N is an integer constant that can be moved into a register in one
4098// instruction, return an SDValue of such a constant (will become a MOV
4099// instruction). Otherwise return null.
4100static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4101 const ARMSubtarget *ST, DebugLoc dl) {
4102 uint64_t Val;
4103 if (!isa<ConstantSDNode>(N))
4104 return SDValue();
4105 Val = cast<ConstantSDNode>(N)->getZExtValue();
4106
4107 if (ST->isThumb1Only()) {
4108 if (Val <= 255 || ~Val <= 255)
4109 return DAG.getConstant(Val, MVT::i32);
4110 } else {
4111 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4112 return DAG.getConstant(Val, MVT::i32);
4113 }
4114 return SDValue();
4115}
4116
Bob Wilson5bafff32009-06-22 23:27:02 +00004117// If this is a case we can't handle, return null and let the default
4118// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004119SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4120 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004121 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004122 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004123 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004124
4125 APInt SplatBits, SplatUndef;
4126 unsigned SplatBitSize;
4127 bool HasAnyUndefs;
4128 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004129 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004130 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004131 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004132 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004133 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004134 DAG, VmovVT, VT.is128BitVector(),
4135 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004136 if (Val.getNode()) {
4137 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004138 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004139 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004140
4141 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004142 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004143 Val = isNEONModifiedImm(NegatedImm,
4144 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004145 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004146 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004147 if (Val.getNode()) {
4148 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004149 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004150 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004151
4152 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004153 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004154 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004155 if (ImmVal != -1) {
4156 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4157 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4158 }
4159 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004160 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004161 }
4162
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004163 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004164 //
4165 // As an optimisation, even if more than one value is used it may be more
4166 // profitable to splat with one value then change some lanes.
4167 //
4168 // Heuristically we decide to do this if the vector has a "dominant" value,
4169 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004170 unsigned NumElts = VT.getVectorNumElements();
4171 bool isOnlyLowElement = true;
4172 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004173 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004174 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004175
4176 // Map of the number of times a particular SDValue appears in the
4177 // element list.
4178 DenseMap<SDValue, int> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004179 SDValue Value;
4180 for (unsigned i = 0; i < NumElts; ++i) {
4181 SDValue V = Op.getOperand(i);
4182 if (V.getOpcode() == ISD::UNDEF)
4183 continue;
4184 if (i > 0)
4185 isOnlyLowElement = false;
4186 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4187 isConstant = false;
4188
James Molloyba8562a2012-09-06 09:55:02 +00004189 ValueCounts.insert(std::make_pair(V, 0));
4190 int &Count = ValueCounts[V];
4191
4192 // Is this value dominant? (takes up more than half of the lanes)
4193 if (++Count > (NumElts / 2)) {
4194 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004195 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004196 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004197 }
James Molloyba8562a2012-09-06 09:55:02 +00004198 if (ValueCounts.size() != 1)
4199 usesOnlyOneValue = false;
4200 if (!Value.getNode() && ValueCounts.size() > 0)
4201 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004202
James Molloyba8562a2012-09-06 09:55:02 +00004203 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004204 return DAG.getUNDEF(VT);
4205
4206 if (isOnlyLowElement)
4207 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4208
Dale Johannesenf630c712010-07-29 20:10:08 +00004209 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4210
Dale Johannesen575cd142010-10-19 20:00:17 +00004211 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4212 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004213 if (hasDominantValue && EltSize <= 32) {
4214 if (!isConstant) {
4215 SDValue N;
4216
4217 // If we are VDUPing a value that comes directly from a vector, that will
4218 // cause an unnecessary move to and from a GPR, where instead we could
4219 // just use VDUPLANE.
4220 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT)
4221 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4222 Value->getOperand(0), Value->getOperand(1));
4223 else
4224 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4225
4226 if (!usesOnlyOneValue) {
4227 // The dominant value was splatted as 'N', but we now have to insert
4228 // all differing elements.
4229 for (unsigned I = 0; I < NumElts; ++I) {
4230 if (Op.getOperand(I) == Value)
4231 continue;
4232 SmallVector<SDValue, 3> Ops;
4233 Ops.push_back(N);
4234 Ops.push_back(Op.getOperand(I));
4235 Ops.push_back(DAG.getConstant(I, MVT::i32));
4236 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4237 }
4238 }
4239 return N;
4240 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004241 if (VT.getVectorElementType().isFloatingPoint()) {
4242 SmallVector<SDValue, 8> Ops;
4243 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004244 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004245 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004246 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4247 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004248 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4249 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004250 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004251 }
James Molloyba8562a2012-09-06 09:55:02 +00004252 if (usesOnlyOneValue) {
4253 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4254 if (isConstant && Val.getNode())
4255 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4256 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004257 }
4258
4259 // If all elements are constants and the case above didn't get hit, fall back
4260 // to the default expansion, which will generate a load from the constant
4261 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004262 if (isConstant)
4263 return SDValue();
4264
Bob Wilson11a1dff2011-01-07 21:37:30 +00004265 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4266 if (NumElts >= 4) {
4267 SDValue shuffle = ReconstructShuffle(Op, DAG);
4268 if (shuffle != SDValue())
4269 return shuffle;
4270 }
4271
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004272 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004273 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4274 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004275 if (EltSize >= 32) {
4276 // Do the expansion with floating-point types, since that is what the VFP
4277 // registers are defined to use, and since i64 is not legal.
4278 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4279 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004280 SmallVector<SDValue, 8> Ops;
4281 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004282 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004283 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004284 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004285 }
4286
4287 return SDValue();
4288}
4289
Bob Wilson11a1dff2011-01-07 21:37:30 +00004290// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004291// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004292SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4293 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004294 DebugLoc dl = Op.getDebugLoc();
4295 EVT VT = Op.getValueType();
4296 unsigned NumElts = VT.getVectorNumElements();
4297
4298 SmallVector<SDValue, 2> SourceVecs;
4299 SmallVector<unsigned, 2> MinElts;
4300 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004301
Bob Wilson11a1dff2011-01-07 21:37:30 +00004302 for (unsigned i = 0; i < NumElts; ++i) {
4303 SDValue V = Op.getOperand(i);
4304 if (V.getOpcode() == ISD::UNDEF)
4305 continue;
4306 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4307 // A shuffle can only come from building a vector from various
4308 // elements of other vectors.
4309 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004310 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4311 VT.getVectorElementType()) {
4312 // This code doesn't know how to handle shuffles where the vector
4313 // element types do not match (this happens because type legalization
4314 // promotes the return type of EXTRACT_VECTOR_ELT).
4315 // FIXME: It might be appropriate to extend this code to handle
4316 // mismatched types.
4317 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004318 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004319
Bob Wilson11a1dff2011-01-07 21:37:30 +00004320 // Record this extraction against the appropriate vector if possible...
4321 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004322 // If the element number isn't a constant, we can't effectively
4323 // analyze what's going on.
4324 if (!isa<ConstantSDNode>(V.getOperand(1)))
4325 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004326 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4327 bool FoundSource = false;
4328 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4329 if (SourceVecs[j] == SourceVec) {
4330 if (MinElts[j] > EltNo)
4331 MinElts[j] = EltNo;
4332 if (MaxElts[j] < EltNo)
4333 MaxElts[j] = EltNo;
4334 FoundSource = true;
4335 break;
4336 }
4337 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004338
Bob Wilson11a1dff2011-01-07 21:37:30 +00004339 // Or record a new source if not...
4340 if (!FoundSource) {
4341 SourceVecs.push_back(SourceVec);
4342 MinElts.push_back(EltNo);
4343 MaxElts.push_back(EltNo);
4344 }
4345 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004346
Bob Wilson11a1dff2011-01-07 21:37:30 +00004347 // Currently only do something sane when at most two source vectors
4348 // involved.
4349 if (SourceVecs.size() > 2)
4350 return SDValue();
4351
4352 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4353 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004354
Bob Wilson11a1dff2011-01-07 21:37:30 +00004355 // This loop extracts the usage patterns of the source vectors
4356 // and prepares appropriate SDValues for a shuffle if possible.
4357 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4358 if (SourceVecs[i].getValueType() == VT) {
4359 // No VEXT necessary
4360 ShuffleSrcs[i] = SourceVecs[i];
4361 VEXTOffsets[i] = 0;
4362 continue;
4363 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4364 // It probably isn't worth padding out a smaller vector just to
4365 // break it down again in a shuffle.
4366 return SDValue();
4367 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004368
Bob Wilson11a1dff2011-01-07 21:37:30 +00004369 // Since only 64-bit and 128-bit vectors are legal on ARM and
4370 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004371 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4372 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004373
Bob Wilson11a1dff2011-01-07 21:37:30 +00004374 if (MaxElts[i] - MinElts[i] >= NumElts) {
4375 // Span too large for a VEXT to cope
4376 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004377 }
4378
Bob Wilson11a1dff2011-01-07 21:37:30 +00004379 if (MinElts[i] >= NumElts) {
4380 // The extraction can just take the second half
4381 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004382 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4383 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004384 DAG.getIntPtrConstant(NumElts));
4385 } else if (MaxElts[i] < NumElts) {
4386 // The extraction can just take the first half
4387 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004388 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4389 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004390 DAG.getIntPtrConstant(0));
4391 } else {
4392 // An actual VEXT is needed
4393 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004394 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4395 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004396 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004397 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4398 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004399 DAG.getIntPtrConstant(NumElts));
4400 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4401 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4402 }
4403 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004404
Bob Wilson11a1dff2011-01-07 21:37:30 +00004405 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004406
Bob Wilson11a1dff2011-01-07 21:37:30 +00004407 for (unsigned i = 0; i < NumElts; ++i) {
4408 SDValue Entry = Op.getOperand(i);
4409 if (Entry.getOpcode() == ISD::UNDEF) {
4410 Mask.push_back(-1);
4411 continue;
4412 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004413
Bob Wilson11a1dff2011-01-07 21:37:30 +00004414 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004415 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4416 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004417 if (ExtractVec == SourceVecs[0]) {
4418 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4419 } else {
4420 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4421 }
4422 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004423
Bob Wilson11a1dff2011-01-07 21:37:30 +00004424 // Final check before we try to produce nonsense...
4425 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004426 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4427 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004428
Bob Wilson11a1dff2011-01-07 21:37:30 +00004429 return SDValue();
4430}
4431
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004432/// isShuffleMaskLegal - Targets can use this to indicate that they only
4433/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4434/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4435/// are assumed to be legal.
4436bool
4437ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4438 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004439 if (VT.getVectorNumElements() == 4 &&
4440 (VT.is128BitVector() || VT.is64BitVector())) {
4441 unsigned PFIndexes[4];
4442 for (unsigned i = 0; i != 4; ++i) {
4443 if (M[i] < 0)
4444 PFIndexes[i] = 8;
4445 else
4446 PFIndexes[i] = M[i];
4447 }
4448
4449 // Compute the index in the perfect shuffle table.
4450 unsigned PFTableIndex =
4451 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4452 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4453 unsigned Cost = (PFEntry >> 30);
4454
4455 if (Cost <= 4)
4456 return true;
4457 }
4458
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004459 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004460 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004461
Bob Wilson53dd2452010-06-07 23:53:38 +00004462 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4463 return (EltSize >= 32 ||
4464 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004465 isVREVMask(M, VT, 64) ||
4466 isVREVMask(M, VT, 32) ||
4467 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004468 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004469 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004470 isVTRNMask(M, VT, WhichResult) ||
4471 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004472 isVZIPMask(M, VT, WhichResult) ||
4473 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4474 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4475 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004476}
4477
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004478/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4479/// the specified operations to build the shuffle.
4480static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4481 SDValue RHS, SelectionDAG &DAG,
4482 DebugLoc dl) {
4483 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4484 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4485 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4486
4487 enum {
4488 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4489 OP_VREV,
4490 OP_VDUP0,
4491 OP_VDUP1,
4492 OP_VDUP2,
4493 OP_VDUP3,
4494 OP_VEXT1,
4495 OP_VEXT2,
4496 OP_VEXT3,
4497 OP_VUZPL, // VUZP, left result
4498 OP_VUZPR, // VUZP, right result
4499 OP_VZIPL, // VZIP, left result
4500 OP_VZIPR, // VZIP, right result
4501 OP_VTRNL, // VTRN, left result
4502 OP_VTRNR // VTRN, right result
4503 };
4504
4505 if (OpNum == OP_COPY) {
4506 if (LHSID == (1*9+2)*9+3) return LHS;
4507 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4508 return RHS;
4509 }
4510
4511 SDValue OpLHS, OpRHS;
4512 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4513 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4514 EVT VT = OpLHS.getValueType();
4515
4516 switch (OpNum) {
4517 default: llvm_unreachable("Unknown shuffle opcode!");
4518 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004519 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004520 if (VT.getVectorElementType() == MVT::i32 ||
4521 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004522 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4523 // vrev <4 x i16> -> VREV32
4524 if (VT.getVectorElementType() == MVT::i16)
4525 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4526 // vrev <4 x i8> -> VREV16
4527 assert(VT.getVectorElementType() == MVT::i8);
4528 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004529 case OP_VDUP0:
4530 case OP_VDUP1:
4531 case OP_VDUP2:
4532 case OP_VDUP3:
4533 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004534 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004535 case OP_VEXT1:
4536 case OP_VEXT2:
4537 case OP_VEXT3:
4538 return DAG.getNode(ARMISD::VEXT, dl, VT,
4539 OpLHS, OpRHS,
4540 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4541 case OP_VUZPL:
4542 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004543 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004544 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4545 case OP_VZIPL:
4546 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004547 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004548 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4549 case OP_VTRNL:
4550 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004551 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4552 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004553 }
4554}
4555
Bill Wendling69a05a72011-03-14 23:02:38 +00004556static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004557 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004558 SelectionDAG &DAG) {
4559 // Check to see if we can use the VTBL instruction.
4560 SDValue V1 = Op.getOperand(0);
4561 SDValue V2 = Op.getOperand(1);
4562 DebugLoc DL = Op.getDebugLoc();
4563
4564 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004565 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004566 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4567 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4568
4569 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4570 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4571 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4572 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004573
Owen Anderson76706012011-04-05 21:48:57 +00004574 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004575 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4576 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004577}
4578
Bob Wilson5bafff32009-06-22 23:27:02 +00004579static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004580 SDValue V1 = Op.getOperand(0);
4581 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004582 DebugLoc dl = Op.getDebugLoc();
4583 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004584 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004585
Bob Wilson28865062009-08-13 02:13:04 +00004586 // Convert shuffles that are directly supported on NEON to target-specific
4587 // DAG nodes, instead of keeping them as shuffles and matching them again
4588 // during code selection. This is more efficient and avoids the possibility
4589 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004590 // FIXME: floating-point vectors should be canonicalized to integer vectors
4591 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004592 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004593
Bob Wilson53dd2452010-06-07 23:53:38 +00004594 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4595 if (EltSize <= 32) {
4596 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4597 int Lane = SVN->getSplatIndex();
4598 // If this is undef splat, generate it via "just" vdup, if possible.
4599 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004600
Dan Gohman65fd6562011-11-03 21:49:52 +00004601 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004602 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4603 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4604 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004605 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4606 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4607 // reaches it).
4608 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4609 !isa<ConstantSDNode>(V1.getOperand(0))) {
4610 bool IsScalarToVector = true;
4611 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4612 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4613 IsScalarToVector = false;
4614 break;
4615 }
4616 if (IsScalarToVector)
4617 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4618 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004619 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4620 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004621 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004622
4623 bool ReverseVEXT;
4624 unsigned Imm;
4625 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4626 if (ReverseVEXT)
4627 std::swap(V1, V2);
4628 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4629 DAG.getConstant(Imm, MVT::i32));
4630 }
4631
4632 if (isVREVMask(ShuffleMask, VT, 64))
4633 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4634 if (isVREVMask(ShuffleMask, VT, 32))
4635 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4636 if (isVREVMask(ShuffleMask, VT, 16))
4637 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4638
4639 // Check for Neon shuffles that modify both input vectors in place.
4640 // If both results are used, i.e., if there are two shuffles with the same
4641 // source operands and with masks corresponding to both results of one of
4642 // these operations, DAG memoization will ensure that a single node is
4643 // used for both shuffles.
4644 unsigned WhichResult;
4645 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4646 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4647 V1, V2).getValue(WhichResult);
4648 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4649 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4650 V1, V2).getValue(WhichResult);
4651 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4652 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4653 V1, V2).getValue(WhichResult);
4654
4655 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4656 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4657 V1, V1).getValue(WhichResult);
4658 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4659 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4660 V1, V1).getValue(WhichResult);
4661 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4662 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4663 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004664 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004665
Bob Wilsonc692cb72009-08-21 20:54:19 +00004666 // If the shuffle is not directly supported and it has 4 elements, use
4667 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004668 unsigned NumElts = VT.getVectorNumElements();
4669 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004670 unsigned PFIndexes[4];
4671 for (unsigned i = 0; i != 4; ++i) {
4672 if (ShuffleMask[i] < 0)
4673 PFIndexes[i] = 8;
4674 else
4675 PFIndexes[i] = ShuffleMask[i];
4676 }
4677
4678 // Compute the index in the perfect shuffle table.
4679 unsigned PFTableIndex =
4680 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004681 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4682 unsigned Cost = (PFEntry >> 30);
4683
4684 if (Cost <= 4)
4685 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4686 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004687
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004688 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004689 if (EltSize >= 32) {
4690 // Do the expansion with floating-point types, since that is what the VFP
4691 // registers are defined to use, and since i64 is not legal.
4692 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4693 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004694 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4695 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004696 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004697 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004698 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004699 Ops.push_back(DAG.getUNDEF(EltVT));
4700 else
4701 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4702 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4703 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4704 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004705 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004706 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004707 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004708 }
4709
Bill Wendling69a05a72011-03-14 23:02:38 +00004710 if (VT == MVT::v8i8) {
4711 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4712 if (NewOp.getNode())
4713 return NewOp;
4714 }
4715
Bob Wilson22cac0d2009-08-14 05:16:33 +00004716 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004717}
4718
Eli Friedman5c89cb82011-10-24 23:08:52 +00004719static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4720 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4721 SDValue Lane = Op.getOperand(2);
4722 if (!isa<ConstantSDNode>(Lane))
4723 return SDValue();
4724
4725 return Op;
4726}
4727
Bob Wilson5bafff32009-06-22 23:27:02 +00004728static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004729 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004730 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004731 if (!isa<ConstantSDNode>(Lane))
4732 return SDValue();
4733
4734 SDValue Vec = Op.getOperand(0);
4735 if (Op.getValueType() == MVT::i32 &&
4736 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4737 DebugLoc dl = Op.getDebugLoc();
4738 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4739 }
4740
4741 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004742}
4743
Bob Wilsona6d65862009-08-03 20:36:38 +00004744static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4745 // The only time a CONCAT_VECTORS operation can have legal types is when
4746 // two 64-bit vectors are concatenated to a 128-bit vector.
4747 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4748 "unexpected CONCAT_VECTORS");
4749 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004750 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004751 SDValue Op0 = Op.getOperand(0);
4752 SDValue Op1 = Op.getOperand(1);
4753 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004755 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004756 DAG.getIntPtrConstant(0));
4757 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004758 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004759 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004760 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004761 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004762}
4763
Bob Wilson626613d2010-11-23 19:38:38 +00004764/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4765/// element has been zero/sign-extended, depending on the isSigned parameter,
4766/// from an integer type half its size.
4767static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4768 bool isSigned) {
4769 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4770 EVT VT = N->getValueType(0);
4771 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4772 SDNode *BVN = N->getOperand(0).getNode();
4773 if (BVN->getValueType(0) != MVT::v4i32 ||
4774 BVN->getOpcode() != ISD::BUILD_VECTOR)
4775 return false;
4776 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4777 unsigned HiElt = 1 - LoElt;
4778 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4779 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4780 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4781 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4782 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4783 return false;
4784 if (isSigned) {
4785 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4786 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4787 return true;
4788 } else {
4789 if (Hi0->isNullValue() && Hi1->isNullValue())
4790 return true;
4791 }
4792 return false;
4793 }
4794
4795 if (N->getOpcode() != ISD::BUILD_VECTOR)
4796 return false;
4797
4798 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4799 SDNode *Elt = N->getOperand(i).getNode();
4800 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4801 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4802 unsigned HalfSize = EltSize / 2;
4803 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004804 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004805 return false;
4806 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004807 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004808 return false;
4809 }
4810 continue;
4811 }
4812 return false;
4813 }
4814
4815 return true;
4816}
4817
4818/// isSignExtended - Check if a node is a vector value that is sign-extended
4819/// or a constant BUILD_VECTOR with sign-extended elements.
4820static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4821 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4822 return true;
4823 if (isExtendedBUILD_VECTOR(N, DAG, true))
4824 return true;
4825 return false;
4826}
4827
4828/// isZeroExtended - Check if a node is a vector value that is zero-extended
4829/// or a constant BUILD_VECTOR with zero-extended elements.
4830static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4831 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4832 return true;
4833 if (isExtendedBUILD_VECTOR(N, DAG, false))
4834 return true;
4835 return false;
4836}
4837
4838/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4839/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004840static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4841 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4842 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004843 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4844 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4845 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004846 LD->isNonTemporal(), LD->isInvariant(),
4847 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004848 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4849 // have been legalized as a BITCAST from v4i32.
4850 if (N->getOpcode() == ISD::BITCAST) {
4851 SDNode *BVN = N->getOperand(0).getNode();
4852 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4853 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4854 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4855 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4856 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4857 }
4858 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4859 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4860 EVT VT = N->getValueType(0);
4861 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4862 unsigned NumElts = VT.getVectorNumElements();
4863 MVT TruncVT = MVT::getIntegerVT(EltSize);
4864 SmallVector<SDValue, 8> Ops;
4865 for (unsigned i = 0; i != NumElts; ++i) {
4866 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4867 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004868 // Element types smaller than 32 bits are not legal, so use i32 elements.
4869 // The values are implicitly truncated so sext vs. zext doesn't matter.
4870 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004871 }
4872 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4873 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004874}
4875
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004876static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4877 unsigned Opcode = N->getOpcode();
4878 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4879 SDNode *N0 = N->getOperand(0).getNode();
4880 SDNode *N1 = N->getOperand(1).getNode();
4881 return N0->hasOneUse() && N1->hasOneUse() &&
4882 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4883 }
4884 return false;
4885}
4886
4887static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4888 unsigned Opcode = N->getOpcode();
4889 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4890 SDNode *N0 = N->getOperand(0).getNode();
4891 SDNode *N1 = N->getOperand(1).getNode();
4892 return N0->hasOneUse() && N1->hasOneUse() &&
4893 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4894 }
4895 return false;
4896}
4897
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004898static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4899 // Multiplications are only custom-lowered for 128-bit vectors so that
4900 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4901 EVT VT = Op.getValueType();
4902 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4903 SDNode *N0 = Op.getOperand(0).getNode();
4904 SDNode *N1 = Op.getOperand(1).getNode();
4905 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004906 bool isMLA = false;
4907 bool isN0SExt = isSignExtended(N0, DAG);
4908 bool isN1SExt = isSignExtended(N1, DAG);
4909 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004910 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004911 else {
4912 bool isN0ZExt = isZeroExtended(N0, DAG);
4913 bool isN1ZExt = isZeroExtended(N1, DAG);
4914 if (isN0ZExt && isN1ZExt)
4915 NewOpc = ARMISD::VMULLu;
4916 else if (isN1SExt || isN1ZExt) {
4917 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4918 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4919 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4920 NewOpc = ARMISD::VMULLs;
4921 isMLA = true;
4922 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4923 NewOpc = ARMISD::VMULLu;
4924 isMLA = true;
4925 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4926 std::swap(N0, N1);
4927 NewOpc = ARMISD::VMULLu;
4928 isMLA = true;
4929 }
4930 }
4931
4932 if (!NewOpc) {
4933 if (VT == MVT::v2i64)
4934 // Fall through to expand this. It is not legal.
4935 return SDValue();
4936 else
4937 // Other vector multiplications are legal.
4938 return Op;
4939 }
4940 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004941
4942 // Legalize to a VMULL instruction.
4943 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004944 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004945 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004946 if (!isMLA) {
4947 Op0 = SkipExtension(N0, DAG);
4948 assert(Op0.getValueType().is64BitVector() &&
4949 Op1.getValueType().is64BitVector() &&
4950 "unexpected types for extended operands to VMULL");
4951 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4952 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004953
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004954 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4955 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4956 // vmull q0, d4, d6
4957 // vmlal q0, d5, d6
4958 // is faster than
4959 // vaddl q0, d4, d5
4960 // vmovl q1, d6
4961 // vmul q0, q0, q1
4962 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4963 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4964 EVT Op1VT = Op1.getValueType();
4965 return DAG.getNode(N0->getOpcode(), DL, VT,
4966 DAG.getNode(NewOpc, DL, VT,
4967 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4968 DAG.getNode(NewOpc, DL, VT,
4969 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004970}
4971
Owen Anderson76706012011-04-05 21:48:57 +00004972static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004973LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4974 // Convert to float
4975 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4976 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4977 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4978 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4979 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4980 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4981 // Get reciprocal estimate.
4982 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004983 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004984 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4985 // Because char has a smaller range than uchar, we can actually get away
4986 // without any newton steps. This requires that we use a weird bias
4987 // of 0xb000, however (again, this has been exhaustively tested).
4988 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4989 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4990 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4991 Y = DAG.getConstant(0xb000, MVT::i32);
4992 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4993 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4994 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4995 // Convert back to short.
4996 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4997 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4998 return X;
4999}
5000
Owen Anderson76706012011-04-05 21:48:57 +00005001static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005002LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5003 SDValue N2;
5004 // Convert to float.
5005 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5006 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5007 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5008 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5009 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5010 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005011
Nate Begeman7973f352011-02-11 20:53:29 +00005012 // Use reciprocal estimate and one refinement step.
5013 // float4 recip = vrecpeq_f32(yf);
5014 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005015 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005016 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005017 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005018 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5019 N1, N2);
5020 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5021 // Because short has a smaller range than ushort, we can actually get away
5022 // with only a single newton step. This requires that we use a weird bias
5023 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005024 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005025 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5026 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005027 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005028 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5029 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5030 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5031 // Convert back to integer and return.
5032 // return vmovn_s32(vcvt_s32_f32(result));
5033 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5034 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5035 return N0;
5036}
5037
5038static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5039 EVT VT = Op.getValueType();
5040 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5041 "unexpected type for custom-lowering ISD::SDIV");
5042
5043 DebugLoc dl = Op.getDebugLoc();
5044 SDValue N0 = Op.getOperand(0);
5045 SDValue N1 = Op.getOperand(1);
5046 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005047
Nate Begeman7973f352011-02-11 20:53:29 +00005048 if (VT == MVT::v8i8) {
5049 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5050 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005051
Nate Begeman7973f352011-02-11 20:53:29 +00005052 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5053 DAG.getIntPtrConstant(4));
5054 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005055 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005056 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5057 DAG.getIntPtrConstant(0));
5058 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5059 DAG.getIntPtrConstant(0));
5060
5061 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5062 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5063
5064 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5065 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005066
Nate Begeman7973f352011-02-11 20:53:29 +00005067 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5068 return N0;
5069 }
5070 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5071}
5072
5073static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5074 EVT VT = Op.getValueType();
5075 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5076 "unexpected type for custom-lowering ISD::UDIV");
5077
5078 DebugLoc dl = Op.getDebugLoc();
5079 SDValue N0 = Op.getOperand(0);
5080 SDValue N1 = Op.getOperand(1);
5081 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005082
Nate Begeman7973f352011-02-11 20:53:29 +00005083 if (VT == MVT::v8i8) {
5084 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5085 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005086
Nate Begeman7973f352011-02-11 20:53:29 +00005087 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5088 DAG.getIntPtrConstant(4));
5089 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005090 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005091 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5092 DAG.getIntPtrConstant(0));
5093 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5094 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005095
Nate Begeman7973f352011-02-11 20:53:29 +00005096 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5097 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005098
Nate Begeman7973f352011-02-11 20:53:29 +00005099 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5100 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005101
5102 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005103 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5104 N0);
5105 return N0;
5106 }
Owen Anderson76706012011-04-05 21:48:57 +00005107
Nate Begeman7973f352011-02-11 20:53:29 +00005108 // v4i16 sdiv ... Convert to float.
5109 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5110 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5111 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5112 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5113 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005114 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005115
5116 // Use reciprocal estimate and two refinement steps.
5117 // float4 recip = vrecpeq_f32(yf);
5118 // recip *= vrecpsq_f32(yf, recip);
5119 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005120 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005121 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005122 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005123 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005124 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005125 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005126 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005127 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005128 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005129 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5130 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5131 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5132 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005133 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005134 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5135 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5136 N1 = DAG.getConstant(2, MVT::i32);
5137 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5138 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5139 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5140 // Convert back to integer and return.
5141 // return vmovn_u32(vcvt_s32_f32(result));
5142 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5143 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5144 return N0;
5145}
5146
Evan Cheng342e3162011-08-30 01:34:54 +00005147static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5148 EVT VT = Op.getNode()->getValueType(0);
5149 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5150
5151 unsigned Opc;
5152 bool ExtraOp = false;
5153 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005154 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005155 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5156 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5157 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5158 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5159 }
5160
5161 if (!ExtraOp)
5162 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5163 Op.getOperand(1));
5164 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5165 Op.getOperand(1), Op.getOperand(2));
5166}
5167
Eli Friedman74bf18c2011-09-15 22:26:18 +00005168static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005169 // Monotonic load/store is legal for all targets
5170 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5171 return Op;
5172
5173 // Aquire/Release load/store is not legal for targets without a
5174 // dmb or equivalent available.
5175 return SDValue();
5176}
5177
5178
Eli Friedman2bdffe42011-08-31 00:31:29 +00005179static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005180ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5181 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005182 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005183 assert (Node->getValueType(0) == MVT::i64 &&
5184 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005185
Eli Friedman4d3f3292011-08-31 17:52:22 +00005186 SmallVector<SDValue, 6> Ops;
5187 Ops.push_back(Node->getOperand(0)); // Chain
5188 Ops.push_back(Node->getOperand(1)); // Ptr
5189 // Low part of Val1
5190 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5191 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5192 // High part of Val1
5193 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5194 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005195 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005196 // High part of Val1
5197 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5198 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5199 // High part of Val2
5200 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5201 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5202 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005203 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5204 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005205 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005206 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005207 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005208 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5209 Results.push_back(Result.getValue(2));
5210}
5211
Dan Gohmand858e902010-04-17 15:26:15 +00005212SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005213 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005214 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005215 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005216 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005217 case ISD::GlobalAddress:
5218 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5219 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005220 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005221 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005222 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5223 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005224 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005225 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005226 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005227 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005228 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005229 case ISD::SINT_TO_FP:
5230 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5231 case ISD::FP_TO_SINT:
5232 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005233 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005234 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005235 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005236 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005237 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005238 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005239 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5240 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005241 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005242 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005243 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005244 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005245 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005246 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005247 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005248 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005249 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005250 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005251 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005252 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005253 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005254 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005255 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005256 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005257 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005258 case ISD::SDIV: return LowerSDIV(Op, DAG);
5259 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005260 case ISD::ADDC:
5261 case ISD::ADDE:
5262 case ISD::SUBC:
5263 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005264 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005265 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005266 }
Evan Chenga8e29892007-01-19 07:51:42 +00005267}
5268
Duncan Sands1607f052008-12-01 11:39:25 +00005269/// ReplaceNodeResults - Replace the results of node with an illegal result
5270/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005271void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5272 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005273 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005274 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005275 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005276 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005277 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005278 case ISD::BITCAST:
5279 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005280 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005281 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005282 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005283 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005284 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005285 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005286 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005287 return;
5288 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005289 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005290 return;
5291 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005292 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005293 return;
5294 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005295 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005296 return;
5297 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005298 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005299 return;
5300 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005301 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005302 return;
5303 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005304 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005305 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005306 case ISD::ATOMIC_CMP_SWAP:
5307 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5308 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005309 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005310 if (Res.getNode())
5311 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005312}
Chris Lattner27a6c732007-11-24 07:07:01 +00005313
Evan Chenga8e29892007-01-19 07:51:42 +00005314//===----------------------------------------------------------------------===//
5315// ARM Scheduler Hooks
5316//===----------------------------------------------------------------------===//
5317
5318MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005319ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5320 MachineBasicBlock *BB,
5321 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005322 unsigned dest = MI->getOperand(0).getReg();
5323 unsigned ptr = MI->getOperand(1).getReg();
5324 unsigned oldval = MI->getOperand(2).getReg();
5325 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005326 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5327 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005328 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005329
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005330 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005331 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5332 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5333 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005334
5335 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005336 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5337 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5338 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005339 }
5340
Jim Grosbach5278eb82009-12-11 01:42:04 +00005341 unsigned ldrOpc, strOpc;
5342 switch (Size) {
5343 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005344 case 1:
5345 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005346 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005347 break;
5348 case 2:
5349 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5350 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5351 break;
5352 case 4:
5353 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5354 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5355 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005356 }
5357
5358 MachineFunction *MF = BB->getParent();
5359 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5360 MachineFunction::iterator It = BB;
5361 ++It; // insert the new blocks after the current block
5362
5363 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5364 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5365 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5366 MF->insert(It, loop1MBB);
5367 MF->insert(It, loop2MBB);
5368 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005369
5370 // Transfer the remainder of BB and its successor edges to exitMBB.
5371 exitMBB->splice(exitMBB->begin(), BB,
5372 llvm::next(MachineBasicBlock::iterator(MI)),
5373 BB->end());
5374 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005375
5376 // thisMBB:
5377 // ...
5378 // fallthrough --> loop1MBB
5379 BB->addSuccessor(loop1MBB);
5380
5381 // loop1MBB:
5382 // ldrex dest, [ptr]
5383 // cmp dest, oldval
5384 // bne exitMBB
5385 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005386 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5387 if (ldrOpc == ARM::t2LDREX)
5388 MIB.addImm(0);
5389 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005390 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005391 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005392 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5393 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005394 BB->addSuccessor(loop2MBB);
5395 BB->addSuccessor(exitMBB);
5396
5397 // loop2MBB:
5398 // strex scratch, newval, [ptr]
5399 // cmp scratch, #0
5400 // bne loop1MBB
5401 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005402 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5403 if (strOpc == ARM::t2STREX)
5404 MIB.addImm(0);
5405 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005406 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005407 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005408 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5409 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005410 BB->addSuccessor(loop1MBB);
5411 BB->addSuccessor(exitMBB);
5412
5413 // exitMBB:
5414 // ...
5415 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005416
Dan Gohman14152b42010-07-06 20:24:04 +00005417 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005418
Jim Grosbach5278eb82009-12-11 01:42:04 +00005419 return BB;
5420}
5421
5422MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005423ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5424 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005425 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5426 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5427
5428 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005429 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005430 MachineFunction::iterator It = BB;
5431 ++It;
5432
5433 unsigned dest = MI->getOperand(0).getReg();
5434 unsigned ptr = MI->getOperand(1).getReg();
5435 unsigned incr = MI->getOperand(2).getReg();
5436 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005437 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005438
5439 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5440 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005441 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5442 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005443 }
5444
Jim Grosbachc3c23542009-12-14 04:22:04 +00005445 unsigned ldrOpc, strOpc;
5446 switch (Size) {
5447 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005448 case 1:
5449 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005450 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005451 break;
5452 case 2:
5453 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5454 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5455 break;
5456 case 4:
5457 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5458 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5459 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005460 }
5461
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005462 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5463 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5464 MF->insert(It, loopMBB);
5465 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005466
5467 // Transfer the remainder of BB and its successor edges to exitMBB.
5468 exitMBB->splice(exitMBB->begin(), BB,
5469 llvm::next(MachineBasicBlock::iterator(MI)),
5470 BB->end());
5471 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005472
Craig Topper420761a2012-04-20 07:30:17 +00005473 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005474 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005475 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005476 unsigned scratch = MRI.createVirtualRegister(TRC);
5477 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005478
5479 // thisMBB:
5480 // ...
5481 // fallthrough --> loopMBB
5482 BB->addSuccessor(loopMBB);
5483
5484 // loopMBB:
5485 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005486 // <binop> scratch2, dest, incr
5487 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005488 // cmp scratch, #0
5489 // bne- loopMBB
5490 // fallthrough --> exitMBB
5491 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005492 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5493 if (ldrOpc == ARM::t2LDREX)
5494 MIB.addImm(0);
5495 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005496 if (BinOpcode) {
5497 // operand order needs to go the other way for NAND
5498 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5499 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5500 addReg(incr).addReg(dest)).addReg(0);
5501 else
5502 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5503 addReg(dest).addReg(incr)).addReg(0);
5504 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005505
Jim Grosbachb6aed502011-09-09 18:37:27 +00005506 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5507 if (strOpc == ARM::t2STREX)
5508 MIB.addImm(0);
5509 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005510 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005511 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005512 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5513 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005514
5515 BB->addSuccessor(loopMBB);
5516 BB->addSuccessor(exitMBB);
5517
5518 // exitMBB:
5519 // ...
5520 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005521
Dan Gohman14152b42010-07-06 20:24:04 +00005522 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005523
Jim Grosbachc3c23542009-12-14 04:22:04 +00005524 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005525}
5526
Jim Grosbachf7da8822011-04-26 19:44:18 +00005527MachineBasicBlock *
5528ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5529 MachineBasicBlock *BB,
5530 unsigned Size,
5531 bool signExtend,
5532 ARMCC::CondCodes Cond) const {
5533 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5534
5535 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5536 MachineFunction *MF = BB->getParent();
5537 MachineFunction::iterator It = BB;
5538 ++It;
5539
5540 unsigned dest = MI->getOperand(0).getReg();
5541 unsigned ptr = MI->getOperand(1).getReg();
5542 unsigned incr = MI->getOperand(2).getReg();
5543 unsigned oldval = dest;
5544 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005545 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005546
5547 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5548 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005549 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5550 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005551 }
5552
Jim Grosbachf7da8822011-04-26 19:44:18 +00005553 unsigned ldrOpc, strOpc, extendOpc;
5554 switch (Size) {
5555 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5556 case 1:
5557 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5558 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005559 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005560 break;
5561 case 2:
5562 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5563 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005564 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005565 break;
5566 case 4:
5567 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5568 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5569 extendOpc = 0;
5570 break;
5571 }
5572
5573 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5574 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5575 MF->insert(It, loopMBB);
5576 MF->insert(It, exitMBB);
5577
5578 // Transfer the remainder of BB and its successor edges to exitMBB.
5579 exitMBB->splice(exitMBB->begin(), BB,
5580 llvm::next(MachineBasicBlock::iterator(MI)),
5581 BB->end());
5582 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5583
Craig Topper420761a2012-04-20 07:30:17 +00005584 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005585 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005586 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005587 unsigned scratch = MRI.createVirtualRegister(TRC);
5588 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005589
5590 // thisMBB:
5591 // ...
5592 // fallthrough --> loopMBB
5593 BB->addSuccessor(loopMBB);
5594
5595 // loopMBB:
5596 // ldrex dest, ptr
5597 // (sign extend dest, if required)
5598 // cmp dest, incr
5599 // cmov.cond scratch2, dest, incr
5600 // strex scratch, scratch2, ptr
5601 // cmp scratch, #0
5602 // bne- loopMBB
5603 // fallthrough --> exitMBB
5604 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005605 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5606 if (ldrOpc == ARM::t2LDREX)
5607 MIB.addImm(0);
5608 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005609
5610 // Sign extend the value, if necessary.
5611 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005612 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005613 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5614 .addReg(dest)
5615 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005616 }
5617
5618 // Build compare and cmov instructions.
5619 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5620 .addReg(oldval).addReg(incr));
5621 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5622 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5623
Jim Grosbachb6aed502011-09-09 18:37:27 +00005624 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5625 if (strOpc == ARM::t2STREX)
5626 MIB.addImm(0);
5627 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005628 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5629 .addReg(scratch).addImm(0));
5630 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5631 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5632
5633 BB->addSuccessor(loopMBB);
5634 BB->addSuccessor(exitMBB);
5635
5636 // exitMBB:
5637 // ...
5638 BB = exitMBB;
5639
5640 MI->eraseFromParent(); // The instruction is gone now.
5641
5642 return BB;
5643}
5644
Eli Friedman2bdffe42011-08-31 00:31:29 +00005645MachineBasicBlock *
5646ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5647 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005648 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005649 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5650 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5651
5652 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5653 MachineFunction *MF = BB->getParent();
5654 MachineFunction::iterator It = BB;
5655 ++It;
5656
5657 unsigned destlo = MI->getOperand(0).getReg();
5658 unsigned desthi = MI->getOperand(1).getReg();
5659 unsigned ptr = MI->getOperand(2).getReg();
5660 unsigned vallo = MI->getOperand(3).getReg();
5661 unsigned valhi = MI->getOperand(4).getReg();
5662 DebugLoc dl = MI->getDebugLoc();
5663 bool isThumb2 = Subtarget->isThumb2();
5664
5665 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5666 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005667 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5668 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5669 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005670 }
5671
5672 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5673 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5674
5675 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005676 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005677 if (IsCmpxchg) {
5678 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5679 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5680 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005681 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5682 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005683 if (IsCmpxchg) {
5684 MF->insert(It, contBB);
5685 MF->insert(It, cont2BB);
5686 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005687 MF->insert(It, exitMBB);
5688
5689 // Transfer the remainder of BB and its successor edges to exitMBB.
5690 exitMBB->splice(exitMBB->begin(), BB,
5691 llvm::next(MachineBasicBlock::iterator(MI)),
5692 BB->end());
5693 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5694
Craig Topper420761a2012-04-20 07:30:17 +00005695 const TargetRegisterClass *TRC = isThumb2 ?
5696 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5697 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005698 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5699
5700 // thisMBB:
5701 // ...
5702 // fallthrough --> loopMBB
5703 BB->addSuccessor(loopMBB);
5704
5705 // loopMBB:
5706 // ldrexd r2, r3, ptr
5707 // <binopa> r0, r2, incr
5708 // <binopb> r1, r3, incr
5709 // strexd storesuccess, r0, r1, ptr
5710 // cmp storesuccess, #0
5711 // bne- loopMBB
5712 // fallthrough --> exitMBB
5713 //
5714 // Note that the registers are explicitly specified because there is not any
5715 // way to force the register allocator to allocate a register pair.
5716 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005717 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005718 // need to properly enforce the restriction that the two output registers
5719 // for ldrexd must be different.
5720 BB = loopMBB;
5721 // Load
5722 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5723 .addReg(ARM::R2, RegState::Define)
5724 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5725 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5726 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5727 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005728
5729 if (IsCmpxchg) {
5730 // Add early exit
5731 for (unsigned i = 0; i < 2; i++) {
5732 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5733 ARM::CMPrr))
5734 .addReg(i == 0 ? destlo : desthi)
5735 .addReg(i == 0 ? vallo : valhi));
5736 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5737 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5738 BB->addSuccessor(exitMBB);
5739 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5740 BB = (i == 0 ? contBB : cont2BB);
5741 }
5742
5743 // Copy to physregs for strexd
5744 unsigned setlo = MI->getOperand(5).getReg();
5745 unsigned sethi = MI->getOperand(6).getReg();
5746 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5747 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5748 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005749 // Perform binary operation
5750 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5751 .addReg(destlo).addReg(vallo))
5752 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5753 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5754 .addReg(desthi).addReg(valhi)).addReg(0);
5755 } else {
5756 // Copy to physregs for strexd
5757 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5758 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5759 }
5760
5761 // Store
5762 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5763 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5764 // Cmp+jump
5765 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5766 .addReg(storesuccess).addImm(0));
5767 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5768 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5769
5770 BB->addSuccessor(loopMBB);
5771 BB->addSuccessor(exitMBB);
5772
5773 // exitMBB:
5774 // ...
5775 BB = exitMBB;
5776
5777 MI->eraseFromParent(); // The instruction is gone now.
5778
5779 return BB;
5780}
5781
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005782/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5783/// registers the function context.
5784void ARMTargetLowering::
5785SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5786 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005787 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5788 DebugLoc dl = MI->getDebugLoc();
5789 MachineFunction *MF = MBB->getParent();
5790 MachineRegisterInfo *MRI = &MF->getRegInfo();
5791 MachineConstantPool *MCP = MF->getConstantPool();
5792 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5793 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005794
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005795 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005796 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005797
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005798 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005799 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005800 ARMConstantPoolValue *CPV =
5801 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5802 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5803
Craig Topper420761a2012-04-20 07:30:17 +00005804 const TargetRegisterClass *TRC = isThumb ?
5805 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5806 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005807
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005808 // Grab constant pool and fixed stack memory operands.
5809 MachineMemOperand *CPMMO =
5810 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5811 MachineMemOperand::MOLoad, 4, 4);
5812
5813 MachineMemOperand *FIMMOSt =
5814 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5815 MachineMemOperand::MOStore, 4, 4);
5816
5817 // Load the address of the dispatch MBB into the jump buffer.
5818 if (isThumb2) {
5819 // Incoming value: jbuf
5820 // ldr.n r5, LCPI1_1
5821 // orr r5, r5, #1
5822 // add r5, pc
5823 // str r5, [$jbuf, #+4] ; &jbuf[1]
5824 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5825 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5826 .addConstantPoolIndex(CPI)
5827 .addMemOperand(CPMMO));
5828 // Set the low bit because of thumb mode.
5829 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5830 AddDefaultCC(
5831 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5832 .addReg(NewVReg1, RegState::Kill)
5833 .addImm(0x01)));
5834 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5835 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5836 .addReg(NewVReg2, RegState::Kill)
5837 .addImm(PCLabelId);
5838 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5839 .addReg(NewVReg3, RegState::Kill)
5840 .addFrameIndex(FI)
5841 .addImm(36) // &jbuf[1] :: pc
5842 .addMemOperand(FIMMOSt));
5843 } else if (isThumb) {
5844 // Incoming value: jbuf
5845 // ldr.n r1, LCPI1_4
5846 // add r1, pc
5847 // mov r2, #1
5848 // orrs r1, r2
5849 // add r2, $jbuf, #+4 ; &jbuf[1]
5850 // str r1, [r2]
5851 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5852 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5853 .addConstantPoolIndex(CPI)
5854 .addMemOperand(CPMMO));
5855 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5856 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5857 .addReg(NewVReg1, RegState::Kill)
5858 .addImm(PCLabelId);
5859 // Set the low bit because of thumb mode.
5860 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5861 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5862 .addReg(ARM::CPSR, RegState::Define)
5863 .addImm(1));
5864 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5865 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5866 .addReg(ARM::CPSR, RegState::Define)
5867 .addReg(NewVReg2, RegState::Kill)
5868 .addReg(NewVReg3, RegState::Kill));
5869 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5870 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5871 .addFrameIndex(FI)
5872 .addImm(36)); // &jbuf[1] :: pc
5873 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5874 .addReg(NewVReg4, RegState::Kill)
5875 .addReg(NewVReg5, RegState::Kill)
5876 .addImm(0)
5877 .addMemOperand(FIMMOSt));
5878 } else {
5879 // Incoming value: jbuf
5880 // ldr r1, LCPI1_1
5881 // add r1, pc, r1
5882 // str r1, [$jbuf, #+4] ; &jbuf[1]
5883 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5884 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5885 .addConstantPoolIndex(CPI)
5886 .addImm(0)
5887 .addMemOperand(CPMMO));
5888 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5889 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5890 .addReg(NewVReg1, RegState::Kill)
5891 .addImm(PCLabelId));
5892 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5893 .addReg(NewVReg2, RegState::Kill)
5894 .addFrameIndex(FI)
5895 .addImm(36) // &jbuf[1] :: pc
5896 .addMemOperand(FIMMOSt));
5897 }
5898}
5899
5900MachineBasicBlock *ARMTargetLowering::
5901EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5902 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5903 DebugLoc dl = MI->getDebugLoc();
5904 MachineFunction *MF = MBB->getParent();
5905 MachineRegisterInfo *MRI = &MF->getRegInfo();
5906 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5907 MachineFrameInfo *MFI = MF->getFrameInfo();
5908 int FI = MFI->getFunctionContextIndex();
5909
Craig Topper420761a2012-04-20 07:30:17 +00005910 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5911 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005912 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005913
Bill Wendling04f15b42011-10-06 21:29:56 +00005914 // Get a mapping of the call site numbers to all of the landing pads they're
5915 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005916 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5917 unsigned MaxCSNum = 0;
5918 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005919 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5920 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005921 if (!BB->isLandingPad()) continue;
5922
5923 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5924 // pad.
5925 for (MachineBasicBlock::iterator
5926 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5927 if (!II->isEHLabel()) continue;
5928
5929 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005930 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005931
Bill Wendling5cbef192011-10-05 23:28:57 +00005932 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5933 for (SmallVectorImpl<unsigned>::iterator
5934 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5935 CSI != CSE; ++CSI) {
5936 CallSiteNumToLPad[*CSI].push_back(BB);
5937 MaxCSNum = std::max(MaxCSNum, *CSI);
5938 }
Bill Wendling2a850152011-10-05 00:02:33 +00005939 break;
5940 }
5941 }
5942
5943 // Get an ordered list of the machine basic blocks for the jump table.
5944 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005945 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005946 LPadList.reserve(CallSiteNumToLPad.size());
5947 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5948 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5949 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005950 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005951 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005952 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5953 }
Bill Wendling2a850152011-10-05 00:02:33 +00005954 }
5955
Bill Wendling5cbef192011-10-05 23:28:57 +00005956 assert(!LPadList.empty() &&
5957 "No landing pad destinations for the dispatch jump table!");
5958
Bill Wendling04f15b42011-10-06 21:29:56 +00005959 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005960 MachineJumpTableInfo *JTI =
5961 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5962 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5963 unsigned UId = AFI->createJumpTableUId();
5964
Bill Wendling04f15b42011-10-06 21:29:56 +00005965 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005966
5967 // Shove the dispatch's address into the return slot in the function context.
5968 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5969 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005970
Bill Wendlingbb734682011-10-05 00:39:32 +00005971 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005972 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005973 DispatchBB->addSuccessor(TrapBB);
5974
5975 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5976 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005977
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005978 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005979 MF->insert(MF->end(), DispatchBB);
5980 MF->insert(MF->end(), DispContBB);
5981 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005982
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005983 // Insert code into the entry block that creates and registers the function
5984 // context.
5985 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5986
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005987 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005988 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005989 MachineMemOperand::MOLoad |
5990 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005991
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005992 if (AFI->isThumb1OnlyFunction())
5993 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5994 else if (!Subtarget->hasVFP2())
5995 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005996 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005997 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005998
Bill Wendling952cb502011-10-18 22:49:07 +00005999 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006000 if (Subtarget->isThumb2()) {
6001 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6002 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6003 .addFrameIndex(FI)
6004 .addImm(4)
6005 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006006
Bill Wendling952cb502011-10-18 22:49:07 +00006007 if (NumLPads < 256) {
6008 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6009 .addReg(NewVReg1)
6010 .addImm(LPadList.size()));
6011 } else {
6012 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6013 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006014 .addImm(NumLPads & 0xFFFF));
6015
6016 unsigned VReg2 = VReg1;
6017 if ((NumLPads & 0xFFFF0000) != 0) {
6018 VReg2 = MRI->createVirtualRegister(TRC);
6019 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6020 .addReg(VReg1)
6021 .addImm(NumLPads >> 16));
6022 }
6023
Bill Wendling952cb502011-10-18 22:49:07 +00006024 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6025 .addReg(NewVReg1)
6026 .addReg(VReg2));
6027 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006028
Bill Wendling95ce2e92011-10-06 22:53:00 +00006029 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6030 .addMBB(TrapBB)
6031 .addImm(ARMCC::HI)
6032 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006033
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006034 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6035 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006036 .addJumpTableIndex(MJTI)
6037 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006038
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006039 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006040 AddDefaultCC(
6041 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006042 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6043 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006044 .addReg(NewVReg1)
6045 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6046
6047 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006048 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006049 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006050 .addJumpTableIndex(MJTI)
6051 .addImm(UId);
6052 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006053 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6054 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6055 .addFrameIndex(FI)
6056 .addImm(1)
6057 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006058
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006059 if (NumLPads < 256) {
6060 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6061 .addReg(NewVReg1)
6062 .addImm(NumLPads));
6063 } else {
6064 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006065 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6066 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6067
6068 // MachineConstantPool wants an explicit alignment.
6069 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6070 if (Align == 0)
6071 Align = getTargetData()->getTypeAllocSize(C->getType());
6072 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006073
6074 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6075 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6076 .addReg(VReg1, RegState::Define)
6077 .addConstantPoolIndex(Idx));
6078 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6079 .addReg(NewVReg1)
6080 .addReg(VReg1));
6081 }
6082
Bill Wendling083a8eb2011-10-06 23:37:36 +00006083 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6084 .addMBB(TrapBB)
6085 .addImm(ARMCC::HI)
6086 .addReg(ARM::CPSR);
6087
6088 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6089 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6090 .addReg(ARM::CPSR, RegState::Define)
6091 .addReg(NewVReg1)
6092 .addImm(2));
6093
6094 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006095 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006096 .addJumpTableIndex(MJTI)
6097 .addImm(UId));
6098
6099 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6100 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6101 .addReg(ARM::CPSR, RegState::Define)
6102 .addReg(NewVReg2, RegState::Kill)
6103 .addReg(NewVReg3));
6104
6105 MachineMemOperand *JTMMOLd =
6106 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6107 MachineMemOperand::MOLoad, 4, 4);
6108
6109 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6110 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6111 .addReg(NewVReg4, RegState::Kill)
6112 .addImm(0)
6113 .addMemOperand(JTMMOLd));
6114
6115 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6116 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6117 .addReg(ARM::CPSR, RegState::Define)
6118 .addReg(NewVReg5, RegState::Kill)
6119 .addReg(NewVReg3));
6120
6121 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6122 .addReg(NewVReg6, RegState::Kill)
6123 .addJumpTableIndex(MJTI)
6124 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006125 } else {
6126 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6127 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6128 .addFrameIndex(FI)
6129 .addImm(4)
6130 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006131
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006132 if (NumLPads < 256) {
6133 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6134 .addReg(NewVReg1)
6135 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006136 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006137 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6138 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006139 .addImm(NumLPads & 0xFFFF));
6140
6141 unsigned VReg2 = VReg1;
6142 if ((NumLPads & 0xFFFF0000) != 0) {
6143 VReg2 = MRI->createVirtualRegister(TRC);
6144 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6145 .addReg(VReg1)
6146 .addImm(NumLPads >> 16));
6147 }
6148
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006149 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6150 .addReg(NewVReg1)
6151 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006152 } else {
6153 MachineConstantPool *ConstantPool = MF->getConstantPool();
6154 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6155 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6156
6157 // MachineConstantPool wants an explicit alignment.
6158 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6159 if (Align == 0)
6160 Align = getTargetData()->getTypeAllocSize(C->getType());
6161 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6162
6163 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6164 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6165 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006166 .addConstantPoolIndex(Idx)
6167 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006168 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6169 .addReg(NewVReg1)
6170 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006171 }
6172
Bill Wendling95ce2e92011-10-06 22:53:00 +00006173 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6174 .addMBB(TrapBB)
6175 .addImm(ARMCC::HI)
6176 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006177
Bill Wendling564392b2011-10-18 22:11:18 +00006178 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006179 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006180 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006181 .addReg(NewVReg1)
6182 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006183 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6184 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006185 .addJumpTableIndex(MJTI)
6186 .addImm(UId));
6187
6188 MachineMemOperand *JTMMOLd =
6189 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6190 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006191 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006192 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006193 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6194 .addReg(NewVReg3, RegState::Kill)
6195 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006196 .addImm(0)
6197 .addMemOperand(JTMMOLd));
6198
6199 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006200 .addReg(NewVReg5, RegState::Kill)
6201 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006202 .addJumpTableIndex(MJTI)
6203 .addImm(UId);
6204 }
Bill Wendling2a850152011-10-05 00:02:33 +00006205
Bill Wendlingbb734682011-10-05 00:39:32 +00006206 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006207 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006208 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006209 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6210 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006211 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006212 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006213 }
6214
Bill Wendling24bb9252011-10-17 05:25:09 +00006215 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006216 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6217 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006218 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006219 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006220 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6221 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6222 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006223
6224 // Remove the landing pad successor from the invoke block and replace it
6225 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006226 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6227 BB->succ_end());
6228 while (!Successors.empty()) {
6229 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006230 if (SMBB->isLandingPad()) {
6231 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006232 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006233 }
6234 }
6235
6236 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006237
6238 // Find the invoke call and mark all of the callee-saved registers as
6239 // 'implicit defined' so that they're spilled. This prevents code from
6240 // moving instructions to before the EH block, where they will never be
6241 // executed.
6242 for (MachineBasicBlock::reverse_iterator
6243 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006244 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006245
6246 DenseMap<unsigned, bool> DefRegs;
6247 for (MachineInstr::mop_iterator
6248 OI = II->operands_begin(), OE = II->operands_end();
6249 OI != OE; ++OI) {
6250 if (!OI->isReg()) continue;
6251 DefRegs[OI->getReg()] = true;
6252 }
6253
6254 MachineInstrBuilder MIB(&*II);
6255
Bill Wendling5d798592011-10-14 23:55:44 +00006256 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006257 unsigned Reg = SavedRegs[i];
6258 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006259 !ARM::tGPRRegClass.contains(Reg) &&
6260 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006261 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006262 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006263 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006264 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006265 continue;
6266 if (!DefRegs[Reg])
6267 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006268 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006269
6270 break;
6271 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006272 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006273
Bill Wendlingf7b02072011-10-18 18:30:49 +00006274 // Mark all former landing pads as non-landing pads. The dispatch is the only
6275 // landing pad now.
6276 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6277 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6278 (*I)->setIsLandingPad(false);
6279
Bill Wendlingbb734682011-10-05 00:39:32 +00006280 // The instruction is gone now.
6281 MI->eraseFromParent();
6282
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006283 return MBB;
6284}
6285
Evan Cheng218977b2010-07-13 19:27:42 +00006286static
6287MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6288 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6289 E = MBB->succ_end(); I != E; ++I)
6290 if (*I != Succ)
6291 return *I;
6292 llvm_unreachable("Expecting a BB with two successors!");
6293}
6294
Manman Ren68f25572012-06-01 19:33:18 +00006295MachineBasicBlock *ARMTargetLowering::
6296EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6297 // This pseudo instruction has 3 operands: dst, src, size
6298 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6299 // Otherwise, we will generate unrolled scalar copies.
6300 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6301 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6302 MachineFunction::iterator It = BB;
6303 ++It;
6304
6305 unsigned dest = MI->getOperand(0).getReg();
6306 unsigned src = MI->getOperand(1).getReg();
6307 unsigned SizeVal = MI->getOperand(2).getImm();
6308 unsigned Align = MI->getOperand(3).getImm();
6309 DebugLoc dl = MI->getDebugLoc();
6310
6311 bool isThumb2 = Subtarget->isThumb2();
6312 MachineFunction *MF = BB->getParent();
6313 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006314 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006315
6316 const TargetRegisterClass *TRC = isThumb2 ?
6317 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6318 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006319 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006320
6321 if (Align & 1) {
6322 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6323 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6324 UnitSize = 1;
6325 } else if (Align & 2) {
6326 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6327 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6328 UnitSize = 2;
6329 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006330 // Check whether we can use NEON instructions.
6331 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6332 Subtarget->hasNEON()) {
6333 if ((Align % 16 == 0) && SizeVal >= 16) {
6334 ldrOpc = ARM::VLD1q32wb_fixed;
6335 strOpc = ARM::VST1q32wb_fixed;
6336 UnitSize = 16;
6337 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6338 }
6339 else if ((Align % 8 == 0) && SizeVal >= 8) {
6340 ldrOpc = ARM::VLD1d32wb_fixed;
6341 strOpc = ARM::VST1d32wb_fixed;
6342 UnitSize = 8;
6343 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6344 }
6345 }
6346 // Can't use NEON instructions.
6347 if (UnitSize == 0) {
6348 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6349 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6350 UnitSize = 4;
6351 }
Manman Ren68f25572012-06-01 19:33:18 +00006352 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006353
Manman Ren68f25572012-06-01 19:33:18 +00006354 unsigned BytesLeft = SizeVal % UnitSize;
6355 unsigned LoopSize = SizeVal - BytesLeft;
6356
6357 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6358 // Use LDR and STR to copy.
6359 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6360 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6361 unsigned srcIn = src;
6362 unsigned destIn = dest;
6363 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006364 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006365 unsigned srcOut = MRI.createVirtualRegister(TRC);
6366 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006367 if (UnitSize >= 8) {
6368 AddDefaultPred(BuildMI(*BB, MI, dl,
6369 TII->get(ldrOpc), scratch)
6370 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6371
6372 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6373 .addReg(destIn).addImm(0).addReg(scratch));
6374 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006375 AddDefaultPred(BuildMI(*BB, MI, dl,
6376 TII->get(ldrOpc), scratch)
6377 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6378
6379 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6380 .addReg(scratch).addReg(destIn)
6381 .addImm(UnitSize));
6382 } else {
6383 AddDefaultPred(BuildMI(*BB, MI, dl,
6384 TII->get(ldrOpc), scratch)
6385 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6386 .addImm(UnitSize));
6387
6388 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6389 .addReg(scratch).addReg(destIn)
6390 .addReg(0).addImm(UnitSize));
6391 }
6392 srcIn = srcOut;
6393 destIn = destOut;
6394 }
6395
6396 // Handle the leftover bytes with LDRB and STRB.
6397 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6398 // [destOut] = STRB_POST(scratch, destIn, 1)
6399 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6400 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6401 for (unsigned i = 0; i < BytesLeft; i++) {
6402 unsigned scratch = MRI.createVirtualRegister(TRC);
6403 unsigned srcOut = MRI.createVirtualRegister(TRC);
6404 unsigned destOut = MRI.createVirtualRegister(TRC);
6405 if (isThumb2) {
6406 AddDefaultPred(BuildMI(*BB, MI, dl,
6407 TII->get(ldrOpc),scratch)
6408 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6409
6410 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6411 .addReg(scratch).addReg(destIn)
6412 .addReg(0).addImm(1));
6413 } else {
6414 AddDefaultPred(BuildMI(*BB, MI, dl,
6415 TII->get(ldrOpc),scratch)
6416 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6417
6418 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6419 .addReg(scratch).addReg(destIn)
6420 .addReg(0).addImm(1));
6421 }
6422 srcIn = srcOut;
6423 destIn = destOut;
6424 }
6425 MI->eraseFromParent(); // The instruction is gone now.
6426 return BB;
6427 }
6428
6429 // Expand the pseudo op to a loop.
6430 // thisMBB:
6431 // ...
6432 // movw varEnd, # --> with thumb2
6433 // movt varEnd, #
6434 // ldrcp varEnd, idx --> without thumb2
6435 // fallthrough --> loopMBB
6436 // loopMBB:
6437 // PHI varPhi, varEnd, varLoop
6438 // PHI srcPhi, src, srcLoop
6439 // PHI destPhi, dst, destLoop
6440 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6441 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6442 // subs varLoop, varPhi, #UnitSize
6443 // bne loopMBB
6444 // fallthrough --> exitMBB
6445 // exitMBB:
6446 // epilogue to handle left-over bytes
6447 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6448 // [destOut] = STRB_POST(scratch, destLoop, 1)
6449 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6450 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6451 MF->insert(It, loopMBB);
6452 MF->insert(It, exitMBB);
6453
6454 // Transfer the remainder of BB and its successor edges to exitMBB.
6455 exitMBB->splice(exitMBB->begin(), BB,
6456 llvm::next(MachineBasicBlock::iterator(MI)),
6457 BB->end());
6458 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6459
6460 // Load an immediate to varEnd.
6461 unsigned varEnd = MRI.createVirtualRegister(TRC);
6462 if (isThumb2) {
6463 unsigned VReg1 = varEnd;
6464 if ((LoopSize & 0xFFFF0000) != 0)
6465 VReg1 = MRI.createVirtualRegister(TRC);
6466 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6467 .addImm(LoopSize & 0xFFFF));
6468
6469 if ((LoopSize & 0xFFFF0000) != 0)
6470 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6471 .addReg(VReg1)
6472 .addImm(LoopSize >> 16));
6473 } else {
6474 MachineConstantPool *ConstantPool = MF->getConstantPool();
6475 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6476 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6477
6478 // MachineConstantPool wants an explicit alignment.
6479 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6480 if (Align == 0)
6481 Align = getTargetData()->getTypeAllocSize(C->getType());
6482 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6483
6484 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6485 .addReg(varEnd, RegState::Define)
6486 .addConstantPoolIndex(Idx)
6487 .addImm(0));
6488 }
6489 BB->addSuccessor(loopMBB);
6490
6491 // Generate the loop body:
6492 // varPhi = PHI(varLoop, varEnd)
6493 // srcPhi = PHI(srcLoop, src)
6494 // destPhi = PHI(destLoop, dst)
6495 MachineBasicBlock *entryBB = BB;
6496 BB = loopMBB;
6497 unsigned varLoop = MRI.createVirtualRegister(TRC);
6498 unsigned varPhi = MRI.createVirtualRegister(TRC);
6499 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6500 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6501 unsigned destLoop = MRI.createVirtualRegister(TRC);
6502 unsigned destPhi = MRI.createVirtualRegister(TRC);
6503
6504 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6505 .addReg(varLoop).addMBB(loopMBB)
6506 .addReg(varEnd).addMBB(entryBB);
6507 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6508 .addReg(srcLoop).addMBB(loopMBB)
6509 .addReg(src).addMBB(entryBB);
6510 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6511 .addReg(destLoop).addMBB(loopMBB)
6512 .addReg(dest).addMBB(entryBB);
6513
6514 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6515 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006516 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6517 if (UnitSize >= 8) {
6518 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6519 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6520
6521 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6522 .addReg(destPhi).addImm(0).addReg(scratch));
6523 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006524 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6525 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6526
6527 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6528 .addReg(scratch).addReg(destPhi)
6529 .addImm(UnitSize));
6530 } else {
6531 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6532 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6533 .addImm(UnitSize));
6534
6535 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6536 .addReg(scratch).addReg(destPhi)
6537 .addReg(0).addImm(UnitSize));
6538 }
6539
6540 // Decrement loop variable by UnitSize.
6541 MachineInstrBuilder MIB = BuildMI(BB, dl,
6542 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6543 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6544 MIB->getOperand(5).setReg(ARM::CPSR);
6545 MIB->getOperand(5).setIsDef(true);
6546
6547 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6548 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6549
6550 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6551 BB->addSuccessor(loopMBB);
6552 BB->addSuccessor(exitMBB);
6553
6554 // Add epilogue to handle BytesLeft.
6555 BB = exitMBB;
6556 MachineInstr *StartOfExit = exitMBB->begin();
6557 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6558 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6559
6560 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6561 // [destOut] = STRB_POST(scratch, destLoop, 1)
6562 unsigned srcIn = srcLoop;
6563 unsigned destIn = destLoop;
6564 for (unsigned i = 0; i < BytesLeft; i++) {
6565 unsigned scratch = MRI.createVirtualRegister(TRC);
6566 unsigned srcOut = MRI.createVirtualRegister(TRC);
6567 unsigned destOut = MRI.createVirtualRegister(TRC);
6568 if (isThumb2) {
6569 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6570 TII->get(ldrOpc),scratch)
6571 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6572
6573 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6574 .addReg(scratch).addReg(destIn)
6575 .addImm(1));
6576 } else {
6577 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6578 TII->get(ldrOpc),scratch)
6579 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6580
6581 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6582 .addReg(scratch).addReg(destIn)
6583 .addReg(0).addImm(1));
6584 }
6585 srcIn = srcOut;
6586 destIn = destOut;
6587 }
6588
6589 MI->eraseFromParent(); // The instruction is gone now.
6590 return BB;
6591}
6592
Jim Grosbache801dc42009-12-12 01:40:06 +00006593MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006594ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006595 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006596 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006597 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006598 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006599 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006600 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006601 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006602 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006603 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006604 // The Thumb2 pre-indexed stores have the same MI operands, they just
6605 // define them differently in the .td files from the isel patterns, so
6606 // they need pseudos.
6607 case ARM::t2STR_preidx:
6608 MI->setDesc(TII->get(ARM::t2STR_PRE));
6609 return BB;
6610 case ARM::t2STRB_preidx:
6611 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6612 return BB;
6613 case ARM::t2STRH_preidx:
6614 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6615 return BB;
6616
Jim Grosbach19dec202011-08-05 20:35:44 +00006617 case ARM::STRi_preidx:
6618 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006619 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006620 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6621 // Decode the offset.
6622 unsigned Offset = MI->getOperand(4).getImm();
6623 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6624 Offset = ARM_AM::getAM2Offset(Offset);
6625 if (isSub)
6626 Offset = -Offset;
6627
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006628 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006629 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006630 .addOperand(MI->getOperand(0)) // Rn_wb
6631 .addOperand(MI->getOperand(1)) // Rt
6632 .addOperand(MI->getOperand(2)) // Rn
6633 .addImm(Offset) // offset (skip GPR==zero_reg)
6634 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006635 .addOperand(MI->getOperand(6))
6636 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006637 MI->eraseFromParent();
6638 return BB;
6639 }
6640 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006641 case ARM::STRBr_preidx:
6642 case ARM::STRH_preidx: {
6643 unsigned NewOpc;
6644 switch (MI->getOpcode()) {
6645 default: llvm_unreachable("unexpected opcode!");
6646 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6647 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6648 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6649 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006650 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6651 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6652 MIB.addOperand(MI->getOperand(i));
6653 MI->eraseFromParent();
6654 return BB;
6655 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006656 case ARM::ATOMIC_LOAD_ADD_I8:
6657 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6658 case ARM::ATOMIC_LOAD_ADD_I16:
6659 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6660 case ARM::ATOMIC_LOAD_ADD_I32:
6661 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006662
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006663 case ARM::ATOMIC_LOAD_AND_I8:
6664 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6665 case ARM::ATOMIC_LOAD_AND_I16:
6666 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6667 case ARM::ATOMIC_LOAD_AND_I32:
6668 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006669
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006670 case ARM::ATOMIC_LOAD_OR_I8:
6671 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6672 case ARM::ATOMIC_LOAD_OR_I16:
6673 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6674 case ARM::ATOMIC_LOAD_OR_I32:
6675 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006676
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006677 case ARM::ATOMIC_LOAD_XOR_I8:
6678 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6679 case ARM::ATOMIC_LOAD_XOR_I16:
6680 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6681 case ARM::ATOMIC_LOAD_XOR_I32:
6682 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006683
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006684 case ARM::ATOMIC_LOAD_NAND_I8:
6685 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6686 case ARM::ATOMIC_LOAD_NAND_I16:
6687 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6688 case ARM::ATOMIC_LOAD_NAND_I32:
6689 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006690
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006691 case ARM::ATOMIC_LOAD_SUB_I8:
6692 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6693 case ARM::ATOMIC_LOAD_SUB_I16:
6694 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6695 case ARM::ATOMIC_LOAD_SUB_I32:
6696 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006697
Jim Grosbachf7da8822011-04-26 19:44:18 +00006698 case ARM::ATOMIC_LOAD_MIN_I8:
6699 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6700 case ARM::ATOMIC_LOAD_MIN_I16:
6701 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6702 case ARM::ATOMIC_LOAD_MIN_I32:
6703 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6704
6705 case ARM::ATOMIC_LOAD_MAX_I8:
6706 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6707 case ARM::ATOMIC_LOAD_MAX_I16:
6708 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6709 case ARM::ATOMIC_LOAD_MAX_I32:
6710 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6711
6712 case ARM::ATOMIC_LOAD_UMIN_I8:
6713 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6714 case ARM::ATOMIC_LOAD_UMIN_I16:
6715 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6716 case ARM::ATOMIC_LOAD_UMIN_I32:
6717 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6718
6719 case ARM::ATOMIC_LOAD_UMAX_I8:
6720 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6721 case ARM::ATOMIC_LOAD_UMAX_I16:
6722 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6723 case ARM::ATOMIC_LOAD_UMAX_I32:
6724 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6725
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006726 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6727 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6728 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006729
6730 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6731 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6732 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006733
Eli Friedman2bdffe42011-08-31 00:31:29 +00006734
6735 case ARM::ATOMADD6432:
6736 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006737 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6738 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006739 case ARM::ATOMSUB6432:
6740 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006741 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6742 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006743 case ARM::ATOMOR6432:
6744 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006745 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006746 case ARM::ATOMXOR6432:
6747 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006748 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006749 case ARM::ATOMAND6432:
6750 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006751 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006752 case ARM::ATOMSWAP6432:
6753 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006754 case ARM::ATOMCMPXCHG6432:
6755 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6756 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6757 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006758
Evan Cheng007ea272009-08-12 05:17:19 +00006759 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006760 // To "insert" a SELECT_CC instruction, we actually have to insert the
6761 // diamond control-flow pattern. The incoming instruction knows the
6762 // destination vreg to set, the condition code register to branch on, the
6763 // true/false values to select between, and a branch opcode to use.
6764 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006765 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006766 ++It;
6767
6768 // thisMBB:
6769 // ...
6770 // TrueVal = ...
6771 // cmpTY ccX, r1, r2
6772 // bCC copy1MBB
6773 // fallthrough --> copy0MBB
6774 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006775 MachineFunction *F = BB->getParent();
6776 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6777 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006778 F->insert(It, copy0MBB);
6779 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006780
6781 // Transfer the remainder of BB and its successor edges to sinkMBB.
6782 sinkMBB->splice(sinkMBB->begin(), BB,
6783 llvm::next(MachineBasicBlock::iterator(MI)),
6784 BB->end());
6785 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6786
Dan Gohman258c58c2010-07-06 15:49:48 +00006787 BB->addSuccessor(copy0MBB);
6788 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006789
Dan Gohman14152b42010-07-06 20:24:04 +00006790 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6791 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6792
Evan Chenga8e29892007-01-19 07:51:42 +00006793 // copy0MBB:
6794 // %FalseValue = ...
6795 // # fallthrough to sinkMBB
6796 BB = copy0MBB;
6797
6798 // Update machine-CFG edges
6799 BB->addSuccessor(sinkMBB);
6800
6801 // sinkMBB:
6802 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6803 // ...
6804 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006805 BuildMI(*BB, BB->begin(), dl,
6806 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006807 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6808 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6809
Dan Gohman14152b42010-07-06 20:24:04 +00006810 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006811 return BB;
6812 }
Evan Cheng86198642009-08-07 00:34:42 +00006813
Evan Cheng218977b2010-07-13 19:27:42 +00006814 case ARM::BCCi64:
6815 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006816 // If there is an unconditional branch to the other successor, remove it.
6817 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006818
Evan Cheng218977b2010-07-13 19:27:42 +00006819 // Compare both parts that make up the double comparison separately for
6820 // equality.
6821 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6822
6823 unsigned LHS1 = MI->getOperand(1).getReg();
6824 unsigned LHS2 = MI->getOperand(2).getReg();
6825 if (RHSisZero) {
6826 AddDefaultPred(BuildMI(BB, dl,
6827 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6828 .addReg(LHS1).addImm(0));
6829 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6830 .addReg(LHS2).addImm(0)
6831 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6832 } else {
6833 unsigned RHS1 = MI->getOperand(3).getReg();
6834 unsigned RHS2 = MI->getOperand(4).getReg();
6835 AddDefaultPred(BuildMI(BB, dl,
6836 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6837 .addReg(LHS1).addReg(RHS1));
6838 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6839 .addReg(LHS2).addReg(RHS2)
6840 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6841 }
6842
6843 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6844 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6845 if (MI->getOperand(0).getImm() == ARMCC::NE)
6846 std::swap(destMBB, exitMBB);
6847
6848 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6849 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006850 if (isThumb2)
6851 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6852 else
6853 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006854
6855 MI->eraseFromParent(); // The pseudo instruction is gone now.
6856 return BB;
6857 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006858
Bill Wendling5bc85282011-10-17 20:37:20 +00006859 case ARM::Int_eh_sjlj_setjmp:
6860 case ARM::Int_eh_sjlj_setjmp_nofp:
6861 case ARM::tInt_eh_sjlj_setjmp:
6862 case ARM::t2Int_eh_sjlj_setjmp:
6863 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6864 EmitSjLjDispatchBlock(MI, BB);
6865 return BB;
6866
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006867 case ARM::ABS:
6868 case ARM::t2ABS: {
6869 // To insert an ABS instruction, we have to insert the
6870 // diamond control-flow pattern. The incoming instruction knows the
6871 // source vreg to test against 0, the destination vreg to set,
6872 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006873 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006874 // It transforms
6875 // V1 = ABS V0
6876 // into
6877 // V2 = MOVS V0
6878 // BCC (branch to SinkBB if V0 >= 0)
6879 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006880 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006881 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6882 MachineFunction::iterator BBI = BB;
6883 ++BBI;
6884 MachineFunction *Fn = BB->getParent();
6885 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6886 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6887 Fn->insert(BBI, RSBBB);
6888 Fn->insert(BBI, SinkBB);
6889
6890 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6891 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6892 bool isThumb2 = Subtarget->isThumb2();
6893 MachineRegisterInfo &MRI = Fn->getRegInfo();
6894 // In Thumb mode S must not be specified if source register is the SP or
6895 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006896 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6897 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6898 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006899
6900 // Transfer the remainder of BB and its successor edges to sinkMBB.
6901 SinkBB->splice(SinkBB->begin(), BB,
6902 llvm::next(MachineBasicBlock::iterator(MI)),
6903 BB->end());
6904 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6905
6906 BB->addSuccessor(RSBBB);
6907 BB->addSuccessor(SinkBB);
6908
6909 // fall through to SinkMBB
6910 RSBBB->addSuccessor(SinkBB);
6911
Manman Ren307473d2012-06-15 21:32:12 +00006912 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006913 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006914 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6915 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006916
6917 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006918 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006919 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6920 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6921
6922 // insert rsbri in RSBBB
6923 // Note: BCC and rsbri will be converted into predicated rsbmi
6924 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006925 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006926 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006927 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006928 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6929
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006930 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006931 // reuse ABSDstReg to not change uses of ABS instruction
6932 BuildMI(*SinkBB, SinkBB->begin(), dl,
6933 TII->get(ARM::PHI), ABSDstReg)
6934 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006935 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006936
6937 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006938 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006939
6940 // return last added BB
6941 return SinkBB;
6942 }
Manman Ren68f25572012-06-01 19:33:18 +00006943 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006944 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006945 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006946 }
6947}
6948
Evan Cheng37fefc22011-08-30 19:09:48 +00006949void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6950 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006951 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006952 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6953 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6954 return;
6955 }
6956
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006957 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006958 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6959 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6960 // operand is still set to noreg. If needed, set the optional operand's
6961 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006962 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006963 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006964
Andrew Trick3be654f2011-09-21 02:20:46 +00006965 // Rename pseudo opcodes.
6966 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6967 if (NewOpc) {
6968 const ARMBaseInstrInfo *TII =
6969 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006970 MCID = &TII->get(NewOpc);
6971
6972 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6973 "converted opcode should be the same except for cc_out");
6974
6975 MI->setDesc(*MCID);
6976
6977 // Add the optional cc_out operand
6978 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006979 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006980 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006981
6982 // Any ARM instruction that sets the 's' bit should specify an optional
6983 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006984 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006985 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006986 return;
6987 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006988 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6989 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006990 bool definesCPSR = false;
6991 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006992 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006993 i != e; ++i) {
6994 const MachineOperand &MO = MI->getOperand(i);
6995 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6996 definesCPSR = true;
6997 if (MO.isDead())
6998 deadCPSR = true;
6999 MI->RemoveOperand(i);
7000 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007001 }
7002 }
Andrew Trick4815d562011-09-20 03:17:40 +00007003 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007004 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007005 return;
7006 }
7007 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007008 if (deadCPSR) {
7009 assert(!MI->getOperand(ccOutIdx).getReg() &&
7010 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007011 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007012 }
Andrew Trick4815d562011-09-20 03:17:40 +00007013
Andrew Trick3be654f2011-09-21 02:20:46 +00007014 // If this instruction was defined with an optional CPSR def and its dag node
7015 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007016 MachineOperand &MO = MI->getOperand(ccOutIdx);
7017 MO.setReg(ARM::CPSR);
7018 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007019}
7020
Evan Chenga8e29892007-01-19 07:51:42 +00007021//===----------------------------------------------------------------------===//
7022// ARM Optimization Hooks
7023//===----------------------------------------------------------------------===//
7024
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007025// Helper function that checks if N is a null or all ones constant.
7026static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7027 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7028 if (!C)
7029 return false;
7030 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7031}
7032
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007033// Return true if N is conditionally 0 or all ones.
7034// Detects these expressions where cc is an i1 value:
7035//
7036// (select cc 0, y) [AllOnes=0]
7037// (select cc y, 0) [AllOnes=0]
7038// (zext cc) [AllOnes=0]
7039// (sext cc) [AllOnes=0/1]
7040// (select cc -1, y) [AllOnes=1]
7041// (select cc y, -1) [AllOnes=1]
7042//
7043// Invert is set when N is the null/all ones constant when CC is false.
7044// OtherOp is set to the alternative value of N.
7045static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7046 SDValue &CC, bool &Invert,
7047 SDValue &OtherOp,
7048 SelectionDAG &DAG) {
7049 switch (N->getOpcode()) {
7050 default: return false;
7051 case ISD::SELECT: {
7052 CC = N->getOperand(0);
7053 SDValue N1 = N->getOperand(1);
7054 SDValue N2 = N->getOperand(2);
7055 if (isZeroOrAllOnes(N1, AllOnes)) {
7056 Invert = false;
7057 OtherOp = N2;
7058 return true;
7059 }
7060 if (isZeroOrAllOnes(N2, AllOnes)) {
7061 Invert = true;
7062 OtherOp = N1;
7063 return true;
7064 }
7065 return false;
7066 }
7067 case ISD::ZERO_EXTEND:
7068 // (zext cc) can never be the all ones value.
7069 if (AllOnes)
7070 return false;
7071 // Fall through.
7072 case ISD::SIGN_EXTEND: {
7073 EVT VT = N->getValueType(0);
7074 CC = N->getOperand(0);
7075 if (CC.getValueType() != MVT::i1)
7076 return false;
7077 Invert = !AllOnes;
7078 if (AllOnes)
7079 // When looking for an AllOnes constant, N is an sext, and the 'other'
7080 // value is 0.
7081 OtherOp = DAG.getConstant(0, VT);
7082 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7083 // When looking for a 0 constant, N can be zext or sext.
7084 OtherOp = DAG.getConstant(1, VT);
7085 else
7086 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7087 return true;
7088 }
7089 }
7090}
7091
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007092// Combine a constant select operand into its use:
7093//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007094// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7095// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7096// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7097// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7098// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007099//
7100// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007101// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007102//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007103// Also recognize sext/zext from i1:
7104//
7105// (add (zext cc), x) -> (select cc (add x, 1), x)
7106// (add (sext cc), x) -> (select cc (add x, -1), x)
7107//
7108// These transformations eventually create predicated instructions.
7109//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007110// @param N The node to transform.
7111// @param Slct The N operand that is a select.
7112// @param OtherOp The other N operand (x above).
7113// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007114// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007115// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007116static
7117SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007118 TargetLowering::DAGCombinerInfo &DCI,
7119 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007120 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007121 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007122 SDValue NonConstantVal;
7123 SDValue CCOp;
7124 bool SwapSelectOps;
7125 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7126 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007127 return SDValue();
7128
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007129 // Slct is now know to be the desired identity constant when CC is true.
7130 SDValue TrueVal = OtherOp;
7131 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7132 OtherOp, NonConstantVal);
7133 // Unless SwapSelectOps says CC should be false.
7134 if (SwapSelectOps)
7135 std::swap(TrueVal, FalseVal);
7136
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007137 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007138 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007139}
7140
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007141// Attempt combineSelectAndUse on each operand of a commutative operator N.
7142static
7143SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7144 TargetLowering::DAGCombinerInfo &DCI) {
7145 SDValue N0 = N->getOperand(0);
7146 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007147 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007148 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7149 if (Result.getNode())
7150 return Result;
7151 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007152 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007153 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7154 if (Result.getNode())
7155 return Result;
7156 }
7157 return SDValue();
7158}
7159
Eric Christopherfa6f5912011-06-29 21:10:36 +00007160// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007161// (only after legalization).
7162static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7163 TargetLowering::DAGCombinerInfo &DCI,
7164 const ARMSubtarget *Subtarget) {
7165
7166 // Only perform optimization if after legalize, and if NEON is available. We
7167 // also expected both operands to be BUILD_VECTORs.
7168 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7169 || N0.getOpcode() != ISD::BUILD_VECTOR
7170 || N1.getOpcode() != ISD::BUILD_VECTOR)
7171 return SDValue();
7172
7173 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7174 EVT VT = N->getValueType(0);
7175 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7176 return SDValue();
7177
7178 // Check that the vector operands are of the right form.
7179 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7180 // operands, where N is the size of the formed vector.
7181 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7182 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007183
7184 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007185 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007186 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007187 SDValue Vec = N0->getOperand(0)->getOperand(0);
7188 SDNode *V = Vec.getNode();
7189 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007190
Eric Christopherfa6f5912011-06-29 21:10:36 +00007191 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007192 // check to see if each of their operands are an EXTRACT_VECTOR with
7193 // the same vector and appropriate index.
7194 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7195 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7196 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007197
Tanya Lattner189531f2011-06-14 23:48:48 +00007198 SDValue ExtVec0 = N0->getOperand(i);
7199 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007200
Tanya Lattner189531f2011-06-14 23:48:48 +00007201 // First operand is the vector, verify its the same.
7202 if (V != ExtVec0->getOperand(0).getNode() ||
7203 V != ExtVec1->getOperand(0).getNode())
7204 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007205
Tanya Lattner189531f2011-06-14 23:48:48 +00007206 // Second is the constant, verify its correct.
7207 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7208 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007209
Tanya Lattner189531f2011-06-14 23:48:48 +00007210 // For the constant, we want to see all the even or all the odd.
7211 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7212 || C1->getZExtValue() != nextIndex+1)
7213 return SDValue();
7214
7215 // Increment index.
7216 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007217 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007218 return SDValue();
7219 }
7220
7221 // Create VPADDL node.
7222 SelectionDAG &DAG = DCI.DAG;
7223 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007224
7225 // Build operand list.
7226 SmallVector<SDValue, 8> Ops;
7227 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7228 TLI.getPointerTy()));
7229
7230 // Input is the vector.
7231 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007232
Tanya Lattner189531f2011-06-14 23:48:48 +00007233 // Get widened type and narrowed type.
7234 MVT widenType;
7235 unsigned numElem = VT.getVectorNumElements();
7236 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7237 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7238 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7239 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7240 default:
Craig Topperbc219812012-02-07 02:50:20 +00007241 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007242 }
7243
7244 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7245 widenType, &Ops[0], Ops.size());
7246 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7247}
7248
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007249static SDValue findMUL_LOHI(SDValue V) {
7250 if (V->getOpcode() == ISD::UMUL_LOHI ||
7251 V->getOpcode() == ISD::SMUL_LOHI)
7252 return V;
7253 return SDValue();
7254}
7255
7256static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7257 TargetLowering::DAGCombinerInfo &DCI,
7258 const ARMSubtarget *Subtarget) {
7259
7260 if (Subtarget->isThumb1Only()) return SDValue();
7261
7262 // Only perform the checks after legalize when the pattern is available.
7263 if (DCI.isBeforeLegalize()) return SDValue();
7264
7265 // Look for multiply add opportunities.
7266 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7267 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7268 // a glue link from the first add to the second add.
7269 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7270 // a S/UMLAL instruction.
7271 // loAdd UMUL_LOHI
7272 // \ / :lo \ :hi
7273 // \ / \ [no multiline comment]
7274 // ADDC | hiAdd
7275 // \ :glue / /
7276 // \ / /
7277 // ADDE
7278 //
7279 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7280 SDValue AddcOp0 = AddcNode->getOperand(0);
7281 SDValue AddcOp1 = AddcNode->getOperand(1);
7282
7283 // Check if the two operands are from the same mul_lohi node.
7284 if (AddcOp0.getNode() == AddcOp1.getNode())
7285 return SDValue();
7286
7287 assert(AddcNode->getNumValues() == 2 &&
7288 AddcNode->getValueType(0) == MVT::i32 &&
7289 AddcNode->getValueType(1) == MVT::Glue &&
7290 "Expect ADDC with two result values: i32, glue");
7291
7292 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7293 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7294 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7295 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7296 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7297 return SDValue();
7298
7299 // Look for the glued ADDE.
7300 SDNode* AddeNode = AddcNode->getGluedUser();
7301 if (AddeNode == NULL)
7302 return SDValue();
7303
7304 // Make sure it is really an ADDE.
7305 if (AddeNode->getOpcode() != ISD::ADDE)
7306 return SDValue();
7307
7308 assert(AddeNode->getNumOperands() == 3 &&
7309 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7310 "ADDE node has the wrong inputs");
7311
7312 // Check for the triangle shape.
7313 SDValue AddeOp0 = AddeNode->getOperand(0);
7314 SDValue AddeOp1 = AddeNode->getOperand(1);
7315
7316 // Make sure that the ADDE operands are not coming from the same node.
7317 if (AddeOp0.getNode() == AddeOp1.getNode())
7318 return SDValue();
7319
7320 // Find the MUL_LOHI node walking up ADDE's operands.
7321 bool IsLeftOperandMUL = false;
7322 SDValue MULOp = findMUL_LOHI(AddeOp0);
7323 if (MULOp == SDValue())
7324 MULOp = findMUL_LOHI(AddeOp1);
7325 else
7326 IsLeftOperandMUL = true;
7327 if (MULOp == SDValue())
7328 return SDValue();
7329
7330 // Figure out the right opcode.
7331 unsigned Opc = MULOp->getOpcode();
7332 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7333
7334 // Figure out the high and low input values to the MLAL node.
7335 SDValue* HiMul = &MULOp;
7336 SDValue* HiAdd = NULL;
7337 SDValue* LoMul = NULL;
7338 SDValue* LowAdd = NULL;
7339
7340 if (IsLeftOperandMUL)
7341 HiAdd = &AddeOp1;
7342 else
7343 HiAdd = &AddeOp0;
7344
7345
7346 if (AddcOp0->getOpcode() == Opc) {
7347 LoMul = &AddcOp0;
7348 LowAdd = &AddcOp1;
7349 }
7350 if (AddcOp1->getOpcode() == Opc) {
7351 LoMul = &AddcOp1;
7352 LowAdd = &AddcOp0;
7353 }
7354
7355 if (LoMul == NULL)
7356 return SDValue();
7357
7358 if (LoMul->getNode() != HiMul->getNode())
7359 return SDValue();
7360
7361 // Create the merged node.
7362 SelectionDAG &DAG = DCI.DAG;
7363
7364 // Build operand list.
7365 SmallVector<SDValue, 8> Ops;
7366 Ops.push_back(LoMul->getOperand(0));
7367 Ops.push_back(LoMul->getOperand(1));
7368 Ops.push_back(*LowAdd);
7369 Ops.push_back(*HiAdd);
7370
7371 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7372 DAG.getVTList(MVT::i32, MVT::i32),
7373 &Ops[0], Ops.size());
7374
7375 // Replace the ADDs' nodes uses by the MLA node's values.
7376 SDValue HiMLALResult(MLALNode.getNode(), 1);
7377 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7378
7379 SDValue LoMLALResult(MLALNode.getNode(), 0);
7380 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7381
7382 // Return original node to notify the driver to stop replacing.
7383 SDValue resNode(AddcNode, 0);
7384 return resNode;
7385}
7386
7387/// PerformADDCCombine - Target-specific dag combine transform from
7388/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7389static SDValue PerformADDCCombine(SDNode *N,
7390 TargetLowering::DAGCombinerInfo &DCI,
7391 const ARMSubtarget *Subtarget) {
7392
7393 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7394
7395}
7396
Bob Wilson3d5792a2010-07-29 20:34:14 +00007397/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7398/// operands N0 and N1. This is a helper for PerformADDCombine that is
7399/// called with the default operands, and if that fails, with commuted
7400/// operands.
7401static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007402 TargetLowering::DAGCombinerInfo &DCI,
7403 const ARMSubtarget *Subtarget){
7404
7405 // Attempt to create vpaddl for this add.
7406 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7407 if (Result.getNode())
7408 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007409
Chris Lattnerd1980a52009-03-12 06:52:53 +00007410 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007411 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007412 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7413 if (Result.getNode()) return Result;
7414 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007415 return SDValue();
7416}
7417
Bob Wilson3d5792a2010-07-29 20:34:14 +00007418/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7419///
7420static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007421 TargetLowering::DAGCombinerInfo &DCI,
7422 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007423 SDValue N0 = N->getOperand(0);
7424 SDValue N1 = N->getOperand(1);
7425
7426 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007427 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007428 if (Result.getNode())
7429 return Result;
7430
7431 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007432 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007433}
7434
Chris Lattnerd1980a52009-03-12 06:52:53 +00007435/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007436///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007437static SDValue PerformSUBCombine(SDNode *N,
7438 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007439 SDValue N0 = N->getOperand(0);
7440 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007441
Chris Lattnerd1980a52009-03-12 06:52:53 +00007442 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007443 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007444 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7445 if (Result.getNode()) return Result;
7446 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007447
Chris Lattnerd1980a52009-03-12 06:52:53 +00007448 return SDValue();
7449}
7450
Evan Cheng463d3582011-03-31 19:38:48 +00007451/// PerformVMULCombine
7452/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7453/// special multiplier accumulator forwarding.
7454/// vmul d3, d0, d2
7455/// vmla d3, d1, d2
7456/// is faster than
7457/// vadd d3, d0, d1
7458/// vmul d3, d3, d2
7459static SDValue PerformVMULCombine(SDNode *N,
7460 TargetLowering::DAGCombinerInfo &DCI,
7461 const ARMSubtarget *Subtarget) {
7462 if (!Subtarget->hasVMLxForwarding())
7463 return SDValue();
7464
7465 SelectionDAG &DAG = DCI.DAG;
7466 SDValue N0 = N->getOperand(0);
7467 SDValue N1 = N->getOperand(1);
7468 unsigned Opcode = N0.getOpcode();
7469 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7470 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007471 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007472 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7473 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7474 return SDValue();
7475 std::swap(N0, N1);
7476 }
7477
7478 EVT VT = N->getValueType(0);
7479 DebugLoc DL = N->getDebugLoc();
7480 SDValue N00 = N0->getOperand(0);
7481 SDValue N01 = N0->getOperand(1);
7482 return DAG.getNode(Opcode, DL, VT,
7483 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7484 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7485}
7486
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007487static SDValue PerformMULCombine(SDNode *N,
7488 TargetLowering::DAGCombinerInfo &DCI,
7489 const ARMSubtarget *Subtarget) {
7490 SelectionDAG &DAG = DCI.DAG;
7491
7492 if (Subtarget->isThumb1Only())
7493 return SDValue();
7494
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007495 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7496 return SDValue();
7497
7498 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007499 if (VT.is64BitVector() || VT.is128BitVector())
7500 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007501 if (VT != MVT::i32)
7502 return SDValue();
7503
7504 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7505 if (!C)
7506 return SDValue();
7507
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007508 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007509 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007510
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007511 ShiftAmt = ShiftAmt & (32 - 1);
7512 SDValue V = N->getOperand(0);
7513 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007514
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007515 SDValue Res;
7516 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007517
7518 if (MulAmt >= 0) {
7519 if (isPowerOf2_32(MulAmt - 1)) {
7520 // (mul x, 2^N + 1) => (add (shl x, N), x)
7521 Res = DAG.getNode(ISD::ADD, DL, VT,
7522 V,
7523 DAG.getNode(ISD::SHL, DL, VT,
7524 V,
7525 DAG.getConstant(Log2_32(MulAmt - 1),
7526 MVT::i32)));
7527 } else if (isPowerOf2_32(MulAmt + 1)) {
7528 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7529 Res = DAG.getNode(ISD::SUB, DL, VT,
7530 DAG.getNode(ISD::SHL, DL, VT,
7531 V,
7532 DAG.getConstant(Log2_32(MulAmt + 1),
7533 MVT::i32)),
7534 V);
7535 } else
7536 return SDValue();
7537 } else {
7538 uint64_t MulAmtAbs = -MulAmt;
7539 if (isPowerOf2_32(MulAmtAbs + 1)) {
7540 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7541 Res = DAG.getNode(ISD::SUB, DL, VT,
7542 V,
7543 DAG.getNode(ISD::SHL, DL, VT,
7544 V,
7545 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7546 MVT::i32)));
7547 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7548 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7549 Res = DAG.getNode(ISD::ADD, DL, VT,
7550 V,
7551 DAG.getNode(ISD::SHL, DL, VT,
7552 V,
7553 DAG.getConstant(Log2_32(MulAmtAbs-1),
7554 MVT::i32)));
7555 Res = DAG.getNode(ISD::SUB, DL, VT,
7556 DAG.getConstant(0, MVT::i32),Res);
7557
7558 } else
7559 return SDValue();
7560 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007561
7562 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007563 Res = DAG.getNode(ISD::SHL, DL, VT,
7564 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007565
7566 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007567 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007568 return SDValue();
7569}
7570
Owen Anderson080c0922010-11-05 19:27:46 +00007571static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007572 TargetLowering::DAGCombinerInfo &DCI,
7573 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007574
Owen Anderson080c0922010-11-05 19:27:46 +00007575 // Attempt to use immediate-form VBIC
7576 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7577 DebugLoc dl = N->getDebugLoc();
7578 EVT VT = N->getValueType(0);
7579 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007580
Tanya Lattner0433b212011-04-07 15:24:20 +00007581 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7582 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007583
Owen Anderson080c0922010-11-05 19:27:46 +00007584 APInt SplatBits, SplatUndef;
7585 unsigned SplatBitSize;
7586 bool HasAnyUndefs;
7587 if (BVN &&
7588 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7589 if (SplatBitSize <= 64) {
7590 EVT VbicVT;
7591 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7592 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007593 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007594 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007595 if (Val.getNode()) {
7596 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007597 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007598 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007599 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007600 }
7601 }
7602 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007603
Evan Chengc892aeb2012-02-23 01:19:06 +00007604 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007605 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7606 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7607 if (Result.getNode())
7608 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007609 }
7610
Owen Anderson080c0922010-11-05 19:27:46 +00007611 return SDValue();
7612}
7613
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007614/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7615static SDValue PerformORCombine(SDNode *N,
7616 TargetLowering::DAGCombinerInfo &DCI,
7617 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007618 // Attempt to use immediate-form VORR
7619 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7620 DebugLoc dl = N->getDebugLoc();
7621 EVT VT = N->getValueType(0);
7622 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007623
Tanya Lattner0433b212011-04-07 15:24:20 +00007624 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7625 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007626
Owen Anderson60f48702010-11-03 23:15:26 +00007627 APInt SplatBits, SplatUndef;
7628 unsigned SplatBitSize;
7629 bool HasAnyUndefs;
7630 if (BVN && Subtarget->hasNEON() &&
7631 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7632 if (SplatBitSize <= 64) {
7633 EVT VorrVT;
7634 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7635 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007636 DAG, VorrVT, VT.is128BitVector(),
7637 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007638 if (Val.getNode()) {
7639 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007640 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007641 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007642 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007643 }
7644 }
7645 }
7646
Evan Chengc892aeb2012-02-23 01:19:06 +00007647 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007648 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7649 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7650 if (Result.getNode())
7651 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007652 }
7653
Nadav Rotemdf832032012-08-13 18:52:44 +00007654 // The code below optimizes (or (and X, Y), Z).
7655 // The AND operand needs to have a single user to make these optimizations
7656 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007657 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00007658 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007659 return SDValue();
7660 SDValue N1 = N->getOperand(1);
7661
7662 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7663 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7664 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7665 APInt SplatUndef;
7666 unsigned SplatBitSize;
7667 bool HasAnyUndefs;
7668
7669 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7670 APInt SplatBits0;
7671 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7672 HasAnyUndefs) && !HasAnyUndefs) {
7673 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7674 APInt SplatBits1;
7675 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7676 HasAnyUndefs) && !HasAnyUndefs &&
7677 SplatBits0 == ~SplatBits1) {
7678 // Canonicalize the vector type to make instruction selection simpler.
7679 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7680 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7681 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007682 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007683 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7684 }
7685 }
7686 }
7687
Jim Grosbach54238562010-07-17 03:30:54 +00007688 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7689 // reasonable.
7690
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007691 // BFI is only available on V6T2+
7692 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7693 return SDValue();
7694
Jim Grosbach54238562010-07-17 03:30:54 +00007695 DebugLoc DL = N->getDebugLoc();
7696 // 1) or (and A, mask), val => ARMbfi A, val, mask
7697 // iff (val & mask) == val
7698 //
7699 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7700 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007701 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007702 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007703 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007704 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007705
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007706 if (VT != MVT::i32)
7707 return SDValue();
7708
Evan Cheng30fb13f2010-12-13 20:32:54 +00007709 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007710
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007711 // The value and the mask need to be constants so we can verify this is
7712 // actually a bitfield set. If the mask is 0xffff, we can do better
7713 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007714 SDValue MaskOp = N0.getOperand(1);
7715 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7716 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007717 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007718 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007719 if (Mask == 0xffff)
7720 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007721 SDValue Res;
7722 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007723 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7724 if (N1C) {
7725 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007726 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007727 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007728
Evan Chenga9688c42010-12-11 04:11:38 +00007729 if (ARM::isBitFieldInvertedMask(Mask)) {
7730 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007731
Evan Cheng30fb13f2010-12-13 20:32:54 +00007732 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007733 DAG.getConstant(Val, MVT::i32),
7734 DAG.getConstant(Mask, MVT::i32));
7735
7736 // Do not add new nodes to DAG combiner worklist.
7737 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007738 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007739 }
Jim Grosbach54238562010-07-17 03:30:54 +00007740 } else if (N1.getOpcode() == ISD::AND) {
7741 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007742 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7743 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007744 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007745 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007746
Eric Christopher29aeed12011-03-26 01:21:03 +00007747 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7748 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007749 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007750 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007751 // The pack halfword instruction works better for masks that fit it,
7752 // so use that when it's available.
7753 if (Subtarget->hasT2ExtractPack() &&
7754 (Mask == 0xffff || Mask == 0xffff0000))
7755 return SDValue();
7756 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007757 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007758 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007759 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007760 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007761 DAG.getConstant(Mask, MVT::i32));
7762 // Do not add new nodes to DAG combiner worklist.
7763 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007764 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007765 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007766 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007767 // The pack halfword instruction works better for masks that fit it,
7768 // so use that when it's available.
7769 if (Subtarget->hasT2ExtractPack() &&
7770 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7771 return SDValue();
7772 // 2b
7773 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007774 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007775 DAG.getConstant(lsb, MVT::i32));
7776 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007777 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007778 // Do not add new nodes to DAG combiner worklist.
7779 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007780 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007781 }
7782 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007783
Evan Cheng30fb13f2010-12-13 20:32:54 +00007784 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7785 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7786 ARM::isBitFieldInvertedMask(~Mask)) {
7787 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7788 // where lsb(mask) == #shamt and masked bits of B are known zero.
7789 SDValue ShAmt = N00.getOperand(1);
7790 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7791 unsigned LSB = CountTrailingZeros_32(Mask);
7792 if (ShAmtC != LSB)
7793 return SDValue();
7794
7795 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7796 DAG.getConstant(~Mask, MVT::i32));
7797
7798 // Do not add new nodes to DAG combiner worklist.
7799 DCI.CombineTo(N, Res, false);
7800 }
7801
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007802 return SDValue();
7803}
7804
Evan Chengc892aeb2012-02-23 01:19:06 +00007805static SDValue PerformXORCombine(SDNode *N,
7806 TargetLowering::DAGCombinerInfo &DCI,
7807 const ARMSubtarget *Subtarget) {
7808 EVT VT = N->getValueType(0);
7809 SelectionDAG &DAG = DCI.DAG;
7810
7811 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7812 return SDValue();
7813
7814 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007815 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
7816 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7817 if (Result.getNode())
7818 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007819 }
7820
7821 return SDValue();
7822}
7823
Evan Chengbf188ae2011-06-15 01:12:31 +00007824/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7825/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007826static SDValue PerformBFICombine(SDNode *N,
7827 TargetLowering::DAGCombinerInfo &DCI) {
7828 SDValue N1 = N->getOperand(1);
7829 if (N1.getOpcode() == ISD::AND) {
7830 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7831 if (!N11C)
7832 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007833 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7834 unsigned LSB = CountTrailingZeros_32(~InvMask);
7835 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7836 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007837 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007838 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007839 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7840 N->getOperand(0), N1.getOperand(0),
7841 N->getOperand(2));
7842 }
7843 return SDValue();
7844}
7845
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007846/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7847/// ARMISD::VMOVRRD.
7848static SDValue PerformVMOVRRDCombine(SDNode *N,
7849 TargetLowering::DAGCombinerInfo &DCI) {
7850 // vmovrrd(vmovdrr x, y) -> x,y
7851 SDValue InDouble = N->getOperand(0);
7852 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7853 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007854
7855 // vmovrrd(load f64) -> (load i32), (load i32)
7856 SDNode *InNode = InDouble.getNode();
7857 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7858 InNode->getValueType(0) == MVT::f64 &&
7859 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7860 !cast<LoadSDNode>(InNode)->isVolatile()) {
7861 // TODO: Should this be done for non-FrameIndex operands?
7862 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7863
7864 SelectionDAG &DAG = DCI.DAG;
7865 DebugLoc DL = LD->getDebugLoc();
7866 SDValue BasePtr = LD->getBasePtr();
7867 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7868 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007869 LD->isNonTemporal(), LD->isInvariant(),
7870 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007871
7872 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7873 DAG.getConstant(4, MVT::i32));
7874 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7875 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007876 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007877 std::min(4U, LD->getAlignment() / 2));
7878
7879 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7880 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7881 DCI.RemoveFromWorklist(LD);
7882 DAG.DeleteNode(LD);
7883 return Result;
7884 }
7885
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007886 return SDValue();
7887}
7888
7889/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7890/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7891static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7892 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7893 SDValue Op0 = N->getOperand(0);
7894 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007895 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007896 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007897 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007898 Op1 = Op1.getOperand(0);
7899 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7900 Op0.getNode() == Op1.getNode() &&
7901 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007902 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007903 N->getValueType(0), Op0.getOperand(0));
7904 return SDValue();
7905}
7906
Bob Wilson31600902010-12-21 06:43:19 +00007907/// PerformSTORECombine - Target-specific dag combine xforms for
7908/// ISD::STORE.
7909static SDValue PerformSTORECombine(SDNode *N,
7910 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007911 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007912 if (St->isVolatile())
7913 return SDValue();
7914
Andrew Trick49b446f2012-07-18 18:34:24 +00007915 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007916 // pack all of the elements in one place. Next, store to memory in fewer
7917 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007918 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007919 EVT VT = StVal.getValueType();
7920 if (St->isTruncatingStore() && VT.isVector()) {
7921 SelectionDAG &DAG = DCI.DAG;
7922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7923 EVT StVT = St->getMemoryVT();
7924 unsigned NumElems = VT.getVectorNumElements();
7925 assert(StVT != VT && "Cannot truncate to the same type");
7926 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7927 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7928
7929 // From, To sizes and ElemCount must be pow of two
7930 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7931
7932 // We are going to use the original vector elt for storing.
7933 // Accumulated smaller vector elements must be a multiple of the store size.
7934 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7935
7936 unsigned SizeRatio = FromEltSz / ToEltSz;
7937 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7938
7939 // Create a type on which we perform the shuffle.
7940 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7941 NumElems*SizeRatio);
7942 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7943
7944 DebugLoc DL = St->getDebugLoc();
7945 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7946 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7947 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7948
7949 // Can't shuffle using an illegal type.
7950 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7951
7952 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7953 DAG.getUNDEF(WideVec.getValueType()),
7954 ShuffleVec.data());
7955 // At this point all of the data is stored at the bottom of the
7956 // register. We now need to save it to mem.
7957
7958 // Find the largest store unit
7959 MVT StoreType = MVT::i8;
7960 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7961 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7962 MVT Tp = (MVT::SimpleValueType)tp;
7963 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7964 StoreType = Tp;
7965 }
7966 // Didn't find a legal store type.
7967 if (!TLI.isTypeLegal(StoreType))
7968 return SDValue();
7969
7970 // Bitcast the original vector into a vector of store-size units
7971 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7972 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7973 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7974 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7975 SmallVector<SDValue, 8> Chains;
7976 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7977 TLI.getPointerTy());
7978 SDValue BasePtr = St->getBasePtr();
7979
7980 // Perform one or more big stores into memory.
7981 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7982 for (unsigned I = 0; I < E; I++) {
7983 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7984 StoreType, ShuffWide,
7985 DAG.getIntPtrConstant(I));
7986 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7987 St->getPointerInfo(), St->isVolatile(),
7988 St->isNonTemporal(), St->getAlignment());
7989 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7990 Increment);
7991 Chains.push_back(Ch);
7992 }
7993 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7994 Chains.size());
7995 }
7996
7997 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007998 return SDValue();
7999
Chad Rosier96b66d62012-04-09 19:38:15 +00008000 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8001 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008002 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008003 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008004 SelectionDAG &DAG = DCI.DAG;
8005 DebugLoc DL = St->getDebugLoc();
8006 SDValue BasePtr = St->getBasePtr();
8007 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8008 StVal.getNode()->getOperand(0), BasePtr,
8009 St->getPointerInfo(), St->isVolatile(),
8010 St->isNonTemporal(), St->getAlignment());
8011
8012 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8013 DAG.getConstant(4, MVT::i32));
8014 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8015 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8016 St->isNonTemporal(),
8017 std::min(4U, St->getAlignment() / 2));
8018 }
8019
8020 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008021 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8022 return SDValue();
8023
Chad Rosier96b66d62012-04-09 19:38:15 +00008024 // Bitcast an i64 store extracted from a vector to f64.
8025 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008026 SelectionDAG &DAG = DCI.DAG;
8027 DebugLoc dl = StVal.getDebugLoc();
8028 SDValue IntVec = StVal.getOperand(0);
8029 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8030 IntVec.getValueType().getVectorNumElements());
8031 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8032 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8033 Vec, StVal.getOperand(1));
8034 dl = N->getDebugLoc();
8035 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8036 // Make the DAGCombiner fold the bitcasts.
8037 DCI.AddToWorklist(Vec.getNode());
8038 DCI.AddToWorklist(ExtElt.getNode());
8039 DCI.AddToWorklist(V.getNode());
8040 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8041 St->getPointerInfo(), St->isVolatile(),
8042 St->isNonTemporal(), St->getAlignment(),
8043 St->getTBAAInfo());
8044}
8045
8046/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8047/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8048/// i64 vector to have f64 elements, since the value can then be loaded
8049/// directly into a VFP register.
8050static bool hasNormalLoadOperand(SDNode *N) {
8051 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8052 for (unsigned i = 0; i < NumElts; ++i) {
8053 SDNode *Elt = N->getOperand(i).getNode();
8054 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8055 return true;
8056 }
8057 return false;
8058}
8059
Bob Wilson75f02882010-09-17 22:59:05 +00008060/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8061/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008062static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8063 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008064 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8065 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8066 // into a pair of GPRs, which is fine when the value is used as a scalar,
8067 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008068 SelectionDAG &DAG = DCI.DAG;
8069 if (N->getNumOperands() == 2) {
8070 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8071 if (RV.getNode())
8072 return RV;
8073 }
Bob Wilson75f02882010-09-17 22:59:05 +00008074
Bob Wilson31600902010-12-21 06:43:19 +00008075 // Load i64 elements as f64 values so that type legalization does not split
8076 // them up into i32 values.
8077 EVT VT = N->getValueType(0);
8078 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8079 return SDValue();
8080 DebugLoc dl = N->getDebugLoc();
8081 SmallVector<SDValue, 8> Ops;
8082 unsigned NumElts = VT.getVectorNumElements();
8083 for (unsigned i = 0; i < NumElts; ++i) {
8084 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8085 Ops.push_back(V);
8086 // Make the DAGCombiner fold the bitcast.
8087 DCI.AddToWorklist(V.getNode());
8088 }
8089 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8090 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8091 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8092}
8093
8094/// PerformInsertEltCombine - Target-specific dag combine xforms for
8095/// ISD::INSERT_VECTOR_ELT.
8096static SDValue PerformInsertEltCombine(SDNode *N,
8097 TargetLowering::DAGCombinerInfo &DCI) {
8098 // Bitcast an i64 load inserted into a vector to f64.
8099 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8100 EVT VT = N->getValueType(0);
8101 SDNode *Elt = N->getOperand(1).getNode();
8102 if (VT.getVectorElementType() != MVT::i64 ||
8103 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8104 return SDValue();
8105
8106 SelectionDAG &DAG = DCI.DAG;
8107 DebugLoc dl = N->getDebugLoc();
8108 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8109 VT.getVectorNumElements());
8110 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8111 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8112 // Make the DAGCombiner fold the bitcasts.
8113 DCI.AddToWorklist(Vec.getNode());
8114 DCI.AddToWorklist(V.getNode());
8115 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8116 Vec, V, N->getOperand(2));
8117 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008118}
8119
Bob Wilsonf20700c2010-10-27 20:38:28 +00008120/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8121/// ISD::VECTOR_SHUFFLE.
8122static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8123 // The LLVM shufflevector instruction does not require the shuffle mask
8124 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8125 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8126 // operands do not match the mask length, they are extended by concatenating
8127 // them with undef vectors. That is probably the right thing for other
8128 // targets, but for NEON it is better to concatenate two double-register
8129 // size vector operands into a single quad-register size vector. Do that
8130 // transformation here:
8131 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8132 // shuffle(concat(v1, v2), undef)
8133 SDValue Op0 = N->getOperand(0);
8134 SDValue Op1 = N->getOperand(1);
8135 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8136 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8137 Op0.getNumOperands() != 2 ||
8138 Op1.getNumOperands() != 2)
8139 return SDValue();
8140 SDValue Concat0Op1 = Op0.getOperand(1);
8141 SDValue Concat1Op1 = Op1.getOperand(1);
8142 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8143 Concat1Op1.getOpcode() != ISD::UNDEF)
8144 return SDValue();
8145 // Skip the transformation if any of the types are illegal.
8146 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8147 EVT VT = N->getValueType(0);
8148 if (!TLI.isTypeLegal(VT) ||
8149 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8150 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8151 return SDValue();
8152
8153 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8154 Op0.getOperand(0), Op1.getOperand(0));
8155 // Translate the shuffle mask.
8156 SmallVector<int, 16> NewMask;
8157 unsigned NumElts = VT.getVectorNumElements();
8158 unsigned HalfElts = NumElts/2;
8159 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8160 for (unsigned n = 0; n < NumElts; ++n) {
8161 int MaskElt = SVN->getMaskElt(n);
8162 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008163 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008164 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008165 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008166 NewElt = HalfElts + MaskElt - NumElts;
8167 NewMask.push_back(NewElt);
8168 }
8169 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8170 DAG.getUNDEF(VT), NewMask.data());
8171}
8172
Bob Wilson1c3ef902011-02-07 17:43:21 +00008173/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8174/// NEON load/store intrinsics to merge base address updates.
8175static SDValue CombineBaseUpdate(SDNode *N,
8176 TargetLowering::DAGCombinerInfo &DCI) {
8177 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8178 return SDValue();
8179
8180 SelectionDAG &DAG = DCI.DAG;
8181 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8182 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8183 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8184 SDValue Addr = N->getOperand(AddrOpIdx);
8185
8186 // Search for a use of the address operand that is an increment.
8187 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8188 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8189 SDNode *User = *UI;
8190 if (User->getOpcode() != ISD::ADD ||
8191 UI.getUse().getResNo() != Addr.getResNo())
8192 continue;
8193
8194 // Check that the add is independent of the load/store. Otherwise, folding
8195 // it would create a cycle.
8196 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8197 continue;
8198
8199 // Find the new opcode for the updating load/store.
8200 bool isLoad = true;
8201 bool isLaneOp = false;
8202 unsigned NewOpc = 0;
8203 unsigned NumVecs = 0;
8204 if (isIntrinsic) {
8205 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8206 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008207 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008208 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8209 NumVecs = 1; break;
8210 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8211 NumVecs = 2; break;
8212 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8213 NumVecs = 3; break;
8214 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8215 NumVecs = 4; break;
8216 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8217 NumVecs = 2; isLaneOp = true; break;
8218 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8219 NumVecs = 3; isLaneOp = true; break;
8220 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8221 NumVecs = 4; isLaneOp = true; break;
8222 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8223 NumVecs = 1; isLoad = false; break;
8224 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8225 NumVecs = 2; isLoad = false; break;
8226 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8227 NumVecs = 3; isLoad = false; break;
8228 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8229 NumVecs = 4; isLoad = false; break;
8230 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8231 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8232 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8233 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8234 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8235 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8236 }
8237 } else {
8238 isLaneOp = true;
8239 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008240 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008241 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8242 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8243 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8244 }
8245 }
8246
8247 // Find the size of memory referenced by the load/store.
8248 EVT VecTy;
8249 if (isLoad)
8250 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008251 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008252 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8253 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8254 if (isLaneOp)
8255 NumBytes /= VecTy.getVectorNumElements();
8256
8257 // If the increment is a constant, it must match the memory ref size.
8258 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8259 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8260 uint64_t IncVal = CInc->getZExtValue();
8261 if (IncVal != NumBytes)
8262 continue;
8263 } else if (NumBytes >= 3 * 16) {
8264 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8265 // separate instructions that make it harder to use a non-constant update.
8266 continue;
8267 }
8268
8269 // Create the new updating load/store node.
8270 EVT Tys[6];
8271 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8272 unsigned n;
8273 for (n = 0; n < NumResultVecs; ++n)
8274 Tys[n] = VecTy;
8275 Tys[n++] = MVT::i32;
8276 Tys[n] = MVT::Other;
8277 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8278 SmallVector<SDValue, 8> Ops;
8279 Ops.push_back(N->getOperand(0)); // incoming chain
8280 Ops.push_back(N->getOperand(AddrOpIdx));
8281 Ops.push_back(Inc);
8282 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8283 Ops.push_back(N->getOperand(i));
8284 }
8285 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8286 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8287 Ops.data(), Ops.size(),
8288 MemInt->getMemoryVT(),
8289 MemInt->getMemOperand());
8290
8291 // Update the uses.
8292 std::vector<SDValue> NewResults;
8293 for (unsigned i = 0; i < NumResultVecs; ++i) {
8294 NewResults.push_back(SDValue(UpdN.getNode(), i));
8295 }
8296 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8297 DCI.CombineTo(N, NewResults);
8298 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8299
8300 break;
Owen Anderson76706012011-04-05 21:48:57 +00008301 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008302 return SDValue();
8303}
8304
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008305/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8306/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8307/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8308/// return true.
8309static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8310 SelectionDAG &DAG = DCI.DAG;
8311 EVT VT = N->getValueType(0);
8312 // vldN-dup instructions only support 64-bit vectors for N > 1.
8313 if (!VT.is64BitVector())
8314 return false;
8315
8316 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8317 SDNode *VLD = N->getOperand(0).getNode();
8318 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8319 return false;
8320 unsigned NumVecs = 0;
8321 unsigned NewOpc = 0;
8322 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8323 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8324 NumVecs = 2;
8325 NewOpc = ARMISD::VLD2DUP;
8326 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8327 NumVecs = 3;
8328 NewOpc = ARMISD::VLD3DUP;
8329 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8330 NumVecs = 4;
8331 NewOpc = ARMISD::VLD4DUP;
8332 } else {
8333 return false;
8334 }
8335
8336 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8337 // numbers match the load.
8338 unsigned VLDLaneNo =
8339 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8340 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8341 UI != UE; ++UI) {
8342 // Ignore uses of the chain result.
8343 if (UI.getUse().getResNo() == NumVecs)
8344 continue;
8345 SDNode *User = *UI;
8346 if (User->getOpcode() != ARMISD::VDUPLANE ||
8347 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8348 return false;
8349 }
8350
8351 // Create the vldN-dup node.
8352 EVT Tys[5];
8353 unsigned n;
8354 for (n = 0; n < NumVecs; ++n)
8355 Tys[n] = VT;
8356 Tys[n] = MVT::Other;
8357 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8358 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8359 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8360 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8361 Ops, 2, VLDMemInt->getMemoryVT(),
8362 VLDMemInt->getMemOperand());
8363
8364 // Update the uses.
8365 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8366 UI != UE; ++UI) {
8367 unsigned ResNo = UI.getUse().getResNo();
8368 // Ignore uses of the chain result.
8369 if (ResNo == NumVecs)
8370 continue;
8371 SDNode *User = *UI;
8372 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8373 }
8374
8375 // Now the vldN-lane intrinsic is dead except for its chain result.
8376 // Update uses of the chain.
8377 std::vector<SDValue> VLDDupResults;
8378 for (unsigned n = 0; n < NumVecs; ++n)
8379 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8380 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8381 DCI.CombineTo(VLD, VLDDupResults);
8382
8383 return true;
8384}
8385
Bob Wilson9e82bf12010-07-14 01:22:12 +00008386/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8387/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008388static SDValue PerformVDUPLANECombine(SDNode *N,
8389 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008390 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008391
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008392 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8393 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8394 if (CombineVLDDUP(N, DCI))
8395 return SDValue(N, 0);
8396
8397 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8398 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008399 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008400 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008401 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008402 return SDValue();
8403
8404 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8405 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8406 // The canonical VMOV for a zero vector uses a 32-bit element size.
8407 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8408 unsigned EltBits;
8409 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8410 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008411 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008412 if (EltSize > VT.getVectorElementType().getSizeInBits())
8413 return SDValue();
8414
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008415 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008416}
8417
Eric Christopherfa6f5912011-06-29 21:10:36 +00008418// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008419// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8420static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8421{
Chad Rosier118c9a02011-06-28 17:26:57 +00008422 integerPart cN;
8423 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008424 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8425 I != E; I++) {
8426 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8427 if (!C)
8428 return false;
8429
Eric Christopherfa6f5912011-06-29 21:10:36 +00008430 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008431 APFloat APF = C->getValueAPF();
8432 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8433 != APFloat::opOK || !isExact)
8434 return false;
8435
8436 c0 = (I == 0) ? cN : c0;
8437 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8438 return false;
8439 }
8440 C = c0;
8441 return true;
8442}
8443
8444/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8445/// can replace combinations of VMUL and VCVT (floating-point to integer)
8446/// when the VMUL has a constant operand that is a power of 2.
8447///
8448/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8449/// vmul.f32 d16, d17, d16
8450/// vcvt.s32.f32 d16, d16
8451/// becomes:
8452/// vcvt.s32.f32 d16, d16, #3
8453static SDValue PerformVCVTCombine(SDNode *N,
8454 TargetLowering::DAGCombinerInfo &DCI,
8455 const ARMSubtarget *Subtarget) {
8456 SelectionDAG &DAG = DCI.DAG;
8457 SDValue Op = N->getOperand(0);
8458
8459 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8460 Op.getOpcode() != ISD::FMUL)
8461 return SDValue();
8462
8463 uint64_t C;
8464 SDValue N0 = Op->getOperand(0);
8465 SDValue ConstVec = Op->getOperand(1);
8466 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8467
Eric Christopherfa6f5912011-06-29 21:10:36 +00008468 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008469 !isConstVecPow2(ConstVec, isSigned, C))
8470 return SDValue();
8471
8472 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8473 Intrinsic::arm_neon_vcvtfp2fxu;
8474 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8475 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008476 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008477 DAG.getConstant(Log2_64(C), MVT::i32));
8478}
8479
8480/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8481/// can replace combinations of VCVT (integer to floating-point) and VDIV
8482/// when the VDIV has a constant operand that is a power of 2.
8483///
8484/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8485/// vcvt.f32.s32 d16, d16
8486/// vdiv.f32 d16, d17, d16
8487/// becomes:
8488/// vcvt.f32.s32 d16, d16, #3
8489static SDValue PerformVDIVCombine(SDNode *N,
8490 TargetLowering::DAGCombinerInfo &DCI,
8491 const ARMSubtarget *Subtarget) {
8492 SelectionDAG &DAG = DCI.DAG;
8493 SDValue Op = N->getOperand(0);
8494 unsigned OpOpcode = Op.getNode()->getOpcode();
8495
8496 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8497 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8498 return SDValue();
8499
8500 uint64_t C;
8501 SDValue ConstVec = N->getOperand(1);
8502 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8503
8504 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8505 !isConstVecPow2(ConstVec, isSigned, C))
8506 return SDValue();
8507
Eric Christopherfa6f5912011-06-29 21:10:36 +00008508 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008509 Intrinsic::arm_neon_vcvtfxu2fp;
8510 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8511 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008512 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008513 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8514}
8515
8516/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008517/// operand of a vector shift operation, where all the elements of the
8518/// build_vector must have the same constant integer value.
8519static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8520 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008521 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008522 Op = Op.getOperand(0);
8523 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8524 APInt SplatBits, SplatUndef;
8525 unsigned SplatBitSize;
8526 bool HasAnyUndefs;
8527 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8528 HasAnyUndefs, ElementBits) ||
8529 SplatBitSize > ElementBits)
8530 return false;
8531 Cnt = SplatBits.getSExtValue();
8532 return true;
8533}
8534
8535/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8536/// operand of a vector shift left operation. That value must be in the range:
8537/// 0 <= Value < ElementBits for a left shift; or
8538/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008539static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008540 assert(VT.isVector() && "vector shift count is not a vector type");
8541 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8542 if (! getVShiftImm(Op, ElementBits, Cnt))
8543 return false;
8544 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8545}
8546
8547/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8548/// operand of a vector shift right operation. For a shift opcode, the value
8549/// is positive, but for an intrinsic the value count must be negative. The
8550/// absolute value must be in the range:
8551/// 1 <= |Value| <= ElementBits for a right shift; or
8552/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008553static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008554 int64_t &Cnt) {
8555 assert(VT.isVector() && "vector shift count is not a vector type");
8556 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8557 if (! getVShiftImm(Op, ElementBits, Cnt))
8558 return false;
8559 if (isIntrinsic)
8560 Cnt = -Cnt;
8561 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8562}
8563
8564/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8565static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8566 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8567 switch (IntNo) {
8568 default:
8569 // Don't do anything for most intrinsics.
8570 break;
8571
8572 // Vector shifts: check for immediate versions and lower them.
8573 // Note: This is done during DAG combining instead of DAG legalizing because
8574 // the build_vectors for 64-bit vector element shift counts are generally
8575 // not legal, and it is hard to see their values after they get legalized to
8576 // loads from a constant pool.
8577 case Intrinsic::arm_neon_vshifts:
8578 case Intrinsic::arm_neon_vshiftu:
8579 case Intrinsic::arm_neon_vshiftls:
8580 case Intrinsic::arm_neon_vshiftlu:
8581 case Intrinsic::arm_neon_vshiftn:
8582 case Intrinsic::arm_neon_vrshifts:
8583 case Intrinsic::arm_neon_vrshiftu:
8584 case Intrinsic::arm_neon_vrshiftn:
8585 case Intrinsic::arm_neon_vqshifts:
8586 case Intrinsic::arm_neon_vqshiftu:
8587 case Intrinsic::arm_neon_vqshiftsu:
8588 case Intrinsic::arm_neon_vqshiftns:
8589 case Intrinsic::arm_neon_vqshiftnu:
8590 case Intrinsic::arm_neon_vqshiftnsu:
8591 case Intrinsic::arm_neon_vqrshiftns:
8592 case Intrinsic::arm_neon_vqrshiftnu:
8593 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008594 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008595 int64_t Cnt;
8596 unsigned VShiftOpc = 0;
8597
8598 switch (IntNo) {
8599 case Intrinsic::arm_neon_vshifts:
8600 case Intrinsic::arm_neon_vshiftu:
8601 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8602 VShiftOpc = ARMISD::VSHL;
8603 break;
8604 }
8605 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8606 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8607 ARMISD::VSHRs : ARMISD::VSHRu);
8608 break;
8609 }
8610 return SDValue();
8611
8612 case Intrinsic::arm_neon_vshiftls:
8613 case Intrinsic::arm_neon_vshiftlu:
8614 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8615 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008616 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008617
8618 case Intrinsic::arm_neon_vrshifts:
8619 case Intrinsic::arm_neon_vrshiftu:
8620 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8621 break;
8622 return SDValue();
8623
8624 case Intrinsic::arm_neon_vqshifts:
8625 case Intrinsic::arm_neon_vqshiftu:
8626 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8627 break;
8628 return SDValue();
8629
8630 case Intrinsic::arm_neon_vqshiftsu:
8631 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8632 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008633 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008634
8635 case Intrinsic::arm_neon_vshiftn:
8636 case Intrinsic::arm_neon_vrshiftn:
8637 case Intrinsic::arm_neon_vqshiftns:
8638 case Intrinsic::arm_neon_vqshiftnu:
8639 case Intrinsic::arm_neon_vqshiftnsu:
8640 case Intrinsic::arm_neon_vqrshiftns:
8641 case Intrinsic::arm_neon_vqrshiftnu:
8642 case Intrinsic::arm_neon_vqrshiftnsu:
8643 // Narrowing shifts require an immediate right shift.
8644 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8645 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008646 llvm_unreachable("invalid shift count for narrowing vector shift "
8647 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008648
8649 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008650 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008651 }
8652
8653 switch (IntNo) {
8654 case Intrinsic::arm_neon_vshifts:
8655 case Intrinsic::arm_neon_vshiftu:
8656 // Opcode already set above.
8657 break;
8658 case Intrinsic::arm_neon_vshiftls:
8659 case Intrinsic::arm_neon_vshiftlu:
8660 if (Cnt == VT.getVectorElementType().getSizeInBits())
8661 VShiftOpc = ARMISD::VSHLLi;
8662 else
8663 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8664 ARMISD::VSHLLs : ARMISD::VSHLLu);
8665 break;
8666 case Intrinsic::arm_neon_vshiftn:
8667 VShiftOpc = ARMISD::VSHRN; break;
8668 case Intrinsic::arm_neon_vrshifts:
8669 VShiftOpc = ARMISD::VRSHRs; break;
8670 case Intrinsic::arm_neon_vrshiftu:
8671 VShiftOpc = ARMISD::VRSHRu; break;
8672 case Intrinsic::arm_neon_vrshiftn:
8673 VShiftOpc = ARMISD::VRSHRN; break;
8674 case Intrinsic::arm_neon_vqshifts:
8675 VShiftOpc = ARMISD::VQSHLs; break;
8676 case Intrinsic::arm_neon_vqshiftu:
8677 VShiftOpc = ARMISD::VQSHLu; break;
8678 case Intrinsic::arm_neon_vqshiftsu:
8679 VShiftOpc = ARMISD::VQSHLsu; break;
8680 case Intrinsic::arm_neon_vqshiftns:
8681 VShiftOpc = ARMISD::VQSHRNs; break;
8682 case Intrinsic::arm_neon_vqshiftnu:
8683 VShiftOpc = ARMISD::VQSHRNu; break;
8684 case Intrinsic::arm_neon_vqshiftnsu:
8685 VShiftOpc = ARMISD::VQSHRNsu; break;
8686 case Intrinsic::arm_neon_vqrshiftns:
8687 VShiftOpc = ARMISD::VQRSHRNs; break;
8688 case Intrinsic::arm_neon_vqrshiftnu:
8689 VShiftOpc = ARMISD::VQRSHRNu; break;
8690 case Intrinsic::arm_neon_vqrshiftnsu:
8691 VShiftOpc = ARMISD::VQRSHRNsu; break;
8692 }
8693
8694 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008696 }
8697
8698 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008699 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008700 int64_t Cnt;
8701 unsigned VShiftOpc = 0;
8702
8703 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8704 VShiftOpc = ARMISD::VSLI;
8705 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8706 VShiftOpc = ARMISD::VSRI;
8707 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008708 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008709 }
8710
8711 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8712 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008713 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008714 }
8715
8716 case Intrinsic::arm_neon_vqrshifts:
8717 case Intrinsic::arm_neon_vqrshiftu:
8718 // No immediate versions of these to check for.
8719 break;
8720 }
8721
8722 return SDValue();
8723}
8724
8725/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8726/// lowers them. As with the vector shift intrinsics, this is done during DAG
8727/// combining instead of DAG legalizing because the build_vectors for 64-bit
8728/// vector element shift counts are generally not legal, and it is hard to see
8729/// their values after they get legalized to loads from a constant pool.
8730static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8731 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008732 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008733 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8734 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8735 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8736 SDValue N1 = N->getOperand(1);
8737 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8738 SDValue N0 = N->getOperand(0);
8739 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8740 DAG.MaskedValueIsZero(N0.getOperand(0),
8741 APInt::getHighBitsSet(32, 16)))
8742 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8743 }
8744 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008745
8746 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008747 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8748 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008749 return SDValue();
8750
8751 assert(ST->hasNEON() && "unexpected vector shift");
8752 int64_t Cnt;
8753
8754 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008755 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008756
8757 case ISD::SHL:
8758 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8759 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008760 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008761 break;
8762
8763 case ISD::SRA:
8764 case ISD::SRL:
8765 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8766 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8767 ARMISD::VSHRs : ARMISD::VSHRu);
8768 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008770 }
8771 }
8772 return SDValue();
8773}
8774
8775/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8776/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8777static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8778 const ARMSubtarget *ST) {
8779 SDValue N0 = N->getOperand(0);
8780
8781 // Check for sign- and zero-extensions of vector extract operations of 8-
8782 // and 16-bit vector elements. NEON supports these directly. They are
8783 // handled during DAG combining because type legalization will promote them
8784 // to 32-bit types and it is messy to recognize the operations after that.
8785 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8786 SDValue Vec = N0.getOperand(0);
8787 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008788 EVT VT = N->getValueType(0);
8789 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008790 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8791
Owen Anderson825b72b2009-08-11 20:47:22 +00008792 if (VT == MVT::i32 &&
8793 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008794 TLI.isTypeLegal(Vec.getValueType()) &&
8795 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008796
8797 unsigned Opc = 0;
8798 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008799 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008800 case ISD::SIGN_EXTEND:
8801 Opc = ARMISD::VGETLANEs;
8802 break;
8803 case ISD::ZERO_EXTEND:
8804 case ISD::ANY_EXTEND:
8805 Opc = ARMISD::VGETLANEu;
8806 break;
8807 }
8808 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8809 }
8810 }
8811
8812 return SDValue();
8813}
8814
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008815/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8816/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8817static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8818 const ARMSubtarget *ST) {
8819 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008820 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008821 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8822 // a NaN; only do the transformation when it matches that behavior.
8823
8824 // For now only do this when using NEON for FP operations; if using VFP, it
8825 // is not obvious that the benefit outweighs the cost of switching to the
8826 // NEON pipeline.
8827 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8828 N->getValueType(0) != MVT::f32)
8829 return SDValue();
8830
8831 SDValue CondLHS = N->getOperand(0);
8832 SDValue CondRHS = N->getOperand(1);
8833 SDValue LHS = N->getOperand(2);
8834 SDValue RHS = N->getOperand(3);
8835 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8836
8837 unsigned Opcode = 0;
8838 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008839 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008840 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008841 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008842 IsReversed = true ; // x CC y ? y : x
8843 } else {
8844 return SDValue();
8845 }
8846
Bob Wilsone742bb52010-02-24 22:15:53 +00008847 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008848 switch (CC) {
8849 default: break;
8850 case ISD::SETOLT:
8851 case ISD::SETOLE:
8852 case ISD::SETLT:
8853 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008854 case ISD::SETULT:
8855 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008856 // If LHS is NaN, an ordered comparison will be false and the result will
8857 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8858 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8859 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8860 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8861 break;
8862 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8863 // will return -0, so vmin can only be used for unsafe math or if one of
8864 // the operands is known to be nonzero.
8865 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008866 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008867 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8868 break;
8869 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008870 break;
8871
8872 case ISD::SETOGT:
8873 case ISD::SETOGE:
8874 case ISD::SETGT:
8875 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008876 case ISD::SETUGT:
8877 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008878 // If LHS is NaN, an ordered comparison will be false and the result will
8879 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8880 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8881 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8882 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8883 break;
8884 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8885 // will return +0, so vmax can only be used for unsafe math or if one of
8886 // the operands is known to be nonzero.
8887 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008888 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008889 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8890 break;
8891 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008892 break;
8893 }
8894
8895 if (!Opcode)
8896 return SDValue();
8897 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8898}
8899
Evan Chenge721f5c2011-07-13 00:42:17 +00008900/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8901SDValue
8902ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8903 SDValue Cmp = N->getOperand(4);
8904 if (Cmp.getOpcode() != ARMISD::CMPZ)
8905 // Only looking at EQ and NE cases.
8906 return SDValue();
8907
8908 EVT VT = N->getValueType(0);
8909 DebugLoc dl = N->getDebugLoc();
8910 SDValue LHS = Cmp.getOperand(0);
8911 SDValue RHS = Cmp.getOperand(1);
8912 SDValue FalseVal = N->getOperand(0);
8913 SDValue TrueVal = N->getOperand(1);
8914 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008915 ARMCC::CondCodes CC =
8916 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008917
8918 // Simplify
8919 // mov r1, r0
8920 // cmp r1, x
8921 // mov r0, y
8922 // moveq r0, x
8923 // to
8924 // cmp r0, x
8925 // movne r0, y
8926 //
8927 // mov r1, r0
8928 // cmp r1, x
8929 // mov r0, x
8930 // movne r0, y
8931 // to
8932 // cmp r0, x
8933 // movne r0, y
8934 /// FIXME: Turn this into a target neutral optimization?
8935 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008936 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008937 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8938 N->getOperand(3), Cmp);
8939 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8940 SDValue ARMcc;
8941 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8942 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8943 N->getOperand(3), NewCmp);
8944 }
8945
8946 if (Res.getNode()) {
8947 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008948 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008949 // Capture demanded bits information that would be otherwise lost.
8950 if (KnownZero == 0xfffffffe)
8951 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8952 DAG.getValueType(MVT::i1));
8953 else if (KnownZero == 0xffffff00)
8954 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8955 DAG.getValueType(MVT::i8));
8956 else if (KnownZero == 0xffff0000)
8957 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8958 DAG.getValueType(MVT::i16));
8959 }
8960
8961 return Res;
8962}
8963
Dan Gohman475871a2008-07-27 21:46:04 +00008964SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008965 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008966 switch (N->getOpcode()) {
8967 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00008968 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00008969 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008970 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008971 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008972 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008973 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8974 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008975 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008976 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008977 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008978 case ISD::STORE: return PerformSTORECombine(N, DCI);
8979 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8980 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008981 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008982 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008983 case ISD::FP_TO_SINT:
8984 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8985 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008986 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008987 case ISD::SHL:
8988 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008989 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008990 case ISD::SIGN_EXTEND:
8991 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008992 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8993 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008994 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008995 case ARMISD::VLD2DUP:
8996 case ARMISD::VLD3DUP:
8997 case ARMISD::VLD4DUP:
8998 return CombineBaseUpdate(N, DCI);
8999 case ISD::INTRINSIC_VOID:
9000 case ISD::INTRINSIC_W_CHAIN:
9001 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9002 case Intrinsic::arm_neon_vld1:
9003 case Intrinsic::arm_neon_vld2:
9004 case Intrinsic::arm_neon_vld3:
9005 case Intrinsic::arm_neon_vld4:
9006 case Intrinsic::arm_neon_vld2lane:
9007 case Intrinsic::arm_neon_vld3lane:
9008 case Intrinsic::arm_neon_vld4lane:
9009 case Intrinsic::arm_neon_vst1:
9010 case Intrinsic::arm_neon_vst2:
9011 case Intrinsic::arm_neon_vst3:
9012 case Intrinsic::arm_neon_vst4:
9013 case Intrinsic::arm_neon_vst2lane:
9014 case Intrinsic::arm_neon_vst3lane:
9015 case Intrinsic::arm_neon_vst4lane:
9016 return CombineBaseUpdate(N, DCI);
9017 default: break;
9018 }
9019 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009020 }
Dan Gohman475871a2008-07-27 21:46:04 +00009021 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009022}
9023
Evan Cheng31959b12011-02-02 01:06:55 +00009024bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9025 EVT VT) const {
9026 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9027}
9028
Bill Wendlingaf566342009-08-15 21:21:19 +00009029bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00009030 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00009031 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00009032
9033 switch (VT.getSimpleVT().SimpleTy) {
9034 default:
9035 return false;
9036 case MVT::i8:
9037 case MVT::i16:
9038 case MVT::i32:
9039 return true;
Evan Chenga99c5082012-08-15 17:44:53 +00009040 case MVT::f64:
9041 return Subtarget->hasNEON();
Bill Wendlingaf566342009-08-15 21:21:19 +00009042 // FIXME: VLD1 etc with standard alignment is legal.
9043 }
9044}
9045
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009046static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9047 unsigned AlignCheck) {
9048 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9049 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9050}
9051
9052EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9053 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00009054 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009055 bool MemcpyStrSrc,
9056 MachineFunction &MF) const {
9057 const Function *F = MF.getFunction();
9058
9059 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00009060 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009061 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
9062 Subtarget->hasNEON()) {
9063 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
9064 return MVT::v4i32;
9065 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
9066 return MVT::v2i32;
9067 }
9068 }
9069
Lang Hames5207bf22011-11-08 18:56:23 +00009070 // Lowering to i32/i16 if the size permits.
9071 if (Size >= 4) {
9072 return MVT::i32;
9073 } else if (Size >= 2) {
9074 return MVT::i16;
9075 }
9076
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009077 // Let the target-independent logic figure it out.
9078 return MVT::Other;
9079}
9080
Evan Chenge6c835f2009-08-14 20:09:37 +00009081static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9082 if (V < 0)
9083 return false;
9084
9085 unsigned Scale = 1;
9086 switch (VT.getSimpleVT().SimpleTy) {
9087 default: return false;
9088 case MVT::i1:
9089 case MVT::i8:
9090 // Scale == 1;
9091 break;
9092 case MVT::i16:
9093 // Scale == 2;
9094 Scale = 2;
9095 break;
9096 case MVT::i32:
9097 // Scale == 4;
9098 Scale = 4;
9099 break;
9100 }
9101
9102 if ((V & (Scale - 1)) != 0)
9103 return false;
9104 V /= Scale;
9105 return V == (V & ((1LL << 5) - 1));
9106}
9107
9108static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9109 const ARMSubtarget *Subtarget) {
9110 bool isNeg = false;
9111 if (V < 0) {
9112 isNeg = true;
9113 V = - V;
9114 }
9115
9116 switch (VT.getSimpleVT().SimpleTy) {
9117 default: return false;
9118 case MVT::i1:
9119 case MVT::i8:
9120 case MVT::i16:
9121 case MVT::i32:
9122 // + imm12 or - imm8
9123 if (isNeg)
9124 return V == (V & ((1LL << 8) - 1));
9125 return V == (V & ((1LL << 12) - 1));
9126 case MVT::f32:
9127 case MVT::f64:
9128 // Same as ARM mode. FIXME: NEON?
9129 if (!Subtarget->hasVFP2())
9130 return false;
9131 if ((V & 3) != 0)
9132 return false;
9133 V >>= 2;
9134 return V == (V & ((1LL << 8) - 1));
9135 }
9136}
9137
Evan Chengb01fad62007-03-12 23:30:29 +00009138/// isLegalAddressImmediate - Return true if the integer value can be used
9139/// as the offset of the target addressing mode for load / store of the
9140/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009141static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009142 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009143 if (V == 0)
9144 return true;
9145
Evan Cheng65011532009-03-09 19:15:00 +00009146 if (!VT.isSimple())
9147 return false;
9148
Evan Chenge6c835f2009-08-14 20:09:37 +00009149 if (Subtarget->isThumb1Only())
9150 return isLegalT1AddressImmediate(V, VT);
9151 else if (Subtarget->isThumb2())
9152 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009153
Evan Chenge6c835f2009-08-14 20:09:37 +00009154 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009155 if (V < 0)
9156 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009158 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009159 case MVT::i1:
9160 case MVT::i8:
9161 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009162 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009163 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009164 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009165 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009166 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009167 case MVT::f32:
9168 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009169 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009170 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009171 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009172 return false;
9173 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009174 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009175 }
Evan Chenga8e29892007-01-19 07:51:42 +00009176}
9177
Evan Chenge6c835f2009-08-14 20:09:37 +00009178bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9179 EVT VT) const {
9180 int Scale = AM.Scale;
9181 if (Scale < 0)
9182 return false;
9183
9184 switch (VT.getSimpleVT().SimpleTy) {
9185 default: return false;
9186 case MVT::i1:
9187 case MVT::i8:
9188 case MVT::i16:
9189 case MVT::i32:
9190 if (Scale == 1)
9191 return true;
9192 // r + r << imm
9193 Scale = Scale & ~1;
9194 return Scale == 2 || Scale == 4 || Scale == 8;
9195 case MVT::i64:
9196 // r + r
9197 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9198 return true;
9199 return false;
9200 case MVT::isVoid:
9201 // Note, we allow "void" uses (basically, uses that aren't loads or
9202 // stores), because arm allows folding a scale into many arithmetic
9203 // operations. This should be made more precise and revisited later.
9204
9205 // Allow r << imm, but the imm has to be a multiple of two.
9206 if (Scale & 1) return false;
9207 return isPowerOf2_32(Scale);
9208 }
9209}
9210
Chris Lattner37caf8c2007-04-09 23:33:39 +00009211/// isLegalAddressingMode - Return true if the addressing mode represented
9212/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009213bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009214 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009215 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009216 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009217 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009218
Chris Lattner37caf8c2007-04-09 23:33:39 +00009219 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009220 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009221 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009222
Chris Lattner37caf8c2007-04-09 23:33:39 +00009223 switch (AM.Scale) {
9224 case 0: // no scale reg, must be "r+i" or "r", or "i".
9225 break;
9226 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009227 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009228 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009229 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009230 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009231 // ARM doesn't support any R+R*scale+imm addr modes.
9232 if (AM.BaseOffs)
9233 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009234
Bob Wilson2c7dab12009-04-08 17:55:28 +00009235 if (!VT.isSimple())
9236 return false;
9237
Evan Chenge6c835f2009-08-14 20:09:37 +00009238 if (Subtarget->isThumb2())
9239 return isLegalT2ScaledAddressingMode(AM, VT);
9240
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009241 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009242 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009243 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009244 case MVT::i1:
9245 case MVT::i8:
9246 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009247 if (Scale < 0) Scale = -Scale;
9248 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009249 return true;
9250 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009251 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009252 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009253 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009254 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009255 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009256 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009257 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009258
Owen Anderson825b72b2009-08-11 20:47:22 +00009259 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009260 // Note, we allow "void" uses (basically, uses that aren't loads or
9261 // stores), because arm allows folding a scale into many arithmetic
9262 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009263
Chris Lattner37caf8c2007-04-09 23:33:39 +00009264 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009265 if (Scale & 1) return false;
9266 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009267 }
Evan Chengb01fad62007-03-12 23:30:29 +00009268 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009269 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009270}
9271
Evan Cheng77e47512009-11-11 19:05:52 +00009272/// isLegalICmpImmediate - Return true if the specified immediate is legal
9273/// icmp immediate, that is the target has icmp instructions which can compare
9274/// a register against the immediate without having to materialize the
9275/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009276bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009277 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009278 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009279 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009280 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009281 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009282 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009283 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009284}
9285
Andrew Trick8d8d9612012-07-18 18:34:27 +00009286/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9287/// *or sub* immediate, that is the target has add or sub instructions which can
9288/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009289/// immediate into a register.
9290bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009291 // Same encoding for add/sub, just flip the sign.
9292 int64_t AbsImm = llvm::abs64(Imm);
9293 if (!Subtarget->isThumb())
9294 return ARM_AM::getSOImmVal(AbsImm) != -1;
9295 if (Subtarget->isThumb2())
9296 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9297 // Thumb1 only has 8-bit unsigned immediate.
9298 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009299}
9300
Owen Andersone50ed302009-08-10 22:56:29 +00009301static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009302 bool isSEXTLoad, SDValue &Base,
9303 SDValue &Offset, bool &isInc,
9304 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009305 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9306 return false;
9307
Owen Anderson825b72b2009-08-11 20:47:22 +00009308 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009309 // AddressingMode 3
9310 Base = Ptr->getOperand(0);
9311 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009312 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009313 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009314 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009315 isInc = false;
9316 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9317 return true;
9318 }
9319 }
9320 isInc = (Ptr->getOpcode() == ISD::ADD);
9321 Offset = Ptr->getOperand(1);
9322 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009323 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009324 // AddressingMode 2
9325 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009326 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009327 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009328 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009329 isInc = false;
9330 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9331 Base = Ptr->getOperand(0);
9332 return true;
9333 }
9334 }
9335
9336 if (Ptr->getOpcode() == ISD::ADD) {
9337 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009338 ARM_AM::ShiftOpc ShOpcVal=
9339 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009340 if (ShOpcVal != ARM_AM::no_shift) {
9341 Base = Ptr->getOperand(1);
9342 Offset = Ptr->getOperand(0);
9343 } else {
9344 Base = Ptr->getOperand(0);
9345 Offset = Ptr->getOperand(1);
9346 }
9347 return true;
9348 }
9349
9350 isInc = (Ptr->getOpcode() == ISD::ADD);
9351 Base = Ptr->getOperand(0);
9352 Offset = Ptr->getOperand(1);
9353 return true;
9354 }
9355
Jim Grosbache5165492009-11-09 00:11:35 +00009356 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009357 return false;
9358}
9359
Owen Andersone50ed302009-08-10 22:56:29 +00009360static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009361 bool isSEXTLoad, SDValue &Base,
9362 SDValue &Offset, bool &isInc,
9363 SelectionDAG &DAG) {
9364 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9365 return false;
9366
9367 Base = Ptr->getOperand(0);
9368 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9369 int RHSC = (int)RHS->getZExtValue();
9370 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9371 assert(Ptr->getOpcode() == ISD::ADD);
9372 isInc = false;
9373 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9374 return true;
9375 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9376 isInc = Ptr->getOpcode() == ISD::ADD;
9377 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9378 return true;
9379 }
9380 }
9381
9382 return false;
9383}
9384
Evan Chenga8e29892007-01-19 07:51:42 +00009385/// getPreIndexedAddressParts - returns true by value, base pointer and
9386/// offset pointer and addressing mode by reference if the node's address
9387/// can be legally represented as pre-indexed load / store address.
9388bool
Dan Gohman475871a2008-07-27 21:46:04 +00009389ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9390 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009391 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009392 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009393 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009394 return false;
9395
Owen Andersone50ed302009-08-10 22:56:29 +00009396 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009397 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009398 bool isSEXTLoad = false;
9399 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9400 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009401 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009402 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9403 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9404 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009405 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009406 } else
9407 return false;
9408
9409 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009410 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009411 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009412 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9413 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009414 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009415 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009416 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009417 if (!isLegal)
9418 return false;
9419
9420 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9421 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009422}
9423
9424/// getPostIndexedAddressParts - returns true by value, base pointer and
9425/// offset pointer and addressing mode by reference if this node can be
9426/// combined with a load / store to form a post-indexed load / store.
9427bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009428 SDValue &Base,
9429 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009430 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009431 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009432 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009433 return false;
9434
Owen Andersone50ed302009-08-10 22:56:29 +00009435 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009436 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009437 bool isSEXTLoad = false;
9438 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009439 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009440 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009441 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9442 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009443 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009444 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009445 } else
9446 return false;
9447
9448 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009449 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009450 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009451 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009452 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009453 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009454 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9455 isInc, DAG);
9456 if (!isLegal)
9457 return false;
9458
Evan Cheng28dad2a2010-05-18 21:31:17 +00009459 if (Ptr != Base) {
9460 // Swap base ptr and offset to catch more post-index load / store when
9461 // it's legal. In Thumb2 mode, offset must be an immediate.
9462 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9463 !Subtarget->isThumb2())
9464 std::swap(Base, Offset);
9465
9466 // Post-indexed load / store update the base pointer.
9467 if (Ptr != Base)
9468 return false;
9469 }
9470
Evan Chenge88d5ce2009-07-02 07:28:31 +00009471 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9472 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009473}
9474
Dan Gohman475871a2008-07-27 21:46:04 +00009475void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009476 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009477 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009478 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009479 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009480 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009481 switch (Op.getOpcode()) {
9482 default: break;
9483 case ARMISD::CMOV: {
9484 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009485 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009486 if (KnownZero == 0 && KnownOne == 0) return;
9487
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009488 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009489 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009490 KnownZero &= KnownZeroRHS;
9491 KnownOne &= KnownOneRHS;
9492 return;
9493 }
9494 }
9495}
9496
9497//===----------------------------------------------------------------------===//
9498// ARM Inline Assembly Support
9499//===----------------------------------------------------------------------===//
9500
Evan Cheng55d42002011-01-08 01:24:27 +00009501bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9502 // Looking for "rev" which is V6+.
9503 if (!Subtarget->hasV6Ops())
9504 return false;
9505
9506 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9507 std::string AsmStr = IA->getAsmString();
9508 SmallVector<StringRef, 4> AsmPieces;
9509 SplitString(AsmStr, AsmPieces, ";\n");
9510
9511 switch (AsmPieces.size()) {
9512 default: return false;
9513 case 1:
9514 AsmStr = AsmPieces[0];
9515 AsmPieces.clear();
9516 SplitString(AsmStr, AsmPieces, " \t,");
9517
9518 // rev $0, $1
9519 if (AsmPieces.size() == 3 &&
9520 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9521 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009522 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009523 if (Ty && Ty->getBitWidth() == 32)
9524 return IntrinsicLowering::LowerToByteSwap(CI);
9525 }
9526 break;
9527 }
9528
9529 return false;
9530}
9531
Evan Chenga8e29892007-01-19 07:51:42 +00009532/// getConstraintType - Given a constraint letter, return the type of
9533/// constraint it is for this target.
9534ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009535ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9536 if (Constraint.size() == 1) {
9537 switch (Constraint[0]) {
9538 default: break;
9539 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009540 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009541 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009542 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009543 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009544 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009545 // An address with a single base register. Due to the way we
9546 // currently handle addresses it is the same as an 'r' memory constraint.
9547 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009548 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009549 } else if (Constraint.size() == 2) {
9550 switch (Constraint[0]) {
9551 default: break;
9552 // All 'U+' constraints are addresses.
9553 case 'U': return C_Memory;
9554 }
Evan Chenga8e29892007-01-19 07:51:42 +00009555 }
Chris Lattner4234f572007-03-25 02:14:49 +00009556 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009557}
9558
John Thompson44ab89e2010-10-29 17:29:13 +00009559/// Examine constraint type and operand type and determine a weight value.
9560/// This object must already have been set up with the operand type
9561/// and the current alternative constraint selected.
9562TargetLowering::ConstraintWeight
9563ARMTargetLowering::getSingleConstraintMatchWeight(
9564 AsmOperandInfo &info, const char *constraint) const {
9565 ConstraintWeight weight = CW_Invalid;
9566 Value *CallOperandVal = info.CallOperandVal;
9567 // If we don't have a value, we can't do a match,
9568 // but allow it at the lowest weight.
9569 if (CallOperandVal == NULL)
9570 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009571 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009572 // Look at the constraint type.
9573 switch (*constraint) {
9574 default:
9575 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9576 break;
9577 case 'l':
9578 if (type->isIntegerTy()) {
9579 if (Subtarget->isThumb())
9580 weight = CW_SpecificReg;
9581 else
9582 weight = CW_Register;
9583 }
9584 break;
9585 case 'w':
9586 if (type->isFloatingPointTy())
9587 weight = CW_Register;
9588 break;
9589 }
9590 return weight;
9591}
9592
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009593typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9594RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009595ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009596 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009597 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009598 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009599 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009600 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009601 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009602 return RCPair(0U, &ARM::tGPRRegClass);
9603 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009604 case 'h': // High regs or no regs.
9605 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009606 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009607 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009608 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009609 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009610 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009611 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009612 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009613 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009614 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009615 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009616 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009617 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009618 case 'x':
9619 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009620 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009621 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009622 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009623 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009624 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009625 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009626 case 't':
9627 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009628 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009629 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009630 }
9631 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009632 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009633 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009634
Evan Chenga8e29892007-01-19 07:51:42 +00009635 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9636}
9637
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009638/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9639/// vector. If it is invalid, don't add anything to Ops.
9640void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009641 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009642 std::vector<SDValue>&Ops,
9643 SelectionDAG &DAG) const {
9644 SDValue Result(0, 0);
9645
Eric Christopher100c8332011-06-02 23:16:42 +00009646 // Currently only support length 1 constraints.
9647 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009648
Eric Christopher100c8332011-06-02 23:16:42 +00009649 char ConstraintLetter = Constraint[0];
9650 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009651 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009652 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009653 case 'I': case 'J': case 'K': case 'L':
9654 case 'M': case 'N': case 'O':
9655 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9656 if (!C)
9657 return;
9658
9659 int64_t CVal64 = C->getSExtValue();
9660 int CVal = (int) CVal64;
9661 // None of these constraints allow values larger than 32 bits. Check
9662 // that the value fits in an int.
9663 if (CVal != CVal64)
9664 return;
9665
Eric Christopher100c8332011-06-02 23:16:42 +00009666 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009667 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009668 // Constant suitable for movw, must be between 0 and
9669 // 65535.
9670 if (Subtarget->hasV6T2Ops())
9671 if (CVal >= 0 && CVal <= 65535)
9672 break;
9673 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009674 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009675 if (Subtarget->isThumb1Only()) {
9676 // This must be a constant between 0 and 255, for ADD
9677 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009678 if (CVal >= 0 && CVal <= 255)
9679 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009680 } else if (Subtarget->isThumb2()) {
9681 // A constant that can be used as an immediate value in a
9682 // data-processing instruction.
9683 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9684 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009685 } else {
9686 // A constant that can be used as an immediate value in a
9687 // data-processing instruction.
9688 if (ARM_AM::getSOImmVal(CVal) != -1)
9689 break;
9690 }
9691 return;
9692
9693 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009694 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009695 // This must be a constant between -255 and -1, for negated ADD
9696 // immediates. This can be used in GCC with an "n" modifier that
9697 // prints the negated value, for use with SUB instructions. It is
9698 // not useful otherwise but is implemented for compatibility.
9699 if (CVal >= -255 && CVal <= -1)
9700 break;
9701 } else {
9702 // This must be a constant between -4095 and 4095. It is not clear
9703 // what this constraint is intended for. Implemented for
9704 // compatibility with GCC.
9705 if (CVal >= -4095 && CVal <= 4095)
9706 break;
9707 }
9708 return;
9709
9710 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009711 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009712 // A 32-bit value where only one byte has a nonzero value. Exclude
9713 // zero to match GCC. This constraint is used by GCC internally for
9714 // constants that can be loaded with a move/shift combination.
9715 // It is not useful otherwise but is implemented for compatibility.
9716 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9717 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009718 } else if (Subtarget->isThumb2()) {
9719 // A constant whose bitwise inverse can be used as an immediate
9720 // value in a data-processing instruction. This can be used in GCC
9721 // with a "B" modifier that prints the inverted value, for use with
9722 // BIC and MVN instructions. It is not useful otherwise but is
9723 // implemented for compatibility.
9724 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9725 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009726 } else {
9727 // A constant whose bitwise inverse can be used as an immediate
9728 // value in a data-processing instruction. This can be used in GCC
9729 // with a "B" modifier that prints the inverted value, for use with
9730 // BIC and MVN instructions. It is not useful otherwise but is
9731 // implemented for compatibility.
9732 if (ARM_AM::getSOImmVal(~CVal) != -1)
9733 break;
9734 }
9735 return;
9736
9737 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009738 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009739 // This must be a constant between -7 and 7,
9740 // for 3-operand ADD/SUB immediate instructions.
9741 if (CVal >= -7 && CVal < 7)
9742 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009743 } else if (Subtarget->isThumb2()) {
9744 // A constant whose negation can be used as an immediate value in a
9745 // data-processing instruction. This can be used in GCC with an "n"
9746 // modifier that prints the negated value, for use with SUB
9747 // instructions. It is not useful otherwise but is implemented for
9748 // compatibility.
9749 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9750 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009751 } else {
9752 // A constant whose negation can be used as an immediate value in a
9753 // data-processing instruction. This can be used in GCC with an "n"
9754 // modifier that prints the negated value, for use with SUB
9755 // instructions. It is not useful otherwise but is implemented for
9756 // compatibility.
9757 if (ARM_AM::getSOImmVal(-CVal) != -1)
9758 break;
9759 }
9760 return;
9761
9762 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009763 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009764 // This must be a multiple of 4 between 0 and 1020, for
9765 // ADD sp + immediate.
9766 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9767 break;
9768 } else {
9769 // A power of two or a constant between 0 and 32. This is used in
9770 // GCC for the shift amount on shifted register operands, but it is
9771 // useful in general for any shift amounts.
9772 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9773 break;
9774 }
9775 return;
9776
9777 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009778 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009779 // This must be a constant between 0 and 31, for shift amounts.
9780 if (CVal >= 0 && CVal <= 31)
9781 break;
9782 }
9783 return;
9784
9785 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009786 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009787 // This must be a multiple of 4 between -508 and 508, for
9788 // ADD/SUB sp = sp + immediate.
9789 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9790 break;
9791 }
9792 return;
9793 }
9794 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9795 break;
9796 }
9797
9798 if (Result.getNode()) {
9799 Ops.push_back(Result);
9800 return;
9801 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009802 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009803}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009804
9805bool
9806ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9807 // The ARM target isn't yet aware of offsets.
9808 return false;
9809}
Evan Cheng39382422009-10-28 01:44:26 +00009810
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009811bool ARM::isBitFieldInvertedMask(unsigned v) {
9812 if (v == 0xffffffff)
9813 return 0;
9814 // there can be 1's on either or both "outsides", all the "inside"
9815 // bits must be 0's
9816 unsigned int lsb = 0, msb = 31;
9817 while (v & (1 << msb)) --msb;
9818 while (v & (1 << lsb)) ++lsb;
9819 for (unsigned int i = lsb; i <= msb; ++i) {
9820 if (v & (1 << i))
9821 return 0;
9822 }
9823 return 1;
9824}
9825
Evan Cheng39382422009-10-28 01:44:26 +00009826/// isFPImmLegal - Returns true if the target can instruction select the
9827/// specified FP immediate natively. If false, the legalizer will
9828/// materialize the FP immediate as a load from a constant pool.
9829bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9830 if (!Subtarget->hasVFP3())
9831 return false;
9832 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009833 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009834 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009835 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009836 return false;
9837}
Bob Wilson65ffec42010-09-21 17:56:22 +00009838
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009839/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009840/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9841/// specified in the intrinsic calls.
9842bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9843 const CallInst &I,
9844 unsigned Intrinsic) const {
9845 switch (Intrinsic) {
9846 case Intrinsic::arm_neon_vld1:
9847 case Intrinsic::arm_neon_vld2:
9848 case Intrinsic::arm_neon_vld3:
9849 case Intrinsic::arm_neon_vld4:
9850 case Intrinsic::arm_neon_vld2lane:
9851 case Intrinsic::arm_neon_vld3lane:
9852 case Intrinsic::arm_neon_vld4lane: {
9853 Info.opc = ISD::INTRINSIC_W_CHAIN;
9854 // Conservatively set memVT to the entire set of vectors loaded.
9855 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9856 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9857 Info.ptrVal = I.getArgOperand(0);
9858 Info.offset = 0;
9859 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9860 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9861 Info.vol = false; // volatile loads with NEON intrinsics not supported
9862 Info.readMem = true;
9863 Info.writeMem = false;
9864 return true;
9865 }
9866 case Intrinsic::arm_neon_vst1:
9867 case Intrinsic::arm_neon_vst2:
9868 case Intrinsic::arm_neon_vst3:
9869 case Intrinsic::arm_neon_vst4:
9870 case Intrinsic::arm_neon_vst2lane:
9871 case Intrinsic::arm_neon_vst3lane:
9872 case Intrinsic::arm_neon_vst4lane: {
9873 Info.opc = ISD::INTRINSIC_VOID;
9874 // Conservatively set memVT to the entire set of vectors stored.
9875 unsigned NumElts = 0;
9876 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009877 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009878 if (!ArgTy->isVectorTy())
9879 break;
9880 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9881 }
9882 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9883 Info.ptrVal = I.getArgOperand(0);
9884 Info.offset = 0;
9885 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9886 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9887 Info.vol = false; // volatile stores with NEON intrinsics not supported
9888 Info.readMem = false;
9889 Info.writeMem = true;
9890 return true;
9891 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009892 case Intrinsic::arm_strexd: {
9893 Info.opc = ISD::INTRINSIC_W_CHAIN;
9894 Info.memVT = MVT::i64;
9895 Info.ptrVal = I.getArgOperand(2);
9896 Info.offset = 0;
9897 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009898 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009899 Info.readMem = false;
9900 Info.writeMem = true;
9901 return true;
9902 }
9903 case Intrinsic::arm_ldrexd: {
9904 Info.opc = ISD::INTRINSIC_W_CHAIN;
9905 Info.memVT = MVT::i64;
9906 Info.ptrVal = I.getArgOperand(0);
9907 Info.offset = 0;
9908 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009909 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009910 Info.readMem = true;
9911 Info.writeMem = false;
9912 return true;
9913 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009914 default:
9915 break;
9916 }
9917
9918 return false;
9919}