blob: d139a568a74626256e68a1c43c5a61f0f74dbafc [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);
Jim Grosbach4346fa92012-10-12 22:59:21 +0000125 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper0faf46c2012-08-12 03:16:37 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 if (VT.isInteger()) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000128 setOperationAction(ISD::SHL, VT, Custom);
129 setOperationAction(ISD::SRA, VT, Custom);
130 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 }
132
133 // Promote all bit-wise operations.
134 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000135 setOperationAction(ISD::AND, VT, Promote);
136 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
137 setOperationAction(ISD::OR, VT, Promote);
138 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
139 setOperationAction(ISD::XOR, VT, Promote);
140 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141 }
Bob Wilson16330762009-09-16 00:17:28 +0000142
143 // Neon does not support vector divide/remainder operations.
Craig Topper0faf46c2012-08-12 03:16:37 +0000144 setOperationAction(ISD::SDIV, VT, Expand);
145 setOperationAction(ISD::UDIV, VT, Expand);
146 setOperationAction(ISD::FDIV, VT, Expand);
147 setOperationAction(ISD::SREM, VT, Expand);
148 setOperationAction(ISD::UREM, VT, Expand);
149 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000150}
151
Craig Topper0faf46c2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000155}
156
Craig Topper0faf46c2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000158 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000160}
161
Chris Lattnerf0144122009-07-28 03:13:23 +0000162static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
163 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000164 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000165
Chris Lattner80ec2792009-08-02 00:34:36 +0000166 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Evan Chenga8e29892007-01-19 07:51:42 +0000169ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000171 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000172 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000173 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000174
Duncan Sands28b77e92011-09-06 19:07:46 +0000175 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
176
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 // Uses VFP for Thumb libfuncs if available.
179 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
180 // Single-precision floating-point arithmetic.
181 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
182 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
183 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
184 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000185
Evan Chengb1df8f22007-04-27 08:15:43 +0000186 // Double-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
188 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
189 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
190 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Single-precision comparisons.
193 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
194 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
195 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
196 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
197 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
198 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
199 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
200 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000201
Evan Chengb1df8f22007-04-27 08:15:43 +0000202 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000210
Evan Chengb1df8f22007-04-27 08:15:43 +0000211 // Double-precision comparisons.
212 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
213 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
214 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
215 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
216 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
217 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
218 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
219 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000220
Evan Chengb1df8f22007-04-27 08:15:43 +0000221 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000229
Evan Chengb1df8f22007-04-27 08:15:43 +0000230 // Floating-point to integer conversions.
231 // i64 conversions are done via library routines even when generating VFP
232 // instructions, so use the same ones.
233 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
234 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
235 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
236 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000237
Evan Chengb1df8f22007-04-27 08:15:43 +0000238 // Conversions between floating types.
239 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
240 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
241
242 // Integer to floating-point conversions.
243 // i64 conversions are done via library routines even when generating VFP
244 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000245 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
246 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000247 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
248 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
249 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
250 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
251 }
Evan Chenga8e29892007-01-19 07:51:42 +0000252 }
253
Bob Wilson2f954612009-05-22 17:38:41 +0000254 // These libcalls are not available in 32-bit.
255 setLibcallName(RTLIB::SHL_I128, 0);
256 setLibcallName(RTLIB::SRL_I128, 0);
257 setLibcallName(RTLIB::SRA_I128, 0);
258
Evan Cheng07043272012-02-21 20:46:00 +0000259 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000260 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000261 // RTABI chapter 4.1.2, Table 2
262 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
263 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
264 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
265 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
266 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
268 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
269 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
270
271 // Double-precision floating-point comparison helper functions
272 // RTABI chapter 4.1.2, Table 3
273 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
274 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
275 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
276 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
277 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
278 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
279 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
280 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
281 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
282 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
284 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
286 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
287 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
288 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
289 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
297
298 // Single-precision floating-point arithmetic helper functions
299 // RTABI chapter 4.1.2, Table 4
300 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
301 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
302 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
303 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
304 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
306 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
307 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
308
309 // Single-precision floating-point comparison helper functions
310 // RTABI chapter 4.1.2, Table 5
311 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
312 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
313 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
314 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
315 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
316 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
317 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
318 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
319 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
320 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
322 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
324 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
325 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
326 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
327 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
335
336 // Floating-point to integer conversions.
337 // RTABI chapter 4.1.2, Table 6
338 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
339 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
340 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
341 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
342 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
343 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
345 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
346 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
354
355 // Conversions between floating types.
356 // RTABI chapter 4.1.2, Table 7
357 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
358 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
359 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000360 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000361
362 // Integer to floating-point conversions.
363 // RTABI chapter 4.1.2, Table 8
364 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
365 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
366 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
367 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
368 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
369 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
370 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
371 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
372 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
380
381 // Long long helper functions
382 // RTABI chapter 4.2, Table 9
383 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000384 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
385 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
386 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
387 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
393
394 // Integer division functions
395 // RTABI chapter 4.3.1
396 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
397 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
398 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000399 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000400 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
401 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
402 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000403 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000404 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
406 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000407 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000408 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000410 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000411 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000412
413 // Memory operations
414 // RTABI chapter 4.3.4
415 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
416 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
417 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000418 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
420 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000421 }
422
Bob Wilson2fef4572011-10-07 16:59:21 +0000423 // Use divmod compiler-rt calls for iOS 5.0 and later.
424 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
425 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
426 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
427 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
428 }
429
David Goodwinf1daf7d2009-07-08 23:10:31 +0000430 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000432 else
Craig Topper420761a2012-04-20 07:30:17 +0000433 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000434 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
435 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000436 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000437 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000438 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000441 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
447 setTruncStoreAction((MVT::SimpleValueType)VT,
448 (MVT::SimpleValueType)InnerVT, Expand);
449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 }
453
Lang Hames45b5f882012-03-15 18:49:02 +0000454 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
455
Bob Wilson5bafff32009-06-22 23:27:02 +0000456 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 addDRTypeForNEON(MVT::v2f32);
458 addDRTypeForNEON(MVT::v8i8);
459 addDRTypeForNEON(MVT::v4i16);
460 addDRTypeForNEON(MVT::v2i32);
461 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000462
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 addQRTypeForNEON(MVT::v4f32);
464 addQRTypeForNEON(MVT::v2f64);
465 addQRTypeForNEON(MVT::v16i8);
466 addQRTypeForNEON(MVT::v8i16);
467 addQRTypeForNEON(MVT::v4i32);
468 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000469
Bob Wilson74dc72e2009-09-15 23:55:57 +0000470 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
471 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000472 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
473 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000474 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
475 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
476 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000477 // FIXME: Code duplication: FDIV and FREM are expanded always, see
478 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000479 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
480 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000481 // FIXME: Create unittest.
482 // In another words, find a way when "copysign" appears in DAG with vector
483 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000484 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000485 // FIXME: Code duplication: SETCC has custom operation action, see
486 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000487 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000488 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000489 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
490 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
491 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
492 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
493 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
494 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
495 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
498 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
499 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
500 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000501 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000502 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
503 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
504 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
505 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
506 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000507
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000508 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
509 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
510 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
512 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
515 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
517 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000518 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
519 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
520 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
521 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000522 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000523
Bob Wilson642b3292009-09-16 00:32:15 +0000524 // Neon does not support some operations on v1i64 and v2i64 types.
525 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000526 // Custom handling for some quad-vector types to detect VMULL.
527 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
528 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
529 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000530 // Custom handling for some vector types to avoid expensive expansions
531 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
533 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
534 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000535 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
536 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000537 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000538 // a destination type that is wider than the source, and nor does
539 // it have a FP_TO_[SU]INT instruction with a narrower destination than
540 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000541 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
542 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000543 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
544 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000545
Eli Friedman846ce8e2012-11-15 22:44:27 +0000546 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman43147af2012-11-17 01:52:46 +0000547 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedman846ce8e2012-11-15 22:44:27 +0000548
Bob Wilson1c3ef902011-02-07 17:43:21 +0000549 setTargetDAGCombine(ISD::INTRINSIC_VOID);
550 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000551 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
552 setTargetDAGCombine(ISD::SHL);
553 setTargetDAGCombine(ISD::SRL);
554 setTargetDAGCombine(ISD::SRA);
555 setTargetDAGCombine(ISD::SIGN_EXTEND);
556 setTargetDAGCombine(ISD::ZERO_EXTEND);
557 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000558 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000559 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000560 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000561 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
562 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000563 setTargetDAGCombine(ISD::FP_TO_SINT);
564 setTargetDAGCombine(ISD::FP_TO_UINT);
565 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000566
James Molloy873fd5f2012-02-20 09:24:05 +0000567 // It is legal to extload from v4i8 to v4i16 or v4i32.
568 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
569 MVT::v4i16, MVT::v2i16,
570 MVT::v2i32};
571 for (unsigned i = 0; i < 6; ++i) {
572 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
573 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
574 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
575 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000576 }
577
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000578 // ARM and Thumb2 support UMLAL/SMLAL.
579 if (!Subtarget->isThumb1Only())
580 setTargetDAGCombine(ISD::ADDC);
581
582
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000583 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000584
585 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000587
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000588 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000590
Evan Chenga8e29892007-01-19 07:51:42 +0000591 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000592 if (!Subtarget->isThumb1Only()) {
593 for (unsigned im = (unsigned)ISD::PRE_INC;
594 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setIndexedLoadAction(im, MVT::i1, Legal);
596 setIndexedLoadAction(im, MVT::i8, Legal);
597 setIndexedLoadAction(im, MVT::i16, Legal);
598 setIndexedLoadAction(im, MVT::i32, Legal);
599 setIndexedStoreAction(im, MVT::i1, Legal);
600 setIndexedStoreAction(im, MVT::i8, Legal);
601 setIndexedStoreAction(im, MVT::i16, Legal);
602 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000603 }
Evan Chenga8e29892007-01-19 07:51:42 +0000604 }
605
606 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000607 setOperationAction(ISD::MUL, MVT::i64, Expand);
608 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000609 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
611 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000612 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000613 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
614 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000615 setOperationAction(ISD::MULHS, MVT::i32, Expand);
616
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000617 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000618 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000619 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::SRL, MVT::i64, Custom);
621 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000622
Evan Cheng342e3162011-08-30 01:34:54 +0000623 if (!Subtarget->isThumb1Only()) {
624 // FIXME: We should do this for Thumb1 as well.
625 setOperationAction(ISD::ADDC, MVT::i32, Custom);
626 setOperationAction(ISD::ADDE, MVT::i32, Custom);
627 setOperationAction(ISD::SUBC, MVT::i32, Custom);
628 setOperationAction(ISD::SUBE, MVT::i32, Custom);
629 }
630
Evan Chenga8e29892007-01-19 07:51:42 +0000631 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000633 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000635 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000637
Chandler Carruth63974b22011-12-13 01:56:10 +0000638 // These just redirect to CTTZ and CTLZ on ARM.
639 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
640 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
641
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000642 // Only ARMv6 has BSWAP.
643 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000645
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000646 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
647 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
648 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000649 setOperationAction(ISD::SDIV, MVT::i32, Expand);
650 setOperationAction(ISD::UDIV, MVT::i32, Expand);
651 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::SREM, MVT::i32, Expand);
653 setOperationAction(ISD::UREM, MVT::i32, Expand);
654 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
655 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000656
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
658 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
659 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
660 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000661 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000662
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000663 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000664
Evan Chenga8e29892007-01-19 07:51:42 +0000665 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::VASTART, MVT::Other, Custom);
667 setOperationAction(ISD::VAARG, MVT::Other, Expand);
668 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
669 setOperationAction(ISD::VAEND, MVT::Other, Expand);
670 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
671 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000672
673 if (!Subtarget->isTargetDarwin()) {
674 // Non-Darwin platforms may return values in these registers via the
675 // personality function.
676 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
677 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
678 setExceptionPointerRegister(ARM::R0);
679 setExceptionSelectorRegister(ARM::R1);
680 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000681
Evan Cheng3a1588a2010-04-15 22:20:34 +0000682 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000683 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
684 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000685 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000686 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000687 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000688 // membarrier needs custom lowering; the rest are legal and handled
689 // normally.
690 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000691 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000692 // Custom lowering for 64-bit ops
693 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
694 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
695 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
696 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
697 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
Silviu Baranga35b3df62012-11-29 14:41:25 +0000698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
699 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
700 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
701 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
702 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000703 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000704 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
705 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000706 } else {
707 // Set them all for expansion, which will force libcalls.
708 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000709 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000710 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000711 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000712 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000713 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000714 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000715 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000716 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000717 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000718 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000719 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000720 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000721 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000722 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
723 // Unordered/Monotonic case.
724 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
725 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000726 // Since the libcalls include locking, fold in the fences
727 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000728 }
Evan Chenga8e29892007-01-19 07:51:42 +0000729
Evan Cheng416941d2010-11-04 05:19:35 +0000730 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000731
Eli Friedmana2c6f452010-06-26 04:36:50 +0000732 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
733 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
735 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000736 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000738
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000739 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
740 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000741 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000742 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000743 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000744 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
745 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000746
747 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000749 if (Subtarget->isTargetDarwin()) {
750 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
751 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000752 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000753 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000754
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::SETCC, MVT::i32, Expand);
756 setOperationAction(ISD::SETCC, MVT::f32, Expand);
757 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000758 setOperationAction(ISD::SELECT, MVT::i32, Custom);
759 setOperationAction(ISD::SELECT, MVT::f32, Custom);
760 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
762 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
763 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000764
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
766 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
767 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
768 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
769 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000770
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000771 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::FSIN, MVT::f64, Expand);
773 setOperationAction(ISD::FSIN, MVT::f32, Expand);
774 setOperationAction(ISD::FCOS, MVT::f32, Expand);
775 setOperationAction(ISD::FCOS, MVT::f64, Expand);
776 setOperationAction(ISD::FREM, MVT::f64, Expand);
777 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000778 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
779 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
781 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000782 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 setOperationAction(ISD::FPOW, MVT::f64, Expand);
784 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000785
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000786 if (!Subtarget->hasVFP4()) {
787 setOperationAction(ISD::FMA, MVT::f64, Expand);
788 setOperationAction(ISD::FMA, MVT::f32, Expand);
789 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000790
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000791 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000792 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000793 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
794 if (Subtarget->hasVFP2()) {
795 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
796 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
797 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
798 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
799 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000800 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000801 if (!Subtarget->hasFP16()) {
802 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
803 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000804 }
Evan Cheng110cf482008-04-01 01:50:16 +0000805 }
Evan Chenga8e29892007-01-19 07:51:42 +0000806
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000807 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000808 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000809 setTargetDAGCombine(ISD::ADD);
810 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000811 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000812 setTargetDAGCombine(ISD::AND);
813 setTargetDAGCombine(ISD::OR);
814 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000815
Evan Cheng5fb468a2012-02-23 02:58:19 +0000816 if (Subtarget->hasV6Ops())
817 setTargetDAGCombine(ISD::SRL);
818
Evan Chenga8e29892007-01-19 07:51:42 +0000819 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000820
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000821 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
822 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000823 setSchedulingPreference(Sched::RegPressure);
824 else
825 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000826
Evan Cheng05219282011-01-06 06:52:41 +0000827 //// temporary - rewrite interface to use type
828 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000829 maxStoresPerMemset = 16;
830 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000831
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000832 // On ARM arguments smaller than 4 bytes are extended, so all arguments
833 // are at least 4 bytes aligned.
834 setMinStackArgumentAlignment(4);
835
Evan Chengfff606d2010-09-24 19:07:23 +0000836 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000837
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000838 // Prefer likely predicted branches to selects on out-of-order cores.
Silviu Baranga616471d2012-09-13 15:05:10 +0000839 predictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000840
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000841 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000842}
843
Andrew Trick32cec0a2011-01-19 02:35:27 +0000844// FIXME: It might make sense to define the representative register class as the
845// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
846// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
847// SPR's representative would be DPR_VFP2. This should work well if register
848// pressure tracking were modified such that a register use would increment the
849// pressure of the register class's representative and all of it's super
850// classes' representatives transitively. We have not implemented this because
851// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000852// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000853// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000854std::pair<const TargetRegisterClass*, uint8_t>
855ARMTargetLowering::findRepresentativeClass(EVT VT) const{
856 const TargetRegisterClass *RRC = 0;
857 uint8_t Cost = 1;
858 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000859 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000860 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000861 // Use DPR as representative register class for all floating point
862 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
863 // the cost is 1 for both f32 and f64.
864 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000865 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000867 // When NEON is used for SP, only half of the register file is available
868 // because operations that define both SP and DP results will be constrained
869 // to the VFP2 class (D0-D15). We currently model this constraint prior to
870 // coalescing by double-counting the SP regs. See the FIXME above.
871 if (Subtarget->useNEONForSinglePrecisionFP())
872 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873 break;
874 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
875 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000876 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000877 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 break;
879 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000880 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000881 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000882 break;
883 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000884 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000885 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000886 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000887 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000888 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000889}
890
Evan Chenga8e29892007-01-19 07:51:42 +0000891const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
892 switch (Opcode) {
893 default: return 0;
894 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000895 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000896 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000897 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
898 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000899 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000900 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
901 case ARMISD::tCALL: return "ARMISD::tCALL";
902 case ARMISD::BRCOND: return "ARMISD::BRCOND";
903 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000904 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000905 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
906 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
907 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000908 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000909 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000910 case ARMISD::CMPFP: return "ARMISD::CMPFP";
911 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000912 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000913 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000914
Evan Chenga8e29892007-01-19 07:51:42 +0000915 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000916
Jim Grosbach3482c802010-01-18 19:58:49 +0000917 case ARMISD::RBIT: return "ARMISD::RBIT";
918
Bob Wilson76a312b2010-03-19 22:51:32 +0000919 case ARMISD::FTOSI: return "ARMISD::FTOSI";
920 case ARMISD::FTOUI: return "ARMISD::FTOUI";
921 case ARMISD::SITOF: return "ARMISD::SITOF";
922 case ARMISD::UITOF: return "ARMISD::UITOF";
923
Evan Chenga8e29892007-01-19 07:51:42 +0000924 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
925 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
926 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000927
Evan Cheng342e3162011-08-30 01:34:54 +0000928 case ARMISD::ADDC: return "ARMISD::ADDC";
929 case ARMISD::ADDE: return "ARMISD::ADDE";
930 case ARMISD::SUBC: return "ARMISD::SUBC";
931 case ARMISD::SUBE: return "ARMISD::SUBE";
932
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000933 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
934 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000935
Evan Chengc5942082009-10-28 06:55:03 +0000936 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
937 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
938
Dale Johannesen51e28e62010-06-03 21:09:53 +0000939 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000940
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000941 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000942
Evan Cheng86198642009-08-07 00:34:42 +0000943 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
944
Jim Grosbach3728e962009-12-10 00:11:09 +0000945 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000946 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000947
Evan Chengdfed19f2010-11-03 06:34:55 +0000948 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
949
Bob Wilson5bafff32009-06-22 23:27:02 +0000950 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000951 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000952 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000953 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
954 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000955 case ARMISD::VCGEU: return "ARMISD::VCGEU";
956 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000957 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
958 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000959 case ARMISD::VCGTU: return "ARMISD::VCGTU";
960 case ARMISD::VTST: return "ARMISD::VTST";
961
962 case ARMISD::VSHL: return "ARMISD::VSHL";
963 case ARMISD::VSHRs: return "ARMISD::VSHRs";
964 case ARMISD::VSHRu: return "ARMISD::VSHRu";
965 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
966 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
967 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
968 case ARMISD::VSHRN: return "ARMISD::VSHRN";
969 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
970 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
971 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
972 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
973 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
974 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
975 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
976 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
977 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
978 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
979 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
980 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
981 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
982 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000983 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000984 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000985 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000986 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000987 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000988 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000989 case ARMISD::VREV64: return "ARMISD::VREV64";
990 case ARMISD::VREV32: return "ARMISD::VREV32";
991 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000992 case ARMISD::VZIP: return "ARMISD::VZIP";
993 case ARMISD::VUZP: return "ARMISD::VUZP";
994 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000995 case ARMISD::VTBL1: return "ARMISD::VTBL1";
996 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000997 case ARMISD::VMULLs: return "ARMISD::VMULLs";
998 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000999 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1000 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001001 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001002 case ARMISD::FMAX: return "ARMISD::FMAX";
1003 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001004 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001005 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1006 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001007 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001008 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1009 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1010 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001011 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1012 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1013 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1014 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1015 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1016 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1017 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1018 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1019 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1020 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1021 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1022 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1023 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1024 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1025 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1026 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1027 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001028 }
1029}
1030
Duncan Sands28b77e92011-09-06 19:07:46 +00001031EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1032 if (!VT.isVector()) return getPointerTy();
1033 return VT.changeVectorElementTypeToInteger();
1034}
1035
Evan Cheng06b666c2010-05-15 02:18:07 +00001036/// getRegClassFor - Return the register class that should be used for the
1037/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001038const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001039 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1040 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1041 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001042 if (Subtarget->hasNEON()) {
1043 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001044 return &ARM::QQPRRegClass;
1045 if (VT == MVT::v8i64)
1046 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001047 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001048 return TargetLowering::getRegClassFor(VT);
1049}
1050
Eric Christopherab695882010-07-21 22:26:11 +00001051// Create a fast isel object.
1052FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001053ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1054 const TargetLibraryInfo *libInfo) const {
1055 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001056}
1057
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001058/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1059/// be used for loads / stores from the global.
1060unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1061 return (Subtarget->isThumb1Only() ? 127 : 4095);
1062}
1063
Evan Cheng1cc39842010-05-20 23:26:43 +00001064Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001065 unsigned NumVals = N->getNumValues();
1066 if (!NumVals)
1067 return Sched::RegPressure;
1068
1069 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001070 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001071 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001072 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001073 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001074 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001075 }
Evan Chengc10f5432010-05-28 23:25:23 +00001076
1077 if (!N->isMachineOpcode())
1078 return Sched::RegPressure;
1079
1080 // Load are scheduled for latency even if there instruction itinerary
1081 // is not available.
1082 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001083 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001084
Evan Chenge837dea2011-06-28 19:10:37 +00001085 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001086 return Sched::RegPressure;
1087 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001088 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001089 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001090
Evan Cheng1cc39842010-05-20 23:26:43 +00001091 return Sched::RegPressure;
1092}
1093
Evan Chenga8e29892007-01-19 07:51:42 +00001094//===----------------------------------------------------------------------===//
1095// Lowering Code
1096//===----------------------------------------------------------------------===//
1097
Evan Chenga8e29892007-01-19 07:51:42 +00001098/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1099static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1100 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001101 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001102 case ISD::SETNE: return ARMCC::NE;
1103 case ISD::SETEQ: return ARMCC::EQ;
1104 case ISD::SETGT: return ARMCC::GT;
1105 case ISD::SETGE: return ARMCC::GE;
1106 case ISD::SETLT: return ARMCC::LT;
1107 case ISD::SETLE: return ARMCC::LE;
1108 case ISD::SETUGT: return ARMCC::HI;
1109 case ISD::SETUGE: return ARMCC::HS;
1110 case ISD::SETULT: return ARMCC::LO;
1111 case ISD::SETULE: return ARMCC::LS;
1112 }
1113}
1114
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001115/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1116static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001117 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001118 CondCode2 = ARMCC::AL;
1119 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001120 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001121 case ISD::SETEQ:
1122 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1123 case ISD::SETGT:
1124 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1125 case ISD::SETGE:
1126 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1127 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001128 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001129 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1130 case ISD::SETO: CondCode = ARMCC::VC; break;
1131 case ISD::SETUO: CondCode = ARMCC::VS; break;
1132 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1133 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1134 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1135 case ISD::SETLT:
1136 case ISD::SETULT: CondCode = ARMCC::LT; break;
1137 case ISD::SETLE:
1138 case ISD::SETULE: CondCode = ARMCC::LE; break;
1139 case ISD::SETNE:
1140 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1141 }
Evan Chenga8e29892007-01-19 07:51:42 +00001142}
1143
Bob Wilson1f595bb2009-04-17 19:07:39 +00001144//===----------------------------------------------------------------------===//
1145// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001146//===----------------------------------------------------------------------===//
1147
1148#include "ARMGenCallingConv.inc"
1149
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001150/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1151/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001152CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001153 bool Return,
1154 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001155 switch (CC) {
1156 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001157 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001158 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001159 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001160 if (!Subtarget->isAAPCS_ABI())
1161 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1162 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1163 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1164 }
1165 // Fallthrough
1166 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001167 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001168 if (!Subtarget->isAAPCS_ABI())
1169 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1170 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001171 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1172 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001173 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1174 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1175 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001176 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001177 if (!isVarArg)
1178 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1179 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001180 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001181 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001182 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001183 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001184 case CallingConv::GHC:
1185 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001186 }
1187}
1188
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189/// LowerCallResult - Lower the result values of a call into the
1190/// appropriate copies out of appropriate physical registers.
1191SDValue
1192ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001193 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 const SmallVectorImpl<ISD::InputArg> &Ins,
1195 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001196 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001197
Bob Wilson1f595bb2009-04-17 19:07:39 +00001198 // Assign locations to each value returned by this call.
1199 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001200 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1201 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001203 CCAssignFnForNode(CallConv, /* Return*/ true,
1204 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001205
1206 // Copy all of the result registers out of their specified physreg.
1207 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1208 CCValAssign VA = RVLocs[i];
1209
Bob Wilson80915242009-04-25 00:33:20 +00001210 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001211 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001212 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001213 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001214 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001215 Chain = Lo.getValue(1);
1216 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001219 InFlag);
1220 Chain = Hi.getValue(1);
1221 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001222 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001223
Owen Anderson825b72b2009-08-11 20:47:22 +00001224 if (VA.getLocVT() == MVT::v2f64) {
1225 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1226 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1227 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001228
1229 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001231 Chain = Lo.getValue(1);
1232 InFlag = Lo.getValue(2);
1233 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001235 Chain = Hi.getValue(1);
1236 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001237 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1239 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001240 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001241 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001242 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1243 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001244 Chain = Val.getValue(1);
1245 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246 }
Bob Wilson80915242009-04-25 00:33:20 +00001247
1248 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001249 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001250 case CCValAssign::Full: break;
1251 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001252 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001253 break;
1254 }
1255
Dan Gohman98ca4f22009-08-05 01:29:28 +00001256 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 }
1258
Dan Gohman98ca4f22009-08-05 01:29:28 +00001259 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260}
1261
Bob Wilsondee46d72009-04-17 20:35:10 +00001262/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001263SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1265 SDValue StackPtr, SDValue Arg,
1266 DebugLoc dl, SelectionDAG &DAG,
1267 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001268 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001269 unsigned LocMemOffset = VA.getLocMemOffset();
1270 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1271 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001272 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001273 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001274 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001275}
1276
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001278 SDValue Chain, SDValue &Arg,
1279 RegsToPassVector &RegsToPass,
1280 CCValAssign &VA, CCValAssign &NextVA,
1281 SDValue &StackPtr,
1282 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001283 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001284
Jim Grosbache5165492009-11-09 00:11:35 +00001285 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001287 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1288
1289 if (NextVA.isRegLoc())
1290 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1291 else {
1292 assert(NextVA.isMemLoc());
1293 if (StackPtr.getNode() == 0)
1294 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1295
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1297 dl, DAG, NextVA,
1298 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001299 }
1300}
1301
Dan Gohman98ca4f22009-08-05 01:29:28 +00001302/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001303/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1304/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001305SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001306ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001307 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001308 SelectionDAG &DAG = CLI.DAG;
1309 DebugLoc &dl = CLI.DL;
1310 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1311 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1312 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1313 SDValue Chain = CLI.Chain;
1314 SDValue Callee = CLI.Callee;
1315 bool &isTailCall = CLI.IsTailCall;
1316 CallingConv::ID CallConv = CLI.CallConv;
1317 bool doesNotRet = CLI.DoesNotReturn;
1318 bool isVarArg = CLI.IsVarArg;
1319
Dale Johannesen51e28e62010-06-03 21:09:53 +00001320 MachineFunction &MF = DAG.getMachineFunction();
1321 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1322 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001323 // Disable tail calls if they're not supported.
1324 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001325 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001326 if (isTailCall) {
1327 // Check if it's really possible to do a tail call.
1328 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1329 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001330 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001331 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1332 // detected sibcalls.
1333 if (isTailCall) {
1334 ++NumTailCalls;
1335 IsSibCall = true;
1336 }
1337 }
Evan Chenga8e29892007-01-19 07:51:42 +00001338
Bob Wilson1f595bb2009-04-17 19:07:39 +00001339 // Analyze operands of the call, assigning locations to each operand.
1340 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001341 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1342 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001344 CCAssignFnForNode(CallConv, /* Return*/ false,
1345 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001346
Bob Wilson1f595bb2009-04-17 19:07:39 +00001347 // Get a count of how many bytes are to be pushed on the stack.
1348 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001349
Dale Johannesen51e28e62010-06-03 21:09:53 +00001350 // For tail calls, memory operands are available in our caller's stack.
1351 if (IsSibCall)
1352 NumBytes = 0;
1353
Evan Chenga8e29892007-01-19 07:51:42 +00001354 // Adjust the stack pointer for the new arguments...
1355 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001356 if (!IsSibCall)
1357 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001358
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001359 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001360
Bob Wilson5bafff32009-06-22 23:27:02 +00001361 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001362 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001363
Bob Wilson1f595bb2009-04-17 19:07:39 +00001364 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001365 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001366 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1367 i != e;
1368 ++i, ++realArgIdx) {
1369 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001370 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001372 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001373
Bob Wilson1f595bb2009-04-17 19:07:39 +00001374 // Promote the value if needed.
1375 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001376 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001377 case CCValAssign::Full: break;
1378 case CCValAssign::SExt:
1379 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1380 break;
1381 case CCValAssign::ZExt:
1382 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1383 break;
1384 case CCValAssign::AExt:
1385 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1386 break;
1387 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001388 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001389 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001390 }
1391
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001392 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001393 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 if (VA.getLocVT() == MVT::v2f64) {
1395 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1396 DAG.getConstant(0, MVT::i32));
1397 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1398 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001399
Dan Gohman98ca4f22009-08-05 01:29:28 +00001400 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001401 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1402
1403 VA = ArgLocs[++i]; // skip ahead to next loc
1404 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001405 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001406 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1407 } else {
1408 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001409
Dan Gohman98ca4f22009-08-05 01:29:28 +00001410 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1411 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001412 }
1413 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001414 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001415 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001416 }
1417 } else if (VA.isRegLoc()) {
1418 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001419 } else if (isByVal) {
1420 assert(VA.isMemLoc());
1421 unsigned offset = 0;
1422
1423 // True if this byval aggregate will be split between registers
1424 // and memory.
1425 if (CCInfo.isFirstByValRegValid()) {
1426 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1427 unsigned int i, j;
1428 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1429 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1430 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1431 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1432 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001433 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001434 MemOpChains.push_back(Load.getValue(1));
1435 RegsToPass.push_back(std::make_pair(j, Load));
1436 }
1437 offset = ARM::R4 - CCInfo.getFirstByValReg();
1438 CCInfo.clearFirstByValReg();
1439 }
1440
Manman Ren763a75d2012-06-01 02:44:42 +00001441 if (Flags.getByValSize() - 4*offset > 0) {
1442 unsigned LocMemOffset = VA.getLocMemOffset();
1443 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1444 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1445 StkPtrOff);
1446 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1447 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1448 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1449 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001450 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001451
Manman Ren763a75d2012-06-01 02:44:42 +00001452 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001453 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001454 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1455 Ops, array_lengthof(Ops)));
1456 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001457 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001458 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001459
Dan Gohman98ca4f22009-08-05 01:29:28 +00001460 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1461 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001462 }
Evan Chenga8e29892007-01-19 07:51:42 +00001463 }
1464
1465 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001466 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001467 &MemOpChains[0], MemOpChains.size());
1468
1469 // Build a sequence of copy-to-reg nodes chained together with token chain
1470 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001471 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001472 // Tail call byval lowering might overwrite argument registers so in case of
1473 // tail call optimization the copies to registers are lowered later.
1474 if (!isTailCall)
1475 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1476 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1477 RegsToPass[i].second, InFlag);
1478 InFlag = Chain.getValue(1);
1479 }
Evan Chenga8e29892007-01-19 07:51:42 +00001480
Dale Johannesen51e28e62010-06-03 21:09:53 +00001481 // For tail calls lower the arguments to the 'real' stack slot.
1482 if (isTailCall) {
1483 // Force all the incoming stack arguments to be loaded from the stack
1484 // before any new outgoing arguments are stored to the stack, because the
1485 // outgoing stack slots may alias the incoming argument stack slots, and
1486 // the alias isn't otherwise explicit. This is slightly more conservative
1487 // than necessary, because it means that each store effectively depends
1488 // on every argument instead of just those arguments it would clobber.
1489
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001490 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001491 InFlag = SDValue();
1492 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1493 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1494 RegsToPass[i].second, InFlag);
1495 InFlag = Chain.getValue(1);
1496 }
1497 InFlag =SDValue();
1498 }
1499
Bill Wendling056292f2008-09-16 21:48:12 +00001500 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1501 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1502 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001503 bool isDirect = false;
1504 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001505 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001506 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001507
1508 if (EnableARMLongCalls) {
1509 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1510 && "long-calls with non-static relocation model!");
1511 // Handle a global address or an external symbol. If it's not one of
1512 // those, the target's already in a register, so we don't need to do
1513 // anything extra.
1514 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001515 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001516 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001517 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001518 ARMConstantPoolValue *CPV =
1519 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1520
Jim Grosbache7b52522010-04-14 22:28:31 +00001521 // Get the address of the callee into a register
1522 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1523 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1524 Callee = DAG.getLoad(getPointerTy(), dl,
1525 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001526 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001527 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001528 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1529 const char *Sym = S->getSymbol();
1530
1531 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001532 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001533 ARMConstantPoolValue *CPV =
1534 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1535 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001536 // Get the address of the callee into a register
1537 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1538 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1539 Callee = DAG.getLoad(getPointerTy(), dl,
1540 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001541 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001542 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001543 }
1544 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001545 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001546 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001547 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001548 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001549 getTargetMachine().getRelocationModel() != Reloc::Static;
1550 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001551 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001552 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001553 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001554 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001555 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001556 ARMConstantPoolValue *CPV =
1557 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001558 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001560 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001561 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001562 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001563 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001564 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001565 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001566 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001567 } else {
1568 // On ELF targets for PIC code, direct calls should go through the PLT
1569 unsigned OpFlags = 0;
1570 if (Subtarget->isTargetELF() &&
1571 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1572 OpFlags = ARMII::MO_PLT;
1573 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1574 }
Bill Wendling056292f2008-09-16 21:48:12 +00001575 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001576 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001577 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001578 getTargetMachine().getRelocationModel() != Reloc::Static;
1579 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001580 // tBX takes a register source operand.
1581 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001582 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001583 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001584 ARMConstantPoolValue *CPV =
1585 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1586 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001587 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001589 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001590 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001591 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001592 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001593 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001594 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001595 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001596 } else {
1597 unsigned OpFlags = 0;
1598 // On ELF targets for PIC code, direct calls should go through the PLT
1599 if (Subtarget->isTargetELF() &&
1600 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1601 OpFlags = ARMII::MO_PLT;
1602 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1603 }
Evan Chenga8e29892007-01-19 07:51:42 +00001604 }
1605
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001606 // FIXME: handle tail calls differently.
1607 unsigned CallOpc;
Quentin Colombet9a419f62012-10-30 16:32:52 +00001608 bool HasMinSizeAttr = MF.getFunction()->getFnAttributes().
1609 hasAttribute(Attributes::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001610 if (Subtarget->isThumb()) {
1611 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001612 CallOpc = ARMISD::CALL_NOLINK;
1613 else
1614 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1615 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001616 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001617 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001618 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001619 // Emit regular call when code size is the priority
1620 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001621 // "mov lr, pc; b _foo" to avoid confusing the RSP
1622 CallOpc = ARMISD::CALL_NOLINK;
1623 else
1624 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001625 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001626
Dan Gohman475871a2008-07-27 21:46:04 +00001627 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001628 Ops.push_back(Chain);
1629 Ops.push_back(Callee);
1630
1631 // Add argument registers to the end of the list so that they are known live
1632 // into the call.
1633 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1634 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1635 RegsToPass[i].second.getValueType()));
1636
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001637 // Add a register mask operand representing the call-preserved registers.
1638 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1639 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1640 assert(Mask && "Missing call preserved mask for calling convention");
1641 Ops.push_back(DAG.getRegisterMask(Mask));
1642
Gabor Greifba36cb52008-08-28 21:40:38 +00001643 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001644 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001645
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001646 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001647 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001648 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001649
Duncan Sands4bdcb612008-07-02 17:40:58 +00001650 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001651 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001652 InFlag = Chain.getValue(1);
1653
Chris Lattnere563bbc2008-10-11 22:08:30 +00001654 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1655 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001657 InFlag = Chain.getValue(1);
1658
Bob Wilson1f595bb2009-04-17 19:07:39 +00001659 // Handle result values, copying them out of physregs into vregs that we
1660 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001661 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1662 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001663}
1664
Stuart Hastingsf222e592011-02-28 17:17:53 +00001665/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001666/// on the stack. Remember the next parameter register to allocate,
1667/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001668/// this.
1669void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001670ARMTargetLowering::HandleByVal(
1671 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001672 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1673 assert((State->getCallOrPrologue() == Prologue ||
1674 State->getCallOrPrologue() == Call) &&
1675 "unhandled ParmContext");
1676 if ((!State->isFirstByValRegValid()) &&
1677 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001678 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1679 unsigned AlignInRegs = Align / 4;
1680 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1681 for (unsigned i = 0; i < Waste; ++i)
1682 reg = State->AllocateReg(GPRArgRegs, 4);
1683 }
1684 if (reg != 0) {
1685 State->setFirstByValReg(reg);
1686 // At a call site, a byval parameter that is split between
1687 // registers and memory needs its size truncated here. In a
1688 // function prologue, such byval parameters are reassembled in
1689 // memory, and are not truncated.
1690 if (State->getCallOrPrologue() == Call) {
1691 unsigned excess = 4 * (ARM::R4 - reg);
1692 assert(size >= excess && "expected larger existing stack allocation");
1693 size -= excess;
1694 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001695 }
1696 }
1697 // Confiscate any remaining parameter registers to preclude their
1698 // assignment to subsequent parameters.
1699 while (State->AllocateReg(GPRArgRegs, 4))
1700 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001701}
1702
Dale Johannesen51e28e62010-06-03 21:09:53 +00001703/// MatchingStackOffset - Return true if the given stack call argument is
1704/// already available in the same position (relatively) of the caller's
1705/// incoming argument stack.
1706static
1707bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1708 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001709 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001710 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1711 int FI = INT_MAX;
1712 if (Arg.getOpcode() == ISD::CopyFromReg) {
1713 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001714 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001715 return false;
1716 MachineInstr *Def = MRI->getVRegDef(VR);
1717 if (!Def)
1718 return false;
1719 if (!Flags.isByVal()) {
1720 if (!TII->isLoadFromStackSlot(Def, FI))
1721 return false;
1722 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001723 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001724 }
1725 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1726 if (Flags.isByVal())
1727 // ByVal argument is passed in as a pointer but it's now being
1728 // dereferenced. e.g.
1729 // define @foo(%struct.X* %A) {
1730 // tail call @bar(%struct.X* byval %A)
1731 // }
1732 return false;
1733 SDValue Ptr = Ld->getBasePtr();
1734 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1735 if (!FINode)
1736 return false;
1737 FI = FINode->getIndex();
1738 } else
1739 return false;
1740
1741 assert(FI != INT_MAX);
1742 if (!MFI->isFixedObjectIndex(FI))
1743 return false;
1744 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1745}
1746
1747/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1748/// for tail call optimization. Targets which want to do tail call
1749/// optimization should implement this function.
1750bool
1751ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1752 CallingConv::ID CalleeCC,
1753 bool isVarArg,
1754 bool isCalleeStructRet,
1755 bool isCallerStructRet,
1756 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001757 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001758 const SmallVectorImpl<ISD::InputArg> &Ins,
1759 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001760 const Function *CallerF = DAG.getMachineFunction().getFunction();
1761 CallingConv::ID CallerCC = CallerF->getCallingConv();
1762 bool CCMatch = CallerCC == CalleeCC;
1763
1764 // Look for obvious safe cases to perform tail call optimization that do not
1765 // require ABI changes. This is what gcc calls sibcall.
1766
Jim Grosbach7616b642010-06-16 23:45:49 +00001767 // Do not sibcall optimize vararg calls unless the call site is not passing
1768 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001769 if (isVarArg && !Outs.empty())
1770 return false;
1771
1772 // Also avoid sibcall optimization if either caller or callee uses struct
1773 // return semantics.
1774 if (isCalleeStructRet || isCallerStructRet)
1775 return false;
1776
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001777 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001778 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1779 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1780 // support in the assembler and linker to be used. This would need to be
1781 // fixed to fully support tail calls in Thumb1.
1782 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001783 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1784 // LR. This means if we need to reload LR, it takes an extra instructions,
1785 // which outweighs the value of the tail call; but here we don't know yet
1786 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001787 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001788 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001789
1790 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1791 // but we need to make sure there are enough registers; the only valid
1792 // registers are the 4 used for parameters. We don't currently do this
1793 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001794 if (Subtarget->isThumb1Only())
1795 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001796
Dale Johannesen51e28e62010-06-03 21:09:53 +00001797 // If the calling conventions do not match, then we'd better make sure the
1798 // results are returned in the same way as what the caller expects.
1799 if (!CCMatch) {
1800 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001801 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1802 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001803 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1804
1805 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001806 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1807 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001808 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1809
1810 if (RVLocs1.size() != RVLocs2.size())
1811 return false;
1812 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1813 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1814 return false;
1815 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1816 return false;
1817 if (RVLocs1[i].isRegLoc()) {
1818 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1819 return false;
1820 } else {
1821 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1822 return false;
1823 }
1824 }
1825 }
1826
Manman Rene6c3cc82012-10-12 23:39:43 +00001827 // If Caller's vararg or byval argument has been split between registers and
1828 // stack, do not perform tail call, since part of the argument is in caller's
1829 // local frame.
1830 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1831 getInfo<ARMFunctionInfo>();
1832 if (AFI_Caller->getVarArgsRegSaveSize())
1833 return false;
1834
Dale Johannesen51e28e62010-06-03 21:09:53 +00001835 // If the callee takes no arguments then go on to check the results of the
1836 // call.
1837 if (!Outs.empty()) {
1838 // Check if stack adjustment is needed. For now, do not do this if any
1839 // argument is passed on the stack.
1840 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001841 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1842 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001843 CCInfo.AnalyzeCallOperands(Outs,
1844 CCAssignFnForNode(CalleeCC, false, isVarArg));
1845 if (CCInfo.getNextStackOffset()) {
1846 MachineFunction &MF = DAG.getMachineFunction();
1847
1848 // Check if the arguments are already laid out in the right way as
1849 // the caller's fixed stack objects.
1850 MachineFrameInfo *MFI = MF.getFrameInfo();
1851 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001852 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001853 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1854 i != e;
1855 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001856 CCValAssign &VA = ArgLocs[i];
1857 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001858 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001859 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001860 if (VA.getLocInfo() == CCValAssign::Indirect)
1861 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001862 if (VA.needsCustom()) {
1863 // f64 and vector types are split into multiple registers or
1864 // register/stack-slot combinations. The types will not match
1865 // the registers; give up on memory f64 refs until we figure
1866 // out what to do about this.
1867 if (!VA.isRegLoc())
1868 return false;
1869 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001870 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001871 if (RegVT == MVT::v2f64) {
1872 if (!ArgLocs[++i].isRegLoc())
1873 return false;
1874 if (!ArgLocs[++i].isRegLoc())
1875 return false;
1876 }
1877 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001878 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1879 MFI, MRI, TII))
1880 return false;
1881 }
1882 }
1883 }
1884 }
1885
1886 return true;
1887}
1888
Benjamin Kramer350c0082012-11-28 20:55:10 +00001889bool
1890ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1891 MachineFunction &MF, bool isVarArg,
1892 const SmallVectorImpl<ISD::OutputArg> &Outs,
1893 LLVMContext &Context) const {
1894 SmallVector<CCValAssign, 16> RVLocs;
1895 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1896 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1897 isVarArg));
1898}
1899
Dan Gohman98ca4f22009-08-05 01:29:28 +00001900SDValue
1901ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001902 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001904 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001905 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001906
Bob Wilsondee46d72009-04-17 20:35:10 +00001907 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001908 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001909
Bob Wilsondee46d72009-04-17 20:35:10 +00001910 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001911 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1912 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001913
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001915 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1916 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001917
1918 // If this is the first return lowered for this function, add
1919 // the regs to the liveout set for the function.
1920 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1921 for (unsigned i = 0; i != RVLocs.size(); ++i)
1922 if (RVLocs[i].isRegLoc())
1923 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001924 }
1925
Bob Wilson1f595bb2009-04-17 19:07:39 +00001926 SDValue Flag;
1927
1928 // Copy the result values into the output registers.
1929 for (unsigned i = 0, realRVLocIdx = 0;
1930 i != RVLocs.size();
1931 ++i, ++realRVLocIdx) {
1932 CCValAssign &VA = RVLocs[i];
1933 assert(VA.isRegLoc() && "Can only return in registers!");
1934
Dan Gohmanc9403652010-07-07 15:54:55 +00001935 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001936
1937 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001938 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001939 case CCValAssign::Full: break;
1940 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001941 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001942 break;
1943 }
1944
Bob Wilson1f595bb2009-04-17 19:07:39 +00001945 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001946 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001947 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1949 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001950 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001951 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001952
1953 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1954 Flag = Chain.getValue(1);
1955 VA = RVLocs[++i]; // skip ahead to next loc
1956 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1957 HalfGPRs.getValue(1), Flag);
1958 Flag = Chain.getValue(1);
1959 VA = RVLocs[++i]; // skip ahead to next loc
1960
1961 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1963 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001964 }
1965 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1966 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001967 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001968 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001969 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001970 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001971 VA = RVLocs[++i]; // skip ahead to next loc
1972 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1973 Flag);
1974 } else
1975 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1976
Bob Wilsondee46d72009-04-17 20:35:10 +00001977 // Guarantee that all emitted copies are
1978 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001979 Flag = Chain.getValue(1);
1980 }
1981
1982 SDValue result;
1983 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001984 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001985 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001986 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001987
1988 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001989}
1990
Evan Chengbf010eb2012-04-10 01:51:00 +00001991bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992 if (N->getNumValues() != 1)
1993 return false;
1994 if (!N->hasNUsesOfValue(1, 0))
1995 return false;
1996
Evan Chengbf010eb2012-04-10 01:51:00 +00001997 SDValue TCChain = Chain;
1998 SDNode *Copy = *N->use_begin();
1999 if (Copy->getOpcode() == ISD::CopyToReg) {
2000 // If the copy has a glue operand, we conservatively assume it isn't safe to
2001 // perform a tail call.
2002 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2003 return false;
2004 TCChain = Copy->getOperand(0);
2005 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2006 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002007 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002008 SmallPtrSet<SDNode*, 2> Copies;
2009 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002010 UI != UE; ++UI) {
2011 if (UI->getOpcode() != ISD::CopyToReg)
2012 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002013 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002014 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002015 if (Copies.size() > 2)
2016 return false;
2017
2018 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2019 UI != UE; ++UI) {
2020 SDValue UseChain = UI->getOperand(0);
2021 if (Copies.count(UseChain.getNode()))
2022 // Second CopyToReg
2023 Copy = *UI;
2024 else
2025 // First CopyToReg
2026 TCChain = UseChain;
2027 }
2028 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002029 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002030 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002031 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002032 Copy = *Copy->use_begin();
2033 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002034 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002035 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002036 } else {
2037 return false;
2038 }
2039
Evan Cheng1bf891a2010-12-01 22:59:46 +00002040 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002041 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2042 UI != UE; ++UI) {
2043 if (UI->getOpcode() != ARMISD::RET_FLAG)
2044 return false;
2045 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002046 }
2047
Evan Chengbf010eb2012-04-10 01:51:00 +00002048 if (!HasRet)
2049 return false;
2050
2051 Chain = TCChain;
2052 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002053}
2054
Evan Cheng485fafc2011-03-21 01:19:09 +00002055bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002056 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002057 return false;
2058
2059 if (!CI->isTailCall())
2060 return false;
2061
2062 return !Subtarget->isThumb1Only();
2063}
2064
Bob Wilsonb62d2572009-11-03 00:02:05 +00002065// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2066// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2067// one of the above mentioned nodes. It has to be wrapped because otherwise
2068// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2069// be used to form addressing mode. These wrapped nodes will be selected
2070// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002071static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002072 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002073 // FIXME there is no actual debug info here
2074 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002075 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002076 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002077 if (CP->isMachineConstantPoolEntry())
2078 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2079 CP->getAlignment());
2080 else
2081 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2082 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002084}
2085
Jim Grosbache1102ca2010-07-19 17:20:38 +00002086unsigned ARMTargetLowering::getJumpTableEncoding() const {
2087 return MachineJumpTableInfo::EK_Inline;
2088}
2089
Dan Gohmand858e902010-04-17 15:26:15 +00002090SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2091 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002092 MachineFunction &MF = DAG.getMachineFunction();
2093 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2094 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002095 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002096 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002097 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002098 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2099 SDValue CPAddr;
2100 if (RelocM == Reloc::Static) {
2101 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2102 } else {
2103 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002104 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002105 ARMConstantPoolValue *CPV =
2106 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2107 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002108 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2109 }
2110 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2111 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002112 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002113 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002114 if (RelocM == Reloc::Static)
2115 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002116 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002117 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002118}
2119
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002120// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002121SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002122ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002123 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002124 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002125 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002126 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002127 MachineFunction &MF = DAG.getMachineFunction();
2128 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002129 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002130 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002131 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2132 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002133 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002134 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002135 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002136 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002137 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002138 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002139
Evan Chenge7e0d622009-11-06 22:24:13 +00002140 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002141 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002142
2143 // call __tls_get_addr.
2144 ArgListTy Args;
2145 ArgListEntry Entry;
2146 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002147 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002148 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002149 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002150 TargetLowering::CallLoweringInfo CLI(Chain,
2151 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002152 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002153 0, CallingConv::C, /*isTailCall=*/false,
2154 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002155 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002156 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002157 return CallResult.first;
2158}
2159
2160// Lower ISD::GlobalTLSAddress using the "initial exec" or
2161// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002162SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002163ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002164 SelectionDAG &DAG,
2165 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002166 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002167 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue Offset;
2169 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002170 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002171 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002172 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002173
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002174 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002175 MachineFunction &MF = DAG.getMachineFunction();
2176 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002177 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002178 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002179 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2180 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002181 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2182 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2183 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002184 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002185 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002186 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002187 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002188 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002189 Chain = Offset.getValue(1);
2190
Evan Chenge7e0d622009-11-06 22:24:13 +00002191 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002192 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002193
Evan Cheng9eda6892009-10-31 03:39:36 +00002194 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002195 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002196 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002197 } else {
2198 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002199 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002200 ARMConstantPoolValue *CPV =
2201 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002202 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002204 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002205 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002206 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002207 }
2208
2209 // The address of the thread local variable is the add of the thread
2210 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002212}
2213
Dan Gohman475871a2008-07-27 21:46:04 +00002214SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002215ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002216 // TODO: implement the "local dynamic" model
2217 assert(Subtarget->isTargetELF() &&
2218 "TLS not implemented for non-ELF targets");
2219 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002220
2221 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2222
2223 switch (model) {
2224 case TLSModel::GeneralDynamic:
2225 case TLSModel::LocalDynamic:
2226 return LowerToTLSGeneralDynamicModel(GA, DAG);
2227 case TLSModel::InitialExec:
2228 case TLSModel::LocalExec:
2229 return LowerToTLSExecModels(GA, DAG, model);
2230 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002231 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002232}
2233
Dan Gohman475871a2008-07-27 21:46:04 +00002234SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002235 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002236 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002237 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002238 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002239 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2240 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002241 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002242 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002243 ARMConstantPoolConstant::Create(GV,
2244 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002245 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002246 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002247 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002248 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002249 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002250 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002251 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002252 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002253 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002254 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002255 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002256 MachinePointerInfo::getGOT(),
2257 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002258 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002259 }
2260
2261 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002262 // pair. This is always cheaper.
2263 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002264 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002265 // FIXME: Once remat is capable of dealing with instructions with register
2266 // operands, expand this into two nodes.
2267 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2268 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002269 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002270 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2271 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2272 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2273 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002274 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002275 }
2276}
2277
Dan Gohman475871a2008-07-27 21:46:04 +00002278SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002279 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002280 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002281 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002282 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002283 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002284 MachineFunction &MF = DAG.getMachineFunction();
2285 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2286
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002287 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2288 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002289 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002290 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002291 // FIXME: Once remat is capable of dealing with instructions with register
2292 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002293 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002294 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2295 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2296
Evan Cheng53519f02011-01-21 18:55:51 +00002297 unsigned Wrapper = (RelocM == Reloc::PIC_)
2298 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2299 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002300 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002301 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2302 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002303 MachinePointerInfo::getGOT(),
2304 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002305 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002306 }
2307
2308 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002309 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002310 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002311 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002312 } else {
2313 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002314 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2315 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002316 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2317 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002318 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002319 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002320 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002321
Evan Cheng9eda6892009-10-31 03:39:36 +00002322 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002323 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002324 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002325 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002326
2327 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002328 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002329 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002330 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002331
Evan Cheng63476a82009-09-03 07:04:02 +00002332 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002333 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002334 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002335
2336 return Result;
2337}
2338
Dan Gohman475871a2008-07-27 21:46:04 +00002339SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002340 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002341 assert(Subtarget->isTargetELF() &&
2342 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002343 MachineFunction &MF = DAG.getMachineFunction();
2344 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002345 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002346 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002347 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002348 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002349 ARMConstantPoolValue *CPV =
2350 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2351 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002352 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002354 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002355 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002356 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002357 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002358 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002359}
2360
Jim Grosbach0e0da732009-05-12 23:59:14 +00002361SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002362ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2363 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002364 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002365 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2366 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002367 Op.getOperand(1), Val);
2368}
2369
2370SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002371ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2372 DebugLoc dl = Op.getDebugLoc();
2373 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2374 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2375}
2376
2377SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002378ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002379 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002380 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002381 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002382 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002383 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002384 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002385 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002386 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2387 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002388 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002389 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002390 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002391 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002392 EVT PtrVT = getPointerTy();
2393 DebugLoc dl = Op.getDebugLoc();
2394 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2395 SDValue CPAddr;
2396 unsigned PCAdj = (RelocM != Reloc::PIC_)
2397 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002398 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002399 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2400 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002401 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002403 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002404 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002405 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002406 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002407
2408 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002409 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002410 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2411 }
2412 return Result;
2413 }
Evan Cheng92e39162011-03-29 23:06:19 +00002414 case Intrinsic::arm_neon_vmulls:
2415 case Intrinsic::arm_neon_vmullu: {
2416 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2417 ? ARMISD::VMULLs : ARMISD::VMULLu;
2418 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2419 Op.getOperand(1), Op.getOperand(2));
2420 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002421 }
2422}
2423
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002424static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002425 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002426 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002427 if (!Subtarget->hasDataBarrier()) {
2428 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2429 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2430 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002431 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002432 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002433 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002434 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002435 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002436
2437 SDValue Op5 = Op.getOperand(5);
2438 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2439 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2440 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2441 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2442
2443 ARM_MB::MemBOpt DMBOpt;
2444 if (isDeviceBarrier)
2445 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2446 else
2447 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2448 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2449 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002450}
2451
Eli Friedman26689ac2011-08-03 21:06:02 +00002452
2453static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2454 const ARMSubtarget *Subtarget) {
2455 // FIXME: handle "fence singlethread" more efficiently.
2456 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002457 if (!Subtarget->hasDataBarrier()) {
2458 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2459 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2460 // here.
2461 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2462 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002463 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002464 DAG.getConstant(0, MVT::i32));
2465 }
2466
Eli Friedman26689ac2011-08-03 21:06:02 +00002467 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002468 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002469}
2470
Evan Chengdfed19f2010-11-03 06:34:55 +00002471static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2472 const ARMSubtarget *Subtarget) {
2473 // ARM pre v5TE and Thumb1 does not have preload instructions.
2474 if (!(Subtarget->isThumb2() ||
2475 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2476 // Just preserve the chain.
2477 return Op.getOperand(0);
2478
2479 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002480 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2481 if (!isRead &&
2482 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2483 // ARMv7 with MP extension has PLDW.
2484 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002485
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002486 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2487 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002488 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002489 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002490 isData = ~isData & 1;
2491 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002492
2493 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002494 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2495 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002496}
2497
Dan Gohman1e93df62010-04-17 14:41:14 +00002498static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2499 MachineFunction &MF = DAG.getMachineFunction();
2500 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2501
Evan Chenga8e29892007-01-19 07:51:42 +00002502 // vastart just stores the address of the VarArgsFrameIndex slot into the
2503 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002504 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002505 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002506 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002507 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002508 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2509 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002510}
2511
Dan Gohman475871a2008-07-27 21:46:04 +00002512SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002513ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2514 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002515 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002516 MachineFunction &MF = DAG.getMachineFunction();
2517 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2518
Craig Topper44d23822012-02-22 05:59:10 +00002519 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002520 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002521 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002522 else
Craig Topper420761a2012-04-20 07:30:17 +00002523 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002524
2525 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002526 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002527 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002528
2529 SDValue ArgValue2;
2530 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002531 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002532 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002533
2534 // Create load node to retrieve arguments from the stack.
2535 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002536 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002537 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002538 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002539 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002540 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002542 }
2543
Jim Grosbache5165492009-11-09 00:11:35 +00002544 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002545}
2546
Stuart Hastingsc7315872011-04-20 16:47:52 +00002547void
2548ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2549 unsigned &VARegSize, unsigned &VARegSaveSize)
2550 const {
2551 unsigned NumGPRs;
2552 if (CCInfo.isFirstByValRegValid())
2553 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2554 else {
2555 unsigned int firstUnalloced;
2556 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2557 sizeof(GPRArgRegs) /
2558 sizeof(GPRArgRegs[0]));
2559 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2560 }
2561
2562 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2563 VARegSize = NumGPRs * 4;
2564 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2565}
2566
2567// The remaining GPRs hold either the beginning of variable-argument
2568// data, or the beginning of an aggregate passed by value (usuall
2569// byval). Either way, we allocate stack slots adjacent to the data
2570// provided by our caller, and store the unallocated registers there.
2571// If this is a variadic function, the va_list pointer will begin with
2572// these values; otherwise, this reassembles a (byval) structure that
2573// was split between registers and memory.
2574void
2575ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2576 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002577 const Value *OrigArg,
2578 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002579 unsigned ArgOffset,
2580 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002581 MachineFunction &MF = DAG.getMachineFunction();
2582 MachineFrameInfo *MFI = MF.getFrameInfo();
2583 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2584 unsigned firstRegToSaveIndex;
2585 if (CCInfo.isFirstByValRegValid())
2586 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2587 else {
2588 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2589 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2590 }
2591
2592 unsigned VARegSize, VARegSaveSize;
2593 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2594 if (VARegSaveSize) {
2595 // If this function is vararg, store any remaining integer argument regs
2596 // to their spots on the stack so that they may be loaded by deferencing
2597 // the result of va_next.
2598 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002599 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2600 ArgOffset + VARegSaveSize
2601 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002602 false));
2603 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2604 getPointerTy());
2605
2606 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002607 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002608 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002609 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002610 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002611 else
Craig Topper420761a2012-04-20 07:30:17 +00002612 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002613
2614 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2615 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2616 SDValue Store =
2617 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002618 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002619 false, false, 0);
2620 MemOps.push_back(Store);
2621 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2622 DAG.getConstant(4, getPointerTy()));
2623 }
2624 if (!MemOps.empty())
2625 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2626 &MemOps[0], MemOps.size());
2627 } else
2628 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002629 AFI->setVarArgsFrameIndex(
2630 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002631}
2632
Bob Wilson5bafff32009-06-22 23:27:02 +00002633SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002634ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002635 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002636 const SmallVectorImpl<ISD::InputArg>
2637 &Ins,
2638 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002639 SmallVectorImpl<SDValue> &InVals)
2640 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002641 MachineFunction &MF = DAG.getMachineFunction();
2642 MachineFrameInfo *MFI = MF.getFrameInfo();
2643
Bob Wilson1f595bb2009-04-17 19:07:39 +00002644 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2645
2646 // Assign locations to all of the incoming arguments.
2647 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002648 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2649 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002651 CCAssignFnForNode(CallConv, /* Return*/ false,
2652 isVarArg));
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002653
Bob Wilson1f595bb2009-04-17 19:07:39 +00002654 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002655 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002656 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002657 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2658 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002659 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2660 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002661 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2662 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002663 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002664 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002665 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002666
Bob Wilson1f595bb2009-04-17 19:07:39 +00002667 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002668 // f64 and vector types are split up into multiple registers or
2669 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002671 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002672 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002673 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002674 SDValue ArgValue2;
2675 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002676 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002677 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2678 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002679 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002680 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002681 } else {
2682 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2683 Chain, DAG, dl);
2684 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002685 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2686 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002687 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002688 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002689 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2690 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002691 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002692
Bob Wilson5bafff32009-06-22 23:27:02 +00002693 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002694 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002695
Owen Anderson825b72b2009-08-11 20:47:22 +00002696 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002697 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002698 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002699 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002700 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002701 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002702 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002703 RC = AFI->isThumb1OnlyFunction() ?
2704 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2705 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002706 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002707 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002708
2709 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002710 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002711 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002712 }
2713
2714 // If this is an 8 or 16-bit value, it is really passed promoted
2715 // to 32 bits. Insert an assert[sz]ext to capture this, then
2716 // truncate to the right size.
2717 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002718 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002719 case CCValAssign::Full: break;
2720 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002721 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002722 break;
2723 case CCValAssign::SExt:
2724 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2725 DAG.getValueType(VA.getValVT()));
2726 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2727 break;
2728 case CCValAssign::ZExt:
2729 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2730 DAG.getValueType(VA.getValVT()));
2731 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2732 break;
2733 }
2734
Dan Gohman98ca4f22009-08-05 01:29:28 +00002735 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002736
2737 } else { // VA.isRegLoc()
2738
2739 // sanity check
2740 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002741 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002742
Stuart Hastingsf222e592011-02-28 17:17:53 +00002743 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002744
Stuart Hastingsf222e592011-02-28 17:17:53 +00002745 // Some Ins[] entries become multiple ArgLoc[] entries.
2746 // Process them only once.
2747 if (index != lastInsIndex)
2748 {
2749 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002750 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002751 // This can be changed with more analysis.
2752 // In case of tail call optimization mark all arguments mutable.
2753 // Since they could be overwritten by lowering of arguments in case of
2754 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002755 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002756 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2757 if (!AFI->getVarArgsFrameIndex()) {
2758 VarArgStyleRegisters(CCInfo, DAG,
2759 dl, Chain, CurOrigArg,
2760 Ins[VA.getValNo()].PartOffset,
2761 VA.getLocMemOffset(),
2762 true /*force mutable frames*/);
2763 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2764 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2765 } else {
2766 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2767 VA.getLocMemOffset(), false);
2768 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2769 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002770 } else {
2771 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2772 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002773
Stuart Hastingsf222e592011-02-28 17:17:53 +00002774 // Create load nodes to retrieve arguments from the stack.
2775 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2776 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2777 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002778 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002779 }
2780 lastInsIndex = index;
2781 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002782 }
2783 }
2784
2785 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002786 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002787 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2788 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002789
Dan Gohman98ca4f22009-08-05 01:29:28 +00002790 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002791}
2792
2793/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002794static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002795 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002796 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002797 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002798 // Maybe this has already been legalized into the constant pool?
2799 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002800 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002801 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002802 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002803 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002804 }
2805 }
2806 return false;
2807}
2808
Evan Chenga8e29892007-01-19 07:51:42 +00002809/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2810/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002811SDValue
2812ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002813 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002814 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002815 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002816 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002817 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002818 // Constant does not fit, try adjusting it by one?
2819 switch (CC) {
2820 default: break;
2821 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002822 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002823 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002824 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002825 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002826 }
2827 break;
2828 case ISD::SETULT:
2829 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002830 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002831 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002832 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002833 }
2834 break;
2835 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002836 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002837 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002838 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002839 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002840 }
2841 break;
2842 case ISD::SETULE:
2843 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002844 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002845 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002846 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002847 }
2848 break;
2849 }
2850 }
2851 }
2852
2853 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002854 ARMISD::NodeType CompareType;
2855 switch (CondCode) {
2856 default:
2857 CompareType = ARMISD::CMP;
2858 break;
2859 case ARMCC::EQ:
2860 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002861 // Uses only Z Flag
2862 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002863 break;
2864 }
Evan Cheng218977b2010-07-13 19:27:42 +00002865 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002866 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002867}
2868
2869/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002870SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002871ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002872 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002873 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002874 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002875 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002876 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002877 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2878 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002879}
2880
Bob Wilson79f56c92011-03-08 01:17:20 +00002881/// duplicateCmp - Glue values can have only one use, so this function
2882/// duplicates a comparison node.
2883SDValue
2884ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2885 unsigned Opc = Cmp.getOpcode();
2886 DebugLoc DL = Cmp.getDebugLoc();
2887 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2888 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2889
2890 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2891 Cmp = Cmp.getOperand(0);
2892 Opc = Cmp.getOpcode();
2893 if (Opc == ARMISD::CMPFP)
2894 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2895 else {
2896 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2897 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2898 }
2899 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2900}
2901
Bill Wendlingde2b1512010-08-11 08:43:16 +00002902SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2903 SDValue Cond = Op.getOperand(0);
2904 SDValue SelectTrue = Op.getOperand(1);
2905 SDValue SelectFalse = Op.getOperand(2);
2906 DebugLoc dl = Op.getDebugLoc();
2907
2908 // Convert:
2909 //
2910 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2911 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2912 //
2913 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2914 const ConstantSDNode *CMOVTrue =
2915 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2916 const ConstantSDNode *CMOVFalse =
2917 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2918
2919 if (CMOVTrue && CMOVFalse) {
2920 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2921 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2922
2923 SDValue True;
2924 SDValue False;
2925 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2926 True = SelectTrue;
2927 False = SelectFalse;
2928 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2929 True = SelectFalse;
2930 False = SelectTrue;
2931 }
2932
2933 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002934 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002935 SDValue ARMcc = Cond.getOperand(2);
2936 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002937 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002938 assert(True.getValueType() == VT);
2939 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002940 }
2941 }
2942 }
2943
Dan Gohmandb953892012-02-24 00:09:36 +00002944 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2945 // undefined bits before doing a full-word comparison with zero.
2946 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2947 DAG.getConstant(1, Cond.getValueType()));
2948
Bill Wendlingde2b1512010-08-11 08:43:16 +00002949 return DAG.getSelectCC(dl, Cond,
2950 DAG.getConstant(0, Cond.getValueType()),
2951 SelectTrue, SelectFalse, ISD::SETNE);
2952}
2953
Dan Gohmand858e902010-04-17 15:26:15 +00002954SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002955 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002956 SDValue LHS = Op.getOperand(0);
2957 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002958 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002959 SDValue TrueVal = Op.getOperand(2);
2960 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002961 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002962
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002964 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002965 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002966 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002967 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002968 }
2969
2970 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002971 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002972
Evan Cheng218977b2010-07-13 19:27:42 +00002973 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2974 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002975 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002976 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002977 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002978 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002979 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002980 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002981 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002982 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002983 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002984 }
2985 return Result;
2986}
2987
Evan Cheng218977b2010-07-13 19:27:42 +00002988/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2989/// to morph to an integer compare sequence.
2990static bool canChangeToInt(SDValue Op, bool &SeenZero,
2991 const ARMSubtarget *Subtarget) {
2992 SDNode *N = Op.getNode();
2993 if (!N->hasOneUse())
2994 // Otherwise it requires moving the value from fp to integer registers.
2995 return false;
2996 if (!N->getNumValues())
2997 return false;
2998 EVT VT = Op.getValueType();
2999 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3000 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3001 // vmrs are very slow, e.g. cortex-a8.
3002 return false;
3003
3004 if (isFloatingPointZero(Op)) {
3005 SeenZero = true;
3006 return true;
3007 }
3008 return ISD::isNormalLoad(N);
3009}
3010
3011static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3012 if (isFloatingPointZero(Op))
3013 return DAG.getConstant(0, MVT::i32);
3014
3015 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3016 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003017 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003018 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003019 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003020
3021 llvm_unreachable("Unknown VFP cmp argument!");
3022}
3023
3024static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3025 SDValue &RetVal1, SDValue &RetVal2) {
3026 if (isFloatingPointZero(Op)) {
3027 RetVal1 = DAG.getConstant(0, MVT::i32);
3028 RetVal2 = DAG.getConstant(0, MVT::i32);
3029 return;
3030 }
3031
3032 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3033 SDValue Ptr = Ld->getBasePtr();
3034 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3035 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003036 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003037 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003038 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003039
3040 EVT PtrType = Ptr.getValueType();
3041 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3042 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3043 PtrType, Ptr, DAG.getConstant(4, PtrType));
3044 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3045 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003046 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003047 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003048 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003049 return;
3050 }
3051
3052 llvm_unreachable("Unknown VFP cmp argument!");
3053}
3054
3055/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3056/// f32 and even f64 comparisons to integer ones.
3057SDValue
3058ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3059 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003060 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003061 SDValue LHS = Op.getOperand(2);
3062 SDValue RHS = Op.getOperand(3);
3063 SDValue Dest = Op.getOperand(4);
3064 DebugLoc dl = Op.getDebugLoc();
3065
Evan Chengfc501a32012-03-01 23:27:13 +00003066 bool LHSSeenZero = false;
3067 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3068 bool RHSSeenZero = false;
3069 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3070 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003071 // If unsafe fp math optimization is enabled and there are no other uses of
3072 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003073 // to an integer comparison.
3074 if (CC == ISD::SETOEQ)
3075 CC = ISD::SETEQ;
3076 else if (CC == ISD::SETUNE)
3077 CC = ISD::SETNE;
3078
Evan Chengfc501a32012-03-01 23:27:13 +00003079 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003080 SDValue ARMcc;
3081 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003082 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3083 bitcastf32Toi32(LHS, DAG), Mask);
3084 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3085 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003086 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3087 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3088 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3089 Chain, Dest, ARMcc, CCR, Cmp);
3090 }
3091
3092 SDValue LHS1, LHS2;
3093 SDValue RHS1, RHS2;
3094 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3095 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003096 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3097 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003098 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3099 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003100 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003101 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3102 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3103 }
3104
3105 return SDValue();
3106}
3107
3108SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3109 SDValue Chain = Op.getOperand(0);
3110 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3111 SDValue LHS = Op.getOperand(2);
3112 SDValue RHS = Op.getOperand(3);
3113 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003114 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003115
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003117 SDValue ARMcc;
3118 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003120 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003121 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003122 }
3123
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003125
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003126 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003127 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3128 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3129 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3130 if (Result.getNode())
3131 return Result;
3132 }
3133
Evan Chenga8e29892007-01-19 07:51:42 +00003134 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003135 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003136
Evan Cheng218977b2010-07-13 19:27:42 +00003137 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3138 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003140 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003141 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003142 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003143 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003144 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3145 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003146 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003147 }
3148 return Res;
3149}
3150
Dan Gohmand858e902010-04-17 15:26:15 +00003151SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003152 SDValue Chain = Op.getOperand(0);
3153 SDValue Table = Op.getOperand(1);
3154 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003155 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003156
Owen Andersone50ed302009-08-10 22:56:29 +00003157 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003158 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3159 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003160 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003161 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003162 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003163 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3164 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003165 if (Subtarget->isThumb2()) {
3166 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3167 // which does another jump to the destination. This also makes it easier
3168 // to translate it to TBB / TBH later.
3169 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003171 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003172 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003173 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003174 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003175 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003176 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003177 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003178 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003180 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003181 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003182 MachinePointerInfo::getJumpTable(),
3183 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003184 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003185 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003186 }
Evan Chenga8e29892007-01-19 07:51:42 +00003187}
3188
Eli Friedman14e809c2011-11-09 23:36:02 +00003189static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003190 EVT VT = Op.getValueType();
3191 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003192
James Molloy873fd5f2012-02-20 09:24:05 +00003193 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3194 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3195 return Op;
3196 return DAG.UnrollVectorOp(Op.getNode());
3197 }
3198
3199 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3200 "Invalid type for custom lowering!");
3201 if (VT != MVT::v4i16)
3202 return DAG.UnrollVectorOp(Op.getNode());
3203
3204 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3205 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003206}
3207
Bob Wilson76a312b2010-03-19 22:51:32 +00003208static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003209 EVT VT = Op.getValueType();
3210 if (VT.isVector())
3211 return LowerVectorFP_TO_INT(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::FP_TO_SINT:
3219 Opc = ARMISD::FTOSI;
3220 break;
3221 case ISD::FP_TO_UINT:
3222 Opc = ARMISD::FTOUI;
3223 break;
3224 }
3225 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003226 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003227}
3228
Cameron Zwarich3007d332011-03-29 21:41:55 +00003229static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3230 EVT VT = Op.getValueType();
3231 DebugLoc dl = Op.getDebugLoc();
3232
Eli Friedman14e809c2011-11-09 23:36:02 +00003233 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3234 if (VT.getVectorElementType() == MVT::f32)
3235 return Op;
3236 return DAG.UnrollVectorOp(Op.getNode());
3237 }
3238
Duncan Sands1f6a3292011-08-12 14:54:45 +00003239 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3240 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003241 if (VT != MVT::v4f32)
3242 return DAG.UnrollVectorOp(Op.getNode());
3243
3244 unsigned CastOpc;
3245 unsigned Opc;
3246 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003247 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003248 case ISD::SINT_TO_FP:
3249 CastOpc = ISD::SIGN_EXTEND;
3250 Opc = ISD::SINT_TO_FP;
3251 break;
3252 case ISD::UINT_TO_FP:
3253 CastOpc = ISD::ZERO_EXTEND;
3254 Opc = ISD::UINT_TO_FP;
3255 break;
3256 }
3257
3258 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3259 return DAG.getNode(Opc, dl, VT, Op);
3260}
3261
Bob Wilson76a312b2010-03-19 22:51:32 +00003262static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3263 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003264 if (VT.isVector())
3265 return LowerVectorINT_TO_FP(Op, DAG);
3266
Bob Wilson76a312b2010-03-19 22:51:32 +00003267 DebugLoc dl = Op.getDebugLoc();
3268 unsigned Opc;
3269
3270 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003271 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003272 case ISD::SINT_TO_FP:
3273 Opc = ARMISD::SITOF;
3274 break;
3275 case ISD::UINT_TO_FP:
3276 Opc = ARMISD::UITOF;
3277 break;
3278 }
3279
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003280 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003281 return DAG.getNode(Opc, dl, VT, Op);
3282}
3283
Evan Cheng515fe3a2010-07-08 02:08:50 +00003284SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003285 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003286 SDValue Tmp0 = Op.getOperand(0);
3287 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003288 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003289 EVT VT = Op.getValueType();
3290 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003291 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3292 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3293 bool UseNEON = !InGPR && Subtarget->hasNEON();
3294
3295 if (UseNEON) {
3296 // Use VBSL to copy the sign bit.
3297 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3298 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3299 DAG.getTargetConstant(EncodedVal, MVT::i32));
3300 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3301 if (VT == MVT::f64)
3302 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3303 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3304 DAG.getConstant(32, MVT::i32));
3305 else /*if (VT == MVT::f32)*/
3306 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3307 if (SrcVT == MVT::f32) {
3308 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3309 if (VT == MVT::f64)
3310 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3311 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3312 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003313 } else if (VT == MVT::f32)
3314 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3315 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3316 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003317 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3318 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3319
3320 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3321 MVT::i32);
3322 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3323 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3324 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003325
Evan Chenge573fb32011-02-23 02:24:55 +00003326 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3327 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3328 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003329 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003330 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3331 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3332 DAG.getConstant(0, MVT::i32));
3333 } else {
3334 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3335 }
3336
3337 return Res;
3338 }
Evan Chengc143dd42011-02-11 02:28:55 +00003339
3340 // Bitcast operand 1 to i32.
3341 if (SrcVT == MVT::f64)
3342 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3343 &Tmp1, 1).getValue(1);
3344 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3345
Evan Chenge573fb32011-02-23 02:24:55 +00003346 // Or in the signbit with integer operations.
3347 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3348 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3349 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3350 if (VT == MVT::f32) {
3351 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3352 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3353 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3354 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003355 }
3356
Evan Chenge573fb32011-02-23 02:24:55 +00003357 // f64: Or the high part with signbit and then combine two parts.
3358 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3359 &Tmp0, 1);
3360 SDValue Lo = Tmp0.getValue(0);
3361 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3362 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3363 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003364}
3365
Evan Cheng2457f2c2010-05-22 01:47:14 +00003366SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3367 MachineFunction &MF = DAG.getMachineFunction();
3368 MachineFrameInfo *MFI = MF.getFrameInfo();
3369 MFI->setReturnAddressIsTaken(true);
3370
3371 EVT VT = Op.getValueType();
3372 DebugLoc dl = Op.getDebugLoc();
3373 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3374 if (Depth) {
3375 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3376 SDValue Offset = DAG.getConstant(4, MVT::i32);
3377 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3378 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003379 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003380 }
3381
3382 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003383 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003384 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3385}
3386
Dan Gohmand858e902010-04-17 15:26:15 +00003387SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003388 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3389 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003390
Owen Andersone50ed302009-08-10 22:56:29 +00003391 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003392 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3393 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003394 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003395 ? ARM::R7 : ARM::R11;
3396 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3397 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003398 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3399 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003400 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003401 return FrameAddr;
3402}
3403
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003404/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003405/// expand a bit convert where either the source or destination type is i64 to
3406/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3407/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3408/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003409static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003410 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3411 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003412 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003413
Bob Wilson9f3f0612010-04-17 05:30:19 +00003414 // This function is only supposed to be called for i64 types, either as the
3415 // source or destination of the bit convert.
3416 EVT SrcVT = Op.getValueType();
3417 EVT DstVT = N->getValueType(0);
3418 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003419 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003420
Bob Wilson9f3f0612010-04-17 05:30:19 +00003421 // Turn i64->f64 into VMOVDRR.
3422 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3424 DAG.getConstant(0, MVT::i32));
3425 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3426 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003427 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003428 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003429 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003430
Jim Grosbache5165492009-11-09 00:11:35 +00003431 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003432 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3433 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3434 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3435 // Merge the pieces into a single i64 value.
3436 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3437 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003438
Bob Wilson9f3f0612010-04-17 05:30:19 +00003439 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003440}
3441
Bob Wilson5bafff32009-06-22 23:27:02 +00003442/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003443/// Zero vectors are used to represent vector negation and in those cases
3444/// will be implemented with the NEON VNEG instruction. However, VNEG does
3445/// not support i64 elements, so sometimes the zero vectors will need to be
3446/// explicitly constructed. Regardless, use a canonical VMOV to create the
3447/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003448static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003449 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003450 // The canonical modified immediate encoding of a zero vector is....0!
3451 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3452 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3453 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003454 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003455}
3456
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003457/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3458/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003459SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3460 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003461 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3462 EVT VT = Op.getValueType();
3463 unsigned VTBits = VT.getSizeInBits();
3464 DebugLoc dl = Op.getDebugLoc();
3465 SDValue ShOpLo = Op.getOperand(0);
3466 SDValue ShOpHi = Op.getOperand(1);
3467 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003468 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003469 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003470
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003471 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3472
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003473 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3474 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3475 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3476 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3477 DAG.getConstant(VTBits, MVT::i32));
3478 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3479 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003480 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003481
3482 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3483 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003484 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003485 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003486 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003487 CCR, Cmp);
3488
3489 SDValue Ops[2] = { Lo, Hi };
3490 return DAG.getMergeValues(Ops, 2, dl);
3491}
3492
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003493/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3494/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003495SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3496 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003497 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3498 EVT VT = Op.getValueType();
3499 unsigned VTBits = VT.getSizeInBits();
3500 DebugLoc dl = Op.getDebugLoc();
3501 SDValue ShOpLo = Op.getOperand(0);
3502 SDValue ShOpHi = Op.getOperand(1);
3503 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003504 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003505
3506 assert(Op.getOpcode() == ISD::SHL_PARTS);
3507 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3508 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3509 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3510 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3511 DAG.getConstant(VTBits, MVT::i32));
3512 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3513 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3514
3515 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3516 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3517 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003518 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003519 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003520 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003521 CCR, Cmp);
3522
3523 SDValue Ops[2] = { Lo, Hi };
3524 return DAG.getMergeValues(Ops, 2, dl);
3525}
3526
Jim Grosbach4725ca72010-09-08 03:54:02 +00003527SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003528 SelectionDAG &DAG) const {
3529 // The rounding mode is in bits 23:22 of the FPSCR.
3530 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3531 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3532 // so that the shift + and get folded into a bitfield extract.
3533 DebugLoc dl = Op.getDebugLoc();
3534 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3535 DAG.getConstant(Intrinsic::arm_get_fpscr,
3536 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003537 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003538 DAG.getConstant(1U << 22, MVT::i32));
3539 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3540 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003541 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003542 DAG.getConstant(3, MVT::i32));
3543}
3544
Jim Grosbach3482c802010-01-18 19:58:49 +00003545static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3546 const ARMSubtarget *ST) {
3547 EVT VT = N->getValueType(0);
3548 DebugLoc dl = N->getDebugLoc();
3549
3550 if (!ST->hasV6T2Ops())
3551 return SDValue();
3552
3553 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3554 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3555}
3556
Bob Wilson5bafff32009-06-22 23:27:02 +00003557static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3558 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003559 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003560 DebugLoc dl = N->getDebugLoc();
3561
Bob Wilsond5448bb2010-11-18 21:16:28 +00003562 if (!VT.isVector())
3563 return SDValue();
3564
Bob Wilson5bafff32009-06-22 23:27:02 +00003565 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003566 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003567
Bob Wilsond5448bb2010-11-18 21:16:28 +00003568 // Left shifts translate directly to the vshiftu intrinsic.
3569 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003570 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003571 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3572 N->getOperand(0), N->getOperand(1));
3573
3574 assert((N->getOpcode() == ISD::SRA ||
3575 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3576
3577 // NEON uses the same intrinsics for both left and right shifts. For
3578 // right shifts, the shift amounts are negative, so negate the vector of
3579 // shift amounts.
3580 EVT ShiftVT = N->getOperand(1).getValueType();
3581 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3582 getZeroVector(ShiftVT, DAG, dl),
3583 N->getOperand(1));
3584 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3585 Intrinsic::arm_neon_vshifts :
3586 Intrinsic::arm_neon_vshiftu);
3587 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3588 DAG.getConstant(vshiftInt, MVT::i32),
3589 N->getOperand(0), NegatedCount);
3590}
3591
3592static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3593 const ARMSubtarget *ST) {
3594 EVT VT = N->getValueType(0);
3595 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003596
Eli Friedmance392eb2009-08-22 03:13:10 +00003597 // We can get here for a node like i32 = ISD::SHL i32, i64
3598 if (VT != MVT::i64)
3599 return SDValue();
3600
3601 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003602 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003603
Chris Lattner27a6c732007-11-24 07:07:01 +00003604 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3605 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003606 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003607 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003608
Chris Lattner27a6c732007-11-24 07:07:01 +00003609 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003610 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003611
Chris Lattner27a6c732007-11-24 07:07:01 +00003612 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003614 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003616 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003617
Chris Lattner27a6c732007-11-24 07:07:01 +00003618 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3619 // captures the result into a carry flag.
3620 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003621 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003622
Chris Lattner27a6c732007-11-24 07:07:01 +00003623 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003625
Chris Lattner27a6c732007-11-24 07:07:01 +00003626 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003627 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003628}
3629
Bob Wilson5bafff32009-06-22 23:27:02 +00003630static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3631 SDValue TmpOp0, TmpOp1;
3632 bool Invert = false;
3633 bool Swap = false;
3634 unsigned Opc = 0;
3635
3636 SDValue Op0 = Op.getOperand(0);
3637 SDValue Op1 = Op.getOperand(1);
3638 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003639 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003640 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3641 DebugLoc dl = Op.getDebugLoc();
3642
3643 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3644 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003645 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003646 case ISD::SETUNE:
3647 case ISD::SETNE: Invert = true; // Fallthrough
3648 case ISD::SETOEQ:
3649 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3650 case ISD::SETOLT:
3651 case ISD::SETLT: Swap = true; // Fallthrough
3652 case ISD::SETOGT:
3653 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3654 case ISD::SETOLE:
3655 case ISD::SETLE: Swap = true; // Fallthrough
3656 case ISD::SETOGE:
3657 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3658 case ISD::SETUGE: Swap = true; // Fallthrough
3659 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3660 case ISD::SETUGT: Swap = true; // Fallthrough
3661 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3662 case ISD::SETUEQ: Invert = true; // Fallthrough
3663 case ISD::SETONE:
3664 // Expand this to (OLT | OGT).
3665 TmpOp0 = Op0;
3666 TmpOp1 = Op1;
3667 Opc = ISD::OR;
3668 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3669 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3670 break;
3671 case ISD::SETUO: Invert = true; // Fallthrough
3672 case ISD::SETO:
3673 // Expand this to (OLT | OGE).
3674 TmpOp0 = Op0;
3675 TmpOp1 = Op1;
3676 Opc = ISD::OR;
3677 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3678 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3679 break;
3680 }
3681 } else {
3682 // Integer comparisons.
3683 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003684 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003685 case ISD::SETNE: Invert = true;
3686 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3687 case ISD::SETLT: Swap = true;
3688 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3689 case ISD::SETLE: Swap = true;
3690 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3691 case ISD::SETULT: Swap = true;
3692 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3693 case ISD::SETULE: Swap = true;
3694 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3695 }
3696
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003697 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003698 if (Opc == ARMISD::VCEQ) {
3699
3700 SDValue AndOp;
3701 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3702 AndOp = Op0;
3703 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3704 AndOp = Op1;
3705
3706 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003707 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003708 AndOp = AndOp.getOperand(0);
3709
3710 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3711 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003712 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3713 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003714 Invert = !Invert;
3715 }
3716 }
3717 }
3718
3719 if (Swap)
3720 std::swap(Op0, Op1);
3721
Owen Andersonc24cb352010-11-08 23:21:22 +00003722 // If one of the operands is a constant vector zero, attempt to fold the
3723 // comparison to a specialized compare-against-zero form.
3724 SDValue SingleOp;
3725 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3726 SingleOp = Op0;
3727 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3728 if (Opc == ARMISD::VCGE)
3729 Opc = ARMISD::VCLEZ;
3730 else if (Opc == ARMISD::VCGT)
3731 Opc = ARMISD::VCLTZ;
3732 SingleOp = Op1;
3733 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003734
Owen Andersonc24cb352010-11-08 23:21:22 +00003735 SDValue Result;
3736 if (SingleOp.getNode()) {
3737 switch (Opc) {
3738 case ARMISD::VCEQ:
3739 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3740 case ARMISD::VCGE:
3741 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3742 case ARMISD::VCLEZ:
3743 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3744 case ARMISD::VCGT:
3745 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3746 case ARMISD::VCLTZ:
3747 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3748 default:
3749 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3750 }
3751 } else {
3752 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3753 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003754
3755 if (Invert)
3756 Result = DAG.getNOT(dl, Result, VT);
3757
3758 return Result;
3759}
3760
Bob Wilsond3c42842010-06-14 22:19:57 +00003761/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3762/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003763/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003764static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3765 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003766 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003767 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003768
Bob Wilson827b2102010-06-15 19:05:35 +00003769 // SplatBitSize is set to the smallest size that splats the vector, so a
3770 // zero vector will always have SplatBitSize == 8. However, NEON modified
3771 // immediate instructions others than VMOV do not support the 8-bit encoding
3772 // of a zero vector, and the default encoding of zero is supposed to be the
3773 // 32-bit version.
3774 if (SplatBits == 0)
3775 SplatBitSize = 32;
3776
Bob Wilson5bafff32009-06-22 23:27:02 +00003777 switch (SplatBitSize) {
3778 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003779 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003780 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003781 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003782 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003783 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003784 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003785 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003786 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003787
3788 case 16:
3789 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003790 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003791 if ((SplatBits & ~0xff) == 0) {
3792 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003793 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003794 Imm = SplatBits;
3795 break;
3796 }
3797 if ((SplatBits & ~0xff00) == 0) {
3798 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003799 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003800 Imm = SplatBits >> 8;
3801 break;
3802 }
3803 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003804
3805 case 32:
3806 // NEON's 32-bit VMOV supports splat values where:
3807 // * only one byte is nonzero, or
3808 // * the least significant byte is 0xff and the second byte is nonzero, or
3809 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003810 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003811 if ((SplatBits & ~0xff) == 0) {
3812 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003813 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003814 Imm = SplatBits;
3815 break;
3816 }
3817 if ((SplatBits & ~0xff00) == 0) {
3818 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003819 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 Imm = SplatBits >> 8;
3821 break;
3822 }
3823 if ((SplatBits & ~0xff0000) == 0) {
3824 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003825 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003826 Imm = SplatBits >> 16;
3827 break;
3828 }
3829 if ((SplatBits & ~0xff000000) == 0) {
3830 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003831 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003832 Imm = SplatBits >> 24;
3833 break;
3834 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003835
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003836 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3837 if (type == OtherModImm) return SDValue();
3838
Bob Wilson5bafff32009-06-22 23:27:02 +00003839 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003840 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3841 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003842 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003843 Imm = SplatBits >> 8;
3844 SplatBits |= 0xff;
3845 break;
3846 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003847
3848 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003849 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3850 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003851 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003852 Imm = SplatBits >> 16;
3853 SplatBits |= 0xffff;
3854 break;
3855 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003856
3857 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3858 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3859 // VMOV.I32. A (very) minor optimization would be to replicate the value
3860 // and fall through here to test for a valid 64-bit splat. But, then the
3861 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003862 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003863
3864 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003865 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003866 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003867 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003868 uint64_t BitMask = 0xff;
3869 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003870 unsigned ImmMask = 1;
3871 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003872 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003873 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003874 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003875 Imm |= ImmMask;
3876 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003877 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003878 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003879 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003880 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003881 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003882 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003883 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003884 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003885 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003886 break;
3887 }
3888
Bob Wilson1a913ed2010-06-11 21:34:50 +00003889 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003890 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003891 }
3892
Bob Wilsoncba270d2010-07-13 21:16:48 +00003893 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3894 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003895}
3896
Lang Hamesc0a9f822012-03-29 21:56:11 +00003897SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3898 const ARMSubtarget *ST) const {
3899 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3900 return SDValue();
3901
3902 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3903 assert(Op.getValueType() == MVT::f32 &&
3904 "ConstantFP custom lowering should only occur for f32.");
3905
3906 // Try splatting with a VMOV.f32...
3907 APFloat FPVal = CFP->getValueAPF();
3908 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3909 if (ImmVal != -1) {
3910 DebugLoc DL = Op.getDebugLoc();
3911 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3912 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3913 NewVal);
3914 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3915 DAG.getConstant(0, MVT::i32));
3916 }
3917
3918 // If that fails, try a VMOV.i32
3919 EVT VMovVT;
3920 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3921 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3922 VMOVModImm);
3923 if (NewVal != SDValue()) {
3924 DebugLoc DL = Op.getDebugLoc();
3925 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3926 NewVal);
3927 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3928 VecConstant);
3929 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3930 DAG.getConstant(0, MVT::i32));
3931 }
3932
3933 // Finally, try a VMVN.i32
3934 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3935 VMVNModImm);
3936 if (NewVal != SDValue()) {
3937 DebugLoc DL = Op.getDebugLoc();
3938 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3939 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3940 VecConstant);
3941 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3942 DAG.getConstant(0, MVT::i32));
3943 }
3944
3945 return SDValue();
3946}
3947
Quentin Colombet43934ae2012-11-02 21:32:17 +00003948// check if an VEXT instruction can handle the shuffle mask when the
3949// vector sources of the shuffle are the same.
3950static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
3951 unsigned NumElts = VT.getVectorNumElements();
3952
3953 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3954 if (M[0] < 0)
3955 return false;
3956
3957 Imm = M[0];
3958
3959 // If this is a VEXT shuffle, the immediate value is the index of the first
3960 // element. The other shuffle indices must be the successive elements after
3961 // the first one.
3962 unsigned ExpectedElt = Imm;
3963 for (unsigned i = 1; i < NumElts; ++i) {
3964 // Increment the expected index. If it wraps around, just follow it
3965 // back to index zero and keep going.
3966 ++ExpectedElt;
3967 if (ExpectedElt == NumElts)
3968 ExpectedElt = 0;
3969
3970 if (M[i] < 0) continue; // ignore UNDEF indices
3971 if (ExpectedElt != static_cast<unsigned>(M[i]))
3972 return false;
3973 }
3974
3975 return true;
3976}
3977
Lang Hamesc0a9f822012-03-29 21:56:11 +00003978
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003979static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003980 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003981 unsigned NumElts = VT.getVectorNumElements();
3982 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003983
3984 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3985 if (M[0] < 0)
3986 return false;
3987
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003988 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003989
3990 // If this is a VEXT shuffle, the immediate value is the index of the first
3991 // element. The other shuffle indices must be the successive elements after
3992 // the first one.
3993 unsigned ExpectedElt = Imm;
3994 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003995 // Increment the expected index. If it wraps around, it may still be
3996 // a VEXT but the source vectors must be swapped.
3997 ExpectedElt += 1;
3998 if (ExpectedElt == NumElts * 2) {
3999 ExpectedElt = 0;
4000 ReverseVEXT = true;
4001 }
4002
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004003 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004004 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004005 return false;
4006 }
4007
4008 // Adjust the index value if the source operands will be swapped.
4009 if (ReverseVEXT)
4010 Imm -= NumElts;
4011
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004012 return true;
4013}
4014
Bob Wilson8bb9e482009-07-26 00:39:34 +00004015/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4016/// instruction with the specified blocksize. (The order of the elements
4017/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004018static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004019 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4020 "Only possible block sizes for VREV are: 16, 32, 64");
4021
Bob Wilson8bb9e482009-07-26 00:39:34 +00004022 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004023 if (EltSz == 64)
4024 return false;
4025
4026 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004027 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004028 // If the first shuffle index is UNDEF, be optimistic.
4029 if (M[0] < 0)
4030 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004031
4032 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4033 return false;
4034
4035 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004036 if (M[i] < 0) continue; // ignore UNDEF indices
4037 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004038 return false;
4039 }
4040
4041 return true;
4042}
4043
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004044static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004045 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4046 // range, then 0 is placed into the resulting vector. So pretty much any mask
4047 // of 8 elements can work here.
4048 return VT == MVT::v8i8 && M.size() == 8;
4049}
4050
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004051static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004052 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4053 if (EltSz == 64)
4054 return false;
4055
Bob Wilsonc692cb72009-08-21 20:54:19 +00004056 unsigned NumElts = VT.getVectorNumElements();
4057 WhichResult = (M[0] == 0 ? 0 : 1);
4058 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004059 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4060 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004061 return false;
4062 }
4063 return true;
4064}
4065
Bob Wilson324f4f12009-12-03 06:40:55 +00004066/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4067/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4068/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004069static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004070 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4071 if (EltSz == 64)
4072 return false;
4073
4074 unsigned NumElts = VT.getVectorNumElements();
4075 WhichResult = (M[0] == 0 ? 0 : 1);
4076 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004077 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4078 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004079 return false;
4080 }
4081 return true;
4082}
4083
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004084static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004085 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4086 if (EltSz == 64)
4087 return false;
4088
Bob Wilsonc692cb72009-08-21 20:54:19 +00004089 unsigned NumElts = VT.getVectorNumElements();
4090 WhichResult = (M[0] == 0 ? 0 : 1);
4091 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004092 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004093 if ((unsigned) M[i] != 2 * i + WhichResult)
4094 return false;
4095 }
4096
4097 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004098 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004099 return false;
4100
4101 return true;
4102}
4103
Bob Wilson324f4f12009-12-03 06:40:55 +00004104/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4105/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4106/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004107static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004108 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4109 if (EltSz == 64)
4110 return false;
4111
4112 unsigned Half = VT.getVectorNumElements() / 2;
4113 WhichResult = (M[0] == 0 ? 0 : 1);
4114 for (unsigned j = 0; j != 2; ++j) {
4115 unsigned Idx = WhichResult;
4116 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004117 int MIdx = M[i + j * Half];
4118 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004119 return false;
4120 Idx += 2;
4121 }
4122 }
4123
4124 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4125 if (VT.is64BitVector() && EltSz == 32)
4126 return false;
4127
4128 return true;
4129}
4130
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004131static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004132 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4133 if (EltSz == 64)
4134 return false;
4135
Bob Wilsonc692cb72009-08-21 20:54:19 +00004136 unsigned NumElts = VT.getVectorNumElements();
4137 WhichResult = (M[0] == 0 ? 0 : 1);
4138 unsigned Idx = WhichResult * NumElts / 2;
4139 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004140 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4141 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004142 return false;
4143 Idx += 1;
4144 }
4145
4146 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004147 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004148 return false;
4149
4150 return true;
4151}
4152
Bob Wilson324f4f12009-12-03 06:40:55 +00004153/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4154/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4155/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004156static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004157 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4158 if (EltSz == 64)
4159 return false;
4160
4161 unsigned NumElts = VT.getVectorNumElements();
4162 WhichResult = (M[0] == 0 ? 0 : 1);
4163 unsigned Idx = WhichResult * NumElts / 2;
4164 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004165 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4166 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004167 return false;
4168 Idx += 1;
4169 }
4170
4171 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4172 if (VT.is64BitVector() && EltSz == 32)
4173 return false;
4174
4175 return true;
4176}
4177
Dale Johannesenf630c712010-07-29 20:10:08 +00004178// If N is an integer constant that can be moved into a register in one
4179// instruction, return an SDValue of such a constant (will become a MOV
4180// instruction). Otherwise return null.
4181static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4182 const ARMSubtarget *ST, DebugLoc dl) {
4183 uint64_t Val;
4184 if (!isa<ConstantSDNode>(N))
4185 return SDValue();
4186 Val = cast<ConstantSDNode>(N)->getZExtValue();
4187
4188 if (ST->isThumb1Only()) {
4189 if (Val <= 255 || ~Val <= 255)
4190 return DAG.getConstant(Val, MVT::i32);
4191 } else {
4192 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4193 return DAG.getConstant(Val, MVT::i32);
4194 }
4195 return SDValue();
4196}
4197
Bob Wilson5bafff32009-06-22 23:27:02 +00004198// If this is a case we can't handle, return null and let the default
4199// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004200SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4201 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004202 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004203 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004204 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004205
4206 APInt SplatBits, SplatUndef;
4207 unsigned SplatBitSize;
4208 bool HasAnyUndefs;
4209 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004210 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004211 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004212 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004213 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004214 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004215 DAG, VmovVT, VT.is128BitVector(),
4216 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004217 if (Val.getNode()) {
4218 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004219 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004220 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004221
4222 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004223 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004224 Val = isNEONModifiedImm(NegatedImm,
4225 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004226 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004227 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004228 if (Val.getNode()) {
4229 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004230 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004231 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004232
4233 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004234 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004235 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004236 if (ImmVal != -1) {
4237 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4238 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4239 }
4240 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004241 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004242 }
4243
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004244 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004245 //
4246 // As an optimisation, even if more than one value is used it may be more
4247 // profitable to splat with one value then change some lanes.
4248 //
4249 // Heuristically we decide to do this if the vector has a "dominant" value,
4250 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004251 unsigned NumElts = VT.getVectorNumElements();
4252 bool isOnlyLowElement = true;
4253 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004254 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004255 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004256
4257 // Map of the number of times a particular SDValue appears in the
4258 // element list.
James Molloy95154342012-09-06 10:32:08 +00004259 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004260 SDValue Value;
4261 for (unsigned i = 0; i < NumElts; ++i) {
4262 SDValue V = Op.getOperand(i);
4263 if (V.getOpcode() == ISD::UNDEF)
4264 continue;
4265 if (i > 0)
4266 isOnlyLowElement = false;
4267 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4268 isConstant = false;
4269
James Molloyba8562a2012-09-06 09:55:02 +00004270 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004271 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004272
4273 // Is this value dominant? (takes up more than half of the lanes)
4274 if (++Count > (NumElts / 2)) {
4275 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004276 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004277 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004278 }
James Molloyba8562a2012-09-06 09:55:02 +00004279 if (ValueCounts.size() != 1)
4280 usesOnlyOneValue = false;
4281 if (!Value.getNode() && ValueCounts.size() > 0)
4282 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004283
James Molloyba8562a2012-09-06 09:55:02 +00004284 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004285 return DAG.getUNDEF(VT);
4286
4287 if (isOnlyLowElement)
4288 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4289
Dale Johannesenf630c712010-07-29 20:10:08 +00004290 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4291
Dale Johannesen575cd142010-10-19 20:00:17 +00004292 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4293 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004294 if (hasDominantValue && EltSize <= 32) {
4295 if (!isConstant) {
4296 SDValue N;
4297
4298 // If we are VDUPing a value that comes directly from a vector, that will
4299 // cause an unnecessary move to and from a GPR, where instead we could
4300 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004301 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4302 // We need to create a new undef vector to use for the VDUPLANE if the
4303 // size of the vector from which we get the value is different than the
4304 // size of the vector that we need to create. We will insert the element
4305 // such that the register coalescer will remove unnecessary copies.
4306 if (VT != Value->getOperand(0).getValueType()) {
4307 ConstantSDNode *constIndex;
4308 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4309 assert(constIndex && "The index is not a constant!");
4310 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4311 VT.getVectorNumElements();
4312 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4313 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4314 Value, DAG.getConstant(index, MVT::i32)),
4315 DAG.getConstant(index, MVT::i32));
4316 } else {
4317 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004318 Value->getOperand(0), Value->getOperand(1));
Silviu Barangabb1078e2012-10-15 09:41:32 +00004319 }
4320 }
James Molloyba8562a2012-09-06 09:55:02 +00004321 else
4322 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4323
4324 if (!usesOnlyOneValue) {
4325 // The dominant value was splatted as 'N', but we now have to insert
4326 // all differing elements.
4327 for (unsigned I = 0; I < NumElts; ++I) {
4328 if (Op.getOperand(I) == Value)
4329 continue;
4330 SmallVector<SDValue, 3> Ops;
4331 Ops.push_back(N);
4332 Ops.push_back(Op.getOperand(I));
4333 Ops.push_back(DAG.getConstant(I, MVT::i32));
4334 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4335 }
4336 }
4337 return N;
4338 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004339 if (VT.getVectorElementType().isFloatingPoint()) {
4340 SmallVector<SDValue, 8> Ops;
4341 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004342 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004343 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004344 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4345 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004346 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4347 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004348 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004349 }
James Molloyba8562a2012-09-06 09:55:02 +00004350 if (usesOnlyOneValue) {
4351 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4352 if (isConstant && Val.getNode())
4353 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4354 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004355 }
4356
4357 // If all elements are constants and the case above didn't get hit, fall back
4358 // to the default expansion, which will generate a load from the constant
4359 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004360 if (isConstant)
4361 return SDValue();
4362
Bob Wilson11a1dff2011-01-07 21:37:30 +00004363 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4364 if (NumElts >= 4) {
4365 SDValue shuffle = ReconstructShuffle(Op, DAG);
4366 if (shuffle != SDValue())
4367 return shuffle;
4368 }
4369
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004370 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004371 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4372 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004373 if (EltSize >= 32) {
4374 // Do the expansion with floating-point types, since that is what the VFP
4375 // registers are defined to use, and since i64 is not legal.
4376 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4377 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004378 SmallVector<SDValue, 8> Ops;
4379 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004380 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004381 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004382 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004383 }
4384
4385 return SDValue();
4386}
4387
Bob Wilson11a1dff2011-01-07 21:37:30 +00004388// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004389// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004390SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4391 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004392 DebugLoc dl = Op.getDebugLoc();
4393 EVT VT = Op.getValueType();
4394 unsigned NumElts = VT.getVectorNumElements();
4395
4396 SmallVector<SDValue, 2> SourceVecs;
4397 SmallVector<unsigned, 2> MinElts;
4398 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004399
Bob Wilson11a1dff2011-01-07 21:37:30 +00004400 for (unsigned i = 0; i < NumElts; ++i) {
4401 SDValue V = Op.getOperand(i);
4402 if (V.getOpcode() == ISD::UNDEF)
4403 continue;
4404 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4405 // A shuffle can only come from building a vector from various
4406 // elements of other vectors.
4407 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004408 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4409 VT.getVectorElementType()) {
4410 // This code doesn't know how to handle shuffles where the vector
4411 // element types do not match (this happens because type legalization
4412 // promotes the return type of EXTRACT_VECTOR_ELT).
4413 // FIXME: It might be appropriate to extend this code to handle
4414 // mismatched types.
4415 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004416 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004417
Bob Wilson11a1dff2011-01-07 21:37:30 +00004418 // Record this extraction against the appropriate vector if possible...
4419 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004420 // If the element number isn't a constant, we can't effectively
4421 // analyze what's going on.
4422 if (!isa<ConstantSDNode>(V.getOperand(1)))
4423 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004424 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4425 bool FoundSource = false;
4426 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4427 if (SourceVecs[j] == SourceVec) {
4428 if (MinElts[j] > EltNo)
4429 MinElts[j] = EltNo;
4430 if (MaxElts[j] < EltNo)
4431 MaxElts[j] = EltNo;
4432 FoundSource = true;
4433 break;
4434 }
4435 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004436
Bob Wilson11a1dff2011-01-07 21:37:30 +00004437 // Or record a new source if not...
4438 if (!FoundSource) {
4439 SourceVecs.push_back(SourceVec);
4440 MinElts.push_back(EltNo);
4441 MaxElts.push_back(EltNo);
4442 }
4443 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004444
Bob Wilson11a1dff2011-01-07 21:37:30 +00004445 // Currently only do something sane when at most two source vectors
4446 // involved.
4447 if (SourceVecs.size() > 2)
4448 return SDValue();
4449
4450 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4451 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004452
Bob Wilson11a1dff2011-01-07 21:37:30 +00004453 // This loop extracts the usage patterns of the source vectors
4454 // and prepares appropriate SDValues for a shuffle if possible.
4455 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4456 if (SourceVecs[i].getValueType() == VT) {
4457 // No VEXT necessary
4458 ShuffleSrcs[i] = SourceVecs[i];
4459 VEXTOffsets[i] = 0;
4460 continue;
4461 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4462 // It probably isn't worth padding out a smaller vector just to
4463 // break it down again in a shuffle.
4464 return SDValue();
4465 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004466
Bob Wilson11a1dff2011-01-07 21:37:30 +00004467 // Since only 64-bit and 128-bit vectors are legal on ARM and
4468 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004469 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4470 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004471
Bob Wilson11a1dff2011-01-07 21:37:30 +00004472 if (MaxElts[i] - MinElts[i] >= NumElts) {
4473 // Span too large for a VEXT to cope
4474 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004475 }
4476
Bob Wilson11a1dff2011-01-07 21:37:30 +00004477 if (MinElts[i] >= NumElts) {
4478 // The extraction can just take the second half
4479 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004480 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4481 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004482 DAG.getIntPtrConstant(NumElts));
4483 } else if (MaxElts[i] < NumElts) {
4484 // The extraction can just take the first half
4485 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004486 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4487 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004488 DAG.getIntPtrConstant(0));
4489 } else {
4490 // An actual VEXT is needed
4491 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004492 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4493 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004494 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004495 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4496 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004497 DAG.getIntPtrConstant(NumElts));
4498 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4499 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4500 }
4501 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004502
Bob Wilson11a1dff2011-01-07 21:37:30 +00004503 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004504
Bob Wilson11a1dff2011-01-07 21:37:30 +00004505 for (unsigned i = 0; i < NumElts; ++i) {
4506 SDValue Entry = Op.getOperand(i);
4507 if (Entry.getOpcode() == ISD::UNDEF) {
4508 Mask.push_back(-1);
4509 continue;
4510 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004511
Bob Wilson11a1dff2011-01-07 21:37:30 +00004512 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004513 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4514 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004515 if (ExtractVec == SourceVecs[0]) {
4516 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4517 } else {
4518 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4519 }
4520 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004521
Bob Wilson11a1dff2011-01-07 21:37:30 +00004522 // Final check before we try to produce nonsense...
4523 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004524 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4525 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004526
Bob Wilson11a1dff2011-01-07 21:37:30 +00004527 return SDValue();
4528}
4529
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004530/// isShuffleMaskLegal - Targets can use this to indicate that they only
4531/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4532/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4533/// are assumed to be legal.
4534bool
4535ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4536 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004537 if (VT.getVectorNumElements() == 4 &&
4538 (VT.is128BitVector() || VT.is64BitVector())) {
4539 unsigned PFIndexes[4];
4540 for (unsigned i = 0; i != 4; ++i) {
4541 if (M[i] < 0)
4542 PFIndexes[i] = 8;
4543 else
4544 PFIndexes[i] = M[i];
4545 }
4546
4547 // Compute the index in the perfect shuffle table.
4548 unsigned PFTableIndex =
4549 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4550 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4551 unsigned Cost = (PFEntry >> 30);
4552
4553 if (Cost <= 4)
4554 return true;
4555 }
4556
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004557 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004558 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004559
Bob Wilson53dd2452010-06-07 23:53:38 +00004560 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4561 return (EltSize >= 32 ||
4562 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004563 isVREVMask(M, VT, 64) ||
4564 isVREVMask(M, VT, 32) ||
4565 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004566 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004567 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004568 isVTRNMask(M, VT, WhichResult) ||
4569 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004570 isVZIPMask(M, VT, WhichResult) ||
4571 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4572 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4573 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004574}
4575
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004576/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4577/// the specified operations to build the shuffle.
4578static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4579 SDValue RHS, SelectionDAG &DAG,
4580 DebugLoc dl) {
4581 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4582 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4583 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4584
4585 enum {
4586 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4587 OP_VREV,
4588 OP_VDUP0,
4589 OP_VDUP1,
4590 OP_VDUP2,
4591 OP_VDUP3,
4592 OP_VEXT1,
4593 OP_VEXT2,
4594 OP_VEXT3,
4595 OP_VUZPL, // VUZP, left result
4596 OP_VUZPR, // VUZP, right result
4597 OP_VZIPL, // VZIP, left result
4598 OP_VZIPR, // VZIP, right result
4599 OP_VTRNL, // VTRN, left result
4600 OP_VTRNR // VTRN, right result
4601 };
4602
4603 if (OpNum == OP_COPY) {
4604 if (LHSID == (1*9+2)*9+3) return LHS;
4605 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4606 return RHS;
4607 }
4608
4609 SDValue OpLHS, OpRHS;
4610 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4611 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4612 EVT VT = OpLHS.getValueType();
4613
4614 switch (OpNum) {
4615 default: llvm_unreachable("Unknown shuffle opcode!");
4616 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004617 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004618 if (VT.getVectorElementType() == MVT::i32 ||
4619 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004620 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4621 // vrev <4 x i16> -> VREV32
4622 if (VT.getVectorElementType() == MVT::i16)
4623 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4624 // vrev <4 x i8> -> VREV16
4625 assert(VT.getVectorElementType() == MVT::i8);
4626 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004627 case OP_VDUP0:
4628 case OP_VDUP1:
4629 case OP_VDUP2:
4630 case OP_VDUP3:
4631 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004632 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004633 case OP_VEXT1:
4634 case OP_VEXT2:
4635 case OP_VEXT3:
4636 return DAG.getNode(ARMISD::VEXT, dl, VT,
4637 OpLHS, OpRHS,
4638 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4639 case OP_VUZPL:
4640 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004641 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004642 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4643 case OP_VZIPL:
4644 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004645 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004646 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4647 case OP_VTRNL:
4648 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004649 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4650 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004651 }
4652}
4653
Bill Wendling69a05a72011-03-14 23:02:38 +00004654static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004655 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004656 SelectionDAG &DAG) {
4657 // Check to see if we can use the VTBL instruction.
4658 SDValue V1 = Op.getOperand(0);
4659 SDValue V2 = Op.getOperand(1);
4660 DebugLoc DL = Op.getDebugLoc();
4661
4662 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004663 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004664 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4665 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4666
4667 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4668 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4669 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4670 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004671
Owen Anderson76706012011-04-05 21:48:57 +00004672 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004673 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4674 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004675}
4676
Bob Wilson5bafff32009-06-22 23:27:02 +00004677static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004678 SDValue V1 = Op.getOperand(0);
4679 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004680 DebugLoc dl = Op.getDebugLoc();
4681 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004682 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004683
Bob Wilson28865062009-08-13 02:13:04 +00004684 // Convert shuffles that are directly supported on NEON to target-specific
4685 // DAG nodes, instead of keeping them as shuffles and matching them again
4686 // during code selection. This is more efficient and avoids the possibility
4687 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004688 // FIXME: floating-point vectors should be canonicalized to integer vectors
4689 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004690 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004691
Bob Wilson53dd2452010-06-07 23:53:38 +00004692 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4693 if (EltSize <= 32) {
4694 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4695 int Lane = SVN->getSplatIndex();
4696 // If this is undef splat, generate it via "just" vdup, if possible.
4697 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004698
Dan Gohman65fd6562011-11-03 21:49:52 +00004699 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004700 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4701 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4702 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004703 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4704 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4705 // reaches it).
4706 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4707 !isa<ConstantSDNode>(V1.getOperand(0))) {
4708 bool IsScalarToVector = true;
4709 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4710 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4711 IsScalarToVector = false;
4712 break;
4713 }
4714 if (IsScalarToVector)
4715 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4716 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004717 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4718 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004719 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004720
4721 bool ReverseVEXT;
4722 unsigned Imm;
4723 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4724 if (ReverseVEXT)
4725 std::swap(V1, V2);
4726 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4727 DAG.getConstant(Imm, MVT::i32));
4728 }
4729
4730 if (isVREVMask(ShuffleMask, VT, 64))
4731 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4732 if (isVREVMask(ShuffleMask, VT, 32))
4733 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4734 if (isVREVMask(ShuffleMask, VT, 16))
4735 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4736
Quentin Colombet43934ae2012-11-02 21:32:17 +00004737 if (V2->getOpcode() == ISD::UNDEF &&
4738 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4739 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4740 DAG.getConstant(Imm, MVT::i32));
4741 }
4742
Bob Wilson53dd2452010-06-07 23:53:38 +00004743 // Check for Neon shuffles that modify both input vectors in place.
4744 // If both results are used, i.e., if there are two shuffles with the same
4745 // source operands and with masks corresponding to both results of one of
4746 // these operations, DAG memoization will ensure that a single node is
4747 // used for both shuffles.
4748 unsigned WhichResult;
4749 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4750 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4751 V1, V2).getValue(WhichResult);
4752 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4753 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4754 V1, V2).getValue(WhichResult);
4755 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4756 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4757 V1, V2).getValue(WhichResult);
4758
4759 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4760 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4761 V1, V1).getValue(WhichResult);
4762 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4763 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4764 V1, V1).getValue(WhichResult);
4765 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4766 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4767 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004768 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004769
Bob Wilsonc692cb72009-08-21 20:54:19 +00004770 // If the shuffle is not directly supported and it has 4 elements, use
4771 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004772 unsigned NumElts = VT.getVectorNumElements();
4773 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004774 unsigned PFIndexes[4];
4775 for (unsigned i = 0; i != 4; ++i) {
4776 if (ShuffleMask[i] < 0)
4777 PFIndexes[i] = 8;
4778 else
4779 PFIndexes[i] = ShuffleMask[i];
4780 }
4781
4782 // Compute the index in the perfect shuffle table.
4783 unsigned PFTableIndex =
4784 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004785 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4786 unsigned Cost = (PFEntry >> 30);
4787
4788 if (Cost <= 4)
4789 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4790 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004791
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004792 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004793 if (EltSize >= 32) {
4794 // Do the expansion with floating-point types, since that is what the VFP
4795 // registers are defined to use, and since i64 is not legal.
4796 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4797 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004798 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4799 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004800 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004801 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004802 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004803 Ops.push_back(DAG.getUNDEF(EltVT));
4804 else
4805 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4806 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4807 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4808 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004809 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004810 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004811 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004812 }
4813
Bill Wendling69a05a72011-03-14 23:02:38 +00004814 if (VT == MVT::v8i8) {
4815 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4816 if (NewOp.getNode())
4817 return NewOp;
4818 }
4819
Bob Wilson22cac0d2009-08-14 05:16:33 +00004820 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004821}
4822
Eli Friedman5c89cb82011-10-24 23:08:52 +00004823static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4824 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4825 SDValue Lane = Op.getOperand(2);
4826 if (!isa<ConstantSDNode>(Lane))
4827 return SDValue();
4828
4829 return Op;
4830}
4831
Bob Wilson5bafff32009-06-22 23:27:02 +00004832static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004833 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004834 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004835 if (!isa<ConstantSDNode>(Lane))
4836 return SDValue();
4837
4838 SDValue Vec = Op.getOperand(0);
4839 if (Op.getValueType() == MVT::i32 &&
4840 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4841 DebugLoc dl = Op.getDebugLoc();
4842 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4843 }
4844
4845 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004846}
4847
Bob Wilsona6d65862009-08-03 20:36:38 +00004848static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4849 // The only time a CONCAT_VECTORS operation can have legal types is when
4850 // two 64-bit vectors are concatenated to a 128-bit vector.
4851 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4852 "unexpected CONCAT_VECTORS");
4853 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004855 SDValue Op0 = Op.getOperand(0);
4856 SDValue Op1 = Op.getOperand(1);
4857 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004859 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004860 DAG.getIntPtrConstant(0));
4861 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004862 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004863 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004864 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004865 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004866}
4867
Bob Wilson626613d2010-11-23 19:38:38 +00004868/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4869/// element has been zero/sign-extended, depending on the isSigned parameter,
4870/// from an integer type half its size.
4871static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4872 bool isSigned) {
4873 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4874 EVT VT = N->getValueType(0);
4875 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4876 SDNode *BVN = N->getOperand(0).getNode();
4877 if (BVN->getValueType(0) != MVT::v4i32 ||
4878 BVN->getOpcode() != ISD::BUILD_VECTOR)
4879 return false;
4880 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4881 unsigned HiElt = 1 - LoElt;
4882 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4883 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4884 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4885 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4886 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4887 return false;
4888 if (isSigned) {
4889 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4890 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4891 return true;
4892 } else {
4893 if (Hi0->isNullValue() && Hi1->isNullValue())
4894 return true;
4895 }
4896 return false;
4897 }
4898
4899 if (N->getOpcode() != ISD::BUILD_VECTOR)
4900 return false;
4901
4902 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4903 SDNode *Elt = N->getOperand(i).getNode();
4904 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4905 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4906 unsigned HalfSize = EltSize / 2;
4907 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004908 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004909 return false;
4910 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004911 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004912 return false;
4913 }
4914 continue;
4915 }
4916 return false;
4917 }
4918
4919 return true;
4920}
4921
4922/// isSignExtended - Check if a node is a vector value that is sign-extended
4923/// or a constant BUILD_VECTOR with sign-extended elements.
4924static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4925 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4926 return true;
4927 if (isExtendedBUILD_VECTOR(N, DAG, true))
4928 return true;
4929 return false;
4930}
4931
4932/// isZeroExtended - Check if a node is a vector value that is zero-extended
4933/// or a constant BUILD_VECTOR with zero-extended elements.
4934static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4935 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4936 return true;
4937 if (isExtendedBUILD_VECTOR(N, DAG, false))
4938 return true;
4939 return false;
4940}
4941
4942/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4943/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004944static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4945 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4946 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004947 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4948 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4949 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004950 LD->isNonTemporal(), LD->isInvariant(),
4951 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004952 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4953 // have been legalized as a BITCAST from v4i32.
4954 if (N->getOpcode() == ISD::BITCAST) {
4955 SDNode *BVN = N->getOperand(0).getNode();
4956 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4957 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4958 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4959 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4960 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4961 }
4962 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4963 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4964 EVT VT = N->getValueType(0);
4965 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4966 unsigned NumElts = VT.getVectorNumElements();
4967 MVT TruncVT = MVT::getIntegerVT(EltSize);
4968 SmallVector<SDValue, 8> Ops;
4969 for (unsigned i = 0; i != NumElts; ++i) {
4970 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4971 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004972 // Element types smaller than 32 bits are not legal, so use i32 elements.
4973 // The values are implicitly truncated so sext vs. zext doesn't matter.
4974 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004975 }
4976 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4977 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004978}
4979
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004980static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4981 unsigned Opcode = N->getOpcode();
4982 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4983 SDNode *N0 = N->getOperand(0).getNode();
4984 SDNode *N1 = N->getOperand(1).getNode();
4985 return N0->hasOneUse() && N1->hasOneUse() &&
4986 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4987 }
4988 return false;
4989}
4990
4991static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4992 unsigned Opcode = N->getOpcode();
4993 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4994 SDNode *N0 = N->getOperand(0).getNode();
4995 SDNode *N1 = N->getOperand(1).getNode();
4996 return N0->hasOneUse() && N1->hasOneUse() &&
4997 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4998 }
4999 return false;
5000}
5001
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005002static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5003 // Multiplications are only custom-lowered for 128-bit vectors so that
5004 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5005 EVT VT = Op.getValueType();
5006 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
5007 SDNode *N0 = Op.getOperand(0).getNode();
5008 SDNode *N1 = Op.getOperand(1).getNode();
5009 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005010 bool isMLA = false;
5011 bool isN0SExt = isSignExtended(N0, DAG);
5012 bool isN1SExt = isSignExtended(N1, DAG);
5013 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005014 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005015 else {
5016 bool isN0ZExt = isZeroExtended(N0, DAG);
5017 bool isN1ZExt = isZeroExtended(N1, DAG);
5018 if (isN0ZExt && isN1ZExt)
5019 NewOpc = ARMISD::VMULLu;
5020 else if (isN1SExt || isN1ZExt) {
5021 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5022 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5023 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5024 NewOpc = ARMISD::VMULLs;
5025 isMLA = true;
5026 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5027 NewOpc = ARMISD::VMULLu;
5028 isMLA = true;
5029 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5030 std::swap(N0, N1);
5031 NewOpc = ARMISD::VMULLu;
5032 isMLA = true;
5033 }
5034 }
5035
5036 if (!NewOpc) {
5037 if (VT == MVT::v2i64)
5038 // Fall through to expand this. It is not legal.
5039 return SDValue();
5040 else
5041 // Other vector multiplications are legal.
5042 return Op;
5043 }
5044 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005045
5046 // Legalize to a VMULL instruction.
5047 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005048 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005049 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005050 if (!isMLA) {
5051 Op0 = SkipExtension(N0, DAG);
5052 assert(Op0.getValueType().is64BitVector() &&
5053 Op1.getValueType().is64BitVector() &&
5054 "unexpected types for extended operands to VMULL");
5055 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5056 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005057
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005058 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5059 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5060 // vmull q0, d4, d6
5061 // vmlal q0, d5, d6
5062 // is faster than
5063 // vaddl q0, d4, d5
5064 // vmovl q1, d6
5065 // vmul q0, q0, q1
5066 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
5067 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
5068 EVT Op1VT = Op1.getValueType();
5069 return DAG.getNode(N0->getOpcode(), DL, VT,
5070 DAG.getNode(NewOpc, DL, VT,
5071 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5072 DAG.getNode(NewOpc, DL, VT,
5073 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005074}
5075
Owen Anderson76706012011-04-05 21:48:57 +00005076static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005077LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5078 // Convert to float
5079 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5080 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5081 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5082 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5083 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5084 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5085 // Get reciprocal estimate.
5086 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005087 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005088 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5089 // Because char has a smaller range than uchar, we can actually get away
5090 // without any newton steps. This requires that we use a weird bias
5091 // of 0xb000, however (again, this has been exhaustively tested).
5092 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5093 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5094 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5095 Y = DAG.getConstant(0xb000, MVT::i32);
5096 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5097 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5098 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5099 // Convert back to short.
5100 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5101 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5102 return X;
5103}
5104
Owen Anderson76706012011-04-05 21:48:57 +00005105static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005106LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5107 SDValue N2;
5108 // Convert to float.
5109 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5110 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5111 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5112 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5113 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5114 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005115
Nate Begeman7973f352011-02-11 20:53:29 +00005116 // Use reciprocal estimate and one refinement step.
5117 // float4 recip = vrecpeq_f32(yf);
5118 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005119 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005120 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005121 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005122 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5123 N1, N2);
5124 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5125 // Because short has a smaller range than ushort, we can actually get away
5126 // with only a single newton step. This requires that we use a weird bias
5127 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005128 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005129 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5130 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005131 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005132 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5133 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5134 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5135 // Convert back to integer and return.
5136 // return vmovn_s32(vcvt_s32_f32(result));
5137 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5138 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5139 return N0;
5140}
5141
5142static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5143 EVT VT = Op.getValueType();
5144 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5145 "unexpected type for custom-lowering ISD::SDIV");
5146
5147 DebugLoc dl = Op.getDebugLoc();
5148 SDValue N0 = Op.getOperand(0);
5149 SDValue N1 = Op.getOperand(1);
5150 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005151
Nate Begeman7973f352011-02-11 20:53:29 +00005152 if (VT == MVT::v8i8) {
5153 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5154 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005155
Nate Begeman7973f352011-02-11 20:53:29 +00005156 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5157 DAG.getIntPtrConstant(4));
5158 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005159 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005160 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5161 DAG.getIntPtrConstant(0));
5162 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5163 DAG.getIntPtrConstant(0));
5164
5165 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5166 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5167
5168 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5169 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005170
Nate Begeman7973f352011-02-11 20:53:29 +00005171 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5172 return N0;
5173 }
5174 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5175}
5176
5177static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5178 EVT VT = Op.getValueType();
5179 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5180 "unexpected type for custom-lowering ISD::UDIV");
5181
5182 DebugLoc dl = Op.getDebugLoc();
5183 SDValue N0 = Op.getOperand(0);
5184 SDValue N1 = Op.getOperand(1);
5185 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005186
Nate Begeman7973f352011-02-11 20:53:29 +00005187 if (VT == MVT::v8i8) {
5188 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5189 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005190
Nate Begeman7973f352011-02-11 20:53:29 +00005191 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5192 DAG.getIntPtrConstant(4));
5193 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005194 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005195 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5196 DAG.getIntPtrConstant(0));
5197 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5198 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005199
Nate Begeman7973f352011-02-11 20:53:29 +00005200 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5201 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005202
Nate Begeman7973f352011-02-11 20:53:29 +00005203 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5204 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005205
5206 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005207 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5208 N0);
5209 return N0;
5210 }
Owen Anderson76706012011-04-05 21:48:57 +00005211
Nate Begeman7973f352011-02-11 20:53:29 +00005212 // v4i16 sdiv ... Convert to float.
5213 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5214 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5215 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5216 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5217 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005218 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005219
5220 // Use reciprocal estimate and two refinement steps.
5221 // float4 recip = vrecpeq_f32(yf);
5222 // recip *= vrecpsq_f32(yf, recip);
5223 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005224 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005225 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005226 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005227 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005228 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005229 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005230 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005231 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005232 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005233 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5234 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5235 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5236 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005237 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005238 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5239 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5240 N1 = DAG.getConstant(2, MVT::i32);
5241 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5242 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5243 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5244 // Convert back to integer and return.
5245 // return vmovn_u32(vcvt_s32_f32(result));
5246 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5247 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5248 return N0;
5249}
5250
Evan Cheng342e3162011-08-30 01:34:54 +00005251static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5252 EVT VT = Op.getNode()->getValueType(0);
5253 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5254
5255 unsigned Opc;
5256 bool ExtraOp = false;
5257 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005258 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005259 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5260 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5261 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5262 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5263 }
5264
5265 if (!ExtraOp)
5266 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5267 Op.getOperand(1));
5268 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5269 Op.getOperand(1), Op.getOperand(2));
5270}
5271
Eli Friedman74bf18c2011-09-15 22:26:18 +00005272static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005273 // Monotonic load/store is legal for all targets
5274 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5275 return Op;
5276
5277 // Aquire/Release load/store is not legal for targets without a
5278 // dmb or equivalent available.
5279 return SDValue();
5280}
5281
5282
Eli Friedman2bdffe42011-08-31 00:31:29 +00005283static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005284ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5285 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005286 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005287 assert (Node->getValueType(0) == MVT::i64 &&
5288 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005289
Eli Friedman4d3f3292011-08-31 17:52:22 +00005290 SmallVector<SDValue, 6> Ops;
5291 Ops.push_back(Node->getOperand(0)); // Chain
5292 Ops.push_back(Node->getOperand(1)); // Ptr
5293 // Low part of Val1
5294 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5295 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5296 // High part of Val1
5297 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5298 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005299 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005300 // High part of Val1
5301 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5302 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5303 // High part of Val2
5304 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5305 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5306 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005307 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5308 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005309 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005310 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005311 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005312 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5313 Results.push_back(Result.getValue(2));
5314}
5315
Dan Gohmand858e902010-04-17 15:26:15 +00005316SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005317 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005318 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005319 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005320 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005321 case ISD::GlobalAddress:
5322 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5323 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005324 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005325 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005326 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5327 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005328 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005329 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005330 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005331 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005332 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005333 case ISD::SINT_TO_FP:
5334 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5335 case ISD::FP_TO_SINT:
5336 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005337 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005338 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005339 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005340 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005341 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005342 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005343 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5344 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005345 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005346 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005347 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005348 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005349 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005350 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005351 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005352 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005353 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005354 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005355 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005356 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005357 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005358 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005359 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005360 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005361 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005362 case ISD::SDIV: return LowerSDIV(Op, DAG);
5363 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005364 case ISD::ADDC:
5365 case ISD::ADDE:
5366 case ISD::SUBC:
5367 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005368 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005369 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005370 }
Evan Chenga8e29892007-01-19 07:51:42 +00005371}
5372
Duncan Sands1607f052008-12-01 11:39:25 +00005373/// ReplaceNodeResults - Replace the results of node with an illegal result
5374/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005375void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5376 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005377 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005378 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005379 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005380 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005381 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005382 case ISD::BITCAST:
5383 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005384 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005385 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005386 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005387 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005388 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005389 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005390 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005391 return;
5392 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005393 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005394 return;
5395 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005396 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005397 return;
5398 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005399 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005400 return;
5401 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005402 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005403 return;
5404 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005405 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005406 return;
5407 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005408 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005409 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005410 case ISD::ATOMIC_CMP_SWAP:
5411 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5412 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005413 case ISD::ATOMIC_LOAD_MIN:
5414 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5415 return;
5416 case ISD::ATOMIC_LOAD_UMIN:
5417 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5418 return;
5419 case ISD::ATOMIC_LOAD_MAX:
5420 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5421 return;
5422 case ISD::ATOMIC_LOAD_UMAX:
5423 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5424 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005425 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005426 if (Res.getNode())
5427 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005428}
Chris Lattner27a6c732007-11-24 07:07:01 +00005429
Evan Chenga8e29892007-01-19 07:51:42 +00005430//===----------------------------------------------------------------------===//
5431// ARM Scheduler Hooks
5432//===----------------------------------------------------------------------===//
5433
5434MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005435ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5436 MachineBasicBlock *BB,
5437 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005438 unsigned dest = MI->getOperand(0).getReg();
5439 unsigned ptr = MI->getOperand(1).getReg();
5440 unsigned oldval = MI->getOperand(2).getReg();
5441 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005442 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5443 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005444 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005445
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005446 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005447 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5448 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5449 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005450
5451 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005452 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5453 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5454 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005455 }
5456
Jim Grosbach5278eb82009-12-11 01:42:04 +00005457 unsigned ldrOpc, strOpc;
5458 switch (Size) {
5459 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005460 case 1:
5461 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005462 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005463 break;
5464 case 2:
5465 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5466 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5467 break;
5468 case 4:
5469 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5470 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5471 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005472 }
5473
5474 MachineFunction *MF = BB->getParent();
5475 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5476 MachineFunction::iterator It = BB;
5477 ++It; // insert the new blocks after the current block
5478
5479 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5480 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5481 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5482 MF->insert(It, loop1MBB);
5483 MF->insert(It, loop2MBB);
5484 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005485
5486 // Transfer the remainder of BB and its successor edges to exitMBB.
5487 exitMBB->splice(exitMBB->begin(), BB,
5488 llvm::next(MachineBasicBlock::iterator(MI)),
5489 BB->end());
5490 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005491
5492 // thisMBB:
5493 // ...
5494 // fallthrough --> loop1MBB
5495 BB->addSuccessor(loop1MBB);
5496
5497 // loop1MBB:
5498 // ldrex dest, [ptr]
5499 // cmp dest, oldval
5500 // bne exitMBB
5501 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005502 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5503 if (ldrOpc == ARM::t2LDREX)
5504 MIB.addImm(0);
5505 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005506 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005507 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005508 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5509 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005510 BB->addSuccessor(loop2MBB);
5511 BB->addSuccessor(exitMBB);
5512
5513 // loop2MBB:
5514 // strex scratch, newval, [ptr]
5515 // cmp scratch, #0
5516 // bne loop1MBB
5517 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005518 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5519 if (strOpc == ARM::t2STREX)
5520 MIB.addImm(0);
5521 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005522 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005523 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005524 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5525 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005526 BB->addSuccessor(loop1MBB);
5527 BB->addSuccessor(exitMBB);
5528
5529 // exitMBB:
5530 // ...
5531 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005532
Dan Gohman14152b42010-07-06 20:24:04 +00005533 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005534
Jim Grosbach5278eb82009-12-11 01:42:04 +00005535 return BB;
5536}
5537
5538MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005539ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5540 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005541 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5542 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5543
5544 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005545 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005546 MachineFunction::iterator It = BB;
5547 ++It;
5548
5549 unsigned dest = MI->getOperand(0).getReg();
5550 unsigned ptr = MI->getOperand(1).getReg();
5551 unsigned incr = MI->getOperand(2).getReg();
5552 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005553 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005554
5555 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5556 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005557 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5558 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005559 }
5560
Jim Grosbachc3c23542009-12-14 04:22:04 +00005561 unsigned ldrOpc, strOpc;
5562 switch (Size) {
5563 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005564 case 1:
5565 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005566 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005567 break;
5568 case 2:
5569 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5570 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5571 break;
5572 case 4:
5573 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5574 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5575 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005576 }
5577
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005578 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5579 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5580 MF->insert(It, loopMBB);
5581 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005582
5583 // Transfer the remainder of BB and its successor edges to exitMBB.
5584 exitMBB->splice(exitMBB->begin(), BB,
5585 llvm::next(MachineBasicBlock::iterator(MI)),
5586 BB->end());
5587 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005588
Craig Topper420761a2012-04-20 07:30:17 +00005589 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005590 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005591 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005592 unsigned scratch = MRI.createVirtualRegister(TRC);
5593 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005594
5595 // thisMBB:
5596 // ...
5597 // fallthrough --> loopMBB
5598 BB->addSuccessor(loopMBB);
5599
5600 // loopMBB:
5601 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005602 // <binop> scratch2, dest, incr
5603 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005604 // cmp scratch, #0
5605 // bne- loopMBB
5606 // fallthrough --> exitMBB
5607 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005608 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5609 if (ldrOpc == ARM::t2LDREX)
5610 MIB.addImm(0);
5611 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005612 if (BinOpcode) {
5613 // operand order needs to go the other way for NAND
5614 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5615 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5616 addReg(incr).addReg(dest)).addReg(0);
5617 else
5618 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5619 addReg(dest).addReg(incr)).addReg(0);
5620 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005621
Jim Grosbachb6aed502011-09-09 18:37:27 +00005622 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5623 if (strOpc == ARM::t2STREX)
5624 MIB.addImm(0);
5625 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005626 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005627 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005628 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5629 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005630
5631 BB->addSuccessor(loopMBB);
5632 BB->addSuccessor(exitMBB);
5633
5634 // exitMBB:
5635 // ...
5636 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005637
Dan Gohman14152b42010-07-06 20:24:04 +00005638 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005639
Jim Grosbachc3c23542009-12-14 04:22:04 +00005640 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005641}
5642
Jim Grosbachf7da8822011-04-26 19:44:18 +00005643MachineBasicBlock *
5644ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5645 MachineBasicBlock *BB,
5646 unsigned Size,
5647 bool signExtend,
5648 ARMCC::CondCodes Cond) const {
5649 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5650
5651 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5652 MachineFunction *MF = BB->getParent();
5653 MachineFunction::iterator It = BB;
5654 ++It;
5655
5656 unsigned dest = MI->getOperand(0).getReg();
5657 unsigned ptr = MI->getOperand(1).getReg();
5658 unsigned incr = MI->getOperand(2).getReg();
5659 unsigned oldval = dest;
5660 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005661 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005662
5663 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5664 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005665 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5666 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005667 }
5668
Jim Grosbachf7da8822011-04-26 19:44:18 +00005669 unsigned ldrOpc, strOpc, extendOpc;
5670 switch (Size) {
5671 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5672 case 1:
5673 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5674 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005675 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005676 break;
5677 case 2:
5678 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5679 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005680 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005681 break;
5682 case 4:
5683 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5684 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5685 extendOpc = 0;
5686 break;
5687 }
5688
5689 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5690 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5691 MF->insert(It, loopMBB);
5692 MF->insert(It, exitMBB);
5693
5694 // Transfer the remainder of BB and its successor edges to exitMBB.
5695 exitMBB->splice(exitMBB->begin(), BB,
5696 llvm::next(MachineBasicBlock::iterator(MI)),
5697 BB->end());
5698 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5699
Craig Topper420761a2012-04-20 07:30:17 +00005700 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005701 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005702 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005703 unsigned scratch = MRI.createVirtualRegister(TRC);
5704 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005705
5706 // thisMBB:
5707 // ...
5708 // fallthrough --> loopMBB
5709 BB->addSuccessor(loopMBB);
5710
5711 // loopMBB:
5712 // ldrex dest, ptr
5713 // (sign extend dest, if required)
5714 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005715 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005716 // strex scratch, scratch2, ptr
5717 // cmp scratch, #0
5718 // bne- loopMBB
5719 // fallthrough --> exitMBB
5720 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005721 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5722 if (ldrOpc == ARM::t2LDREX)
5723 MIB.addImm(0);
5724 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005725
5726 // Sign extend the value, if necessary.
5727 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005728 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005729 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5730 .addReg(dest)
5731 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005732 }
5733
5734 // Build compare and cmov instructions.
5735 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5736 .addReg(oldval).addReg(incr));
5737 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005738 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005739
Jim Grosbachb6aed502011-09-09 18:37:27 +00005740 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5741 if (strOpc == ARM::t2STREX)
5742 MIB.addImm(0);
5743 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005744 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5745 .addReg(scratch).addImm(0));
5746 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5747 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5748
5749 BB->addSuccessor(loopMBB);
5750 BB->addSuccessor(exitMBB);
5751
5752 // exitMBB:
5753 // ...
5754 BB = exitMBB;
5755
5756 MI->eraseFromParent(); // The instruction is gone now.
5757
5758 return BB;
5759}
5760
Eli Friedman2bdffe42011-08-31 00:31:29 +00005761MachineBasicBlock *
5762ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5763 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00005764 bool NeedsCarry, bool IsCmpxchg,
5765 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005766 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5767 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5768
5769 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5770 MachineFunction *MF = BB->getParent();
5771 MachineFunction::iterator It = BB;
5772 ++It;
5773
5774 unsigned destlo = MI->getOperand(0).getReg();
5775 unsigned desthi = MI->getOperand(1).getReg();
5776 unsigned ptr = MI->getOperand(2).getReg();
5777 unsigned vallo = MI->getOperand(3).getReg();
5778 unsigned valhi = MI->getOperand(4).getReg();
5779 DebugLoc dl = MI->getDebugLoc();
5780 bool isThumb2 = Subtarget->isThumb2();
5781
5782 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5783 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005784 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5785 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5786 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005787 }
5788
5789 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5790 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5791
5792 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005793 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005794 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00005795 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005796 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00005797 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005798 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005799
Eli Friedman2bdffe42011-08-31 00:31:29 +00005800 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005801 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
5802 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005803 MF->insert(It, exitMBB);
5804
5805 // Transfer the remainder of BB and its successor edges to exitMBB.
5806 exitMBB->splice(exitMBB->begin(), BB,
5807 llvm::next(MachineBasicBlock::iterator(MI)),
5808 BB->end());
5809 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5810
Craig Topper420761a2012-04-20 07:30:17 +00005811 const TargetRegisterClass *TRC = isThumb2 ?
5812 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5813 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005814 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5815
5816 // thisMBB:
5817 // ...
5818 // fallthrough --> loopMBB
5819 BB->addSuccessor(loopMBB);
5820
5821 // loopMBB:
5822 // ldrexd r2, r3, ptr
5823 // <binopa> r0, r2, incr
5824 // <binopb> r1, r3, incr
5825 // strexd storesuccess, r0, r1, ptr
5826 // cmp storesuccess, #0
5827 // bne- loopMBB
5828 // fallthrough --> exitMBB
5829 //
5830 // Note that the registers are explicitly specified because there is not any
5831 // way to force the register allocator to allocate a register pair.
5832 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005833 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005834 // need to properly enforce the restriction that the two output registers
5835 // for ldrexd must be different.
5836 BB = loopMBB;
5837 // Load
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005838 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5839 unsigned GPRPair1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005840 unsigned GPRPair2;
5841 if (IsMinMax) {
5842 //We need an extra double register for doing min/max.
5843 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5844 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5845 GPRPair2 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5846 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
5847 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5848 .addReg(undef)
5849 .addReg(vallo)
5850 .addImm(ARM::gsub_0);
5851 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair2)
5852 .addReg(r1)
5853 .addReg(valhi)
5854 .addImm(ARM::gsub_1);
5855 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005856
Eli Friedman2bdffe42011-08-31 00:31:29 +00005857 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005858 .addReg(GPRPair0, RegState::Define).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00005859 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005860 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
5861 .addReg(GPRPair0, 0, ARM::gsub_0);
5862 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
5863 .addReg(GPRPair0, 0, ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005864
5865 if (IsCmpxchg) {
5866 // Add early exit
5867 for (unsigned i = 0; i < 2; i++) {
5868 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5869 ARM::CMPrr))
5870 .addReg(i == 0 ? destlo : desthi)
5871 .addReg(i == 0 ? vallo : valhi));
5872 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5873 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5874 BB->addSuccessor(exitMBB);
5875 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5876 BB = (i == 0 ? contBB : cont2BB);
5877 }
5878
5879 // Copy to physregs for strexd
5880 unsigned setlo = MI->getOperand(5).getReg();
5881 unsigned sethi = MI->getOperand(6).getReg();
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005882 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5883 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5884 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
5885 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5886 .addReg(undef)
5887 .addReg(setlo)
5888 .addImm(ARM::gsub_0);
5889 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
5890 .addReg(r1)
5891 .addReg(sethi)
5892 .addImm(ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005893 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005894 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005895 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
5896 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00005897 .addReg(destlo).addReg(vallo))
5898 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005899 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
5900 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00005901 .addReg(desthi).addReg(valhi))
5902 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005903
5904 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5905 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
5906 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5907 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5908 .addReg(UndefPair)
5909 .addReg(tmpRegLo)
5910 .addImm(ARM::gsub_0);
5911 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
5912 .addReg(r1)
5913 .addReg(tmpRegHi)
5914 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005915 } else {
5916 // Copy to physregs for strexd
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005917 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5918 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5919 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
5920 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5921 .addReg(UndefPair)
5922 .addReg(vallo)
5923 .addImm(ARM::gsub_0);
5924 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
5925 .addReg(r1)
5926 .addReg(valhi)
5927 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005928 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00005929 unsigned GPRPairStore = GPRPair1;
5930 if (IsMinMax) {
5931 // Compare and branch to exit block.
5932 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5933 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
5934 BB->addSuccessor(exitMBB);
5935 BB->addSuccessor(contBB);
5936 BB = contBB;
5937 GPRPairStore = GPRPair2;
5938 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005939
5940 // Store
5941 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
Silviu Baranga35b3df62012-11-29 14:41:25 +00005942 .addReg(GPRPairStore).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00005943 // Cmp+jump
5944 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5945 .addReg(storesuccess).addImm(0));
5946 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5947 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5948
5949 BB->addSuccessor(loopMBB);
5950 BB->addSuccessor(exitMBB);
5951
5952 // exitMBB:
5953 // ...
5954 BB = exitMBB;
5955
5956 MI->eraseFromParent(); // The instruction is gone now.
5957
5958 return BB;
5959}
5960
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005961/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5962/// registers the function context.
5963void ARMTargetLowering::
5964SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5965 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005966 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5967 DebugLoc dl = MI->getDebugLoc();
5968 MachineFunction *MF = MBB->getParent();
5969 MachineRegisterInfo *MRI = &MF->getRegInfo();
5970 MachineConstantPool *MCP = MF->getConstantPool();
5971 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5972 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005973
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005974 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005975 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005976
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005977 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005978 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005979 ARMConstantPoolValue *CPV =
5980 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5981 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5982
Craig Topper420761a2012-04-20 07:30:17 +00005983 const TargetRegisterClass *TRC = isThumb ?
5984 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5985 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005986
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005987 // Grab constant pool and fixed stack memory operands.
5988 MachineMemOperand *CPMMO =
5989 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5990 MachineMemOperand::MOLoad, 4, 4);
5991
5992 MachineMemOperand *FIMMOSt =
5993 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5994 MachineMemOperand::MOStore, 4, 4);
5995
5996 // Load the address of the dispatch MBB into the jump buffer.
5997 if (isThumb2) {
5998 // Incoming value: jbuf
5999 // ldr.n r5, LCPI1_1
6000 // orr r5, r5, #1
6001 // add r5, pc
6002 // str r5, [$jbuf, #+4] ; &jbuf[1]
6003 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6004 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6005 .addConstantPoolIndex(CPI)
6006 .addMemOperand(CPMMO));
6007 // Set the low bit because of thumb mode.
6008 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6009 AddDefaultCC(
6010 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6011 .addReg(NewVReg1, RegState::Kill)
6012 .addImm(0x01)));
6013 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6014 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6015 .addReg(NewVReg2, RegState::Kill)
6016 .addImm(PCLabelId);
6017 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6018 .addReg(NewVReg3, RegState::Kill)
6019 .addFrameIndex(FI)
6020 .addImm(36) // &jbuf[1] :: pc
6021 .addMemOperand(FIMMOSt));
6022 } else if (isThumb) {
6023 // Incoming value: jbuf
6024 // ldr.n r1, LCPI1_4
6025 // add r1, pc
6026 // mov r2, #1
6027 // orrs r1, r2
6028 // add r2, $jbuf, #+4 ; &jbuf[1]
6029 // str r1, [r2]
6030 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6031 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6032 .addConstantPoolIndex(CPI)
6033 .addMemOperand(CPMMO));
6034 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6035 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6036 .addReg(NewVReg1, RegState::Kill)
6037 .addImm(PCLabelId);
6038 // Set the low bit because of thumb mode.
6039 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6040 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6041 .addReg(ARM::CPSR, RegState::Define)
6042 .addImm(1));
6043 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6044 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6045 .addReg(ARM::CPSR, RegState::Define)
6046 .addReg(NewVReg2, RegState::Kill)
6047 .addReg(NewVReg3, RegState::Kill));
6048 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6049 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6050 .addFrameIndex(FI)
6051 .addImm(36)); // &jbuf[1] :: pc
6052 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6053 .addReg(NewVReg4, RegState::Kill)
6054 .addReg(NewVReg5, RegState::Kill)
6055 .addImm(0)
6056 .addMemOperand(FIMMOSt));
6057 } else {
6058 // Incoming value: jbuf
6059 // ldr r1, LCPI1_1
6060 // add r1, pc, r1
6061 // str r1, [$jbuf, #+4] ; &jbuf[1]
6062 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6063 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6064 .addConstantPoolIndex(CPI)
6065 .addImm(0)
6066 .addMemOperand(CPMMO));
6067 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6068 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6069 .addReg(NewVReg1, RegState::Kill)
6070 .addImm(PCLabelId));
6071 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6072 .addReg(NewVReg2, RegState::Kill)
6073 .addFrameIndex(FI)
6074 .addImm(36) // &jbuf[1] :: pc
6075 .addMemOperand(FIMMOSt));
6076 }
6077}
6078
6079MachineBasicBlock *ARMTargetLowering::
6080EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6081 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6082 DebugLoc dl = MI->getDebugLoc();
6083 MachineFunction *MF = MBB->getParent();
6084 MachineRegisterInfo *MRI = &MF->getRegInfo();
6085 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6086 MachineFrameInfo *MFI = MF->getFrameInfo();
6087 int FI = MFI->getFunctionContextIndex();
6088
Craig Topper420761a2012-04-20 07:30:17 +00006089 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6090 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006091 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006092
Bill Wendling04f15b42011-10-06 21:29:56 +00006093 // Get a mapping of the call site numbers to all of the landing pads they're
6094 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006095 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6096 unsigned MaxCSNum = 0;
6097 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006098 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6099 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006100 if (!BB->isLandingPad()) continue;
6101
6102 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6103 // pad.
6104 for (MachineBasicBlock::iterator
6105 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6106 if (!II->isEHLabel()) continue;
6107
6108 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006109 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006110
Bill Wendling5cbef192011-10-05 23:28:57 +00006111 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6112 for (SmallVectorImpl<unsigned>::iterator
6113 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6114 CSI != CSE; ++CSI) {
6115 CallSiteNumToLPad[*CSI].push_back(BB);
6116 MaxCSNum = std::max(MaxCSNum, *CSI);
6117 }
Bill Wendling2a850152011-10-05 00:02:33 +00006118 break;
6119 }
6120 }
6121
6122 // Get an ordered list of the machine basic blocks for the jump table.
6123 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006124 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006125 LPadList.reserve(CallSiteNumToLPad.size());
6126 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6127 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6128 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006129 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006130 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006131 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6132 }
Bill Wendling2a850152011-10-05 00:02:33 +00006133 }
6134
Bill Wendling5cbef192011-10-05 23:28:57 +00006135 assert(!LPadList.empty() &&
6136 "No landing pad destinations for the dispatch jump table!");
6137
Bill Wendling04f15b42011-10-06 21:29:56 +00006138 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006139 MachineJumpTableInfo *JTI =
6140 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6141 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6142 unsigned UId = AFI->createJumpTableUId();
6143
Bill Wendling04f15b42011-10-06 21:29:56 +00006144 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006145
6146 // Shove the dispatch's address into the return slot in the function context.
6147 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6148 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006149
Bill Wendlingbb734682011-10-05 00:39:32 +00006150 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00006151 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00006152 DispatchBB->addSuccessor(TrapBB);
6153
6154 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6155 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006156
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006157 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006158 MF->insert(MF->end(), DispatchBB);
6159 MF->insert(MF->end(), DispContBB);
6160 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006161
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006162 // Insert code into the entry block that creates and registers the function
6163 // context.
6164 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6165
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006166 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006167 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006168 MachineMemOperand::MOLoad |
6169 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006170
Chad Rosiere7bd5192012-11-06 23:05:24 +00006171 MachineInstrBuilder MIB;
6172 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6173
6174 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6175 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6176
6177 // Add a register mask with no preserved registers. This results in all
6178 // registers being marked as clobbered.
6179 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006180
Bill Wendling952cb502011-10-18 22:49:07 +00006181 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006182 if (Subtarget->isThumb2()) {
6183 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6184 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6185 .addFrameIndex(FI)
6186 .addImm(4)
6187 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006188
Bill Wendling952cb502011-10-18 22:49:07 +00006189 if (NumLPads < 256) {
6190 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6191 .addReg(NewVReg1)
6192 .addImm(LPadList.size()));
6193 } else {
6194 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6195 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006196 .addImm(NumLPads & 0xFFFF));
6197
6198 unsigned VReg2 = VReg1;
6199 if ((NumLPads & 0xFFFF0000) != 0) {
6200 VReg2 = MRI->createVirtualRegister(TRC);
6201 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6202 .addReg(VReg1)
6203 .addImm(NumLPads >> 16));
6204 }
6205
Bill Wendling952cb502011-10-18 22:49:07 +00006206 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6207 .addReg(NewVReg1)
6208 .addReg(VReg2));
6209 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006210
Bill Wendling95ce2e92011-10-06 22:53:00 +00006211 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6212 .addMBB(TrapBB)
6213 .addImm(ARMCC::HI)
6214 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006215
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006216 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6217 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006218 .addJumpTableIndex(MJTI)
6219 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006220
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006221 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006222 AddDefaultCC(
6223 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006224 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6225 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006226 .addReg(NewVReg1)
6227 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6228
6229 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006230 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006231 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006232 .addJumpTableIndex(MJTI)
6233 .addImm(UId);
6234 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006235 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6236 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6237 .addFrameIndex(FI)
6238 .addImm(1)
6239 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006240
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006241 if (NumLPads < 256) {
6242 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6243 .addReg(NewVReg1)
6244 .addImm(NumLPads));
6245 } else {
6246 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006247 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6248 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6249
6250 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006251 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006252 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006253 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006254 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006255
6256 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6257 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6258 .addReg(VReg1, RegState::Define)
6259 .addConstantPoolIndex(Idx));
6260 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6261 .addReg(NewVReg1)
6262 .addReg(VReg1));
6263 }
6264
Bill Wendling083a8eb2011-10-06 23:37:36 +00006265 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6266 .addMBB(TrapBB)
6267 .addImm(ARMCC::HI)
6268 .addReg(ARM::CPSR);
6269
6270 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6271 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6272 .addReg(ARM::CPSR, RegState::Define)
6273 .addReg(NewVReg1)
6274 .addImm(2));
6275
6276 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006277 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006278 .addJumpTableIndex(MJTI)
6279 .addImm(UId));
6280
6281 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6282 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6283 .addReg(ARM::CPSR, RegState::Define)
6284 .addReg(NewVReg2, RegState::Kill)
6285 .addReg(NewVReg3));
6286
6287 MachineMemOperand *JTMMOLd =
6288 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6289 MachineMemOperand::MOLoad, 4, 4);
6290
6291 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6292 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6293 .addReg(NewVReg4, RegState::Kill)
6294 .addImm(0)
6295 .addMemOperand(JTMMOLd));
6296
6297 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6298 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6299 .addReg(ARM::CPSR, RegState::Define)
6300 .addReg(NewVReg5, RegState::Kill)
6301 .addReg(NewVReg3));
6302
6303 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6304 .addReg(NewVReg6, RegState::Kill)
6305 .addJumpTableIndex(MJTI)
6306 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006307 } else {
6308 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6309 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6310 .addFrameIndex(FI)
6311 .addImm(4)
6312 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006313
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006314 if (NumLPads < 256) {
6315 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6316 .addReg(NewVReg1)
6317 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006318 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006319 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6320 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006321 .addImm(NumLPads & 0xFFFF));
6322
6323 unsigned VReg2 = VReg1;
6324 if ((NumLPads & 0xFFFF0000) != 0) {
6325 VReg2 = MRI->createVirtualRegister(TRC);
6326 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6327 .addReg(VReg1)
6328 .addImm(NumLPads >> 16));
6329 }
6330
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006331 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6332 .addReg(NewVReg1)
6333 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006334 } else {
6335 MachineConstantPool *ConstantPool = MF->getConstantPool();
6336 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6337 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6338
6339 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006340 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006341 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006342 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006343 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6344
6345 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6346 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6347 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006348 .addConstantPoolIndex(Idx)
6349 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006350 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6351 .addReg(NewVReg1)
6352 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006353 }
6354
Bill Wendling95ce2e92011-10-06 22:53:00 +00006355 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6356 .addMBB(TrapBB)
6357 .addImm(ARMCC::HI)
6358 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006359
Bill Wendling564392b2011-10-18 22:11:18 +00006360 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006361 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006362 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006363 .addReg(NewVReg1)
6364 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006365 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6366 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006367 .addJumpTableIndex(MJTI)
6368 .addImm(UId));
6369
6370 MachineMemOperand *JTMMOLd =
6371 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6372 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006373 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006374 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006375 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6376 .addReg(NewVReg3, RegState::Kill)
6377 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006378 .addImm(0)
6379 .addMemOperand(JTMMOLd));
6380
6381 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006382 .addReg(NewVReg5, RegState::Kill)
6383 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006384 .addJumpTableIndex(MJTI)
6385 .addImm(UId);
6386 }
Bill Wendling2a850152011-10-05 00:02:33 +00006387
Bill Wendlingbb734682011-10-05 00:39:32 +00006388 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006389 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006390 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006391 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6392 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006393 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006394 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006395 }
6396
Bill Wendling24bb9252011-10-17 05:25:09 +00006397 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006398 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006399 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006400 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6401 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6402 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006403
6404 // Remove the landing pad successor from the invoke block and replace it
6405 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006406 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6407 BB->succ_end());
6408 while (!Successors.empty()) {
6409 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006410 if (SMBB->isLandingPad()) {
6411 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006412 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006413 }
6414 }
6415
6416 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006417
6418 // Find the invoke call and mark all of the callee-saved registers as
6419 // 'implicit defined' so that they're spilled. This prevents code from
6420 // moving instructions to before the EH block, where they will never be
6421 // executed.
6422 for (MachineBasicBlock::reverse_iterator
6423 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006424 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006425
6426 DenseMap<unsigned, bool> DefRegs;
6427 for (MachineInstr::mop_iterator
6428 OI = II->operands_begin(), OE = II->operands_end();
6429 OI != OE; ++OI) {
6430 if (!OI->isReg()) continue;
6431 DefRegs[OI->getReg()] = true;
6432 }
6433
6434 MachineInstrBuilder MIB(&*II);
6435
Bill Wendling5d798592011-10-14 23:55:44 +00006436 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006437 unsigned Reg = SavedRegs[i];
6438 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006439 !ARM::tGPRRegClass.contains(Reg) &&
6440 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006441 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006442 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006443 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006444 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006445 continue;
6446 if (!DefRegs[Reg])
6447 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006448 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006449
6450 break;
6451 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006452 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006453
Bill Wendlingf7b02072011-10-18 18:30:49 +00006454 // Mark all former landing pads as non-landing pads. The dispatch is the only
6455 // landing pad now.
6456 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6457 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6458 (*I)->setIsLandingPad(false);
6459
Bill Wendlingbb734682011-10-05 00:39:32 +00006460 // The instruction is gone now.
6461 MI->eraseFromParent();
6462
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006463 return MBB;
6464}
6465
Evan Cheng218977b2010-07-13 19:27:42 +00006466static
6467MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6468 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6469 E = MBB->succ_end(); I != E; ++I)
6470 if (*I != Succ)
6471 return *I;
6472 llvm_unreachable("Expecting a BB with two successors!");
6473}
6474
Manman Ren68f25572012-06-01 19:33:18 +00006475MachineBasicBlock *ARMTargetLowering::
6476EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6477 // This pseudo instruction has 3 operands: dst, src, size
6478 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6479 // Otherwise, we will generate unrolled scalar copies.
6480 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6481 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6482 MachineFunction::iterator It = BB;
6483 ++It;
6484
6485 unsigned dest = MI->getOperand(0).getReg();
6486 unsigned src = MI->getOperand(1).getReg();
6487 unsigned SizeVal = MI->getOperand(2).getImm();
6488 unsigned Align = MI->getOperand(3).getImm();
6489 DebugLoc dl = MI->getDebugLoc();
6490
6491 bool isThumb2 = Subtarget->isThumb2();
6492 MachineFunction *MF = BB->getParent();
6493 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006494 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006495
6496 const TargetRegisterClass *TRC = isThumb2 ?
6497 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6498 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006499 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006500
6501 if (Align & 1) {
6502 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6503 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6504 UnitSize = 1;
6505 } else if (Align & 2) {
6506 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6507 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6508 UnitSize = 2;
6509 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006510 // Check whether we can use NEON instructions.
Bill Wendling67658342012-10-09 07:45:08 +00006511 if (!MF->getFunction()->getFnAttributes().
6512 hasAttribute(Attributes::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006513 Subtarget->hasNEON()) {
6514 if ((Align % 16 == 0) && SizeVal >= 16) {
6515 ldrOpc = ARM::VLD1q32wb_fixed;
6516 strOpc = ARM::VST1q32wb_fixed;
6517 UnitSize = 16;
6518 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6519 }
6520 else if ((Align % 8 == 0) && SizeVal >= 8) {
6521 ldrOpc = ARM::VLD1d32wb_fixed;
6522 strOpc = ARM::VST1d32wb_fixed;
6523 UnitSize = 8;
6524 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6525 }
6526 }
6527 // Can't use NEON instructions.
6528 if (UnitSize == 0) {
6529 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6530 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6531 UnitSize = 4;
6532 }
Manman Ren68f25572012-06-01 19:33:18 +00006533 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006534
Manman Ren68f25572012-06-01 19:33:18 +00006535 unsigned BytesLeft = SizeVal % UnitSize;
6536 unsigned LoopSize = SizeVal - BytesLeft;
6537
6538 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6539 // Use LDR and STR to copy.
6540 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6541 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6542 unsigned srcIn = src;
6543 unsigned destIn = dest;
6544 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006545 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006546 unsigned srcOut = MRI.createVirtualRegister(TRC);
6547 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006548 if (UnitSize >= 8) {
6549 AddDefaultPred(BuildMI(*BB, MI, dl,
6550 TII->get(ldrOpc), scratch)
6551 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6552
6553 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6554 .addReg(destIn).addImm(0).addReg(scratch));
6555 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006556 AddDefaultPred(BuildMI(*BB, MI, dl,
6557 TII->get(ldrOpc), scratch)
6558 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6559
6560 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6561 .addReg(scratch).addReg(destIn)
6562 .addImm(UnitSize));
6563 } else {
6564 AddDefaultPred(BuildMI(*BB, MI, dl,
6565 TII->get(ldrOpc), scratch)
6566 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6567 .addImm(UnitSize));
6568
6569 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6570 .addReg(scratch).addReg(destIn)
6571 .addReg(0).addImm(UnitSize));
6572 }
6573 srcIn = srcOut;
6574 destIn = destOut;
6575 }
6576
6577 // Handle the leftover bytes with LDRB and STRB.
6578 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6579 // [destOut] = STRB_POST(scratch, destIn, 1)
6580 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6581 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6582 for (unsigned i = 0; i < BytesLeft; i++) {
6583 unsigned scratch = MRI.createVirtualRegister(TRC);
6584 unsigned srcOut = MRI.createVirtualRegister(TRC);
6585 unsigned destOut = MRI.createVirtualRegister(TRC);
6586 if (isThumb2) {
6587 AddDefaultPred(BuildMI(*BB, MI, dl,
6588 TII->get(ldrOpc),scratch)
6589 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6590
6591 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6592 .addReg(scratch).addReg(destIn)
6593 .addReg(0).addImm(1));
6594 } else {
6595 AddDefaultPred(BuildMI(*BB, MI, dl,
6596 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006597 .addReg(srcOut, RegState::Define).addReg(srcIn)
6598 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006599
6600 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6601 .addReg(scratch).addReg(destIn)
6602 .addReg(0).addImm(1));
6603 }
6604 srcIn = srcOut;
6605 destIn = destOut;
6606 }
6607 MI->eraseFromParent(); // The instruction is gone now.
6608 return BB;
6609 }
6610
6611 // Expand the pseudo op to a loop.
6612 // thisMBB:
6613 // ...
6614 // movw varEnd, # --> with thumb2
6615 // movt varEnd, #
6616 // ldrcp varEnd, idx --> without thumb2
6617 // fallthrough --> loopMBB
6618 // loopMBB:
6619 // PHI varPhi, varEnd, varLoop
6620 // PHI srcPhi, src, srcLoop
6621 // PHI destPhi, dst, destLoop
6622 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6623 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6624 // subs varLoop, varPhi, #UnitSize
6625 // bne loopMBB
6626 // fallthrough --> exitMBB
6627 // exitMBB:
6628 // epilogue to handle left-over bytes
6629 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6630 // [destOut] = STRB_POST(scratch, destLoop, 1)
6631 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6632 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6633 MF->insert(It, loopMBB);
6634 MF->insert(It, exitMBB);
6635
6636 // Transfer the remainder of BB and its successor edges to exitMBB.
6637 exitMBB->splice(exitMBB->begin(), BB,
6638 llvm::next(MachineBasicBlock::iterator(MI)),
6639 BB->end());
6640 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6641
6642 // Load an immediate to varEnd.
6643 unsigned varEnd = MRI.createVirtualRegister(TRC);
6644 if (isThumb2) {
6645 unsigned VReg1 = varEnd;
6646 if ((LoopSize & 0xFFFF0000) != 0)
6647 VReg1 = MRI.createVirtualRegister(TRC);
6648 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6649 .addImm(LoopSize & 0xFFFF));
6650
6651 if ((LoopSize & 0xFFFF0000) != 0)
6652 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6653 .addReg(VReg1)
6654 .addImm(LoopSize >> 16));
6655 } else {
6656 MachineConstantPool *ConstantPool = MF->getConstantPool();
6657 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6658 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6659
6660 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006661 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006662 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006663 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006664 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6665
6666 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6667 .addReg(varEnd, RegState::Define)
6668 .addConstantPoolIndex(Idx)
6669 .addImm(0));
6670 }
6671 BB->addSuccessor(loopMBB);
6672
6673 // Generate the loop body:
6674 // varPhi = PHI(varLoop, varEnd)
6675 // srcPhi = PHI(srcLoop, src)
6676 // destPhi = PHI(destLoop, dst)
6677 MachineBasicBlock *entryBB = BB;
6678 BB = loopMBB;
6679 unsigned varLoop = MRI.createVirtualRegister(TRC);
6680 unsigned varPhi = MRI.createVirtualRegister(TRC);
6681 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6682 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6683 unsigned destLoop = MRI.createVirtualRegister(TRC);
6684 unsigned destPhi = MRI.createVirtualRegister(TRC);
6685
6686 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6687 .addReg(varLoop).addMBB(loopMBB)
6688 .addReg(varEnd).addMBB(entryBB);
6689 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6690 .addReg(srcLoop).addMBB(loopMBB)
6691 .addReg(src).addMBB(entryBB);
6692 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6693 .addReg(destLoop).addMBB(loopMBB)
6694 .addReg(dest).addMBB(entryBB);
6695
6696 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6697 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006698 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6699 if (UnitSize >= 8) {
6700 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6701 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6702
6703 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6704 .addReg(destPhi).addImm(0).addReg(scratch));
6705 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006706 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6707 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6708
6709 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6710 .addReg(scratch).addReg(destPhi)
6711 .addImm(UnitSize));
6712 } else {
6713 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6714 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6715 .addImm(UnitSize));
6716
6717 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6718 .addReg(scratch).addReg(destPhi)
6719 .addReg(0).addImm(UnitSize));
6720 }
6721
6722 // Decrement loop variable by UnitSize.
6723 MachineInstrBuilder MIB = BuildMI(BB, dl,
6724 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6725 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6726 MIB->getOperand(5).setReg(ARM::CPSR);
6727 MIB->getOperand(5).setIsDef(true);
6728
6729 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6730 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6731
6732 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6733 BB->addSuccessor(loopMBB);
6734 BB->addSuccessor(exitMBB);
6735
6736 // Add epilogue to handle BytesLeft.
6737 BB = exitMBB;
6738 MachineInstr *StartOfExit = exitMBB->begin();
6739 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6740 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6741
6742 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6743 // [destOut] = STRB_POST(scratch, destLoop, 1)
6744 unsigned srcIn = srcLoop;
6745 unsigned destIn = destLoop;
6746 for (unsigned i = 0; i < BytesLeft; i++) {
6747 unsigned scratch = MRI.createVirtualRegister(TRC);
6748 unsigned srcOut = MRI.createVirtualRegister(TRC);
6749 unsigned destOut = MRI.createVirtualRegister(TRC);
6750 if (isThumb2) {
6751 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6752 TII->get(ldrOpc),scratch)
6753 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6754
6755 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6756 .addReg(scratch).addReg(destIn)
6757 .addImm(1));
6758 } else {
6759 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6760 TII->get(ldrOpc),scratch)
6761 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6762
6763 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6764 .addReg(scratch).addReg(destIn)
6765 .addReg(0).addImm(1));
6766 }
6767 srcIn = srcOut;
6768 destIn = destOut;
6769 }
6770
6771 MI->eraseFromParent(); // The instruction is gone now.
6772 return BB;
6773}
6774
Jim Grosbache801dc42009-12-12 01:40:06 +00006775MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006776ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006777 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006778 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006779 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006780 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006781 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006782 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006783 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006784 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006785 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006786 // The Thumb2 pre-indexed stores have the same MI operands, they just
6787 // define them differently in the .td files from the isel patterns, so
6788 // they need pseudos.
6789 case ARM::t2STR_preidx:
6790 MI->setDesc(TII->get(ARM::t2STR_PRE));
6791 return BB;
6792 case ARM::t2STRB_preidx:
6793 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6794 return BB;
6795 case ARM::t2STRH_preidx:
6796 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6797 return BB;
6798
Jim Grosbach19dec202011-08-05 20:35:44 +00006799 case ARM::STRi_preidx:
6800 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006801 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006802 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6803 // Decode the offset.
6804 unsigned Offset = MI->getOperand(4).getImm();
6805 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6806 Offset = ARM_AM::getAM2Offset(Offset);
6807 if (isSub)
6808 Offset = -Offset;
6809
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006810 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006811 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006812 .addOperand(MI->getOperand(0)) // Rn_wb
6813 .addOperand(MI->getOperand(1)) // Rt
6814 .addOperand(MI->getOperand(2)) // Rn
6815 .addImm(Offset) // offset (skip GPR==zero_reg)
6816 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006817 .addOperand(MI->getOperand(6))
6818 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006819 MI->eraseFromParent();
6820 return BB;
6821 }
6822 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006823 case ARM::STRBr_preidx:
6824 case ARM::STRH_preidx: {
6825 unsigned NewOpc;
6826 switch (MI->getOpcode()) {
6827 default: llvm_unreachable("unexpected opcode!");
6828 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6829 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6830 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6831 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006832 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6833 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6834 MIB.addOperand(MI->getOperand(i));
6835 MI->eraseFromParent();
6836 return BB;
6837 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006838 case ARM::ATOMIC_LOAD_ADD_I8:
6839 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6840 case ARM::ATOMIC_LOAD_ADD_I16:
6841 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6842 case ARM::ATOMIC_LOAD_ADD_I32:
6843 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006844
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006845 case ARM::ATOMIC_LOAD_AND_I8:
6846 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6847 case ARM::ATOMIC_LOAD_AND_I16:
6848 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6849 case ARM::ATOMIC_LOAD_AND_I32:
6850 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006851
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006852 case ARM::ATOMIC_LOAD_OR_I8:
6853 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6854 case ARM::ATOMIC_LOAD_OR_I16:
6855 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6856 case ARM::ATOMIC_LOAD_OR_I32:
6857 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006858
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006859 case ARM::ATOMIC_LOAD_XOR_I8:
6860 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6861 case ARM::ATOMIC_LOAD_XOR_I16:
6862 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6863 case ARM::ATOMIC_LOAD_XOR_I32:
6864 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006865
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006866 case ARM::ATOMIC_LOAD_NAND_I8:
6867 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6868 case ARM::ATOMIC_LOAD_NAND_I16:
6869 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6870 case ARM::ATOMIC_LOAD_NAND_I32:
6871 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006872
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006873 case ARM::ATOMIC_LOAD_SUB_I8:
6874 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6875 case ARM::ATOMIC_LOAD_SUB_I16:
6876 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6877 case ARM::ATOMIC_LOAD_SUB_I32:
6878 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006879
Jim Grosbachf7da8822011-04-26 19:44:18 +00006880 case ARM::ATOMIC_LOAD_MIN_I8:
6881 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6882 case ARM::ATOMIC_LOAD_MIN_I16:
6883 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6884 case ARM::ATOMIC_LOAD_MIN_I32:
6885 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6886
6887 case ARM::ATOMIC_LOAD_MAX_I8:
6888 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6889 case ARM::ATOMIC_LOAD_MAX_I16:
6890 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6891 case ARM::ATOMIC_LOAD_MAX_I32:
6892 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6893
6894 case ARM::ATOMIC_LOAD_UMIN_I8:
6895 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6896 case ARM::ATOMIC_LOAD_UMIN_I16:
6897 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6898 case ARM::ATOMIC_LOAD_UMIN_I32:
6899 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6900
6901 case ARM::ATOMIC_LOAD_UMAX_I8:
6902 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6903 case ARM::ATOMIC_LOAD_UMAX_I16:
6904 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6905 case ARM::ATOMIC_LOAD_UMAX_I32:
6906 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6907
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006908 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6909 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6910 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006911
6912 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6913 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6914 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006915
Eli Friedman2bdffe42011-08-31 00:31:29 +00006916
6917 case ARM::ATOMADD6432:
6918 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006919 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6920 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006921 case ARM::ATOMSUB6432:
6922 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006923 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6924 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006925 case ARM::ATOMOR6432:
6926 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006927 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006928 case ARM::ATOMXOR6432:
6929 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006930 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006931 case ARM::ATOMAND6432:
6932 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006933 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006934 case ARM::ATOMSWAP6432:
6935 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006936 case ARM::ATOMCMPXCHG6432:
6937 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6938 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6939 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006940 case ARM::ATOMMIN6432:
6941 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6942 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6943 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
6944 /*IsMinMax*/ true, ARMCC::LE);
6945 case ARM::ATOMMAX6432:
6946 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6947 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6948 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
6949 /*IsMinMax*/ true, ARMCC::GE);
6950 case ARM::ATOMUMIN6432:
6951 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6952 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6953 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
6954 /*IsMinMax*/ true, ARMCC::LS);
6955 case ARM::ATOMUMAX6432:
6956 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6957 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6958 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
6959 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006960
Evan Cheng007ea272009-08-12 05:17:19 +00006961 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006962 // To "insert" a SELECT_CC instruction, we actually have to insert the
6963 // diamond control-flow pattern. The incoming instruction knows the
6964 // destination vreg to set, the condition code register to branch on, the
6965 // true/false values to select between, and a branch opcode to use.
6966 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006967 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006968 ++It;
6969
6970 // thisMBB:
6971 // ...
6972 // TrueVal = ...
6973 // cmpTY ccX, r1, r2
6974 // bCC copy1MBB
6975 // fallthrough --> copy0MBB
6976 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006977 MachineFunction *F = BB->getParent();
6978 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6979 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006980 F->insert(It, copy0MBB);
6981 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006982
6983 // Transfer the remainder of BB and its successor edges to sinkMBB.
6984 sinkMBB->splice(sinkMBB->begin(), BB,
6985 llvm::next(MachineBasicBlock::iterator(MI)),
6986 BB->end());
6987 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6988
Dan Gohman258c58c2010-07-06 15:49:48 +00006989 BB->addSuccessor(copy0MBB);
6990 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006991
Dan Gohman14152b42010-07-06 20:24:04 +00006992 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6993 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6994
Evan Chenga8e29892007-01-19 07:51:42 +00006995 // copy0MBB:
6996 // %FalseValue = ...
6997 // # fallthrough to sinkMBB
6998 BB = copy0MBB;
6999
7000 // Update machine-CFG edges
7001 BB->addSuccessor(sinkMBB);
7002
7003 // sinkMBB:
7004 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7005 // ...
7006 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007007 BuildMI(*BB, BB->begin(), dl,
7008 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007009 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7010 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7011
Dan Gohman14152b42010-07-06 20:24:04 +00007012 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007013 return BB;
7014 }
Evan Cheng86198642009-08-07 00:34:42 +00007015
Evan Cheng218977b2010-07-13 19:27:42 +00007016 case ARM::BCCi64:
7017 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007018 // If there is an unconditional branch to the other successor, remove it.
7019 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007020
Evan Cheng218977b2010-07-13 19:27:42 +00007021 // Compare both parts that make up the double comparison separately for
7022 // equality.
7023 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7024
7025 unsigned LHS1 = MI->getOperand(1).getReg();
7026 unsigned LHS2 = MI->getOperand(2).getReg();
7027 if (RHSisZero) {
7028 AddDefaultPred(BuildMI(BB, dl,
7029 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7030 .addReg(LHS1).addImm(0));
7031 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7032 .addReg(LHS2).addImm(0)
7033 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7034 } else {
7035 unsigned RHS1 = MI->getOperand(3).getReg();
7036 unsigned RHS2 = MI->getOperand(4).getReg();
7037 AddDefaultPred(BuildMI(BB, dl,
7038 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7039 .addReg(LHS1).addReg(RHS1));
7040 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7041 .addReg(LHS2).addReg(RHS2)
7042 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7043 }
7044
7045 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7046 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7047 if (MI->getOperand(0).getImm() == ARMCC::NE)
7048 std::swap(destMBB, exitMBB);
7049
7050 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7051 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007052 if (isThumb2)
7053 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7054 else
7055 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007056
7057 MI->eraseFromParent(); // The pseudo instruction is gone now.
7058 return BB;
7059 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007060
Bill Wendling5bc85282011-10-17 20:37:20 +00007061 case ARM::Int_eh_sjlj_setjmp:
7062 case ARM::Int_eh_sjlj_setjmp_nofp:
7063 case ARM::tInt_eh_sjlj_setjmp:
7064 case ARM::t2Int_eh_sjlj_setjmp:
7065 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7066 EmitSjLjDispatchBlock(MI, BB);
7067 return BB;
7068
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007069 case ARM::ABS:
7070 case ARM::t2ABS: {
7071 // To insert an ABS instruction, we have to insert the
7072 // diamond control-flow pattern. The incoming instruction knows the
7073 // source vreg to test against 0, the destination vreg to set,
7074 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007075 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007076 // It transforms
7077 // V1 = ABS V0
7078 // into
7079 // V2 = MOVS V0
7080 // BCC (branch to SinkBB if V0 >= 0)
7081 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007082 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007083 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7084 MachineFunction::iterator BBI = BB;
7085 ++BBI;
7086 MachineFunction *Fn = BB->getParent();
7087 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7088 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7089 Fn->insert(BBI, RSBBB);
7090 Fn->insert(BBI, SinkBB);
7091
7092 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7093 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7094 bool isThumb2 = Subtarget->isThumb2();
7095 MachineRegisterInfo &MRI = Fn->getRegInfo();
7096 // In Thumb mode S must not be specified if source register is the SP or
7097 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007098 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7099 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7100 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007101
7102 // Transfer the remainder of BB and its successor edges to sinkMBB.
7103 SinkBB->splice(SinkBB->begin(), BB,
7104 llvm::next(MachineBasicBlock::iterator(MI)),
7105 BB->end());
7106 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7107
7108 BB->addSuccessor(RSBBB);
7109 BB->addSuccessor(SinkBB);
7110
7111 // fall through to SinkMBB
7112 RSBBB->addSuccessor(SinkBB);
7113
Manman Ren307473d2012-06-15 21:32:12 +00007114 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007115 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007116 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7117 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007118
7119 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007120 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007121 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7122 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7123
7124 // insert rsbri in RSBBB
7125 // Note: BCC and rsbri will be converted into predicated rsbmi
7126 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007127 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007128 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007129 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007130 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7131
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007132 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007133 // reuse ABSDstReg to not change uses of ABS instruction
7134 BuildMI(*SinkBB, SinkBB->begin(), dl,
7135 TII->get(ARM::PHI), ABSDstReg)
7136 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007137 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007138
7139 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007140 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007141
7142 // return last added BB
7143 return SinkBB;
7144 }
Manman Ren68f25572012-06-01 19:33:18 +00007145 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007146 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007147 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007148 }
7149}
7150
Evan Cheng37fefc22011-08-30 19:09:48 +00007151void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7152 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007153 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007154 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7155 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7156 return;
7157 }
7158
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007159 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007160 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7161 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7162 // operand is still set to noreg. If needed, set the optional operand's
7163 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007164 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007165 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007166
Andrew Trick3be654f2011-09-21 02:20:46 +00007167 // Rename pseudo opcodes.
7168 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7169 if (NewOpc) {
7170 const ARMBaseInstrInfo *TII =
7171 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007172 MCID = &TII->get(NewOpc);
7173
7174 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7175 "converted opcode should be the same except for cc_out");
7176
7177 MI->setDesc(*MCID);
7178
7179 // Add the optional cc_out operand
7180 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007181 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007182 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007183
7184 // Any ARM instruction that sets the 's' bit should specify an optional
7185 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007186 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007187 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007188 return;
7189 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007190 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7191 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007192 bool definesCPSR = false;
7193 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007194 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007195 i != e; ++i) {
7196 const MachineOperand &MO = MI->getOperand(i);
7197 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7198 definesCPSR = true;
7199 if (MO.isDead())
7200 deadCPSR = true;
7201 MI->RemoveOperand(i);
7202 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007203 }
7204 }
Andrew Trick4815d562011-09-20 03:17:40 +00007205 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007206 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007207 return;
7208 }
7209 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007210 if (deadCPSR) {
7211 assert(!MI->getOperand(ccOutIdx).getReg() &&
7212 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007213 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007214 }
Andrew Trick4815d562011-09-20 03:17:40 +00007215
Andrew Trick3be654f2011-09-21 02:20:46 +00007216 // If this instruction was defined with an optional CPSR def and its dag node
7217 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007218 MachineOperand &MO = MI->getOperand(ccOutIdx);
7219 MO.setReg(ARM::CPSR);
7220 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007221}
7222
Evan Chenga8e29892007-01-19 07:51:42 +00007223//===----------------------------------------------------------------------===//
7224// ARM Optimization Hooks
7225//===----------------------------------------------------------------------===//
7226
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007227// Helper function that checks if N is a null or all ones constant.
7228static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7229 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7230 if (!C)
7231 return false;
7232 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7233}
7234
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007235// Return true if N is conditionally 0 or all ones.
7236// Detects these expressions where cc is an i1 value:
7237//
7238// (select cc 0, y) [AllOnes=0]
7239// (select cc y, 0) [AllOnes=0]
7240// (zext cc) [AllOnes=0]
7241// (sext cc) [AllOnes=0/1]
7242// (select cc -1, y) [AllOnes=1]
7243// (select cc y, -1) [AllOnes=1]
7244//
7245// Invert is set when N is the null/all ones constant when CC is false.
7246// OtherOp is set to the alternative value of N.
7247static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7248 SDValue &CC, bool &Invert,
7249 SDValue &OtherOp,
7250 SelectionDAG &DAG) {
7251 switch (N->getOpcode()) {
7252 default: return false;
7253 case ISD::SELECT: {
7254 CC = N->getOperand(0);
7255 SDValue N1 = N->getOperand(1);
7256 SDValue N2 = N->getOperand(2);
7257 if (isZeroOrAllOnes(N1, AllOnes)) {
7258 Invert = false;
7259 OtherOp = N2;
7260 return true;
7261 }
7262 if (isZeroOrAllOnes(N2, AllOnes)) {
7263 Invert = true;
7264 OtherOp = N1;
7265 return true;
7266 }
7267 return false;
7268 }
7269 case ISD::ZERO_EXTEND:
7270 // (zext cc) can never be the all ones value.
7271 if (AllOnes)
7272 return false;
7273 // Fall through.
7274 case ISD::SIGN_EXTEND: {
7275 EVT VT = N->getValueType(0);
7276 CC = N->getOperand(0);
7277 if (CC.getValueType() != MVT::i1)
7278 return false;
7279 Invert = !AllOnes;
7280 if (AllOnes)
7281 // When looking for an AllOnes constant, N is an sext, and the 'other'
7282 // value is 0.
7283 OtherOp = DAG.getConstant(0, VT);
7284 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7285 // When looking for a 0 constant, N can be zext or sext.
7286 OtherOp = DAG.getConstant(1, VT);
7287 else
7288 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7289 return true;
7290 }
7291 }
7292}
7293
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007294// Combine a constant select operand into its use:
7295//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007296// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7297// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7298// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7299// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7300// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007301//
7302// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007303// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007304//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007305// Also recognize sext/zext from i1:
7306//
7307// (add (zext cc), x) -> (select cc (add x, 1), x)
7308// (add (sext cc), x) -> (select cc (add x, -1), x)
7309//
7310// These transformations eventually create predicated instructions.
7311//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007312// @param N The node to transform.
7313// @param Slct The N operand that is a select.
7314// @param OtherOp The other N operand (x above).
7315// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007316// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007317// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007318static
7319SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007320 TargetLowering::DAGCombinerInfo &DCI,
7321 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007322 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007323 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007324 SDValue NonConstantVal;
7325 SDValue CCOp;
7326 bool SwapSelectOps;
7327 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7328 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007329 return SDValue();
7330
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007331 // Slct is now know to be the desired identity constant when CC is true.
7332 SDValue TrueVal = OtherOp;
7333 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7334 OtherOp, NonConstantVal);
7335 // Unless SwapSelectOps says CC should be false.
7336 if (SwapSelectOps)
7337 std::swap(TrueVal, FalseVal);
7338
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007339 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007340 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007341}
7342
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007343// Attempt combineSelectAndUse on each operand of a commutative operator N.
7344static
7345SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7346 TargetLowering::DAGCombinerInfo &DCI) {
7347 SDValue N0 = N->getOperand(0);
7348 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007349 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007350 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7351 if (Result.getNode())
7352 return Result;
7353 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007354 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007355 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7356 if (Result.getNode())
7357 return Result;
7358 }
7359 return SDValue();
7360}
7361
Eric Christopherfa6f5912011-06-29 21:10:36 +00007362// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007363// (only after legalization).
7364static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7365 TargetLowering::DAGCombinerInfo &DCI,
7366 const ARMSubtarget *Subtarget) {
7367
7368 // Only perform optimization if after legalize, and if NEON is available. We
7369 // also expected both operands to be BUILD_VECTORs.
7370 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7371 || N0.getOpcode() != ISD::BUILD_VECTOR
7372 || N1.getOpcode() != ISD::BUILD_VECTOR)
7373 return SDValue();
7374
7375 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7376 EVT VT = N->getValueType(0);
7377 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7378 return SDValue();
7379
7380 // Check that the vector operands are of the right form.
7381 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7382 // operands, where N is the size of the formed vector.
7383 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7384 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007385
7386 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007387 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007388 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007389 SDValue Vec = N0->getOperand(0)->getOperand(0);
7390 SDNode *V = Vec.getNode();
7391 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007392
Eric Christopherfa6f5912011-06-29 21:10:36 +00007393 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007394 // check to see if each of their operands are an EXTRACT_VECTOR with
7395 // the same vector and appropriate index.
7396 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7397 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7398 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007399
Tanya Lattner189531f2011-06-14 23:48:48 +00007400 SDValue ExtVec0 = N0->getOperand(i);
7401 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007402
Tanya Lattner189531f2011-06-14 23:48:48 +00007403 // First operand is the vector, verify its the same.
7404 if (V != ExtVec0->getOperand(0).getNode() ||
7405 V != ExtVec1->getOperand(0).getNode())
7406 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007407
Tanya Lattner189531f2011-06-14 23:48:48 +00007408 // Second is the constant, verify its correct.
7409 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7410 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007411
Tanya Lattner189531f2011-06-14 23:48:48 +00007412 // For the constant, we want to see all the even or all the odd.
7413 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7414 || C1->getZExtValue() != nextIndex+1)
7415 return SDValue();
7416
7417 // Increment index.
7418 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007419 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007420 return SDValue();
7421 }
7422
7423 // Create VPADDL node.
7424 SelectionDAG &DAG = DCI.DAG;
7425 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007426
7427 // Build operand list.
7428 SmallVector<SDValue, 8> Ops;
7429 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7430 TLI.getPointerTy()));
7431
7432 // Input is the vector.
7433 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007434
Tanya Lattner189531f2011-06-14 23:48:48 +00007435 // Get widened type and narrowed type.
7436 MVT widenType;
7437 unsigned numElem = VT.getVectorNumElements();
7438 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7439 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7440 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7441 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7442 default:
Craig Topperbc219812012-02-07 02:50:20 +00007443 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007444 }
7445
7446 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7447 widenType, &Ops[0], Ops.size());
7448 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7449}
7450
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007451static SDValue findMUL_LOHI(SDValue V) {
7452 if (V->getOpcode() == ISD::UMUL_LOHI ||
7453 V->getOpcode() == ISD::SMUL_LOHI)
7454 return V;
7455 return SDValue();
7456}
7457
7458static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7459 TargetLowering::DAGCombinerInfo &DCI,
7460 const ARMSubtarget *Subtarget) {
7461
7462 if (Subtarget->isThumb1Only()) return SDValue();
7463
7464 // Only perform the checks after legalize when the pattern is available.
7465 if (DCI.isBeforeLegalize()) return SDValue();
7466
7467 // Look for multiply add opportunities.
7468 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7469 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7470 // a glue link from the first add to the second add.
7471 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7472 // a S/UMLAL instruction.
7473 // loAdd UMUL_LOHI
7474 // \ / :lo \ :hi
7475 // \ / \ [no multiline comment]
7476 // ADDC | hiAdd
7477 // \ :glue / /
7478 // \ / /
7479 // ADDE
7480 //
7481 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7482 SDValue AddcOp0 = AddcNode->getOperand(0);
7483 SDValue AddcOp1 = AddcNode->getOperand(1);
7484
7485 // Check if the two operands are from the same mul_lohi node.
7486 if (AddcOp0.getNode() == AddcOp1.getNode())
7487 return SDValue();
7488
7489 assert(AddcNode->getNumValues() == 2 &&
7490 AddcNode->getValueType(0) == MVT::i32 &&
7491 AddcNode->getValueType(1) == MVT::Glue &&
7492 "Expect ADDC with two result values: i32, glue");
7493
7494 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7495 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7496 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7497 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7498 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7499 return SDValue();
7500
7501 // Look for the glued ADDE.
7502 SDNode* AddeNode = AddcNode->getGluedUser();
7503 if (AddeNode == NULL)
7504 return SDValue();
7505
7506 // Make sure it is really an ADDE.
7507 if (AddeNode->getOpcode() != ISD::ADDE)
7508 return SDValue();
7509
7510 assert(AddeNode->getNumOperands() == 3 &&
7511 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7512 "ADDE node has the wrong inputs");
7513
7514 // Check for the triangle shape.
7515 SDValue AddeOp0 = AddeNode->getOperand(0);
7516 SDValue AddeOp1 = AddeNode->getOperand(1);
7517
7518 // Make sure that the ADDE operands are not coming from the same node.
7519 if (AddeOp0.getNode() == AddeOp1.getNode())
7520 return SDValue();
7521
7522 // Find the MUL_LOHI node walking up ADDE's operands.
7523 bool IsLeftOperandMUL = false;
7524 SDValue MULOp = findMUL_LOHI(AddeOp0);
7525 if (MULOp == SDValue())
7526 MULOp = findMUL_LOHI(AddeOp1);
7527 else
7528 IsLeftOperandMUL = true;
7529 if (MULOp == SDValue())
7530 return SDValue();
7531
7532 // Figure out the right opcode.
7533 unsigned Opc = MULOp->getOpcode();
7534 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7535
7536 // Figure out the high and low input values to the MLAL node.
7537 SDValue* HiMul = &MULOp;
7538 SDValue* HiAdd = NULL;
7539 SDValue* LoMul = NULL;
7540 SDValue* LowAdd = NULL;
7541
7542 if (IsLeftOperandMUL)
7543 HiAdd = &AddeOp1;
7544 else
7545 HiAdd = &AddeOp0;
7546
7547
7548 if (AddcOp0->getOpcode() == Opc) {
7549 LoMul = &AddcOp0;
7550 LowAdd = &AddcOp1;
7551 }
7552 if (AddcOp1->getOpcode() == Opc) {
7553 LoMul = &AddcOp1;
7554 LowAdd = &AddcOp0;
7555 }
7556
7557 if (LoMul == NULL)
7558 return SDValue();
7559
7560 if (LoMul->getNode() != HiMul->getNode())
7561 return SDValue();
7562
7563 // Create the merged node.
7564 SelectionDAG &DAG = DCI.DAG;
7565
7566 // Build operand list.
7567 SmallVector<SDValue, 8> Ops;
7568 Ops.push_back(LoMul->getOperand(0));
7569 Ops.push_back(LoMul->getOperand(1));
7570 Ops.push_back(*LowAdd);
7571 Ops.push_back(*HiAdd);
7572
7573 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7574 DAG.getVTList(MVT::i32, MVT::i32),
7575 &Ops[0], Ops.size());
7576
7577 // Replace the ADDs' nodes uses by the MLA node's values.
7578 SDValue HiMLALResult(MLALNode.getNode(), 1);
7579 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7580
7581 SDValue LoMLALResult(MLALNode.getNode(), 0);
7582 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7583
7584 // Return original node to notify the driver to stop replacing.
7585 SDValue resNode(AddcNode, 0);
7586 return resNode;
7587}
7588
7589/// PerformADDCCombine - Target-specific dag combine transform from
7590/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7591static SDValue PerformADDCCombine(SDNode *N,
7592 TargetLowering::DAGCombinerInfo &DCI,
7593 const ARMSubtarget *Subtarget) {
7594
7595 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7596
7597}
7598
Bob Wilson3d5792a2010-07-29 20:34:14 +00007599/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7600/// operands N0 and N1. This is a helper for PerformADDCombine that is
7601/// called with the default operands, and if that fails, with commuted
7602/// operands.
7603static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007604 TargetLowering::DAGCombinerInfo &DCI,
7605 const ARMSubtarget *Subtarget){
7606
7607 // Attempt to create vpaddl for this add.
7608 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7609 if (Result.getNode())
7610 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007611
Chris Lattnerd1980a52009-03-12 06:52:53 +00007612 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007613 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007614 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7615 if (Result.getNode()) return Result;
7616 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007617 return SDValue();
7618}
7619
Bob Wilson3d5792a2010-07-29 20:34:14 +00007620/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7621///
7622static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007623 TargetLowering::DAGCombinerInfo &DCI,
7624 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007625 SDValue N0 = N->getOperand(0);
7626 SDValue N1 = N->getOperand(1);
7627
7628 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007629 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007630 if (Result.getNode())
7631 return Result;
7632
7633 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007634 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007635}
7636
Chris Lattnerd1980a52009-03-12 06:52:53 +00007637/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007638///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007639static SDValue PerformSUBCombine(SDNode *N,
7640 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007641 SDValue N0 = N->getOperand(0);
7642 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007643
Chris Lattnerd1980a52009-03-12 06:52:53 +00007644 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007645 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007646 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7647 if (Result.getNode()) return Result;
7648 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007649
Chris Lattnerd1980a52009-03-12 06:52:53 +00007650 return SDValue();
7651}
7652
Evan Cheng463d3582011-03-31 19:38:48 +00007653/// PerformVMULCombine
7654/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7655/// special multiplier accumulator forwarding.
7656/// vmul d3, d0, d2
7657/// vmla d3, d1, d2
7658/// is faster than
7659/// vadd d3, d0, d1
7660/// vmul d3, d3, d2
7661static SDValue PerformVMULCombine(SDNode *N,
7662 TargetLowering::DAGCombinerInfo &DCI,
7663 const ARMSubtarget *Subtarget) {
7664 if (!Subtarget->hasVMLxForwarding())
7665 return SDValue();
7666
7667 SelectionDAG &DAG = DCI.DAG;
7668 SDValue N0 = N->getOperand(0);
7669 SDValue N1 = N->getOperand(1);
7670 unsigned Opcode = N0.getOpcode();
7671 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7672 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007673 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007674 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7675 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7676 return SDValue();
7677 std::swap(N0, N1);
7678 }
7679
7680 EVT VT = N->getValueType(0);
7681 DebugLoc DL = N->getDebugLoc();
7682 SDValue N00 = N0->getOperand(0);
7683 SDValue N01 = N0->getOperand(1);
7684 return DAG.getNode(Opcode, DL, VT,
7685 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7686 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7687}
7688
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007689static SDValue PerformMULCombine(SDNode *N,
7690 TargetLowering::DAGCombinerInfo &DCI,
7691 const ARMSubtarget *Subtarget) {
7692 SelectionDAG &DAG = DCI.DAG;
7693
7694 if (Subtarget->isThumb1Only())
7695 return SDValue();
7696
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007697 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7698 return SDValue();
7699
7700 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007701 if (VT.is64BitVector() || VT.is128BitVector())
7702 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007703 if (VT != MVT::i32)
7704 return SDValue();
7705
7706 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7707 if (!C)
7708 return SDValue();
7709
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007710 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007711 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007712
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007713 ShiftAmt = ShiftAmt & (32 - 1);
7714 SDValue V = N->getOperand(0);
7715 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007716
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007717 SDValue Res;
7718 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007719
7720 if (MulAmt >= 0) {
7721 if (isPowerOf2_32(MulAmt - 1)) {
7722 // (mul x, 2^N + 1) => (add (shl x, N), x)
7723 Res = DAG.getNode(ISD::ADD, DL, VT,
7724 V,
7725 DAG.getNode(ISD::SHL, DL, VT,
7726 V,
7727 DAG.getConstant(Log2_32(MulAmt - 1),
7728 MVT::i32)));
7729 } else if (isPowerOf2_32(MulAmt + 1)) {
7730 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7731 Res = DAG.getNode(ISD::SUB, DL, VT,
7732 DAG.getNode(ISD::SHL, DL, VT,
7733 V,
7734 DAG.getConstant(Log2_32(MulAmt + 1),
7735 MVT::i32)),
7736 V);
7737 } else
7738 return SDValue();
7739 } else {
7740 uint64_t MulAmtAbs = -MulAmt;
7741 if (isPowerOf2_32(MulAmtAbs + 1)) {
7742 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7743 Res = DAG.getNode(ISD::SUB, DL, VT,
7744 V,
7745 DAG.getNode(ISD::SHL, DL, VT,
7746 V,
7747 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7748 MVT::i32)));
7749 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7750 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7751 Res = DAG.getNode(ISD::ADD, DL, VT,
7752 V,
7753 DAG.getNode(ISD::SHL, DL, VT,
7754 V,
7755 DAG.getConstant(Log2_32(MulAmtAbs-1),
7756 MVT::i32)));
7757 Res = DAG.getNode(ISD::SUB, DL, VT,
7758 DAG.getConstant(0, MVT::i32),Res);
7759
7760 } else
7761 return SDValue();
7762 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007763
7764 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007765 Res = DAG.getNode(ISD::SHL, DL, VT,
7766 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007767
7768 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007769 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007770 return SDValue();
7771}
7772
Owen Anderson080c0922010-11-05 19:27:46 +00007773static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007774 TargetLowering::DAGCombinerInfo &DCI,
7775 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007776
Owen Anderson080c0922010-11-05 19:27:46 +00007777 // Attempt to use immediate-form VBIC
7778 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7779 DebugLoc dl = N->getDebugLoc();
7780 EVT VT = N->getValueType(0);
7781 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007782
Tanya Lattner0433b212011-04-07 15:24:20 +00007783 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7784 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007785
Owen Anderson080c0922010-11-05 19:27:46 +00007786 APInt SplatBits, SplatUndef;
7787 unsigned SplatBitSize;
7788 bool HasAnyUndefs;
7789 if (BVN &&
7790 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7791 if (SplatBitSize <= 64) {
7792 EVT VbicVT;
7793 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7794 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007795 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007796 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007797 if (Val.getNode()) {
7798 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007799 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007800 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007801 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007802 }
7803 }
7804 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007805
Evan Chengc892aeb2012-02-23 01:19:06 +00007806 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007807 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7808 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7809 if (Result.getNode())
7810 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007811 }
7812
Owen Anderson080c0922010-11-05 19:27:46 +00007813 return SDValue();
7814}
7815
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007816/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7817static SDValue PerformORCombine(SDNode *N,
7818 TargetLowering::DAGCombinerInfo &DCI,
7819 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007820 // Attempt to use immediate-form VORR
7821 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7822 DebugLoc dl = N->getDebugLoc();
7823 EVT VT = N->getValueType(0);
7824 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007825
Tanya Lattner0433b212011-04-07 15:24:20 +00007826 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7827 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007828
Owen Anderson60f48702010-11-03 23:15:26 +00007829 APInt SplatBits, SplatUndef;
7830 unsigned SplatBitSize;
7831 bool HasAnyUndefs;
7832 if (BVN && Subtarget->hasNEON() &&
7833 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7834 if (SplatBitSize <= 64) {
7835 EVT VorrVT;
7836 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7837 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007838 DAG, VorrVT, VT.is128BitVector(),
7839 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007840 if (Val.getNode()) {
7841 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007842 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007843 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007844 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007845 }
7846 }
7847 }
7848
Evan Chengc892aeb2012-02-23 01:19:06 +00007849 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007850 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7851 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7852 if (Result.getNode())
7853 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007854 }
7855
Nadav Rotemdf832032012-08-13 18:52:44 +00007856 // The code below optimizes (or (and X, Y), Z).
7857 // The AND operand needs to have a single user to make these optimizations
7858 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007859 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00007860 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007861 return SDValue();
7862 SDValue N1 = N->getOperand(1);
7863
7864 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7865 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7866 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7867 APInt SplatUndef;
7868 unsigned SplatBitSize;
7869 bool HasAnyUndefs;
7870
7871 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7872 APInt SplatBits0;
7873 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7874 HasAnyUndefs) && !HasAnyUndefs) {
7875 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7876 APInt SplatBits1;
7877 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7878 HasAnyUndefs) && !HasAnyUndefs &&
7879 SplatBits0 == ~SplatBits1) {
7880 // Canonicalize the vector type to make instruction selection simpler.
7881 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7882 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7883 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007884 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007885 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7886 }
7887 }
7888 }
7889
Jim Grosbach54238562010-07-17 03:30:54 +00007890 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7891 // reasonable.
7892
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007893 // BFI is only available on V6T2+
7894 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7895 return SDValue();
7896
Jim Grosbach54238562010-07-17 03:30:54 +00007897 DebugLoc DL = N->getDebugLoc();
7898 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00007899 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00007900 //
7901 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00007902 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007903 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00007904 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007905 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007906 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007907
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007908 if (VT != MVT::i32)
7909 return SDValue();
7910
Evan Cheng30fb13f2010-12-13 20:32:54 +00007911 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007912
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007913 // The value and the mask need to be constants so we can verify this is
7914 // actually a bitfield set. If the mask is 0xffff, we can do better
7915 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007916 SDValue MaskOp = N0.getOperand(1);
7917 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7918 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007919 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007920 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007921 if (Mask == 0xffff)
7922 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007923 SDValue Res;
7924 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7926 if (N1C) {
7927 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007928 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007929 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007930
Evan Chenga9688c42010-12-11 04:11:38 +00007931 if (ARM::isBitFieldInvertedMask(Mask)) {
7932 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007933
Evan Cheng30fb13f2010-12-13 20:32:54 +00007934 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007935 DAG.getConstant(Val, MVT::i32),
7936 DAG.getConstant(Mask, MVT::i32));
7937
7938 // Do not add new nodes to DAG combiner worklist.
7939 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007940 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007941 }
Jim Grosbach54238562010-07-17 03:30:54 +00007942 } else if (N1.getOpcode() == ISD::AND) {
7943 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007944 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7945 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007946 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007947 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007948
Eric Christopher29aeed12011-03-26 01:21:03 +00007949 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7950 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007951 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007952 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007953 // The pack halfword instruction works better for masks that fit it,
7954 // so use that when it's available.
7955 if (Subtarget->hasT2ExtractPack() &&
7956 (Mask == 0xffff || Mask == 0xffff0000))
7957 return SDValue();
7958 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007959 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007960 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007961 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007962 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007963 DAG.getConstant(Mask, MVT::i32));
7964 // Do not add new nodes to DAG combiner worklist.
7965 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007966 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007967 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007968 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007969 // The pack halfword instruction works better for masks that fit it,
7970 // so use that when it's available.
7971 if (Subtarget->hasT2ExtractPack() &&
7972 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7973 return SDValue();
7974 // 2b
7975 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007976 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007977 DAG.getConstant(lsb, MVT::i32));
7978 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007979 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007980 // Do not add new nodes to DAG combiner worklist.
7981 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007982 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007983 }
7984 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007985
Evan Cheng30fb13f2010-12-13 20:32:54 +00007986 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7987 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7988 ARM::isBitFieldInvertedMask(~Mask)) {
7989 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7990 // where lsb(mask) == #shamt and masked bits of B are known zero.
7991 SDValue ShAmt = N00.getOperand(1);
7992 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7993 unsigned LSB = CountTrailingZeros_32(Mask);
7994 if (ShAmtC != LSB)
7995 return SDValue();
7996
7997 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7998 DAG.getConstant(~Mask, MVT::i32));
7999
8000 // Do not add new nodes to DAG combiner worklist.
8001 DCI.CombineTo(N, Res, false);
8002 }
8003
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008004 return SDValue();
8005}
8006
Evan Chengc892aeb2012-02-23 01:19:06 +00008007static SDValue PerformXORCombine(SDNode *N,
8008 TargetLowering::DAGCombinerInfo &DCI,
8009 const ARMSubtarget *Subtarget) {
8010 EVT VT = N->getValueType(0);
8011 SelectionDAG &DAG = DCI.DAG;
8012
8013 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8014 return SDValue();
8015
8016 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008017 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8018 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8019 if (Result.getNode())
8020 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008021 }
8022
8023 return SDValue();
8024}
8025
Evan Chengbf188ae2011-06-15 01:12:31 +00008026/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8027/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008028static SDValue PerformBFICombine(SDNode *N,
8029 TargetLowering::DAGCombinerInfo &DCI) {
8030 SDValue N1 = N->getOperand(1);
8031 if (N1.getOpcode() == ISD::AND) {
8032 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8033 if (!N11C)
8034 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008035 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8036 unsigned LSB = CountTrailingZeros_32(~InvMask);
8037 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8038 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008039 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008040 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008041 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8042 N->getOperand(0), N1.getOperand(0),
8043 N->getOperand(2));
8044 }
8045 return SDValue();
8046}
8047
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008048/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8049/// ARMISD::VMOVRRD.
8050static SDValue PerformVMOVRRDCombine(SDNode *N,
8051 TargetLowering::DAGCombinerInfo &DCI) {
8052 // vmovrrd(vmovdrr x, y) -> x,y
8053 SDValue InDouble = N->getOperand(0);
8054 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8055 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008056
8057 // vmovrrd(load f64) -> (load i32), (load i32)
8058 SDNode *InNode = InDouble.getNode();
8059 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8060 InNode->getValueType(0) == MVT::f64 &&
8061 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8062 !cast<LoadSDNode>(InNode)->isVolatile()) {
8063 // TODO: Should this be done for non-FrameIndex operands?
8064 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8065
8066 SelectionDAG &DAG = DCI.DAG;
8067 DebugLoc DL = LD->getDebugLoc();
8068 SDValue BasePtr = LD->getBasePtr();
8069 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8070 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008071 LD->isNonTemporal(), LD->isInvariant(),
8072 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008073
8074 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8075 DAG.getConstant(4, MVT::i32));
8076 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8077 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008078 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008079 std::min(4U, LD->getAlignment() / 2));
8080
8081 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8082 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8083 DCI.RemoveFromWorklist(LD);
8084 DAG.DeleteNode(LD);
8085 return Result;
8086 }
8087
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008088 return SDValue();
8089}
8090
8091/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8092/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8093static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8094 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8095 SDValue Op0 = N->getOperand(0);
8096 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008097 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008098 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008099 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008100 Op1 = Op1.getOperand(0);
8101 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8102 Op0.getNode() == Op1.getNode() &&
8103 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008104 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008105 N->getValueType(0), Op0.getOperand(0));
8106 return SDValue();
8107}
8108
Bob Wilson31600902010-12-21 06:43:19 +00008109/// PerformSTORECombine - Target-specific dag combine xforms for
8110/// ISD::STORE.
8111static SDValue PerformSTORECombine(SDNode *N,
8112 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008113 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008114 if (St->isVolatile())
8115 return SDValue();
8116
Andrew Trick49b446f2012-07-18 18:34:24 +00008117 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008118 // pack all of the elements in one place. Next, store to memory in fewer
8119 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008120 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008121 EVT VT = StVal.getValueType();
8122 if (St->isTruncatingStore() && VT.isVector()) {
8123 SelectionDAG &DAG = DCI.DAG;
8124 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8125 EVT StVT = St->getMemoryVT();
8126 unsigned NumElems = VT.getVectorNumElements();
8127 assert(StVT != VT && "Cannot truncate to the same type");
8128 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8129 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8130
8131 // From, To sizes and ElemCount must be pow of two
8132 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8133
8134 // We are going to use the original vector elt for storing.
8135 // Accumulated smaller vector elements must be a multiple of the store size.
8136 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8137
8138 unsigned SizeRatio = FromEltSz / ToEltSz;
8139 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8140
8141 // Create a type on which we perform the shuffle.
8142 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8143 NumElems*SizeRatio);
8144 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8145
8146 DebugLoc DL = St->getDebugLoc();
8147 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8148 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8149 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8150
8151 // Can't shuffle using an illegal type.
8152 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8153
8154 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8155 DAG.getUNDEF(WideVec.getValueType()),
8156 ShuffleVec.data());
8157 // At this point all of the data is stored at the bottom of the
8158 // register. We now need to save it to mem.
8159
8160 // Find the largest store unit
8161 MVT StoreType = MVT::i8;
8162 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8163 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8164 MVT Tp = (MVT::SimpleValueType)tp;
8165 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8166 StoreType = Tp;
8167 }
8168 // Didn't find a legal store type.
8169 if (!TLI.isTypeLegal(StoreType))
8170 return SDValue();
8171
8172 // Bitcast the original vector into a vector of store-size units
8173 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8174 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8175 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8176 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8177 SmallVector<SDValue, 8> Chains;
8178 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8179 TLI.getPointerTy());
8180 SDValue BasePtr = St->getBasePtr();
8181
8182 // Perform one or more big stores into memory.
8183 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8184 for (unsigned I = 0; I < E; I++) {
8185 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8186 StoreType, ShuffWide,
8187 DAG.getIntPtrConstant(I));
8188 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8189 St->getPointerInfo(), St->isVolatile(),
8190 St->isNonTemporal(), St->getAlignment());
8191 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8192 Increment);
8193 Chains.push_back(Ch);
8194 }
8195 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8196 Chains.size());
8197 }
8198
8199 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008200 return SDValue();
8201
Chad Rosier96b66d62012-04-09 19:38:15 +00008202 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8203 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008204 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008205 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008206 SelectionDAG &DAG = DCI.DAG;
8207 DebugLoc DL = St->getDebugLoc();
8208 SDValue BasePtr = St->getBasePtr();
8209 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8210 StVal.getNode()->getOperand(0), BasePtr,
8211 St->getPointerInfo(), St->isVolatile(),
8212 St->isNonTemporal(), St->getAlignment());
8213
8214 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8215 DAG.getConstant(4, MVT::i32));
8216 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8217 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8218 St->isNonTemporal(),
8219 std::min(4U, St->getAlignment() / 2));
8220 }
8221
8222 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008223 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8224 return SDValue();
8225
Chad Rosier96b66d62012-04-09 19:38:15 +00008226 // Bitcast an i64 store extracted from a vector to f64.
8227 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008228 SelectionDAG &DAG = DCI.DAG;
8229 DebugLoc dl = StVal.getDebugLoc();
8230 SDValue IntVec = StVal.getOperand(0);
8231 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8232 IntVec.getValueType().getVectorNumElements());
8233 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8234 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8235 Vec, StVal.getOperand(1));
8236 dl = N->getDebugLoc();
8237 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8238 // Make the DAGCombiner fold the bitcasts.
8239 DCI.AddToWorklist(Vec.getNode());
8240 DCI.AddToWorklist(ExtElt.getNode());
8241 DCI.AddToWorklist(V.getNode());
8242 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8243 St->getPointerInfo(), St->isVolatile(),
8244 St->isNonTemporal(), St->getAlignment(),
8245 St->getTBAAInfo());
8246}
8247
8248/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8249/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8250/// i64 vector to have f64 elements, since the value can then be loaded
8251/// directly into a VFP register.
8252static bool hasNormalLoadOperand(SDNode *N) {
8253 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8254 for (unsigned i = 0; i < NumElts; ++i) {
8255 SDNode *Elt = N->getOperand(i).getNode();
8256 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8257 return true;
8258 }
8259 return false;
8260}
8261
Bob Wilson75f02882010-09-17 22:59:05 +00008262/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8263/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008264static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8265 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008266 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8267 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8268 // into a pair of GPRs, which is fine when the value is used as a scalar,
8269 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008270 SelectionDAG &DAG = DCI.DAG;
8271 if (N->getNumOperands() == 2) {
8272 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8273 if (RV.getNode())
8274 return RV;
8275 }
Bob Wilson75f02882010-09-17 22:59:05 +00008276
Bob Wilson31600902010-12-21 06:43:19 +00008277 // Load i64 elements as f64 values so that type legalization does not split
8278 // them up into i32 values.
8279 EVT VT = N->getValueType(0);
8280 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8281 return SDValue();
8282 DebugLoc dl = N->getDebugLoc();
8283 SmallVector<SDValue, 8> Ops;
8284 unsigned NumElts = VT.getVectorNumElements();
8285 for (unsigned i = 0; i < NumElts; ++i) {
8286 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8287 Ops.push_back(V);
8288 // Make the DAGCombiner fold the bitcast.
8289 DCI.AddToWorklist(V.getNode());
8290 }
8291 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8292 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8293 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8294}
8295
8296/// PerformInsertEltCombine - Target-specific dag combine xforms for
8297/// ISD::INSERT_VECTOR_ELT.
8298static SDValue PerformInsertEltCombine(SDNode *N,
8299 TargetLowering::DAGCombinerInfo &DCI) {
8300 // Bitcast an i64 load inserted into a vector to f64.
8301 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8302 EVT VT = N->getValueType(0);
8303 SDNode *Elt = N->getOperand(1).getNode();
8304 if (VT.getVectorElementType() != MVT::i64 ||
8305 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8306 return SDValue();
8307
8308 SelectionDAG &DAG = DCI.DAG;
8309 DebugLoc dl = N->getDebugLoc();
8310 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8311 VT.getVectorNumElements());
8312 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8313 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8314 // Make the DAGCombiner fold the bitcasts.
8315 DCI.AddToWorklist(Vec.getNode());
8316 DCI.AddToWorklist(V.getNode());
8317 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8318 Vec, V, N->getOperand(2));
8319 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008320}
8321
Bob Wilsonf20700c2010-10-27 20:38:28 +00008322/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8323/// ISD::VECTOR_SHUFFLE.
8324static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8325 // The LLVM shufflevector instruction does not require the shuffle mask
8326 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8327 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8328 // operands do not match the mask length, they are extended by concatenating
8329 // them with undef vectors. That is probably the right thing for other
8330 // targets, but for NEON it is better to concatenate two double-register
8331 // size vector operands into a single quad-register size vector. Do that
8332 // transformation here:
8333 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8334 // shuffle(concat(v1, v2), undef)
8335 SDValue Op0 = N->getOperand(0);
8336 SDValue Op1 = N->getOperand(1);
8337 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8338 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8339 Op0.getNumOperands() != 2 ||
8340 Op1.getNumOperands() != 2)
8341 return SDValue();
8342 SDValue Concat0Op1 = Op0.getOperand(1);
8343 SDValue Concat1Op1 = Op1.getOperand(1);
8344 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8345 Concat1Op1.getOpcode() != ISD::UNDEF)
8346 return SDValue();
8347 // Skip the transformation if any of the types are illegal.
8348 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8349 EVT VT = N->getValueType(0);
8350 if (!TLI.isTypeLegal(VT) ||
8351 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8352 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8353 return SDValue();
8354
8355 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8356 Op0.getOperand(0), Op1.getOperand(0));
8357 // Translate the shuffle mask.
8358 SmallVector<int, 16> NewMask;
8359 unsigned NumElts = VT.getVectorNumElements();
8360 unsigned HalfElts = NumElts/2;
8361 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8362 for (unsigned n = 0; n < NumElts; ++n) {
8363 int MaskElt = SVN->getMaskElt(n);
8364 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008365 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008366 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008367 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008368 NewElt = HalfElts + MaskElt - NumElts;
8369 NewMask.push_back(NewElt);
8370 }
8371 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8372 DAG.getUNDEF(VT), NewMask.data());
8373}
8374
Bob Wilson1c3ef902011-02-07 17:43:21 +00008375/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8376/// NEON load/store intrinsics to merge base address updates.
8377static SDValue CombineBaseUpdate(SDNode *N,
8378 TargetLowering::DAGCombinerInfo &DCI) {
8379 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8380 return SDValue();
8381
8382 SelectionDAG &DAG = DCI.DAG;
8383 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8384 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8385 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8386 SDValue Addr = N->getOperand(AddrOpIdx);
8387
8388 // Search for a use of the address operand that is an increment.
8389 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8390 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8391 SDNode *User = *UI;
8392 if (User->getOpcode() != ISD::ADD ||
8393 UI.getUse().getResNo() != Addr.getResNo())
8394 continue;
8395
8396 // Check that the add is independent of the load/store. Otherwise, folding
8397 // it would create a cycle.
8398 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8399 continue;
8400
8401 // Find the new opcode for the updating load/store.
8402 bool isLoad = true;
8403 bool isLaneOp = false;
8404 unsigned NewOpc = 0;
8405 unsigned NumVecs = 0;
8406 if (isIntrinsic) {
8407 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8408 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008409 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008410 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8411 NumVecs = 1; break;
8412 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8413 NumVecs = 2; break;
8414 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8415 NumVecs = 3; break;
8416 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8417 NumVecs = 4; break;
8418 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8419 NumVecs = 2; isLaneOp = true; break;
8420 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8421 NumVecs = 3; isLaneOp = true; break;
8422 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8423 NumVecs = 4; isLaneOp = true; break;
8424 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8425 NumVecs = 1; isLoad = false; break;
8426 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8427 NumVecs = 2; isLoad = false; break;
8428 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8429 NumVecs = 3; isLoad = false; break;
8430 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8431 NumVecs = 4; isLoad = false; break;
8432 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8433 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8434 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8435 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8436 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8437 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8438 }
8439 } else {
8440 isLaneOp = true;
8441 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008442 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008443 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8444 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8445 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8446 }
8447 }
8448
8449 // Find the size of memory referenced by the load/store.
8450 EVT VecTy;
8451 if (isLoad)
8452 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008453 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008454 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8455 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8456 if (isLaneOp)
8457 NumBytes /= VecTy.getVectorNumElements();
8458
8459 // If the increment is a constant, it must match the memory ref size.
8460 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8461 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8462 uint64_t IncVal = CInc->getZExtValue();
8463 if (IncVal != NumBytes)
8464 continue;
8465 } else if (NumBytes >= 3 * 16) {
8466 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8467 // separate instructions that make it harder to use a non-constant update.
8468 continue;
8469 }
8470
8471 // Create the new updating load/store node.
8472 EVT Tys[6];
8473 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8474 unsigned n;
8475 for (n = 0; n < NumResultVecs; ++n)
8476 Tys[n] = VecTy;
8477 Tys[n++] = MVT::i32;
8478 Tys[n] = MVT::Other;
8479 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8480 SmallVector<SDValue, 8> Ops;
8481 Ops.push_back(N->getOperand(0)); // incoming chain
8482 Ops.push_back(N->getOperand(AddrOpIdx));
8483 Ops.push_back(Inc);
8484 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8485 Ops.push_back(N->getOperand(i));
8486 }
8487 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8488 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8489 Ops.data(), Ops.size(),
8490 MemInt->getMemoryVT(),
8491 MemInt->getMemOperand());
8492
8493 // Update the uses.
8494 std::vector<SDValue> NewResults;
8495 for (unsigned i = 0; i < NumResultVecs; ++i) {
8496 NewResults.push_back(SDValue(UpdN.getNode(), i));
8497 }
8498 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8499 DCI.CombineTo(N, NewResults);
8500 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8501
8502 break;
Owen Anderson76706012011-04-05 21:48:57 +00008503 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008504 return SDValue();
8505}
8506
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008507/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8508/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8509/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8510/// return true.
8511static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8512 SelectionDAG &DAG = DCI.DAG;
8513 EVT VT = N->getValueType(0);
8514 // vldN-dup instructions only support 64-bit vectors for N > 1.
8515 if (!VT.is64BitVector())
8516 return false;
8517
8518 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8519 SDNode *VLD = N->getOperand(0).getNode();
8520 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8521 return false;
8522 unsigned NumVecs = 0;
8523 unsigned NewOpc = 0;
8524 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8525 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8526 NumVecs = 2;
8527 NewOpc = ARMISD::VLD2DUP;
8528 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8529 NumVecs = 3;
8530 NewOpc = ARMISD::VLD3DUP;
8531 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8532 NumVecs = 4;
8533 NewOpc = ARMISD::VLD4DUP;
8534 } else {
8535 return false;
8536 }
8537
8538 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8539 // numbers match the load.
8540 unsigned VLDLaneNo =
8541 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8542 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8543 UI != UE; ++UI) {
8544 // Ignore uses of the chain result.
8545 if (UI.getUse().getResNo() == NumVecs)
8546 continue;
8547 SDNode *User = *UI;
8548 if (User->getOpcode() != ARMISD::VDUPLANE ||
8549 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8550 return false;
8551 }
8552
8553 // Create the vldN-dup node.
8554 EVT Tys[5];
8555 unsigned n;
8556 for (n = 0; n < NumVecs; ++n)
8557 Tys[n] = VT;
8558 Tys[n] = MVT::Other;
8559 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8560 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8561 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8562 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8563 Ops, 2, VLDMemInt->getMemoryVT(),
8564 VLDMemInt->getMemOperand());
8565
8566 // Update the uses.
8567 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8568 UI != UE; ++UI) {
8569 unsigned ResNo = UI.getUse().getResNo();
8570 // Ignore uses of the chain result.
8571 if (ResNo == NumVecs)
8572 continue;
8573 SDNode *User = *UI;
8574 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8575 }
8576
8577 // Now the vldN-lane intrinsic is dead except for its chain result.
8578 // Update uses of the chain.
8579 std::vector<SDValue> VLDDupResults;
8580 for (unsigned n = 0; n < NumVecs; ++n)
8581 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8582 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8583 DCI.CombineTo(VLD, VLDDupResults);
8584
8585 return true;
8586}
8587
Bob Wilson9e82bf12010-07-14 01:22:12 +00008588/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8589/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008590static SDValue PerformVDUPLANECombine(SDNode *N,
8591 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008592 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008593
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008594 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8595 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8596 if (CombineVLDDUP(N, DCI))
8597 return SDValue(N, 0);
8598
8599 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8600 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008601 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008602 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008603 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008604 return SDValue();
8605
8606 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8607 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8608 // The canonical VMOV for a zero vector uses a 32-bit element size.
8609 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8610 unsigned EltBits;
8611 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8612 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008613 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008614 if (EltSize > VT.getVectorElementType().getSizeInBits())
8615 return SDValue();
8616
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008617 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008618}
8619
Eric Christopherfa6f5912011-06-29 21:10:36 +00008620// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008621// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8622static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8623{
Chad Rosier118c9a02011-06-28 17:26:57 +00008624 integerPart cN;
8625 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008626 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8627 I != E; I++) {
8628 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8629 if (!C)
8630 return false;
8631
Eric Christopherfa6f5912011-06-29 21:10:36 +00008632 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008633 APFloat APF = C->getValueAPF();
8634 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8635 != APFloat::opOK || !isExact)
8636 return false;
8637
8638 c0 = (I == 0) ? cN : c0;
8639 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8640 return false;
8641 }
8642 C = c0;
8643 return true;
8644}
8645
8646/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8647/// can replace combinations of VMUL and VCVT (floating-point to integer)
8648/// when the VMUL has a constant operand that is a power of 2.
8649///
8650/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8651/// vmul.f32 d16, d17, d16
8652/// vcvt.s32.f32 d16, d16
8653/// becomes:
8654/// vcvt.s32.f32 d16, d16, #3
8655static SDValue PerformVCVTCombine(SDNode *N,
8656 TargetLowering::DAGCombinerInfo &DCI,
8657 const ARMSubtarget *Subtarget) {
8658 SelectionDAG &DAG = DCI.DAG;
8659 SDValue Op = N->getOperand(0);
8660
8661 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8662 Op.getOpcode() != ISD::FMUL)
8663 return SDValue();
8664
8665 uint64_t C;
8666 SDValue N0 = Op->getOperand(0);
8667 SDValue ConstVec = Op->getOperand(1);
8668 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8669
Eric Christopherfa6f5912011-06-29 21:10:36 +00008670 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008671 !isConstVecPow2(ConstVec, isSigned, C))
8672 return SDValue();
8673
8674 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8675 Intrinsic::arm_neon_vcvtfp2fxu;
8676 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8677 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008678 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008679 DAG.getConstant(Log2_64(C), MVT::i32));
8680}
8681
8682/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8683/// can replace combinations of VCVT (integer to floating-point) and VDIV
8684/// when the VDIV has a constant operand that is a power of 2.
8685///
8686/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8687/// vcvt.f32.s32 d16, d16
8688/// vdiv.f32 d16, d17, d16
8689/// becomes:
8690/// vcvt.f32.s32 d16, d16, #3
8691static SDValue PerformVDIVCombine(SDNode *N,
8692 TargetLowering::DAGCombinerInfo &DCI,
8693 const ARMSubtarget *Subtarget) {
8694 SelectionDAG &DAG = DCI.DAG;
8695 SDValue Op = N->getOperand(0);
8696 unsigned OpOpcode = Op.getNode()->getOpcode();
8697
8698 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8699 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8700 return SDValue();
8701
8702 uint64_t C;
8703 SDValue ConstVec = N->getOperand(1);
8704 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8705
8706 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8707 !isConstVecPow2(ConstVec, isSigned, C))
8708 return SDValue();
8709
Eric Christopherfa6f5912011-06-29 21:10:36 +00008710 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008711 Intrinsic::arm_neon_vcvtfxu2fp;
8712 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8713 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008714 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008715 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8716}
8717
8718/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008719/// operand of a vector shift operation, where all the elements of the
8720/// build_vector must have the same constant integer value.
8721static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8722 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008723 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008724 Op = Op.getOperand(0);
8725 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8726 APInt SplatBits, SplatUndef;
8727 unsigned SplatBitSize;
8728 bool HasAnyUndefs;
8729 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8730 HasAnyUndefs, ElementBits) ||
8731 SplatBitSize > ElementBits)
8732 return false;
8733 Cnt = SplatBits.getSExtValue();
8734 return true;
8735}
8736
8737/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8738/// operand of a vector shift left operation. That value must be in the range:
8739/// 0 <= Value < ElementBits for a left shift; or
8740/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008741static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008742 assert(VT.isVector() && "vector shift count is not a vector type");
8743 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8744 if (! getVShiftImm(Op, ElementBits, Cnt))
8745 return false;
8746 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8747}
8748
8749/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8750/// operand of a vector shift right operation. For a shift opcode, the value
8751/// is positive, but for an intrinsic the value count must be negative. The
8752/// absolute value must be in the range:
8753/// 1 <= |Value| <= ElementBits for a right shift; or
8754/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008755static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008756 int64_t &Cnt) {
8757 assert(VT.isVector() && "vector shift count is not a vector type");
8758 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8759 if (! getVShiftImm(Op, ElementBits, Cnt))
8760 return false;
8761 if (isIntrinsic)
8762 Cnt = -Cnt;
8763 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8764}
8765
8766/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8767static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8768 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8769 switch (IntNo) {
8770 default:
8771 // Don't do anything for most intrinsics.
8772 break;
8773
8774 // Vector shifts: check for immediate versions and lower them.
8775 // Note: This is done during DAG combining instead of DAG legalizing because
8776 // the build_vectors for 64-bit vector element shift counts are generally
8777 // not legal, and it is hard to see their values after they get legalized to
8778 // loads from a constant pool.
8779 case Intrinsic::arm_neon_vshifts:
8780 case Intrinsic::arm_neon_vshiftu:
8781 case Intrinsic::arm_neon_vshiftls:
8782 case Intrinsic::arm_neon_vshiftlu:
8783 case Intrinsic::arm_neon_vshiftn:
8784 case Intrinsic::arm_neon_vrshifts:
8785 case Intrinsic::arm_neon_vrshiftu:
8786 case Intrinsic::arm_neon_vrshiftn:
8787 case Intrinsic::arm_neon_vqshifts:
8788 case Intrinsic::arm_neon_vqshiftu:
8789 case Intrinsic::arm_neon_vqshiftsu:
8790 case Intrinsic::arm_neon_vqshiftns:
8791 case Intrinsic::arm_neon_vqshiftnu:
8792 case Intrinsic::arm_neon_vqshiftnsu:
8793 case Intrinsic::arm_neon_vqrshiftns:
8794 case Intrinsic::arm_neon_vqrshiftnu:
8795 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008796 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008797 int64_t Cnt;
8798 unsigned VShiftOpc = 0;
8799
8800 switch (IntNo) {
8801 case Intrinsic::arm_neon_vshifts:
8802 case Intrinsic::arm_neon_vshiftu:
8803 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8804 VShiftOpc = ARMISD::VSHL;
8805 break;
8806 }
8807 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8808 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8809 ARMISD::VSHRs : ARMISD::VSHRu);
8810 break;
8811 }
8812 return SDValue();
8813
8814 case Intrinsic::arm_neon_vshiftls:
8815 case Intrinsic::arm_neon_vshiftlu:
8816 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8817 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008818 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008819
8820 case Intrinsic::arm_neon_vrshifts:
8821 case Intrinsic::arm_neon_vrshiftu:
8822 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8823 break;
8824 return SDValue();
8825
8826 case Intrinsic::arm_neon_vqshifts:
8827 case Intrinsic::arm_neon_vqshiftu:
8828 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8829 break;
8830 return SDValue();
8831
8832 case Intrinsic::arm_neon_vqshiftsu:
8833 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8834 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008835 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008836
8837 case Intrinsic::arm_neon_vshiftn:
8838 case Intrinsic::arm_neon_vrshiftn:
8839 case Intrinsic::arm_neon_vqshiftns:
8840 case Intrinsic::arm_neon_vqshiftnu:
8841 case Intrinsic::arm_neon_vqshiftnsu:
8842 case Intrinsic::arm_neon_vqrshiftns:
8843 case Intrinsic::arm_neon_vqrshiftnu:
8844 case Intrinsic::arm_neon_vqrshiftnsu:
8845 // Narrowing shifts require an immediate right shift.
8846 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8847 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008848 llvm_unreachable("invalid shift count for narrowing vector shift "
8849 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008850
8851 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008852 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008853 }
8854
8855 switch (IntNo) {
8856 case Intrinsic::arm_neon_vshifts:
8857 case Intrinsic::arm_neon_vshiftu:
8858 // Opcode already set above.
8859 break;
8860 case Intrinsic::arm_neon_vshiftls:
8861 case Intrinsic::arm_neon_vshiftlu:
8862 if (Cnt == VT.getVectorElementType().getSizeInBits())
8863 VShiftOpc = ARMISD::VSHLLi;
8864 else
8865 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8866 ARMISD::VSHLLs : ARMISD::VSHLLu);
8867 break;
8868 case Intrinsic::arm_neon_vshiftn:
8869 VShiftOpc = ARMISD::VSHRN; break;
8870 case Intrinsic::arm_neon_vrshifts:
8871 VShiftOpc = ARMISD::VRSHRs; break;
8872 case Intrinsic::arm_neon_vrshiftu:
8873 VShiftOpc = ARMISD::VRSHRu; break;
8874 case Intrinsic::arm_neon_vrshiftn:
8875 VShiftOpc = ARMISD::VRSHRN; break;
8876 case Intrinsic::arm_neon_vqshifts:
8877 VShiftOpc = ARMISD::VQSHLs; break;
8878 case Intrinsic::arm_neon_vqshiftu:
8879 VShiftOpc = ARMISD::VQSHLu; break;
8880 case Intrinsic::arm_neon_vqshiftsu:
8881 VShiftOpc = ARMISD::VQSHLsu; break;
8882 case Intrinsic::arm_neon_vqshiftns:
8883 VShiftOpc = ARMISD::VQSHRNs; break;
8884 case Intrinsic::arm_neon_vqshiftnu:
8885 VShiftOpc = ARMISD::VQSHRNu; break;
8886 case Intrinsic::arm_neon_vqshiftnsu:
8887 VShiftOpc = ARMISD::VQSHRNsu; break;
8888 case Intrinsic::arm_neon_vqrshiftns:
8889 VShiftOpc = ARMISD::VQRSHRNs; break;
8890 case Intrinsic::arm_neon_vqrshiftnu:
8891 VShiftOpc = ARMISD::VQRSHRNu; break;
8892 case Intrinsic::arm_neon_vqrshiftnsu:
8893 VShiftOpc = ARMISD::VQRSHRNsu; break;
8894 }
8895
8896 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008897 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008898 }
8899
8900 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008901 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008902 int64_t Cnt;
8903 unsigned VShiftOpc = 0;
8904
8905 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8906 VShiftOpc = ARMISD::VSLI;
8907 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8908 VShiftOpc = ARMISD::VSRI;
8909 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008910 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008911 }
8912
8913 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8914 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008915 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008916 }
8917
8918 case Intrinsic::arm_neon_vqrshifts:
8919 case Intrinsic::arm_neon_vqrshiftu:
8920 // No immediate versions of these to check for.
8921 break;
8922 }
8923
8924 return SDValue();
8925}
8926
8927/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8928/// lowers them. As with the vector shift intrinsics, this is done during DAG
8929/// combining instead of DAG legalizing because the build_vectors for 64-bit
8930/// vector element shift counts are generally not legal, and it is hard to see
8931/// their values after they get legalized to loads from a constant pool.
8932static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8933 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008934 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008935 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8936 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8937 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8938 SDValue N1 = N->getOperand(1);
8939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8940 SDValue N0 = N->getOperand(0);
8941 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8942 DAG.MaskedValueIsZero(N0.getOperand(0),
8943 APInt::getHighBitsSet(32, 16)))
8944 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8945 }
8946 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008947
8948 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008949 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8950 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008951 return SDValue();
8952
8953 assert(ST->hasNEON() && "unexpected vector shift");
8954 int64_t Cnt;
8955
8956 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008957 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008958
8959 case ISD::SHL:
8960 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8961 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008962 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008963 break;
8964
8965 case ISD::SRA:
8966 case ISD::SRL:
8967 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8968 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8969 ARMISD::VSHRs : ARMISD::VSHRu);
8970 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008972 }
8973 }
8974 return SDValue();
8975}
8976
8977/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8978/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8979static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8980 const ARMSubtarget *ST) {
8981 SDValue N0 = N->getOperand(0);
8982
8983 // Check for sign- and zero-extensions of vector extract operations of 8-
8984 // and 16-bit vector elements. NEON supports these directly. They are
8985 // handled during DAG combining because type legalization will promote them
8986 // to 32-bit types and it is messy to recognize the operations after that.
8987 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8988 SDValue Vec = N0.getOperand(0);
8989 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008990 EVT VT = N->getValueType(0);
8991 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008992 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8993
Owen Anderson825b72b2009-08-11 20:47:22 +00008994 if (VT == MVT::i32 &&
8995 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008996 TLI.isTypeLegal(Vec.getValueType()) &&
8997 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008998
8999 unsigned Opc = 0;
9000 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009001 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009002 case ISD::SIGN_EXTEND:
9003 Opc = ARMISD::VGETLANEs;
9004 break;
9005 case ISD::ZERO_EXTEND:
9006 case ISD::ANY_EXTEND:
9007 Opc = ARMISD::VGETLANEu;
9008 break;
9009 }
9010 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9011 }
9012 }
9013
9014 return SDValue();
9015}
9016
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009017/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9018/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9019static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9020 const ARMSubtarget *ST) {
9021 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009022 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009023 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9024 // a NaN; only do the transformation when it matches that behavior.
9025
9026 // For now only do this when using NEON for FP operations; if using VFP, it
9027 // is not obvious that the benefit outweighs the cost of switching to the
9028 // NEON pipeline.
9029 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9030 N->getValueType(0) != MVT::f32)
9031 return SDValue();
9032
9033 SDValue CondLHS = N->getOperand(0);
9034 SDValue CondRHS = N->getOperand(1);
9035 SDValue LHS = N->getOperand(2);
9036 SDValue RHS = N->getOperand(3);
9037 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9038
9039 unsigned Opcode = 0;
9040 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009041 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009042 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009043 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009044 IsReversed = true ; // x CC y ? y : x
9045 } else {
9046 return SDValue();
9047 }
9048
Bob Wilsone742bb52010-02-24 22:15:53 +00009049 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009050 switch (CC) {
9051 default: break;
9052 case ISD::SETOLT:
9053 case ISD::SETOLE:
9054 case ISD::SETLT:
9055 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009056 case ISD::SETULT:
9057 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009058 // If LHS is NaN, an ordered comparison will be false and the result will
9059 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9060 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9061 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9062 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9063 break;
9064 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9065 // will return -0, so vmin can only be used for unsafe math or if one of
9066 // the operands is known to be nonzero.
9067 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009068 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009069 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9070 break;
9071 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009072 break;
9073
9074 case ISD::SETOGT:
9075 case ISD::SETOGE:
9076 case ISD::SETGT:
9077 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009078 case ISD::SETUGT:
9079 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009080 // If LHS is NaN, an ordered comparison will be false and the result will
9081 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9082 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9083 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9084 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9085 break;
9086 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9087 // will return +0, so vmax can only be used for unsafe math or if one of
9088 // the operands is known to be nonzero.
9089 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009090 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009091 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9092 break;
9093 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009094 break;
9095 }
9096
9097 if (!Opcode)
9098 return SDValue();
9099 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9100}
9101
Evan Chenge721f5c2011-07-13 00:42:17 +00009102/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9103SDValue
9104ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9105 SDValue Cmp = N->getOperand(4);
9106 if (Cmp.getOpcode() != ARMISD::CMPZ)
9107 // Only looking at EQ and NE cases.
9108 return SDValue();
9109
9110 EVT VT = N->getValueType(0);
9111 DebugLoc dl = N->getDebugLoc();
9112 SDValue LHS = Cmp.getOperand(0);
9113 SDValue RHS = Cmp.getOperand(1);
9114 SDValue FalseVal = N->getOperand(0);
9115 SDValue TrueVal = N->getOperand(1);
9116 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009117 ARMCC::CondCodes CC =
9118 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009119
9120 // Simplify
9121 // mov r1, r0
9122 // cmp r1, x
9123 // mov r0, y
9124 // moveq r0, x
9125 // to
9126 // cmp r0, x
9127 // movne r0, y
9128 //
9129 // mov r1, r0
9130 // cmp r1, x
9131 // mov r0, x
9132 // movne r0, y
9133 // to
9134 // cmp r0, x
9135 // movne r0, y
9136 /// FIXME: Turn this into a target neutral optimization?
9137 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009138 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009139 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9140 N->getOperand(3), Cmp);
9141 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9142 SDValue ARMcc;
9143 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9144 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9145 N->getOperand(3), NewCmp);
9146 }
9147
9148 if (Res.getNode()) {
9149 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009150 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009151 // Capture demanded bits information that would be otherwise lost.
9152 if (KnownZero == 0xfffffffe)
9153 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9154 DAG.getValueType(MVT::i1));
9155 else if (KnownZero == 0xffffff00)
9156 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9157 DAG.getValueType(MVT::i8));
9158 else if (KnownZero == 0xffff0000)
9159 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9160 DAG.getValueType(MVT::i16));
9161 }
9162
9163 return Res;
9164}
9165
Dan Gohman475871a2008-07-27 21:46:04 +00009166SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009167 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009168 switch (N->getOpcode()) {
9169 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009170 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009171 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009172 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009173 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009174 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009175 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9176 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009177 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009178 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009179 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009180 case ISD::STORE: return PerformSTORECombine(N, DCI);
9181 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9182 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009183 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009184 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009185 case ISD::FP_TO_SINT:
9186 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9187 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009188 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009189 case ISD::SHL:
9190 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009191 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009192 case ISD::SIGN_EXTEND:
9193 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009194 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9195 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009196 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009197 case ARMISD::VLD2DUP:
9198 case ARMISD::VLD3DUP:
9199 case ARMISD::VLD4DUP:
9200 return CombineBaseUpdate(N, DCI);
9201 case ISD::INTRINSIC_VOID:
9202 case ISD::INTRINSIC_W_CHAIN:
9203 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9204 case Intrinsic::arm_neon_vld1:
9205 case Intrinsic::arm_neon_vld2:
9206 case Intrinsic::arm_neon_vld3:
9207 case Intrinsic::arm_neon_vld4:
9208 case Intrinsic::arm_neon_vld2lane:
9209 case Intrinsic::arm_neon_vld3lane:
9210 case Intrinsic::arm_neon_vld4lane:
9211 case Intrinsic::arm_neon_vst1:
9212 case Intrinsic::arm_neon_vst2:
9213 case Intrinsic::arm_neon_vst3:
9214 case Intrinsic::arm_neon_vst4:
9215 case Intrinsic::arm_neon_vst2lane:
9216 case Intrinsic::arm_neon_vst3lane:
9217 case Intrinsic::arm_neon_vst4lane:
9218 return CombineBaseUpdate(N, DCI);
9219 default: break;
9220 }
9221 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009222 }
Dan Gohman475871a2008-07-27 21:46:04 +00009223 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009224}
9225
Evan Cheng31959b12011-02-02 01:06:55 +00009226bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9227 EVT VT) const {
9228 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9229}
9230
Bill Wendlingaf566342009-08-15 21:21:19 +00009231bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009232 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009233 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009234
9235 switch (VT.getSimpleVT().SimpleTy) {
9236 default:
9237 return false;
9238 case MVT::i8:
9239 case MVT::i16:
9240 case MVT::i32:
Evan Chengd10eab02012-09-18 01:42:45 +00009241 // Unaligned access can use (for example) LRDB, LRDH, LDR
9242 return AllowsUnaligned;
Evan Chenga99c5082012-08-15 17:44:53 +00009243 case MVT::f64:
Evan Chengd10eab02012-09-18 01:42:45 +00009244 case MVT::v2f64:
9245 // For any little-endian targets with neon, we can support unaligned ld/st
9246 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9247 // A big-endian target may also explictly support unaligned accesses
9248 return Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian());
Bill Wendlingaf566342009-08-15 21:21:19 +00009249 }
9250}
9251
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009252static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9253 unsigned AlignCheck) {
9254 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9255 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9256}
9257
9258EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9259 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00009260 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009261 bool MemcpyStrSrc,
9262 MachineFunction &MF) const {
9263 const Function *F = MF.getFunction();
9264
9265 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00009266 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00009267 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat) &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009268 Subtarget->hasNEON()) {
9269 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
9270 return MVT::v4i32;
9271 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
9272 return MVT::v2i32;
9273 }
9274 }
9275
Lang Hames5207bf22011-11-08 18:56:23 +00009276 // Lowering to i32/i16 if the size permits.
9277 if (Size >= 4) {
9278 return MVT::i32;
9279 } else if (Size >= 2) {
9280 return MVT::i16;
9281 }
9282
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009283 // Let the target-independent logic figure it out.
9284 return MVT::Other;
9285}
9286
Evan Chenge6c835f2009-08-14 20:09:37 +00009287static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9288 if (V < 0)
9289 return false;
9290
9291 unsigned Scale = 1;
9292 switch (VT.getSimpleVT().SimpleTy) {
9293 default: return false;
9294 case MVT::i1:
9295 case MVT::i8:
9296 // Scale == 1;
9297 break;
9298 case MVT::i16:
9299 // Scale == 2;
9300 Scale = 2;
9301 break;
9302 case MVT::i32:
9303 // Scale == 4;
9304 Scale = 4;
9305 break;
9306 }
9307
9308 if ((V & (Scale - 1)) != 0)
9309 return false;
9310 V /= Scale;
9311 return V == (V & ((1LL << 5) - 1));
9312}
9313
9314static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9315 const ARMSubtarget *Subtarget) {
9316 bool isNeg = false;
9317 if (V < 0) {
9318 isNeg = true;
9319 V = - V;
9320 }
9321
9322 switch (VT.getSimpleVT().SimpleTy) {
9323 default: return false;
9324 case MVT::i1:
9325 case MVT::i8:
9326 case MVT::i16:
9327 case MVT::i32:
9328 // + imm12 or - imm8
9329 if (isNeg)
9330 return V == (V & ((1LL << 8) - 1));
9331 return V == (V & ((1LL << 12) - 1));
9332 case MVT::f32:
9333 case MVT::f64:
9334 // Same as ARM mode. FIXME: NEON?
9335 if (!Subtarget->hasVFP2())
9336 return false;
9337 if ((V & 3) != 0)
9338 return false;
9339 V >>= 2;
9340 return V == (V & ((1LL << 8) - 1));
9341 }
9342}
9343
Evan Chengb01fad62007-03-12 23:30:29 +00009344/// isLegalAddressImmediate - Return true if the integer value can be used
9345/// as the offset of the target addressing mode for load / store of the
9346/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009347static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009348 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009349 if (V == 0)
9350 return true;
9351
Evan Cheng65011532009-03-09 19:15:00 +00009352 if (!VT.isSimple())
9353 return false;
9354
Evan Chenge6c835f2009-08-14 20:09:37 +00009355 if (Subtarget->isThumb1Only())
9356 return isLegalT1AddressImmediate(V, VT);
9357 else if (Subtarget->isThumb2())
9358 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009359
Evan Chenge6c835f2009-08-14 20:09:37 +00009360 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009361 if (V < 0)
9362 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009364 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009365 case MVT::i1:
9366 case MVT::i8:
9367 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009368 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009369 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009370 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009371 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009372 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009373 case MVT::f32:
9374 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009375 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009376 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009377 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009378 return false;
9379 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009380 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009381 }
Evan Chenga8e29892007-01-19 07:51:42 +00009382}
9383
Evan Chenge6c835f2009-08-14 20:09:37 +00009384bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9385 EVT VT) const {
9386 int Scale = AM.Scale;
9387 if (Scale < 0)
9388 return false;
9389
9390 switch (VT.getSimpleVT().SimpleTy) {
9391 default: return false;
9392 case MVT::i1:
9393 case MVT::i8:
9394 case MVT::i16:
9395 case MVT::i32:
9396 if (Scale == 1)
9397 return true;
9398 // r + r << imm
9399 Scale = Scale & ~1;
9400 return Scale == 2 || Scale == 4 || Scale == 8;
9401 case MVT::i64:
9402 // r + r
9403 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9404 return true;
9405 return false;
9406 case MVT::isVoid:
9407 // Note, we allow "void" uses (basically, uses that aren't loads or
9408 // stores), because arm allows folding a scale into many arithmetic
9409 // operations. This should be made more precise and revisited later.
9410
9411 // Allow r << imm, but the imm has to be a multiple of two.
9412 if (Scale & 1) return false;
9413 return isPowerOf2_32(Scale);
9414 }
9415}
9416
Chris Lattner37caf8c2007-04-09 23:33:39 +00009417/// isLegalAddressingMode - Return true if the addressing mode represented
9418/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009419bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009420 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009421 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009422 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009423 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009424
Chris Lattner37caf8c2007-04-09 23:33:39 +00009425 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009426 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009427 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009428
Chris Lattner37caf8c2007-04-09 23:33:39 +00009429 switch (AM.Scale) {
9430 case 0: // no scale reg, must be "r+i" or "r", or "i".
9431 break;
9432 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009433 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009434 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009435 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009436 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009437 // ARM doesn't support any R+R*scale+imm addr modes.
9438 if (AM.BaseOffs)
9439 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009440
Bob Wilson2c7dab12009-04-08 17:55:28 +00009441 if (!VT.isSimple())
9442 return false;
9443
Evan Chenge6c835f2009-08-14 20:09:37 +00009444 if (Subtarget->isThumb2())
9445 return isLegalT2ScaledAddressingMode(AM, VT);
9446
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009447 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009448 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009449 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009450 case MVT::i1:
9451 case MVT::i8:
9452 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009453 if (Scale < 0) Scale = -Scale;
9454 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009455 return true;
9456 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009457 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009458 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009459 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009460 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009461 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009462 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009463 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009464
Owen Anderson825b72b2009-08-11 20:47:22 +00009465 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009466 // Note, we allow "void" uses (basically, uses that aren't loads or
9467 // stores), because arm allows folding a scale into many arithmetic
9468 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009469
Chris Lattner37caf8c2007-04-09 23:33:39 +00009470 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009471 if (Scale & 1) return false;
9472 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009473 }
Evan Chengb01fad62007-03-12 23:30:29 +00009474 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009475 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009476}
9477
Evan Cheng77e47512009-11-11 19:05:52 +00009478/// isLegalICmpImmediate - Return true if the specified immediate is legal
9479/// icmp immediate, that is the target has icmp instructions which can compare
9480/// a register against the immediate without having to materialize the
9481/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009482bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009483 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009484 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009485 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009486 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009487 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009488 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009489 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009490}
9491
Andrew Trick8d8d9612012-07-18 18:34:27 +00009492/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9493/// *or sub* immediate, that is the target has add or sub instructions which can
9494/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009495/// immediate into a register.
9496bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009497 // Same encoding for add/sub, just flip the sign.
9498 int64_t AbsImm = llvm::abs64(Imm);
9499 if (!Subtarget->isThumb())
9500 return ARM_AM::getSOImmVal(AbsImm) != -1;
9501 if (Subtarget->isThumb2())
9502 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9503 // Thumb1 only has 8-bit unsigned immediate.
9504 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009505}
9506
Owen Andersone50ed302009-08-10 22:56:29 +00009507static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009508 bool isSEXTLoad, SDValue &Base,
9509 SDValue &Offset, bool &isInc,
9510 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009511 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9512 return false;
9513
Owen Anderson825b72b2009-08-11 20:47:22 +00009514 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009515 // AddressingMode 3
9516 Base = Ptr->getOperand(0);
9517 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009518 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009519 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009520 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009521 isInc = false;
9522 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9523 return true;
9524 }
9525 }
9526 isInc = (Ptr->getOpcode() == ISD::ADD);
9527 Offset = Ptr->getOperand(1);
9528 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009530 // AddressingMode 2
9531 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009532 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009533 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009534 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009535 isInc = false;
9536 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9537 Base = Ptr->getOperand(0);
9538 return true;
9539 }
9540 }
9541
9542 if (Ptr->getOpcode() == ISD::ADD) {
9543 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009544 ARM_AM::ShiftOpc ShOpcVal=
9545 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009546 if (ShOpcVal != ARM_AM::no_shift) {
9547 Base = Ptr->getOperand(1);
9548 Offset = Ptr->getOperand(0);
9549 } else {
9550 Base = Ptr->getOperand(0);
9551 Offset = Ptr->getOperand(1);
9552 }
9553 return true;
9554 }
9555
9556 isInc = (Ptr->getOpcode() == ISD::ADD);
9557 Base = Ptr->getOperand(0);
9558 Offset = Ptr->getOperand(1);
9559 return true;
9560 }
9561
Jim Grosbache5165492009-11-09 00:11:35 +00009562 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009563 return false;
9564}
9565
Owen Andersone50ed302009-08-10 22:56:29 +00009566static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009567 bool isSEXTLoad, SDValue &Base,
9568 SDValue &Offset, bool &isInc,
9569 SelectionDAG &DAG) {
9570 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9571 return false;
9572
9573 Base = Ptr->getOperand(0);
9574 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9575 int RHSC = (int)RHS->getZExtValue();
9576 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9577 assert(Ptr->getOpcode() == ISD::ADD);
9578 isInc = false;
9579 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9580 return true;
9581 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9582 isInc = Ptr->getOpcode() == ISD::ADD;
9583 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9584 return true;
9585 }
9586 }
9587
9588 return false;
9589}
9590
Evan Chenga8e29892007-01-19 07:51:42 +00009591/// getPreIndexedAddressParts - returns true by value, base pointer and
9592/// offset pointer and addressing mode by reference if the node's address
9593/// can be legally represented as pre-indexed load / store address.
9594bool
Dan Gohman475871a2008-07-27 21:46:04 +00009595ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9596 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009597 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009598 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009599 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009600 return false;
9601
Owen Andersone50ed302009-08-10 22:56:29 +00009602 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009603 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009604 bool isSEXTLoad = false;
9605 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9606 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009607 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009608 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9609 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9610 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009611 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009612 } else
9613 return false;
9614
9615 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009616 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009617 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009618 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9619 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009620 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009621 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009622 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009623 if (!isLegal)
9624 return false;
9625
9626 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9627 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009628}
9629
9630/// getPostIndexedAddressParts - returns true by value, base pointer and
9631/// offset pointer and addressing mode by reference if this node can be
9632/// combined with a load / store to form a post-indexed load / store.
9633bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009634 SDValue &Base,
9635 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009636 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009637 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009638 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009639 return false;
9640
Owen Andersone50ed302009-08-10 22:56:29 +00009641 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009642 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009643 bool isSEXTLoad = false;
9644 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009645 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009646 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009647 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9648 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009649 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009650 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009651 } else
9652 return false;
9653
9654 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009655 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009656 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009657 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009658 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009659 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009660 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9661 isInc, DAG);
9662 if (!isLegal)
9663 return false;
9664
Evan Cheng28dad2a2010-05-18 21:31:17 +00009665 if (Ptr != Base) {
9666 // Swap base ptr and offset to catch more post-index load / store when
9667 // it's legal. In Thumb2 mode, offset must be an immediate.
9668 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9669 !Subtarget->isThumb2())
9670 std::swap(Base, Offset);
9671
9672 // Post-indexed load / store update the base pointer.
9673 if (Ptr != Base)
9674 return false;
9675 }
9676
Evan Chenge88d5ce2009-07-02 07:28:31 +00009677 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9678 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009679}
9680
Dan Gohman475871a2008-07-27 21:46:04 +00009681void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009682 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009683 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009684 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009685 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009686 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009687 switch (Op.getOpcode()) {
9688 default: break;
9689 case ARMISD::CMOV: {
9690 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009691 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009692 if (KnownZero == 0 && KnownOne == 0) return;
9693
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009694 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009695 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009696 KnownZero &= KnownZeroRHS;
9697 KnownOne &= KnownOneRHS;
9698 return;
9699 }
9700 }
9701}
9702
9703//===----------------------------------------------------------------------===//
9704// ARM Inline Assembly Support
9705//===----------------------------------------------------------------------===//
9706
Evan Cheng55d42002011-01-08 01:24:27 +00009707bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9708 // Looking for "rev" which is V6+.
9709 if (!Subtarget->hasV6Ops())
9710 return false;
9711
9712 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9713 std::string AsmStr = IA->getAsmString();
9714 SmallVector<StringRef, 4> AsmPieces;
9715 SplitString(AsmStr, AsmPieces, ";\n");
9716
9717 switch (AsmPieces.size()) {
9718 default: return false;
9719 case 1:
9720 AsmStr = AsmPieces[0];
9721 AsmPieces.clear();
9722 SplitString(AsmStr, AsmPieces, " \t,");
9723
9724 // rev $0, $1
9725 if (AsmPieces.size() == 3 &&
9726 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9727 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009728 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009729 if (Ty && Ty->getBitWidth() == 32)
9730 return IntrinsicLowering::LowerToByteSwap(CI);
9731 }
9732 break;
9733 }
9734
9735 return false;
9736}
9737
Evan Chenga8e29892007-01-19 07:51:42 +00009738/// getConstraintType - Given a constraint letter, return the type of
9739/// constraint it is for this target.
9740ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009741ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9742 if (Constraint.size() == 1) {
9743 switch (Constraint[0]) {
9744 default: break;
9745 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009746 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009747 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009748 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009749 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009750 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009751 // An address with a single base register. Due to the way we
9752 // currently handle addresses it is the same as an 'r' memory constraint.
9753 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009754 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009755 } else if (Constraint.size() == 2) {
9756 switch (Constraint[0]) {
9757 default: break;
9758 // All 'U+' constraints are addresses.
9759 case 'U': return C_Memory;
9760 }
Evan Chenga8e29892007-01-19 07:51:42 +00009761 }
Chris Lattner4234f572007-03-25 02:14:49 +00009762 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009763}
9764
John Thompson44ab89e2010-10-29 17:29:13 +00009765/// Examine constraint type and operand type and determine a weight value.
9766/// This object must already have been set up with the operand type
9767/// and the current alternative constraint selected.
9768TargetLowering::ConstraintWeight
9769ARMTargetLowering::getSingleConstraintMatchWeight(
9770 AsmOperandInfo &info, const char *constraint) const {
9771 ConstraintWeight weight = CW_Invalid;
9772 Value *CallOperandVal = info.CallOperandVal;
9773 // If we don't have a value, we can't do a match,
9774 // but allow it at the lowest weight.
9775 if (CallOperandVal == NULL)
9776 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009777 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009778 // Look at the constraint type.
9779 switch (*constraint) {
9780 default:
9781 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9782 break;
9783 case 'l':
9784 if (type->isIntegerTy()) {
9785 if (Subtarget->isThumb())
9786 weight = CW_SpecificReg;
9787 else
9788 weight = CW_Register;
9789 }
9790 break;
9791 case 'w':
9792 if (type->isFloatingPointTy())
9793 weight = CW_Register;
9794 break;
9795 }
9796 return weight;
9797}
9798
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009799typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9800RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009801ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009802 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009803 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009804 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009805 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009806 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009807 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009808 return RCPair(0U, &ARM::tGPRRegClass);
9809 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009810 case 'h': // High regs or no regs.
9811 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009812 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009813 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009814 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009815 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009816 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009817 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009818 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009819 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009820 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009821 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009822 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009823 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009824 case 'x':
9825 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009826 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009827 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009828 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009829 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009830 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009831 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009832 case 't':
9833 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009834 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009835 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009836 }
9837 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009838 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009839 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009840
Evan Chenga8e29892007-01-19 07:51:42 +00009841 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9842}
9843
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009844/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9845/// vector. If it is invalid, don't add anything to Ops.
9846void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009847 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009848 std::vector<SDValue>&Ops,
9849 SelectionDAG &DAG) const {
9850 SDValue Result(0, 0);
9851
Eric Christopher100c8332011-06-02 23:16:42 +00009852 // Currently only support length 1 constraints.
9853 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009854
Eric Christopher100c8332011-06-02 23:16:42 +00009855 char ConstraintLetter = Constraint[0];
9856 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009857 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009858 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009859 case 'I': case 'J': case 'K': case 'L':
9860 case 'M': case 'N': case 'O':
9861 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9862 if (!C)
9863 return;
9864
9865 int64_t CVal64 = C->getSExtValue();
9866 int CVal = (int) CVal64;
9867 // None of these constraints allow values larger than 32 bits. Check
9868 // that the value fits in an int.
9869 if (CVal != CVal64)
9870 return;
9871
Eric Christopher100c8332011-06-02 23:16:42 +00009872 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009873 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009874 // Constant suitable for movw, must be between 0 and
9875 // 65535.
9876 if (Subtarget->hasV6T2Ops())
9877 if (CVal >= 0 && CVal <= 65535)
9878 break;
9879 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009880 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009881 if (Subtarget->isThumb1Only()) {
9882 // This must be a constant between 0 and 255, for ADD
9883 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009884 if (CVal >= 0 && CVal <= 255)
9885 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009886 } else if (Subtarget->isThumb2()) {
9887 // A constant that can be used as an immediate value in a
9888 // data-processing instruction.
9889 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9890 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009891 } else {
9892 // A constant that can be used as an immediate value in a
9893 // data-processing instruction.
9894 if (ARM_AM::getSOImmVal(CVal) != -1)
9895 break;
9896 }
9897 return;
9898
9899 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009900 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009901 // This must be a constant between -255 and -1, for negated ADD
9902 // immediates. This can be used in GCC with an "n" modifier that
9903 // prints the negated value, for use with SUB instructions. It is
9904 // not useful otherwise but is implemented for compatibility.
9905 if (CVal >= -255 && CVal <= -1)
9906 break;
9907 } else {
9908 // This must be a constant between -4095 and 4095. It is not clear
9909 // what this constraint is intended for. Implemented for
9910 // compatibility with GCC.
9911 if (CVal >= -4095 && CVal <= 4095)
9912 break;
9913 }
9914 return;
9915
9916 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009917 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009918 // A 32-bit value where only one byte has a nonzero value. Exclude
9919 // zero to match GCC. This constraint is used by GCC internally for
9920 // constants that can be loaded with a move/shift combination.
9921 // It is not useful otherwise but is implemented for compatibility.
9922 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9923 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009924 } else if (Subtarget->isThumb2()) {
9925 // A constant whose bitwise inverse can be used as an immediate
9926 // value in a data-processing instruction. This can be used in GCC
9927 // with a "B" modifier that prints the inverted value, for use with
9928 // BIC and MVN instructions. It is not useful otherwise but is
9929 // implemented for compatibility.
9930 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9931 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009932 } else {
9933 // A constant whose bitwise inverse can be used as an immediate
9934 // value in a data-processing instruction. This can be used in GCC
9935 // with a "B" modifier that prints the inverted value, for use with
9936 // BIC and MVN instructions. It is not useful otherwise but is
9937 // implemented for compatibility.
9938 if (ARM_AM::getSOImmVal(~CVal) != -1)
9939 break;
9940 }
9941 return;
9942
9943 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009944 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009945 // This must be a constant between -7 and 7,
9946 // for 3-operand ADD/SUB immediate instructions.
9947 if (CVal >= -7 && CVal < 7)
9948 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009949 } else if (Subtarget->isThumb2()) {
9950 // A constant whose negation can be used as an immediate value in a
9951 // data-processing instruction. This can be used in GCC with an "n"
9952 // modifier that prints the negated value, for use with SUB
9953 // instructions. It is not useful otherwise but is implemented for
9954 // compatibility.
9955 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9956 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009957 } else {
9958 // A constant whose negation can be used as an immediate value in a
9959 // data-processing instruction. This can be used in GCC with an "n"
9960 // modifier that prints the negated value, for use with SUB
9961 // instructions. It is not useful otherwise but is implemented for
9962 // compatibility.
9963 if (ARM_AM::getSOImmVal(-CVal) != -1)
9964 break;
9965 }
9966 return;
9967
9968 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009969 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009970 // This must be a multiple of 4 between 0 and 1020, for
9971 // ADD sp + immediate.
9972 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9973 break;
9974 } else {
9975 // A power of two or a constant between 0 and 32. This is used in
9976 // GCC for the shift amount on shifted register operands, but it is
9977 // useful in general for any shift amounts.
9978 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9979 break;
9980 }
9981 return;
9982
9983 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009984 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009985 // This must be a constant between 0 and 31, for shift amounts.
9986 if (CVal >= 0 && CVal <= 31)
9987 break;
9988 }
9989 return;
9990
9991 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009992 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009993 // This must be a multiple of 4 between -508 and 508, for
9994 // ADD/SUB sp = sp + immediate.
9995 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9996 break;
9997 }
9998 return;
9999 }
10000 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10001 break;
10002 }
10003
10004 if (Result.getNode()) {
10005 Ops.push_back(Result);
10006 return;
10007 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010008 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010009}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010010
10011bool
10012ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10013 // The ARM target isn't yet aware of offsets.
10014 return false;
10015}
Evan Cheng39382422009-10-28 01:44:26 +000010016
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010017bool ARM::isBitFieldInvertedMask(unsigned v) {
10018 if (v == 0xffffffff)
10019 return 0;
10020 // there can be 1's on either or both "outsides", all the "inside"
10021 // bits must be 0's
10022 unsigned int lsb = 0, msb = 31;
10023 while (v & (1 << msb)) --msb;
10024 while (v & (1 << lsb)) ++lsb;
10025 for (unsigned int i = lsb; i <= msb; ++i) {
10026 if (v & (1 << i))
10027 return 0;
10028 }
10029 return 1;
10030}
10031
Evan Cheng39382422009-10-28 01:44:26 +000010032/// isFPImmLegal - Returns true if the target can instruction select the
10033/// specified FP immediate natively. If false, the legalizer will
10034/// materialize the FP immediate as a load from a constant pool.
10035bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10036 if (!Subtarget->hasVFP3())
10037 return false;
10038 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010039 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010040 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010041 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010042 return false;
10043}
Bob Wilson65ffec42010-09-21 17:56:22 +000010044
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010045/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010046/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10047/// specified in the intrinsic calls.
10048bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10049 const CallInst &I,
10050 unsigned Intrinsic) const {
10051 switch (Intrinsic) {
10052 case Intrinsic::arm_neon_vld1:
10053 case Intrinsic::arm_neon_vld2:
10054 case Intrinsic::arm_neon_vld3:
10055 case Intrinsic::arm_neon_vld4:
10056 case Intrinsic::arm_neon_vld2lane:
10057 case Intrinsic::arm_neon_vld3lane:
10058 case Intrinsic::arm_neon_vld4lane: {
10059 Info.opc = ISD::INTRINSIC_W_CHAIN;
10060 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010061 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010062 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10063 Info.ptrVal = I.getArgOperand(0);
10064 Info.offset = 0;
10065 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10066 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10067 Info.vol = false; // volatile loads with NEON intrinsics not supported
10068 Info.readMem = true;
10069 Info.writeMem = false;
10070 return true;
10071 }
10072 case Intrinsic::arm_neon_vst1:
10073 case Intrinsic::arm_neon_vst2:
10074 case Intrinsic::arm_neon_vst3:
10075 case Intrinsic::arm_neon_vst4:
10076 case Intrinsic::arm_neon_vst2lane:
10077 case Intrinsic::arm_neon_vst3lane:
10078 case Intrinsic::arm_neon_vst4lane: {
10079 Info.opc = ISD::INTRINSIC_VOID;
10080 // Conservatively set memVT to the entire set of vectors stored.
10081 unsigned NumElts = 0;
10082 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010083 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010084 if (!ArgTy->isVectorTy())
10085 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010086 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010087 }
10088 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10089 Info.ptrVal = I.getArgOperand(0);
10090 Info.offset = 0;
10091 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10092 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10093 Info.vol = false; // volatile stores with NEON intrinsics not supported
10094 Info.readMem = false;
10095 Info.writeMem = true;
10096 return true;
10097 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010098 case Intrinsic::arm_strexd: {
10099 Info.opc = ISD::INTRINSIC_W_CHAIN;
10100 Info.memVT = MVT::i64;
10101 Info.ptrVal = I.getArgOperand(2);
10102 Info.offset = 0;
10103 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010104 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010105 Info.readMem = false;
10106 Info.writeMem = true;
10107 return true;
10108 }
10109 case Intrinsic::arm_ldrexd: {
10110 Info.opc = ISD::INTRINSIC_W_CHAIN;
10111 Info.memVT = MVT::i64;
10112 Info.ptrVal = I.getArgOperand(0);
10113 Info.offset = 0;
10114 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010115 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010116 Info.readMem = true;
10117 Info.writeMem = false;
10118 return true;
10119 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010120 default:
10121 break;
10122 }
10123
10124 return false;
10125}