blob: 29ca8eace32c5c8f4fe8dab143fa6451ff583bda [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);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000799 setTargetDAGCombine(ISD::AND);
800 setTargetDAGCombine(ISD::OR);
801 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000802
Evan Cheng5fb468a2012-02-23 02:58:19 +0000803 if (Subtarget->hasV6Ops())
804 setTargetDAGCombine(ISD::SRL);
805
Evan Chenga8e29892007-01-19 07:51:42 +0000806 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000807
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000808 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
809 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000810 setSchedulingPreference(Sched::RegPressure);
811 else
812 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000813
Evan Cheng05219282011-01-06 06:52:41 +0000814 //// temporary - rewrite interface to use type
815 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000816 maxStoresPerMemset = 16;
817 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000818
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000819 // On ARM arguments smaller than 4 bytes are extended, so all arguments
820 // are at least 4 bytes aligned.
821 setMinStackArgumentAlignment(4);
822
Evan Chengfff606d2010-09-24 19:07:23 +0000823 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000824
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000825 // Prefer likely predicted branches to selects on out-of-order cores.
826 predictableSelectIsExpensive = Subtarget->isCortexA9();
827
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000828 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000829}
830
Andrew Trick32cec0a2011-01-19 02:35:27 +0000831// FIXME: It might make sense to define the representative register class as the
832// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
833// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
834// SPR's representative would be DPR_VFP2. This should work well if register
835// pressure tracking were modified such that a register use would increment the
836// pressure of the register class's representative and all of it's super
837// classes' representatives transitively. We have not implemented this because
838// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000839// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000840// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000841std::pair<const TargetRegisterClass*, uint8_t>
842ARMTargetLowering::findRepresentativeClass(EVT VT) const{
843 const TargetRegisterClass *RRC = 0;
844 uint8_t Cost = 1;
845 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000846 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000847 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000848 // Use DPR as representative register class for all floating point
849 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
850 // the cost is 1 for both f32 and f64.
851 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000852 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000853 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000854 // When NEON is used for SP, only half of the register file is available
855 // because operations that define both SP and DP results will be constrained
856 // to the VFP2 class (D0-D15). We currently model this constraint prior to
857 // coalescing by double-counting the SP regs. See the FIXME above.
858 if (Subtarget->useNEONForSinglePrecisionFP())
859 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000860 break;
861 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
862 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000863 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000864 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000865 break;
866 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000867 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000868 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000869 break;
870 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000871 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000872 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000874 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000875 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000876}
877
Evan Chenga8e29892007-01-19 07:51:42 +0000878const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
879 switch (Opcode) {
880 default: return 0;
881 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000882 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000883 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000884 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
885 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000886 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000887 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
888 case ARMISD::tCALL: return "ARMISD::tCALL";
889 case ARMISD::BRCOND: return "ARMISD::BRCOND";
890 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000891 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000892 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
893 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
894 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000895 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000896 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000897 case ARMISD::CMPFP: return "ARMISD::CMPFP";
898 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000899 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000901
Evan Chenga8e29892007-01-19 07:51:42 +0000902 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000903
Jim Grosbach3482c802010-01-18 19:58:49 +0000904 case ARMISD::RBIT: return "ARMISD::RBIT";
905
Bob Wilson76a312b2010-03-19 22:51:32 +0000906 case ARMISD::FTOSI: return "ARMISD::FTOSI";
907 case ARMISD::FTOUI: return "ARMISD::FTOUI";
908 case ARMISD::SITOF: return "ARMISD::SITOF";
909 case ARMISD::UITOF: return "ARMISD::UITOF";
910
Evan Chenga8e29892007-01-19 07:51:42 +0000911 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
912 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
913 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000914
Evan Cheng342e3162011-08-30 01:34:54 +0000915 case ARMISD::ADDC: return "ARMISD::ADDC";
916 case ARMISD::ADDE: return "ARMISD::ADDE";
917 case ARMISD::SUBC: return "ARMISD::SUBC";
918 case ARMISD::SUBE: return "ARMISD::SUBE";
919
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000920 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
921 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000922
Evan Chengc5942082009-10-28 06:55:03 +0000923 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
924 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
925
Dale Johannesen51e28e62010-06-03 21:09:53 +0000926 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000927
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000928 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000929
Evan Cheng86198642009-08-07 00:34:42 +0000930 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
931
Jim Grosbach3728e962009-12-10 00:11:09 +0000932 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000933 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000934
Evan Chengdfed19f2010-11-03 06:34:55 +0000935 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
936
Bob Wilson5bafff32009-06-22 23:27:02 +0000937 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000938 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000939 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000940 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
941 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000942 case ARMISD::VCGEU: return "ARMISD::VCGEU";
943 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000944 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
945 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000946 case ARMISD::VCGTU: return "ARMISD::VCGTU";
947 case ARMISD::VTST: return "ARMISD::VTST";
948
949 case ARMISD::VSHL: return "ARMISD::VSHL";
950 case ARMISD::VSHRs: return "ARMISD::VSHRs";
951 case ARMISD::VSHRu: return "ARMISD::VSHRu";
952 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
953 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
954 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
955 case ARMISD::VSHRN: return "ARMISD::VSHRN";
956 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
957 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
958 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
959 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
960 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
961 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
962 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
963 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
964 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
965 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
966 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
967 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
968 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
969 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000970 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000971 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000972 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000973 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000974 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000975 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000976 case ARMISD::VREV64: return "ARMISD::VREV64";
977 case ARMISD::VREV32: return "ARMISD::VREV32";
978 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000979 case ARMISD::VZIP: return "ARMISD::VZIP";
980 case ARMISD::VUZP: return "ARMISD::VUZP";
981 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000982 case ARMISD::VTBL1: return "ARMISD::VTBL1";
983 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000984 case ARMISD::VMULLs: return "ARMISD::VMULLs";
985 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000986 case ARMISD::UMLAL: return "ARMISD::UMLAL";
987 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000988 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000989 case ARMISD::FMAX: return "ARMISD::FMAX";
990 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +0000991 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +0000992 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
993 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +0000994 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +0000995 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
996 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
997 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +0000998 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
999 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1000 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1001 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1002 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1003 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1004 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1005 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1006 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1007 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1008 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1009 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1010 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1011 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1012 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1013 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1014 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001015 }
1016}
1017
Duncan Sands28b77e92011-09-06 19:07:46 +00001018EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1019 if (!VT.isVector()) return getPointerTy();
1020 return VT.changeVectorElementTypeToInteger();
1021}
1022
Evan Cheng06b666c2010-05-15 02:18:07 +00001023/// getRegClassFor - Return the register class that should be used for the
1024/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001025const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001026 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1027 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1028 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001029 if (Subtarget->hasNEON()) {
1030 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001031 return &ARM::QQPRRegClass;
1032 if (VT == MVT::v8i64)
1033 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001034 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001035 return TargetLowering::getRegClassFor(VT);
1036}
1037
Eric Christopherab695882010-07-21 22:26:11 +00001038// Create a fast isel object.
1039FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001040ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1041 const TargetLibraryInfo *libInfo) const {
1042 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001043}
1044
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001045/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1046/// be used for loads / stores from the global.
1047unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1048 return (Subtarget->isThumb1Only() ? 127 : 4095);
1049}
1050
Evan Cheng1cc39842010-05-20 23:26:43 +00001051Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001052 unsigned NumVals = N->getNumValues();
1053 if (!NumVals)
1054 return Sched::RegPressure;
1055
1056 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001057 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001058 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001059 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001060 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001061 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001062 }
Evan Chengc10f5432010-05-28 23:25:23 +00001063
1064 if (!N->isMachineOpcode())
1065 return Sched::RegPressure;
1066
1067 // Load are scheduled for latency even if there instruction itinerary
1068 // is not available.
1069 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001070 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001071
Evan Chenge837dea2011-06-28 19:10:37 +00001072 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001073 return Sched::RegPressure;
1074 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001075 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001076 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001077
Evan Cheng1cc39842010-05-20 23:26:43 +00001078 return Sched::RegPressure;
1079}
1080
Evan Chenga8e29892007-01-19 07:51:42 +00001081//===----------------------------------------------------------------------===//
1082// Lowering Code
1083//===----------------------------------------------------------------------===//
1084
Evan Chenga8e29892007-01-19 07:51:42 +00001085/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1086static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1087 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001088 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001089 case ISD::SETNE: return ARMCC::NE;
1090 case ISD::SETEQ: return ARMCC::EQ;
1091 case ISD::SETGT: return ARMCC::GT;
1092 case ISD::SETGE: return ARMCC::GE;
1093 case ISD::SETLT: return ARMCC::LT;
1094 case ISD::SETLE: return ARMCC::LE;
1095 case ISD::SETUGT: return ARMCC::HI;
1096 case ISD::SETUGE: return ARMCC::HS;
1097 case ISD::SETULT: return ARMCC::LO;
1098 case ISD::SETULE: return ARMCC::LS;
1099 }
1100}
1101
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001102/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1103static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001104 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001105 CondCode2 = ARMCC::AL;
1106 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001107 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001108 case ISD::SETEQ:
1109 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1110 case ISD::SETGT:
1111 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1112 case ISD::SETGE:
1113 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1114 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001115 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001116 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1117 case ISD::SETO: CondCode = ARMCC::VC; break;
1118 case ISD::SETUO: CondCode = ARMCC::VS; break;
1119 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1120 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1121 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1122 case ISD::SETLT:
1123 case ISD::SETULT: CondCode = ARMCC::LT; break;
1124 case ISD::SETLE:
1125 case ISD::SETULE: CondCode = ARMCC::LE; break;
1126 case ISD::SETNE:
1127 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1128 }
Evan Chenga8e29892007-01-19 07:51:42 +00001129}
1130
Bob Wilson1f595bb2009-04-17 19:07:39 +00001131//===----------------------------------------------------------------------===//
1132// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001133//===----------------------------------------------------------------------===//
1134
1135#include "ARMGenCallingConv.inc"
1136
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001137/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1138/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001139CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001140 bool Return,
1141 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001142 switch (CC) {
1143 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001144 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001145 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001146 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001147 if (!Subtarget->isAAPCS_ABI())
1148 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1149 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1150 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1151 }
1152 // Fallthrough
1153 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001154 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001155 if (!Subtarget->isAAPCS_ABI())
1156 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1157 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001158 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1159 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001160 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1161 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1162 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001163 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001164 if (!isVarArg)
1165 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1166 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001167 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001168 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001170 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001171 case CallingConv::GHC:
1172 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001173 }
1174}
1175
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176/// LowerCallResult - Lower the result values of a call into the
1177/// appropriate copies out of appropriate physical registers.
1178SDValue
1179ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001180 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001181 const SmallVectorImpl<ISD::InputArg> &Ins,
1182 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001183 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185 // Assign locations to each value returned by this call.
1186 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001187 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1188 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001190 CCAssignFnForNode(CallConv, /* Return*/ true,
1191 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001192
1193 // Copy all of the result registers out of their specified physreg.
1194 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1195 CCValAssign VA = RVLocs[i];
1196
Bob Wilson80915242009-04-25 00:33:20 +00001197 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001199 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001202 Chain = Lo.getValue(1);
1203 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001204 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001206 InFlag);
1207 Chain = Hi.getValue(1);
1208 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001209 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001210
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 if (VA.getLocVT() == MVT::v2f64) {
1212 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1213 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1214 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001215
1216 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001218 Chain = Lo.getValue(1);
1219 InFlag = Lo.getValue(2);
1220 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001221 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001222 Chain = Hi.getValue(1);
1223 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001224 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1226 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001227 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001229 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1230 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001231 Chain = Val.getValue(1);
1232 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 }
Bob Wilson80915242009-04-25 00:33:20 +00001234
1235 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001236 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001237 case CCValAssign::Full: break;
1238 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001239 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001240 break;
1241 }
1242
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001244 }
1245
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247}
1248
Bob Wilsondee46d72009-04-17 20:35:10 +00001249/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001250SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1252 SDValue StackPtr, SDValue Arg,
1253 DebugLoc dl, SelectionDAG &DAG,
1254 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001255 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256 unsigned LocMemOffset = VA.getLocMemOffset();
1257 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1258 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001260 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001261 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001262}
1263
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001265 SDValue Chain, SDValue &Arg,
1266 RegsToPassVector &RegsToPass,
1267 CCValAssign &VA, CCValAssign &NextVA,
1268 SDValue &StackPtr,
1269 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001270 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001271
Jim Grosbache5165492009-11-09 00:11:35 +00001272 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1275
1276 if (NextVA.isRegLoc())
1277 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1278 else {
1279 assert(NextVA.isMemLoc());
1280 if (StackPtr.getNode() == 0)
1281 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1282
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1284 dl, DAG, NextVA,
1285 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001286 }
1287}
1288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001290/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1291/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001293ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001294 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001295 SelectionDAG &DAG = CLI.DAG;
1296 DebugLoc &dl = CLI.DL;
1297 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1298 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1299 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1300 SDValue Chain = CLI.Chain;
1301 SDValue Callee = CLI.Callee;
1302 bool &isTailCall = CLI.IsTailCall;
1303 CallingConv::ID CallConv = CLI.CallConv;
1304 bool doesNotRet = CLI.DoesNotReturn;
1305 bool isVarArg = CLI.IsVarArg;
1306
Dale Johannesen51e28e62010-06-03 21:09:53 +00001307 MachineFunction &MF = DAG.getMachineFunction();
1308 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1309 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001310 // Disable tail calls if they're not supported.
1311 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001312 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001313 if (isTailCall) {
1314 // Check if it's really possible to do a tail call.
1315 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1316 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001317 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001318 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1319 // detected sibcalls.
1320 if (isTailCall) {
1321 ++NumTailCalls;
1322 IsSibCall = true;
1323 }
1324 }
Evan Chenga8e29892007-01-19 07:51:42 +00001325
Bob Wilson1f595bb2009-04-17 19:07:39 +00001326 // Analyze operands of the call, assigning locations to each operand.
1327 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001328 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1329 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001330 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001331 CCAssignFnForNode(CallConv, /* Return*/ false,
1332 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001333
Bob Wilson1f595bb2009-04-17 19:07:39 +00001334 // Get a count of how many bytes are to be pushed on the stack.
1335 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001336
Dale Johannesen51e28e62010-06-03 21:09:53 +00001337 // For tail calls, memory operands are available in our caller's stack.
1338 if (IsSibCall)
1339 NumBytes = 0;
1340
Evan Chenga8e29892007-01-19 07:51:42 +00001341 // Adjust the stack pointer for the new arguments...
1342 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001343 if (!IsSibCall)
1344 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001345
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001346 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001347
Bob Wilson5bafff32009-06-22 23:27:02 +00001348 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001349 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001350
Bob Wilson1f595bb2009-04-17 19:07:39 +00001351 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001352 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001353 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1354 i != e;
1355 ++i, ++realArgIdx) {
1356 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001357 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001359 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001360
Bob Wilson1f595bb2009-04-17 19:07:39 +00001361 // Promote the value if needed.
1362 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001363 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 case CCValAssign::Full: break;
1365 case CCValAssign::SExt:
1366 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1367 break;
1368 case CCValAssign::ZExt:
1369 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1370 break;
1371 case CCValAssign::AExt:
1372 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1373 break;
1374 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001375 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001377 }
1378
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001379 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001380 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001381 if (VA.getLocVT() == MVT::v2f64) {
1382 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1383 DAG.getConstant(0, MVT::i32));
1384 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1385 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001386
Dan Gohman98ca4f22009-08-05 01:29:28 +00001387 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001388 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1389
1390 VA = ArgLocs[++i]; // skip ahead to next loc
1391 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001393 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1394 } else {
1395 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001396
Dan Gohman98ca4f22009-08-05 01:29:28 +00001397 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1398 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001399 }
1400 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001402 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001403 }
1404 } else if (VA.isRegLoc()) {
1405 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001406 } else if (isByVal) {
1407 assert(VA.isMemLoc());
1408 unsigned offset = 0;
1409
1410 // True if this byval aggregate will be split between registers
1411 // and memory.
1412 if (CCInfo.isFirstByValRegValid()) {
1413 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1414 unsigned int i, j;
1415 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1416 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1417 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1418 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1419 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001420 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001421 MemOpChains.push_back(Load.getValue(1));
1422 RegsToPass.push_back(std::make_pair(j, Load));
1423 }
1424 offset = ARM::R4 - CCInfo.getFirstByValReg();
1425 CCInfo.clearFirstByValReg();
1426 }
1427
Manman Ren763a75d2012-06-01 02:44:42 +00001428 if (Flags.getByValSize() - 4*offset > 0) {
1429 unsigned LocMemOffset = VA.getLocMemOffset();
1430 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1431 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1432 StkPtrOff);
1433 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1434 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1435 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1436 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001437 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001438
Manman Ren763a75d2012-06-01 02:44:42 +00001439 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001440 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001441 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1442 Ops, array_lengthof(Ops)));
1443 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001444 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001445 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001446
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1448 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001449 }
Evan Chenga8e29892007-01-19 07:51:42 +00001450 }
1451
1452 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001453 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001454 &MemOpChains[0], MemOpChains.size());
1455
1456 // Build a sequence of copy-to-reg nodes chained together with token chain
1457 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001458 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001459 // Tail call byval lowering might overwrite argument registers so in case of
1460 // tail call optimization the copies to registers are lowered later.
1461 if (!isTailCall)
1462 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1463 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1464 RegsToPass[i].second, InFlag);
1465 InFlag = Chain.getValue(1);
1466 }
Evan Chenga8e29892007-01-19 07:51:42 +00001467
Dale Johannesen51e28e62010-06-03 21:09:53 +00001468 // For tail calls lower the arguments to the 'real' stack slot.
1469 if (isTailCall) {
1470 // Force all the incoming stack arguments to be loaded from the stack
1471 // before any new outgoing arguments are stored to the stack, because the
1472 // outgoing stack slots may alias the incoming argument stack slots, and
1473 // the alias isn't otherwise explicit. This is slightly more conservative
1474 // than necessary, because it means that each store effectively depends
1475 // on every argument instead of just those arguments it would clobber.
1476
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001477 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001478 InFlag = SDValue();
1479 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1480 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1481 RegsToPass[i].second, InFlag);
1482 InFlag = Chain.getValue(1);
1483 }
1484 InFlag =SDValue();
1485 }
1486
Bill Wendling056292f2008-09-16 21:48:12 +00001487 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1488 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1489 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001490 bool isDirect = false;
1491 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001492 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001493 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001494
1495 if (EnableARMLongCalls) {
1496 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1497 && "long-calls with non-static relocation model!");
1498 // Handle a global address or an external symbol. If it's not one of
1499 // those, the target's already in a register, so we don't need to do
1500 // anything extra.
1501 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001502 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001503 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001504 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001505 ARMConstantPoolValue *CPV =
1506 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1507
Jim Grosbache7b52522010-04-14 22:28:31 +00001508 // Get the address of the callee into a register
1509 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1510 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1511 Callee = DAG.getLoad(getPointerTy(), dl,
1512 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001513 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001514 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001515 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1516 const char *Sym = S->getSymbol();
1517
1518 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001519 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001520 ARMConstantPoolValue *CPV =
1521 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1522 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001523 // Get the address of the callee into a register
1524 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1525 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1526 Callee = DAG.getLoad(getPointerTy(), dl,
1527 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001528 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001529 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001530 }
1531 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001532 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001533 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001534 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001535 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001536 getTargetMachine().getRelocationModel() != Reloc::Static;
1537 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001538 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001539 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001540 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001541 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001542 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001543 ARMConstantPoolValue *CPV =
1544 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001545 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001546 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001547 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001548 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001549 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001550 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001551 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001552 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001553 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001554 } else {
1555 // On ELF targets for PIC code, direct calls should go through the PLT
1556 unsigned OpFlags = 0;
1557 if (Subtarget->isTargetELF() &&
1558 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1559 OpFlags = ARMII::MO_PLT;
1560 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1561 }
Bill Wendling056292f2008-09-16 21:48:12 +00001562 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001563 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001564 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001565 getTargetMachine().getRelocationModel() != Reloc::Static;
1566 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001567 // tBX takes a register source operand.
1568 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001569 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001570 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001571 ARMConstantPoolValue *CPV =
1572 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1573 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001574 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001576 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001577 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001578 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001579 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001580 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001581 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001582 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001583 } else {
1584 unsigned OpFlags = 0;
1585 // On ELF targets for PIC code, direct calls should go through the PLT
1586 if (Subtarget->isTargetELF() &&
1587 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1588 OpFlags = ARMII::MO_PLT;
1589 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1590 }
Evan Chenga8e29892007-01-19 07:51:42 +00001591 }
1592
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001593 // FIXME: handle tail calls differently.
1594 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001595 if (Subtarget->isThumb()) {
1596 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001597 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001598 else if (doesNotRet && isDirect && !isARMFunc &&
1599 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1600 // "mov lr, pc; b _foo" to avoid confusing the RSP
1601 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001602 else
1603 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1604 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001605 if (!isDirect && !Subtarget->hasV5TOps()) {
1606 CallOpc = ARMISD::CALL_NOLINK;
1607 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1608 // "mov lr, pc; b _foo" to avoid confusing the RSP
1609 CallOpc = ARMISD::CALL_NOLINK;
1610 else
1611 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001612 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001613
Dan Gohman475871a2008-07-27 21:46:04 +00001614 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001615 Ops.push_back(Chain);
1616 Ops.push_back(Callee);
1617
1618 // Add argument registers to the end of the list so that they are known live
1619 // into the call.
1620 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1621 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1622 RegsToPass[i].second.getValueType()));
1623
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001624 // Add a register mask operand representing the call-preserved registers.
1625 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1626 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1627 assert(Mask && "Missing call preserved mask for calling convention");
1628 Ops.push_back(DAG.getRegisterMask(Mask));
1629
Gabor Greifba36cb52008-08-28 21:40:38 +00001630 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001631 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001632
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001633 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001634 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001636
Duncan Sands4bdcb612008-07-02 17:40:58 +00001637 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001638 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001639 InFlag = Chain.getValue(1);
1640
Chris Lattnere563bbc2008-10-11 22:08:30 +00001641 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1642 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001643 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001644 InFlag = Chain.getValue(1);
1645
Bob Wilson1f595bb2009-04-17 19:07:39 +00001646 // Handle result values, copying them out of physregs into vregs that we
1647 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001648 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1649 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001650}
1651
Stuart Hastingsf222e592011-02-28 17:17:53 +00001652/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001653/// on the stack. Remember the next parameter register to allocate,
1654/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001655/// this.
1656void
Craig Topperc89c7442012-03-27 07:21:54 +00001657ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001658 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1659 assert((State->getCallOrPrologue() == Prologue ||
1660 State->getCallOrPrologue() == Call) &&
1661 "unhandled ParmContext");
1662 if ((!State->isFirstByValRegValid()) &&
1663 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1664 State->setFirstByValReg(reg);
1665 // At a call site, a byval parameter that is split between
1666 // registers and memory needs its size truncated here. In a
1667 // function prologue, such byval parameters are reassembled in
1668 // memory, and are not truncated.
1669 if (State->getCallOrPrologue() == Call) {
1670 unsigned excess = 4 * (ARM::R4 - reg);
1671 assert(size >= excess && "expected larger existing stack allocation");
1672 size -= excess;
1673 }
1674 }
1675 // Confiscate any remaining parameter registers to preclude their
1676 // assignment to subsequent parameters.
1677 while (State->AllocateReg(GPRArgRegs, 4))
1678 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001679}
1680
Dale Johannesen51e28e62010-06-03 21:09:53 +00001681/// MatchingStackOffset - Return true if the given stack call argument is
1682/// already available in the same position (relatively) of the caller's
1683/// incoming argument stack.
1684static
1685bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1686 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001687 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1689 int FI = INT_MAX;
1690 if (Arg.getOpcode() == ISD::CopyFromReg) {
1691 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001692 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001693 return false;
1694 MachineInstr *Def = MRI->getVRegDef(VR);
1695 if (!Def)
1696 return false;
1697 if (!Flags.isByVal()) {
1698 if (!TII->isLoadFromStackSlot(Def, FI))
1699 return false;
1700 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001701 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001702 }
1703 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1704 if (Flags.isByVal())
1705 // ByVal argument is passed in as a pointer but it's now being
1706 // dereferenced. e.g.
1707 // define @foo(%struct.X* %A) {
1708 // tail call @bar(%struct.X* byval %A)
1709 // }
1710 return false;
1711 SDValue Ptr = Ld->getBasePtr();
1712 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1713 if (!FINode)
1714 return false;
1715 FI = FINode->getIndex();
1716 } else
1717 return false;
1718
1719 assert(FI != INT_MAX);
1720 if (!MFI->isFixedObjectIndex(FI))
1721 return false;
1722 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1723}
1724
1725/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1726/// for tail call optimization. Targets which want to do tail call
1727/// optimization should implement this function.
1728bool
1729ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1730 CallingConv::ID CalleeCC,
1731 bool isVarArg,
1732 bool isCalleeStructRet,
1733 bool isCallerStructRet,
1734 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001735 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001736 const SmallVectorImpl<ISD::InputArg> &Ins,
1737 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001738 const Function *CallerF = DAG.getMachineFunction().getFunction();
1739 CallingConv::ID CallerCC = CallerF->getCallingConv();
1740 bool CCMatch = CallerCC == CalleeCC;
1741
1742 // Look for obvious safe cases to perform tail call optimization that do not
1743 // require ABI changes. This is what gcc calls sibcall.
1744
Jim Grosbach7616b642010-06-16 23:45:49 +00001745 // Do not sibcall optimize vararg calls unless the call site is not passing
1746 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001747 if (isVarArg && !Outs.empty())
1748 return false;
1749
1750 // Also avoid sibcall optimization if either caller or callee uses struct
1751 // return semantics.
1752 if (isCalleeStructRet || isCallerStructRet)
1753 return false;
1754
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001755 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001756 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1757 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1758 // support in the assembler and linker to be used. This would need to be
1759 // fixed to fully support tail calls in Thumb1.
1760 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001761 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1762 // LR. This means if we need to reload LR, it takes an extra instructions,
1763 // which outweighs the value of the tail call; but here we don't know yet
1764 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001765 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001766 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001767
1768 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1769 // but we need to make sure there are enough registers; the only valid
1770 // registers are the 4 used for parameters. We don't currently do this
1771 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001772 if (Subtarget->isThumb1Only())
1773 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001774
Dale Johannesen51e28e62010-06-03 21:09:53 +00001775 // If the calling conventions do not match, then we'd better make sure the
1776 // results are returned in the same way as what the caller expects.
1777 if (!CCMatch) {
1778 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001779 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1780 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001781 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1782
1783 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001784 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1785 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001786 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1787
1788 if (RVLocs1.size() != RVLocs2.size())
1789 return false;
1790 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1791 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1792 return false;
1793 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1794 return false;
1795 if (RVLocs1[i].isRegLoc()) {
1796 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1797 return false;
1798 } else {
1799 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1800 return false;
1801 }
1802 }
1803 }
1804
1805 // If the callee takes no arguments then go on to check the results of the
1806 // call.
1807 if (!Outs.empty()) {
1808 // Check if stack adjustment is needed. For now, do not do this if any
1809 // argument is passed on the stack.
1810 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001811 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1812 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001813 CCInfo.AnalyzeCallOperands(Outs,
1814 CCAssignFnForNode(CalleeCC, false, isVarArg));
1815 if (CCInfo.getNextStackOffset()) {
1816 MachineFunction &MF = DAG.getMachineFunction();
1817
1818 // Check if the arguments are already laid out in the right way as
1819 // the caller's fixed stack objects.
1820 MachineFrameInfo *MFI = MF.getFrameInfo();
1821 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001822 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001823 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1824 i != e;
1825 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001826 CCValAssign &VA = ArgLocs[i];
1827 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001828 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001829 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001830 if (VA.getLocInfo() == CCValAssign::Indirect)
1831 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001832 if (VA.needsCustom()) {
1833 // f64 and vector types are split into multiple registers or
1834 // register/stack-slot combinations. The types will not match
1835 // the registers; give up on memory f64 refs until we figure
1836 // out what to do about this.
1837 if (!VA.isRegLoc())
1838 return false;
1839 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001840 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001841 if (RegVT == MVT::v2f64) {
1842 if (!ArgLocs[++i].isRegLoc())
1843 return false;
1844 if (!ArgLocs[++i].isRegLoc())
1845 return false;
1846 }
1847 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001848 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1849 MFI, MRI, TII))
1850 return false;
1851 }
1852 }
1853 }
1854 }
1855
1856 return true;
1857}
1858
Dan Gohman98ca4f22009-08-05 01:29:28 +00001859SDValue
1860ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001861 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001863 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001864 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001865
Bob Wilsondee46d72009-04-17 20:35:10 +00001866 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001867 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001868
Bob Wilsondee46d72009-04-17 20:35:10 +00001869 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001870 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1871 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001872
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001874 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1875 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001876
1877 // If this is the first return lowered for this function, add
1878 // the regs to the liveout set for the function.
1879 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1880 for (unsigned i = 0; i != RVLocs.size(); ++i)
1881 if (RVLocs[i].isRegLoc())
1882 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001883 }
1884
Bob Wilson1f595bb2009-04-17 19:07:39 +00001885 SDValue Flag;
1886
1887 // Copy the result values into the output registers.
1888 for (unsigned i = 0, realRVLocIdx = 0;
1889 i != RVLocs.size();
1890 ++i, ++realRVLocIdx) {
1891 CCValAssign &VA = RVLocs[i];
1892 assert(VA.isRegLoc() && "Can only return in registers!");
1893
Dan Gohmanc9403652010-07-07 15:54:55 +00001894 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001895
1896 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001897 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001898 case CCValAssign::Full: break;
1899 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001900 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001901 break;
1902 }
1903
Bob Wilson1f595bb2009-04-17 19:07:39 +00001904 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001905 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001906 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1908 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001909 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001911
1912 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1913 Flag = Chain.getValue(1);
1914 VA = RVLocs[++i]; // skip ahead to next loc
1915 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1916 HalfGPRs.getValue(1), Flag);
1917 Flag = Chain.getValue(1);
1918 VA = RVLocs[++i]; // skip ahead to next loc
1919
1920 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1922 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001923 }
1924 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1925 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001926 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001927 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001928 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001929 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001930 VA = RVLocs[++i]; // skip ahead to next loc
1931 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1932 Flag);
1933 } else
1934 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1935
Bob Wilsondee46d72009-04-17 20:35:10 +00001936 // Guarantee that all emitted copies are
1937 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001938 Flag = Chain.getValue(1);
1939 }
1940
1941 SDValue result;
1942 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001943 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001944 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001946
1947 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001948}
1949
Evan Chengbf010eb2012-04-10 01:51:00 +00001950bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001951 if (N->getNumValues() != 1)
1952 return false;
1953 if (!N->hasNUsesOfValue(1, 0))
1954 return false;
1955
Evan Chengbf010eb2012-04-10 01:51:00 +00001956 SDValue TCChain = Chain;
1957 SDNode *Copy = *N->use_begin();
1958 if (Copy->getOpcode() == ISD::CopyToReg) {
1959 // If the copy has a glue operand, we conservatively assume it isn't safe to
1960 // perform a tail call.
1961 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1962 return false;
1963 TCChain = Copy->getOperand(0);
1964 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1965 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001966 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001967 SmallPtrSet<SDNode*, 2> Copies;
1968 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001969 UI != UE; ++UI) {
1970 if (UI->getOpcode() != ISD::CopyToReg)
1971 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001972 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001973 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001974 if (Copies.size() > 2)
1975 return false;
1976
1977 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1978 UI != UE; ++UI) {
1979 SDValue UseChain = UI->getOperand(0);
1980 if (Copies.count(UseChain.getNode()))
1981 // Second CopyToReg
1982 Copy = *UI;
1983 else
1984 // First CopyToReg
1985 TCChain = UseChain;
1986 }
1987 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001988 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001989 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001990 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001991 Copy = *Copy->use_begin();
1992 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001993 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001994 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001995 } else {
1996 return false;
1997 }
1998
Evan Cheng1bf891a2010-12-01 22:59:46 +00001999 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002000 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2001 UI != UE; ++UI) {
2002 if (UI->getOpcode() != ARMISD::RET_FLAG)
2003 return false;
2004 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002005 }
2006
Evan Chengbf010eb2012-04-10 01:51:00 +00002007 if (!HasRet)
2008 return false;
2009
2010 Chain = TCChain;
2011 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002012}
2013
Evan Cheng485fafc2011-03-21 01:19:09 +00002014bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002015 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002016 return false;
2017
2018 if (!CI->isTailCall())
2019 return false;
2020
2021 return !Subtarget->isThumb1Only();
2022}
2023
Bob Wilsonb62d2572009-11-03 00:02:05 +00002024// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2025// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2026// one of the above mentioned nodes. It has to be wrapped because otherwise
2027// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2028// be used to form addressing mode. These wrapped nodes will be selected
2029// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002030static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002031 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002032 // FIXME there is no actual debug info here
2033 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002034 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002036 if (CP->isMachineConstantPoolEntry())
2037 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2038 CP->getAlignment());
2039 else
2040 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2041 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002042 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002043}
2044
Jim Grosbache1102ca2010-07-19 17:20:38 +00002045unsigned ARMTargetLowering::getJumpTableEncoding() const {
2046 return MachineJumpTableInfo::EK_Inline;
2047}
2048
Dan Gohmand858e902010-04-17 15:26:15 +00002049SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2050 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002051 MachineFunction &MF = DAG.getMachineFunction();
2052 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2053 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002054 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002055 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002056 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002057 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2058 SDValue CPAddr;
2059 if (RelocM == Reloc::Static) {
2060 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2061 } else {
2062 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002063 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002064 ARMConstantPoolValue *CPV =
2065 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2066 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002067 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2068 }
2069 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2070 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002071 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002072 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002073 if (RelocM == Reloc::Static)
2074 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002075 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002076 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002077}
2078
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002079// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002080SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002081ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002082 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002083 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002084 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002085 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002086 MachineFunction &MF = DAG.getMachineFunction();
2087 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002088 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002089 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002090 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2091 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002092 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002094 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002095 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002096 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002097 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002098
Evan Chenge7e0d622009-11-06 22:24:13 +00002099 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002100 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002101
2102 // call __tls_get_addr.
2103 ArgListTy Args;
2104 ArgListEntry Entry;
2105 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002106 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002107 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002108 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002109 TargetLowering::CallLoweringInfo CLI(Chain,
2110 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002111 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002112 0, CallingConv::C, /*isTailCall=*/false,
2113 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002114 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002115 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002116 return CallResult.first;
2117}
2118
2119// Lower ISD::GlobalTLSAddress using the "initial exec" or
2120// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002121SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002122ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002123 SelectionDAG &DAG,
2124 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002125 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002126 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002127 SDValue Offset;
2128 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002129 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002130 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002132
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002133 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002134 MachineFunction &MF = DAG.getMachineFunction();
2135 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002136 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002137 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002138 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2139 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002140 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2141 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2142 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002143 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002144 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002145 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002146 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002147 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002148 Chain = Offset.getValue(1);
2149
Evan Chenge7e0d622009-11-06 22:24:13 +00002150 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002151 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002152
Evan Cheng9eda6892009-10-31 03:39:36 +00002153 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002154 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002155 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002156 } else {
2157 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002158 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002159 ARMConstantPoolValue *CPV =
2160 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002161 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002162 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002163 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002164 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002165 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002166 }
2167
2168 // The address of the thread local variable is the add of the thread
2169 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002170 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002171}
2172
Dan Gohman475871a2008-07-27 21:46:04 +00002173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002174ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002175 // TODO: implement the "local dynamic" model
2176 assert(Subtarget->isTargetELF() &&
2177 "TLS not implemented for non-ELF targets");
2178 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002179
2180 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2181
2182 switch (model) {
2183 case TLSModel::GeneralDynamic:
2184 case TLSModel::LocalDynamic:
2185 return LowerToTLSGeneralDynamicModel(GA, DAG);
2186 case TLSModel::InitialExec:
2187 case TLSModel::LocalExec:
2188 return LowerToTLSExecModels(GA, DAG, model);
2189 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002190 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002191}
2192
Dan Gohman475871a2008-07-27 21:46:04 +00002193SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002194 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002195 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002196 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002197 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002198 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2199 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002200 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002201 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002202 ARMConstantPoolConstant::Create(GV,
2203 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002204 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002205 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002206 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002207 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002208 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002209 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002210 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002211 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002212 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002213 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002214 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002215 MachinePointerInfo::getGOT(),
2216 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002217 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002218 }
2219
2220 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002221 // pair. This is always cheaper.
2222 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002223 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002224 // FIXME: Once remat is capable of dealing with instructions with register
2225 // operands, expand this into two nodes.
2226 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2227 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002228 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002229 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2230 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2231 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2232 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002233 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002234 }
2235}
2236
Dan Gohman475871a2008-07-27 21:46:04 +00002237SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002238 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002239 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002240 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002241 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002242 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002243 MachineFunction &MF = DAG.getMachineFunction();
2244 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2245
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002246 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2247 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002248 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002249 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002250 // FIXME: Once remat is capable of dealing with instructions with register
2251 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002252 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002253 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2254 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2255
Evan Cheng53519f02011-01-21 18:55:51 +00002256 unsigned Wrapper = (RelocM == Reloc::PIC_)
2257 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2258 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002259 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002260 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2261 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002262 MachinePointerInfo::getGOT(),
2263 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002264 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002265 }
2266
2267 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002269 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002270 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002271 } else {
2272 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002273 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2274 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002275 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2276 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002277 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002278 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002279 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002280
Evan Cheng9eda6892009-10-31 03:39:36 +00002281 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002282 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002283 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002285
2286 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002287 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002288 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002289 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002290
Evan Cheng63476a82009-09-03 07:04:02 +00002291 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002292 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002293 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002294
2295 return Result;
2296}
2297
Dan Gohman475871a2008-07-27 21:46:04 +00002298SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002299 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002300 assert(Subtarget->isTargetELF() &&
2301 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002302 MachineFunction &MF = DAG.getMachineFunction();
2303 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002304 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002305 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002306 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002307 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002308 ARMConstantPoolValue *CPV =
2309 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2310 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002311 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002312 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002313 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002314 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002315 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002316 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002317 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002318}
2319
Jim Grosbach0e0da732009-05-12 23:59:14 +00002320SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002321ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2322 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002323 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002324 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2325 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002326 Op.getOperand(1), Val);
2327}
2328
2329SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002330ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2331 DebugLoc dl = Op.getDebugLoc();
2332 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2333 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2334}
2335
2336SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002337ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002338 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002339 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002340 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002341 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002342 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002343 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002344 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002345 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2346 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002347 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002348 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002349 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002350 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002351 EVT PtrVT = getPointerTy();
2352 DebugLoc dl = Op.getDebugLoc();
2353 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2354 SDValue CPAddr;
2355 unsigned PCAdj = (RelocM != Reloc::PIC_)
2356 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002357 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002358 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2359 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002360 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002361 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002362 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002363 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002364 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002365 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002366
2367 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002368 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002369 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2370 }
2371 return Result;
2372 }
Evan Cheng92e39162011-03-29 23:06:19 +00002373 case Intrinsic::arm_neon_vmulls:
2374 case Intrinsic::arm_neon_vmullu: {
2375 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2376 ? ARMISD::VMULLs : ARMISD::VMULLu;
2377 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2378 Op.getOperand(1), Op.getOperand(2));
2379 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002380 }
2381}
2382
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002383static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002384 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002385 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002386 if (!Subtarget->hasDataBarrier()) {
2387 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2388 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2389 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002390 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002391 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002392 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002393 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002394 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002395
2396 SDValue Op5 = Op.getOperand(5);
2397 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2398 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2399 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2400 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2401
2402 ARM_MB::MemBOpt DMBOpt;
2403 if (isDeviceBarrier)
2404 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2405 else
2406 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2407 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2408 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002409}
2410
Eli Friedman26689ac2011-08-03 21:06:02 +00002411
2412static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2413 const ARMSubtarget *Subtarget) {
2414 // FIXME: handle "fence singlethread" more efficiently.
2415 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002416 if (!Subtarget->hasDataBarrier()) {
2417 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2418 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2419 // here.
2420 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2421 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002422 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002423 DAG.getConstant(0, MVT::i32));
2424 }
2425
Eli Friedman26689ac2011-08-03 21:06:02 +00002426 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002427 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002428}
2429
Evan Chengdfed19f2010-11-03 06:34:55 +00002430static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2431 const ARMSubtarget *Subtarget) {
2432 // ARM pre v5TE and Thumb1 does not have preload instructions.
2433 if (!(Subtarget->isThumb2() ||
2434 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2435 // Just preserve the chain.
2436 return Op.getOperand(0);
2437
2438 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002439 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2440 if (!isRead &&
2441 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2442 // ARMv7 with MP extension has PLDW.
2443 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002444
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002445 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2446 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002447 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002448 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002449 isData = ~isData & 1;
2450 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002451
2452 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002453 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2454 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002455}
2456
Dan Gohman1e93df62010-04-17 14:41:14 +00002457static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2458 MachineFunction &MF = DAG.getMachineFunction();
2459 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2460
Evan Chenga8e29892007-01-19 07:51:42 +00002461 // vastart just stores the address of the VarArgsFrameIndex slot into the
2462 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002463 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002464 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002465 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002466 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002467 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2468 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002469}
2470
Dan Gohman475871a2008-07-27 21:46:04 +00002471SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002472ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2473 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002474 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002475 MachineFunction &MF = DAG.getMachineFunction();
2476 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2477
Craig Topper44d23822012-02-22 05:59:10 +00002478 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002479 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002480 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002481 else
Craig Topper420761a2012-04-20 07:30:17 +00002482 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002483
2484 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002485 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002487
2488 SDValue ArgValue2;
2489 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002490 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002491 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002492
2493 // Create load node to retrieve arguments from the stack.
2494 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002495 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002496 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002497 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002498 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002499 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002500 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 }
2502
Jim Grosbache5165492009-11-09 00:11:35 +00002503 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002504}
2505
Stuart Hastingsc7315872011-04-20 16:47:52 +00002506void
2507ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2508 unsigned &VARegSize, unsigned &VARegSaveSize)
2509 const {
2510 unsigned NumGPRs;
2511 if (CCInfo.isFirstByValRegValid())
2512 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2513 else {
2514 unsigned int firstUnalloced;
2515 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2516 sizeof(GPRArgRegs) /
2517 sizeof(GPRArgRegs[0]));
2518 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2519 }
2520
2521 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2522 VARegSize = NumGPRs * 4;
2523 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2524}
2525
2526// The remaining GPRs hold either the beginning of variable-argument
2527// data, or the beginning of an aggregate passed by value (usuall
2528// byval). Either way, we allocate stack slots adjacent to the data
2529// provided by our caller, and store the unallocated registers there.
2530// If this is a variadic function, the va_list pointer will begin with
2531// these values; otherwise, this reassembles a (byval) structure that
2532// was split between registers and memory.
2533void
2534ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2535 DebugLoc dl, SDValue &Chain,
2536 unsigned ArgOffset) const {
2537 MachineFunction &MF = DAG.getMachineFunction();
2538 MachineFrameInfo *MFI = MF.getFrameInfo();
2539 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2540 unsigned firstRegToSaveIndex;
2541 if (CCInfo.isFirstByValRegValid())
2542 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2543 else {
2544 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2545 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2546 }
2547
2548 unsigned VARegSize, VARegSaveSize;
2549 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2550 if (VARegSaveSize) {
2551 // If this function is vararg, store any remaining integer argument regs
2552 // to their spots on the stack so that they may be loaded by deferencing
2553 // the result of va_next.
2554 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002555 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2556 ArgOffset + VARegSaveSize
2557 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002558 false));
2559 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2560 getPointerTy());
2561
2562 SmallVector<SDValue, 4> MemOps;
2563 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002564 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002565 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002566 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002567 else
Craig Topper420761a2012-04-20 07:30:17 +00002568 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002569
2570 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2571 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2572 SDValue Store =
2573 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002574 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002575 false, false, 0);
2576 MemOps.push_back(Store);
2577 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2578 DAG.getConstant(4, getPointerTy()));
2579 }
2580 if (!MemOps.empty())
2581 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2582 &MemOps[0], MemOps.size());
2583 } else
2584 // This will point to the next argument passed via stack.
2585 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2586}
2587
Bob Wilson5bafff32009-06-22 23:27:02 +00002588SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002590 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002591 const SmallVectorImpl<ISD::InputArg>
2592 &Ins,
2593 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002594 SmallVectorImpl<SDValue> &InVals)
2595 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002596 MachineFunction &MF = DAG.getMachineFunction();
2597 MachineFrameInfo *MFI = MF.getFrameInfo();
2598
Bob Wilson1f595bb2009-04-17 19:07:39 +00002599 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2600
2601 // Assign locations to all of the incoming arguments.
2602 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002603 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2604 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002605 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002606 CCAssignFnForNode(CallConv, /* Return*/ false,
2607 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002608
2609 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002610 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002611
Stuart Hastingsf222e592011-02-28 17:17:53 +00002612 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002613 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2614 CCValAssign &VA = ArgLocs[i];
2615
Bob Wilsondee46d72009-04-17 20:35:10 +00002616 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002617 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002618 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619
Bob Wilson1f595bb2009-04-17 19:07:39 +00002620 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002621 // f64 and vector types are split up into multiple registers or
2622 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002624 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002625 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002626 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002627 SDValue ArgValue2;
2628 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002629 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002630 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2631 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002632 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002633 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002634 } else {
2635 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2636 Chain, DAG, dl);
2637 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2639 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002640 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002642 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2643 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002644 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002645
Bob Wilson5bafff32009-06-22 23:27:02 +00002646 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002647 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002648
Owen Anderson825b72b2009-08-11 20:47:22 +00002649 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002650 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002652 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002654 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002656 RC = AFI->isThumb1OnlyFunction() ?
2657 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2658 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002659 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002660 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002661
2662 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002663 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002665 }
2666
2667 // If this is an 8 or 16-bit value, it is really passed promoted
2668 // to 32 bits. Insert an assert[sz]ext to capture this, then
2669 // truncate to the right size.
2670 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002671 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002672 case CCValAssign::Full: break;
2673 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002674 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002675 break;
2676 case CCValAssign::SExt:
2677 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2678 DAG.getValueType(VA.getValVT()));
2679 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2680 break;
2681 case CCValAssign::ZExt:
2682 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2683 DAG.getValueType(VA.getValVT()));
2684 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2685 break;
2686 }
2687
Dan Gohman98ca4f22009-08-05 01:29:28 +00002688 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002689
2690 } else { // VA.isRegLoc()
2691
2692 // sanity check
2693 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002694 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002695
Stuart Hastingsf222e592011-02-28 17:17:53 +00002696 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002697
Stuart Hastingsf222e592011-02-28 17:17:53 +00002698 // Some Ins[] entries become multiple ArgLoc[] entries.
2699 // Process them only once.
2700 if (index != lastInsIndex)
2701 {
2702 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002703 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002704 // This can be changed with more analysis.
2705 // In case of tail call optimization mark all arguments mutable.
2706 // Since they could be overwritten by lowering of arguments in case of
2707 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002708 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002709 unsigned VARegSize, VARegSaveSize;
2710 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2711 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2712 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002713 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002714 int FI = MFI->CreateFixedObject(Bytes,
2715 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002716 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2717 } else {
2718 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2719 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002720
Stuart Hastingsf222e592011-02-28 17:17:53 +00002721 // Create load nodes to retrieve arguments from the stack.
2722 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2723 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2724 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002725 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002726 }
2727 lastInsIndex = index;
2728 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002729 }
2730 }
2731
2732 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002733 if (isVarArg)
2734 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002735
Dan Gohman98ca4f22009-08-05 01:29:28 +00002736 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002737}
2738
2739/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002740static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002741 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002742 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002743 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002744 // Maybe this has already been legalized into the constant pool?
2745 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002747 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002748 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002749 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002750 }
2751 }
2752 return false;
2753}
2754
Evan Chenga8e29892007-01-19 07:51:42 +00002755/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2756/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002757SDValue
2758ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002759 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002760 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002761 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002762 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002763 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002764 // Constant does not fit, try adjusting it by one?
2765 switch (CC) {
2766 default: break;
2767 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002768 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002769 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002770 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002771 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002772 }
2773 break;
2774 case ISD::SETULT:
2775 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002776 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002777 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002779 }
2780 break;
2781 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002782 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002783 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002784 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002785 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002786 }
2787 break;
2788 case ISD::SETULE:
2789 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002790 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002791 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002792 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002793 }
2794 break;
2795 }
2796 }
2797 }
2798
2799 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002800 ARMISD::NodeType CompareType;
2801 switch (CondCode) {
2802 default:
2803 CompareType = ARMISD::CMP;
2804 break;
2805 case ARMCC::EQ:
2806 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002807 // Uses only Z Flag
2808 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002809 break;
2810 }
Evan Cheng218977b2010-07-13 19:27:42 +00002811 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002812 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002813}
2814
2815/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002816SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002817ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002818 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002819 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002820 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002821 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002822 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002823 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2824 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002825}
2826
Bob Wilson79f56c92011-03-08 01:17:20 +00002827/// duplicateCmp - Glue values can have only one use, so this function
2828/// duplicates a comparison node.
2829SDValue
2830ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2831 unsigned Opc = Cmp.getOpcode();
2832 DebugLoc DL = Cmp.getDebugLoc();
2833 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2834 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2835
2836 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2837 Cmp = Cmp.getOperand(0);
2838 Opc = Cmp.getOpcode();
2839 if (Opc == ARMISD::CMPFP)
2840 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2841 else {
2842 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2843 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2844 }
2845 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2846}
2847
Bill Wendlingde2b1512010-08-11 08:43:16 +00002848SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2849 SDValue Cond = Op.getOperand(0);
2850 SDValue SelectTrue = Op.getOperand(1);
2851 SDValue SelectFalse = Op.getOperand(2);
2852 DebugLoc dl = Op.getDebugLoc();
2853
2854 // Convert:
2855 //
2856 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2857 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2858 //
2859 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2860 const ConstantSDNode *CMOVTrue =
2861 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2862 const ConstantSDNode *CMOVFalse =
2863 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2864
2865 if (CMOVTrue && CMOVFalse) {
2866 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2867 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2868
2869 SDValue True;
2870 SDValue False;
2871 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2872 True = SelectTrue;
2873 False = SelectFalse;
2874 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2875 True = SelectFalse;
2876 False = SelectTrue;
2877 }
2878
2879 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002880 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002881 SDValue ARMcc = Cond.getOperand(2);
2882 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002883 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002884 assert(True.getValueType() == VT);
2885 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002886 }
2887 }
2888 }
2889
Dan Gohmandb953892012-02-24 00:09:36 +00002890 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2891 // undefined bits before doing a full-word comparison with zero.
2892 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2893 DAG.getConstant(1, Cond.getValueType()));
2894
Bill Wendlingde2b1512010-08-11 08:43:16 +00002895 return DAG.getSelectCC(dl, Cond,
2896 DAG.getConstant(0, Cond.getValueType()),
2897 SelectTrue, SelectFalse, ISD::SETNE);
2898}
2899
Dan Gohmand858e902010-04-17 15:26:15 +00002900SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002901 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002902 SDValue LHS = Op.getOperand(0);
2903 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002904 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue TrueVal = Op.getOperand(2);
2906 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002907 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002908
Owen Anderson825b72b2009-08-11 20:47:22 +00002909 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002910 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002911 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002912 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002913 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002914 }
2915
2916 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002917 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002918
Evan Cheng218977b2010-07-13 19:27:42 +00002919 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2920 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002922 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002923 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002924 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002925 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002926 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002927 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002928 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002929 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002930 }
2931 return Result;
2932}
2933
Evan Cheng218977b2010-07-13 19:27:42 +00002934/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2935/// to morph to an integer compare sequence.
2936static bool canChangeToInt(SDValue Op, bool &SeenZero,
2937 const ARMSubtarget *Subtarget) {
2938 SDNode *N = Op.getNode();
2939 if (!N->hasOneUse())
2940 // Otherwise it requires moving the value from fp to integer registers.
2941 return false;
2942 if (!N->getNumValues())
2943 return false;
2944 EVT VT = Op.getValueType();
2945 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2946 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2947 // vmrs are very slow, e.g. cortex-a8.
2948 return false;
2949
2950 if (isFloatingPointZero(Op)) {
2951 SeenZero = true;
2952 return true;
2953 }
2954 return ISD::isNormalLoad(N);
2955}
2956
2957static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2958 if (isFloatingPointZero(Op))
2959 return DAG.getConstant(0, MVT::i32);
2960
2961 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2962 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002963 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002964 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002965 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002966
2967 llvm_unreachable("Unknown VFP cmp argument!");
2968}
2969
2970static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2971 SDValue &RetVal1, SDValue &RetVal2) {
2972 if (isFloatingPointZero(Op)) {
2973 RetVal1 = DAG.getConstant(0, MVT::i32);
2974 RetVal2 = DAG.getConstant(0, MVT::i32);
2975 return;
2976 }
2977
2978 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2979 SDValue Ptr = Ld->getBasePtr();
2980 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2981 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002982 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002983 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002984 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002985
2986 EVT PtrType = Ptr.getValueType();
2987 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2988 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2989 PtrType, Ptr, DAG.getConstant(4, PtrType));
2990 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2991 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002992 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002993 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002994 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002995 return;
2996 }
2997
2998 llvm_unreachable("Unknown VFP cmp argument!");
2999}
3000
3001/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3002/// f32 and even f64 comparisons to integer ones.
3003SDValue
3004ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3005 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003006 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003007 SDValue LHS = Op.getOperand(2);
3008 SDValue RHS = Op.getOperand(3);
3009 SDValue Dest = Op.getOperand(4);
3010 DebugLoc dl = Op.getDebugLoc();
3011
Evan Chengfc501a32012-03-01 23:27:13 +00003012 bool LHSSeenZero = false;
3013 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3014 bool RHSSeenZero = false;
3015 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3016 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003017 // If unsafe fp math optimization is enabled and there are no other uses of
3018 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003019 // to an integer comparison.
3020 if (CC == ISD::SETOEQ)
3021 CC = ISD::SETEQ;
3022 else if (CC == ISD::SETUNE)
3023 CC = ISD::SETNE;
3024
Evan Chengfc501a32012-03-01 23:27:13 +00003025 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003026 SDValue ARMcc;
3027 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003028 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3029 bitcastf32Toi32(LHS, DAG), Mask);
3030 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3031 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003032 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3033 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3034 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3035 Chain, Dest, ARMcc, CCR, Cmp);
3036 }
3037
3038 SDValue LHS1, LHS2;
3039 SDValue RHS1, RHS2;
3040 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3041 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003042 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3043 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003044 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3045 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003046 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003047 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3048 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3049 }
3050
3051 return SDValue();
3052}
3053
3054SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3055 SDValue Chain = Op.getOperand(0);
3056 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3057 SDValue LHS = Op.getOperand(2);
3058 SDValue RHS = Op.getOperand(3);
3059 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003060 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003061
Owen Anderson825b72b2009-08-11 20:47:22 +00003062 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003063 SDValue ARMcc;
3064 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003065 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003067 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003068 }
3069
Owen Anderson825b72b2009-08-11 20:47:22 +00003070 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003071
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003072 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003073 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3074 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3075 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3076 if (Result.getNode())
3077 return Result;
3078 }
3079
Evan Chenga8e29892007-01-19 07:51:42 +00003080 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003081 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003082
Evan Cheng218977b2010-07-13 19:27:42 +00003083 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3084 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003085 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003086 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003087 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003088 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003089 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003090 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3091 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003092 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003093 }
3094 return Res;
3095}
3096
Dan Gohmand858e902010-04-17 15:26:15 +00003097SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003098 SDValue Chain = Op.getOperand(0);
3099 SDValue Table = Op.getOperand(1);
3100 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003101 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003102
Owen Andersone50ed302009-08-10 22:56:29 +00003103 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003104 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3105 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003106 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003107 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003109 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3110 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003111 if (Subtarget->isThumb2()) {
3112 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3113 // which does another jump to the destination. This also makes it easier
3114 // to translate it to TBB / TBH later.
3115 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003117 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003118 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003119 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003120 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003121 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003122 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003123 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003124 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003126 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003127 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003128 MachinePointerInfo::getJumpTable(),
3129 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003130 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003132 }
Evan Chenga8e29892007-01-19 07:51:42 +00003133}
3134
Eli Friedman14e809c2011-11-09 23:36:02 +00003135static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003136 EVT VT = Op.getValueType();
3137 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003138
James Molloy873fd5f2012-02-20 09:24:05 +00003139 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3140 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3141 return Op;
3142 return DAG.UnrollVectorOp(Op.getNode());
3143 }
3144
3145 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3146 "Invalid type for custom lowering!");
3147 if (VT != MVT::v4i16)
3148 return DAG.UnrollVectorOp(Op.getNode());
3149
3150 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3151 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003152}
3153
Bob Wilson76a312b2010-03-19 22:51:32 +00003154static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003155 EVT VT = Op.getValueType();
3156 if (VT.isVector())
3157 return LowerVectorFP_TO_INT(Op, DAG);
3158
Bob Wilson76a312b2010-03-19 22:51:32 +00003159 DebugLoc dl = Op.getDebugLoc();
3160 unsigned Opc;
3161
3162 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003163 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003164 case ISD::FP_TO_SINT:
3165 Opc = ARMISD::FTOSI;
3166 break;
3167 case ISD::FP_TO_UINT:
3168 Opc = ARMISD::FTOUI;
3169 break;
3170 }
3171 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003172 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003173}
3174
Cameron Zwarich3007d332011-03-29 21:41:55 +00003175static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3176 EVT VT = Op.getValueType();
3177 DebugLoc dl = Op.getDebugLoc();
3178
Eli Friedman14e809c2011-11-09 23:36:02 +00003179 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3180 if (VT.getVectorElementType() == MVT::f32)
3181 return Op;
3182 return DAG.UnrollVectorOp(Op.getNode());
3183 }
3184
Duncan Sands1f6a3292011-08-12 14:54:45 +00003185 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3186 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003187 if (VT != MVT::v4f32)
3188 return DAG.UnrollVectorOp(Op.getNode());
3189
3190 unsigned CastOpc;
3191 unsigned Opc;
3192 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003193 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003194 case ISD::SINT_TO_FP:
3195 CastOpc = ISD::SIGN_EXTEND;
3196 Opc = ISD::SINT_TO_FP;
3197 break;
3198 case ISD::UINT_TO_FP:
3199 CastOpc = ISD::ZERO_EXTEND;
3200 Opc = ISD::UINT_TO_FP;
3201 break;
3202 }
3203
3204 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3205 return DAG.getNode(Opc, dl, VT, Op);
3206}
3207
Bob Wilson76a312b2010-03-19 22:51:32 +00003208static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3209 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003210 if (VT.isVector())
3211 return LowerVectorINT_TO_FP(Op, DAG);
3212
Bob Wilson76a312b2010-03-19 22:51:32 +00003213 DebugLoc dl = Op.getDebugLoc();
3214 unsigned Opc;
3215
3216 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003217 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003218 case ISD::SINT_TO_FP:
3219 Opc = ARMISD::SITOF;
3220 break;
3221 case ISD::UINT_TO_FP:
3222 Opc = ARMISD::UITOF;
3223 break;
3224 }
3225
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003226 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003227 return DAG.getNode(Opc, dl, VT, Op);
3228}
3229
Evan Cheng515fe3a2010-07-08 02:08:50 +00003230SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003231 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003232 SDValue Tmp0 = Op.getOperand(0);
3233 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003234 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003235 EVT VT = Op.getValueType();
3236 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003237 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3238 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3239 bool UseNEON = !InGPR && Subtarget->hasNEON();
3240
3241 if (UseNEON) {
3242 // Use VBSL to copy the sign bit.
3243 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3244 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3245 DAG.getTargetConstant(EncodedVal, MVT::i32));
3246 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3247 if (VT == MVT::f64)
3248 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3249 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3250 DAG.getConstant(32, MVT::i32));
3251 else /*if (VT == MVT::f32)*/
3252 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3253 if (SrcVT == MVT::f32) {
3254 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3255 if (VT == MVT::f64)
3256 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3257 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3258 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003259 } else if (VT == MVT::f32)
3260 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3261 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3262 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003263 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3264 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3265
3266 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3267 MVT::i32);
3268 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3269 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3270 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003271
Evan Chenge573fb32011-02-23 02:24:55 +00003272 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3273 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3274 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003275 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003276 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3277 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3278 DAG.getConstant(0, MVT::i32));
3279 } else {
3280 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3281 }
3282
3283 return Res;
3284 }
Evan Chengc143dd42011-02-11 02:28:55 +00003285
3286 // Bitcast operand 1 to i32.
3287 if (SrcVT == MVT::f64)
3288 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3289 &Tmp1, 1).getValue(1);
3290 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3291
Evan Chenge573fb32011-02-23 02:24:55 +00003292 // Or in the signbit with integer operations.
3293 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3294 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3295 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3296 if (VT == MVT::f32) {
3297 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3298 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3299 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3300 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003301 }
3302
Evan Chenge573fb32011-02-23 02:24:55 +00003303 // f64: Or the high part with signbit and then combine two parts.
3304 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3305 &Tmp0, 1);
3306 SDValue Lo = Tmp0.getValue(0);
3307 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3308 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3309 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003310}
3311
Evan Cheng2457f2c2010-05-22 01:47:14 +00003312SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3313 MachineFunction &MF = DAG.getMachineFunction();
3314 MachineFrameInfo *MFI = MF.getFrameInfo();
3315 MFI->setReturnAddressIsTaken(true);
3316
3317 EVT VT = Op.getValueType();
3318 DebugLoc dl = Op.getDebugLoc();
3319 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3320 if (Depth) {
3321 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3322 SDValue Offset = DAG.getConstant(4, MVT::i32);
3323 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3324 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003325 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003326 }
3327
3328 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003329 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003330 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3331}
3332
Dan Gohmand858e902010-04-17 15:26:15 +00003333SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003334 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3335 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003336
Owen Andersone50ed302009-08-10 22:56:29 +00003337 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003338 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3339 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003340 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003341 ? ARM::R7 : ARM::R11;
3342 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3343 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003344 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3345 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003346 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003347 return FrameAddr;
3348}
3349
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003350/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003351/// expand a bit convert where either the source or destination type is i64 to
3352/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3353/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3354/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003355static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003356 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3357 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003358 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003359
Bob Wilson9f3f0612010-04-17 05:30:19 +00003360 // This function is only supposed to be called for i64 types, either as the
3361 // source or destination of the bit convert.
3362 EVT SrcVT = Op.getValueType();
3363 EVT DstVT = N->getValueType(0);
3364 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003365 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003366
Bob Wilson9f3f0612010-04-17 05:30:19 +00003367 // Turn i64->f64 into VMOVDRR.
3368 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3370 DAG.getConstant(0, MVT::i32));
3371 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3372 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003373 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003374 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003375 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003376
Jim Grosbache5165492009-11-09 00:11:35 +00003377 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003378 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3379 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3380 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3381 // Merge the pieces into a single i64 value.
3382 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3383 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003384
Bob Wilson9f3f0612010-04-17 05:30:19 +00003385 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003386}
3387
Bob Wilson5bafff32009-06-22 23:27:02 +00003388/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003389/// Zero vectors are used to represent vector negation and in those cases
3390/// will be implemented with the NEON VNEG instruction. However, VNEG does
3391/// not support i64 elements, so sometimes the zero vectors will need to be
3392/// explicitly constructed. Regardless, use a canonical VMOV to create the
3393/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003394static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003395 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003396 // The canonical modified immediate encoding of a zero vector is....0!
3397 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3398 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3399 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003400 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003401}
3402
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003403/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3404/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003405SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3406 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003407 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3408 EVT VT = Op.getValueType();
3409 unsigned VTBits = VT.getSizeInBits();
3410 DebugLoc dl = Op.getDebugLoc();
3411 SDValue ShOpLo = Op.getOperand(0);
3412 SDValue ShOpHi = Op.getOperand(1);
3413 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003414 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003415 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003416
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003417 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3418
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003419 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3420 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3421 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3422 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3423 DAG.getConstant(VTBits, MVT::i32));
3424 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3425 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003426 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003427
3428 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3429 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003430 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003431 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003432 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003433 CCR, Cmp);
3434
3435 SDValue Ops[2] = { Lo, Hi };
3436 return DAG.getMergeValues(Ops, 2, dl);
3437}
3438
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003439/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3440/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003441SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3442 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003443 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3444 EVT VT = Op.getValueType();
3445 unsigned VTBits = VT.getSizeInBits();
3446 DebugLoc dl = Op.getDebugLoc();
3447 SDValue ShOpLo = Op.getOperand(0);
3448 SDValue ShOpHi = Op.getOperand(1);
3449 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003450 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003451
3452 assert(Op.getOpcode() == ISD::SHL_PARTS);
3453 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3454 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3455 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3456 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3457 DAG.getConstant(VTBits, MVT::i32));
3458 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3459 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3460
3461 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3462 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3463 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003464 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003465 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003466 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003467 CCR, Cmp);
3468
3469 SDValue Ops[2] = { Lo, Hi };
3470 return DAG.getMergeValues(Ops, 2, dl);
3471}
3472
Jim Grosbach4725ca72010-09-08 03:54:02 +00003473SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003474 SelectionDAG &DAG) const {
3475 // The rounding mode is in bits 23:22 of the FPSCR.
3476 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3477 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3478 // so that the shift + and get folded into a bitfield extract.
3479 DebugLoc dl = Op.getDebugLoc();
3480 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3481 DAG.getConstant(Intrinsic::arm_get_fpscr,
3482 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003483 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003484 DAG.getConstant(1U << 22, MVT::i32));
3485 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3486 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003487 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003488 DAG.getConstant(3, MVT::i32));
3489}
3490
Jim Grosbach3482c802010-01-18 19:58:49 +00003491static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3492 const ARMSubtarget *ST) {
3493 EVT VT = N->getValueType(0);
3494 DebugLoc dl = N->getDebugLoc();
3495
3496 if (!ST->hasV6T2Ops())
3497 return SDValue();
3498
3499 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3500 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3501}
3502
Bob Wilson5bafff32009-06-22 23:27:02 +00003503static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3504 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003505 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003506 DebugLoc dl = N->getDebugLoc();
3507
Bob Wilsond5448bb2010-11-18 21:16:28 +00003508 if (!VT.isVector())
3509 return SDValue();
3510
Bob Wilson5bafff32009-06-22 23:27:02 +00003511 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003512 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003513
Bob Wilsond5448bb2010-11-18 21:16:28 +00003514 // Left shifts translate directly to the vshiftu intrinsic.
3515 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003516 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003517 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3518 N->getOperand(0), N->getOperand(1));
3519
3520 assert((N->getOpcode() == ISD::SRA ||
3521 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3522
3523 // NEON uses the same intrinsics for both left and right shifts. For
3524 // right shifts, the shift amounts are negative, so negate the vector of
3525 // shift amounts.
3526 EVT ShiftVT = N->getOperand(1).getValueType();
3527 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3528 getZeroVector(ShiftVT, DAG, dl),
3529 N->getOperand(1));
3530 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3531 Intrinsic::arm_neon_vshifts :
3532 Intrinsic::arm_neon_vshiftu);
3533 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3534 DAG.getConstant(vshiftInt, MVT::i32),
3535 N->getOperand(0), NegatedCount);
3536}
3537
3538static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3539 const ARMSubtarget *ST) {
3540 EVT VT = N->getValueType(0);
3541 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003542
Eli Friedmance392eb2009-08-22 03:13:10 +00003543 // We can get here for a node like i32 = ISD::SHL i32, i64
3544 if (VT != MVT::i64)
3545 return SDValue();
3546
3547 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003548 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003549
Chris Lattner27a6c732007-11-24 07:07:01 +00003550 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3551 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003552 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003553 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003554
Chris Lattner27a6c732007-11-24 07:07:01 +00003555 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003556 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003557
Chris Lattner27a6c732007-11-24 07:07:01 +00003558 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003560 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003562 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003563
Chris Lattner27a6c732007-11-24 07:07:01 +00003564 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3565 // captures the result into a carry flag.
3566 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003567 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003568
Chris Lattner27a6c732007-11-24 07:07:01 +00003569 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003571
Chris Lattner27a6c732007-11-24 07:07:01 +00003572 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003574}
3575
Bob Wilson5bafff32009-06-22 23:27:02 +00003576static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3577 SDValue TmpOp0, TmpOp1;
3578 bool Invert = false;
3579 bool Swap = false;
3580 unsigned Opc = 0;
3581
3582 SDValue Op0 = Op.getOperand(0);
3583 SDValue Op1 = Op.getOperand(1);
3584 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003585 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003586 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3587 DebugLoc dl = Op.getDebugLoc();
3588
3589 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3590 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003591 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003592 case ISD::SETUNE:
3593 case ISD::SETNE: Invert = true; // Fallthrough
3594 case ISD::SETOEQ:
3595 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3596 case ISD::SETOLT:
3597 case ISD::SETLT: Swap = true; // Fallthrough
3598 case ISD::SETOGT:
3599 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3600 case ISD::SETOLE:
3601 case ISD::SETLE: Swap = true; // Fallthrough
3602 case ISD::SETOGE:
3603 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3604 case ISD::SETUGE: Swap = true; // Fallthrough
3605 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3606 case ISD::SETUGT: Swap = true; // Fallthrough
3607 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3608 case ISD::SETUEQ: Invert = true; // Fallthrough
3609 case ISD::SETONE:
3610 // Expand this to (OLT | OGT).
3611 TmpOp0 = Op0;
3612 TmpOp1 = Op1;
3613 Opc = ISD::OR;
3614 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3615 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3616 break;
3617 case ISD::SETUO: Invert = true; // Fallthrough
3618 case ISD::SETO:
3619 // Expand this to (OLT | OGE).
3620 TmpOp0 = Op0;
3621 TmpOp1 = Op1;
3622 Opc = ISD::OR;
3623 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3624 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3625 break;
3626 }
3627 } else {
3628 // Integer comparisons.
3629 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003630 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 case ISD::SETNE: Invert = true;
3632 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3633 case ISD::SETLT: Swap = true;
3634 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3635 case ISD::SETLE: Swap = true;
3636 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3637 case ISD::SETULT: Swap = true;
3638 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3639 case ISD::SETULE: Swap = true;
3640 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3641 }
3642
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003643 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003644 if (Opc == ARMISD::VCEQ) {
3645
3646 SDValue AndOp;
3647 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3648 AndOp = Op0;
3649 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3650 AndOp = Op1;
3651
3652 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003653 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003654 AndOp = AndOp.getOperand(0);
3655
3656 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3657 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003658 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3659 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003660 Invert = !Invert;
3661 }
3662 }
3663 }
3664
3665 if (Swap)
3666 std::swap(Op0, Op1);
3667
Owen Andersonc24cb352010-11-08 23:21:22 +00003668 // If one of the operands is a constant vector zero, attempt to fold the
3669 // comparison to a specialized compare-against-zero form.
3670 SDValue SingleOp;
3671 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3672 SingleOp = Op0;
3673 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3674 if (Opc == ARMISD::VCGE)
3675 Opc = ARMISD::VCLEZ;
3676 else if (Opc == ARMISD::VCGT)
3677 Opc = ARMISD::VCLTZ;
3678 SingleOp = Op1;
3679 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003680
Owen Andersonc24cb352010-11-08 23:21:22 +00003681 SDValue Result;
3682 if (SingleOp.getNode()) {
3683 switch (Opc) {
3684 case ARMISD::VCEQ:
3685 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3686 case ARMISD::VCGE:
3687 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3688 case ARMISD::VCLEZ:
3689 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3690 case ARMISD::VCGT:
3691 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3692 case ARMISD::VCLTZ:
3693 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3694 default:
3695 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3696 }
3697 } else {
3698 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3699 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003700
3701 if (Invert)
3702 Result = DAG.getNOT(dl, Result, VT);
3703
3704 return Result;
3705}
3706
Bob Wilsond3c42842010-06-14 22:19:57 +00003707/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3708/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003709/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003710static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3711 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003712 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003713 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003714
Bob Wilson827b2102010-06-15 19:05:35 +00003715 // SplatBitSize is set to the smallest size that splats the vector, so a
3716 // zero vector will always have SplatBitSize == 8. However, NEON modified
3717 // immediate instructions others than VMOV do not support the 8-bit encoding
3718 // of a zero vector, and the default encoding of zero is supposed to be the
3719 // 32-bit version.
3720 if (SplatBits == 0)
3721 SplatBitSize = 32;
3722
Bob Wilson5bafff32009-06-22 23:27:02 +00003723 switch (SplatBitSize) {
3724 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003725 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003726 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003727 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003728 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003729 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003730 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003731 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003732 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003733
3734 case 16:
3735 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003736 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003737 if ((SplatBits & ~0xff) == 0) {
3738 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003739 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003740 Imm = SplatBits;
3741 break;
3742 }
3743 if ((SplatBits & ~0xff00) == 0) {
3744 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003745 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003746 Imm = SplatBits >> 8;
3747 break;
3748 }
3749 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003750
3751 case 32:
3752 // NEON's 32-bit VMOV supports splat values where:
3753 // * only one byte is nonzero, or
3754 // * the least significant byte is 0xff and the second byte is nonzero, or
3755 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003756 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003757 if ((SplatBits & ~0xff) == 0) {
3758 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003759 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003760 Imm = SplatBits;
3761 break;
3762 }
3763 if ((SplatBits & ~0xff00) == 0) {
3764 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003765 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003766 Imm = SplatBits >> 8;
3767 break;
3768 }
3769 if ((SplatBits & ~0xff0000) == 0) {
3770 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003771 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003772 Imm = SplatBits >> 16;
3773 break;
3774 }
3775 if ((SplatBits & ~0xff000000) == 0) {
3776 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003777 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003778 Imm = SplatBits >> 24;
3779 break;
3780 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003781
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003782 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3783 if (type == OtherModImm) return SDValue();
3784
Bob Wilson5bafff32009-06-22 23:27:02 +00003785 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003786 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3787 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003788 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003789 Imm = SplatBits >> 8;
3790 SplatBits |= 0xff;
3791 break;
3792 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003793
3794 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003795 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3796 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003797 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003798 Imm = SplatBits >> 16;
3799 SplatBits |= 0xffff;
3800 break;
3801 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003802
3803 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3804 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3805 // VMOV.I32. A (very) minor optimization would be to replicate the value
3806 // and fall through here to test for a valid 64-bit splat. But, then the
3807 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003808 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003809
3810 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003811 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003812 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003813 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003814 uint64_t BitMask = 0xff;
3815 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003816 unsigned ImmMask = 1;
3817 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003818 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003819 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003820 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003821 Imm |= ImmMask;
3822 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003823 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003824 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003825 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003826 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003827 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003828 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003829 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003830 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003831 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003832 break;
3833 }
3834
Bob Wilson1a913ed2010-06-11 21:34:50 +00003835 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003836 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003837 }
3838
Bob Wilsoncba270d2010-07-13 21:16:48 +00003839 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3840 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003841}
3842
Lang Hamesc0a9f822012-03-29 21:56:11 +00003843SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3844 const ARMSubtarget *ST) const {
3845 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3846 return SDValue();
3847
3848 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3849 assert(Op.getValueType() == MVT::f32 &&
3850 "ConstantFP custom lowering should only occur for f32.");
3851
3852 // Try splatting with a VMOV.f32...
3853 APFloat FPVal = CFP->getValueAPF();
3854 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3855 if (ImmVal != -1) {
3856 DebugLoc DL = Op.getDebugLoc();
3857 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3858 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3859 NewVal);
3860 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3861 DAG.getConstant(0, MVT::i32));
3862 }
3863
3864 // If that fails, try a VMOV.i32
3865 EVT VMovVT;
3866 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3867 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3868 VMOVModImm);
3869 if (NewVal != SDValue()) {
3870 DebugLoc DL = Op.getDebugLoc();
3871 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3872 NewVal);
3873 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3874 VecConstant);
3875 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3876 DAG.getConstant(0, MVT::i32));
3877 }
3878
3879 // Finally, try a VMVN.i32
3880 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3881 VMVNModImm);
3882 if (NewVal != SDValue()) {
3883 DebugLoc DL = Op.getDebugLoc();
3884 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3885 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3886 VecConstant);
3887 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3888 DAG.getConstant(0, MVT::i32));
3889 }
3890
3891 return SDValue();
3892}
3893
3894
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003895static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003896 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003897 unsigned NumElts = VT.getVectorNumElements();
3898 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003899
3900 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3901 if (M[0] < 0)
3902 return false;
3903
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003904 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003905
3906 // If this is a VEXT shuffle, the immediate value is the index of the first
3907 // element. The other shuffle indices must be the successive elements after
3908 // the first one.
3909 unsigned ExpectedElt = Imm;
3910 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003911 // Increment the expected index. If it wraps around, it may still be
3912 // a VEXT but the source vectors must be swapped.
3913 ExpectedElt += 1;
3914 if (ExpectedElt == NumElts * 2) {
3915 ExpectedElt = 0;
3916 ReverseVEXT = true;
3917 }
3918
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003919 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003920 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003921 return false;
3922 }
3923
3924 // Adjust the index value if the source operands will be swapped.
3925 if (ReverseVEXT)
3926 Imm -= NumElts;
3927
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003928 return true;
3929}
3930
Bob Wilson8bb9e482009-07-26 00:39:34 +00003931/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3932/// instruction with the specified blocksize. (The order of the elements
3933/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003934static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003935 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3936 "Only possible block sizes for VREV are: 16, 32, 64");
3937
Bob Wilson8bb9e482009-07-26 00:39:34 +00003938 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003939 if (EltSz == 64)
3940 return false;
3941
3942 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003943 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003944 // If the first shuffle index is UNDEF, be optimistic.
3945 if (M[0] < 0)
3946 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003947
3948 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3949 return false;
3950
3951 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003952 if (M[i] < 0) continue; // ignore UNDEF indices
3953 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003954 return false;
3955 }
3956
3957 return true;
3958}
3959
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003960static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003961 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3962 // range, then 0 is placed into the resulting vector. So pretty much any mask
3963 // of 8 elements can work here.
3964 return VT == MVT::v8i8 && M.size() == 8;
3965}
3966
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003967static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003968 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3969 if (EltSz == 64)
3970 return false;
3971
Bob Wilsonc692cb72009-08-21 20:54:19 +00003972 unsigned NumElts = VT.getVectorNumElements();
3973 WhichResult = (M[0] == 0 ? 0 : 1);
3974 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003975 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3976 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003977 return false;
3978 }
3979 return true;
3980}
3981
Bob Wilson324f4f12009-12-03 06:40:55 +00003982/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3983/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3984/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003985static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003986 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3987 if (EltSz == 64)
3988 return false;
3989
3990 unsigned NumElts = VT.getVectorNumElements();
3991 WhichResult = (M[0] == 0 ? 0 : 1);
3992 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003993 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3994 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003995 return false;
3996 }
3997 return true;
3998}
3999
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004000static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004001 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4002 if (EltSz == 64)
4003 return false;
4004
Bob Wilsonc692cb72009-08-21 20:54:19 +00004005 unsigned NumElts = VT.getVectorNumElements();
4006 WhichResult = (M[0] == 0 ? 0 : 1);
4007 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004008 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004009 if ((unsigned) M[i] != 2 * i + WhichResult)
4010 return false;
4011 }
4012
4013 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004014 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004015 return false;
4016
4017 return true;
4018}
4019
Bob Wilson324f4f12009-12-03 06:40:55 +00004020/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4021/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4022/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004023static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004024 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4025 if (EltSz == 64)
4026 return false;
4027
4028 unsigned Half = VT.getVectorNumElements() / 2;
4029 WhichResult = (M[0] == 0 ? 0 : 1);
4030 for (unsigned j = 0; j != 2; ++j) {
4031 unsigned Idx = WhichResult;
4032 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004033 int MIdx = M[i + j * Half];
4034 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004035 return false;
4036 Idx += 2;
4037 }
4038 }
4039
4040 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4041 if (VT.is64BitVector() && EltSz == 32)
4042 return false;
4043
4044 return true;
4045}
4046
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004047static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004048 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4049 if (EltSz == 64)
4050 return false;
4051
Bob Wilsonc692cb72009-08-21 20:54:19 +00004052 unsigned NumElts = VT.getVectorNumElements();
4053 WhichResult = (M[0] == 0 ? 0 : 1);
4054 unsigned Idx = WhichResult * NumElts / 2;
4055 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004056 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4057 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004058 return false;
4059 Idx += 1;
4060 }
4061
4062 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004063 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004064 return false;
4065
4066 return true;
4067}
4068
Bob Wilson324f4f12009-12-03 06:40:55 +00004069/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4070/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4071/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004072static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004073 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4074 if (EltSz == 64)
4075 return false;
4076
4077 unsigned NumElts = VT.getVectorNumElements();
4078 WhichResult = (M[0] == 0 ? 0 : 1);
4079 unsigned Idx = WhichResult * NumElts / 2;
4080 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004081 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4082 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004083 return false;
4084 Idx += 1;
4085 }
4086
4087 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4088 if (VT.is64BitVector() && EltSz == 32)
4089 return false;
4090
4091 return true;
4092}
4093
Dale Johannesenf630c712010-07-29 20:10:08 +00004094// If N is an integer constant that can be moved into a register in one
4095// instruction, return an SDValue of such a constant (will become a MOV
4096// instruction). Otherwise return null.
4097static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4098 const ARMSubtarget *ST, DebugLoc dl) {
4099 uint64_t Val;
4100 if (!isa<ConstantSDNode>(N))
4101 return SDValue();
4102 Val = cast<ConstantSDNode>(N)->getZExtValue();
4103
4104 if (ST->isThumb1Only()) {
4105 if (Val <= 255 || ~Val <= 255)
4106 return DAG.getConstant(Val, MVT::i32);
4107 } else {
4108 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4109 return DAG.getConstant(Val, MVT::i32);
4110 }
4111 return SDValue();
4112}
4113
Bob Wilson5bafff32009-06-22 23:27:02 +00004114// If this is a case we can't handle, return null and let the default
4115// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004116SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4117 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004118 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004119 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004120 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004121
4122 APInt SplatBits, SplatUndef;
4123 unsigned SplatBitSize;
4124 bool HasAnyUndefs;
4125 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004126 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004127 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004128 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004129 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004130 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004131 DAG, VmovVT, VT.is128BitVector(),
4132 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004133 if (Val.getNode()) {
4134 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004135 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004136 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004137
4138 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004139 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004140 Val = isNEONModifiedImm(NegatedImm,
4141 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004142 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004143 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004144 if (Val.getNode()) {
4145 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004146 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004147 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004148
4149 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004150 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004151 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004152 if (ImmVal != -1) {
4153 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4154 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4155 }
4156 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004157 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004158 }
4159
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004160 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004161 //
4162 // As an optimisation, even if more than one value is used it may be more
4163 // profitable to splat with one value then change some lanes.
4164 //
4165 // Heuristically we decide to do this if the vector has a "dominant" value,
4166 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004167 unsigned NumElts = VT.getVectorNumElements();
4168 bool isOnlyLowElement = true;
4169 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004170 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004171 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004172
4173 // Map of the number of times a particular SDValue appears in the
4174 // element list.
James Molloy95154342012-09-06 10:32:08 +00004175 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004176 SDValue Value;
4177 for (unsigned i = 0; i < NumElts; ++i) {
4178 SDValue V = Op.getOperand(i);
4179 if (V.getOpcode() == ISD::UNDEF)
4180 continue;
4181 if (i > 0)
4182 isOnlyLowElement = false;
4183 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4184 isConstant = false;
4185
James Molloyba8562a2012-09-06 09:55:02 +00004186 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004187 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004188
4189 // Is this value dominant? (takes up more than half of the lanes)
4190 if (++Count > (NumElts / 2)) {
4191 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004192 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004193 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004194 }
James Molloyba8562a2012-09-06 09:55:02 +00004195 if (ValueCounts.size() != 1)
4196 usesOnlyOneValue = false;
4197 if (!Value.getNode() && ValueCounts.size() > 0)
4198 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004199
James Molloyba8562a2012-09-06 09:55:02 +00004200 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004201 return DAG.getUNDEF(VT);
4202
4203 if (isOnlyLowElement)
4204 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4205
Dale Johannesenf630c712010-07-29 20:10:08 +00004206 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4207
Dale Johannesen575cd142010-10-19 20:00:17 +00004208 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4209 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004210 if (hasDominantValue && EltSize <= 32) {
4211 if (!isConstant) {
4212 SDValue N;
4213
4214 // If we are VDUPing a value that comes directly from a vector, that will
4215 // cause an unnecessary move to and from a GPR, where instead we could
4216 // just use VDUPLANE.
4217 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT)
4218 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4219 Value->getOperand(0), Value->getOperand(1));
4220 else
4221 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4222
4223 if (!usesOnlyOneValue) {
4224 // The dominant value was splatted as 'N', but we now have to insert
4225 // all differing elements.
4226 for (unsigned I = 0; I < NumElts; ++I) {
4227 if (Op.getOperand(I) == Value)
4228 continue;
4229 SmallVector<SDValue, 3> Ops;
4230 Ops.push_back(N);
4231 Ops.push_back(Op.getOperand(I));
4232 Ops.push_back(DAG.getConstant(I, MVT::i32));
4233 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4234 }
4235 }
4236 return N;
4237 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004238 if (VT.getVectorElementType().isFloatingPoint()) {
4239 SmallVector<SDValue, 8> Ops;
4240 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004241 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004242 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004243 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4244 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004245 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4246 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004247 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004248 }
James Molloyba8562a2012-09-06 09:55:02 +00004249 if (usesOnlyOneValue) {
4250 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4251 if (isConstant && Val.getNode())
4252 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4253 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004254 }
4255
4256 // If all elements are constants and the case above didn't get hit, fall back
4257 // to the default expansion, which will generate a load from the constant
4258 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004259 if (isConstant)
4260 return SDValue();
4261
Bob Wilson11a1dff2011-01-07 21:37:30 +00004262 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4263 if (NumElts >= 4) {
4264 SDValue shuffle = ReconstructShuffle(Op, DAG);
4265 if (shuffle != SDValue())
4266 return shuffle;
4267 }
4268
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004269 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004270 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4271 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004272 if (EltSize >= 32) {
4273 // Do the expansion with floating-point types, since that is what the VFP
4274 // registers are defined to use, and since i64 is not legal.
4275 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4276 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004277 SmallVector<SDValue, 8> Ops;
4278 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004279 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004280 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004281 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004282 }
4283
4284 return SDValue();
4285}
4286
Bob Wilson11a1dff2011-01-07 21:37:30 +00004287// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004288// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004289SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4290 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004291 DebugLoc dl = Op.getDebugLoc();
4292 EVT VT = Op.getValueType();
4293 unsigned NumElts = VT.getVectorNumElements();
4294
4295 SmallVector<SDValue, 2> SourceVecs;
4296 SmallVector<unsigned, 2> MinElts;
4297 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004298
Bob Wilson11a1dff2011-01-07 21:37:30 +00004299 for (unsigned i = 0; i < NumElts; ++i) {
4300 SDValue V = Op.getOperand(i);
4301 if (V.getOpcode() == ISD::UNDEF)
4302 continue;
4303 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4304 // A shuffle can only come from building a vector from various
4305 // elements of other vectors.
4306 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004307 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4308 VT.getVectorElementType()) {
4309 // This code doesn't know how to handle shuffles where the vector
4310 // element types do not match (this happens because type legalization
4311 // promotes the return type of EXTRACT_VECTOR_ELT).
4312 // FIXME: It might be appropriate to extend this code to handle
4313 // mismatched types.
4314 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004315 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004316
Bob Wilson11a1dff2011-01-07 21:37:30 +00004317 // Record this extraction against the appropriate vector if possible...
4318 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004319 // If the element number isn't a constant, we can't effectively
4320 // analyze what's going on.
4321 if (!isa<ConstantSDNode>(V.getOperand(1)))
4322 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004323 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4324 bool FoundSource = false;
4325 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4326 if (SourceVecs[j] == SourceVec) {
4327 if (MinElts[j] > EltNo)
4328 MinElts[j] = EltNo;
4329 if (MaxElts[j] < EltNo)
4330 MaxElts[j] = EltNo;
4331 FoundSource = true;
4332 break;
4333 }
4334 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004335
Bob Wilson11a1dff2011-01-07 21:37:30 +00004336 // Or record a new source if not...
4337 if (!FoundSource) {
4338 SourceVecs.push_back(SourceVec);
4339 MinElts.push_back(EltNo);
4340 MaxElts.push_back(EltNo);
4341 }
4342 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004343
Bob Wilson11a1dff2011-01-07 21:37:30 +00004344 // Currently only do something sane when at most two source vectors
4345 // involved.
4346 if (SourceVecs.size() > 2)
4347 return SDValue();
4348
4349 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4350 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004351
Bob Wilson11a1dff2011-01-07 21:37:30 +00004352 // This loop extracts the usage patterns of the source vectors
4353 // and prepares appropriate SDValues for a shuffle if possible.
4354 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4355 if (SourceVecs[i].getValueType() == VT) {
4356 // No VEXT necessary
4357 ShuffleSrcs[i] = SourceVecs[i];
4358 VEXTOffsets[i] = 0;
4359 continue;
4360 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4361 // It probably isn't worth padding out a smaller vector just to
4362 // break it down again in a shuffle.
4363 return SDValue();
4364 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004365
Bob Wilson11a1dff2011-01-07 21:37:30 +00004366 // Since only 64-bit and 128-bit vectors are legal on ARM and
4367 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004368 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4369 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004370
Bob Wilson11a1dff2011-01-07 21:37:30 +00004371 if (MaxElts[i] - MinElts[i] >= NumElts) {
4372 // Span too large for a VEXT to cope
4373 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004374 }
4375
Bob Wilson11a1dff2011-01-07 21:37:30 +00004376 if (MinElts[i] >= NumElts) {
4377 // The extraction can just take the second half
4378 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004379 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4380 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004381 DAG.getIntPtrConstant(NumElts));
4382 } else if (MaxElts[i] < NumElts) {
4383 // The extraction can just take the first half
4384 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004385 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4386 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004387 DAG.getIntPtrConstant(0));
4388 } else {
4389 // An actual VEXT is needed
4390 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004391 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4392 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004393 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004394 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4395 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004396 DAG.getIntPtrConstant(NumElts));
4397 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4398 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4399 }
4400 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004401
Bob Wilson11a1dff2011-01-07 21:37:30 +00004402 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004403
Bob Wilson11a1dff2011-01-07 21:37:30 +00004404 for (unsigned i = 0; i < NumElts; ++i) {
4405 SDValue Entry = Op.getOperand(i);
4406 if (Entry.getOpcode() == ISD::UNDEF) {
4407 Mask.push_back(-1);
4408 continue;
4409 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004410
Bob Wilson11a1dff2011-01-07 21:37:30 +00004411 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004412 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4413 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004414 if (ExtractVec == SourceVecs[0]) {
4415 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4416 } else {
4417 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4418 }
4419 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004420
Bob Wilson11a1dff2011-01-07 21:37:30 +00004421 // Final check before we try to produce nonsense...
4422 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004423 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4424 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004425
Bob Wilson11a1dff2011-01-07 21:37:30 +00004426 return SDValue();
4427}
4428
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004429/// isShuffleMaskLegal - Targets can use this to indicate that they only
4430/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4431/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4432/// are assumed to be legal.
4433bool
4434ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4435 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004436 if (VT.getVectorNumElements() == 4 &&
4437 (VT.is128BitVector() || VT.is64BitVector())) {
4438 unsigned PFIndexes[4];
4439 for (unsigned i = 0; i != 4; ++i) {
4440 if (M[i] < 0)
4441 PFIndexes[i] = 8;
4442 else
4443 PFIndexes[i] = M[i];
4444 }
4445
4446 // Compute the index in the perfect shuffle table.
4447 unsigned PFTableIndex =
4448 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4449 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4450 unsigned Cost = (PFEntry >> 30);
4451
4452 if (Cost <= 4)
4453 return true;
4454 }
4455
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004456 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004457 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004458
Bob Wilson53dd2452010-06-07 23:53:38 +00004459 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4460 return (EltSize >= 32 ||
4461 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004462 isVREVMask(M, VT, 64) ||
4463 isVREVMask(M, VT, 32) ||
4464 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004465 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004466 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004467 isVTRNMask(M, VT, WhichResult) ||
4468 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004469 isVZIPMask(M, VT, WhichResult) ||
4470 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4471 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4472 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004473}
4474
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004475/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4476/// the specified operations to build the shuffle.
4477static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4478 SDValue RHS, SelectionDAG &DAG,
4479 DebugLoc dl) {
4480 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4481 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4482 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4483
4484 enum {
4485 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4486 OP_VREV,
4487 OP_VDUP0,
4488 OP_VDUP1,
4489 OP_VDUP2,
4490 OP_VDUP3,
4491 OP_VEXT1,
4492 OP_VEXT2,
4493 OP_VEXT3,
4494 OP_VUZPL, // VUZP, left result
4495 OP_VUZPR, // VUZP, right result
4496 OP_VZIPL, // VZIP, left result
4497 OP_VZIPR, // VZIP, right result
4498 OP_VTRNL, // VTRN, left result
4499 OP_VTRNR // VTRN, right result
4500 };
4501
4502 if (OpNum == OP_COPY) {
4503 if (LHSID == (1*9+2)*9+3) return LHS;
4504 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4505 return RHS;
4506 }
4507
4508 SDValue OpLHS, OpRHS;
4509 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4510 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4511 EVT VT = OpLHS.getValueType();
4512
4513 switch (OpNum) {
4514 default: llvm_unreachable("Unknown shuffle opcode!");
4515 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004516 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004517 if (VT.getVectorElementType() == MVT::i32 ||
4518 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004519 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4520 // vrev <4 x i16> -> VREV32
4521 if (VT.getVectorElementType() == MVT::i16)
4522 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4523 // vrev <4 x i8> -> VREV16
4524 assert(VT.getVectorElementType() == MVT::i8);
4525 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004526 case OP_VDUP0:
4527 case OP_VDUP1:
4528 case OP_VDUP2:
4529 case OP_VDUP3:
4530 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004531 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004532 case OP_VEXT1:
4533 case OP_VEXT2:
4534 case OP_VEXT3:
4535 return DAG.getNode(ARMISD::VEXT, dl, VT,
4536 OpLHS, OpRHS,
4537 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4538 case OP_VUZPL:
4539 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004540 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004541 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4542 case OP_VZIPL:
4543 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004544 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004545 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4546 case OP_VTRNL:
4547 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004548 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4549 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004550 }
4551}
4552
Bill Wendling69a05a72011-03-14 23:02:38 +00004553static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004554 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004555 SelectionDAG &DAG) {
4556 // Check to see if we can use the VTBL instruction.
4557 SDValue V1 = Op.getOperand(0);
4558 SDValue V2 = Op.getOperand(1);
4559 DebugLoc DL = Op.getDebugLoc();
4560
4561 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004562 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004563 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4564 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4565
4566 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4567 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4568 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4569 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004570
Owen Anderson76706012011-04-05 21:48:57 +00004571 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004572 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4573 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004574}
4575
Bob Wilson5bafff32009-06-22 23:27:02 +00004576static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004577 SDValue V1 = Op.getOperand(0);
4578 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004579 DebugLoc dl = Op.getDebugLoc();
4580 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004581 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004582
Bob Wilson28865062009-08-13 02:13:04 +00004583 // Convert shuffles that are directly supported on NEON to target-specific
4584 // DAG nodes, instead of keeping them as shuffles and matching them again
4585 // during code selection. This is more efficient and avoids the possibility
4586 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004587 // FIXME: floating-point vectors should be canonicalized to integer vectors
4588 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004589 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004590
Bob Wilson53dd2452010-06-07 23:53:38 +00004591 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4592 if (EltSize <= 32) {
4593 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4594 int Lane = SVN->getSplatIndex();
4595 // If this is undef splat, generate it via "just" vdup, if possible.
4596 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004597
Dan Gohman65fd6562011-11-03 21:49:52 +00004598 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004599 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4600 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4601 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004602 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4603 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4604 // reaches it).
4605 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4606 !isa<ConstantSDNode>(V1.getOperand(0))) {
4607 bool IsScalarToVector = true;
4608 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4609 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4610 IsScalarToVector = false;
4611 break;
4612 }
4613 if (IsScalarToVector)
4614 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4615 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004616 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4617 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004618 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004619
4620 bool ReverseVEXT;
4621 unsigned Imm;
4622 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4623 if (ReverseVEXT)
4624 std::swap(V1, V2);
4625 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4626 DAG.getConstant(Imm, MVT::i32));
4627 }
4628
4629 if (isVREVMask(ShuffleMask, VT, 64))
4630 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4631 if (isVREVMask(ShuffleMask, VT, 32))
4632 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4633 if (isVREVMask(ShuffleMask, VT, 16))
4634 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4635
4636 // Check for Neon shuffles that modify both input vectors in place.
4637 // If both results are used, i.e., if there are two shuffles with the same
4638 // source operands and with masks corresponding to both results of one of
4639 // these operations, DAG memoization will ensure that a single node is
4640 // used for both shuffles.
4641 unsigned WhichResult;
4642 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4643 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4644 V1, V2).getValue(WhichResult);
4645 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4646 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4647 V1, V2).getValue(WhichResult);
4648 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4649 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4650 V1, V2).getValue(WhichResult);
4651
4652 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4653 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4654 V1, V1).getValue(WhichResult);
4655 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4656 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4657 V1, V1).getValue(WhichResult);
4658 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4659 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4660 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004661 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004662
Bob Wilsonc692cb72009-08-21 20:54:19 +00004663 // If the shuffle is not directly supported and it has 4 elements, use
4664 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004665 unsigned NumElts = VT.getVectorNumElements();
4666 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004667 unsigned PFIndexes[4];
4668 for (unsigned i = 0; i != 4; ++i) {
4669 if (ShuffleMask[i] < 0)
4670 PFIndexes[i] = 8;
4671 else
4672 PFIndexes[i] = ShuffleMask[i];
4673 }
4674
4675 // Compute the index in the perfect shuffle table.
4676 unsigned PFTableIndex =
4677 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004678 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4679 unsigned Cost = (PFEntry >> 30);
4680
4681 if (Cost <= 4)
4682 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4683 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004684
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004685 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004686 if (EltSize >= 32) {
4687 // Do the expansion with floating-point types, since that is what the VFP
4688 // registers are defined to use, and since i64 is not legal.
4689 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4690 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004691 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4692 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004693 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004694 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004695 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004696 Ops.push_back(DAG.getUNDEF(EltVT));
4697 else
4698 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4699 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4700 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4701 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004702 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004703 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004704 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004705 }
4706
Bill Wendling69a05a72011-03-14 23:02:38 +00004707 if (VT == MVT::v8i8) {
4708 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4709 if (NewOp.getNode())
4710 return NewOp;
4711 }
4712
Bob Wilson22cac0d2009-08-14 05:16:33 +00004713 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004714}
4715
Eli Friedman5c89cb82011-10-24 23:08:52 +00004716static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4717 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4718 SDValue Lane = Op.getOperand(2);
4719 if (!isa<ConstantSDNode>(Lane))
4720 return SDValue();
4721
4722 return Op;
4723}
4724
Bob Wilson5bafff32009-06-22 23:27:02 +00004725static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004726 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004727 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004728 if (!isa<ConstantSDNode>(Lane))
4729 return SDValue();
4730
4731 SDValue Vec = Op.getOperand(0);
4732 if (Op.getValueType() == MVT::i32 &&
4733 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4734 DebugLoc dl = Op.getDebugLoc();
4735 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4736 }
4737
4738 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004739}
4740
Bob Wilsona6d65862009-08-03 20:36:38 +00004741static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4742 // The only time a CONCAT_VECTORS operation can have legal types is when
4743 // two 64-bit vectors are concatenated to a 128-bit vector.
4744 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4745 "unexpected CONCAT_VECTORS");
4746 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004747 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004748 SDValue Op0 = Op.getOperand(0);
4749 SDValue Op1 = Op.getOperand(1);
4750 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004751 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004752 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004753 DAG.getIntPtrConstant(0));
4754 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004755 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004756 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004757 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004758 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004759}
4760
Bob Wilson626613d2010-11-23 19:38:38 +00004761/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4762/// element has been zero/sign-extended, depending on the isSigned parameter,
4763/// from an integer type half its size.
4764static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4765 bool isSigned) {
4766 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4767 EVT VT = N->getValueType(0);
4768 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4769 SDNode *BVN = N->getOperand(0).getNode();
4770 if (BVN->getValueType(0) != MVT::v4i32 ||
4771 BVN->getOpcode() != ISD::BUILD_VECTOR)
4772 return false;
4773 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4774 unsigned HiElt = 1 - LoElt;
4775 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4776 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4777 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4778 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4779 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4780 return false;
4781 if (isSigned) {
4782 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4783 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4784 return true;
4785 } else {
4786 if (Hi0->isNullValue() && Hi1->isNullValue())
4787 return true;
4788 }
4789 return false;
4790 }
4791
4792 if (N->getOpcode() != ISD::BUILD_VECTOR)
4793 return false;
4794
4795 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4796 SDNode *Elt = N->getOperand(i).getNode();
4797 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4798 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4799 unsigned HalfSize = EltSize / 2;
4800 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004801 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004802 return false;
4803 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004804 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004805 return false;
4806 }
4807 continue;
4808 }
4809 return false;
4810 }
4811
4812 return true;
4813}
4814
4815/// isSignExtended - Check if a node is a vector value that is sign-extended
4816/// or a constant BUILD_VECTOR with sign-extended elements.
4817static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4818 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4819 return true;
4820 if (isExtendedBUILD_VECTOR(N, DAG, true))
4821 return true;
4822 return false;
4823}
4824
4825/// isZeroExtended - Check if a node is a vector value that is zero-extended
4826/// or a constant BUILD_VECTOR with zero-extended elements.
4827static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4828 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4829 return true;
4830 if (isExtendedBUILD_VECTOR(N, DAG, false))
4831 return true;
4832 return false;
4833}
4834
4835/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4836/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004837static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4838 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4839 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004840 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4841 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4842 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004843 LD->isNonTemporal(), LD->isInvariant(),
4844 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004845 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4846 // have been legalized as a BITCAST from v4i32.
4847 if (N->getOpcode() == ISD::BITCAST) {
4848 SDNode *BVN = N->getOperand(0).getNode();
4849 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4850 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4851 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4852 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4853 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4854 }
4855 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4856 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4857 EVT VT = N->getValueType(0);
4858 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4859 unsigned NumElts = VT.getVectorNumElements();
4860 MVT TruncVT = MVT::getIntegerVT(EltSize);
4861 SmallVector<SDValue, 8> Ops;
4862 for (unsigned i = 0; i != NumElts; ++i) {
4863 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4864 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004865 // Element types smaller than 32 bits are not legal, so use i32 elements.
4866 // The values are implicitly truncated so sext vs. zext doesn't matter.
4867 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004868 }
4869 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4870 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004871}
4872
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004873static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4874 unsigned Opcode = N->getOpcode();
4875 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4876 SDNode *N0 = N->getOperand(0).getNode();
4877 SDNode *N1 = N->getOperand(1).getNode();
4878 return N0->hasOneUse() && N1->hasOneUse() &&
4879 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4880 }
4881 return false;
4882}
4883
4884static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4885 unsigned Opcode = N->getOpcode();
4886 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4887 SDNode *N0 = N->getOperand(0).getNode();
4888 SDNode *N1 = N->getOperand(1).getNode();
4889 return N0->hasOneUse() && N1->hasOneUse() &&
4890 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4891 }
4892 return false;
4893}
4894
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004895static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4896 // Multiplications are only custom-lowered for 128-bit vectors so that
4897 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4898 EVT VT = Op.getValueType();
4899 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4900 SDNode *N0 = Op.getOperand(0).getNode();
4901 SDNode *N1 = Op.getOperand(1).getNode();
4902 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004903 bool isMLA = false;
4904 bool isN0SExt = isSignExtended(N0, DAG);
4905 bool isN1SExt = isSignExtended(N1, DAG);
4906 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004907 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004908 else {
4909 bool isN0ZExt = isZeroExtended(N0, DAG);
4910 bool isN1ZExt = isZeroExtended(N1, DAG);
4911 if (isN0ZExt && isN1ZExt)
4912 NewOpc = ARMISD::VMULLu;
4913 else if (isN1SExt || isN1ZExt) {
4914 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4915 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4916 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4917 NewOpc = ARMISD::VMULLs;
4918 isMLA = true;
4919 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4920 NewOpc = ARMISD::VMULLu;
4921 isMLA = true;
4922 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4923 std::swap(N0, N1);
4924 NewOpc = ARMISD::VMULLu;
4925 isMLA = true;
4926 }
4927 }
4928
4929 if (!NewOpc) {
4930 if (VT == MVT::v2i64)
4931 // Fall through to expand this. It is not legal.
4932 return SDValue();
4933 else
4934 // Other vector multiplications are legal.
4935 return Op;
4936 }
4937 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004938
4939 // Legalize to a VMULL instruction.
4940 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004941 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004942 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004943 if (!isMLA) {
4944 Op0 = SkipExtension(N0, DAG);
4945 assert(Op0.getValueType().is64BitVector() &&
4946 Op1.getValueType().is64BitVector() &&
4947 "unexpected types for extended operands to VMULL");
4948 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4949 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004950
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004951 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4952 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4953 // vmull q0, d4, d6
4954 // vmlal q0, d5, d6
4955 // is faster than
4956 // vaddl q0, d4, d5
4957 // vmovl q1, d6
4958 // vmul q0, q0, q1
4959 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4960 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4961 EVT Op1VT = Op1.getValueType();
4962 return DAG.getNode(N0->getOpcode(), DL, VT,
4963 DAG.getNode(NewOpc, DL, VT,
4964 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4965 DAG.getNode(NewOpc, DL, VT,
4966 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004967}
4968
Owen Anderson76706012011-04-05 21:48:57 +00004969static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004970LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4971 // Convert to float
4972 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4973 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4974 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4975 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4976 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4977 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4978 // Get reciprocal estimate.
4979 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004980 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004981 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4982 // Because char has a smaller range than uchar, we can actually get away
4983 // without any newton steps. This requires that we use a weird bias
4984 // of 0xb000, however (again, this has been exhaustively tested).
4985 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4986 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4987 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4988 Y = DAG.getConstant(0xb000, MVT::i32);
4989 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4990 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4991 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4992 // Convert back to short.
4993 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4994 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4995 return X;
4996}
4997
Owen Anderson76706012011-04-05 21:48:57 +00004998static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004999LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5000 SDValue N2;
5001 // Convert to float.
5002 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5003 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5004 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5005 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5006 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5007 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005008
Nate Begeman7973f352011-02-11 20:53:29 +00005009 // Use reciprocal estimate and one refinement step.
5010 // float4 recip = vrecpeq_f32(yf);
5011 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005012 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005013 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005014 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005015 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5016 N1, N2);
5017 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5018 // Because short has a smaller range than ushort, we can actually get away
5019 // with only a single newton step. This requires that we use a weird bias
5020 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005021 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005022 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5023 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005024 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005025 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5026 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5027 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5028 // Convert back to integer and return.
5029 // return vmovn_s32(vcvt_s32_f32(result));
5030 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5031 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5032 return N0;
5033}
5034
5035static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5036 EVT VT = Op.getValueType();
5037 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5038 "unexpected type for custom-lowering ISD::SDIV");
5039
5040 DebugLoc dl = Op.getDebugLoc();
5041 SDValue N0 = Op.getOperand(0);
5042 SDValue N1 = Op.getOperand(1);
5043 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005044
Nate Begeman7973f352011-02-11 20:53:29 +00005045 if (VT == MVT::v8i8) {
5046 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5047 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005048
Nate Begeman7973f352011-02-11 20:53:29 +00005049 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5050 DAG.getIntPtrConstant(4));
5051 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005052 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005053 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5054 DAG.getIntPtrConstant(0));
5055 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5056 DAG.getIntPtrConstant(0));
5057
5058 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5059 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5060
5061 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5062 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005063
Nate Begeman7973f352011-02-11 20:53:29 +00005064 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5065 return N0;
5066 }
5067 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5068}
5069
5070static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5071 EVT VT = Op.getValueType();
5072 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5073 "unexpected type for custom-lowering ISD::UDIV");
5074
5075 DebugLoc dl = Op.getDebugLoc();
5076 SDValue N0 = Op.getOperand(0);
5077 SDValue N1 = Op.getOperand(1);
5078 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005079
Nate Begeman7973f352011-02-11 20:53:29 +00005080 if (VT == MVT::v8i8) {
5081 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5082 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005083
Nate Begeman7973f352011-02-11 20:53:29 +00005084 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5085 DAG.getIntPtrConstant(4));
5086 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005087 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005088 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5089 DAG.getIntPtrConstant(0));
5090 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5091 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005092
Nate Begeman7973f352011-02-11 20:53:29 +00005093 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5094 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005095
Nate Begeman7973f352011-02-11 20:53:29 +00005096 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5097 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005098
5099 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005100 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5101 N0);
5102 return N0;
5103 }
Owen Anderson76706012011-04-05 21:48:57 +00005104
Nate Begeman7973f352011-02-11 20:53:29 +00005105 // v4i16 sdiv ... Convert to float.
5106 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5107 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5108 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5109 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5110 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005111 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005112
5113 // Use reciprocal estimate and two refinement steps.
5114 // float4 recip = vrecpeq_f32(yf);
5115 // recip *= vrecpsq_f32(yf, recip);
5116 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005117 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005118 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005119 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005120 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005121 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005122 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005123 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005124 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005125 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005126 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5127 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5128 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5129 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005130 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005131 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5132 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5133 N1 = DAG.getConstant(2, MVT::i32);
5134 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5135 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5136 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5137 // Convert back to integer and return.
5138 // return vmovn_u32(vcvt_s32_f32(result));
5139 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5140 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5141 return N0;
5142}
5143
Evan Cheng342e3162011-08-30 01:34:54 +00005144static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5145 EVT VT = Op.getNode()->getValueType(0);
5146 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5147
5148 unsigned Opc;
5149 bool ExtraOp = false;
5150 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005151 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005152 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5153 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5154 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5155 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5156 }
5157
5158 if (!ExtraOp)
5159 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5160 Op.getOperand(1));
5161 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5162 Op.getOperand(1), Op.getOperand(2));
5163}
5164
Eli Friedman74bf18c2011-09-15 22:26:18 +00005165static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005166 // Monotonic load/store is legal for all targets
5167 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5168 return Op;
5169
5170 // Aquire/Release load/store is not legal for targets without a
5171 // dmb or equivalent available.
5172 return SDValue();
5173}
5174
5175
Eli Friedman2bdffe42011-08-31 00:31:29 +00005176static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005177ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5178 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005179 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005180 assert (Node->getValueType(0) == MVT::i64 &&
5181 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005182
Eli Friedman4d3f3292011-08-31 17:52:22 +00005183 SmallVector<SDValue, 6> Ops;
5184 Ops.push_back(Node->getOperand(0)); // Chain
5185 Ops.push_back(Node->getOperand(1)); // Ptr
5186 // Low part of Val1
5187 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5188 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5189 // High part of Val1
5190 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5191 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005192 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005193 // High part of Val1
5194 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5195 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5196 // High part of Val2
5197 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5198 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5199 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005200 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5201 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005202 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005203 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005204 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005205 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5206 Results.push_back(Result.getValue(2));
5207}
5208
Dan Gohmand858e902010-04-17 15:26:15 +00005209SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005210 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005211 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005212 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005213 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005214 case ISD::GlobalAddress:
5215 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5216 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005217 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005218 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005219 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5220 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005221 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005222 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005223 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005224 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005225 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005226 case ISD::SINT_TO_FP:
5227 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5228 case ISD::FP_TO_SINT:
5229 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005230 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005231 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005232 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005233 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005234 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005235 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005236 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5237 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005238 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005239 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005240 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005241 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005242 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005243 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005244 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005245 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005246 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005247 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005248 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005249 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005250 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005251 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005252 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005253 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005254 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005255 case ISD::SDIV: return LowerSDIV(Op, DAG);
5256 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005257 case ISD::ADDC:
5258 case ISD::ADDE:
5259 case ISD::SUBC:
5260 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005261 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005262 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005263 }
Evan Chenga8e29892007-01-19 07:51:42 +00005264}
5265
Duncan Sands1607f052008-12-01 11:39:25 +00005266/// ReplaceNodeResults - Replace the results of node with an illegal result
5267/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005268void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5269 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005270 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005271 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005272 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005273 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005274 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005275 case ISD::BITCAST:
5276 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005277 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005278 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005279 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005280 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005281 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005282 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005283 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005284 return;
5285 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005286 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005287 return;
5288 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005289 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005290 return;
5291 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005292 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005293 return;
5294 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005295 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005296 return;
5297 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005298 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005299 return;
5300 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005301 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005302 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005303 case ISD::ATOMIC_CMP_SWAP:
5304 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5305 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005306 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005307 if (Res.getNode())
5308 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005309}
Chris Lattner27a6c732007-11-24 07:07:01 +00005310
Evan Chenga8e29892007-01-19 07:51:42 +00005311//===----------------------------------------------------------------------===//
5312// ARM Scheduler Hooks
5313//===----------------------------------------------------------------------===//
5314
5315MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005316ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5317 MachineBasicBlock *BB,
5318 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005319 unsigned dest = MI->getOperand(0).getReg();
5320 unsigned ptr = MI->getOperand(1).getReg();
5321 unsigned oldval = MI->getOperand(2).getReg();
5322 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005323 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5324 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005325 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005326
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005327 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005328 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5329 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5330 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005331
5332 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005333 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5334 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5335 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005336 }
5337
Jim Grosbach5278eb82009-12-11 01:42:04 +00005338 unsigned ldrOpc, strOpc;
5339 switch (Size) {
5340 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005341 case 1:
5342 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005343 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005344 break;
5345 case 2:
5346 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5347 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5348 break;
5349 case 4:
5350 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5351 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5352 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005353 }
5354
5355 MachineFunction *MF = BB->getParent();
5356 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5357 MachineFunction::iterator It = BB;
5358 ++It; // insert the new blocks after the current block
5359
5360 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5361 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5362 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5363 MF->insert(It, loop1MBB);
5364 MF->insert(It, loop2MBB);
5365 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005366
5367 // Transfer the remainder of BB and its successor edges to exitMBB.
5368 exitMBB->splice(exitMBB->begin(), BB,
5369 llvm::next(MachineBasicBlock::iterator(MI)),
5370 BB->end());
5371 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005372
5373 // thisMBB:
5374 // ...
5375 // fallthrough --> loop1MBB
5376 BB->addSuccessor(loop1MBB);
5377
5378 // loop1MBB:
5379 // ldrex dest, [ptr]
5380 // cmp dest, oldval
5381 // bne exitMBB
5382 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005383 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5384 if (ldrOpc == ARM::t2LDREX)
5385 MIB.addImm(0);
5386 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005387 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005388 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005389 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5390 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005391 BB->addSuccessor(loop2MBB);
5392 BB->addSuccessor(exitMBB);
5393
5394 // loop2MBB:
5395 // strex scratch, newval, [ptr]
5396 // cmp scratch, #0
5397 // bne loop1MBB
5398 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005399 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5400 if (strOpc == ARM::t2STREX)
5401 MIB.addImm(0);
5402 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005403 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005404 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005405 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5406 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005407 BB->addSuccessor(loop1MBB);
5408 BB->addSuccessor(exitMBB);
5409
5410 // exitMBB:
5411 // ...
5412 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005413
Dan Gohman14152b42010-07-06 20:24:04 +00005414 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005415
Jim Grosbach5278eb82009-12-11 01:42:04 +00005416 return BB;
5417}
5418
5419MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005420ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5421 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005422 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5423 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5424
5425 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005426 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005427 MachineFunction::iterator It = BB;
5428 ++It;
5429
5430 unsigned dest = MI->getOperand(0).getReg();
5431 unsigned ptr = MI->getOperand(1).getReg();
5432 unsigned incr = MI->getOperand(2).getReg();
5433 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005434 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005435
5436 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5437 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005438 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5439 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005440 }
5441
Jim Grosbachc3c23542009-12-14 04:22:04 +00005442 unsigned ldrOpc, strOpc;
5443 switch (Size) {
5444 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005445 case 1:
5446 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005447 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005448 break;
5449 case 2:
5450 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5451 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5452 break;
5453 case 4:
5454 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5455 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5456 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005457 }
5458
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005459 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5460 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5461 MF->insert(It, loopMBB);
5462 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005463
5464 // Transfer the remainder of BB and its successor edges to exitMBB.
5465 exitMBB->splice(exitMBB->begin(), BB,
5466 llvm::next(MachineBasicBlock::iterator(MI)),
5467 BB->end());
5468 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005469
Craig Topper420761a2012-04-20 07:30:17 +00005470 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005471 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005472 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005473 unsigned scratch = MRI.createVirtualRegister(TRC);
5474 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005475
5476 // thisMBB:
5477 // ...
5478 // fallthrough --> loopMBB
5479 BB->addSuccessor(loopMBB);
5480
5481 // loopMBB:
5482 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005483 // <binop> scratch2, dest, incr
5484 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005485 // cmp scratch, #0
5486 // bne- loopMBB
5487 // fallthrough --> exitMBB
5488 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005489 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5490 if (ldrOpc == ARM::t2LDREX)
5491 MIB.addImm(0);
5492 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005493 if (BinOpcode) {
5494 // operand order needs to go the other way for NAND
5495 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5496 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5497 addReg(incr).addReg(dest)).addReg(0);
5498 else
5499 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5500 addReg(dest).addReg(incr)).addReg(0);
5501 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005502
Jim Grosbachb6aed502011-09-09 18:37:27 +00005503 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5504 if (strOpc == ARM::t2STREX)
5505 MIB.addImm(0);
5506 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005507 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005508 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005509 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5510 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005511
5512 BB->addSuccessor(loopMBB);
5513 BB->addSuccessor(exitMBB);
5514
5515 // exitMBB:
5516 // ...
5517 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005518
Dan Gohman14152b42010-07-06 20:24:04 +00005519 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005520
Jim Grosbachc3c23542009-12-14 04:22:04 +00005521 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005522}
5523
Jim Grosbachf7da8822011-04-26 19:44:18 +00005524MachineBasicBlock *
5525ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5526 MachineBasicBlock *BB,
5527 unsigned Size,
5528 bool signExtend,
5529 ARMCC::CondCodes Cond) const {
5530 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5531
5532 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5533 MachineFunction *MF = BB->getParent();
5534 MachineFunction::iterator It = BB;
5535 ++It;
5536
5537 unsigned dest = MI->getOperand(0).getReg();
5538 unsigned ptr = MI->getOperand(1).getReg();
5539 unsigned incr = MI->getOperand(2).getReg();
5540 unsigned oldval = dest;
5541 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005542 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005543
5544 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5545 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005546 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5547 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005548 }
5549
Jim Grosbachf7da8822011-04-26 19:44:18 +00005550 unsigned ldrOpc, strOpc, extendOpc;
5551 switch (Size) {
5552 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5553 case 1:
5554 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5555 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005556 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005557 break;
5558 case 2:
5559 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5560 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005561 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005562 break;
5563 case 4:
5564 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5565 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5566 extendOpc = 0;
5567 break;
5568 }
5569
5570 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5571 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5572 MF->insert(It, loopMBB);
5573 MF->insert(It, exitMBB);
5574
5575 // Transfer the remainder of BB and its successor edges to exitMBB.
5576 exitMBB->splice(exitMBB->begin(), BB,
5577 llvm::next(MachineBasicBlock::iterator(MI)),
5578 BB->end());
5579 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5580
Craig Topper420761a2012-04-20 07:30:17 +00005581 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005582 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005583 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005584 unsigned scratch = MRI.createVirtualRegister(TRC);
5585 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005586
5587 // thisMBB:
5588 // ...
5589 // fallthrough --> loopMBB
5590 BB->addSuccessor(loopMBB);
5591
5592 // loopMBB:
5593 // ldrex dest, ptr
5594 // (sign extend dest, if required)
5595 // cmp dest, incr
5596 // cmov.cond scratch2, dest, incr
5597 // strex scratch, scratch2, ptr
5598 // cmp scratch, #0
5599 // bne- loopMBB
5600 // fallthrough --> exitMBB
5601 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005602 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5603 if (ldrOpc == ARM::t2LDREX)
5604 MIB.addImm(0);
5605 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005606
5607 // Sign extend the value, if necessary.
5608 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005609 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005610 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5611 .addReg(dest)
5612 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005613 }
5614
5615 // Build compare and cmov instructions.
5616 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5617 .addReg(oldval).addReg(incr));
5618 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5619 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5620
Jim Grosbachb6aed502011-09-09 18:37:27 +00005621 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5622 if (strOpc == ARM::t2STREX)
5623 MIB.addImm(0);
5624 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005625 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5626 .addReg(scratch).addImm(0));
5627 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5628 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5629
5630 BB->addSuccessor(loopMBB);
5631 BB->addSuccessor(exitMBB);
5632
5633 // exitMBB:
5634 // ...
5635 BB = exitMBB;
5636
5637 MI->eraseFromParent(); // The instruction is gone now.
5638
5639 return BB;
5640}
5641
Eli Friedman2bdffe42011-08-31 00:31:29 +00005642MachineBasicBlock *
5643ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5644 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005645 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005646 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5647 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5648
5649 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5650 MachineFunction *MF = BB->getParent();
5651 MachineFunction::iterator It = BB;
5652 ++It;
5653
5654 unsigned destlo = MI->getOperand(0).getReg();
5655 unsigned desthi = MI->getOperand(1).getReg();
5656 unsigned ptr = MI->getOperand(2).getReg();
5657 unsigned vallo = MI->getOperand(3).getReg();
5658 unsigned valhi = MI->getOperand(4).getReg();
5659 DebugLoc dl = MI->getDebugLoc();
5660 bool isThumb2 = Subtarget->isThumb2();
5661
5662 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5663 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005664 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5665 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5666 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005667 }
5668
5669 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5670 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5671
5672 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005673 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005674 if (IsCmpxchg) {
5675 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5676 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5677 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005678 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5679 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005680 if (IsCmpxchg) {
5681 MF->insert(It, contBB);
5682 MF->insert(It, cont2BB);
5683 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005684 MF->insert(It, exitMBB);
5685
5686 // Transfer the remainder of BB and its successor edges to exitMBB.
5687 exitMBB->splice(exitMBB->begin(), BB,
5688 llvm::next(MachineBasicBlock::iterator(MI)),
5689 BB->end());
5690 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5691
Craig Topper420761a2012-04-20 07:30:17 +00005692 const TargetRegisterClass *TRC = isThumb2 ?
5693 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5694 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005695 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5696
5697 // thisMBB:
5698 // ...
5699 // fallthrough --> loopMBB
5700 BB->addSuccessor(loopMBB);
5701
5702 // loopMBB:
5703 // ldrexd r2, r3, ptr
5704 // <binopa> r0, r2, incr
5705 // <binopb> r1, r3, incr
5706 // strexd storesuccess, r0, r1, ptr
5707 // cmp storesuccess, #0
5708 // bne- loopMBB
5709 // fallthrough --> exitMBB
5710 //
5711 // Note that the registers are explicitly specified because there is not any
5712 // way to force the register allocator to allocate a register pair.
5713 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005714 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005715 // need to properly enforce the restriction that the two output registers
5716 // for ldrexd must be different.
5717 BB = loopMBB;
5718 // Load
5719 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5720 .addReg(ARM::R2, RegState::Define)
5721 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5722 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5723 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5724 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005725
5726 if (IsCmpxchg) {
5727 // Add early exit
5728 for (unsigned i = 0; i < 2; i++) {
5729 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5730 ARM::CMPrr))
5731 .addReg(i == 0 ? destlo : desthi)
5732 .addReg(i == 0 ? vallo : valhi));
5733 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5734 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5735 BB->addSuccessor(exitMBB);
5736 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5737 BB = (i == 0 ? contBB : cont2BB);
5738 }
5739
5740 // Copy to physregs for strexd
5741 unsigned setlo = MI->getOperand(5).getReg();
5742 unsigned sethi = MI->getOperand(6).getReg();
5743 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5744 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5745 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005746 // Perform binary operation
5747 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5748 .addReg(destlo).addReg(vallo))
5749 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5750 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5751 .addReg(desthi).addReg(valhi)).addReg(0);
5752 } else {
5753 // Copy to physregs for strexd
5754 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5755 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5756 }
5757
5758 // Store
5759 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5760 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5761 // Cmp+jump
5762 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5763 .addReg(storesuccess).addImm(0));
5764 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5765 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5766
5767 BB->addSuccessor(loopMBB);
5768 BB->addSuccessor(exitMBB);
5769
5770 // exitMBB:
5771 // ...
5772 BB = exitMBB;
5773
5774 MI->eraseFromParent(); // The instruction is gone now.
5775
5776 return BB;
5777}
5778
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005779/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5780/// registers the function context.
5781void ARMTargetLowering::
5782SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5783 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005784 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5785 DebugLoc dl = MI->getDebugLoc();
5786 MachineFunction *MF = MBB->getParent();
5787 MachineRegisterInfo *MRI = &MF->getRegInfo();
5788 MachineConstantPool *MCP = MF->getConstantPool();
5789 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5790 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005791
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005792 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005793 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005794
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005795 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005796 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005797 ARMConstantPoolValue *CPV =
5798 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5799 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5800
Craig Topper420761a2012-04-20 07:30:17 +00005801 const TargetRegisterClass *TRC = isThumb ?
5802 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5803 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005804
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005805 // Grab constant pool and fixed stack memory operands.
5806 MachineMemOperand *CPMMO =
5807 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5808 MachineMemOperand::MOLoad, 4, 4);
5809
5810 MachineMemOperand *FIMMOSt =
5811 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5812 MachineMemOperand::MOStore, 4, 4);
5813
5814 // Load the address of the dispatch MBB into the jump buffer.
5815 if (isThumb2) {
5816 // Incoming value: jbuf
5817 // ldr.n r5, LCPI1_1
5818 // orr r5, r5, #1
5819 // add r5, pc
5820 // str r5, [$jbuf, #+4] ; &jbuf[1]
5821 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5822 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5823 .addConstantPoolIndex(CPI)
5824 .addMemOperand(CPMMO));
5825 // Set the low bit because of thumb mode.
5826 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5827 AddDefaultCC(
5828 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5829 .addReg(NewVReg1, RegState::Kill)
5830 .addImm(0x01)));
5831 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5832 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5833 .addReg(NewVReg2, RegState::Kill)
5834 .addImm(PCLabelId);
5835 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5836 .addReg(NewVReg3, RegState::Kill)
5837 .addFrameIndex(FI)
5838 .addImm(36) // &jbuf[1] :: pc
5839 .addMemOperand(FIMMOSt));
5840 } else if (isThumb) {
5841 // Incoming value: jbuf
5842 // ldr.n r1, LCPI1_4
5843 // add r1, pc
5844 // mov r2, #1
5845 // orrs r1, r2
5846 // add r2, $jbuf, #+4 ; &jbuf[1]
5847 // str r1, [r2]
5848 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5849 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5850 .addConstantPoolIndex(CPI)
5851 .addMemOperand(CPMMO));
5852 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5853 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5854 .addReg(NewVReg1, RegState::Kill)
5855 .addImm(PCLabelId);
5856 // Set the low bit because of thumb mode.
5857 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5858 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5859 .addReg(ARM::CPSR, RegState::Define)
5860 .addImm(1));
5861 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5862 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5863 .addReg(ARM::CPSR, RegState::Define)
5864 .addReg(NewVReg2, RegState::Kill)
5865 .addReg(NewVReg3, RegState::Kill));
5866 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5867 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5868 .addFrameIndex(FI)
5869 .addImm(36)); // &jbuf[1] :: pc
5870 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5871 .addReg(NewVReg4, RegState::Kill)
5872 .addReg(NewVReg5, RegState::Kill)
5873 .addImm(0)
5874 .addMemOperand(FIMMOSt));
5875 } else {
5876 // Incoming value: jbuf
5877 // ldr r1, LCPI1_1
5878 // add r1, pc, r1
5879 // str r1, [$jbuf, #+4] ; &jbuf[1]
5880 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5881 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5882 .addConstantPoolIndex(CPI)
5883 .addImm(0)
5884 .addMemOperand(CPMMO));
5885 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5886 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5887 .addReg(NewVReg1, RegState::Kill)
5888 .addImm(PCLabelId));
5889 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5890 .addReg(NewVReg2, RegState::Kill)
5891 .addFrameIndex(FI)
5892 .addImm(36) // &jbuf[1] :: pc
5893 .addMemOperand(FIMMOSt));
5894 }
5895}
5896
5897MachineBasicBlock *ARMTargetLowering::
5898EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5899 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5900 DebugLoc dl = MI->getDebugLoc();
5901 MachineFunction *MF = MBB->getParent();
5902 MachineRegisterInfo *MRI = &MF->getRegInfo();
5903 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5904 MachineFrameInfo *MFI = MF->getFrameInfo();
5905 int FI = MFI->getFunctionContextIndex();
5906
Craig Topper420761a2012-04-20 07:30:17 +00005907 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5908 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005909 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005910
Bill Wendling04f15b42011-10-06 21:29:56 +00005911 // Get a mapping of the call site numbers to all of the landing pads they're
5912 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005913 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5914 unsigned MaxCSNum = 0;
5915 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005916 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5917 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005918 if (!BB->isLandingPad()) continue;
5919
5920 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5921 // pad.
5922 for (MachineBasicBlock::iterator
5923 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5924 if (!II->isEHLabel()) continue;
5925
5926 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005927 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005928
Bill Wendling5cbef192011-10-05 23:28:57 +00005929 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5930 for (SmallVectorImpl<unsigned>::iterator
5931 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5932 CSI != CSE; ++CSI) {
5933 CallSiteNumToLPad[*CSI].push_back(BB);
5934 MaxCSNum = std::max(MaxCSNum, *CSI);
5935 }
Bill Wendling2a850152011-10-05 00:02:33 +00005936 break;
5937 }
5938 }
5939
5940 // Get an ordered list of the machine basic blocks for the jump table.
5941 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005942 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005943 LPadList.reserve(CallSiteNumToLPad.size());
5944 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5945 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5946 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005947 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005948 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005949 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5950 }
Bill Wendling2a850152011-10-05 00:02:33 +00005951 }
5952
Bill Wendling5cbef192011-10-05 23:28:57 +00005953 assert(!LPadList.empty() &&
5954 "No landing pad destinations for the dispatch jump table!");
5955
Bill Wendling04f15b42011-10-06 21:29:56 +00005956 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005957 MachineJumpTableInfo *JTI =
5958 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5959 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5960 unsigned UId = AFI->createJumpTableUId();
5961
Bill Wendling04f15b42011-10-06 21:29:56 +00005962 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005963
5964 // Shove the dispatch's address into the return slot in the function context.
5965 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5966 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005967
Bill Wendlingbb734682011-10-05 00:39:32 +00005968 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005969 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005970 DispatchBB->addSuccessor(TrapBB);
5971
5972 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5973 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005974
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005975 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005976 MF->insert(MF->end(), DispatchBB);
5977 MF->insert(MF->end(), DispContBB);
5978 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005979
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005980 // Insert code into the entry block that creates and registers the function
5981 // context.
5982 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5983
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005984 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005985 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005986 MachineMemOperand::MOLoad |
5987 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005988
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005989 if (AFI->isThumb1OnlyFunction())
5990 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5991 else if (!Subtarget->hasVFP2())
5992 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005993 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005994 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005995
Bill Wendling952cb502011-10-18 22:49:07 +00005996 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005997 if (Subtarget->isThumb2()) {
5998 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5999 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6000 .addFrameIndex(FI)
6001 .addImm(4)
6002 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006003
Bill Wendling952cb502011-10-18 22:49:07 +00006004 if (NumLPads < 256) {
6005 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6006 .addReg(NewVReg1)
6007 .addImm(LPadList.size()));
6008 } else {
6009 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6010 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006011 .addImm(NumLPads & 0xFFFF));
6012
6013 unsigned VReg2 = VReg1;
6014 if ((NumLPads & 0xFFFF0000) != 0) {
6015 VReg2 = MRI->createVirtualRegister(TRC);
6016 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6017 .addReg(VReg1)
6018 .addImm(NumLPads >> 16));
6019 }
6020
Bill Wendling952cb502011-10-18 22:49:07 +00006021 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6022 .addReg(NewVReg1)
6023 .addReg(VReg2));
6024 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006025
Bill Wendling95ce2e92011-10-06 22:53:00 +00006026 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6027 .addMBB(TrapBB)
6028 .addImm(ARMCC::HI)
6029 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006030
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006031 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6032 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006033 .addJumpTableIndex(MJTI)
6034 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006035
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006036 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006037 AddDefaultCC(
6038 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006039 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6040 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006041 .addReg(NewVReg1)
6042 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6043
6044 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006045 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006046 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006047 .addJumpTableIndex(MJTI)
6048 .addImm(UId);
6049 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006050 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6051 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6052 .addFrameIndex(FI)
6053 .addImm(1)
6054 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006055
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006056 if (NumLPads < 256) {
6057 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6058 .addReg(NewVReg1)
6059 .addImm(NumLPads));
6060 } else {
6061 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006062 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6063 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6064
6065 // MachineConstantPool wants an explicit alignment.
6066 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6067 if (Align == 0)
6068 Align = getTargetData()->getTypeAllocSize(C->getType());
6069 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006070
6071 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6072 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6073 .addReg(VReg1, RegState::Define)
6074 .addConstantPoolIndex(Idx));
6075 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6076 .addReg(NewVReg1)
6077 .addReg(VReg1));
6078 }
6079
Bill Wendling083a8eb2011-10-06 23:37:36 +00006080 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6081 .addMBB(TrapBB)
6082 .addImm(ARMCC::HI)
6083 .addReg(ARM::CPSR);
6084
6085 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6086 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6087 .addReg(ARM::CPSR, RegState::Define)
6088 .addReg(NewVReg1)
6089 .addImm(2));
6090
6091 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006092 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006093 .addJumpTableIndex(MJTI)
6094 .addImm(UId));
6095
6096 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6097 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6098 .addReg(ARM::CPSR, RegState::Define)
6099 .addReg(NewVReg2, RegState::Kill)
6100 .addReg(NewVReg3));
6101
6102 MachineMemOperand *JTMMOLd =
6103 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6104 MachineMemOperand::MOLoad, 4, 4);
6105
6106 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6107 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6108 .addReg(NewVReg4, RegState::Kill)
6109 .addImm(0)
6110 .addMemOperand(JTMMOLd));
6111
6112 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6113 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6114 .addReg(ARM::CPSR, RegState::Define)
6115 .addReg(NewVReg5, RegState::Kill)
6116 .addReg(NewVReg3));
6117
6118 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6119 .addReg(NewVReg6, RegState::Kill)
6120 .addJumpTableIndex(MJTI)
6121 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006122 } else {
6123 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6124 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6125 .addFrameIndex(FI)
6126 .addImm(4)
6127 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006128
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006129 if (NumLPads < 256) {
6130 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6131 .addReg(NewVReg1)
6132 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006133 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006134 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6135 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006136 .addImm(NumLPads & 0xFFFF));
6137
6138 unsigned VReg2 = VReg1;
6139 if ((NumLPads & 0xFFFF0000) != 0) {
6140 VReg2 = MRI->createVirtualRegister(TRC);
6141 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6142 .addReg(VReg1)
6143 .addImm(NumLPads >> 16));
6144 }
6145
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006146 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6147 .addReg(NewVReg1)
6148 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006149 } else {
6150 MachineConstantPool *ConstantPool = MF->getConstantPool();
6151 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6152 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6153
6154 // MachineConstantPool wants an explicit alignment.
6155 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6156 if (Align == 0)
6157 Align = getTargetData()->getTypeAllocSize(C->getType());
6158 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6159
6160 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6161 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6162 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006163 .addConstantPoolIndex(Idx)
6164 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006165 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6166 .addReg(NewVReg1)
6167 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006168 }
6169
Bill Wendling95ce2e92011-10-06 22:53:00 +00006170 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6171 .addMBB(TrapBB)
6172 .addImm(ARMCC::HI)
6173 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006174
Bill Wendling564392b2011-10-18 22:11:18 +00006175 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006176 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006177 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006178 .addReg(NewVReg1)
6179 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006180 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6181 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006182 .addJumpTableIndex(MJTI)
6183 .addImm(UId));
6184
6185 MachineMemOperand *JTMMOLd =
6186 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6187 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006188 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006189 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006190 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6191 .addReg(NewVReg3, RegState::Kill)
6192 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006193 .addImm(0)
6194 .addMemOperand(JTMMOLd));
6195
6196 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006197 .addReg(NewVReg5, RegState::Kill)
6198 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006199 .addJumpTableIndex(MJTI)
6200 .addImm(UId);
6201 }
Bill Wendling2a850152011-10-05 00:02:33 +00006202
Bill Wendlingbb734682011-10-05 00:39:32 +00006203 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006204 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006205 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006206 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6207 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006208 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006209 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006210 }
6211
Bill Wendling24bb9252011-10-17 05:25:09 +00006212 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006213 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6214 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006215 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006216 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006217 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6218 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6219 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006220
6221 // Remove the landing pad successor from the invoke block and replace it
6222 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006223 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6224 BB->succ_end());
6225 while (!Successors.empty()) {
6226 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006227 if (SMBB->isLandingPad()) {
6228 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006229 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006230 }
6231 }
6232
6233 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006234
6235 // Find the invoke call and mark all of the callee-saved registers as
6236 // 'implicit defined' so that they're spilled. This prevents code from
6237 // moving instructions to before the EH block, where they will never be
6238 // executed.
6239 for (MachineBasicBlock::reverse_iterator
6240 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006241 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006242
6243 DenseMap<unsigned, bool> DefRegs;
6244 for (MachineInstr::mop_iterator
6245 OI = II->operands_begin(), OE = II->operands_end();
6246 OI != OE; ++OI) {
6247 if (!OI->isReg()) continue;
6248 DefRegs[OI->getReg()] = true;
6249 }
6250
6251 MachineInstrBuilder MIB(&*II);
6252
Bill Wendling5d798592011-10-14 23:55:44 +00006253 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006254 unsigned Reg = SavedRegs[i];
6255 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006256 !ARM::tGPRRegClass.contains(Reg) &&
6257 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006258 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006259 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006260 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006261 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006262 continue;
6263 if (!DefRegs[Reg])
6264 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006265 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006266
6267 break;
6268 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006269 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006270
Bill Wendlingf7b02072011-10-18 18:30:49 +00006271 // Mark all former landing pads as non-landing pads. The dispatch is the only
6272 // landing pad now.
6273 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6274 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6275 (*I)->setIsLandingPad(false);
6276
Bill Wendlingbb734682011-10-05 00:39:32 +00006277 // The instruction is gone now.
6278 MI->eraseFromParent();
6279
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006280 return MBB;
6281}
6282
Evan Cheng218977b2010-07-13 19:27:42 +00006283static
6284MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6285 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6286 E = MBB->succ_end(); I != E; ++I)
6287 if (*I != Succ)
6288 return *I;
6289 llvm_unreachable("Expecting a BB with two successors!");
6290}
6291
Manman Ren68f25572012-06-01 19:33:18 +00006292MachineBasicBlock *ARMTargetLowering::
6293EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6294 // This pseudo instruction has 3 operands: dst, src, size
6295 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6296 // Otherwise, we will generate unrolled scalar copies.
6297 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6298 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6299 MachineFunction::iterator It = BB;
6300 ++It;
6301
6302 unsigned dest = MI->getOperand(0).getReg();
6303 unsigned src = MI->getOperand(1).getReg();
6304 unsigned SizeVal = MI->getOperand(2).getImm();
6305 unsigned Align = MI->getOperand(3).getImm();
6306 DebugLoc dl = MI->getDebugLoc();
6307
6308 bool isThumb2 = Subtarget->isThumb2();
6309 MachineFunction *MF = BB->getParent();
6310 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006311 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006312
6313 const TargetRegisterClass *TRC = isThumb2 ?
6314 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6315 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006316 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006317
6318 if (Align & 1) {
6319 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6320 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6321 UnitSize = 1;
6322 } else if (Align & 2) {
6323 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6324 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6325 UnitSize = 2;
6326 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006327 // Check whether we can use NEON instructions.
6328 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6329 Subtarget->hasNEON()) {
6330 if ((Align % 16 == 0) && SizeVal >= 16) {
6331 ldrOpc = ARM::VLD1q32wb_fixed;
6332 strOpc = ARM::VST1q32wb_fixed;
6333 UnitSize = 16;
6334 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6335 }
6336 else if ((Align % 8 == 0) && SizeVal >= 8) {
6337 ldrOpc = ARM::VLD1d32wb_fixed;
6338 strOpc = ARM::VST1d32wb_fixed;
6339 UnitSize = 8;
6340 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6341 }
6342 }
6343 // Can't use NEON instructions.
6344 if (UnitSize == 0) {
6345 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6346 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6347 UnitSize = 4;
6348 }
Manman Ren68f25572012-06-01 19:33:18 +00006349 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006350
Manman Ren68f25572012-06-01 19:33:18 +00006351 unsigned BytesLeft = SizeVal % UnitSize;
6352 unsigned LoopSize = SizeVal - BytesLeft;
6353
6354 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6355 // Use LDR and STR to copy.
6356 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6357 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6358 unsigned srcIn = src;
6359 unsigned destIn = dest;
6360 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006361 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006362 unsigned srcOut = MRI.createVirtualRegister(TRC);
6363 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006364 if (UnitSize >= 8) {
6365 AddDefaultPred(BuildMI(*BB, MI, dl,
6366 TII->get(ldrOpc), scratch)
6367 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6368
6369 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6370 .addReg(destIn).addImm(0).addReg(scratch));
6371 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006372 AddDefaultPred(BuildMI(*BB, MI, dl,
6373 TII->get(ldrOpc), scratch)
6374 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6375
6376 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6377 .addReg(scratch).addReg(destIn)
6378 .addImm(UnitSize));
6379 } else {
6380 AddDefaultPred(BuildMI(*BB, MI, dl,
6381 TII->get(ldrOpc), scratch)
6382 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6383 .addImm(UnitSize));
6384
6385 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6386 .addReg(scratch).addReg(destIn)
6387 .addReg(0).addImm(UnitSize));
6388 }
6389 srcIn = srcOut;
6390 destIn = destOut;
6391 }
6392
6393 // Handle the leftover bytes with LDRB and STRB.
6394 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6395 // [destOut] = STRB_POST(scratch, destIn, 1)
6396 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6397 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6398 for (unsigned i = 0; i < BytesLeft; i++) {
6399 unsigned scratch = MRI.createVirtualRegister(TRC);
6400 unsigned srcOut = MRI.createVirtualRegister(TRC);
6401 unsigned destOut = MRI.createVirtualRegister(TRC);
6402 if (isThumb2) {
6403 AddDefaultPred(BuildMI(*BB, MI, dl,
6404 TII->get(ldrOpc),scratch)
6405 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6406
6407 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6408 .addReg(scratch).addReg(destIn)
6409 .addReg(0).addImm(1));
6410 } else {
6411 AddDefaultPred(BuildMI(*BB, MI, dl,
6412 TII->get(ldrOpc),scratch)
6413 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6414
6415 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6416 .addReg(scratch).addReg(destIn)
6417 .addReg(0).addImm(1));
6418 }
6419 srcIn = srcOut;
6420 destIn = destOut;
6421 }
6422 MI->eraseFromParent(); // The instruction is gone now.
6423 return BB;
6424 }
6425
6426 // Expand the pseudo op to a loop.
6427 // thisMBB:
6428 // ...
6429 // movw varEnd, # --> with thumb2
6430 // movt varEnd, #
6431 // ldrcp varEnd, idx --> without thumb2
6432 // fallthrough --> loopMBB
6433 // loopMBB:
6434 // PHI varPhi, varEnd, varLoop
6435 // PHI srcPhi, src, srcLoop
6436 // PHI destPhi, dst, destLoop
6437 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6438 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6439 // subs varLoop, varPhi, #UnitSize
6440 // bne loopMBB
6441 // fallthrough --> exitMBB
6442 // exitMBB:
6443 // epilogue to handle left-over bytes
6444 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6445 // [destOut] = STRB_POST(scratch, destLoop, 1)
6446 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6447 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6448 MF->insert(It, loopMBB);
6449 MF->insert(It, exitMBB);
6450
6451 // Transfer the remainder of BB and its successor edges to exitMBB.
6452 exitMBB->splice(exitMBB->begin(), BB,
6453 llvm::next(MachineBasicBlock::iterator(MI)),
6454 BB->end());
6455 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6456
6457 // Load an immediate to varEnd.
6458 unsigned varEnd = MRI.createVirtualRegister(TRC);
6459 if (isThumb2) {
6460 unsigned VReg1 = varEnd;
6461 if ((LoopSize & 0xFFFF0000) != 0)
6462 VReg1 = MRI.createVirtualRegister(TRC);
6463 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6464 .addImm(LoopSize & 0xFFFF));
6465
6466 if ((LoopSize & 0xFFFF0000) != 0)
6467 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6468 .addReg(VReg1)
6469 .addImm(LoopSize >> 16));
6470 } else {
6471 MachineConstantPool *ConstantPool = MF->getConstantPool();
6472 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6473 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6474
6475 // MachineConstantPool wants an explicit alignment.
6476 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6477 if (Align == 0)
6478 Align = getTargetData()->getTypeAllocSize(C->getType());
6479 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6480
6481 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6482 .addReg(varEnd, RegState::Define)
6483 .addConstantPoolIndex(Idx)
6484 .addImm(0));
6485 }
6486 BB->addSuccessor(loopMBB);
6487
6488 // Generate the loop body:
6489 // varPhi = PHI(varLoop, varEnd)
6490 // srcPhi = PHI(srcLoop, src)
6491 // destPhi = PHI(destLoop, dst)
6492 MachineBasicBlock *entryBB = BB;
6493 BB = loopMBB;
6494 unsigned varLoop = MRI.createVirtualRegister(TRC);
6495 unsigned varPhi = MRI.createVirtualRegister(TRC);
6496 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6497 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6498 unsigned destLoop = MRI.createVirtualRegister(TRC);
6499 unsigned destPhi = MRI.createVirtualRegister(TRC);
6500
6501 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6502 .addReg(varLoop).addMBB(loopMBB)
6503 .addReg(varEnd).addMBB(entryBB);
6504 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6505 .addReg(srcLoop).addMBB(loopMBB)
6506 .addReg(src).addMBB(entryBB);
6507 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6508 .addReg(destLoop).addMBB(loopMBB)
6509 .addReg(dest).addMBB(entryBB);
6510
6511 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6512 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006513 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6514 if (UnitSize >= 8) {
6515 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6516 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6517
6518 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6519 .addReg(destPhi).addImm(0).addReg(scratch));
6520 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006521 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6522 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6523
6524 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6525 .addReg(scratch).addReg(destPhi)
6526 .addImm(UnitSize));
6527 } else {
6528 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6529 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6530 .addImm(UnitSize));
6531
6532 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6533 .addReg(scratch).addReg(destPhi)
6534 .addReg(0).addImm(UnitSize));
6535 }
6536
6537 // Decrement loop variable by UnitSize.
6538 MachineInstrBuilder MIB = BuildMI(BB, dl,
6539 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6540 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6541 MIB->getOperand(5).setReg(ARM::CPSR);
6542 MIB->getOperand(5).setIsDef(true);
6543
6544 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6545 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6546
6547 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6548 BB->addSuccessor(loopMBB);
6549 BB->addSuccessor(exitMBB);
6550
6551 // Add epilogue to handle BytesLeft.
6552 BB = exitMBB;
6553 MachineInstr *StartOfExit = exitMBB->begin();
6554 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6555 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6556
6557 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6558 // [destOut] = STRB_POST(scratch, destLoop, 1)
6559 unsigned srcIn = srcLoop;
6560 unsigned destIn = destLoop;
6561 for (unsigned i = 0; i < BytesLeft; i++) {
6562 unsigned scratch = MRI.createVirtualRegister(TRC);
6563 unsigned srcOut = MRI.createVirtualRegister(TRC);
6564 unsigned destOut = MRI.createVirtualRegister(TRC);
6565 if (isThumb2) {
6566 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6567 TII->get(ldrOpc),scratch)
6568 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6569
6570 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6571 .addReg(scratch).addReg(destIn)
6572 .addImm(1));
6573 } else {
6574 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6575 TII->get(ldrOpc),scratch)
6576 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6577
6578 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6579 .addReg(scratch).addReg(destIn)
6580 .addReg(0).addImm(1));
6581 }
6582 srcIn = srcOut;
6583 destIn = destOut;
6584 }
6585
6586 MI->eraseFromParent(); // The instruction is gone now.
6587 return BB;
6588}
6589
Jim Grosbache801dc42009-12-12 01:40:06 +00006590MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006591ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006592 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006593 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006594 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006595 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006596 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006597 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006598 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006599 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006600 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006601 // The Thumb2 pre-indexed stores have the same MI operands, they just
6602 // define them differently in the .td files from the isel patterns, so
6603 // they need pseudos.
6604 case ARM::t2STR_preidx:
6605 MI->setDesc(TII->get(ARM::t2STR_PRE));
6606 return BB;
6607 case ARM::t2STRB_preidx:
6608 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6609 return BB;
6610 case ARM::t2STRH_preidx:
6611 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6612 return BB;
6613
Jim Grosbach19dec202011-08-05 20:35:44 +00006614 case ARM::STRi_preidx:
6615 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006616 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006617 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6618 // Decode the offset.
6619 unsigned Offset = MI->getOperand(4).getImm();
6620 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6621 Offset = ARM_AM::getAM2Offset(Offset);
6622 if (isSub)
6623 Offset = -Offset;
6624
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006625 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006626 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006627 .addOperand(MI->getOperand(0)) // Rn_wb
6628 .addOperand(MI->getOperand(1)) // Rt
6629 .addOperand(MI->getOperand(2)) // Rn
6630 .addImm(Offset) // offset (skip GPR==zero_reg)
6631 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006632 .addOperand(MI->getOperand(6))
6633 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006634 MI->eraseFromParent();
6635 return BB;
6636 }
6637 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006638 case ARM::STRBr_preidx:
6639 case ARM::STRH_preidx: {
6640 unsigned NewOpc;
6641 switch (MI->getOpcode()) {
6642 default: llvm_unreachable("unexpected opcode!");
6643 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6644 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6645 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6646 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006647 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6648 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6649 MIB.addOperand(MI->getOperand(i));
6650 MI->eraseFromParent();
6651 return BB;
6652 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006653 case ARM::ATOMIC_LOAD_ADD_I8:
6654 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6655 case ARM::ATOMIC_LOAD_ADD_I16:
6656 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6657 case ARM::ATOMIC_LOAD_ADD_I32:
6658 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006659
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006660 case ARM::ATOMIC_LOAD_AND_I8:
6661 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6662 case ARM::ATOMIC_LOAD_AND_I16:
6663 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6664 case ARM::ATOMIC_LOAD_AND_I32:
6665 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006666
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006667 case ARM::ATOMIC_LOAD_OR_I8:
6668 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6669 case ARM::ATOMIC_LOAD_OR_I16:
6670 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6671 case ARM::ATOMIC_LOAD_OR_I32:
6672 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006673
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006674 case ARM::ATOMIC_LOAD_XOR_I8:
6675 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6676 case ARM::ATOMIC_LOAD_XOR_I16:
6677 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6678 case ARM::ATOMIC_LOAD_XOR_I32:
6679 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006680
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006681 case ARM::ATOMIC_LOAD_NAND_I8:
6682 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6683 case ARM::ATOMIC_LOAD_NAND_I16:
6684 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6685 case ARM::ATOMIC_LOAD_NAND_I32:
6686 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006687
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006688 case ARM::ATOMIC_LOAD_SUB_I8:
6689 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6690 case ARM::ATOMIC_LOAD_SUB_I16:
6691 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6692 case ARM::ATOMIC_LOAD_SUB_I32:
6693 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006694
Jim Grosbachf7da8822011-04-26 19:44:18 +00006695 case ARM::ATOMIC_LOAD_MIN_I8:
6696 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6697 case ARM::ATOMIC_LOAD_MIN_I16:
6698 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6699 case ARM::ATOMIC_LOAD_MIN_I32:
6700 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6701
6702 case ARM::ATOMIC_LOAD_MAX_I8:
6703 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6704 case ARM::ATOMIC_LOAD_MAX_I16:
6705 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6706 case ARM::ATOMIC_LOAD_MAX_I32:
6707 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6708
6709 case ARM::ATOMIC_LOAD_UMIN_I8:
6710 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6711 case ARM::ATOMIC_LOAD_UMIN_I16:
6712 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6713 case ARM::ATOMIC_LOAD_UMIN_I32:
6714 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6715
6716 case ARM::ATOMIC_LOAD_UMAX_I8:
6717 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6718 case ARM::ATOMIC_LOAD_UMAX_I16:
6719 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6720 case ARM::ATOMIC_LOAD_UMAX_I32:
6721 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6722
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006723 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6724 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6725 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006726
6727 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6728 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6729 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006730
Eli Friedman2bdffe42011-08-31 00:31:29 +00006731
6732 case ARM::ATOMADD6432:
6733 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006734 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6735 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006736 case ARM::ATOMSUB6432:
6737 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006738 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6739 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006740 case ARM::ATOMOR6432:
6741 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006742 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006743 case ARM::ATOMXOR6432:
6744 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006745 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006746 case ARM::ATOMAND6432:
6747 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006748 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006749 case ARM::ATOMSWAP6432:
6750 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006751 case ARM::ATOMCMPXCHG6432:
6752 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6753 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6754 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006755
Evan Cheng007ea272009-08-12 05:17:19 +00006756 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006757 // To "insert" a SELECT_CC instruction, we actually have to insert the
6758 // diamond control-flow pattern. The incoming instruction knows the
6759 // destination vreg to set, the condition code register to branch on, the
6760 // true/false values to select between, and a branch opcode to use.
6761 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006762 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006763 ++It;
6764
6765 // thisMBB:
6766 // ...
6767 // TrueVal = ...
6768 // cmpTY ccX, r1, r2
6769 // bCC copy1MBB
6770 // fallthrough --> copy0MBB
6771 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006772 MachineFunction *F = BB->getParent();
6773 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6774 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006775 F->insert(It, copy0MBB);
6776 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006777
6778 // Transfer the remainder of BB and its successor edges to sinkMBB.
6779 sinkMBB->splice(sinkMBB->begin(), BB,
6780 llvm::next(MachineBasicBlock::iterator(MI)),
6781 BB->end());
6782 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6783
Dan Gohman258c58c2010-07-06 15:49:48 +00006784 BB->addSuccessor(copy0MBB);
6785 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006786
Dan Gohman14152b42010-07-06 20:24:04 +00006787 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6788 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6789
Evan Chenga8e29892007-01-19 07:51:42 +00006790 // copy0MBB:
6791 // %FalseValue = ...
6792 // # fallthrough to sinkMBB
6793 BB = copy0MBB;
6794
6795 // Update machine-CFG edges
6796 BB->addSuccessor(sinkMBB);
6797
6798 // sinkMBB:
6799 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6800 // ...
6801 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006802 BuildMI(*BB, BB->begin(), dl,
6803 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006804 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6805 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6806
Dan Gohman14152b42010-07-06 20:24:04 +00006807 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006808 return BB;
6809 }
Evan Cheng86198642009-08-07 00:34:42 +00006810
Evan Cheng218977b2010-07-13 19:27:42 +00006811 case ARM::BCCi64:
6812 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006813 // If there is an unconditional branch to the other successor, remove it.
6814 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006815
Evan Cheng218977b2010-07-13 19:27:42 +00006816 // Compare both parts that make up the double comparison separately for
6817 // equality.
6818 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6819
6820 unsigned LHS1 = MI->getOperand(1).getReg();
6821 unsigned LHS2 = MI->getOperand(2).getReg();
6822 if (RHSisZero) {
6823 AddDefaultPred(BuildMI(BB, dl,
6824 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6825 .addReg(LHS1).addImm(0));
6826 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6827 .addReg(LHS2).addImm(0)
6828 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6829 } else {
6830 unsigned RHS1 = MI->getOperand(3).getReg();
6831 unsigned RHS2 = MI->getOperand(4).getReg();
6832 AddDefaultPred(BuildMI(BB, dl,
6833 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6834 .addReg(LHS1).addReg(RHS1));
6835 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6836 .addReg(LHS2).addReg(RHS2)
6837 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6838 }
6839
6840 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6841 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6842 if (MI->getOperand(0).getImm() == ARMCC::NE)
6843 std::swap(destMBB, exitMBB);
6844
6845 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6846 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006847 if (isThumb2)
6848 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6849 else
6850 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006851
6852 MI->eraseFromParent(); // The pseudo instruction is gone now.
6853 return BB;
6854 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006855
Bill Wendling5bc85282011-10-17 20:37:20 +00006856 case ARM::Int_eh_sjlj_setjmp:
6857 case ARM::Int_eh_sjlj_setjmp_nofp:
6858 case ARM::tInt_eh_sjlj_setjmp:
6859 case ARM::t2Int_eh_sjlj_setjmp:
6860 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6861 EmitSjLjDispatchBlock(MI, BB);
6862 return BB;
6863
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006864 case ARM::ABS:
6865 case ARM::t2ABS: {
6866 // To insert an ABS instruction, we have to insert the
6867 // diamond control-flow pattern. The incoming instruction knows the
6868 // source vreg to test against 0, the destination vreg to set,
6869 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006870 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006871 // It transforms
6872 // V1 = ABS V0
6873 // into
6874 // V2 = MOVS V0
6875 // BCC (branch to SinkBB if V0 >= 0)
6876 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006877 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006878 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6879 MachineFunction::iterator BBI = BB;
6880 ++BBI;
6881 MachineFunction *Fn = BB->getParent();
6882 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6883 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6884 Fn->insert(BBI, RSBBB);
6885 Fn->insert(BBI, SinkBB);
6886
6887 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6888 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6889 bool isThumb2 = Subtarget->isThumb2();
6890 MachineRegisterInfo &MRI = Fn->getRegInfo();
6891 // In Thumb mode S must not be specified if source register is the SP or
6892 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006893 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6894 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6895 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006896
6897 // Transfer the remainder of BB and its successor edges to sinkMBB.
6898 SinkBB->splice(SinkBB->begin(), BB,
6899 llvm::next(MachineBasicBlock::iterator(MI)),
6900 BB->end());
6901 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6902
6903 BB->addSuccessor(RSBBB);
6904 BB->addSuccessor(SinkBB);
6905
6906 // fall through to SinkMBB
6907 RSBBB->addSuccessor(SinkBB);
6908
Manman Ren307473d2012-06-15 21:32:12 +00006909 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006910 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006911 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6912 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006913
6914 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006915 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006916 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6917 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6918
6919 // insert rsbri in RSBBB
6920 // Note: BCC and rsbri will be converted into predicated rsbmi
6921 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006922 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006923 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006924 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006925 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6926
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006927 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006928 // reuse ABSDstReg to not change uses of ABS instruction
6929 BuildMI(*SinkBB, SinkBB->begin(), dl,
6930 TII->get(ARM::PHI), ABSDstReg)
6931 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006932 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006933
6934 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006935 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006936
6937 // return last added BB
6938 return SinkBB;
6939 }
Manman Ren68f25572012-06-01 19:33:18 +00006940 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006941 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006942 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006943 }
6944}
6945
Evan Cheng37fefc22011-08-30 19:09:48 +00006946void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6947 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006948 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006949 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6950 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6951 return;
6952 }
6953
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006954 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006955 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6956 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6957 // operand is still set to noreg. If needed, set the optional operand's
6958 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006959 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006960 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006961
Andrew Trick3be654f2011-09-21 02:20:46 +00006962 // Rename pseudo opcodes.
6963 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6964 if (NewOpc) {
6965 const ARMBaseInstrInfo *TII =
6966 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006967 MCID = &TII->get(NewOpc);
6968
6969 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6970 "converted opcode should be the same except for cc_out");
6971
6972 MI->setDesc(*MCID);
6973
6974 // Add the optional cc_out operand
6975 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006976 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006977 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006978
6979 // Any ARM instruction that sets the 's' bit should specify an optional
6980 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006981 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006982 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006983 return;
6984 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006985 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6986 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006987 bool definesCPSR = false;
6988 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006989 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006990 i != e; ++i) {
6991 const MachineOperand &MO = MI->getOperand(i);
6992 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6993 definesCPSR = true;
6994 if (MO.isDead())
6995 deadCPSR = true;
6996 MI->RemoveOperand(i);
6997 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006998 }
6999 }
Andrew Trick4815d562011-09-20 03:17:40 +00007000 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007001 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007002 return;
7003 }
7004 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007005 if (deadCPSR) {
7006 assert(!MI->getOperand(ccOutIdx).getReg() &&
7007 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007008 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007009 }
Andrew Trick4815d562011-09-20 03:17:40 +00007010
Andrew Trick3be654f2011-09-21 02:20:46 +00007011 // If this instruction was defined with an optional CPSR def and its dag node
7012 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007013 MachineOperand &MO = MI->getOperand(ccOutIdx);
7014 MO.setReg(ARM::CPSR);
7015 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007016}
7017
Evan Chenga8e29892007-01-19 07:51:42 +00007018//===----------------------------------------------------------------------===//
7019// ARM Optimization Hooks
7020//===----------------------------------------------------------------------===//
7021
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007022// Helper function that checks if N is a null or all ones constant.
7023static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7024 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7025 if (!C)
7026 return false;
7027 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7028}
7029
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007030// Return true if N is conditionally 0 or all ones.
7031// Detects these expressions where cc is an i1 value:
7032//
7033// (select cc 0, y) [AllOnes=0]
7034// (select cc y, 0) [AllOnes=0]
7035// (zext cc) [AllOnes=0]
7036// (sext cc) [AllOnes=0/1]
7037// (select cc -1, y) [AllOnes=1]
7038// (select cc y, -1) [AllOnes=1]
7039//
7040// Invert is set when N is the null/all ones constant when CC is false.
7041// OtherOp is set to the alternative value of N.
7042static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7043 SDValue &CC, bool &Invert,
7044 SDValue &OtherOp,
7045 SelectionDAG &DAG) {
7046 switch (N->getOpcode()) {
7047 default: return false;
7048 case ISD::SELECT: {
7049 CC = N->getOperand(0);
7050 SDValue N1 = N->getOperand(1);
7051 SDValue N2 = N->getOperand(2);
7052 if (isZeroOrAllOnes(N1, AllOnes)) {
7053 Invert = false;
7054 OtherOp = N2;
7055 return true;
7056 }
7057 if (isZeroOrAllOnes(N2, AllOnes)) {
7058 Invert = true;
7059 OtherOp = N1;
7060 return true;
7061 }
7062 return false;
7063 }
7064 case ISD::ZERO_EXTEND:
7065 // (zext cc) can never be the all ones value.
7066 if (AllOnes)
7067 return false;
7068 // Fall through.
7069 case ISD::SIGN_EXTEND: {
7070 EVT VT = N->getValueType(0);
7071 CC = N->getOperand(0);
7072 if (CC.getValueType() != MVT::i1)
7073 return false;
7074 Invert = !AllOnes;
7075 if (AllOnes)
7076 // When looking for an AllOnes constant, N is an sext, and the 'other'
7077 // value is 0.
7078 OtherOp = DAG.getConstant(0, VT);
7079 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7080 // When looking for a 0 constant, N can be zext or sext.
7081 OtherOp = DAG.getConstant(1, VT);
7082 else
7083 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7084 return true;
7085 }
7086 }
7087}
7088
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007089// Combine a constant select operand into its use:
7090//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007091// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7092// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7093// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7094// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7095// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007096//
7097// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007098// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007099//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007100// Also recognize sext/zext from i1:
7101//
7102// (add (zext cc), x) -> (select cc (add x, 1), x)
7103// (add (sext cc), x) -> (select cc (add x, -1), x)
7104//
7105// These transformations eventually create predicated instructions.
7106//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007107// @param N The node to transform.
7108// @param Slct The N operand that is a select.
7109// @param OtherOp The other N operand (x above).
7110// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007111// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007112// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007113static
7114SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007115 TargetLowering::DAGCombinerInfo &DCI,
7116 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007117 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007118 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007119 SDValue NonConstantVal;
7120 SDValue CCOp;
7121 bool SwapSelectOps;
7122 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7123 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007124 return SDValue();
7125
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007126 // Slct is now know to be the desired identity constant when CC is true.
7127 SDValue TrueVal = OtherOp;
7128 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7129 OtherOp, NonConstantVal);
7130 // Unless SwapSelectOps says CC should be false.
7131 if (SwapSelectOps)
7132 std::swap(TrueVal, FalseVal);
7133
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007134 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007135 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007136}
7137
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007138// Attempt combineSelectAndUse on each operand of a commutative operator N.
7139static
7140SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7141 TargetLowering::DAGCombinerInfo &DCI) {
7142 SDValue N0 = N->getOperand(0);
7143 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007144 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007145 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7146 if (Result.getNode())
7147 return Result;
7148 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007149 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007150 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7151 if (Result.getNode())
7152 return Result;
7153 }
7154 return SDValue();
7155}
7156
Eric Christopherfa6f5912011-06-29 21:10:36 +00007157// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007158// (only after legalization).
7159static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7160 TargetLowering::DAGCombinerInfo &DCI,
7161 const ARMSubtarget *Subtarget) {
7162
7163 // Only perform optimization if after legalize, and if NEON is available. We
7164 // also expected both operands to be BUILD_VECTORs.
7165 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7166 || N0.getOpcode() != ISD::BUILD_VECTOR
7167 || N1.getOpcode() != ISD::BUILD_VECTOR)
7168 return SDValue();
7169
7170 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7171 EVT VT = N->getValueType(0);
7172 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7173 return SDValue();
7174
7175 // Check that the vector operands are of the right form.
7176 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7177 // operands, where N is the size of the formed vector.
7178 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7179 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007180
7181 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007182 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007183 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007184 SDValue Vec = N0->getOperand(0)->getOperand(0);
7185 SDNode *V = Vec.getNode();
7186 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007187
Eric Christopherfa6f5912011-06-29 21:10:36 +00007188 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007189 // check to see if each of their operands are an EXTRACT_VECTOR with
7190 // the same vector and appropriate index.
7191 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7192 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7193 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007194
Tanya Lattner189531f2011-06-14 23:48:48 +00007195 SDValue ExtVec0 = N0->getOperand(i);
7196 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007197
Tanya Lattner189531f2011-06-14 23:48:48 +00007198 // First operand is the vector, verify its the same.
7199 if (V != ExtVec0->getOperand(0).getNode() ||
7200 V != ExtVec1->getOperand(0).getNode())
7201 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007202
Tanya Lattner189531f2011-06-14 23:48:48 +00007203 // Second is the constant, verify its correct.
7204 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7205 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007206
Tanya Lattner189531f2011-06-14 23:48:48 +00007207 // For the constant, we want to see all the even or all the odd.
7208 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7209 || C1->getZExtValue() != nextIndex+1)
7210 return SDValue();
7211
7212 // Increment index.
7213 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007214 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007215 return SDValue();
7216 }
7217
7218 // Create VPADDL node.
7219 SelectionDAG &DAG = DCI.DAG;
7220 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007221
7222 // Build operand list.
7223 SmallVector<SDValue, 8> Ops;
7224 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7225 TLI.getPointerTy()));
7226
7227 // Input is the vector.
7228 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007229
Tanya Lattner189531f2011-06-14 23:48:48 +00007230 // Get widened type and narrowed type.
7231 MVT widenType;
7232 unsigned numElem = VT.getVectorNumElements();
7233 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7234 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7235 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7236 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7237 default:
Craig Topperbc219812012-02-07 02:50:20 +00007238 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007239 }
7240
7241 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7242 widenType, &Ops[0], Ops.size());
7243 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7244}
7245
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007246static SDValue findMUL_LOHI(SDValue V) {
7247 if (V->getOpcode() == ISD::UMUL_LOHI ||
7248 V->getOpcode() == ISD::SMUL_LOHI)
7249 return V;
7250 return SDValue();
7251}
7252
7253static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7254 TargetLowering::DAGCombinerInfo &DCI,
7255 const ARMSubtarget *Subtarget) {
7256
7257 if (Subtarget->isThumb1Only()) return SDValue();
7258
7259 // Only perform the checks after legalize when the pattern is available.
7260 if (DCI.isBeforeLegalize()) return SDValue();
7261
7262 // Look for multiply add opportunities.
7263 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7264 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7265 // a glue link from the first add to the second add.
7266 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7267 // a S/UMLAL instruction.
7268 // loAdd UMUL_LOHI
7269 // \ / :lo \ :hi
7270 // \ / \ [no multiline comment]
7271 // ADDC | hiAdd
7272 // \ :glue / /
7273 // \ / /
7274 // ADDE
7275 //
7276 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7277 SDValue AddcOp0 = AddcNode->getOperand(0);
7278 SDValue AddcOp1 = AddcNode->getOperand(1);
7279
7280 // Check if the two operands are from the same mul_lohi node.
7281 if (AddcOp0.getNode() == AddcOp1.getNode())
7282 return SDValue();
7283
7284 assert(AddcNode->getNumValues() == 2 &&
7285 AddcNode->getValueType(0) == MVT::i32 &&
7286 AddcNode->getValueType(1) == MVT::Glue &&
7287 "Expect ADDC with two result values: i32, glue");
7288
7289 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7290 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7291 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7292 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7293 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7294 return SDValue();
7295
7296 // Look for the glued ADDE.
7297 SDNode* AddeNode = AddcNode->getGluedUser();
7298 if (AddeNode == NULL)
7299 return SDValue();
7300
7301 // Make sure it is really an ADDE.
7302 if (AddeNode->getOpcode() != ISD::ADDE)
7303 return SDValue();
7304
7305 assert(AddeNode->getNumOperands() == 3 &&
7306 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7307 "ADDE node has the wrong inputs");
7308
7309 // Check for the triangle shape.
7310 SDValue AddeOp0 = AddeNode->getOperand(0);
7311 SDValue AddeOp1 = AddeNode->getOperand(1);
7312
7313 // Make sure that the ADDE operands are not coming from the same node.
7314 if (AddeOp0.getNode() == AddeOp1.getNode())
7315 return SDValue();
7316
7317 // Find the MUL_LOHI node walking up ADDE's operands.
7318 bool IsLeftOperandMUL = false;
7319 SDValue MULOp = findMUL_LOHI(AddeOp0);
7320 if (MULOp == SDValue())
7321 MULOp = findMUL_LOHI(AddeOp1);
7322 else
7323 IsLeftOperandMUL = true;
7324 if (MULOp == SDValue())
7325 return SDValue();
7326
7327 // Figure out the right opcode.
7328 unsigned Opc = MULOp->getOpcode();
7329 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7330
7331 // Figure out the high and low input values to the MLAL node.
7332 SDValue* HiMul = &MULOp;
7333 SDValue* HiAdd = NULL;
7334 SDValue* LoMul = NULL;
7335 SDValue* LowAdd = NULL;
7336
7337 if (IsLeftOperandMUL)
7338 HiAdd = &AddeOp1;
7339 else
7340 HiAdd = &AddeOp0;
7341
7342
7343 if (AddcOp0->getOpcode() == Opc) {
7344 LoMul = &AddcOp0;
7345 LowAdd = &AddcOp1;
7346 }
7347 if (AddcOp1->getOpcode() == Opc) {
7348 LoMul = &AddcOp1;
7349 LowAdd = &AddcOp0;
7350 }
7351
7352 if (LoMul == NULL)
7353 return SDValue();
7354
7355 if (LoMul->getNode() != HiMul->getNode())
7356 return SDValue();
7357
7358 // Create the merged node.
7359 SelectionDAG &DAG = DCI.DAG;
7360
7361 // Build operand list.
7362 SmallVector<SDValue, 8> Ops;
7363 Ops.push_back(LoMul->getOperand(0));
7364 Ops.push_back(LoMul->getOperand(1));
7365 Ops.push_back(*LowAdd);
7366 Ops.push_back(*HiAdd);
7367
7368 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7369 DAG.getVTList(MVT::i32, MVT::i32),
7370 &Ops[0], Ops.size());
7371
7372 // Replace the ADDs' nodes uses by the MLA node's values.
7373 SDValue HiMLALResult(MLALNode.getNode(), 1);
7374 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7375
7376 SDValue LoMLALResult(MLALNode.getNode(), 0);
7377 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7378
7379 // Return original node to notify the driver to stop replacing.
7380 SDValue resNode(AddcNode, 0);
7381 return resNode;
7382}
7383
7384/// PerformADDCCombine - Target-specific dag combine transform from
7385/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7386static SDValue PerformADDCCombine(SDNode *N,
7387 TargetLowering::DAGCombinerInfo &DCI,
7388 const ARMSubtarget *Subtarget) {
7389
7390 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7391
7392}
7393
Bob Wilson3d5792a2010-07-29 20:34:14 +00007394/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7395/// operands N0 and N1. This is a helper for PerformADDCombine that is
7396/// called with the default operands, and if that fails, with commuted
7397/// operands.
7398static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007399 TargetLowering::DAGCombinerInfo &DCI,
7400 const ARMSubtarget *Subtarget){
7401
7402 // Attempt to create vpaddl for this add.
7403 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7404 if (Result.getNode())
7405 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007406
Chris Lattnerd1980a52009-03-12 06:52:53 +00007407 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007408 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007409 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7410 if (Result.getNode()) return Result;
7411 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007412 return SDValue();
7413}
7414
Bob Wilson3d5792a2010-07-29 20:34:14 +00007415/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7416///
7417static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007418 TargetLowering::DAGCombinerInfo &DCI,
7419 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007420 SDValue N0 = N->getOperand(0);
7421 SDValue N1 = N->getOperand(1);
7422
7423 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007424 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007425 if (Result.getNode())
7426 return Result;
7427
7428 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007429 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007430}
7431
Chris Lattnerd1980a52009-03-12 06:52:53 +00007432/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007433///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007434static SDValue PerformSUBCombine(SDNode *N,
7435 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007436 SDValue N0 = N->getOperand(0);
7437 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007438
Chris Lattnerd1980a52009-03-12 06:52:53 +00007439 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007440 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007441 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7442 if (Result.getNode()) return Result;
7443 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007444
Chris Lattnerd1980a52009-03-12 06:52:53 +00007445 return SDValue();
7446}
7447
Evan Cheng463d3582011-03-31 19:38:48 +00007448/// PerformVMULCombine
7449/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7450/// special multiplier accumulator forwarding.
7451/// vmul d3, d0, d2
7452/// vmla d3, d1, d2
7453/// is faster than
7454/// vadd d3, d0, d1
7455/// vmul d3, d3, d2
7456static SDValue PerformVMULCombine(SDNode *N,
7457 TargetLowering::DAGCombinerInfo &DCI,
7458 const ARMSubtarget *Subtarget) {
7459 if (!Subtarget->hasVMLxForwarding())
7460 return SDValue();
7461
7462 SelectionDAG &DAG = DCI.DAG;
7463 SDValue N0 = N->getOperand(0);
7464 SDValue N1 = N->getOperand(1);
7465 unsigned Opcode = N0.getOpcode();
7466 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7467 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007468 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007469 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7470 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7471 return SDValue();
7472 std::swap(N0, N1);
7473 }
7474
7475 EVT VT = N->getValueType(0);
7476 DebugLoc DL = N->getDebugLoc();
7477 SDValue N00 = N0->getOperand(0);
7478 SDValue N01 = N0->getOperand(1);
7479 return DAG.getNode(Opcode, DL, VT,
7480 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7481 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7482}
7483
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007484static SDValue PerformMULCombine(SDNode *N,
7485 TargetLowering::DAGCombinerInfo &DCI,
7486 const ARMSubtarget *Subtarget) {
7487 SelectionDAG &DAG = DCI.DAG;
7488
7489 if (Subtarget->isThumb1Only())
7490 return SDValue();
7491
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007492 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7493 return SDValue();
7494
7495 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007496 if (VT.is64BitVector() || VT.is128BitVector())
7497 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007498 if (VT != MVT::i32)
7499 return SDValue();
7500
7501 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7502 if (!C)
7503 return SDValue();
7504
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007505 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007506 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007507
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007508 ShiftAmt = ShiftAmt & (32 - 1);
7509 SDValue V = N->getOperand(0);
7510 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007511
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007512 SDValue Res;
7513 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007514
7515 if (MulAmt >= 0) {
7516 if (isPowerOf2_32(MulAmt - 1)) {
7517 // (mul x, 2^N + 1) => (add (shl x, N), x)
7518 Res = DAG.getNode(ISD::ADD, DL, VT,
7519 V,
7520 DAG.getNode(ISD::SHL, DL, VT,
7521 V,
7522 DAG.getConstant(Log2_32(MulAmt - 1),
7523 MVT::i32)));
7524 } else if (isPowerOf2_32(MulAmt + 1)) {
7525 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7526 Res = DAG.getNode(ISD::SUB, DL, VT,
7527 DAG.getNode(ISD::SHL, DL, VT,
7528 V,
7529 DAG.getConstant(Log2_32(MulAmt + 1),
7530 MVT::i32)),
7531 V);
7532 } else
7533 return SDValue();
7534 } else {
7535 uint64_t MulAmtAbs = -MulAmt;
7536 if (isPowerOf2_32(MulAmtAbs + 1)) {
7537 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7538 Res = DAG.getNode(ISD::SUB, DL, VT,
7539 V,
7540 DAG.getNode(ISD::SHL, DL, VT,
7541 V,
7542 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7543 MVT::i32)));
7544 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7545 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7546 Res = DAG.getNode(ISD::ADD, DL, VT,
7547 V,
7548 DAG.getNode(ISD::SHL, DL, VT,
7549 V,
7550 DAG.getConstant(Log2_32(MulAmtAbs-1),
7551 MVT::i32)));
7552 Res = DAG.getNode(ISD::SUB, DL, VT,
7553 DAG.getConstant(0, MVT::i32),Res);
7554
7555 } else
7556 return SDValue();
7557 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007558
7559 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007560 Res = DAG.getNode(ISD::SHL, DL, VT,
7561 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007562
7563 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007564 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007565 return SDValue();
7566}
7567
Owen Anderson080c0922010-11-05 19:27:46 +00007568static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007569 TargetLowering::DAGCombinerInfo &DCI,
7570 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007571
Owen Anderson080c0922010-11-05 19:27:46 +00007572 // Attempt to use immediate-form VBIC
7573 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7574 DebugLoc dl = N->getDebugLoc();
7575 EVT VT = N->getValueType(0);
7576 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007577
Tanya Lattner0433b212011-04-07 15:24:20 +00007578 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7579 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007580
Owen Anderson080c0922010-11-05 19:27:46 +00007581 APInt SplatBits, SplatUndef;
7582 unsigned SplatBitSize;
7583 bool HasAnyUndefs;
7584 if (BVN &&
7585 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7586 if (SplatBitSize <= 64) {
7587 EVT VbicVT;
7588 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7589 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007590 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007591 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007592 if (Val.getNode()) {
7593 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007594 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007595 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007596 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007597 }
7598 }
7599 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007600
Evan Chengc892aeb2012-02-23 01:19:06 +00007601 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007602 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7603 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7604 if (Result.getNode())
7605 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007606 }
7607
Owen Anderson080c0922010-11-05 19:27:46 +00007608 return SDValue();
7609}
7610
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007611/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7612static SDValue PerformORCombine(SDNode *N,
7613 TargetLowering::DAGCombinerInfo &DCI,
7614 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007615 // Attempt to use immediate-form VORR
7616 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7617 DebugLoc dl = N->getDebugLoc();
7618 EVT VT = N->getValueType(0);
7619 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007620
Tanya Lattner0433b212011-04-07 15:24:20 +00007621 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7622 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007623
Owen Anderson60f48702010-11-03 23:15:26 +00007624 APInt SplatBits, SplatUndef;
7625 unsigned SplatBitSize;
7626 bool HasAnyUndefs;
7627 if (BVN && Subtarget->hasNEON() &&
7628 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7629 if (SplatBitSize <= 64) {
7630 EVT VorrVT;
7631 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7632 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007633 DAG, VorrVT, VT.is128BitVector(),
7634 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007635 if (Val.getNode()) {
7636 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007637 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007638 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007639 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007640 }
7641 }
7642 }
7643
Evan Chengc892aeb2012-02-23 01:19:06 +00007644 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007645 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7646 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7647 if (Result.getNode())
7648 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007649 }
7650
Nadav Rotemdf832032012-08-13 18:52:44 +00007651 // The code below optimizes (or (and X, Y), Z).
7652 // The AND operand needs to have a single user to make these optimizations
7653 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007654 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00007655 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007656 return SDValue();
7657 SDValue N1 = N->getOperand(1);
7658
7659 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7660 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7661 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7662 APInt SplatUndef;
7663 unsigned SplatBitSize;
7664 bool HasAnyUndefs;
7665
7666 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7667 APInt SplatBits0;
7668 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7669 HasAnyUndefs) && !HasAnyUndefs) {
7670 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7671 APInt SplatBits1;
7672 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7673 HasAnyUndefs) && !HasAnyUndefs &&
7674 SplatBits0 == ~SplatBits1) {
7675 // Canonicalize the vector type to make instruction selection simpler.
7676 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7677 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7678 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007679 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007680 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7681 }
7682 }
7683 }
7684
Jim Grosbach54238562010-07-17 03:30:54 +00007685 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7686 // reasonable.
7687
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007688 // BFI is only available on V6T2+
7689 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7690 return SDValue();
7691
Jim Grosbach54238562010-07-17 03:30:54 +00007692 DebugLoc DL = N->getDebugLoc();
7693 // 1) or (and A, mask), val => ARMbfi A, val, mask
7694 // iff (val & mask) == val
7695 //
7696 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7697 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007698 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007699 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007700 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007701 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007702
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007703 if (VT != MVT::i32)
7704 return SDValue();
7705
Evan Cheng30fb13f2010-12-13 20:32:54 +00007706 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007707
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007708 // The value and the mask need to be constants so we can verify this is
7709 // actually a bitfield set. If the mask is 0xffff, we can do better
7710 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007711 SDValue MaskOp = N0.getOperand(1);
7712 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7713 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007714 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007715 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007716 if (Mask == 0xffff)
7717 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007718 SDValue Res;
7719 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007720 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7721 if (N1C) {
7722 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007723 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007724 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007725
Evan Chenga9688c42010-12-11 04:11:38 +00007726 if (ARM::isBitFieldInvertedMask(Mask)) {
7727 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007728
Evan Cheng30fb13f2010-12-13 20:32:54 +00007729 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007730 DAG.getConstant(Val, MVT::i32),
7731 DAG.getConstant(Mask, MVT::i32));
7732
7733 // Do not add new nodes to DAG combiner worklist.
7734 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007735 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007736 }
Jim Grosbach54238562010-07-17 03:30:54 +00007737 } else if (N1.getOpcode() == ISD::AND) {
7738 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007739 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7740 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007741 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007742 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007743
Eric Christopher29aeed12011-03-26 01:21:03 +00007744 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7745 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007746 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007747 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007748 // The pack halfword instruction works better for masks that fit it,
7749 // so use that when it's available.
7750 if (Subtarget->hasT2ExtractPack() &&
7751 (Mask == 0xffff || Mask == 0xffff0000))
7752 return SDValue();
7753 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007754 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007755 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007756 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007757 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007758 DAG.getConstant(Mask, MVT::i32));
7759 // Do not add new nodes to DAG combiner worklist.
7760 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007761 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007762 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007763 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007764 // The pack halfword instruction works better for masks that fit it,
7765 // so use that when it's available.
7766 if (Subtarget->hasT2ExtractPack() &&
7767 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7768 return SDValue();
7769 // 2b
7770 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007771 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007772 DAG.getConstant(lsb, MVT::i32));
7773 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007774 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007775 // Do not add new nodes to DAG combiner worklist.
7776 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007777 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007778 }
7779 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007780
Evan Cheng30fb13f2010-12-13 20:32:54 +00007781 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7782 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7783 ARM::isBitFieldInvertedMask(~Mask)) {
7784 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7785 // where lsb(mask) == #shamt and masked bits of B are known zero.
7786 SDValue ShAmt = N00.getOperand(1);
7787 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7788 unsigned LSB = CountTrailingZeros_32(Mask);
7789 if (ShAmtC != LSB)
7790 return SDValue();
7791
7792 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7793 DAG.getConstant(~Mask, MVT::i32));
7794
7795 // Do not add new nodes to DAG combiner worklist.
7796 DCI.CombineTo(N, Res, false);
7797 }
7798
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007799 return SDValue();
7800}
7801
Evan Chengc892aeb2012-02-23 01:19:06 +00007802static SDValue PerformXORCombine(SDNode *N,
7803 TargetLowering::DAGCombinerInfo &DCI,
7804 const ARMSubtarget *Subtarget) {
7805 EVT VT = N->getValueType(0);
7806 SelectionDAG &DAG = DCI.DAG;
7807
7808 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7809 return SDValue();
7810
7811 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007812 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
7813 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7814 if (Result.getNode())
7815 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007816 }
7817
7818 return SDValue();
7819}
7820
Evan Chengbf188ae2011-06-15 01:12:31 +00007821/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7822/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007823static SDValue PerformBFICombine(SDNode *N,
7824 TargetLowering::DAGCombinerInfo &DCI) {
7825 SDValue N1 = N->getOperand(1);
7826 if (N1.getOpcode() == ISD::AND) {
7827 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7828 if (!N11C)
7829 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007830 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7831 unsigned LSB = CountTrailingZeros_32(~InvMask);
7832 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7833 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007834 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007835 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007836 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7837 N->getOperand(0), N1.getOperand(0),
7838 N->getOperand(2));
7839 }
7840 return SDValue();
7841}
7842
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007843/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7844/// ARMISD::VMOVRRD.
7845static SDValue PerformVMOVRRDCombine(SDNode *N,
7846 TargetLowering::DAGCombinerInfo &DCI) {
7847 // vmovrrd(vmovdrr x, y) -> x,y
7848 SDValue InDouble = N->getOperand(0);
7849 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7850 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007851
7852 // vmovrrd(load f64) -> (load i32), (load i32)
7853 SDNode *InNode = InDouble.getNode();
7854 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7855 InNode->getValueType(0) == MVT::f64 &&
7856 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7857 !cast<LoadSDNode>(InNode)->isVolatile()) {
7858 // TODO: Should this be done for non-FrameIndex operands?
7859 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7860
7861 SelectionDAG &DAG = DCI.DAG;
7862 DebugLoc DL = LD->getDebugLoc();
7863 SDValue BasePtr = LD->getBasePtr();
7864 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7865 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007866 LD->isNonTemporal(), LD->isInvariant(),
7867 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007868
7869 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7870 DAG.getConstant(4, MVT::i32));
7871 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7872 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007873 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007874 std::min(4U, LD->getAlignment() / 2));
7875
7876 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7877 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7878 DCI.RemoveFromWorklist(LD);
7879 DAG.DeleteNode(LD);
7880 return Result;
7881 }
7882
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007883 return SDValue();
7884}
7885
7886/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7887/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7888static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7889 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7890 SDValue Op0 = N->getOperand(0);
7891 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007892 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007893 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007894 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007895 Op1 = Op1.getOperand(0);
7896 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7897 Op0.getNode() == Op1.getNode() &&
7898 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007899 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007900 N->getValueType(0), Op0.getOperand(0));
7901 return SDValue();
7902}
7903
Bob Wilson31600902010-12-21 06:43:19 +00007904/// PerformSTORECombine - Target-specific dag combine xforms for
7905/// ISD::STORE.
7906static SDValue PerformSTORECombine(SDNode *N,
7907 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007908 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007909 if (St->isVolatile())
7910 return SDValue();
7911
Andrew Trick49b446f2012-07-18 18:34:24 +00007912 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007913 // pack all of the elements in one place. Next, store to memory in fewer
7914 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007915 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007916 EVT VT = StVal.getValueType();
7917 if (St->isTruncatingStore() && VT.isVector()) {
7918 SelectionDAG &DAG = DCI.DAG;
7919 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7920 EVT StVT = St->getMemoryVT();
7921 unsigned NumElems = VT.getVectorNumElements();
7922 assert(StVT != VT && "Cannot truncate to the same type");
7923 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7924 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7925
7926 // From, To sizes and ElemCount must be pow of two
7927 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7928
7929 // We are going to use the original vector elt for storing.
7930 // Accumulated smaller vector elements must be a multiple of the store size.
7931 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7932
7933 unsigned SizeRatio = FromEltSz / ToEltSz;
7934 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7935
7936 // Create a type on which we perform the shuffle.
7937 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7938 NumElems*SizeRatio);
7939 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7940
7941 DebugLoc DL = St->getDebugLoc();
7942 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7943 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7944 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7945
7946 // Can't shuffle using an illegal type.
7947 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7948
7949 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7950 DAG.getUNDEF(WideVec.getValueType()),
7951 ShuffleVec.data());
7952 // At this point all of the data is stored at the bottom of the
7953 // register. We now need to save it to mem.
7954
7955 // Find the largest store unit
7956 MVT StoreType = MVT::i8;
7957 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7958 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7959 MVT Tp = (MVT::SimpleValueType)tp;
7960 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7961 StoreType = Tp;
7962 }
7963 // Didn't find a legal store type.
7964 if (!TLI.isTypeLegal(StoreType))
7965 return SDValue();
7966
7967 // Bitcast the original vector into a vector of store-size units
7968 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7969 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7970 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7971 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7972 SmallVector<SDValue, 8> Chains;
7973 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7974 TLI.getPointerTy());
7975 SDValue BasePtr = St->getBasePtr();
7976
7977 // Perform one or more big stores into memory.
7978 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7979 for (unsigned I = 0; I < E; I++) {
7980 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7981 StoreType, ShuffWide,
7982 DAG.getIntPtrConstant(I));
7983 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7984 St->getPointerInfo(), St->isVolatile(),
7985 St->isNonTemporal(), St->getAlignment());
7986 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7987 Increment);
7988 Chains.push_back(Ch);
7989 }
7990 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7991 Chains.size());
7992 }
7993
7994 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007995 return SDValue();
7996
Chad Rosier96b66d62012-04-09 19:38:15 +00007997 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7998 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007999 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008000 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008001 SelectionDAG &DAG = DCI.DAG;
8002 DebugLoc DL = St->getDebugLoc();
8003 SDValue BasePtr = St->getBasePtr();
8004 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8005 StVal.getNode()->getOperand(0), BasePtr,
8006 St->getPointerInfo(), St->isVolatile(),
8007 St->isNonTemporal(), St->getAlignment());
8008
8009 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8010 DAG.getConstant(4, MVT::i32));
8011 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8012 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8013 St->isNonTemporal(),
8014 std::min(4U, St->getAlignment() / 2));
8015 }
8016
8017 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008018 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8019 return SDValue();
8020
Chad Rosier96b66d62012-04-09 19:38:15 +00008021 // Bitcast an i64 store extracted from a vector to f64.
8022 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008023 SelectionDAG &DAG = DCI.DAG;
8024 DebugLoc dl = StVal.getDebugLoc();
8025 SDValue IntVec = StVal.getOperand(0);
8026 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8027 IntVec.getValueType().getVectorNumElements());
8028 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8029 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8030 Vec, StVal.getOperand(1));
8031 dl = N->getDebugLoc();
8032 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8033 // Make the DAGCombiner fold the bitcasts.
8034 DCI.AddToWorklist(Vec.getNode());
8035 DCI.AddToWorklist(ExtElt.getNode());
8036 DCI.AddToWorklist(V.getNode());
8037 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8038 St->getPointerInfo(), St->isVolatile(),
8039 St->isNonTemporal(), St->getAlignment(),
8040 St->getTBAAInfo());
8041}
8042
8043/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8044/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8045/// i64 vector to have f64 elements, since the value can then be loaded
8046/// directly into a VFP register.
8047static bool hasNormalLoadOperand(SDNode *N) {
8048 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8049 for (unsigned i = 0; i < NumElts; ++i) {
8050 SDNode *Elt = N->getOperand(i).getNode();
8051 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8052 return true;
8053 }
8054 return false;
8055}
8056
Bob Wilson75f02882010-09-17 22:59:05 +00008057/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8058/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008059static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8060 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008061 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8062 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8063 // into a pair of GPRs, which is fine when the value is used as a scalar,
8064 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008065 SelectionDAG &DAG = DCI.DAG;
8066 if (N->getNumOperands() == 2) {
8067 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8068 if (RV.getNode())
8069 return RV;
8070 }
Bob Wilson75f02882010-09-17 22:59:05 +00008071
Bob Wilson31600902010-12-21 06:43:19 +00008072 // Load i64 elements as f64 values so that type legalization does not split
8073 // them up into i32 values.
8074 EVT VT = N->getValueType(0);
8075 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8076 return SDValue();
8077 DebugLoc dl = N->getDebugLoc();
8078 SmallVector<SDValue, 8> Ops;
8079 unsigned NumElts = VT.getVectorNumElements();
8080 for (unsigned i = 0; i < NumElts; ++i) {
8081 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8082 Ops.push_back(V);
8083 // Make the DAGCombiner fold the bitcast.
8084 DCI.AddToWorklist(V.getNode());
8085 }
8086 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8087 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8088 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8089}
8090
8091/// PerformInsertEltCombine - Target-specific dag combine xforms for
8092/// ISD::INSERT_VECTOR_ELT.
8093static SDValue PerformInsertEltCombine(SDNode *N,
8094 TargetLowering::DAGCombinerInfo &DCI) {
8095 // Bitcast an i64 load inserted into a vector to f64.
8096 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8097 EVT VT = N->getValueType(0);
8098 SDNode *Elt = N->getOperand(1).getNode();
8099 if (VT.getVectorElementType() != MVT::i64 ||
8100 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8101 return SDValue();
8102
8103 SelectionDAG &DAG = DCI.DAG;
8104 DebugLoc dl = N->getDebugLoc();
8105 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8106 VT.getVectorNumElements());
8107 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8108 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8109 // Make the DAGCombiner fold the bitcasts.
8110 DCI.AddToWorklist(Vec.getNode());
8111 DCI.AddToWorklist(V.getNode());
8112 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8113 Vec, V, N->getOperand(2));
8114 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008115}
8116
Bob Wilsonf20700c2010-10-27 20:38:28 +00008117/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8118/// ISD::VECTOR_SHUFFLE.
8119static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8120 // The LLVM shufflevector instruction does not require the shuffle mask
8121 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8122 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8123 // operands do not match the mask length, they are extended by concatenating
8124 // them with undef vectors. That is probably the right thing for other
8125 // targets, but for NEON it is better to concatenate two double-register
8126 // size vector operands into a single quad-register size vector. Do that
8127 // transformation here:
8128 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8129 // shuffle(concat(v1, v2), undef)
8130 SDValue Op0 = N->getOperand(0);
8131 SDValue Op1 = N->getOperand(1);
8132 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8133 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8134 Op0.getNumOperands() != 2 ||
8135 Op1.getNumOperands() != 2)
8136 return SDValue();
8137 SDValue Concat0Op1 = Op0.getOperand(1);
8138 SDValue Concat1Op1 = Op1.getOperand(1);
8139 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8140 Concat1Op1.getOpcode() != ISD::UNDEF)
8141 return SDValue();
8142 // Skip the transformation if any of the types are illegal.
8143 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8144 EVT VT = N->getValueType(0);
8145 if (!TLI.isTypeLegal(VT) ||
8146 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8147 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8148 return SDValue();
8149
8150 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8151 Op0.getOperand(0), Op1.getOperand(0));
8152 // Translate the shuffle mask.
8153 SmallVector<int, 16> NewMask;
8154 unsigned NumElts = VT.getVectorNumElements();
8155 unsigned HalfElts = NumElts/2;
8156 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8157 for (unsigned n = 0; n < NumElts; ++n) {
8158 int MaskElt = SVN->getMaskElt(n);
8159 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008160 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008161 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008162 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008163 NewElt = HalfElts + MaskElt - NumElts;
8164 NewMask.push_back(NewElt);
8165 }
8166 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8167 DAG.getUNDEF(VT), NewMask.data());
8168}
8169
Bob Wilson1c3ef902011-02-07 17:43:21 +00008170/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8171/// NEON load/store intrinsics to merge base address updates.
8172static SDValue CombineBaseUpdate(SDNode *N,
8173 TargetLowering::DAGCombinerInfo &DCI) {
8174 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8175 return SDValue();
8176
8177 SelectionDAG &DAG = DCI.DAG;
8178 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8179 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8180 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8181 SDValue Addr = N->getOperand(AddrOpIdx);
8182
8183 // Search for a use of the address operand that is an increment.
8184 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8185 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8186 SDNode *User = *UI;
8187 if (User->getOpcode() != ISD::ADD ||
8188 UI.getUse().getResNo() != Addr.getResNo())
8189 continue;
8190
8191 // Check that the add is independent of the load/store. Otherwise, folding
8192 // it would create a cycle.
8193 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8194 continue;
8195
8196 // Find the new opcode for the updating load/store.
8197 bool isLoad = true;
8198 bool isLaneOp = false;
8199 unsigned NewOpc = 0;
8200 unsigned NumVecs = 0;
8201 if (isIntrinsic) {
8202 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8203 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008204 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008205 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8206 NumVecs = 1; break;
8207 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8208 NumVecs = 2; break;
8209 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8210 NumVecs = 3; break;
8211 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8212 NumVecs = 4; break;
8213 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8214 NumVecs = 2; isLaneOp = true; break;
8215 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8216 NumVecs = 3; isLaneOp = true; break;
8217 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8218 NumVecs = 4; isLaneOp = true; break;
8219 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8220 NumVecs = 1; isLoad = false; break;
8221 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8222 NumVecs = 2; isLoad = false; break;
8223 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8224 NumVecs = 3; isLoad = false; break;
8225 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8226 NumVecs = 4; isLoad = false; break;
8227 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8228 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8229 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8230 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8231 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8232 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8233 }
8234 } else {
8235 isLaneOp = true;
8236 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008237 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008238 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8239 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8240 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8241 }
8242 }
8243
8244 // Find the size of memory referenced by the load/store.
8245 EVT VecTy;
8246 if (isLoad)
8247 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008248 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008249 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8250 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8251 if (isLaneOp)
8252 NumBytes /= VecTy.getVectorNumElements();
8253
8254 // If the increment is a constant, it must match the memory ref size.
8255 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8256 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8257 uint64_t IncVal = CInc->getZExtValue();
8258 if (IncVal != NumBytes)
8259 continue;
8260 } else if (NumBytes >= 3 * 16) {
8261 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8262 // separate instructions that make it harder to use a non-constant update.
8263 continue;
8264 }
8265
8266 // Create the new updating load/store node.
8267 EVT Tys[6];
8268 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8269 unsigned n;
8270 for (n = 0; n < NumResultVecs; ++n)
8271 Tys[n] = VecTy;
8272 Tys[n++] = MVT::i32;
8273 Tys[n] = MVT::Other;
8274 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8275 SmallVector<SDValue, 8> Ops;
8276 Ops.push_back(N->getOperand(0)); // incoming chain
8277 Ops.push_back(N->getOperand(AddrOpIdx));
8278 Ops.push_back(Inc);
8279 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8280 Ops.push_back(N->getOperand(i));
8281 }
8282 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8283 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8284 Ops.data(), Ops.size(),
8285 MemInt->getMemoryVT(),
8286 MemInt->getMemOperand());
8287
8288 // Update the uses.
8289 std::vector<SDValue> NewResults;
8290 for (unsigned i = 0; i < NumResultVecs; ++i) {
8291 NewResults.push_back(SDValue(UpdN.getNode(), i));
8292 }
8293 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8294 DCI.CombineTo(N, NewResults);
8295 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8296
8297 break;
Owen Anderson76706012011-04-05 21:48:57 +00008298 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008299 return SDValue();
8300}
8301
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008302/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8303/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8304/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8305/// return true.
8306static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8307 SelectionDAG &DAG = DCI.DAG;
8308 EVT VT = N->getValueType(0);
8309 // vldN-dup instructions only support 64-bit vectors for N > 1.
8310 if (!VT.is64BitVector())
8311 return false;
8312
8313 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8314 SDNode *VLD = N->getOperand(0).getNode();
8315 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8316 return false;
8317 unsigned NumVecs = 0;
8318 unsigned NewOpc = 0;
8319 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8320 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8321 NumVecs = 2;
8322 NewOpc = ARMISD::VLD2DUP;
8323 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8324 NumVecs = 3;
8325 NewOpc = ARMISD::VLD3DUP;
8326 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8327 NumVecs = 4;
8328 NewOpc = ARMISD::VLD4DUP;
8329 } else {
8330 return false;
8331 }
8332
8333 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8334 // numbers match the load.
8335 unsigned VLDLaneNo =
8336 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8337 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8338 UI != UE; ++UI) {
8339 // Ignore uses of the chain result.
8340 if (UI.getUse().getResNo() == NumVecs)
8341 continue;
8342 SDNode *User = *UI;
8343 if (User->getOpcode() != ARMISD::VDUPLANE ||
8344 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8345 return false;
8346 }
8347
8348 // Create the vldN-dup node.
8349 EVT Tys[5];
8350 unsigned n;
8351 for (n = 0; n < NumVecs; ++n)
8352 Tys[n] = VT;
8353 Tys[n] = MVT::Other;
8354 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8355 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8356 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8357 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8358 Ops, 2, VLDMemInt->getMemoryVT(),
8359 VLDMemInt->getMemOperand());
8360
8361 // Update the uses.
8362 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8363 UI != UE; ++UI) {
8364 unsigned ResNo = UI.getUse().getResNo();
8365 // Ignore uses of the chain result.
8366 if (ResNo == NumVecs)
8367 continue;
8368 SDNode *User = *UI;
8369 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8370 }
8371
8372 // Now the vldN-lane intrinsic is dead except for its chain result.
8373 // Update uses of the chain.
8374 std::vector<SDValue> VLDDupResults;
8375 for (unsigned n = 0; n < NumVecs; ++n)
8376 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8377 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8378 DCI.CombineTo(VLD, VLDDupResults);
8379
8380 return true;
8381}
8382
Bob Wilson9e82bf12010-07-14 01:22:12 +00008383/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8384/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008385static SDValue PerformVDUPLANECombine(SDNode *N,
8386 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008387 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008388
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008389 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8390 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8391 if (CombineVLDDUP(N, DCI))
8392 return SDValue(N, 0);
8393
8394 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8395 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008396 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008397 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008398 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008399 return SDValue();
8400
8401 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8402 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8403 // The canonical VMOV for a zero vector uses a 32-bit element size.
8404 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8405 unsigned EltBits;
8406 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8407 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008408 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008409 if (EltSize > VT.getVectorElementType().getSizeInBits())
8410 return SDValue();
8411
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008412 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008413}
8414
Eric Christopherfa6f5912011-06-29 21:10:36 +00008415// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008416// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8417static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8418{
Chad Rosier118c9a02011-06-28 17:26:57 +00008419 integerPart cN;
8420 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008421 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8422 I != E; I++) {
8423 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8424 if (!C)
8425 return false;
8426
Eric Christopherfa6f5912011-06-29 21:10:36 +00008427 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008428 APFloat APF = C->getValueAPF();
8429 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8430 != APFloat::opOK || !isExact)
8431 return false;
8432
8433 c0 = (I == 0) ? cN : c0;
8434 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8435 return false;
8436 }
8437 C = c0;
8438 return true;
8439}
8440
8441/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8442/// can replace combinations of VMUL and VCVT (floating-point to integer)
8443/// when the VMUL has a constant operand that is a power of 2.
8444///
8445/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8446/// vmul.f32 d16, d17, d16
8447/// vcvt.s32.f32 d16, d16
8448/// becomes:
8449/// vcvt.s32.f32 d16, d16, #3
8450static SDValue PerformVCVTCombine(SDNode *N,
8451 TargetLowering::DAGCombinerInfo &DCI,
8452 const ARMSubtarget *Subtarget) {
8453 SelectionDAG &DAG = DCI.DAG;
8454 SDValue Op = N->getOperand(0);
8455
8456 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8457 Op.getOpcode() != ISD::FMUL)
8458 return SDValue();
8459
8460 uint64_t C;
8461 SDValue N0 = Op->getOperand(0);
8462 SDValue ConstVec = Op->getOperand(1);
8463 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8464
Eric Christopherfa6f5912011-06-29 21:10:36 +00008465 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008466 !isConstVecPow2(ConstVec, isSigned, C))
8467 return SDValue();
8468
8469 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8470 Intrinsic::arm_neon_vcvtfp2fxu;
8471 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8472 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008473 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008474 DAG.getConstant(Log2_64(C), MVT::i32));
8475}
8476
8477/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8478/// can replace combinations of VCVT (integer to floating-point) and VDIV
8479/// when the VDIV has a constant operand that is a power of 2.
8480///
8481/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8482/// vcvt.f32.s32 d16, d16
8483/// vdiv.f32 d16, d17, d16
8484/// becomes:
8485/// vcvt.f32.s32 d16, d16, #3
8486static SDValue PerformVDIVCombine(SDNode *N,
8487 TargetLowering::DAGCombinerInfo &DCI,
8488 const ARMSubtarget *Subtarget) {
8489 SelectionDAG &DAG = DCI.DAG;
8490 SDValue Op = N->getOperand(0);
8491 unsigned OpOpcode = Op.getNode()->getOpcode();
8492
8493 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8494 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8495 return SDValue();
8496
8497 uint64_t C;
8498 SDValue ConstVec = N->getOperand(1);
8499 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8500
8501 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8502 !isConstVecPow2(ConstVec, isSigned, C))
8503 return SDValue();
8504
Eric Christopherfa6f5912011-06-29 21:10:36 +00008505 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008506 Intrinsic::arm_neon_vcvtfxu2fp;
8507 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8508 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008509 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008510 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8511}
8512
8513/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008514/// operand of a vector shift operation, where all the elements of the
8515/// build_vector must have the same constant integer value.
8516static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8517 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008518 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008519 Op = Op.getOperand(0);
8520 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8521 APInt SplatBits, SplatUndef;
8522 unsigned SplatBitSize;
8523 bool HasAnyUndefs;
8524 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8525 HasAnyUndefs, ElementBits) ||
8526 SplatBitSize > ElementBits)
8527 return false;
8528 Cnt = SplatBits.getSExtValue();
8529 return true;
8530}
8531
8532/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8533/// operand of a vector shift left operation. That value must be in the range:
8534/// 0 <= Value < ElementBits for a left shift; or
8535/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008536static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008537 assert(VT.isVector() && "vector shift count is not a vector type");
8538 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8539 if (! getVShiftImm(Op, ElementBits, Cnt))
8540 return false;
8541 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8542}
8543
8544/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8545/// operand of a vector shift right operation. For a shift opcode, the value
8546/// is positive, but for an intrinsic the value count must be negative. The
8547/// absolute value must be in the range:
8548/// 1 <= |Value| <= ElementBits for a right shift; or
8549/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008550static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008551 int64_t &Cnt) {
8552 assert(VT.isVector() && "vector shift count is not a vector type");
8553 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8554 if (! getVShiftImm(Op, ElementBits, Cnt))
8555 return false;
8556 if (isIntrinsic)
8557 Cnt = -Cnt;
8558 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8559}
8560
8561/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8562static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8563 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8564 switch (IntNo) {
8565 default:
8566 // Don't do anything for most intrinsics.
8567 break;
8568
8569 // Vector shifts: check for immediate versions and lower them.
8570 // Note: This is done during DAG combining instead of DAG legalizing because
8571 // the build_vectors for 64-bit vector element shift counts are generally
8572 // not legal, and it is hard to see their values after they get legalized to
8573 // loads from a constant pool.
8574 case Intrinsic::arm_neon_vshifts:
8575 case Intrinsic::arm_neon_vshiftu:
8576 case Intrinsic::arm_neon_vshiftls:
8577 case Intrinsic::arm_neon_vshiftlu:
8578 case Intrinsic::arm_neon_vshiftn:
8579 case Intrinsic::arm_neon_vrshifts:
8580 case Intrinsic::arm_neon_vrshiftu:
8581 case Intrinsic::arm_neon_vrshiftn:
8582 case Intrinsic::arm_neon_vqshifts:
8583 case Intrinsic::arm_neon_vqshiftu:
8584 case Intrinsic::arm_neon_vqshiftsu:
8585 case Intrinsic::arm_neon_vqshiftns:
8586 case Intrinsic::arm_neon_vqshiftnu:
8587 case Intrinsic::arm_neon_vqshiftnsu:
8588 case Intrinsic::arm_neon_vqrshiftns:
8589 case Intrinsic::arm_neon_vqrshiftnu:
8590 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008591 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008592 int64_t Cnt;
8593 unsigned VShiftOpc = 0;
8594
8595 switch (IntNo) {
8596 case Intrinsic::arm_neon_vshifts:
8597 case Intrinsic::arm_neon_vshiftu:
8598 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8599 VShiftOpc = ARMISD::VSHL;
8600 break;
8601 }
8602 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8603 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8604 ARMISD::VSHRs : ARMISD::VSHRu);
8605 break;
8606 }
8607 return SDValue();
8608
8609 case Intrinsic::arm_neon_vshiftls:
8610 case Intrinsic::arm_neon_vshiftlu:
8611 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8612 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008613 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008614
8615 case Intrinsic::arm_neon_vrshifts:
8616 case Intrinsic::arm_neon_vrshiftu:
8617 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8618 break;
8619 return SDValue();
8620
8621 case Intrinsic::arm_neon_vqshifts:
8622 case Intrinsic::arm_neon_vqshiftu:
8623 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8624 break;
8625 return SDValue();
8626
8627 case Intrinsic::arm_neon_vqshiftsu:
8628 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8629 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008630 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008631
8632 case Intrinsic::arm_neon_vshiftn:
8633 case Intrinsic::arm_neon_vrshiftn:
8634 case Intrinsic::arm_neon_vqshiftns:
8635 case Intrinsic::arm_neon_vqshiftnu:
8636 case Intrinsic::arm_neon_vqshiftnsu:
8637 case Intrinsic::arm_neon_vqrshiftns:
8638 case Intrinsic::arm_neon_vqrshiftnu:
8639 case Intrinsic::arm_neon_vqrshiftnsu:
8640 // Narrowing shifts require an immediate right shift.
8641 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8642 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008643 llvm_unreachable("invalid shift count for narrowing vector shift "
8644 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008645
8646 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008647 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008648 }
8649
8650 switch (IntNo) {
8651 case Intrinsic::arm_neon_vshifts:
8652 case Intrinsic::arm_neon_vshiftu:
8653 // Opcode already set above.
8654 break;
8655 case Intrinsic::arm_neon_vshiftls:
8656 case Intrinsic::arm_neon_vshiftlu:
8657 if (Cnt == VT.getVectorElementType().getSizeInBits())
8658 VShiftOpc = ARMISD::VSHLLi;
8659 else
8660 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8661 ARMISD::VSHLLs : ARMISD::VSHLLu);
8662 break;
8663 case Intrinsic::arm_neon_vshiftn:
8664 VShiftOpc = ARMISD::VSHRN; break;
8665 case Intrinsic::arm_neon_vrshifts:
8666 VShiftOpc = ARMISD::VRSHRs; break;
8667 case Intrinsic::arm_neon_vrshiftu:
8668 VShiftOpc = ARMISD::VRSHRu; break;
8669 case Intrinsic::arm_neon_vrshiftn:
8670 VShiftOpc = ARMISD::VRSHRN; break;
8671 case Intrinsic::arm_neon_vqshifts:
8672 VShiftOpc = ARMISD::VQSHLs; break;
8673 case Intrinsic::arm_neon_vqshiftu:
8674 VShiftOpc = ARMISD::VQSHLu; break;
8675 case Intrinsic::arm_neon_vqshiftsu:
8676 VShiftOpc = ARMISD::VQSHLsu; break;
8677 case Intrinsic::arm_neon_vqshiftns:
8678 VShiftOpc = ARMISD::VQSHRNs; break;
8679 case Intrinsic::arm_neon_vqshiftnu:
8680 VShiftOpc = ARMISD::VQSHRNu; break;
8681 case Intrinsic::arm_neon_vqshiftnsu:
8682 VShiftOpc = ARMISD::VQSHRNsu; break;
8683 case Intrinsic::arm_neon_vqrshiftns:
8684 VShiftOpc = ARMISD::VQRSHRNs; break;
8685 case Intrinsic::arm_neon_vqrshiftnu:
8686 VShiftOpc = ARMISD::VQRSHRNu; break;
8687 case Intrinsic::arm_neon_vqrshiftnsu:
8688 VShiftOpc = ARMISD::VQRSHRNsu; break;
8689 }
8690
8691 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008692 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008693 }
8694
8695 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008696 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008697 int64_t Cnt;
8698 unsigned VShiftOpc = 0;
8699
8700 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8701 VShiftOpc = ARMISD::VSLI;
8702 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8703 VShiftOpc = ARMISD::VSRI;
8704 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008705 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008706 }
8707
8708 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8709 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008710 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008711 }
8712
8713 case Intrinsic::arm_neon_vqrshifts:
8714 case Intrinsic::arm_neon_vqrshiftu:
8715 // No immediate versions of these to check for.
8716 break;
8717 }
8718
8719 return SDValue();
8720}
8721
8722/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8723/// lowers them. As with the vector shift intrinsics, this is done during DAG
8724/// combining instead of DAG legalizing because the build_vectors for 64-bit
8725/// vector element shift counts are generally not legal, and it is hard to see
8726/// their values after they get legalized to loads from a constant pool.
8727static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8728 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008729 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008730 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8731 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8732 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8733 SDValue N1 = N->getOperand(1);
8734 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8735 SDValue N0 = N->getOperand(0);
8736 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8737 DAG.MaskedValueIsZero(N0.getOperand(0),
8738 APInt::getHighBitsSet(32, 16)))
8739 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8740 }
8741 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008742
8743 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008744 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8745 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008746 return SDValue();
8747
8748 assert(ST->hasNEON() && "unexpected vector shift");
8749 int64_t Cnt;
8750
8751 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008752 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008753
8754 case ISD::SHL:
8755 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8756 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008757 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008758 break;
8759
8760 case ISD::SRA:
8761 case ISD::SRL:
8762 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8763 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8764 ARMISD::VSHRs : ARMISD::VSHRu);
8765 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008766 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008767 }
8768 }
8769 return SDValue();
8770}
8771
8772/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8773/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8774static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8775 const ARMSubtarget *ST) {
8776 SDValue N0 = N->getOperand(0);
8777
8778 // Check for sign- and zero-extensions of vector extract operations of 8-
8779 // and 16-bit vector elements. NEON supports these directly. They are
8780 // handled during DAG combining because type legalization will promote them
8781 // to 32-bit types and it is messy to recognize the operations after that.
8782 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8783 SDValue Vec = N0.getOperand(0);
8784 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008785 EVT VT = N->getValueType(0);
8786 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008787 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8788
Owen Anderson825b72b2009-08-11 20:47:22 +00008789 if (VT == MVT::i32 &&
8790 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008791 TLI.isTypeLegal(Vec.getValueType()) &&
8792 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008793
8794 unsigned Opc = 0;
8795 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008796 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008797 case ISD::SIGN_EXTEND:
8798 Opc = ARMISD::VGETLANEs;
8799 break;
8800 case ISD::ZERO_EXTEND:
8801 case ISD::ANY_EXTEND:
8802 Opc = ARMISD::VGETLANEu;
8803 break;
8804 }
8805 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8806 }
8807 }
8808
8809 return SDValue();
8810}
8811
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008812/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8813/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8814static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8815 const ARMSubtarget *ST) {
8816 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008817 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008818 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8819 // a NaN; only do the transformation when it matches that behavior.
8820
8821 // For now only do this when using NEON for FP operations; if using VFP, it
8822 // is not obvious that the benefit outweighs the cost of switching to the
8823 // NEON pipeline.
8824 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8825 N->getValueType(0) != MVT::f32)
8826 return SDValue();
8827
8828 SDValue CondLHS = N->getOperand(0);
8829 SDValue CondRHS = N->getOperand(1);
8830 SDValue LHS = N->getOperand(2);
8831 SDValue RHS = N->getOperand(3);
8832 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8833
8834 unsigned Opcode = 0;
8835 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008836 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008837 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008838 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008839 IsReversed = true ; // x CC y ? y : x
8840 } else {
8841 return SDValue();
8842 }
8843
Bob Wilsone742bb52010-02-24 22:15:53 +00008844 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008845 switch (CC) {
8846 default: break;
8847 case ISD::SETOLT:
8848 case ISD::SETOLE:
8849 case ISD::SETLT:
8850 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008851 case ISD::SETULT:
8852 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008853 // If LHS is NaN, an ordered comparison will be false and the result will
8854 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8855 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8856 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8857 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8858 break;
8859 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8860 // will return -0, so vmin can only be used for unsafe math or if one of
8861 // the operands is known to be nonzero.
8862 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008863 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008864 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8865 break;
8866 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008867 break;
8868
8869 case ISD::SETOGT:
8870 case ISD::SETOGE:
8871 case ISD::SETGT:
8872 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008873 case ISD::SETUGT:
8874 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008875 // If LHS is NaN, an ordered comparison will be false and the result will
8876 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8877 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8878 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8879 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8880 break;
8881 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8882 // will return +0, so vmax can only be used for unsafe math or if one of
8883 // the operands is known to be nonzero.
8884 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008885 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008886 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8887 break;
8888 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008889 break;
8890 }
8891
8892 if (!Opcode)
8893 return SDValue();
8894 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8895}
8896
Evan Chenge721f5c2011-07-13 00:42:17 +00008897/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8898SDValue
8899ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8900 SDValue Cmp = N->getOperand(4);
8901 if (Cmp.getOpcode() != ARMISD::CMPZ)
8902 // Only looking at EQ and NE cases.
8903 return SDValue();
8904
8905 EVT VT = N->getValueType(0);
8906 DebugLoc dl = N->getDebugLoc();
8907 SDValue LHS = Cmp.getOperand(0);
8908 SDValue RHS = Cmp.getOperand(1);
8909 SDValue FalseVal = N->getOperand(0);
8910 SDValue TrueVal = N->getOperand(1);
8911 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008912 ARMCC::CondCodes CC =
8913 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008914
8915 // Simplify
8916 // mov r1, r0
8917 // cmp r1, x
8918 // mov r0, y
8919 // moveq r0, x
8920 // to
8921 // cmp r0, x
8922 // movne r0, y
8923 //
8924 // mov r1, r0
8925 // cmp r1, x
8926 // mov r0, x
8927 // movne r0, y
8928 // to
8929 // cmp r0, x
8930 // movne r0, y
8931 /// FIXME: Turn this into a target neutral optimization?
8932 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008933 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008934 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8935 N->getOperand(3), Cmp);
8936 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8937 SDValue ARMcc;
8938 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8939 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8940 N->getOperand(3), NewCmp);
8941 }
8942
8943 if (Res.getNode()) {
8944 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008945 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008946 // Capture demanded bits information that would be otherwise lost.
8947 if (KnownZero == 0xfffffffe)
8948 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8949 DAG.getValueType(MVT::i1));
8950 else if (KnownZero == 0xffffff00)
8951 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8952 DAG.getValueType(MVT::i8));
8953 else if (KnownZero == 0xffff0000)
8954 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8955 DAG.getValueType(MVT::i16));
8956 }
8957
8958 return Res;
8959}
8960
Dan Gohman475871a2008-07-27 21:46:04 +00008961SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008962 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008963 switch (N->getOpcode()) {
8964 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00008965 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00008966 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008967 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008968 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008969 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008970 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8971 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008972 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008973 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008974 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008975 case ISD::STORE: return PerformSTORECombine(N, DCI);
8976 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8977 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008978 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008979 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008980 case ISD::FP_TO_SINT:
8981 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8982 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008983 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008984 case ISD::SHL:
8985 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008986 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008987 case ISD::SIGN_EXTEND:
8988 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008989 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8990 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008991 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008992 case ARMISD::VLD2DUP:
8993 case ARMISD::VLD3DUP:
8994 case ARMISD::VLD4DUP:
8995 return CombineBaseUpdate(N, DCI);
8996 case ISD::INTRINSIC_VOID:
8997 case ISD::INTRINSIC_W_CHAIN:
8998 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8999 case Intrinsic::arm_neon_vld1:
9000 case Intrinsic::arm_neon_vld2:
9001 case Intrinsic::arm_neon_vld3:
9002 case Intrinsic::arm_neon_vld4:
9003 case Intrinsic::arm_neon_vld2lane:
9004 case Intrinsic::arm_neon_vld3lane:
9005 case Intrinsic::arm_neon_vld4lane:
9006 case Intrinsic::arm_neon_vst1:
9007 case Intrinsic::arm_neon_vst2:
9008 case Intrinsic::arm_neon_vst3:
9009 case Intrinsic::arm_neon_vst4:
9010 case Intrinsic::arm_neon_vst2lane:
9011 case Intrinsic::arm_neon_vst3lane:
9012 case Intrinsic::arm_neon_vst4lane:
9013 return CombineBaseUpdate(N, DCI);
9014 default: break;
9015 }
9016 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009017 }
Dan Gohman475871a2008-07-27 21:46:04 +00009018 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009019}
9020
Evan Cheng31959b12011-02-02 01:06:55 +00009021bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9022 EVT VT) const {
9023 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9024}
9025
Bill Wendlingaf566342009-08-15 21:21:19 +00009026bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00009027 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00009028 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00009029
9030 switch (VT.getSimpleVT().SimpleTy) {
9031 default:
9032 return false;
9033 case MVT::i8:
9034 case MVT::i16:
9035 case MVT::i32:
9036 return true;
Evan Chenga99c5082012-08-15 17:44:53 +00009037 case MVT::f64:
9038 return Subtarget->hasNEON();
Bill Wendlingaf566342009-08-15 21:21:19 +00009039 // FIXME: VLD1 etc with standard alignment is legal.
9040 }
9041}
9042
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009043static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9044 unsigned AlignCheck) {
9045 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9046 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9047}
9048
9049EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9050 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00009051 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009052 bool MemcpyStrSrc,
9053 MachineFunction &MF) const {
9054 const Function *F = MF.getFunction();
9055
9056 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00009057 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009058 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
9059 Subtarget->hasNEON()) {
9060 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
9061 return MVT::v4i32;
9062 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
9063 return MVT::v2i32;
9064 }
9065 }
9066
Lang Hames5207bf22011-11-08 18:56:23 +00009067 // Lowering to i32/i16 if the size permits.
9068 if (Size >= 4) {
9069 return MVT::i32;
9070 } else if (Size >= 2) {
9071 return MVT::i16;
9072 }
9073
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009074 // Let the target-independent logic figure it out.
9075 return MVT::Other;
9076}
9077
Evan Chenge6c835f2009-08-14 20:09:37 +00009078static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9079 if (V < 0)
9080 return false;
9081
9082 unsigned Scale = 1;
9083 switch (VT.getSimpleVT().SimpleTy) {
9084 default: return false;
9085 case MVT::i1:
9086 case MVT::i8:
9087 // Scale == 1;
9088 break;
9089 case MVT::i16:
9090 // Scale == 2;
9091 Scale = 2;
9092 break;
9093 case MVT::i32:
9094 // Scale == 4;
9095 Scale = 4;
9096 break;
9097 }
9098
9099 if ((V & (Scale - 1)) != 0)
9100 return false;
9101 V /= Scale;
9102 return V == (V & ((1LL << 5) - 1));
9103}
9104
9105static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9106 const ARMSubtarget *Subtarget) {
9107 bool isNeg = false;
9108 if (V < 0) {
9109 isNeg = true;
9110 V = - V;
9111 }
9112
9113 switch (VT.getSimpleVT().SimpleTy) {
9114 default: return false;
9115 case MVT::i1:
9116 case MVT::i8:
9117 case MVT::i16:
9118 case MVT::i32:
9119 // + imm12 or - imm8
9120 if (isNeg)
9121 return V == (V & ((1LL << 8) - 1));
9122 return V == (V & ((1LL << 12) - 1));
9123 case MVT::f32:
9124 case MVT::f64:
9125 // Same as ARM mode. FIXME: NEON?
9126 if (!Subtarget->hasVFP2())
9127 return false;
9128 if ((V & 3) != 0)
9129 return false;
9130 V >>= 2;
9131 return V == (V & ((1LL << 8) - 1));
9132 }
9133}
9134
Evan Chengb01fad62007-03-12 23:30:29 +00009135/// isLegalAddressImmediate - Return true if the integer value can be used
9136/// as the offset of the target addressing mode for load / store of the
9137/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009138static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009139 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009140 if (V == 0)
9141 return true;
9142
Evan Cheng65011532009-03-09 19:15:00 +00009143 if (!VT.isSimple())
9144 return false;
9145
Evan Chenge6c835f2009-08-14 20:09:37 +00009146 if (Subtarget->isThumb1Only())
9147 return isLegalT1AddressImmediate(V, VT);
9148 else if (Subtarget->isThumb2())
9149 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009150
Evan Chenge6c835f2009-08-14 20:09:37 +00009151 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009152 if (V < 0)
9153 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009154 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009155 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009156 case MVT::i1:
9157 case MVT::i8:
9158 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009159 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009160 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009162 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009163 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009164 case MVT::f32:
9165 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009166 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009167 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009168 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009169 return false;
9170 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009171 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009172 }
Evan Chenga8e29892007-01-19 07:51:42 +00009173}
9174
Evan Chenge6c835f2009-08-14 20:09:37 +00009175bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9176 EVT VT) const {
9177 int Scale = AM.Scale;
9178 if (Scale < 0)
9179 return false;
9180
9181 switch (VT.getSimpleVT().SimpleTy) {
9182 default: return false;
9183 case MVT::i1:
9184 case MVT::i8:
9185 case MVT::i16:
9186 case MVT::i32:
9187 if (Scale == 1)
9188 return true;
9189 // r + r << imm
9190 Scale = Scale & ~1;
9191 return Scale == 2 || Scale == 4 || Scale == 8;
9192 case MVT::i64:
9193 // r + r
9194 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9195 return true;
9196 return false;
9197 case MVT::isVoid:
9198 // Note, we allow "void" uses (basically, uses that aren't loads or
9199 // stores), because arm allows folding a scale into many arithmetic
9200 // operations. This should be made more precise and revisited later.
9201
9202 // Allow r << imm, but the imm has to be a multiple of two.
9203 if (Scale & 1) return false;
9204 return isPowerOf2_32(Scale);
9205 }
9206}
9207
Chris Lattner37caf8c2007-04-09 23:33:39 +00009208/// isLegalAddressingMode - Return true if the addressing mode represented
9209/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009210bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009211 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009212 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009213 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009214 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009215
Chris Lattner37caf8c2007-04-09 23:33:39 +00009216 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009217 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009218 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009219
Chris Lattner37caf8c2007-04-09 23:33:39 +00009220 switch (AM.Scale) {
9221 case 0: // no scale reg, must be "r+i" or "r", or "i".
9222 break;
9223 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009224 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009225 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009226 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009227 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009228 // ARM doesn't support any R+R*scale+imm addr modes.
9229 if (AM.BaseOffs)
9230 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009231
Bob Wilson2c7dab12009-04-08 17:55:28 +00009232 if (!VT.isSimple())
9233 return false;
9234
Evan Chenge6c835f2009-08-14 20:09:37 +00009235 if (Subtarget->isThumb2())
9236 return isLegalT2ScaledAddressingMode(AM, VT);
9237
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009238 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009239 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009240 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009241 case MVT::i1:
9242 case MVT::i8:
9243 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009244 if (Scale < 0) Scale = -Scale;
9245 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009246 return true;
9247 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009248 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009249 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009250 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009251 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009252 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009253 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009254 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009255
Owen Anderson825b72b2009-08-11 20:47:22 +00009256 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009257 // Note, we allow "void" uses (basically, uses that aren't loads or
9258 // stores), because arm allows folding a scale into many arithmetic
9259 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009260
Chris Lattner37caf8c2007-04-09 23:33:39 +00009261 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009262 if (Scale & 1) return false;
9263 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009264 }
Evan Chengb01fad62007-03-12 23:30:29 +00009265 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009266 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009267}
9268
Evan Cheng77e47512009-11-11 19:05:52 +00009269/// isLegalICmpImmediate - Return true if the specified immediate is legal
9270/// icmp immediate, that is the target has icmp instructions which can compare
9271/// a register against the immediate without having to materialize the
9272/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009273bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009274 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009275 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009276 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009277 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009278 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009279 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009280 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009281}
9282
Andrew Trick8d8d9612012-07-18 18:34:27 +00009283/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9284/// *or sub* immediate, that is the target has add or sub instructions which can
9285/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009286/// immediate into a register.
9287bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009288 // Same encoding for add/sub, just flip the sign.
9289 int64_t AbsImm = llvm::abs64(Imm);
9290 if (!Subtarget->isThumb())
9291 return ARM_AM::getSOImmVal(AbsImm) != -1;
9292 if (Subtarget->isThumb2())
9293 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9294 // Thumb1 only has 8-bit unsigned immediate.
9295 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009296}
9297
Owen Andersone50ed302009-08-10 22:56:29 +00009298static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009299 bool isSEXTLoad, SDValue &Base,
9300 SDValue &Offset, bool &isInc,
9301 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009302 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9303 return false;
9304
Owen Anderson825b72b2009-08-11 20:47:22 +00009305 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009306 // AddressingMode 3
9307 Base = Ptr->getOperand(0);
9308 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009309 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009310 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009311 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009312 isInc = false;
9313 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9314 return true;
9315 }
9316 }
9317 isInc = (Ptr->getOpcode() == ISD::ADD);
9318 Offset = Ptr->getOperand(1);
9319 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009320 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009321 // AddressingMode 2
9322 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009323 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009324 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009325 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009326 isInc = false;
9327 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9328 Base = Ptr->getOperand(0);
9329 return true;
9330 }
9331 }
9332
9333 if (Ptr->getOpcode() == ISD::ADD) {
9334 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009335 ARM_AM::ShiftOpc ShOpcVal=
9336 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009337 if (ShOpcVal != ARM_AM::no_shift) {
9338 Base = Ptr->getOperand(1);
9339 Offset = Ptr->getOperand(0);
9340 } else {
9341 Base = Ptr->getOperand(0);
9342 Offset = Ptr->getOperand(1);
9343 }
9344 return true;
9345 }
9346
9347 isInc = (Ptr->getOpcode() == ISD::ADD);
9348 Base = Ptr->getOperand(0);
9349 Offset = Ptr->getOperand(1);
9350 return true;
9351 }
9352
Jim Grosbache5165492009-11-09 00:11:35 +00009353 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009354 return false;
9355}
9356
Owen Andersone50ed302009-08-10 22:56:29 +00009357static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009358 bool isSEXTLoad, SDValue &Base,
9359 SDValue &Offset, bool &isInc,
9360 SelectionDAG &DAG) {
9361 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9362 return false;
9363
9364 Base = Ptr->getOperand(0);
9365 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9366 int RHSC = (int)RHS->getZExtValue();
9367 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9368 assert(Ptr->getOpcode() == ISD::ADD);
9369 isInc = false;
9370 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9371 return true;
9372 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9373 isInc = Ptr->getOpcode() == ISD::ADD;
9374 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9375 return true;
9376 }
9377 }
9378
9379 return false;
9380}
9381
Evan Chenga8e29892007-01-19 07:51:42 +00009382/// getPreIndexedAddressParts - returns true by value, base pointer and
9383/// offset pointer and addressing mode by reference if the node's address
9384/// can be legally represented as pre-indexed load / store address.
9385bool
Dan Gohman475871a2008-07-27 21:46:04 +00009386ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9387 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009388 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009389 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009390 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009391 return false;
9392
Owen Andersone50ed302009-08-10 22:56:29 +00009393 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009394 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009395 bool isSEXTLoad = false;
9396 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9397 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009398 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009399 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9400 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9401 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009402 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009403 } else
9404 return false;
9405
9406 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009407 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009408 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009409 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9410 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009411 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009412 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009413 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009414 if (!isLegal)
9415 return false;
9416
9417 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9418 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009419}
9420
9421/// getPostIndexedAddressParts - returns true by value, base pointer and
9422/// offset pointer and addressing mode by reference if this node can be
9423/// combined with a load / store to form a post-indexed load / store.
9424bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009425 SDValue &Base,
9426 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009427 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009428 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009429 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009430 return false;
9431
Owen Andersone50ed302009-08-10 22:56:29 +00009432 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009433 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009434 bool isSEXTLoad = false;
9435 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009436 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009437 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009438 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9439 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009440 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009441 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009442 } else
9443 return false;
9444
9445 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009446 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009447 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009448 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009449 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009450 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009451 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9452 isInc, DAG);
9453 if (!isLegal)
9454 return false;
9455
Evan Cheng28dad2a2010-05-18 21:31:17 +00009456 if (Ptr != Base) {
9457 // Swap base ptr and offset to catch more post-index load / store when
9458 // it's legal. In Thumb2 mode, offset must be an immediate.
9459 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9460 !Subtarget->isThumb2())
9461 std::swap(Base, Offset);
9462
9463 // Post-indexed load / store update the base pointer.
9464 if (Ptr != Base)
9465 return false;
9466 }
9467
Evan Chenge88d5ce2009-07-02 07:28:31 +00009468 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9469 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009470}
9471
Dan Gohman475871a2008-07-27 21:46:04 +00009472void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009473 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009474 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009475 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009476 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009477 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009478 switch (Op.getOpcode()) {
9479 default: break;
9480 case ARMISD::CMOV: {
9481 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009482 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009483 if (KnownZero == 0 && KnownOne == 0) return;
9484
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009485 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009486 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009487 KnownZero &= KnownZeroRHS;
9488 KnownOne &= KnownOneRHS;
9489 return;
9490 }
9491 }
9492}
9493
9494//===----------------------------------------------------------------------===//
9495// ARM Inline Assembly Support
9496//===----------------------------------------------------------------------===//
9497
Evan Cheng55d42002011-01-08 01:24:27 +00009498bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9499 // Looking for "rev" which is V6+.
9500 if (!Subtarget->hasV6Ops())
9501 return false;
9502
9503 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9504 std::string AsmStr = IA->getAsmString();
9505 SmallVector<StringRef, 4> AsmPieces;
9506 SplitString(AsmStr, AsmPieces, ";\n");
9507
9508 switch (AsmPieces.size()) {
9509 default: return false;
9510 case 1:
9511 AsmStr = AsmPieces[0];
9512 AsmPieces.clear();
9513 SplitString(AsmStr, AsmPieces, " \t,");
9514
9515 // rev $0, $1
9516 if (AsmPieces.size() == 3 &&
9517 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9518 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009519 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009520 if (Ty && Ty->getBitWidth() == 32)
9521 return IntrinsicLowering::LowerToByteSwap(CI);
9522 }
9523 break;
9524 }
9525
9526 return false;
9527}
9528
Evan Chenga8e29892007-01-19 07:51:42 +00009529/// getConstraintType - Given a constraint letter, return the type of
9530/// constraint it is for this target.
9531ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009532ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9533 if (Constraint.size() == 1) {
9534 switch (Constraint[0]) {
9535 default: break;
9536 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009537 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009538 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009539 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009540 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009541 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009542 // An address with a single base register. Due to the way we
9543 // currently handle addresses it is the same as an 'r' memory constraint.
9544 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009545 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009546 } else if (Constraint.size() == 2) {
9547 switch (Constraint[0]) {
9548 default: break;
9549 // All 'U+' constraints are addresses.
9550 case 'U': return C_Memory;
9551 }
Evan Chenga8e29892007-01-19 07:51:42 +00009552 }
Chris Lattner4234f572007-03-25 02:14:49 +00009553 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009554}
9555
John Thompson44ab89e2010-10-29 17:29:13 +00009556/// Examine constraint type and operand type and determine a weight value.
9557/// This object must already have been set up with the operand type
9558/// and the current alternative constraint selected.
9559TargetLowering::ConstraintWeight
9560ARMTargetLowering::getSingleConstraintMatchWeight(
9561 AsmOperandInfo &info, const char *constraint) const {
9562 ConstraintWeight weight = CW_Invalid;
9563 Value *CallOperandVal = info.CallOperandVal;
9564 // If we don't have a value, we can't do a match,
9565 // but allow it at the lowest weight.
9566 if (CallOperandVal == NULL)
9567 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009568 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009569 // Look at the constraint type.
9570 switch (*constraint) {
9571 default:
9572 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9573 break;
9574 case 'l':
9575 if (type->isIntegerTy()) {
9576 if (Subtarget->isThumb())
9577 weight = CW_SpecificReg;
9578 else
9579 weight = CW_Register;
9580 }
9581 break;
9582 case 'w':
9583 if (type->isFloatingPointTy())
9584 weight = CW_Register;
9585 break;
9586 }
9587 return weight;
9588}
9589
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009590typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9591RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009592ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009593 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009594 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009595 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009596 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009597 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009598 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009599 return RCPair(0U, &ARM::tGPRRegClass);
9600 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009601 case 'h': // High regs or no regs.
9602 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009603 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009604 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009605 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009606 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009607 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009609 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009610 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009611 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009612 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009613 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009614 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009615 case 'x':
9616 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009617 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009618 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009619 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009620 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009621 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009622 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009623 case 't':
9624 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009625 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009626 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009627 }
9628 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009629 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009630 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009631
Evan Chenga8e29892007-01-19 07:51:42 +00009632 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9633}
9634
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009635/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9636/// vector. If it is invalid, don't add anything to Ops.
9637void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009638 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009639 std::vector<SDValue>&Ops,
9640 SelectionDAG &DAG) const {
9641 SDValue Result(0, 0);
9642
Eric Christopher100c8332011-06-02 23:16:42 +00009643 // Currently only support length 1 constraints.
9644 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009645
Eric Christopher100c8332011-06-02 23:16:42 +00009646 char ConstraintLetter = Constraint[0];
9647 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009648 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009649 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009650 case 'I': case 'J': case 'K': case 'L':
9651 case 'M': case 'N': case 'O':
9652 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9653 if (!C)
9654 return;
9655
9656 int64_t CVal64 = C->getSExtValue();
9657 int CVal = (int) CVal64;
9658 // None of these constraints allow values larger than 32 bits. Check
9659 // that the value fits in an int.
9660 if (CVal != CVal64)
9661 return;
9662
Eric Christopher100c8332011-06-02 23:16:42 +00009663 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009664 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009665 // Constant suitable for movw, must be between 0 and
9666 // 65535.
9667 if (Subtarget->hasV6T2Ops())
9668 if (CVal >= 0 && CVal <= 65535)
9669 break;
9670 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009671 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009672 if (Subtarget->isThumb1Only()) {
9673 // This must be a constant between 0 and 255, for ADD
9674 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009675 if (CVal >= 0 && CVal <= 255)
9676 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009677 } else if (Subtarget->isThumb2()) {
9678 // A constant that can be used as an immediate value in a
9679 // data-processing instruction.
9680 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9681 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009682 } else {
9683 // A constant that can be used as an immediate value in a
9684 // data-processing instruction.
9685 if (ARM_AM::getSOImmVal(CVal) != -1)
9686 break;
9687 }
9688 return;
9689
9690 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009691 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009692 // This must be a constant between -255 and -1, for negated ADD
9693 // immediates. This can be used in GCC with an "n" modifier that
9694 // prints the negated value, for use with SUB instructions. It is
9695 // not useful otherwise but is implemented for compatibility.
9696 if (CVal >= -255 && CVal <= -1)
9697 break;
9698 } else {
9699 // This must be a constant between -4095 and 4095. It is not clear
9700 // what this constraint is intended for. Implemented for
9701 // compatibility with GCC.
9702 if (CVal >= -4095 && CVal <= 4095)
9703 break;
9704 }
9705 return;
9706
9707 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009708 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009709 // A 32-bit value where only one byte has a nonzero value. Exclude
9710 // zero to match GCC. This constraint is used by GCC internally for
9711 // constants that can be loaded with a move/shift combination.
9712 // It is not useful otherwise but is implemented for compatibility.
9713 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9714 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009715 } else if (Subtarget->isThumb2()) {
9716 // A constant whose bitwise inverse can be used as an immediate
9717 // value in a data-processing instruction. This can be used in GCC
9718 // with a "B" modifier that prints the inverted value, for use with
9719 // BIC and MVN instructions. It is not useful otherwise but is
9720 // implemented for compatibility.
9721 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9722 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009723 } else {
9724 // A constant whose bitwise inverse can be used as an immediate
9725 // value in a data-processing instruction. This can be used in GCC
9726 // with a "B" modifier that prints the inverted value, for use with
9727 // BIC and MVN instructions. It is not useful otherwise but is
9728 // implemented for compatibility.
9729 if (ARM_AM::getSOImmVal(~CVal) != -1)
9730 break;
9731 }
9732 return;
9733
9734 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009735 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009736 // This must be a constant between -7 and 7,
9737 // for 3-operand ADD/SUB immediate instructions.
9738 if (CVal >= -7 && CVal < 7)
9739 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009740 } else if (Subtarget->isThumb2()) {
9741 // A constant whose negation can be used as an immediate value in a
9742 // data-processing instruction. This can be used in GCC with an "n"
9743 // modifier that prints the negated value, for use with SUB
9744 // instructions. It is not useful otherwise but is implemented for
9745 // compatibility.
9746 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9747 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009748 } else {
9749 // A constant whose negation can be used as an immediate value in a
9750 // data-processing instruction. This can be used in GCC with an "n"
9751 // modifier that prints the negated value, for use with SUB
9752 // instructions. It is not useful otherwise but is implemented for
9753 // compatibility.
9754 if (ARM_AM::getSOImmVal(-CVal) != -1)
9755 break;
9756 }
9757 return;
9758
9759 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009760 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009761 // This must be a multiple of 4 between 0 and 1020, for
9762 // ADD sp + immediate.
9763 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9764 break;
9765 } else {
9766 // A power of two or a constant between 0 and 32. This is used in
9767 // GCC for the shift amount on shifted register operands, but it is
9768 // useful in general for any shift amounts.
9769 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9770 break;
9771 }
9772 return;
9773
9774 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009775 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009776 // This must be a constant between 0 and 31, for shift amounts.
9777 if (CVal >= 0 && CVal <= 31)
9778 break;
9779 }
9780 return;
9781
9782 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009783 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009784 // This must be a multiple of 4 between -508 and 508, for
9785 // ADD/SUB sp = sp + immediate.
9786 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9787 break;
9788 }
9789 return;
9790 }
9791 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9792 break;
9793 }
9794
9795 if (Result.getNode()) {
9796 Ops.push_back(Result);
9797 return;
9798 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009799 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009800}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009801
9802bool
9803ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9804 // The ARM target isn't yet aware of offsets.
9805 return false;
9806}
Evan Cheng39382422009-10-28 01:44:26 +00009807
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009808bool ARM::isBitFieldInvertedMask(unsigned v) {
9809 if (v == 0xffffffff)
9810 return 0;
9811 // there can be 1's on either or both "outsides", all the "inside"
9812 // bits must be 0's
9813 unsigned int lsb = 0, msb = 31;
9814 while (v & (1 << msb)) --msb;
9815 while (v & (1 << lsb)) ++lsb;
9816 for (unsigned int i = lsb; i <= msb; ++i) {
9817 if (v & (1 << i))
9818 return 0;
9819 }
9820 return 1;
9821}
9822
Evan Cheng39382422009-10-28 01:44:26 +00009823/// isFPImmLegal - Returns true if the target can instruction select the
9824/// specified FP immediate natively. If false, the legalizer will
9825/// materialize the FP immediate as a load from a constant pool.
9826bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9827 if (!Subtarget->hasVFP3())
9828 return false;
9829 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009830 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009831 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009832 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009833 return false;
9834}
Bob Wilson65ffec42010-09-21 17:56:22 +00009835
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009836/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009837/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9838/// specified in the intrinsic calls.
9839bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9840 const CallInst &I,
9841 unsigned Intrinsic) const {
9842 switch (Intrinsic) {
9843 case Intrinsic::arm_neon_vld1:
9844 case Intrinsic::arm_neon_vld2:
9845 case Intrinsic::arm_neon_vld3:
9846 case Intrinsic::arm_neon_vld4:
9847 case Intrinsic::arm_neon_vld2lane:
9848 case Intrinsic::arm_neon_vld3lane:
9849 case Intrinsic::arm_neon_vld4lane: {
9850 Info.opc = ISD::INTRINSIC_W_CHAIN;
9851 // Conservatively set memVT to the entire set of vectors loaded.
9852 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9853 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9854 Info.ptrVal = I.getArgOperand(0);
9855 Info.offset = 0;
9856 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9857 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9858 Info.vol = false; // volatile loads with NEON intrinsics not supported
9859 Info.readMem = true;
9860 Info.writeMem = false;
9861 return true;
9862 }
9863 case Intrinsic::arm_neon_vst1:
9864 case Intrinsic::arm_neon_vst2:
9865 case Intrinsic::arm_neon_vst3:
9866 case Intrinsic::arm_neon_vst4:
9867 case Intrinsic::arm_neon_vst2lane:
9868 case Intrinsic::arm_neon_vst3lane:
9869 case Intrinsic::arm_neon_vst4lane: {
9870 Info.opc = ISD::INTRINSIC_VOID;
9871 // Conservatively set memVT to the entire set of vectors stored.
9872 unsigned NumElts = 0;
9873 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009874 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009875 if (!ArgTy->isVectorTy())
9876 break;
9877 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9878 }
9879 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9880 Info.ptrVal = I.getArgOperand(0);
9881 Info.offset = 0;
9882 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9883 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9884 Info.vol = false; // volatile stores with NEON intrinsics not supported
9885 Info.readMem = false;
9886 Info.writeMem = true;
9887 return true;
9888 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009889 case Intrinsic::arm_strexd: {
9890 Info.opc = ISD::INTRINSIC_W_CHAIN;
9891 Info.memVT = MVT::i64;
9892 Info.ptrVal = I.getArgOperand(2);
9893 Info.offset = 0;
9894 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009895 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009896 Info.readMem = false;
9897 Info.writeMem = true;
9898 return true;
9899 }
9900 case Intrinsic::arm_ldrexd: {
9901 Info.opc = ISD::INTRINSIC_W_CHAIN;
9902 Info.memVT = MVT::i64;
9903 Info.ptrVal = I.getArgOperand(0);
9904 Info.offset = 0;
9905 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009906 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009907 Info.readMem = true;
9908 Info.writeMem = false;
9909 return true;
9910 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009911 default:
9912 break;
9913 }
9914
9915 return false;
9916}