blob: 8eb23fca593c1fa19677567f78ac07910679fdf3 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000029#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
41#include "llvm/IR/Instruction.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Type.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000046#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000049#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000050#include "llvm/Target/TargetOptions.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
Evan Chengc8e70452012-12-04 22:41:50 +0000549 // NEON does not have single instruction CTPOP for vectors with element
550 // types wider than 8-bits. However, custom lowering can leverage the
551 // v8i8/v16i8 vcnt instruction.
552 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
553 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
554 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
555 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
556
Jim Grosbachb302a4e2013-02-27 21:31:12 +0000557 // NEON only has FMA instructions as of VFP4.
558 if (!Subtarget->hasVFP4()) {
559 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
560 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
561 }
562
Bob Wilson1c3ef902011-02-07 17:43:21 +0000563 setTargetDAGCombine(ISD::INTRINSIC_VOID);
564 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000565 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
566 setTargetDAGCombine(ISD::SHL);
567 setTargetDAGCombine(ISD::SRL);
568 setTargetDAGCombine(ISD::SRA);
569 setTargetDAGCombine(ISD::SIGN_EXTEND);
570 setTargetDAGCombine(ISD::ZERO_EXTEND);
571 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000572 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000573 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000574 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000575 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
576 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000577 setTargetDAGCombine(ISD::FP_TO_SINT);
578 setTargetDAGCombine(ISD::FP_TO_UINT);
579 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000580
James Molloy873fd5f2012-02-20 09:24:05 +0000581 // It is legal to extload from v4i8 to v4i16 or v4i32.
582 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
583 MVT::v4i16, MVT::v2i16,
584 MVT::v2i32};
585 for (unsigned i = 0; i < 6; ++i) {
586 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
587 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
588 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
589 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000590 }
591
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000592 // ARM and Thumb2 support UMLAL/SMLAL.
593 if (!Subtarget->isThumb1Only())
594 setTargetDAGCombine(ISD::ADDC);
595
596
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000597 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000598
599 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000601
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000602 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000604
Evan Chenga8e29892007-01-19 07:51:42 +0000605 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000606 if (!Subtarget->isThumb1Only()) {
607 for (unsigned im = (unsigned)ISD::PRE_INC;
608 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setIndexedLoadAction(im, MVT::i1, Legal);
610 setIndexedLoadAction(im, MVT::i8, Legal);
611 setIndexedLoadAction(im, MVT::i16, Legal);
612 setIndexedLoadAction(im, MVT::i32, Legal);
613 setIndexedStoreAction(im, MVT::i1, Legal);
614 setIndexedStoreAction(im, MVT::i8, Legal);
615 setIndexedStoreAction(im, MVT::i16, Legal);
616 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000617 }
Evan Chenga8e29892007-01-19 07:51:42 +0000618 }
619
620 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000621 setOperationAction(ISD::MUL, MVT::i64, Expand);
622 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000623 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
625 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000626 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000627 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
628 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000629 setOperationAction(ISD::MULHS, MVT::i32, Expand);
630
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000631 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000632 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000633 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::SRL, MVT::i64, Custom);
635 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000636
Evan Cheng342e3162011-08-30 01:34:54 +0000637 if (!Subtarget->isThumb1Only()) {
638 // FIXME: We should do this for Thumb1 as well.
639 setOperationAction(ISD::ADDC, MVT::i32, Custom);
640 setOperationAction(ISD::ADDE, MVT::i32, Custom);
641 setOperationAction(ISD::SUBC, MVT::i32, Custom);
642 setOperationAction(ISD::SUBE, MVT::i32, Custom);
643 }
644
Evan Chenga8e29892007-01-19 07:51:42 +0000645 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000647 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000649 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000651
Chandler Carruth63974b22011-12-13 01:56:10 +0000652 // These just redirect to CTTZ and CTLZ on ARM.
653 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
654 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
655
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000656 // Only ARMv6 has BSWAP.
657 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000659
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000660 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
661 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
662 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000663 setOperationAction(ISD::SDIV, MVT::i32, Expand);
664 setOperationAction(ISD::UDIV, MVT::i32, Expand);
665 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::SREM, MVT::i32, Expand);
667 setOperationAction(ISD::UREM, MVT::i32, Expand);
668 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
669 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
672 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
673 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
674 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000675 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000676
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000677 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000678
Evan Chenga8e29892007-01-19 07:51:42 +0000679 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::VASTART, MVT::Other, Custom);
681 setOperationAction(ISD::VAARG, MVT::Other, Expand);
682 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
683 setOperationAction(ISD::VAEND, MVT::Other, Expand);
684 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
685 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000686
687 if (!Subtarget->isTargetDarwin()) {
688 // Non-Darwin platforms may return values in these registers via the
689 // personality function.
690 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
691 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
692 setExceptionPointerRegister(ARM::R0);
693 setExceptionSelectorRegister(ARM::R1);
694 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000695
Evan Cheng3a1588a2010-04-15 22:20:34 +0000696 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000697 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
698 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000699 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000700 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000701 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 // membarrier needs custom lowering; the rest are legal and handled
703 // normally.
704 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000705 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000706 // Custom lowering for 64-bit ops
707 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
708 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
709 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
710 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
711 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
Silviu Baranga35b3df62012-11-29 14:41:25 +0000712 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
713 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
714 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
715 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
716 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000717 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000718 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
719 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000720 } else {
721 // Set them all for expansion, which will force libcalls.
722 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000723 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000724 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000725 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000726 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000727 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000728 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000729 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000730 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000731 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000732 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000733 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000734 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000735 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000736 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
737 // Unordered/Monotonic case.
738 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
739 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000740 // Since the libcalls include locking, fold in the fences
741 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000742 }
Evan Chenga8e29892007-01-19 07:51:42 +0000743
Evan Cheng416941d2010-11-04 05:19:35 +0000744 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000745
Eli Friedmana2c6f452010-06-26 04:36:50 +0000746 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
747 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
749 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000750 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000752
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000753 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
754 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000755 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000756 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000757 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000758 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
759 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000760
761 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000763 if (Subtarget->isTargetDarwin()) {
764 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
765 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000766 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000767 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::SETCC, MVT::i32, Expand);
770 setOperationAction(ISD::SETCC, MVT::f32, Expand);
771 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000772 setOperationAction(ISD::SELECT, MVT::i32, Custom);
773 setOperationAction(ISD::SELECT, MVT::f32, Custom);
774 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
776 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
777 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000778
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
780 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
781 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
782 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
783 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000784
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000785 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::FSIN, MVT::f64, Expand);
787 setOperationAction(ISD::FSIN, MVT::f32, Expand);
788 setOperationAction(ISD::FCOS, MVT::f32, Expand);
789 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000790 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
791 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FREM, MVT::f64, Expand);
793 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000794 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
795 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
797 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000798 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 setOperationAction(ISD::FPOW, MVT::f64, Expand);
800 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000801
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000802 if (!Subtarget->hasVFP4()) {
803 setOperationAction(ISD::FMA, MVT::f64, Expand);
804 setOperationAction(ISD::FMA, MVT::f32, Expand);
805 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000806
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000807 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000808 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000809 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
810 if (Subtarget->hasVFP2()) {
811 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
812 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
813 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
814 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
815 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000816 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000817 if (!Subtarget->hasFP16()) {
818 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
819 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000820 }
Evan Cheng110cf482008-04-01 01:50:16 +0000821 }
Evan Chenga8e29892007-01-19 07:51:42 +0000822
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000823 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000824 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000825 setTargetDAGCombine(ISD::ADD);
826 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000827 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000828 setTargetDAGCombine(ISD::AND);
829 setTargetDAGCombine(ISD::OR);
830 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000831
Evan Cheng5fb468a2012-02-23 02:58:19 +0000832 if (Subtarget->hasV6Ops())
833 setTargetDAGCombine(ISD::SRL);
834
Evan Chenga8e29892007-01-19 07:51:42 +0000835 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000836
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000837 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
838 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000839 setSchedulingPreference(Sched::RegPressure);
840 else
841 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000842
Evan Cheng05219282011-01-06 06:52:41 +0000843 //// temporary - rewrite interface to use type
Jim Grosbach3450f802013-02-20 21:13:59 +0000844 MaxStoresPerMemset = 8;
845 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
846 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
847 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
848 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
849 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengf6799392010-06-26 01:52:05 +0000850
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000851 // On ARM arguments smaller than 4 bytes are extended, so all arguments
852 // are at least 4 bytes aligned.
853 setMinStackArgumentAlignment(4);
854
Jim Grosbach3450f802013-02-20 21:13:59 +0000855 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000856
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000857 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach3450f802013-02-20 21:13:59 +0000858 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000859
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000860 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000861}
862
Andrew Trick32cec0a2011-01-19 02:35:27 +0000863// FIXME: It might make sense to define the representative register class as the
864// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
865// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
866// SPR's representative would be DPR_VFP2. This should work well if register
867// pressure tracking were modified such that a register use would increment the
868// pressure of the register class's representative and all of it's super
869// classes' representatives transitively. We have not implemented this because
870// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000871// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000872// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +0000874ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Cheng4f6b4672010-07-21 06:09:07 +0000875 const TargetRegisterClass *RRC = 0;
876 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +0000877 switch (VT.SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000878 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000879 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000880 // Use DPR as representative register class for all floating point
881 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
882 // the cost is 1 for both f32 and f64.
883 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000884 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000885 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000886 // When NEON is used for SP, only half of the register file is available
887 // because operations that define both SP and DP results will be constrained
888 // to the VFP2 class (D0-D15). We currently model this constraint prior to
889 // coalescing by double-counting the SP regs. See the FIXME above.
890 if (Subtarget->useNEONForSinglePrecisionFP())
891 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000892 break;
893 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
894 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000895 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000896 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000897 break;
898 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000899 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000900 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000901 break;
902 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000903 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000904 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000905 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000906 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000907 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000908}
909
Evan Chenga8e29892007-01-19 07:51:42 +0000910const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
911 switch (Opcode) {
912 default: return 0;
913 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000914 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000915 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000916 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
917 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000918 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000919 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
920 case ARMISD::tCALL: return "ARMISD::tCALL";
921 case ARMISD::BRCOND: return "ARMISD::BRCOND";
922 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000923 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000924 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
925 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
926 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000927 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000928 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000929 case ARMISD::CMPFP: return "ARMISD::CMPFP";
930 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000931 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000932 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000933
Evan Chenga8e29892007-01-19 07:51:42 +0000934 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000935
Jim Grosbach3482c802010-01-18 19:58:49 +0000936 case ARMISD::RBIT: return "ARMISD::RBIT";
937
Bob Wilson76a312b2010-03-19 22:51:32 +0000938 case ARMISD::FTOSI: return "ARMISD::FTOSI";
939 case ARMISD::FTOUI: return "ARMISD::FTOUI";
940 case ARMISD::SITOF: return "ARMISD::SITOF";
941 case ARMISD::UITOF: return "ARMISD::UITOF";
942
Evan Chenga8e29892007-01-19 07:51:42 +0000943 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
944 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
945 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000946
Evan Cheng342e3162011-08-30 01:34:54 +0000947 case ARMISD::ADDC: return "ARMISD::ADDC";
948 case ARMISD::ADDE: return "ARMISD::ADDE";
949 case ARMISD::SUBC: return "ARMISD::SUBC";
950 case ARMISD::SUBE: return "ARMISD::SUBE";
951
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000952 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
953 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000954
Evan Chengc5942082009-10-28 06:55:03 +0000955 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
956 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
957
Dale Johannesen51e28e62010-06-03 21:09:53 +0000958 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000959
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000960 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000961
Evan Cheng86198642009-08-07 00:34:42 +0000962 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
963
Jim Grosbach3728e962009-12-10 00:11:09 +0000964 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000965 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000966
Evan Chengdfed19f2010-11-03 06:34:55 +0000967 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
968
Bob Wilson5bafff32009-06-22 23:27:02 +0000969 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000970 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000972 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
973 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000974 case ARMISD::VCGEU: return "ARMISD::VCGEU";
975 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000976 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
977 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000978 case ARMISD::VCGTU: return "ARMISD::VCGTU";
979 case ARMISD::VTST: return "ARMISD::VTST";
980
981 case ARMISD::VSHL: return "ARMISD::VSHL";
982 case ARMISD::VSHRs: return "ARMISD::VSHRs";
983 case ARMISD::VSHRu: return "ARMISD::VSHRu";
984 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
985 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
986 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
987 case ARMISD::VSHRN: return "ARMISD::VSHRN";
988 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
989 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
990 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
991 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
992 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
993 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
994 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
995 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
996 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
997 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
998 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
999 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1000 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1001 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +00001002 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +00001003 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +00001004 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +00001005 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +00001006 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +00001007 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +00001008 case ARMISD::VREV64: return "ARMISD::VREV64";
1009 case ARMISD::VREV32: return "ARMISD::VREV32";
1010 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001011 case ARMISD::VZIP: return "ARMISD::VZIP";
1012 case ARMISD::VUZP: return "ARMISD::VUZP";
1013 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +00001014 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1015 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +00001016 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1017 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00001018 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1019 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001020 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001021 case ARMISD::FMAX: return "ARMISD::FMAX";
1022 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001023 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001024 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1025 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001026 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001027 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1028 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1029 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001030 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1031 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1032 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1033 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1034 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1035 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1036 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1037 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1038 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1039 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1040 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1041 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1042 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1043 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1044 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1045 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1046 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001047 }
1048}
1049
Duncan Sands28b77e92011-09-06 19:07:46 +00001050EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1051 if (!VT.isVector()) return getPointerTy();
1052 return VT.changeVectorElementTypeToInteger();
1053}
1054
Evan Cheng06b666c2010-05-15 02:18:07 +00001055/// getRegClassFor - Return the register class that should be used for the
1056/// specified value type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001057const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001058 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1059 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1060 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001061 if (Subtarget->hasNEON()) {
1062 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001063 return &ARM::QQPRRegClass;
1064 if (VT == MVT::v8i64)
1065 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001066 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001067 return TargetLowering::getRegClassFor(VT);
1068}
1069
Eric Christopherab695882010-07-21 22:26:11 +00001070// Create a fast isel object.
1071FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001072ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1073 const TargetLibraryInfo *libInfo) const {
1074 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001075}
1076
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001077/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1078/// be used for loads / stores from the global.
1079unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1080 return (Subtarget->isThumb1Only() ? 127 : 4095);
1081}
1082
Evan Cheng1cc39842010-05-20 23:26:43 +00001083Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001084 unsigned NumVals = N->getNumValues();
1085 if (!NumVals)
1086 return Sched::RegPressure;
1087
1088 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001089 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001090 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001091 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001092 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001093 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001094 }
Evan Chengc10f5432010-05-28 23:25:23 +00001095
1096 if (!N->isMachineOpcode())
1097 return Sched::RegPressure;
1098
1099 // Load are scheduled for latency even if there instruction itinerary
1100 // is not available.
1101 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001102 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001103
Evan Chenge837dea2011-06-28 19:10:37 +00001104 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001105 return Sched::RegPressure;
1106 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001107 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001108 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001109
Evan Cheng1cc39842010-05-20 23:26:43 +00001110 return Sched::RegPressure;
1111}
1112
Evan Chenga8e29892007-01-19 07:51:42 +00001113//===----------------------------------------------------------------------===//
1114// Lowering Code
1115//===----------------------------------------------------------------------===//
1116
Evan Chenga8e29892007-01-19 07:51:42 +00001117/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1118static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1119 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001120 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001121 case ISD::SETNE: return ARMCC::NE;
1122 case ISD::SETEQ: return ARMCC::EQ;
1123 case ISD::SETGT: return ARMCC::GT;
1124 case ISD::SETGE: return ARMCC::GE;
1125 case ISD::SETLT: return ARMCC::LT;
1126 case ISD::SETLE: return ARMCC::LE;
1127 case ISD::SETUGT: return ARMCC::HI;
1128 case ISD::SETUGE: return ARMCC::HS;
1129 case ISD::SETULT: return ARMCC::LO;
1130 case ISD::SETULE: return ARMCC::LS;
1131 }
1132}
1133
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001134/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1135static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001136 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001137 CondCode2 = ARMCC::AL;
1138 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001139 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001140 case ISD::SETEQ:
1141 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1142 case ISD::SETGT:
1143 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1144 case ISD::SETGE:
1145 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1146 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001147 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001148 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1149 case ISD::SETO: CondCode = ARMCC::VC; break;
1150 case ISD::SETUO: CondCode = ARMCC::VS; break;
1151 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1152 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1153 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1154 case ISD::SETLT:
1155 case ISD::SETULT: CondCode = ARMCC::LT; break;
1156 case ISD::SETLE:
1157 case ISD::SETULE: CondCode = ARMCC::LE; break;
1158 case ISD::SETNE:
1159 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1160 }
Evan Chenga8e29892007-01-19 07:51:42 +00001161}
1162
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163//===----------------------------------------------------------------------===//
1164// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165//===----------------------------------------------------------------------===//
1166
1167#include "ARMGenCallingConv.inc"
1168
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1170/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001171CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001172 bool Return,
1173 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001174 switch (CC) {
1175 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001176 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001177 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001178 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001179 if (!Subtarget->isAAPCS_ABI())
1180 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1181 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1182 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1183 }
1184 // Fallthrough
1185 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001186 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001187 if (!Subtarget->isAAPCS_ABI())
1188 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1189 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001190 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1191 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001192 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1193 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1194 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001195 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001196 if (!isVarArg)
1197 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1198 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001199 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001200 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001201 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001202 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001203 case CallingConv::GHC:
1204 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001205 }
1206}
1207
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208/// LowerCallResult - Lower the result values of a call into the
1209/// appropriate copies out of appropriate physical registers.
1210SDValue
1211ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001212 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 const SmallVectorImpl<ISD::InputArg> &Ins,
1214 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001215 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217 // Assign locations to each value returned by this call.
1218 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001219 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1220 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001222 CCAssignFnForNode(CallConv, /* Return*/ true,
1223 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224
1225 // Copy all of the result registers out of their specified physreg.
1226 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1227 CCValAssign VA = RVLocs[i];
1228
Bob Wilson80915242009-04-25 00:33:20 +00001229 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001231 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001232 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001234 Chain = Lo.getValue(1);
1235 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001238 InFlag);
1239 Chain = Hi.getValue(1);
1240 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001241 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001242
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 if (VA.getLocVT() == MVT::v2f64) {
1244 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1245 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1246 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001247
1248 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001250 Chain = Lo.getValue(1);
1251 InFlag = Lo.getValue(2);
1252 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001254 Chain = Hi.getValue(1);
1255 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001256 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1258 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001259 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001261 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1262 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001263 Chain = Val.getValue(1);
1264 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 }
Bob Wilson80915242009-04-25 00:33:20 +00001266
1267 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001268 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001269 case CCValAssign::Full: break;
1270 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001271 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001272 break;
1273 }
1274
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 }
1277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279}
1280
Bob Wilsondee46d72009-04-17 20:35:10 +00001281/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1284 SDValue StackPtr, SDValue Arg,
1285 DebugLoc dl, SelectionDAG &DAG,
1286 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001287 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001288 unsigned LocMemOffset = VA.getLocMemOffset();
1289 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1290 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001292 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001293 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001294}
1295
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001297 SDValue Chain, SDValue &Arg,
1298 RegsToPassVector &RegsToPass,
1299 CCValAssign &VA, CCValAssign &NextVA,
1300 SDValue &StackPtr,
1301 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001302 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001303
Jim Grosbache5165492009-11-09 00:11:35 +00001304 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001305 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1307
1308 if (NextVA.isRegLoc())
1309 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1310 else {
1311 assert(NextVA.isMemLoc());
1312 if (StackPtr.getNode() == 0)
1313 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1316 dl, DAG, NextVA,
1317 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001318 }
1319}
1320
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001322/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1323/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001324SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001325ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001326 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001327 SelectionDAG &DAG = CLI.DAG;
1328 DebugLoc &dl = CLI.DL;
1329 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1330 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1331 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1332 SDValue Chain = CLI.Chain;
1333 SDValue Callee = CLI.Callee;
1334 bool &isTailCall = CLI.IsTailCall;
1335 CallingConv::ID CallConv = CLI.CallConv;
1336 bool doesNotRet = CLI.DoesNotReturn;
1337 bool isVarArg = CLI.IsVarArg;
1338
Dale Johannesen51e28e62010-06-03 21:09:53 +00001339 MachineFunction &MF = DAG.getMachineFunction();
1340 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1341 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001342 // Disable tail calls if they're not supported.
1343 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001344 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001345 if (isTailCall) {
1346 // Check if it's really possible to do a tail call.
1347 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1348 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001349 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001350 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1351 // detected sibcalls.
1352 if (isTailCall) {
1353 ++NumTailCalls;
1354 IsSibCall = true;
1355 }
1356 }
Evan Chenga8e29892007-01-19 07:51:42 +00001357
Bob Wilson1f595bb2009-04-17 19:07:39 +00001358 // Analyze operands of the call, assigning locations to each operand.
1359 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001360 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1361 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001363 CCAssignFnForNode(CallConv, /* Return*/ false,
1364 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001365
Bob Wilson1f595bb2009-04-17 19:07:39 +00001366 // Get a count of how many bytes are to be pushed on the stack.
1367 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001368
Dale Johannesen51e28e62010-06-03 21:09:53 +00001369 // For tail calls, memory operands are available in our caller's stack.
1370 if (IsSibCall)
1371 NumBytes = 0;
1372
Evan Chenga8e29892007-01-19 07:51:42 +00001373 // Adjust the stack pointer for the new arguments...
1374 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001375 if (!IsSibCall)
1376 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001377
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001378 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001379
Bob Wilson5bafff32009-06-22 23:27:02 +00001380 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001381 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001382
Bob Wilson1f595bb2009-04-17 19:07:39 +00001383 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001384 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001385 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1386 i != e;
1387 ++i, ++realArgIdx) {
1388 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001389 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001391 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001392
Bob Wilson1f595bb2009-04-17 19:07:39 +00001393 // Promote the value if needed.
1394 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001395 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001396 case CCValAssign::Full: break;
1397 case CCValAssign::SExt:
1398 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1399 break;
1400 case CCValAssign::ZExt:
1401 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1402 break;
1403 case CCValAssign::AExt:
1404 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1405 break;
1406 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001408 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001409 }
1410
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001411 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001412 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001413 if (VA.getLocVT() == MVT::v2f64) {
1414 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1415 DAG.getConstant(0, MVT::i32));
1416 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1417 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001418
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001420 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1421
1422 VA = ArgLocs[++i]; // skip ahead to next loc
1423 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001424 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001425 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1426 } else {
1427 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001428
Dan Gohman98ca4f22009-08-05 01:29:28 +00001429 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1430 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001431 }
1432 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001434 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001435 }
1436 } else if (VA.isRegLoc()) {
1437 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001438 } else if (isByVal) {
1439 assert(VA.isMemLoc());
1440 unsigned offset = 0;
1441
1442 // True if this byval aggregate will be split between registers
1443 // and memory.
1444 if (CCInfo.isFirstByValRegValid()) {
1445 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1446 unsigned int i, j;
1447 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1448 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1449 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1450 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1451 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001452 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001453 MemOpChains.push_back(Load.getValue(1));
1454 RegsToPass.push_back(std::make_pair(j, Load));
1455 }
1456 offset = ARM::R4 - CCInfo.getFirstByValReg();
1457 CCInfo.clearFirstByValReg();
1458 }
1459
Manman Ren763a75d2012-06-01 02:44:42 +00001460 if (Flags.getByValSize() - 4*offset > 0) {
1461 unsigned LocMemOffset = VA.getLocMemOffset();
1462 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1463 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1464 StkPtrOff);
1465 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1466 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1467 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1468 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001469 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001470
Manman Ren763a75d2012-06-01 02:44:42 +00001471 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001472 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001473 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1474 Ops, array_lengthof(Ops)));
1475 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001476 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001477 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1480 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001481 }
Evan Chenga8e29892007-01-19 07:51:42 +00001482 }
1483
1484 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001486 &MemOpChains[0], MemOpChains.size());
1487
1488 // Build a sequence of copy-to-reg nodes chained together with token chain
1489 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001490 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001491 // Tail call byval lowering might overwrite argument registers so in case of
1492 // tail call optimization the copies to registers are lowered later.
1493 if (!isTailCall)
1494 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1495 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1496 RegsToPass[i].second, InFlag);
1497 InFlag = Chain.getValue(1);
1498 }
Evan Chenga8e29892007-01-19 07:51:42 +00001499
Dale Johannesen51e28e62010-06-03 21:09:53 +00001500 // For tail calls lower the arguments to the 'real' stack slot.
1501 if (isTailCall) {
1502 // Force all the incoming stack arguments to be loaded from the stack
1503 // before any new outgoing arguments are stored to the stack, because the
1504 // outgoing stack slots may alias the incoming argument stack slots, and
1505 // the alias isn't otherwise explicit. This is slightly more conservative
1506 // than necessary, because it means that each store effectively depends
1507 // on every argument instead of just those arguments it would clobber.
1508
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001509 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001510 InFlag = SDValue();
1511 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1512 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1513 RegsToPass[i].second, InFlag);
1514 InFlag = Chain.getValue(1);
1515 }
1516 InFlag =SDValue();
1517 }
1518
Bill Wendling056292f2008-09-16 21:48:12 +00001519 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1520 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1521 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001522 bool isDirect = false;
1523 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001524 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001525 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001526
1527 if (EnableARMLongCalls) {
1528 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1529 && "long-calls with non-static relocation model!");
1530 // Handle a global address or an external symbol. If it's not one of
1531 // those, the target's already in a register, so we don't need to do
1532 // anything extra.
1533 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001534 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001535 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001536 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001537 ARMConstantPoolValue *CPV =
1538 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1539
Jim Grosbache7b52522010-04-14 22:28:31 +00001540 // Get the address of the callee into a register
1541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1543 Callee = DAG.getLoad(getPointerTy(), dl,
1544 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001545 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001546 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001547 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1548 const char *Sym = S->getSymbol();
1549
1550 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001551 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001552 ARMConstantPoolValue *CPV =
1553 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1554 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001555 // Get the address of the callee into a register
1556 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1557 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1558 Callee = DAG.getLoad(getPointerTy(), dl,
1559 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001560 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001561 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001562 }
1563 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001564 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001565 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001566 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001567 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001568 getTargetMachine().getRelocationModel() != Reloc::Static;
1569 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001570 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001571 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001572 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001573 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001574 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001575 ARMConstantPoolValue *CPV =
1576 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001577 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001579 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001580 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001581 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001582 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001583 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001584 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001585 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001586 } else {
1587 // On ELF targets for PIC code, direct calls should go through the PLT
1588 unsigned OpFlags = 0;
1589 if (Subtarget->isTargetELF() &&
Chad Rosiera6ca7032013-02-28 19:16:42 +00001590 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach637d89f2010-09-22 23:27:36 +00001591 OpFlags = ARMII::MO_PLT;
1592 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1593 }
Bill Wendling056292f2008-09-16 21:48:12 +00001594 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001595 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001596 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001597 getTargetMachine().getRelocationModel() != Reloc::Static;
1598 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001599 // tBX takes a register source operand.
1600 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001601 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001602 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001603 ARMConstantPoolValue *CPV =
1604 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1605 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001606 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001607 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001608 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001609 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001610 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001611 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001612 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001613 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001614 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001615 } else {
1616 unsigned OpFlags = 0;
1617 // On ELF targets for PIC code, direct calls should go through the PLT
1618 if (Subtarget->isTargetELF() &&
1619 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1620 OpFlags = ARMII::MO_PLT;
1621 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1622 }
Evan Chenga8e29892007-01-19 07:51:42 +00001623 }
1624
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001625 // FIXME: handle tail calls differently.
1626 unsigned CallOpc;
Bill Wendling831737d2012-12-30 10:32:01 +00001627 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1628 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001629 if (Subtarget->isThumb()) {
1630 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001631 CallOpc = ARMISD::CALL_NOLINK;
1632 else
1633 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1634 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001635 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001636 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001637 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001638 // Emit regular call when code size is the priority
1639 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001640 // "mov lr, pc; b _foo" to avoid confusing the RSP
1641 CallOpc = ARMISD::CALL_NOLINK;
1642 else
1643 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001644 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001645
Dan Gohman475871a2008-07-27 21:46:04 +00001646 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001647 Ops.push_back(Chain);
1648 Ops.push_back(Callee);
1649
1650 // Add argument registers to the end of the list so that they are known live
1651 // into the call.
1652 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1653 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1654 RegsToPass[i].second.getValueType()));
1655
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001656 // Add a register mask operand representing the call-preserved registers.
1657 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1658 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1659 assert(Mask && "Missing call preserved mask for calling convention");
1660 Ops.push_back(DAG.getRegisterMask(Mask));
1661
Gabor Greifba36cb52008-08-28 21:40:38 +00001662 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001663 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001664
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001665 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001666 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001667 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001668
Duncan Sands4bdcb612008-07-02 17:40:58 +00001669 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001670 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001671 InFlag = Chain.getValue(1);
1672
Chris Lattnere563bbc2008-10-11 22:08:30 +00001673 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1674 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001676 InFlag = Chain.getValue(1);
1677
Bob Wilson1f595bb2009-04-17 19:07:39 +00001678 // Handle result values, copying them out of physregs into vregs that we
1679 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1681 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001682}
1683
Stuart Hastingsf222e592011-02-28 17:17:53 +00001684/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001685/// on the stack. Remember the next parameter register to allocate,
1686/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001687/// this.
1688void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001689ARMTargetLowering::HandleByVal(
1690 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001691 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1692 assert((State->getCallOrPrologue() == Prologue ||
1693 State->getCallOrPrologue() == Call) &&
1694 "unhandled ParmContext");
1695 if ((!State->isFirstByValRegValid()) &&
1696 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001697 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1698 unsigned AlignInRegs = Align / 4;
1699 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1700 for (unsigned i = 0; i < Waste; ++i)
1701 reg = State->AllocateReg(GPRArgRegs, 4);
1702 }
1703 if (reg != 0) {
1704 State->setFirstByValReg(reg);
1705 // At a call site, a byval parameter that is split between
1706 // registers and memory needs its size truncated here. In a
1707 // function prologue, such byval parameters are reassembled in
1708 // memory, and are not truncated.
1709 if (State->getCallOrPrologue() == Call) {
1710 unsigned excess = 4 * (ARM::R4 - reg);
1711 assert(size >= excess && "expected larger existing stack allocation");
1712 size -= excess;
1713 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001714 }
1715 }
1716 // Confiscate any remaining parameter registers to preclude their
1717 // assignment to subsequent parameters.
1718 while (State->AllocateReg(GPRArgRegs, 4))
1719 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001720}
1721
Dale Johannesen51e28e62010-06-03 21:09:53 +00001722/// MatchingStackOffset - Return true if the given stack call argument is
1723/// already available in the same position (relatively) of the caller's
1724/// incoming argument stack.
1725static
1726bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1727 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001728 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001729 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1730 int FI = INT_MAX;
1731 if (Arg.getOpcode() == ISD::CopyFromReg) {
1732 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001733 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001734 return false;
1735 MachineInstr *Def = MRI->getVRegDef(VR);
1736 if (!Def)
1737 return false;
1738 if (!Flags.isByVal()) {
1739 if (!TII->isLoadFromStackSlot(Def, FI))
1740 return false;
1741 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001742 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001743 }
1744 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1745 if (Flags.isByVal())
1746 // ByVal argument is passed in as a pointer but it's now being
1747 // dereferenced. e.g.
1748 // define @foo(%struct.X* %A) {
1749 // tail call @bar(%struct.X* byval %A)
1750 // }
1751 return false;
1752 SDValue Ptr = Ld->getBasePtr();
1753 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1754 if (!FINode)
1755 return false;
1756 FI = FINode->getIndex();
1757 } else
1758 return false;
1759
1760 assert(FI != INT_MAX);
1761 if (!MFI->isFixedObjectIndex(FI))
1762 return false;
1763 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1764}
1765
1766/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1767/// for tail call optimization. Targets which want to do tail call
1768/// optimization should implement this function.
1769bool
1770ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1771 CallingConv::ID CalleeCC,
1772 bool isVarArg,
1773 bool isCalleeStructRet,
1774 bool isCallerStructRet,
1775 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001776 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001777 const SmallVectorImpl<ISD::InputArg> &Ins,
1778 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001779 const Function *CallerF = DAG.getMachineFunction().getFunction();
1780 CallingConv::ID CallerCC = CallerF->getCallingConv();
1781 bool CCMatch = CallerCC == CalleeCC;
1782
1783 // Look for obvious safe cases to perform tail call optimization that do not
1784 // require ABI changes. This is what gcc calls sibcall.
1785
Jim Grosbach7616b642010-06-16 23:45:49 +00001786 // Do not sibcall optimize vararg calls unless the call site is not passing
1787 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001788 if (isVarArg && !Outs.empty())
1789 return false;
1790
1791 // Also avoid sibcall optimization if either caller or callee uses struct
1792 // return semantics.
1793 if (isCalleeStructRet || isCallerStructRet)
1794 return false;
1795
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001796 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001797 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1798 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1799 // support in the assembler and linker to be used. This would need to be
1800 // fixed to fully support tail calls in Thumb1.
1801 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001802 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1803 // LR. This means if we need to reload LR, it takes an extra instructions,
1804 // which outweighs the value of the tail call; but here we don't know yet
1805 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001806 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001807 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001808
1809 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1810 // but we need to make sure there are enough registers; the only valid
1811 // registers are the 4 used for parameters. We don't currently do this
1812 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001813 if (Subtarget->isThumb1Only())
1814 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001815
Dale Johannesen51e28e62010-06-03 21:09:53 +00001816 // If the calling conventions do not match, then we'd better make sure the
1817 // results are returned in the same way as what the caller expects.
1818 if (!CCMatch) {
1819 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001820 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1821 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001822 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1823
1824 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001825 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1826 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001827 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1828
1829 if (RVLocs1.size() != RVLocs2.size())
1830 return false;
1831 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1832 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1833 return false;
1834 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1835 return false;
1836 if (RVLocs1[i].isRegLoc()) {
1837 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1838 return false;
1839 } else {
1840 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1841 return false;
1842 }
1843 }
1844 }
1845
Manman Rene6c3cc82012-10-12 23:39:43 +00001846 // If Caller's vararg or byval argument has been split between registers and
1847 // stack, do not perform tail call, since part of the argument is in caller's
1848 // local frame.
1849 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1850 getInfo<ARMFunctionInfo>();
1851 if (AFI_Caller->getVarArgsRegSaveSize())
1852 return false;
1853
Dale Johannesen51e28e62010-06-03 21:09:53 +00001854 // If the callee takes no arguments then go on to check the results of the
1855 // call.
1856 if (!Outs.empty()) {
1857 // Check if stack adjustment is needed. For now, do not do this if any
1858 // argument is passed on the stack.
1859 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001860 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1861 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001862 CCInfo.AnalyzeCallOperands(Outs,
1863 CCAssignFnForNode(CalleeCC, false, isVarArg));
1864 if (CCInfo.getNextStackOffset()) {
1865 MachineFunction &MF = DAG.getMachineFunction();
1866
1867 // Check if the arguments are already laid out in the right way as
1868 // the caller's fixed stack objects.
1869 MachineFrameInfo *MFI = MF.getFrameInfo();
1870 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001871 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001872 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1873 i != e;
1874 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001875 CCValAssign &VA = ArgLocs[i];
1876 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001877 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001878 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001879 if (VA.getLocInfo() == CCValAssign::Indirect)
1880 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001881 if (VA.needsCustom()) {
1882 // f64 and vector types are split into multiple registers or
1883 // register/stack-slot combinations. The types will not match
1884 // the registers; give up on memory f64 refs until we figure
1885 // out what to do about this.
1886 if (!VA.isRegLoc())
1887 return false;
1888 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001889 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001890 if (RegVT == MVT::v2f64) {
1891 if (!ArgLocs[++i].isRegLoc())
1892 return false;
1893 if (!ArgLocs[++i].isRegLoc())
1894 return false;
1895 }
1896 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001897 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1898 MFI, MRI, TII))
1899 return false;
1900 }
1901 }
1902 }
1903 }
1904
1905 return true;
1906}
1907
Benjamin Kramer350c0082012-11-28 20:55:10 +00001908bool
1909ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1910 MachineFunction &MF, bool isVarArg,
1911 const SmallVectorImpl<ISD::OutputArg> &Outs,
1912 LLVMContext &Context) const {
1913 SmallVector<CCValAssign, 16> RVLocs;
1914 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1915 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1916 isVarArg));
1917}
1918
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919SDValue
1920ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001921 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001922 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001923 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001924 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001925
Bob Wilsondee46d72009-04-17 20:35:10 +00001926 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001928
Bob Wilsondee46d72009-04-17 20:35:10 +00001929 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001930 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1931 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001932
Dan Gohman98ca4f22009-08-05 01:29:28 +00001933 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001934 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1935 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001936
Bob Wilson1f595bb2009-04-17 19:07:39 +00001937 SDValue Flag;
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001938 SmallVector<SDValue, 4> RetOps;
1939 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Bob Wilson1f595bb2009-04-17 19:07:39 +00001940
1941 // Copy the result values into the output registers.
1942 for (unsigned i = 0, realRVLocIdx = 0;
1943 i != RVLocs.size();
1944 ++i, ++realRVLocIdx) {
1945 CCValAssign &VA = RVLocs[i];
1946 assert(VA.isRegLoc() && "Can only return in registers!");
1947
Dan Gohmanc9403652010-07-07 15:54:55 +00001948 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001949
1950 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001951 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001952 case CCValAssign::Full: break;
1953 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001954 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001955 break;
1956 }
1957
Bob Wilson1f595bb2009-04-17 19:07:39 +00001958 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001960 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1962 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001963 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001965
1966 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1967 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001968 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00001969 VA = RVLocs[++i]; // skip ahead to next loc
1970 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1971 HalfGPRs.getValue(1), Flag);
1972 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001973 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00001974 VA = RVLocs[++i]; // skip ahead to next loc
1975
1976 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1978 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001979 }
1980 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1981 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001982 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001984 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001985 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001986 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001987 VA = RVLocs[++i]; // skip ahead to next loc
1988 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1989 Flag);
1990 } else
1991 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1992
Bob Wilsondee46d72009-04-17 20:35:10 +00001993 // Guarantee that all emitted copies are
1994 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001995 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001996 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001997 }
1998
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001999 // Update chain and glue.
2000 RetOps[0] = Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002001 if (Flag.getNode())
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002002 RetOps.push_back(Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002003
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002004 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2005 RetOps.data(), RetOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002006}
2007
Evan Chengbf010eb2012-04-10 01:51:00 +00002008bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002009 if (N->getNumValues() != 1)
2010 return false;
2011 if (!N->hasNUsesOfValue(1, 0))
2012 return false;
2013
Evan Chengbf010eb2012-04-10 01:51:00 +00002014 SDValue TCChain = Chain;
2015 SDNode *Copy = *N->use_begin();
2016 if (Copy->getOpcode() == ISD::CopyToReg) {
2017 // If the copy has a glue operand, we conservatively assume it isn't safe to
2018 // perform a tail call.
2019 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2020 return false;
2021 TCChain = Copy->getOperand(0);
2022 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2023 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002024 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002025 SmallPtrSet<SDNode*, 2> Copies;
2026 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002027 UI != UE; ++UI) {
2028 if (UI->getOpcode() != ISD::CopyToReg)
2029 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002030 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002031 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002032 if (Copies.size() > 2)
2033 return false;
2034
2035 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2036 UI != UE; ++UI) {
2037 SDValue UseChain = UI->getOperand(0);
2038 if (Copies.count(UseChain.getNode()))
2039 // Second CopyToReg
2040 Copy = *UI;
2041 else
2042 // First CopyToReg
2043 TCChain = UseChain;
2044 }
2045 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002046 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002047 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002048 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002049 Copy = *Copy->use_begin();
2050 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002051 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002052 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002053 } else {
2054 return false;
2055 }
2056
Evan Cheng1bf891a2010-12-01 22:59:46 +00002057 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002058 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2059 UI != UE; ++UI) {
2060 if (UI->getOpcode() != ARMISD::RET_FLAG)
2061 return false;
2062 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002063 }
2064
Evan Chengbf010eb2012-04-10 01:51:00 +00002065 if (!HasRet)
2066 return false;
2067
2068 Chain = TCChain;
2069 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002070}
2071
Evan Cheng485fafc2011-03-21 01:19:09 +00002072bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002073 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002074 return false;
2075
2076 if (!CI->isTailCall())
2077 return false;
2078
2079 return !Subtarget->isThumb1Only();
2080}
2081
Bob Wilsonb62d2572009-11-03 00:02:05 +00002082// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2083// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2084// one of the above mentioned nodes. It has to be wrapped because otherwise
2085// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2086// be used to form addressing mode. These wrapped nodes will be selected
2087// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002088static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002089 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002090 // FIXME there is no actual debug info here
2091 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002092 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002093 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002094 if (CP->isMachineConstantPoolEntry())
2095 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2096 CP->getAlignment());
2097 else
2098 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2099 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002101}
2102
Jim Grosbache1102ca2010-07-19 17:20:38 +00002103unsigned ARMTargetLowering::getJumpTableEncoding() const {
2104 return MachineJumpTableInfo::EK_Inline;
2105}
2106
Dan Gohmand858e902010-04-17 15:26:15 +00002107SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2108 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002109 MachineFunction &MF = DAG.getMachineFunction();
2110 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2111 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002112 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002113 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002114 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002115 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2116 SDValue CPAddr;
2117 if (RelocM == Reloc::Static) {
2118 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2119 } else {
2120 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002122 ARMConstantPoolValue *CPV =
2123 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2124 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002125 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2126 }
2127 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2128 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002129 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002130 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002131 if (RelocM == Reloc::Static)
2132 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002133 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002134 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002135}
2136
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002137// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002138SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002139ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002140 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002141 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002142 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002143 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002144 MachineFunction &MF = DAG.getMachineFunction();
2145 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002146 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002147 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002148 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2149 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002150 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002151 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002152 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002153 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002154 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002156
Evan Chenge7e0d622009-11-06 22:24:13 +00002157 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002158 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159
2160 // call __tls_get_addr.
2161 ArgListTy Args;
2162 ArgListEntry Entry;
2163 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002164 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002165 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002166 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002167 TargetLowering::CallLoweringInfo CLI(Chain,
2168 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002169 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002170 0, CallingConv::C, /*isTailCall=*/false,
2171 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002172 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002173 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174 return CallResult.first;
2175}
2176
2177// Lower ISD::GlobalTLSAddress using the "initial exec" or
2178// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002179SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002180ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002181 SelectionDAG &DAG,
2182 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002183 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002184 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002185 SDValue Offset;
2186 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002188 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002189 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002190
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002191 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002192 MachineFunction &MF = DAG.getMachineFunction();
2193 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002194 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002195 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002196 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2197 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002198 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2199 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2200 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002201 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002202 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002203 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002204 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002205 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002206 Chain = Offset.getValue(1);
2207
Evan Chenge7e0d622009-11-06 22:24:13 +00002208 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002209 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002210
Evan Cheng9eda6892009-10-31 03:39:36 +00002211 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002212 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002213 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002214 } else {
2215 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002216 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002217 ARMConstantPoolValue *CPV =
2218 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002219 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002221 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002222 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002223 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002224 }
2225
2226 // The address of the thread local variable is the add of the thread
2227 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002228 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002229}
2230
Dan Gohman475871a2008-07-27 21:46:04 +00002231SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002232ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002233 // TODO: implement the "local dynamic" model
2234 assert(Subtarget->isTargetELF() &&
2235 "TLS not implemented for non-ELF targets");
2236 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002237
2238 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2239
2240 switch (model) {
2241 case TLSModel::GeneralDynamic:
2242 case TLSModel::LocalDynamic:
2243 return LowerToTLSGeneralDynamicModel(GA, DAG);
2244 case TLSModel::InitialExec:
2245 case TLSModel::LocalExec:
2246 return LowerToTLSExecModels(GA, DAG, model);
2247 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002248 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002249}
2250
Dan Gohman475871a2008-07-27 21:46:04 +00002251SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002252 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002253 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002254 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002255 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosiera6ca7032013-02-28 19:16:42 +00002256 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002257 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002258 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002259 ARMConstantPoolConstant::Create(GV,
2260 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002261 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002263 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002264 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002265 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002266 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002268 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002269 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002270 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002271 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002272 MachinePointerInfo::getGOT(),
2273 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002274 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002275 }
2276
2277 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002278 // pair. This is always cheaper.
2279 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002280 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002281 // FIXME: Once remat is capable of dealing with instructions with register
2282 // operands, expand this into two nodes.
2283 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2284 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002285 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002286 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2287 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2288 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2289 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002290 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002291 }
2292}
2293
Dan Gohman475871a2008-07-27 21:46:04 +00002294SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002295 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002296 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002297 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002298 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002299 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002300
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002301 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2302 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002303 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002304 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002305 // FIXME: Once remat is capable of dealing with instructions with register
2306 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002307 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002308 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2309 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2310
Evan Cheng53519f02011-01-21 18:55:51 +00002311 unsigned Wrapper = (RelocM == Reloc::PIC_)
2312 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2313 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002314 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002315 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2316 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002317 MachinePointerInfo::getGOT(),
2318 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002319 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002320 }
2321
2322 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002323 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002324 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002325 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002326 } else {
Chad Rosiera6ca7032013-02-28 19:16:42 +00002327 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002328 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002329 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2330 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002331 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2332 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002333 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002334 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002335 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002336
Evan Cheng9eda6892009-10-31 03:39:36 +00002337 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002338 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002339 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002340 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002341
2342 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002343 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002344 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002345 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002346
Evan Cheng63476a82009-09-03 07:04:02 +00002347 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002348 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002349 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002350
2351 return Result;
2352}
2353
Dan Gohman475871a2008-07-27 21:46:04 +00002354SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002355 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002356 assert(Subtarget->isTargetELF() &&
2357 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002358 MachineFunction &MF = DAG.getMachineFunction();
2359 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002360 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002362 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002363 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002364 ARMConstantPoolValue *CPV =
2365 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2366 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002367 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002368 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002369 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002370 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002371 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002372 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002373 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002374}
2375
Jim Grosbach0e0da732009-05-12 23:59:14 +00002376SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002377ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2378 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002379 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002380 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2381 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002382 Op.getOperand(1), Val);
2383}
2384
2385SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002386ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2387 DebugLoc dl = Op.getDebugLoc();
2388 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2389 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2390}
2391
2392SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002393ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002394 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002395 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002396 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002397 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002398 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002399 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002400 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002401 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2402 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002403 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002404 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002405 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002406 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002407 EVT PtrVT = getPointerTy();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002408 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2409 SDValue CPAddr;
2410 unsigned PCAdj = (RelocM != Reloc::PIC_)
2411 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002412 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002413 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2414 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002415 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002416 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002417 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002418 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002419 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002420 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002421
2422 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002423 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002424 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2425 }
2426 return Result;
2427 }
Evan Cheng92e39162011-03-29 23:06:19 +00002428 case Intrinsic::arm_neon_vmulls:
2429 case Intrinsic::arm_neon_vmullu: {
2430 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2431 ? ARMISD::VMULLs : ARMISD::VMULLu;
2432 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2433 Op.getOperand(1), Op.getOperand(2));
2434 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002435 }
2436}
2437
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002438static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002439 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002440 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002441 if (!Subtarget->hasDataBarrier()) {
2442 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2443 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2444 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002445 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002446 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002447 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002448 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002449 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002450
2451 SDValue Op5 = Op.getOperand(5);
2452 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2453 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2454 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2455 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2456
2457 ARM_MB::MemBOpt DMBOpt;
2458 if (isDeviceBarrier)
2459 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2460 else
2461 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2462 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2463 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002464}
2465
Eli Friedman26689ac2011-08-03 21:06:02 +00002466
2467static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2468 const ARMSubtarget *Subtarget) {
2469 // FIXME: handle "fence singlethread" more efficiently.
2470 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002471 if (!Subtarget->hasDataBarrier()) {
2472 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2473 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2474 // here.
2475 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2476 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002477 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002478 DAG.getConstant(0, MVT::i32));
2479 }
2480
Eli Friedman26689ac2011-08-03 21:06:02 +00002481 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002482 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002483}
2484
Evan Chengdfed19f2010-11-03 06:34:55 +00002485static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2486 const ARMSubtarget *Subtarget) {
2487 // ARM pre v5TE and Thumb1 does not have preload instructions.
2488 if (!(Subtarget->isThumb2() ||
2489 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2490 // Just preserve the chain.
2491 return Op.getOperand(0);
2492
2493 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002494 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2495 if (!isRead &&
2496 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2497 // ARMv7 with MP extension has PLDW.
2498 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002499
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002500 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2501 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002502 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002503 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002504 isData = ~isData & 1;
2505 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002506
2507 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002508 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2509 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002510}
2511
Dan Gohman1e93df62010-04-17 14:41:14 +00002512static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2513 MachineFunction &MF = DAG.getMachineFunction();
2514 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2515
Evan Chenga8e29892007-01-19 07:51:42 +00002516 // vastart just stores the address of the VarArgsFrameIndex slot into the
2517 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002518 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002519 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002520 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002521 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002522 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2523 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002524}
2525
Dan Gohman475871a2008-07-27 21:46:04 +00002526SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002527ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2528 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002529 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002530 MachineFunction &MF = DAG.getMachineFunction();
2531 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2532
Craig Topper44d23822012-02-22 05:59:10 +00002533 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002534 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002535 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002536 else
Craig Topper420761a2012-04-20 07:30:17 +00002537 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002538
2539 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002540 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002542
2543 SDValue ArgValue2;
2544 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002545 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002546 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002547
2548 // Create load node to retrieve arguments from the stack.
2549 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002550 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002551 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002552 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002553 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002554 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002555 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002556 }
2557
Jim Grosbache5165492009-11-09 00:11:35 +00002558 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002559}
2560
Stuart Hastingsc7315872011-04-20 16:47:52 +00002561void
2562ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2563 unsigned &VARegSize, unsigned &VARegSaveSize)
2564 const {
2565 unsigned NumGPRs;
2566 if (CCInfo.isFirstByValRegValid())
2567 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2568 else {
2569 unsigned int firstUnalloced;
2570 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2571 sizeof(GPRArgRegs) /
2572 sizeof(GPRArgRegs[0]));
2573 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2574 }
2575
2576 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2577 VARegSize = NumGPRs * 4;
2578 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2579}
2580
2581// The remaining GPRs hold either the beginning of variable-argument
David Peixottoe68542e2013-02-13 00:36:35 +00002582// data, or the beginning of an aggregate passed by value (usually
Stuart Hastingsc7315872011-04-20 16:47:52 +00002583// byval). Either way, we allocate stack slots adjacent to the data
2584// provided by our caller, and store the unallocated registers there.
2585// If this is a variadic function, the va_list pointer will begin with
2586// these values; otherwise, this reassembles a (byval) structure that
2587// was split between registers and memory.
2588void
2589ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2590 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002591 const Value *OrigArg,
2592 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002593 unsigned ArgOffset,
2594 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002595 MachineFunction &MF = DAG.getMachineFunction();
2596 MachineFrameInfo *MFI = MF.getFrameInfo();
2597 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2598 unsigned firstRegToSaveIndex;
2599 if (CCInfo.isFirstByValRegValid())
2600 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2601 else {
2602 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2603 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2604 }
2605
2606 unsigned VARegSize, VARegSaveSize;
2607 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2608 if (VARegSaveSize) {
2609 // If this function is vararg, store any remaining integer argument regs
2610 // to their spots on the stack so that they may be loaded by deferencing
2611 // the result of va_next.
2612 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002613 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2614 ArgOffset + VARegSaveSize
2615 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002616 false));
2617 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2618 getPointerTy());
2619
2620 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002621 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002622 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002623 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002624 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002625 else
Craig Topper420761a2012-04-20 07:30:17 +00002626 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002627
2628 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2629 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2630 SDValue Store =
2631 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002632 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002633 false, false, 0);
2634 MemOps.push_back(Store);
2635 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2636 DAG.getConstant(4, getPointerTy()));
2637 }
2638 if (!MemOps.empty())
2639 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2640 &MemOps[0], MemOps.size());
2641 } else
2642 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002643 AFI->setVarArgsFrameIndex(
2644 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002645}
2646
Bob Wilson5bafff32009-06-22 23:27:02 +00002647SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002648ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002649 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650 const SmallVectorImpl<ISD::InputArg>
2651 &Ins,
2652 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002653 SmallVectorImpl<SDValue> &InVals)
2654 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002655 MachineFunction &MF = DAG.getMachineFunction();
2656 MachineFrameInfo *MFI = MF.getFrameInfo();
2657
Bob Wilson1f595bb2009-04-17 19:07:39 +00002658 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2659
2660 // Assign locations to all of the incoming arguments.
2661 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002662 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2663 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002665 CCAssignFnForNode(CallConv, /* Return*/ false,
2666 isVarArg));
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002667
Bob Wilson1f595bb2009-04-17 19:07:39 +00002668 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002669 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002670 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002671 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2672 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002673 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2674 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002675 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2676 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002677 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002678 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002679 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002680
Bob Wilson1f595bb2009-04-17 19:07:39 +00002681 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002682 // f64 and vector types are split up into multiple registers or
2683 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002684 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002685 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002686 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002687 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002688 SDValue ArgValue2;
2689 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002690 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002691 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2692 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002693 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002694 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002695 } else {
2696 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2697 Chain, DAG, dl);
2698 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002699 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2700 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002701 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002702 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002703 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2704 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002705 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002706
Bob Wilson5bafff32009-06-22 23:27:02 +00002707 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002708 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002709
Owen Anderson825b72b2009-08-11 20:47:22 +00002710 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002711 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002712 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002713 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002715 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002716 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002717 RC = AFI->isThumb1OnlyFunction() ?
2718 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2719 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002720 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002721 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002722
2723 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002724 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002726 }
2727
2728 // If this is an 8 or 16-bit value, it is really passed promoted
2729 // to 32 bits. Insert an assert[sz]ext to capture this, then
2730 // truncate to the right size.
2731 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002732 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002733 case CCValAssign::Full: break;
2734 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002735 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002736 break;
2737 case CCValAssign::SExt:
2738 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2739 DAG.getValueType(VA.getValVT()));
2740 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2741 break;
2742 case CCValAssign::ZExt:
2743 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2744 DAG.getValueType(VA.getValVT()));
2745 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2746 break;
2747 }
2748
Dan Gohman98ca4f22009-08-05 01:29:28 +00002749 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002750
2751 } else { // VA.isRegLoc()
2752
2753 // sanity check
2754 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002755 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002756
Stuart Hastingsf222e592011-02-28 17:17:53 +00002757 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002758
Stuart Hastingsf222e592011-02-28 17:17:53 +00002759 // Some Ins[] entries become multiple ArgLoc[] entries.
2760 // Process them only once.
2761 if (index != lastInsIndex)
2762 {
2763 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002764 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002765 // This can be changed with more analysis.
2766 // In case of tail call optimization mark all arguments mutable.
2767 // Since they could be overwritten by lowering of arguments in case of
2768 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002769 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002770 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2771 if (!AFI->getVarArgsFrameIndex()) {
2772 VarArgStyleRegisters(CCInfo, DAG,
2773 dl, Chain, CurOrigArg,
2774 Ins[VA.getValNo()].PartOffset,
2775 VA.getLocMemOffset(),
2776 true /*force mutable frames*/);
2777 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2778 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2779 } else {
2780 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2781 VA.getLocMemOffset(), false);
2782 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2783 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002784 } else {
2785 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2786 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002787
Stuart Hastingsf222e592011-02-28 17:17:53 +00002788 // Create load nodes to retrieve arguments from the stack.
2789 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2790 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2791 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002792 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002793 }
2794 lastInsIndex = index;
2795 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002796 }
2797 }
2798
2799 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002800 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002801 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2802 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002803
Dan Gohman98ca4f22009-08-05 01:29:28 +00002804 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002805}
2806
2807/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002808static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002809 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002810 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002811 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002812 // Maybe this has already been legalized into the constant pool?
2813 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002814 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002815 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002816 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002817 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002818 }
2819 }
2820 return false;
2821}
2822
Evan Chenga8e29892007-01-19 07:51:42 +00002823/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2824/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002825SDValue
2826ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002827 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002828 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002829 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002830 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002831 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002832 // Constant does not fit, try adjusting it by one?
2833 switch (CC) {
2834 default: break;
2835 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002836 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002837 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002838 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
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::SETULT:
2843 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002844 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002845 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
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 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002850 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002851 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002852 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002853 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002854 }
2855 break;
2856 case ISD::SETULE:
2857 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002858 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002859 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002860 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002861 }
2862 break;
2863 }
2864 }
2865 }
2866
2867 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002868 ARMISD::NodeType CompareType;
2869 switch (CondCode) {
2870 default:
2871 CompareType = ARMISD::CMP;
2872 break;
2873 case ARMCC::EQ:
2874 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002875 // Uses only Z Flag
2876 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002877 break;
2878 }
Evan Cheng218977b2010-07-13 19:27:42 +00002879 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002880 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002881}
2882
2883/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002884SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002885ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002886 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002887 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002888 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002889 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002890 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002891 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2892 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002893}
2894
Bob Wilson79f56c92011-03-08 01:17:20 +00002895/// duplicateCmp - Glue values can have only one use, so this function
2896/// duplicates a comparison node.
2897SDValue
2898ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2899 unsigned Opc = Cmp.getOpcode();
2900 DebugLoc DL = Cmp.getDebugLoc();
2901 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2902 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2903
2904 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2905 Cmp = Cmp.getOperand(0);
2906 Opc = Cmp.getOpcode();
2907 if (Opc == ARMISD::CMPFP)
2908 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2909 else {
2910 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2911 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2912 }
2913 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2914}
2915
Bill Wendlingde2b1512010-08-11 08:43:16 +00002916SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2917 SDValue Cond = Op.getOperand(0);
2918 SDValue SelectTrue = Op.getOperand(1);
2919 SDValue SelectFalse = Op.getOperand(2);
2920 DebugLoc dl = Op.getDebugLoc();
2921
2922 // Convert:
2923 //
2924 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2925 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2926 //
2927 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2928 const ConstantSDNode *CMOVTrue =
2929 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2930 const ConstantSDNode *CMOVFalse =
2931 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2932
2933 if (CMOVTrue && CMOVFalse) {
2934 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2935 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2936
2937 SDValue True;
2938 SDValue False;
2939 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2940 True = SelectTrue;
2941 False = SelectFalse;
2942 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2943 True = SelectFalse;
2944 False = SelectTrue;
2945 }
2946
2947 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002948 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002949 SDValue ARMcc = Cond.getOperand(2);
2950 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002951 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002952 assert(True.getValueType() == VT);
2953 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002954 }
2955 }
2956 }
2957
Dan Gohmandb953892012-02-24 00:09:36 +00002958 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2959 // undefined bits before doing a full-word comparison with zero.
2960 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2961 DAG.getConstant(1, Cond.getValueType()));
2962
Bill Wendlingde2b1512010-08-11 08:43:16 +00002963 return DAG.getSelectCC(dl, Cond,
2964 DAG.getConstant(0, Cond.getValueType()),
2965 SelectTrue, SelectFalse, ISD::SETNE);
2966}
2967
Dan Gohmand858e902010-04-17 15:26:15 +00002968SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002969 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002970 SDValue LHS = Op.getOperand(0);
2971 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002972 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002973 SDValue TrueVal = Op.getOperand(2);
2974 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002975 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002976
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002978 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002980 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002981 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002982 }
2983
2984 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002985 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002986
Evan Cheng218977b2010-07-13 19:27:42 +00002987 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2988 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002989 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002990 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002991 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002992 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002993 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002994 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002995 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002996 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002997 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002998 }
2999 return Result;
3000}
3001
Evan Cheng218977b2010-07-13 19:27:42 +00003002/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3003/// to morph to an integer compare sequence.
3004static bool canChangeToInt(SDValue Op, bool &SeenZero,
3005 const ARMSubtarget *Subtarget) {
3006 SDNode *N = Op.getNode();
3007 if (!N->hasOneUse())
3008 // Otherwise it requires moving the value from fp to integer registers.
3009 return false;
3010 if (!N->getNumValues())
3011 return false;
3012 EVT VT = Op.getValueType();
3013 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3014 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3015 // vmrs are very slow, e.g. cortex-a8.
3016 return false;
3017
3018 if (isFloatingPointZero(Op)) {
3019 SeenZero = true;
3020 return true;
3021 }
3022 return ISD::isNormalLoad(N);
3023}
3024
3025static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3026 if (isFloatingPointZero(Op))
3027 return DAG.getConstant(0, MVT::i32);
3028
3029 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3030 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003031 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003032 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003033 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003034
3035 llvm_unreachable("Unknown VFP cmp argument!");
3036}
3037
3038static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3039 SDValue &RetVal1, SDValue &RetVal2) {
3040 if (isFloatingPointZero(Op)) {
3041 RetVal1 = DAG.getConstant(0, MVT::i32);
3042 RetVal2 = DAG.getConstant(0, MVT::i32);
3043 return;
3044 }
3045
3046 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3047 SDValue Ptr = Ld->getBasePtr();
3048 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3049 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003050 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003051 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003052 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003053
3054 EVT PtrType = Ptr.getValueType();
3055 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3056 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3057 PtrType, Ptr, DAG.getConstant(4, PtrType));
3058 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3059 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003060 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003061 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003062 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003063 return;
3064 }
3065
3066 llvm_unreachable("Unknown VFP cmp argument!");
3067}
3068
3069/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3070/// f32 and even f64 comparisons to integer ones.
3071SDValue
3072ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3073 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003074 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003075 SDValue LHS = Op.getOperand(2);
3076 SDValue RHS = Op.getOperand(3);
3077 SDValue Dest = Op.getOperand(4);
3078 DebugLoc dl = Op.getDebugLoc();
3079
Evan Chengfc501a32012-03-01 23:27:13 +00003080 bool LHSSeenZero = false;
3081 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3082 bool RHSSeenZero = false;
3083 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3084 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003085 // If unsafe fp math optimization is enabled and there are no other uses of
3086 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003087 // to an integer comparison.
3088 if (CC == ISD::SETOEQ)
3089 CC = ISD::SETEQ;
3090 else if (CC == ISD::SETUNE)
3091 CC = ISD::SETNE;
3092
Evan Chengfc501a32012-03-01 23:27:13 +00003093 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003094 SDValue ARMcc;
3095 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003096 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3097 bitcastf32Toi32(LHS, DAG), Mask);
3098 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3099 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003100 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3101 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3102 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3103 Chain, Dest, ARMcc, CCR, Cmp);
3104 }
3105
3106 SDValue LHS1, LHS2;
3107 SDValue RHS1, RHS2;
3108 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3109 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003110 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3111 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003112 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3113 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003114 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003115 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3116 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3117 }
3118
3119 return SDValue();
3120}
3121
3122SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3123 SDValue Chain = Op.getOperand(0);
3124 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3125 SDValue LHS = Op.getOperand(2);
3126 SDValue RHS = Op.getOperand(3);
3127 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003128 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003129
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003131 SDValue ARMcc;
3132 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003135 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003136 }
3137
Owen Anderson825b72b2009-08-11 20:47:22 +00003138 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003139
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003140 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003141 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3142 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3143 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3144 if (Result.getNode())
3145 return Result;
3146 }
3147
Evan Chenga8e29892007-01-19 07:51:42 +00003148 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003149 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003150
Evan Cheng218977b2010-07-13 19:27:42 +00003151 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3152 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003154 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003155 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003156 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003157 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003158 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3159 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003160 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003161 }
3162 return Res;
3163}
3164
Dan Gohmand858e902010-04-17 15:26:15 +00003165SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003166 SDValue Chain = Op.getOperand(0);
3167 SDValue Table = Op.getOperand(1);
3168 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003169 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003170
Owen Andersone50ed302009-08-10 22:56:29 +00003171 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003172 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3173 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003174 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003175 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003176 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003177 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3178 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003179 if (Subtarget->isThumb2()) {
3180 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3181 // which does another jump to the destination. This also makes it easier
3182 // to translate it to TBB / TBH later.
3183 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003185 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003186 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003187 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003188 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003189 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003190 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003191 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003192 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003194 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003195 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003196 MachinePointerInfo::getJumpTable(),
3197 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003198 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003200 }
Evan Chenga8e29892007-01-19 07:51:42 +00003201}
3202
Eli Friedman14e809c2011-11-09 23:36:02 +00003203static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003204 EVT VT = Op.getValueType();
3205 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003206
James Molloy873fd5f2012-02-20 09:24:05 +00003207 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3208 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3209 return Op;
3210 return DAG.UnrollVectorOp(Op.getNode());
3211 }
3212
3213 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3214 "Invalid type for custom lowering!");
3215 if (VT != MVT::v4i16)
3216 return DAG.UnrollVectorOp(Op.getNode());
3217
3218 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3219 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003220}
3221
Bob Wilson76a312b2010-03-19 22:51:32 +00003222static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003223 EVT VT = Op.getValueType();
3224 if (VT.isVector())
3225 return LowerVectorFP_TO_INT(Op, DAG);
3226
Bob Wilson76a312b2010-03-19 22:51:32 +00003227 DebugLoc dl = Op.getDebugLoc();
3228 unsigned Opc;
3229
3230 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003231 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003232 case ISD::FP_TO_SINT:
3233 Opc = ARMISD::FTOSI;
3234 break;
3235 case ISD::FP_TO_UINT:
3236 Opc = ARMISD::FTOUI;
3237 break;
3238 }
3239 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003240 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003241}
3242
Cameron Zwarich3007d332011-03-29 21:41:55 +00003243static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3244 EVT VT = Op.getValueType();
3245 DebugLoc dl = Op.getDebugLoc();
3246
Eli Friedman14e809c2011-11-09 23:36:02 +00003247 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3248 if (VT.getVectorElementType() == MVT::f32)
3249 return Op;
3250 return DAG.UnrollVectorOp(Op.getNode());
3251 }
3252
Duncan Sands1f6a3292011-08-12 14:54:45 +00003253 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3254 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003255 if (VT != MVT::v4f32)
3256 return DAG.UnrollVectorOp(Op.getNode());
3257
3258 unsigned CastOpc;
3259 unsigned Opc;
3260 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003261 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003262 case ISD::SINT_TO_FP:
3263 CastOpc = ISD::SIGN_EXTEND;
3264 Opc = ISD::SINT_TO_FP;
3265 break;
3266 case ISD::UINT_TO_FP:
3267 CastOpc = ISD::ZERO_EXTEND;
3268 Opc = ISD::UINT_TO_FP;
3269 break;
3270 }
3271
3272 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3273 return DAG.getNode(Opc, dl, VT, Op);
3274}
3275
Bob Wilson76a312b2010-03-19 22:51:32 +00003276static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3277 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003278 if (VT.isVector())
3279 return LowerVectorINT_TO_FP(Op, DAG);
3280
Bob Wilson76a312b2010-03-19 22:51:32 +00003281 DebugLoc dl = Op.getDebugLoc();
3282 unsigned Opc;
3283
3284 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003285 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003286 case ISD::SINT_TO_FP:
3287 Opc = ARMISD::SITOF;
3288 break;
3289 case ISD::UINT_TO_FP:
3290 Opc = ARMISD::UITOF;
3291 break;
3292 }
3293
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003294 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003295 return DAG.getNode(Opc, dl, VT, Op);
3296}
3297
Evan Cheng515fe3a2010-07-08 02:08:50 +00003298SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003299 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003300 SDValue Tmp0 = Op.getOperand(0);
3301 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003302 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003303 EVT VT = Op.getValueType();
3304 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003305 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3306 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3307 bool UseNEON = !InGPR && Subtarget->hasNEON();
3308
3309 if (UseNEON) {
3310 // Use VBSL to copy the sign bit.
3311 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3312 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3313 DAG.getTargetConstant(EncodedVal, MVT::i32));
3314 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3315 if (VT == MVT::f64)
3316 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3317 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3318 DAG.getConstant(32, MVT::i32));
3319 else /*if (VT == MVT::f32)*/
3320 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3321 if (SrcVT == MVT::f32) {
3322 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3323 if (VT == MVT::f64)
3324 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3325 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3326 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003327 } else if (VT == MVT::f32)
3328 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3329 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3330 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003331 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3332 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3333
3334 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3335 MVT::i32);
3336 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3337 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3338 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003339
Evan Chenge573fb32011-02-23 02:24:55 +00003340 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3341 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3342 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003343 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003344 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3345 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3346 DAG.getConstant(0, MVT::i32));
3347 } else {
3348 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3349 }
3350
3351 return Res;
3352 }
Evan Chengc143dd42011-02-11 02:28:55 +00003353
3354 // Bitcast operand 1 to i32.
3355 if (SrcVT == MVT::f64)
3356 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3357 &Tmp1, 1).getValue(1);
3358 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3359
Evan Chenge573fb32011-02-23 02:24:55 +00003360 // Or in the signbit with integer operations.
3361 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3362 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3363 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3364 if (VT == MVT::f32) {
3365 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3366 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3367 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3368 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003369 }
3370
Evan Chenge573fb32011-02-23 02:24:55 +00003371 // f64: Or the high part with signbit and then combine two parts.
3372 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3373 &Tmp0, 1);
3374 SDValue Lo = Tmp0.getValue(0);
3375 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3376 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3377 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003378}
3379
Evan Cheng2457f2c2010-05-22 01:47:14 +00003380SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3381 MachineFunction &MF = DAG.getMachineFunction();
3382 MachineFrameInfo *MFI = MF.getFrameInfo();
3383 MFI->setReturnAddressIsTaken(true);
3384
3385 EVT VT = Op.getValueType();
3386 DebugLoc dl = Op.getDebugLoc();
3387 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3388 if (Depth) {
3389 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3390 SDValue Offset = DAG.getConstant(4, MVT::i32);
3391 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3392 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003393 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003394 }
3395
3396 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003397 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003398 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3399}
3400
Dan Gohmand858e902010-04-17 15:26:15 +00003401SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003402 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3403 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003404
Owen Andersone50ed302009-08-10 22:56:29 +00003405 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003406 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3407 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003408 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003409 ? ARM::R7 : ARM::R11;
3410 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3411 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003412 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3413 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003414 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003415 return FrameAddr;
3416}
3417
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003418/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003419/// expand a bit convert where either the source or destination type is i64 to
3420/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3421/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3422/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003423static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003424 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3425 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003426 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003427
Bob Wilson9f3f0612010-04-17 05:30:19 +00003428 // This function is only supposed to be called for i64 types, either as the
3429 // source or destination of the bit convert.
3430 EVT SrcVT = Op.getValueType();
3431 EVT DstVT = N->getValueType(0);
3432 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003433 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003434
Bob Wilson9f3f0612010-04-17 05:30:19 +00003435 // Turn i64->f64 into VMOVDRR.
3436 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3438 DAG.getConstant(0, MVT::i32));
3439 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3440 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003441 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003442 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003443 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003444
Jim Grosbache5165492009-11-09 00:11:35 +00003445 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003446 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3447 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3448 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3449 // Merge the pieces into a single i64 value.
3450 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3451 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003452
Bob Wilson9f3f0612010-04-17 05:30:19 +00003453 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003454}
3455
Bob Wilson5bafff32009-06-22 23:27:02 +00003456/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003457/// Zero vectors are used to represent vector negation and in those cases
3458/// will be implemented with the NEON VNEG instruction. However, VNEG does
3459/// not support i64 elements, so sometimes the zero vectors will need to be
3460/// explicitly constructed. Regardless, use a canonical VMOV to create the
3461/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003462static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003463 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003464 // The canonical modified immediate encoding of a zero vector is....0!
3465 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3466 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3467 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003468 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003469}
3470
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003471/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3472/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003473SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3474 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003475 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3476 EVT VT = Op.getValueType();
3477 unsigned VTBits = VT.getSizeInBits();
3478 DebugLoc dl = Op.getDebugLoc();
3479 SDValue ShOpLo = Op.getOperand(0);
3480 SDValue ShOpHi = Op.getOperand(1);
3481 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003482 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003483 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003484
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003485 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3486
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003487 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3488 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3489 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3490 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3491 DAG.getConstant(VTBits, MVT::i32));
3492 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3493 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003494 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003495
3496 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3497 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003498 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003499 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003500 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003501 CCR, Cmp);
3502
3503 SDValue Ops[2] = { Lo, Hi };
3504 return DAG.getMergeValues(Ops, 2, dl);
3505}
3506
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003507/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3508/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003509SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3510 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003511 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3512 EVT VT = Op.getValueType();
3513 unsigned VTBits = VT.getSizeInBits();
3514 DebugLoc dl = Op.getDebugLoc();
3515 SDValue ShOpLo = Op.getOperand(0);
3516 SDValue ShOpHi = Op.getOperand(1);
3517 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003518 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003519
3520 assert(Op.getOpcode() == ISD::SHL_PARTS);
3521 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3522 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3523 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3524 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3525 DAG.getConstant(VTBits, MVT::i32));
3526 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3527 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3528
3529 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3530 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3531 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003532 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003533 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003534 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003535 CCR, Cmp);
3536
3537 SDValue Ops[2] = { Lo, Hi };
3538 return DAG.getMergeValues(Ops, 2, dl);
3539}
3540
Jim Grosbach4725ca72010-09-08 03:54:02 +00003541SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003542 SelectionDAG &DAG) const {
3543 // The rounding mode is in bits 23:22 of the FPSCR.
3544 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3545 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3546 // so that the shift + and get folded into a bitfield extract.
3547 DebugLoc dl = Op.getDebugLoc();
3548 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3549 DAG.getConstant(Intrinsic::arm_get_fpscr,
3550 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003551 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003552 DAG.getConstant(1U << 22, MVT::i32));
3553 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3554 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003555 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003556 DAG.getConstant(3, MVT::i32));
3557}
3558
Jim Grosbach3482c802010-01-18 19:58:49 +00003559static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3560 const ARMSubtarget *ST) {
3561 EVT VT = N->getValueType(0);
3562 DebugLoc dl = N->getDebugLoc();
3563
3564 if (!ST->hasV6T2Ops())
3565 return SDValue();
3566
3567 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3568 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3569}
3570
Evan Chengc8e70452012-12-04 22:41:50 +00003571/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3572/// for each 16-bit element from operand, repeated. The basic idea is to
3573/// leverage vcnt to get the 8-bit counts, gather and add the results.
3574///
3575/// Trace for v4i16:
3576/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3577/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3578/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
3579/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
3580/// [b0 b1 b2 b3 b4 b5 b6 b7]
3581/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3582/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3583/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3584static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3585 EVT VT = N->getValueType(0);
3586 DebugLoc DL = N->getDebugLoc();
3587
3588 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3589 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3590 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3591 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3592 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3593 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3594}
3595
3596/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3597/// bit-count for each 16-bit element from the operand. We need slightly
3598/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3599/// 64/128-bit registers.
3600///
3601/// Trace for v4i16:
3602/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3603/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3604/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3605/// v4i16:Extracted = [k0 k1 k2 k3 ]
3606static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3607 EVT VT = N->getValueType(0);
3608 DebugLoc DL = N->getDebugLoc();
3609
3610 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3611 if (VT.is64BitVector()) {
3612 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3613 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3614 DAG.getIntPtrConstant(0));
3615 } else {
3616 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3617 BitCounts, DAG.getIntPtrConstant(0));
3618 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3619 }
3620}
3621
3622/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3623/// bit-count for each 32-bit element from the operand. The idea here is
3624/// to split the vector into 16-bit elements, leverage the 16-bit count
3625/// routine, and then combine the results.
3626///
3627/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3628/// input = [v0 v1 ] (vi: 32-bit elements)
3629/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3630/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
3631/// vrev: N0 = [k1 k0 k3 k2 ]
3632/// [k0 k1 k2 k3 ]
3633/// N1 =+[k1 k0 k3 k2 ]
3634/// [k0 k2 k1 k3 ]
3635/// N2 =+[k1 k3 k0 k2 ]
3636/// [k0 k2 k1 k3 ]
3637/// Extended =+[k1 k3 k0 k2 ]
3638/// [k0 k2 ]
3639/// Extracted=+[k1 k3 ]
3640///
3641static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3642 EVT VT = N->getValueType(0);
3643 DebugLoc DL = N->getDebugLoc();
3644
3645 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3646
3647 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3648 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3649 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3650 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3651 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3652
3653 if (VT.is64BitVector()) {
3654 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3655 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3656 DAG.getIntPtrConstant(0));
3657 } else {
3658 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3659 DAG.getIntPtrConstant(0));
3660 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3661 }
3662}
3663
3664static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3665 const ARMSubtarget *ST) {
3666 EVT VT = N->getValueType(0);
3667
3668 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay105ab4f2012-12-04 23:54:02 +00003669 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3670 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengc8e70452012-12-04 22:41:50 +00003671 "Unexpected type for custom ctpop lowering");
3672
3673 if (VT.getVectorElementType() == MVT::i32)
3674 return lowerCTPOP32BitElements(N, DAG);
3675 else
3676 return lowerCTPOP16BitElements(N, DAG);
3677}
3678
Bob Wilson5bafff32009-06-22 23:27:02 +00003679static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3680 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003681 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003682 DebugLoc dl = N->getDebugLoc();
3683
Bob Wilsond5448bb2010-11-18 21:16:28 +00003684 if (!VT.isVector())
3685 return SDValue();
3686
Bob Wilson5bafff32009-06-22 23:27:02 +00003687 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003688 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003689
Bob Wilsond5448bb2010-11-18 21:16:28 +00003690 // Left shifts translate directly to the vshiftu intrinsic.
3691 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003692 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003693 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3694 N->getOperand(0), N->getOperand(1));
3695
3696 assert((N->getOpcode() == ISD::SRA ||
3697 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3698
3699 // NEON uses the same intrinsics for both left and right shifts. For
3700 // right shifts, the shift amounts are negative, so negate the vector of
3701 // shift amounts.
3702 EVT ShiftVT = N->getOperand(1).getValueType();
3703 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3704 getZeroVector(ShiftVT, DAG, dl),
3705 N->getOperand(1));
3706 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3707 Intrinsic::arm_neon_vshifts :
3708 Intrinsic::arm_neon_vshiftu);
3709 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3710 DAG.getConstant(vshiftInt, MVT::i32),
3711 N->getOperand(0), NegatedCount);
3712}
3713
3714static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3715 const ARMSubtarget *ST) {
3716 EVT VT = N->getValueType(0);
3717 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003718
Eli Friedmance392eb2009-08-22 03:13:10 +00003719 // We can get here for a node like i32 = ISD::SHL i32, i64
3720 if (VT != MVT::i64)
3721 return SDValue();
3722
3723 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003724 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003725
Chris Lattner27a6c732007-11-24 07:07:01 +00003726 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3727 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003728 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003729 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003730
Chris Lattner27a6c732007-11-24 07:07:01 +00003731 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003732 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003733
Chris Lattner27a6c732007-11-24 07:07:01 +00003734 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003736 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003738 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003739
Chris Lattner27a6c732007-11-24 07:07:01 +00003740 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3741 // captures the result into a carry flag.
3742 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003743 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003744
Chris Lattner27a6c732007-11-24 07:07:01 +00003745 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003747
Chris Lattner27a6c732007-11-24 07:07:01 +00003748 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003750}
3751
Bob Wilson5bafff32009-06-22 23:27:02 +00003752static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3753 SDValue TmpOp0, TmpOp1;
3754 bool Invert = false;
3755 bool Swap = false;
3756 unsigned Opc = 0;
3757
3758 SDValue Op0 = Op.getOperand(0);
3759 SDValue Op1 = Op.getOperand(1);
3760 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003761 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003762 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3763 DebugLoc dl = Op.getDebugLoc();
3764
3765 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3766 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003767 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003768 case ISD::SETUNE:
3769 case ISD::SETNE: Invert = true; // Fallthrough
3770 case ISD::SETOEQ:
3771 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3772 case ISD::SETOLT:
3773 case ISD::SETLT: Swap = true; // Fallthrough
3774 case ISD::SETOGT:
3775 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3776 case ISD::SETOLE:
3777 case ISD::SETLE: Swap = true; // Fallthrough
3778 case ISD::SETOGE:
3779 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3780 case ISD::SETUGE: Swap = true; // Fallthrough
3781 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3782 case ISD::SETUGT: Swap = true; // Fallthrough
3783 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3784 case ISD::SETUEQ: Invert = true; // Fallthrough
3785 case ISD::SETONE:
3786 // Expand this to (OLT | OGT).
3787 TmpOp0 = Op0;
3788 TmpOp1 = Op1;
3789 Opc = ISD::OR;
3790 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3791 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3792 break;
3793 case ISD::SETUO: Invert = true; // Fallthrough
3794 case ISD::SETO:
3795 // Expand this to (OLT | OGE).
3796 TmpOp0 = Op0;
3797 TmpOp1 = Op1;
3798 Opc = ISD::OR;
3799 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3800 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3801 break;
3802 }
3803 } else {
3804 // Integer comparisons.
3805 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003806 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003807 case ISD::SETNE: Invert = true;
3808 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3809 case ISD::SETLT: Swap = true;
3810 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3811 case ISD::SETLE: Swap = true;
3812 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3813 case ISD::SETULT: Swap = true;
3814 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3815 case ISD::SETULE: Swap = true;
3816 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3817 }
3818
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003819 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003820 if (Opc == ARMISD::VCEQ) {
3821
3822 SDValue AndOp;
3823 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3824 AndOp = Op0;
3825 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3826 AndOp = Op1;
3827
3828 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003829 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003830 AndOp = AndOp.getOperand(0);
3831
3832 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3833 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003834 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3835 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003836 Invert = !Invert;
3837 }
3838 }
3839 }
3840
3841 if (Swap)
3842 std::swap(Op0, Op1);
3843
Owen Andersonc24cb352010-11-08 23:21:22 +00003844 // If one of the operands is a constant vector zero, attempt to fold the
3845 // comparison to a specialized compare-against-zero form.
3846 SDValue SingleOp;
3847 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3848 SingleOp = Op0;
3849 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3850 if (Opc == ARMISD::VCGE)
3851 Opc = ARMISD::VCLEZ;
3852 else if (Opc == ARMISD::VCGT)
3853 Opc = ARMISD::VCLTZ;
3854 SingleOp = Op1;
3855 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003856
Owen Andersonc24cb352010-11-08 23:21:22 +00003857 SDValue Result;
3858 if (SingleOp.getNode()) {
3859 switch (Opc) {
3860 case ARMISD::VCEQ:
3861 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3862 case ARMISD::VCGE:
3863 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3864 case ARMISD::VCLEZ:
3865 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3866 case ARMISD::VCGT:
3867 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3868 case ARMISD::VCLTZ:
3869 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3870 default:
3871 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3872 }
3873 } else {
3874 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3875 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003876
3877 if (Invert)
3878 Result = DAG.getNOT(dl, Result, VT);
3879
3880 return Result;
3881}
3882
Bob Wilsond3c42842010-06-14 22:19:57 +00003883/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3884/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003885/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003886static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3887 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003888 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003889 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003890
Bob Wilson827b2102010-06-15 19:05:35 +00003891 // SplatBitSize is set to the smallest size that splats the vector, so a
3892 // zero vector will always have SplatBitSize == 8. However, NEON modified
3893 // immediate instructions others than VMOV do not support the 8-bit encoding
3894 // of a zero vector, and the default encoding of zero is supposed to be the
3895 // 32-bit version.
3896 if (SplatBits == 0)
3897 SplatBitSize = 32;
3898
Bob Wilson5bafff32009-06-22 23:27:02 +00003899 switch (SplatBitSize) {
3900 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003901 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003902 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003903 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003904 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003905 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003906 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003907 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003908 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003909
3910 case 16:
3911 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003912 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003913 if ((SplatBits & ~0xff) == 0) {
3914 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003915 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003916 Imm = SplatBits;
3917 break;
3918 }
3919 if ((SplatBits & ~0xff00) == 0) {
3920 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003921 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003922 Imm = SplatBits >> 8;
3923 break;
3924 }
3925 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003926
3927 case 32:
3928 // NEON's 32-bit VMOV supports splat values where:
3929 // * only one byte is nonzero, or
3930 // * the least significant byte is 0xff and the second byte is nonzero, or
3931 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003932 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003933 if ((SplatBits & ~0xff) == 0) {
3934 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003935 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003936 Imm = SplatBits;
3937 break;
3938 }
3939 if ((SplatBits & ~0xff00) == 0) {
3940 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003941 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003942 Imm = SplatBits >> 8;
3943 break;
3944 }
3945 if ((SplatBits & ~0xff0000) == 0) {
3946 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003947 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003948 Imm = SplatBits >> 16;
3949 break;
3950 }
3951 if ((SplatBits & ~0xff000000) == 0) {
3952 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003953 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003954 Imm = SplatBits >> 24;
3955 break;
3956 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003957
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003958 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3959 if (type == OtherModImm) return SDValue();
3960
Bob Wilson5bafff32009-06-22 23:27:02 +00003961 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003962 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3963 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003964 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003965 Imm = SplatBits >> 8;
3966 SplatBits |= 0xff;
3967 break;
3968 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003969
3970 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003971 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3972 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003973 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003974 Imm = SplatBits >> 16;
3975 SplatBits |= 0xffff;
3976 break;
3977 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003978
3979 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3980 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3981 // VMOV.I32. A (very) minor optimization would be to replicate the value
3982 // and fall through here to test for a valid 64-bit splat. But, then the
3983 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003984 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003985
3986 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003987 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003988 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003989 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003990 uint64_t BitMask = 0xff;
3991 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003992 unsigned ImmMask = 1;
3993 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003994 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003995 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003996 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003997 Imm |= ImmMask;
3998 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003999 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00004000 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004001 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004002 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00004003 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00004004 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004005 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004006 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00004007 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00004008 break;
4009 }
4010
Bob Wilson1a913ed2010-06-11 21:34:50 +00004011 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00004012 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00004013 }
4014
Bob Wilsoncba270d2010-07-13 21:16:48 +00004015 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4016 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00004017}
4018
Lang Hamesc0a9f822012-03-29 21:56:11 +00004019SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4020 const ARMSubtarget *ST) const {
4021 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
4022 return SDValue();
4023
4024 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4025 assert(Op.getValueType() == MVT::f32 &&
4026 "ConstantFP custom lowering should only occur for f32.");
4027
4028 // Try splatting with a VMOV.f32...
4029 APFloat FPVal = CFP->getValueAPF();
4030 int ImmVal = ARM_AM::getFP32Imm(FPVal);
4031 if (ImmVal != -1) {
4032 DebugLoc DL = Op.getDebugLoc();
4033 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4034 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4035 NewVal);
4036 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4037 DAG.getConstant(0, MVT::i32));
4038 }
4039
4040 // If that fails, try a VMOV.i32
4041 EVT VMovVT;
4042 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
4043 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
4044 VMOVModImm);
4045 if (NewVal != SDValue()) {
4046 DebugLoc DL = Op.getDebugLoc();
4047 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4048 NewVal);
4049 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4050 VecConstant);
4051 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4052 DAG.getConstant(0, MVT::i32));
4053 }
4054
4055 // Finally, try a VMVN.i32
4056 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
4057 VMVNModImm);
4058 if (NewVal != SDValue()) {
4059 DebugLoc DL = Op.getDebugLoc();
4060 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4061 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4062 VecConstant);
4063 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4064 DAG.getConstant(0, MVT::i32));
4065 }
4066
4067 return SDValue();
4068}
4069
Quentin Colombet43934ae2012-11-02 21:32:17 +00004070// check if an VEXT instruction can handle the shuffle mask when the
4071// vector sources of the shuffle are the same.
4072static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4073 unsigned NumElts = VT.getVectorNumElements();
4074
4075 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4076 if (M[0] < 0)
4077 return false;
4078
4079 Imm = M[0];
4080
4081 // If this is a VEXT shuffle, the immediate value is the index of the first
4082 // element. The other shuffle indices must be the successive elements after
4083 // the first one.
4084 unsigned ExpectedElt = Imm;
4085 for (unsigned i = 1; i < NumElts; ++i) {
4086 // Increment the expected index. If it wraps around, just follow it
4087 // back to index zero and keep going.
4088 ++ExpectedElt;
4089 if (ExpectedElt == NumElts)
4090 ExpectedElt = 0;
4091
4092 if (M[i] < 0) continue; // ignore UNDEF indices
4093 if (ExpectedElt != static_cast<unsigned>(M[i]))
4094 return false;
4095 }
4096
4097 return true;
4098}
4099
Lang Hamesc0a9f822012-03-29 21:56:11 +00004100
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004101static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004102 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004103 unsigned NumElts = VT.getVectorNumElements();
4104 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004105
4106 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4107 if (M[0] < 0)
4108 return false;
4109
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004110 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004111
4112 // If this is a VEXT shuffle, the immediate value is the index of the first
4113 // element. The other shuffle indices must be the successive elements after
4114 // the first one.
4115 unsigned ExpectedElt = Imm;
4116 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004117 // Increment the expected index. If it wraps around, it may still be
4118 // a VEXT but the source vectors must be swapped.
4119 ExpectedElt += 1;
4120 if (ExpectedElt == NumElts * 2) {
4121 ExpectedElt = 0;
4122 ReverseVEXT = true;
4123 }
4124
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004125 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004126 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004127 return false;
4128 }
4129
4130 // Adjust the index value if the source operands will be swapped.
4131 if (ReverseVEXT)
4132 Imm -= NumElts;
4133
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004134 return true;
4135}
4136
Bob Wilson8bb9e482009-07-26 00:39:34 +00004137/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4138/// instruction with the specified blocksize. (The order of the elements
4139/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004140static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004141 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4142 "Only possible block sizes for VREV are: 16, 32, 64");
4143
Bob Wilson8bb9e482009-07-26 00:39:34 +00004144 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004145 if (EltSz == 64)
4146 return false;
4147
4148 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004149 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004150 // If the first shuffle index is UNDEF, be optimistic.
4151 if (M[0] < 0)
4152 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004153
4154 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4155 return false;
4156
4157 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004158 if (M[i] < 0) continue; // ignore UNDEF indices
4159 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004160 return false;
4161 }
4162
4163 return true;
4164}
4165
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004166static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004167 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4168 // range, then 0 is placed into the resulting vector. So pretty much any mask
4169 // of 8 elements can work here.
4170 return VT == MVT::v8i8 && M.size() == 8;
4171}
4172
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004173static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004174 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4175 if (EltSz == 64)
4176 return false;
4177
Bob Wilsonc692cb72009-08-21 20:54:19 +00004178 unsigned NumElts = VT.getVectorNumElements();
4179 WhichResult = (M[0] == 0 ? 0 : 1);
4180 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004181 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4182 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004183 return false;
4184 }
4185 return true;
4186}
4187
Bob Wilson324f4f12009-12-03 06:40:55 +00004188/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4189/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4190/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004191static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004192 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4193 if (EltSz == 64)
4194 return false;
4195
4196 unsigned NumElts = VT.getVectorNumElements();
4197 WhichResult = (M[0] == 0 ? 0 : 1);
4198 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004199 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4200 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004201 return false;
4202 }
4203 return true;
4204}
4205
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004206static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004207 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4208 if (EltSz == 64)
4209 return false;
4210
Bob Wilsonc692cb72009-08-21 20:54:19 +00004211 unsigned NumElts = VT.getVectorNumElements();
4212 WhichResult = (M[0] == 0 ? 0 : 1);
4213 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004214 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004215 if ((unsigned) M[i] != 2 * i + WhichResult)
4216 return false;
4217 }
4218
4219 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004220 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004221 return false;
4222
4223 return true;
4224}
4225
Bob Wilson324f4f12009-12-03 06:40:55 +00004226/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4227/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4228/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004229static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004230 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4231 if (EltSz == 64)
4232 return false;
4233
4234 unsigned Half = VT.getVectorNumElements() / 2;
4235 WhichResult = (M[0] == 0 ? 0 : 1);
4236 for (unsigned j = 0; j != 2; ++j) {
4237 unsigned Idx = WhichResult;
4238 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004239 int MIdx = M[i + j * Half];
4240 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004241 return false;
4242 Idx += 2;
4243 }
4244 }
4245
4246 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4247 if (VT.is64BitVector() && EltSz == 32)
4248 return false;
4249
4250 return true;
4251}
4252
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004253static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004254 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4255 if (EltSz == 64)
4256 return false;
4257
Bob Wilsonc692cb72009-08-21 20:54:19 +00004258 unsigned NumElts = VT.getVectorNumElements();
4259 WhichResult = (M[0] == 0 ? 0 : 1);
4260 unsigned Idx = WhichResult * NumElts / 2;
4261 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004262 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4263 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004264 return false;
4265 Idx += 1;
4266 }
4267
4268 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004269 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004270 return false;
4271
4272 return true;
4273}
4274
Bob Wilson324f4f12009-12-03 06:40:55 +00004275/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4276/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4277/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004278static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004279 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4280 if (EltSz == 64)
4281 return false;
4282
4283 unsigned NumElts = VT.getVectorNumElements();
4284 WhichResult = (M[0] == 0 ? 0 : 1);
4285 unsigned Idx = WhichResult * NumElts / 2;
4286 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004287 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4288 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004289 return false;
4290 Idx += 1;
4291 }
4292
4293 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4294 if (VT.is64BitVector() && EltSz == 32)
4295 return false;
4296
4297 return true;
4298}
4299
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004300/// \return true if this is a reverse operation on an vector.
4301static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4302 unsigned NumElts = VT.getVectorNumElements();
4303 // Make sure the mask has the right size.
4304 if (NumElts != M.size())
4305 return false;
4306
4307 // Look for <15, ..., 3, -1, 1, 0>.
4308 for (unsigned i = 0; i != NumElts; ++i)
4309 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4310 return false;
4311
4312 return true;
4313}
4314
Dale Johannesenf630c712010-07-29 20:10:08 +00004315// If N is an integer constant that can be moved into a register in one
4316// instruction, return an SDValue of such a constant (will become a MOV
4317// instruction). Otherwise return null.
4318static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4319 const ARMSubtarget *ST, DebugLoc dl) {
4320 uint64_t Val;
4321 if (!isa<ConstantSDNode>(N))
4322 return SDValue();
4323 Val = cast<ConstantSDNode>(N)->getZExtValue();
4324
4325 if (ST->isThumb1Only()) {
4326 if (Val <= 255 || ~Val <= 255)
4327 return DAG.getConstant(Val, MVT::i32);
4328 } else {
4329 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4330 return DAG.getConstant(Val, MVT::i32);
4331 }
4332 return SDValue();
4333}
4334
Bob Wilson5bafff32009-06-22 23:27:02 +00004335// If this is a case we can't handle, return null and let the default
4336// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004337SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4338 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004339 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004340 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004341 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004342
4343 APInt SplatBits, SplatUndef;
4344 unsigned SplatBitSize;
4345 bool HasAnyUndefs;
4346 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004347 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004348 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004349 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004350 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004351 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004352 DAG, VmovVT, VT.is128BitVector(),
4353 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004354 if (Val.getNode()) {
4355 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004356 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004357 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004358
4359 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004360 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004361 Val = isNEONModifiedImm(NegatedImm,
4362 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004363 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004364 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004365 if (Val.getNode()) {
4366 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004367 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004368 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004369
4370 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004371 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004372 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004373 if (ImmVal != -1) {
4374 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4375 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4376 }
4377 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004378 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004379 }
4380
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004381 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004382 //
4383 // As an optimisation, even if more than one value is used it may be more
4384 // profitable to splat with one value then change some lanes.
4385 //
4386 // Heuristically we decide to do this if the vector has a "dominant" value,
4387 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004388 unsigned NumElts = VT.getVectorNumElements();
4389 bool isOnlyLowElement = true;
4390 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004391 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004392 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004393
4394 // Map of the number of times a particular SDValue appears in the
4395 // element list.
James Molloy95154342012-09-06 10:32:08 +00004396 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004397 SDValue Value;
4398 for (unsigned i = 0; i < NumElts; ++i) {
4399 SDValue V = Op.getOperand(i);
4400 if (V.getOpcode() == ISD::UNDEF)
4401 continue;
4402 if (i > 0)
4403 isOnlyLowElement = false;
4404 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4405 isConstant = false;
4406
James Molloyba8562a2012-09-06 09:55:02 +00004407 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004408 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004409
4410 // Is this value dominant? (takes up more than half of the lanes)
4411 if (++Count > (NumElts / 2)) {
4412 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004413 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004414 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004415 }
James Molloyba8562a2012-09-06 09:55:02 +00004416 if (ValueCounts.size() != 1)
4417 usesOnlyOneValue = false;
4418 if (!Value.getNode() && ValueCounts.size() > 0)
4419 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004420
James Molloyba8562a2012-09-06 09:55:02 +00004421 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004422 return DAG.getUNDEF(VT);
4423
4424 if (isOnlyLowElement)
4425 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4426
Dale Johannesenf630c712010-07-29 20:10:08 +00004427 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4428
Dale Johannesen575cd142010-10-19 20:00:17 +00004429 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4430 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004431 if (hasDominantValue && EltSize <= 32) {
4432 if (!isConstant) {
4433 SDValue N;
4434
4435 // If we are VDUPing a value that comes directly from a vector, that will
4436 // cause an unnecessary move to and from a GPR, where instead we could
4437 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004438 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4439 // We need to create a new undef vector to use for the VDUPLANE if the
4440 // size of the vector from which we get the value is different than the
4441 // size of the vector that we need to create. We will insert the element
4442 // such that the register coalescer will remove unnecessary copies.
4443 if (VT != Value->getOperand(0).getValueType()) {
4444 ConstantSDNode *constIndex;
4445 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4446 assert(constIndex && "The index is not a constant!");
4447 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4448 VT.getVectorNumElements();
4449 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4450 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4451 Value, DAG.getConstant(index, MVT::i32)),
4452 DAG.getConstant(index, MVT::i32));
4453 } else {
4454 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004455 Value->getOperand(0), Value->getOperand(1));
Silviu Barangabb1078e2012-10-15 09:41:32 +00004456 }
4457 }
James Molloyba8562a2012-09-06 09:55:02 +00004458 else
4459 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4460
4461 if (!usesOnlyOneValue) {
4462 // The dominant value was splatted as 'N', but we now have to insert
4463 // all differing elements.
4464 for (unsigned I = 0; I < NumElts; ++I) {
4465 if (Op.getOperand(I) == Value)
4466 continue;
4467 SmallVector<SDValue, 3> Ops;
4468 Ops.push_back(N);
4469 Ops.push_back(Op.getOperand(I));
4470 Ops.push_back(DAG.getConstant(I, MVT::i32));
4471 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4472 }
4473 }
4474 return N;
4475 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004476 if (VT.getVectorElementType().isFloatingPoint()) {
4477 SmallVector<SDValue, 8> Ops;
4478 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004479 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004480 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004481 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4482 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004483 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4484 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004485 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004486 }
James Molloyba8562a2012-09-06 09:55:02 +00004487 if (usesOnlyOneValue) {
4488 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4489 if (isConstant && Val.getNode())
4490 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4491 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004492 }
4493
4494 // If all elements are constants and the case above didn't get hit, fall back
4495 // to the default expansion, which will generate a load from the constant
4496 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004497 if (isConstant)
4498 return SDValue();
4499
Bob Wilson11a1dff2011-01-07 21:37:30 +00004500 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4501 if (NumElts >= 4) {
4502 SDValue shuffle = ReconstructShuffle(Op, DAG);
4503 if (shuffle != SDValue())
4504 return shuffle;
4505 }
4506
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004507 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004508 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4509 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004510 if (EltSize >= 32) {
4511 // Do the expansion with floating-point types, since that is what the VFP
4512 // registers are defined to use, and since i64 is not legal.
4513 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4514 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004515 SmallVector<SDValue, 8> Ops;
4516 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004517 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004518 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004519 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004520 }
4521
4522 return SDValue();
4523}
4524
Bob Wilson11a1dff2011-01-07 21:37:30 +00004525// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004526// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004527SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4528 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004529 DebugLoc dl = Op.getDebugLoc();
4530 EVT VT = Op.getValueType();
4531 unsigned NumElts = VT.getVectorNumElements();
4532
4533 SmallVector<SDValue, 2> SourceVecs;
4534 SmallVector<unsigned, 2> MinElts;
4535 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004536
Bob Wilson11a1dff2011-01-07 21:37:30 +00004537 for (unsigned i = 0; i < NumElts; ++i) {
4538 SDValue V = Op.getOperand(i);
4539 if (V.getOpcode() == ISD::UNDEF)
4540 continue;
4541 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4542 // A shuffle can only come from building a vector from various
4543 // elements of other vectors.
4544 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004545 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4546 VT.getVectorElementType()) {
4547 // This code doesn't know how to handle shuffles where the vector
4548 // element types do not match (this happens because type legalization
4549 // promotes the return type of EXTRACT_VECTOR_ELT).
4550 // FIXME: It might be appropriate to extend this code to handle
4551 // mismatched types.
4552 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004553 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004554
Bob Wilson11a1dff2011-01-07 21:37:30 +00004555 // Record this extraction against the appropriate vector if possible...
4556 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004557 // If the element number isn't a constant, we can't effectively
4558 // analyze what's going on.
4559 if (!isa<ConstantSDNode>(V.getOperand(1)))
4560 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004561 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4562 bool FoundSource = false;
4563 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4564 if (SourceVecs[j] == SourceVec) {
4565 if (MinElts[j] > EltNo)
4566 MinElts[j] = EltNo;
4567 if (MaxElts[j] < EltNo)
4568 MaxElts[j] = EltNo;
4569 FoundSource = true;
4570 break;
4571 }
4572 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004573
Bob Wilson11a1dff2011-01-07 21:37:30 +00004574 // Or record a new source if not...
4575 if (!FoundSource) {
4576 SourceVecs.push_back(SourceVec);
4577 MinElts.push_back(EltNo);
4578 MaxElts.push_back(EltNo);
4579 }
4580 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004581
Bob Wilson11a1dff2011-01-07 21:37:30 +00004582 // Currently only do something sane when at most two source vectors
4583 // involved.
4584 if (SourceVecs.size() > 2)
4585 return SDValue();
4586
4587 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4588 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004589
Bob Wilson11a1dff2011-01-07 21:37:30 +00004590 // This loop extracts the usage patterns of the source vectors
4591 // and prepares appropriate SDValues for a shuffle if possible.
4592 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4593 if (SourceVecs[i].getValueType() == VT) {
4594 // No VEXT necessary
4595 ShuffleSrcs[i] = SourceVecs[i];
4596 VEXTOffsets[i] = 0;
4597 continue;
4598 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4599 // It probably isn't worth padding out a smaller vector just to
4600 // break it down again in a shuffle.
4601 return SDValue();
4602 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004603
Bob Wilson11a1dff2011-01-07 21:37:30 +00004604 // Since only 64-bit and 128-bit vectors are legal on ARM and
4605 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004606 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4607 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004608
Bob Wilson11a1dff2011-01-07 21:37:30 +00004609 if (MaxElts[i] - MinElts[i] >= NumElts) {
4610 // Span too large for a VEXT to cope
4611 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004612 }
4613
Bob Wilson11a1dff2011-01-07 21:37:30 +00004614 if (MinElts[i] >= NumElts) {
4615 // The extraction can just take the second half
4616 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004617 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4618 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004619 DAG.getIntPtrConstant(NumElts));
4620 } else if (MaxElts[i] < NumElts) {
4621 // The extraction can just take the first half
4622 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004623 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4624 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004625 DAG.getIntPtrConstant(0));
4626 } else {
4627 // An actual VEXT is needed
4628 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004629 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4630 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004631 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004632 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4633 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004634 DAG.getIntPtrConstant(NumElts));
4635 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4636 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4637 }
4638 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004639
Bob Wilson11a1dff2011-01-07 21:37:30 +00004640 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004641
Bob Wilson11a1dff2011-01-07 21:37:30 +00004642 for (unsigned i = 0; i < NumElts; ++i) {
4643 SDValue Entry = Op.getOperand(i);
4644 if (Entry.getOpcode() == ISD::UNDEF) {
4645 Mask.push_back(-1);
4646 continue;
4647 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004648
Bob Wilson11a1dff2011-01-07 21:37:30 +00004649 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004650 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4651 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004652 if (ExtractVec == SourceVecs[0]) {
4653 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4654 } else {
4655 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4656 }
4657 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004658
Bob Wilson11a1dff2011-01-07 21:37:30 +00004659 // Final check before we try to produce nonsense...
4660 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004661 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4662 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004663
Bob Wilson11a1dff2011-01-07 21:37:30 +00004664 return SDValue();
4665}
4666
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004667/// isShuffleMaskLegal - Targets can use this to indicate that they only
4668/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4669/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4670/// are assumed to be legal.
4671bool
4672ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4673 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004674 if (VT.getVectorNumElements() == 4 &&
4675 (VT.is128BitVector() || VT.is64BitVector())) {
4676 unsigned PFIndexes[4];
4677 for (unsigned i = 0; i != 4; ++i) {
4678 if (M[i] < 0)
4679 PFIndexes[i] = 8;
4680 else
4681 PFIndexes[i] = M[i];
4682 }
4683
4684 // Compute the index in the perfect shuffle table.
4685 unsigned PFTableIndex =
4686 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4687 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4688 unsigned Cost = (PFEntry >> 30);
4689
4690 if (Cost <= 4)
4691 return true;
4692 }
4693
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004694 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004695 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004696
Bob Wilson53dd2452010-06-07 23:53:38 +00004697 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4698 return (EltSize >= 32 ||
4699 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004700 isVREVMask(M, VT, 64) ||
4701 isVREVMask(M, VT, 32) ||
4702 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004703 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004704 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004705 isVTRNMask(M, VT, WhichResult) ||
4706 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004707 isVZIPMask(M, VT, WhichResult) ||
4708 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4709 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004710 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
4711 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004712}
4713
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004714/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4715/// the specified operations to build the shuffle.
4716static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4717 SDValue RHS, SelectionDAG &DAG,
4718 DebugLoc dl) {
4719 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4720 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4721 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4722
4723 enum {
4724 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4725 OP_VREV,
4726 OP_VDUP0,
4727 OP_VDUP1,
4728 OP_VDUP2,
4729 OP_VDUP3,
4730 OP_VEXT1,
4731 OP_VEXT2,
4732 OP_VEXT3,
4733 OP_VUZPL, // VUZP, left result
4734 OP_VUZPR, // VUZP, right result
4735 OP_VZIPL, // VZIP, left result
4736 OP_VZIPR, // VZIP, right result
4737 OP_VTRNL, // VTRN, left result
4738 OP_VTRNR // VTRN, right result
4739 };
4740
4741 if (OpNum == OP_COPY) {
4742 if (LHSID == (1*9+2)*9+3) return LHS;
4743 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4744 return RHS;
4745 }
4746
4747 SDValue OpLHS, OpRHS;
4748 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4749 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4750 EVT VT = OpLHS.getValueType();
4751
4752 switch (OpNum) {
4753 default: llvm_unreachable("Unknown shuffle opcode!");
4754 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004755 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004756 if (VT.getVectorElementType() == MVT::i32 ||
4757 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004758 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4759 // vrev <4 x i16> -> VREV32
4760 if (VT.getVectorElementType() == MVT::i16)
4761 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4762 // vrev <4 x i8> -> VREV16
4763 assert(VT.getVectorElementType() == MVT::i8);
4764 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004765 case OP_VDUP0:
4766 case OP_VDUP1:
4767 case OP_VDUP2:
4768 case OP_VDUP3:
4769 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004770 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004771 case OP_VEXT1:
4772 case OP_VEXT2:
4773 case OP_VEXT3:
4774 return DAG.getNode(ARMISD::VEXT, dl, VT,
4775 OpLHS, OpRHS,
4776 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4777 case OP_VUZPL:
4778 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004779 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004780 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4781 case OP_VZIPL:
4782 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004783 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004784 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4785 case OP_VTRNL:
4786 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004787 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4788 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004789 }
4790}
4791
Bill Wendling69a05a72011-03-14 23:02:38 +00004792static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004793 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004794 SelectionDAG &DAG) {
4795 // Check to see if we can use the VTBL instruction.
4796 SDValue V1 = Op.getOperand(0);
4797 SDValue V2 = Op.getOperand(1);
4798 DebugLoc DL = Op.getDebugLoc();
4799
4800 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004801 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004802 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4803 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4804
4805 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4806 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4807 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4808 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004809
Owen Anderson76706012011-04-05 21:48:57 +00004810 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004811 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4812 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004813}
4814
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004815static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
4816 SelectionDAG &DAG) {
4817 DebugLoc DL = Op.getDebugLoc();
4818 SDValue OpLHS = Op.getOperand(0);
4819 EVT VT = OpLHS.getValueType();
4820
4821 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
4822 "Expect an v8i16/v16i8 type");
4823 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
4824 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
4825 // extract the first 8 bytes into the top double word and the last 8 bytes
4826 // into the bottom double word. The v8i16 case is similar.
4827 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
4828 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
4829 DAG.getConstant(ExtractNum, MVT::i32));
4830}
4831
Bob Wilson5bafff32009-06-22 23:27:02 +00004832static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004833 SDValue V1 = Op.getOperand(0);
4834 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004835 DebugLoc dl = Op.getDebugLoc();
4836 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004837 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004838
Bob Wilson28865062009-08-13 02:13:04 +00004839 // Convert shuffles that are directly supported on NEON to target-specific
4840 // DAG nodes, instead of keeping them as shuffles and matching them again
4841 // during code selection. This is more efficient and avoids the possibility
4842 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004843 // FIXME: floating-point vectors should be canonicalized to integer vectors
4844 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004845 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004846
Bob Wilson53dd2452010-06-07 23:53:38 +00004847 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4848 if (EltSize <= 32) {
4849 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4850 int Lane = SVN->getSplatIndex();
4851 // If this is undef splat, generate it via "just" vdup, if possible.
4852 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004853
Dan Gohman65fd6562011-11-03 21:49:52 +00004854 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004855 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4856 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4857 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004858 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4859 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4860 // reaches it).
4861 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4862 !isa<ConstantSDNode>(V1.getOperand(0))) {
4863 bool IsScalarToVector = true;
4864 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4865 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4866 IsScalarToVector = false;
4867 break;
4868 }
4869 if (IsScalarToVector)
4870 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4871 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004872 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4873 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004874 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004875
4876 bool ReverseVEXT;
4877 unsigned Imm;
4878 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4879 if (ReverseVEXT)
4880 std::swap(V1, V2);
4881 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4882 DAG.getConstant(Imm, MVT::i32));
4883 }
4884
4885 if (isVREVMask(ShuffleMask, VT, 64))
4886 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4887 if (isVREVMask(ShuffleMask, VT, 32))
4888 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4889 if (isVREVMask(ShuffleMask, VT, 16))
4890 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4891
Quentin Colombet43934ae2012-11-02 21:32:17 +00004892 if (V2->getOpcode() == ISD::UNDEF &&
4893 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4894 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4895 DAG.getConstant(Imm, MVT::i32));
4896 }
4897
Bob Wilson53dd2452010-06-07 23:53:38 +00004898 // Check for Neon shuffles that modify both input vectors in place.
4899 // If both results are used, i.e., if there are two shuffles with the same
4900 // source operands and with masks corresponding to both results of one of
4901 // these operations, DAG memoization will ensure that a single node is
4902 // used for both shuffles.
4903 unsigned WhichResult;
4904 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4905 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4906 V1, V2).getValue(WhichResult);
4907 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4908 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4909 V1, V2).getValue(WhichResult);
4910 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4911 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4912 V1, V2).getValue(WhichResult);
4913
4914 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4915 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4916 V1, V1).getValue(WhichResult);
4917 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4918 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4919 V1, V1).getValue(WhichResult);
4920 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4921 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4922 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004923 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004924
Bob Wilsonc692cb72009-08-21 20:54:19 +00004925 // If the shuffle is not directly supported and it has 4 elements, use
4926 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004927 unsigned NumElts = VT.getVectorNumElements();
4928 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004929 unsigned PFIndexes[4];
4930 for (unsigned i = 0; i != 4; ++i) {
4931 if (ShuffleMask[i] < 0)
4932 PFIndexes[i] = 8;
4933 else
4934 PFIndexes[i] = ShuffleMask[i];
4935 }
4936
4937 // Compute the index in the perfect shuffle table.
4938 unsigned PFTableIndex =
4939 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004940 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4941 unsigned Cost = (PFEntry >> 30);
4942
4943 if (Cost <= 4)
4944 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4945 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004946
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004947 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004948 if (EltSize >= 32) {
4949 // Do the expansion with floating-point types, since that is what the VFP
4950 // registers are defined to use, and since i64 is not legal.
4951 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4952 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004953 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4954 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004955 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004956 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004957 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004958 Ops.push_back(DAG.getUNDEF(EltVT));
4959 else
4960 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4961 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4962 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4963 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004964 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004965 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004966 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004967 }
4968
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004969 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
4970 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
4971
Bill Wendling69a05a72011-03-14 23:02:38 +00004972 if (VT == MVT::v8i8) {
4973 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4974 if (NewOp.getNode())
4975 return NewOp;
4976 }
4977
Bob Wilson22cac0d2009-08-14 05:16:33 +00004978 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004979}
4980
Eli Friedman5c89cb82011-10-24 23:08:52 +00004981static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4982 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4983 SDValue Lane = Op.getOperand(2);
4984 if (!isa<ConstantSDNode>(Lane))
4985 return SDValue();
4986
4987 return Op;
4988}
4989
Bob Wilson5bafff32009-06-22 23:27:02 +00004990static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004991 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004992 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004993 if (!isa<ConstantSDNode>(Lane))
4994 return SDValue();
4995
4996 SDValue Vec = Op.getOperand(0);
4997 if (Op.getValueType() == MVT::i32 &&
4998 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4999 DebugLoc dl = Op.getDebugLoc();
5000 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5001 }
5002
5003 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00005004}
5005
Bob Wilsona6d65862009-08-03 20:36:38 +00005006static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5007 // The only time a CONCAT_VECTORS operation can have legal types is when
5008 // two 64-bit vectors are concatenated to a 128-bit vector.
5009 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5010 "unexpected CONCAT_VECTORS");
5011 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005012 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00005013 SDValue Op0 = Op.getOperand(0);
5014 SDValue Op1 = Op.getOperand(1);
5015 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005016 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005017 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00005018 DAG.getIntPtrConstant(0));
5019 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005020 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005021 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00005022 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005023 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00005024}
5025
Bob Wilson626613d2010-11-23 19:38:38 +00005026/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5027/// element has been zero/sign-extended, depending on the isSigned parameter,
5028/// from an integer type half its size.
5029static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5030 bool isSigned) {
5031 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5032 EVT VT = N->getValueType(0);
5033 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5034 SDNode *BVN = N->getOperand(0).getNode();
5035 if (BVN->getValueType(0) != MVT::v4i32 ||
5036 BVN->getOpcode() != ISD::BUILD_VECTOR)
5037 return false;
5038 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5039 unsigned HiElt = 1 - LoElt;
5040 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5041 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5042 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5043 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5044 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5045 return false;
5046 if (isSigned) {
5047 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5048 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5049 return true;
5050 } else {
5051 if (Hi0->isNullValue() && Hi1->isNullValue())
5052 return true;
5053 }
5054 return false;
5055 }
5056
5057 if (N->getOpcode() != ISD::BUILD_VECTOR)
5058 return false;
5059
5060 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5061 SDNode *Elt = N->getOperand(i).getNode();
5062 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5063 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5064 unsigned HalfSize = EltSize / 2;
5065 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00005066 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005067 return false;
5068 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00005069 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005070 return false;
5071 }
5072 continue;
5073 }
5074 return false;
5075 }
5076
5077 return true;
5078}
5079
5080/// isSignExtended - Check if a node is a vector value that is sign-extended
5081/// or a constant BUILD_VECTOR with sign-extended elements.
5082static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5083 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5084 return true;
5085 if (isExtendedBUILD_VECTOR(N, DAG, true))
5086 return true;
5087 return false;
5088}
5089
5090/// isZeroExtended - Check if a node is a vector value that is zero-extended
5091/// or a constant BUILD_VECTOR with zero-extended elements.
5092static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5093 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5094 return true;
5095 if (isExtendedBUILD_VECTOR(N, DAG, false))
5096 return true;
5097 return false;
5098}
5099
Sebastian Popcb495302012-11-30 19:08:04 +00005100/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5101/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5102/// We insert the required extension here to get the vector to fill a D register.
5103static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5104 const EVT &OrigTy,
5105 const EVT &ExtTy,
5106 unsigned ExtOpcode) {
5107 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5108 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5109 // 64-bits we need to insert a new extension so that it will be 64-bits.
5110 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5111 if (OrigTy.getSizeInBits() >= 64)
5112 return N;
5113
5114 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5115 MVT::SimpleValueType OrigSimpleTy = OrigTy.getSimpleVT().SimpleTy;
5116 EVT NewVT;
5117 switch (OrigSimpleTy) {
5118 default: llvm_unreachable("Unexpected Orig Vector Type");
5119 case MVT::v2i8:
5120 case MVT::v2i16:
5121 NewVT = MVT::v2i32;
5122 break;
5123 case MVT::v4i8:
5124 NewVT = MVT::v4i16;
5125 break;
5126 }
5127 return DAG.getNode(ExtOpcode, N->getDebugLoc(), NewVT, N);
5128}
5129
5130/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5131/// does not do any sign/zero extension. If the original vector is less
5132/// than 64 bits, an appropriate extension will be added after the load to
5133/// reach a total size of 64 bits. We have to add the extension separately
5134/// because ARM does not have a sign/zero extending load for vectors.
5135static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5136 SDValue NonExtendingLoad =
5137 DAG.getLoad(LD->getMemoryVT(), LD->getDebugLoc(), LD->getChain(),
5138 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5139 LD->isNonTemporal(), LD->isInvariant(),
5140 LD->getAlignment());
5141 unsigned ExtOp = 0;
5142 switch (LD->getExtensionType()) {
5143 default: llvm_unreachable("Unexpected LoadExtType");
5144 case ISD::EXTLOAD:
5145 case ISD::SEXTLOAD: ExtOp = ISD::SIGN_EXTEND; break;
5146 case ISD::ZEXTLOAD: ExtOp = ISD::ZERO_EXTEND; break;
5147 }
5148 MVT::SimpleValueType MemType = LD->getMemoryVT().getSimpleVT().SimpleTy;
5149 MVT::SimpleValueType ExtType = LD->getValueType(0).getSimpleVT().SimpleTy;
5150 return AddRequiredExtensionForVMULL(NonExtendingLoad, DAG,
5151 MemType, ExtType, ExtOp);
5152}
5153
5154/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5155/// extending load, or BUILD_VECTOR with extended elements, return the
5156/// unextended value. The unextended vector should be 64 bits so that it can
5157/// be used as an operand to a VMULL instruction. If the original vector size
5158/// before extension is less than 64 bits we add a an extension to resize
5159/// the vector to 64 bits.
5160static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005161 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popcb495302012-11-30 19:08:04 +00005162 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5163 N->getOperand(0)->getValueType(0),
5164 N->getValueType(0),
5165 N->getOpcode());
5166
Bob Wilson626613d2010-11-23 19:38:38 +00005167 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popcb495302012-11-30 19:08:04 +00005168 return SkipLoadExtensionForVMULL(LD, DAG);
5169
Bob Wilson626613d2010-11-23 19:38:38 +00005170 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5171 // have been legalized as a BITCAST from v4i32.
5172 if (N->getOpcode() == ISD::BITCAST) {
5173 SDNode *BVN = N->getOperand(0).getNode();
5174 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5175 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5176 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5177 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
5178 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5179 }
5180 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5181 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5182 EVT VT = N->getValueType(0);
5183 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5184 unsigned NumElts = VT.getVectorNumElements();
5185 MVT TruncVT = MVT::getIntegerVT(EltSize);
5186 SmallVector<SDValue, 8> Ops;
5187 for (unsigned i = 0; i != NumElts; ++i) {
5188 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5189 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00005190 // Element types smaller than 32 bits are not legal, so use i32 elements.
5191 // The values are implicitly truncated so sext vs. zext doesn't matter.
5192 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00005193 }
5194 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5195 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005196}
5197
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005198static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5199 unsigned Opcode = N->getOpcode();
5200 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5201 SDNode *N0 = N->getOperand(0).getNode();
5202 SDNode *N1 = N->getOperand(1).getNode();
5203 return N0->hasOneUse() && N1->hasOneUse() &&
5204 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5205 }
5206 return false;
5207}
5208
5209static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5210 unsigned Opcode = N->getOpcode();
5211 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5212 SDNode *N0 = N->getOperand(0).getNode();
5213 SDNode *N1 = N->getOperand(1).getNode();
5214 return N0->hasOneUse() && N1->hasOneUse() &&
5215 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5216 }
5217 return false;
5218}
5219
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005220static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5221 // Multiplications are only custom-lowered for 128-bit vectors so that
5222 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5223 EVT VT = Op.getValueType();
Sebastian Popcb495302012-11-30 19:08:04 +00005224 assert(VT.is128BitVector() && VT.isInteger() &&
5225 "unexpected type for custom-lowering ISD::MUL");
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005226 SDNode *N0 = Op.getOperand(0).getNode();
5227 SDNode *N1 = Op.getOperand(1).getNode();
5228 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005229 bool isMLA = false;
5230 bool isN0SExt = isSignExtended(N0, DAG);
5231 bool isN1SExt = isSignExtended(N1, DAG);
5232 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005233 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005234 else {
5235 bool isN0ZExt = isZeroExtended(N0, DAG);
5236 bool isN1ZExt = isZeroExtended(N1, DAG);
5237 if (isN0ZExt && isN1ZExt)
5238 NewOpc = ARMISD::VMULLu;
5239 else if (isN1SExt || isN1ZExt) {
5240 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5241 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5242 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5243 NewOpc = ARMISD::VMULLs;
5244 isMLA = true;
5245 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5246 NewOpc = ARMISD::VMULLu;
5247 isMLA = true;
5248 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5249 std::swap(N0, N1);
5250 NewOpc = ARMISD::VMULLu;
5251 isMLA = true;
5252 }
5253 }
5254
5255 if (!NewOpc) {
5256 if (VT == MVT::v2i64)
5257 // Fall through to expand this. It is not legal.
5258 return SDValue();
5259 else
5260 // Other vector multiplications are legal.
5261 return Op;
5262 }
5263 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005264
5265 // Legalize to a VMULL instruction.
5266 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005267 SDValue Op0;
Sebastian Popcb495302012-11-30 19:08:04 +00005268 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005269 if (!isMLA) {
Sebastian Popcb495302012-11-30 19:08:04 +00005270 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005271 assert(Op0.getValueType().is64BitVector() &&
5272 Op1.getValueType().is64BitVector() &&
5273 "unexpected types for extended operands to VMULL");
5274 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5275 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005276
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005277 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5278 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5279 // vmull q0, d4, d6
5280 // vmlal q0, d5, d6
5281 // is faster than
5282 // vaddl q0, d4, d5
5283 // vmovl q1, d6
5284 // vmul q0, q0, q1
Sebastian Popcb495302012-11-30 19:08:04 +00005285 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5286 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005287 EVT Op1VT = Op1.getValueType();
5288 return DAG.getNode(N0->getOpcode(), DL, VT,
5289 DAG.getNode(NewOpc, DL, VT,
5290 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5291 DAG.getNode(NewOpc, DL, VT,
5292 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005293}
5294
Owen Anderson76706012011-04-05 21:48:57 +00005295static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005296LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5297 // Convert to float
5298 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5299 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5300 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5301 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5302 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5303 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5304 // Get reciprocal estimate.
5305 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005306 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005307 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5308 // Because char has a smaller range than uchar, we can actually get away
5309 // without any newton steps. This requires that we use a weird bias
5310 // of 0xb000, however (again, this has been exhaustively tested).
5311 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5312 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5313 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5314 Y = DAG.getConstant(0xb000, MVT::i32);
5315 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5316 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5317 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5318 // Convert back to short.
5319 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5320 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5321 return X;
5322}
5323
Owen Anderson76706012011-04-05 21:48:57 +00005324static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005325LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5326 SDValue N2;
5327 // Convert to float.
5328 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5329 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5330 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5331 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5332 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5333 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005334
Nate Begeman7973f352011-02-11 20:53:29 +00005335 // Use reciprocal estimate and one refinement step.
5336 // float4 recip = vrecpeq_f32(yf);
5337 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005338 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005339 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005340 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005341 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5342 N1, N2);
5343 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5344 // Because short has a smaller range than ushort, we can actually get away
5345 // with only a single newton step. This requires that we use a weird bias
5346 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005347 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005348 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5349 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005350 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005351 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5352 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5353 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5354 // Convert back to integer and return.
5355 // return vmovn_s32(vcvt_s32_f32(result));
5356 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5357 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5358 return N0;
5359}
5360
5361static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5362 EVT VT = Op.getValueType();
5363 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5364 "unexpected type for custom-lowering ISD::SDIV");
5365
5366 DebugLoc dl = Op.getDebugLoc();
5367 SDValue N0 = Op.getOperand(0);
5368 SDValue N1 = Op.getOperand(1);
5369 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005370
Nate Begeman7973f352011-02-11 20:53:29 +00005371 if (VT == MVT::v8i8) {
5372 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5373 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005374
Nate Begeman7973f352011-02-11 20:53:29 +00005375 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5376 DAG.getIntPtrConstant(4));
5377 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005378 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005379 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5380 DAG.getIntPtrConstant(0));
5381 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5382 DAG.getIntPtrConstant(0));
5383
5384 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5385 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5386
5387 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5388 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005389
Nate Begeman7973f352011-02-11 20:53:29 +00005390 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5391 return N0;
5392 }
5393 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5394}
5395
5396static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5397 EVT VT = Op.getValueType();
5398 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5399 "unexpected type for custom-lowering ISD::UDIV");
5400
5401 DebugLoc dl = Op.getDebugLoc();
5402 SDValue N0 = Op.getOperand(0);
5403 SDValue N1 = Op.getOperand(1);
5404 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005405
Nate Begeman7973f352011-02-11 20:53:29 +00005406 if (VT == MVT::v8i8) {
5407 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5408 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005409
Nate Begeman7973f352011-02-11 20:53:29 +00005410 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5411 DAG.getIntPtrConstant(4));
5412 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005413 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005414 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5415 DAG.getIntPtrConstant(0));
5416 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5417 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005418
Nate Begeman7973f352011-02-11 20:53:29 +00005419 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5420 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005421
Nate Begeman7973f352011-02-11 20:53:29 +00005422 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5423 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005424
5425 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005426 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5427 N0);
5428 return N0;
5429 }
Owen Anderson76706012011-04-05 21:48:57 +00005430
Nate Begeman7973f352011-02-11 20:53:29 +00005431 // v4i16 sdiv ... Convert to float.
5432 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5433 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5434 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5435 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5436 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005437 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005438
5439 // Use reciprocal estimate and two refinement steps.
5440 // float4 recip = vrecpeq_f32(yf);
5441 // recip *= vrecpsq_f32(yf, recip);
5442 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005443 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005444 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005445 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005446 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005447 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005448 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005449 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005450 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005451 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005452 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5453 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5454 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5455 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005456 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005457 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5458 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5459 N1 = DAG.getConstant(2, MVT::i32);
5460 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5461 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5462 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5463 // Convert back to integer and return.
5464 // return vmovn_u32(vcvt_s32_f32(result));
5465 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5466 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5467 return N0;
5468}
5469
Evan Cheng342e3162011-08-30 01:34:54 +00005470static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5471 EVT VT = Op.getNode()->getValueType(0);
5472 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5473
5474 unsigned Opc;
5475 bool ExtraOp = false;
5476 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005477 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005478 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5479 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5480 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5481 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5482 }
5483
5484 if (!ExtraOp)
5485 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5486 Op.getOperand(1));
5487 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5488 Op.getOperand(1), Op.getOperand(2));
5489}
5490
Eli Friedman74bf18c2011-09-15 22:26:18 +00005491static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005492 // Monotonic load/store is legal for all targets
5493 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5494 return Op;
5495
5496 // Aquire/Release load/store is not legal for targets without a
5497 // dmb or equivalent available.
5498 return SDValue();
5499}
5500
5501
Eli Friedman2bdffe42011-08-31 00:31:29 +00005502static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005503ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5504 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005505 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005506 assert (Node->getValueType(0) == MVT::i64 &&
5507 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005508
Eli Friedman4d3f3292011-08-31 17:52:22 +00005509 SmallVector<SDValue, 6> Ops;
5510 Ops.push_back(Node->getOperand(0)); // Chain
5511 Ops.push_back(Node->getOperand(1)); // Ptr
5512 // Low part of Val1
5513 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5514 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5515 // High part of Val1
5516 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5517 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005518 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005519 // High part of Val1
5520 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5521 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5522 // High part of Val2
5523 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5524 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5525 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005526 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5527 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005528 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005529 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005530 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005531 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5532 Results.push_back(Result.getValue(2));
5533}
5534
Dan Gohmand858e902010-04-17 15:26:15 +00005535SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005536 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005537 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005538 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005539 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005540 case ISD::GlobalAddress:
5541 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5542 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005543 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005544 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005545 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5546 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005547 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005548 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005549 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005550 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005551 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005552 case ISD::SINT_TO_FP:
5553 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5554 case ISD::FP_TO_SINT:
5555 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005556 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005557 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005558 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005559 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005560 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005561 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005562 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5563 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005564 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005565 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005566 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005567 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005568 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005569 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005570 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005571 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengc8e70452012-12-04 22:41:50 +00005572 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005573 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005574 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005575 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005576 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005577 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005578 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005579 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005580 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005581 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005582 case ISD::SDIV: return LowerSDIV(Op, DAG);
5583 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005584 case ISD::ADDC:
5585 case ISD::ADDE:
5586 case ISD::SUBC:
5587 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005588 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005589 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005590 }
Evan Chenga8e29892007-01-19 07:51:42 +00005591}
5592
Duncan Sands1607f052008-12-01 11:39:25 +00005593/// ReplaceNodeResults - Replace the results of node with an illegal result
5594/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005595void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5596 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005597 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005598 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005599 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005600 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005601 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005602 case ISD::BITCAST:
5603 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005604 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005605 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005606 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005607 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005608 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005609 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005610 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005611 return;
5612 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005613 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005614 return;
5615 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005616 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005617 return;
5618 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005619 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005620 return;
5621 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005622 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005623 return;
5624 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005625 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005626 return;
5627 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005628 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005629 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005630 case ISD::ATOMIC_CMP_SWAP:
5631 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5632 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005633 case ISD::ATOMIC_LOAD_MIN:
5634 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5635 return;
5636 case ISD::ATOMIC_LOAD_UMIN:
5637 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5638 return;
5639 case ISD::ATOMIC_LOAD_MAX:
5640 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5641 return;
5642 case ISD::ATOMIC_LOAD_UMAX:
5643 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5644 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005645 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005646 if (Res.getNode())
5647 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005648}
Chris Lattner27a6c732007-11-24 07:07:01 +00005649
Evan Chenga8e29892007-01-19 07:51:42 +00005650//===----------------------------------------------------------------------===//
5651// ARM Scheduler Hooks
5652//===----------------------------------------------------------------------===//
5653
5654MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005655ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5656 MachineBasicBlock *BB,
5657 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005658 unsigned dest = MI->getOperand(0).getReg();
5659 unsigned ptr = MI->getOperand(1).getReg();
5660 unsigned oldval = MI->getOperand(2).getReg();
5661 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005662 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5663 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005664 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005665
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005666 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005667 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5668 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5669 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005670
5671 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005672 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5673 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5674 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005675 }
5676
Jim Grosbach5278eb82009-12-11 01:42:04 +00005677 unsigned ldrOpc, strOpc;
5678 switch (Size) {
5679 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005680 case 1:
5681 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005682 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005683 break;
5684 case 2:
5685 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5686 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5687 break;
5688 case 4:
5689 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5690 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5691 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005692 }
5693
5694 MachineFunction *MF = BB->getParent();
5695 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5696 MachineFunction::iterator It = BB;
5697 ++It; // insert the new blocks after the current block
5698
5699 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5700 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5701 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5702 MF->insert(It, loop1MBB);
5703 MF->insert(It, loop2MBB);
5704 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005705
5706 // Transfer the remainder of BB and its successor edges to exitMBB.
5707 exitMBB->splice(exitMBB->begin(), BB,
5708 llvm::next(MachineBasicBlock::iterator(MI)),
5709 BB->end());
5710 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005711
5712 // thisMBB:
5713 // ...
5714 // fallthrough --> loop1MBB
5715 BB->addSuccessor(loop1MBB);
5716
5717 // loop1MBB:
5718 // ldrex dest, [ptr]
5719 // cmp dest, oldval
5720 // bne exitMBB
5721 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005722 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5723 if (ldrOpc == ARM::t2LDREX)
5724 MIB.addImm(0);
5725 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005726 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005727 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005728 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5729 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005730 BB->addSuccessor(loop2MBB);
5731 BB->addSuccessor(exitMBB);
5732
5733 // loop2MBB:
5734 // strex scratch, newval, [ptr]
5735 // cmp scratch, #0
5736 // bne loop1MBB
5737 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005738 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5739 if (strOpc == ARM::t2STREX)
5740 MIB.addImm(0);
5741 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005742 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005743 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005744 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5745 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005746 BB->addSuccessor(loop1MBB);
5747 BB->addSuccessor(exitMBB);
5748
5749 // exitMBB:
5750 // ...
5751 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005752
Dan Gohman14152b42010-07-06 20:24:04 +00005753 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005754
Jim Grosbach5278eb82009-12-11 01:42:04 +00005755 return BB;
5756}
5757
5758MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005759ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5760 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005761 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5762 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5763
5764 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005765 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005766 MachineFunction::iterator It = BB;
5767 ++It;
5768
5769 unsigned dest = MI->getOperand(0).getReg();
5770 unsigned ptr = MI->getOperand(1).getReg();
5771 unsigned incr = MI->getOperand(2).getReg();
5772 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005773 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005774
5775 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5776 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005777 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5778 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005779 }
5780
Jim Grosbachc3c23542009-12-14 04:22:04 +00005781 unsigned ldrOpc, strOpc;
5782 switch (Size) {
5783 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005784 case 1:
5785 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005786 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005787 break;
5788 case 2:
5789 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5790 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5791 break;
5792 case 4:
5793 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5794 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5795 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005796 }
5797
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005798 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5799 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5800 MF->insert(It, loopMBB);
5801 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005802
5803 // Transfer the remainder of BB and its successor edges to exitMBB.
5804 exitMBB->splice(exitMBB->begin(), BB,
5805 llvm::next(MachineBasicBlock::iterator(MI)),
5806 BB->end());
5807 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005808
Craig Topper420761a2012-04-20 07:30:17 +00005809 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005810 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005811 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005812 unsigned scratch = MRI.createVirtualRegister(TRC);
5813 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005814
5815 // thisMBB:
5816 // ...
5817 // fallthrough --> loopMBB
5818 BB->addSuccessor(loopMBB);
5819
5820 // loopMBB:
5821 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005822 // <binop> scratch2, dest, incr
5823 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005824 // cmp scratch, #0
5825 // bne- loopMBB
5826 // fallthrough --> exitMBB
5827 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005828 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5829 if (ldrOpc == ARM::t2LDREX)
5830 MIB.addImm(0);
5831 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005832 if (BinOpcode) {
5833 // operand order needs to go the other way for NAND
5834 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5835 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5836 addReg(incr).addReg(dest)).addReg(0);
5837 else
5838 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5839 addReg(dest).addReg(incr)).addReg(0);
5840 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005841
Jim Grosbachb6aed502011-09-09 18:37:27 +00005842 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5843 if (strOpc == ARM::t2STREX)
5844 MIB.addImm(0);
5845 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005846 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005847 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005848 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5849 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005850
5851 BB->addSuccessor(loopMBB);
5852 BB->addSuccessor(exitMBB);
5853
5854 // exitMBB:
5855 // ...
5856 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005857
Dan Gohman14152b42010-07-06 20:24:04 +00005858 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005859
Jim Grosbachc3c23542009-12-14 04:22:04 +00005860 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005861}
5862
Jim Grosbachf7da8822011-04-26 19:44:18 +00005863MachineBasicBlock *
5864ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5865 MachineBasicBlock *BB,
5866 unsigned Size,
5867 bool signExtend,
5868 ARMCC::CondCodes Cond) const {
5869 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5870
5871 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5872 MachineFunction *MF = BB->getParent();
5873 MachineFunction::iterator It = BB;
5874 ++It;
5875
5876 unsigned dest = MI->getOperand(0).getReg();
5877 unsigned ptr = MI->getOperand(1).getReg();
5878 unsigned incr = MI->getOperand(2).getReg();
5879 unsigned oldval = dest;
5880 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005881 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005882
5883 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5884 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005885 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5886 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005887 }
5888
Jim Grosbachf7da8822011-04-26 19:44:18 +00005889 unsigned ldrOpc, strOpc, extendOpc;
5890 switch (Size) {
5891 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5892 case 1:
5893 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5894 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005895 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005896 break;
5897 case 2:
5898 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5899 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005900 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005901 break;
5902 case 4:
5903 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5904 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5905 extendOpc = 0;
5906 break;
5907 }
5908
5909 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5910 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5911 MF->insert(It, loopMBB);
5912 MF->insert(It, exitMBB);
5913
5914 // Transfer the remainder of BB and its successor edges to exitMBB.
5915 exitMBB->splice(exitMBB->begin(), BB,
5916 llvm::next(MachineBasicBlock::iterator(MI)),
5917 BB->end());
5918 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5919
Craig Topper420761a2012-04-20 07:30:17 +00005920 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005921 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005922 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005923 unsigned scratch = MRI.createVirtualRegister(TRC);
5924 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005925
5926 // thisMBB:
5927 // ...
5928 // fallthrough --> loopMBB
5929 BB->addSuccessor(loopMBB);
5930
5931 // loopMBB:
5932 // ldrex dest, ptr
5933 // (sign extend dest, if required)
5934 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005935 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005936 // strex scratch, scratch2, ptr
5937 // cmp scratch, #0
5938 // bne- loopMBB
5939 // fallthrough --> exitMBB
5940 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005941 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5942 if (ldrOpc == ARM::t2LDREX)
5943 MIB.addImm(0);
5944 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005945
5946 // Sign extend the value, if necessary.
5947 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005948 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005949 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5950 .addReg(dest)
5951 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005952 }
5953
5954 // Build compare and cmov instructions.
5955 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5956 .addReg(oldval).addReg(incr));
5957 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005958 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005959
Jim Grosbachb6aed502011-09-09 18:37:27 +00005960 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5961 if (strOpc == ARM::t2STREX)
5962 MIB.addImm(0);
5963 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005964 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5965 .addReg(scratch).addImm(0));
5966 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5967 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5968
5969 BB->addSuccessor(loopMBB);
5970 BB->addSuccessor(exitMBB);
5971
5972 // exitMBB:
5973 // ...
5974 BB = exitMBB;
5975
5976 MI->eraseFromParent(); // The instruction is gone now.
5977
5978 return BB;
5979}
5980
Eli Friedman2bdffe42011-08-31 00:31:29 +00005981MachineBasicBlock *
5982ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5983 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00005984 bool NeedsCarry, bool IsCmpxchg,
5985 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005986 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5987 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5988
5989 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5990 MachineFunction *MF = BB->getParent();
5991 MachineFunction::iterator It = BB;
5992 ++It;
5993
5994 unsigned destlo = MI->getOperand(0).getReg();
5995 unsigned desthi = MI->getOperand(1).getReg();
5996 unsigned ptr = MI->getOperand(2).getReg();
5997 unsigned vallo = MI->getOperand(3).getReg();
5998 unsigned valhi = MI->getOperand(4).getReg();
5999 DebugLoc dl = MI->getDebugLoc();
6000 bool isThumb2 = Subtarget->isThumb2();
6001
6002 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6003 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00006004 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
6005 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
6006 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006007 }
6008
Eli Friedman2bdffe42011-08-31 00:31:29 +00006009 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00006010 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006011 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006012 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006013 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006014 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006015 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006016
Eli Friedman2bdffe42011-08-31 00:31:29 +00006017 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006018 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
6019 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006020 MF->insert(It, exitMBB);
6021
6022 // Transfer the remainder of BB and its successor edges to exitMBB.
6023 exitMBB->splice(exitMBB->begin(), BB,
6024 llvm::next(MachineBasicBlock::iterator(MI)),
6025 BB->end());
6026 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6027
Craig Topper420761a2012-04-20 07:30:17 +00006028 const TargetRegisterClass *TRC = isThumb2 ?
6029 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6030 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006031 unsigned storesuccess = MRI.createVirtualRegister(TRC);
6032
6033 // thisMBB:
6034 // ...
6035 // fallthrough --> loopMBB
6036 BB->addSuccessor(loopMBB);
6037
6038 // loopMBB:
6039 // ldrexd r2, r3, ptr
6040 // <binopa> r0, r2, incr
6041 // <binopb> r1, r3, incr
6042 // strexd storesuccess, r0, r1, ptr
6043 // cmp storesuccess, #0
6044 // bne- loopMBB
6045 // fallthrough --> exitMBB
Eli Friedman2bdffe42011-08-31 00:31:29 +00006046 BB = loopMBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006047
Eli Friedman2bdffe42011-08-31 00:31:29 +00006048 // Load
Tim Northover0adfded2013-01-29 09:06:13 +00006049 if (isThumb2) {
6050 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2LDREXD))
6051 .addReg(destlo, RegState::Define)
6052 .addReg(desthi, RegState::Define)
6053 .addReg(ptr));
6054 } else {
6055 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6056 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDREXD))
6057 .addReg(GPRPair0, RegState::Define).addReg(ptr));
6058 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
6059 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6060 .addReg(GPRPair0, 0, ARM::gsub_0);
6061 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6062 .addReg(GPRPair0, 0, ARM::gsub_1);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006063 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006064
Tim Northover0adfded2013-01-29 09:06:13 +00006065 unsigned StoreLo, StoreHi;
Eli Friedman4d3f3292011-08-31 17:52:22 +00006066 if (IsCmpxchg) {
6067 // Add early exit
6068 for (unsigned i = 0; i < 2; i++) {
6069 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6070 ARM::CMPrr))
6071 .addReg(i == 0 ? destlo : desthi)
6072 .addReg(i == 0 ? vallo : valhi));
6073 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6074 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6075 BB->addSuccessor(exitMBB);
6076 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6077 BB = (i == 0 ? contBB : cont2BB);
6078 }
6079
6080 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006081 StoreLo = MI->getOperand(5).getReg();
6082 StoreHi = MI->getOperand(6).getReg();
Eli Friedman4d3f3292011-08-31 17:52:22 +00006083 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006084 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006085 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6086 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00006087 .addReg(destlo).addReg(vallo))
6088 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006089 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6090 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006091 .addReg(desthi).addReg(valhi))
6092 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006093
Tim Northover0adfded2013-01-29 09:06:13 +00006094 StoreLo = tmpRegLo;
6095 StoreHi = tmpRegHi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006096 } else {
6097 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006098 StoreLo = vallo;
6099 StoreHi = valhi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006100 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00006101 if (IsMinMax) {
6102 // Compare and branch to exit block.
6103 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6104 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6105 BB->addSuccessor(exitMBB);
6106 BB->addSuccessor(contBB);
6107 BB = contBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006108 StoreLo = vallo;
6109 StoreHi = valhi;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006110 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006111
6112 // Store
Tim Northover0adfded2013-01-29 09:06:13 +00006113 if (isThumb2) {
6114 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2STREXD), storesuccess)
6115 .addReg(StoreLo).addReg(StoreHi).addReg(ptr));
6116 } else {
6117 // Marshal a pair...
6118 unsigned StorePair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6119 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6120 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6121 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6122 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6123 .addReg(UndefPair)
6124 .addReg(StoreLo)
6125 .addImm(ARM::gsub_0);
6126 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), StorePair)
6127 .addReg(r1)
6128 .addReg(StoreHi)
6129 .addImm(ARM::gsub_1);
6130
6131 // ...and store it
6132 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::STREXD), storesuccess)
6133 .addReg(StorePair).addReg(ptr));
6134 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006135 // Cmp+jump
6136 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6137 .addReg(storesuccess).addImm(0));
6138 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6139 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6140
6141 BB->addSuccessor(loopMBB);
6142 BB->addSuccessor(exitMBB);
6143
6144 // exitMBB:
6145 // ...
6146 BB = exitMBB;
6147
6148 MI->eraseFromParent(); // The instruction is gone now.
6149
6150 return BB;
6151}
6152
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006153/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6154/// registers the function context.
6155void ARMTargetLowering::
6156SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6157 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6159 DebugLoc dl = MI->getDebugLoc();
6160 MachineFunction *MF = MBB->getParent();
6161 MachineRegisterInfo *MRI = &MF->getRegInfo();
6162 MachineConstantPool *MCP = MF->getConstantPool();
6163 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6164 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006165
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006166 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006167 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006168
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006169 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006170 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006171 ARMConstantPoolValue *CPV =
6172 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6173 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6174
Craig Topper420761a2012-04-20 07:30:17 +00006175 const TargetRegisterClass *TRC = isThumb ?
6176 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6177 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006178
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006179 // Grab constant pool and fixed stack memory operands.
6180 MachineMemOperand *CPMMO =
6181 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6182 MachineMemOperand::MOLoad, 4, 4);
6183
6184 MachineMemOperand *FIMMOSt =
6185 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6186 MachineMemOperand::MOStore, 4, 4);
6187
6188 // Load the address of the dispatch MBB into the jump buffer.
6189 if (isThumb2) {
6190 // Incoming value: jbuf
6191 // ldr.n r5, LCPI1_1
6192 // orr r5, r5, #1
6193 // add r5, pc
6194 // str r5, [$jbuf, #+4] ; &jbuf[1]
6195 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6196 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6197 .addConstantPoolIndex(CPI)
6198 .addMemOperand(CPMMO));
6199 // Set the low bit because of thumb mode.
6200 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6201 AddDefaultCC(
6202 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6203 .addReg(NewVReg1, RegState::Kill)
6204 .addImm(0x01)));
6205 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6206 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6207 .addReg(NewVReg2, RegState::Kill)
6208 .addImm(PCLabelId);
6209 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6210 .addReg(NewVReg3, RegState::Kill)
6211 .addFrameIndex(FI)
6212 .addImm(36) // &jbuf[1] :: pc
6213 .addMemOperand(FIMMOSt));
6214 } else if (isThumb) {
6215 // Incoming value: jbuf
6216 // ldr.n r1, LCPI1_4
6217 // add r1, pc
6218 // mov r2, #1
6219 // orrs r1, r2
6220 // add r2, $jbuf, #+4 ; &jbuf[1]
6221 // str r1, [r2]
6222 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6223 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6224 .addConstantPoolIndex(CPI)
6225 .addMemOperand(CPMMO));
6226 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6227 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6228 .addReg(NewVReg1, RegState::Kill)
6229 .addImm(PCLabelId);
6230 // Set the low bit because of thumb mode.
6231 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6232 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6233 .addReg(ARM::CPSR, RegState::Define)
6234 .addImm(1));
6235 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6236 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6237 .addReg(ARM::CPSR, RegState::Define)
6238 .addReg(NewVReg2, RegState::Kill)
6239 .addReg(NewVReg3, RegState::Kill));
6240 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6241 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6242 .addFrameIndex(FI)
6243 .addImm(36)); // &jbuf[1] :: pc
6244 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6245 .addReg(NewVReg4, RegState::Kill)
6246 .addReg(NewVReg5, RegState::Kill)
6247 .addImm(0)
6248 .addMemOperand(FIMMOSt));
6249 } else {
6250 // Incoming value: jbuf
6251 // ldr r1, LCPI1_1
6252 // add r1, pc, r1
6253 // str r1, [$jbuf, #+4] ; &jbuf[1]
6254 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6255 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6256 .addConstantPoolIndex(CPI)
6257 .addImm(0)
6258 .addMemOperand(CPMMO));
6259 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6260 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6261 .addReg(NewVReg1, RegState::Kill)
6262 .addImm(PCLabelId));
6263 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6264 .addReg(NewVReg2, RegState::Kill)
6265 .addFrameIndex(FI)
6266 .addImm(36) // &jbuf[1] :: pc
6267 .addMemOperand(FIMMOSt));
6268 }
6269}
6270
6271MachineBasicBlock *ARMTargetLowering::
6272EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6273 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6274 DebugLoc dl = MI->getDebugLoc();
6275 MachineFunction *MF = MBB->getParent();
6276 MachineRegisterInfo *MRI = &MF->getRegInfo();
6277 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6278 MachineFrameInfo *MFI = MF->getFrameInfo();
6279 int FI = MFI->getFunctionContextIndex();
6280
Craig Topper420761a2012-04-20 07:30:17 +00006281 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6282 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006283 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006284
Bill Wendling04f15b42011-10-06 21:29:56 +00006285 // Get a mapping of the call site numbers to all of the landing pads they're
6286 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006287 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6288 unsigned MaxCSNum = 0;
6289 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006290 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6291 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006292 if (!BB->isLandingPad()) continue;
6293
6294 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6295 // pad.
6296 for (MachineBasicBlock::iterator
6297 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6298 if (!II->isEHLabel()) continue;
6299
6300 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006301 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006302
Bill Wendling5cbef192011-10-05 23:28:57 +00006303 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6304 for (SmallVectorImpl<unsigned>::iterator
6305 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6306 CSI != CSE; ++CSI) {
6307 CallSiteNumToLPad[*CSI].push_back(BB);
6308 MaxCSNum = std::max(MaxCSNum, *CSI);
6309 }
Bill Wendling2a850152011-10-05 00:02:33 +00006310 break;
6311 }
6312 }
6313
6314 // Get an ordered list of the machine basic blocks for the jump table.
6315 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006316 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006317 LPadList.reserve(CallSiteNumToLPad.size());
6318 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6319 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6320 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006321 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006322 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006323 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6324 }
Bill Wendling2a850152011-10-05 00:02:33 +00006325 }
6326
Bill Wendling5cbef192011-10-05 23:28:57 +00006327 assert(!LPadList.empty() &&
6328 "No landing pad destinations for the dispatch jump table!");
6329
Bill Wendling04f15b42011-10-06 21:29:56 +00006330 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006331 MachineJumpTableInfo *JTI =
6332 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6333 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6334 unsigned UId = AFI->createJumpTableUId();
Chad Rosierb8f307b2013-03-01 18:30:38 +00006335 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling2a850152011-10-05 00:02:33 +00006336
Bill Wendling04f15b42011-10-06 21:29:56 +00006337 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006338
6339 // Shove the dispatch's address into the return slot in the function context.
6340 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6341 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006342
Bill Wendlingbb734682011-10-05 00:39:32 +00006343 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky0f156af2013-01-30 16:30:19 +00006344 unsigned trap_opcode;
Chad Rosier279706e2013-02-28 18:54:27 +00006345 if (Subtarget->isThumb())
Eli Bendersky0f156af2013-01-30 16:30:19 +00006346 trap_opcode = ARM::tTRAP;
Chad Rosier279706e2013-02-28 18:54:27 +00006347 else
6348 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6349
Eli Bendersky0f156af2013-01-30 16:30:19 +00006350 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendlingbb734682011-10-05 00:39:32 +00006351 DispatchBB->addSuccessor(TrapBB);
6352
6353 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6354 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006355
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006356 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006357 MF->insert(MF->end(), DispatchBB);
6358 MF->insert(MF->end(), DispContBB);
6359 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006360
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006361 // Insert code into the entry block that creates and registers the function
6362 // context.
6363 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6364
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006365 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006366 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006367 MachineMemOperand::MOLoad |
6368 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006369
Chad Rosiere7bd5192012-11-06 23:05:24 +00006370 MachineInstrBuilder MIB;
6371 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6372
6373 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6374 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6375
6376 // Add a register mask with no preserved registers. This results in all
6377 // registers being marked as clobbered.
6378 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006379
Bill Wendling952cb502011-10-18 22:49:07 +00006380 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006381 if (Subtarget->isThumb2()) {
6382 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6383 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6384 .addFrameIndex(FI)
6385 .addImm(4)
6386 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006387
Bill Wendling952cb502011-10-18 22:49:07 +00006388 if (NumLPads < 256) {
6389 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6390 .addReg(NewVReg1)
6391 .addImm(LPadList.size()));
6392 } else {
6393 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6394 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006395 .addImm(NumLPads & 0xFFFF));
6396
6397 unsigned VReg2 = VReg1;
6398 if ((NumLPads & 0xFFFF0000) != 0) {
6399 VReg2 = MRI->createVirtualRegister(TRC);
6400 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6401 .addReg(VReg1)
6402 .addImm(NumLPads >> 16));
6403 }
6404
Bill Wendling952cb502011-10-18 22:49:07 +00006405 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6406 .addReg(NewVReg1)
6407 .addReg(VReg2));
6408 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006409
Bill Wendling95ce2e92011-10-06 22:53:00 +00006410 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6411 .addMBB(TrapBB)
6412 .addImm(ARMCC::HI)
6413 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006414
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006415 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6416 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006417 .addJumpTableIndex(MJTI)
6418 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006419
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006420 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006421 AddDefaultCC(
6422 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006423 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6424 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006425 .addReg(NewVReg1)
6426 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6427
6428 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006429 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006430 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006431 .addJumpTableIndex(MJTI)
6432 .addImm(UId);
6433 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006434 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6435 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6436 .addFrameIndex(FI)
6437 .addImm(1)
6438 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006439
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006440 if (NumLPads < 256) {
6441 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6442 .addReg(NewVReg1)
6443 .addImm(NumLPads));
6444 } else {
6445 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006446 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6447 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6448
6449 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006450 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006451 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006452 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006453 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006454
6455 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6456 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6457 .addReg(VReg1, RegState::Define)
6458 .addConstantPoolIndex(Idx));
6459 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6460 .addReg(NewVReg1)
6461 .addReg(VReg1));
6462 }
6463
Bill Wendling083a8eb2011-10-06 23:37:36 +00006464 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6465 .addMBB(TrapBB)
6466 .addImm(ARMCC::HI)
6467 .addReg(ARM::CPSR);
6468
6469 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6470 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6471 .addReg(ARM::CPSR, RegState::Define)
6472 .addReg(NewVReg1)
6473 .addImm(2));
6474
6475 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006476 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006477 .addJumpTableIndex(MJTI)
6478 .addImm(UId));
6479
6480 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6481 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6482 .addReg(ARM::CPSR, RegState::Define)
6483 .addReg(NewVReg2, RegState::Kill)
6484 .addReg(NewVReg3));
6485
6486 MachineMemOperand *JTMMOLd =
6487 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6488 MachineMemOperand::MOLoad, 4, 4);
6489
6490 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6491 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6492 .addReg(NewVReg4, RegState::Kill)
6493 .addImm(0)
6494 .addMemOperand(JTMMOLd));
6495
Chad Rosierb8f307b2013-03-01 18:30:38 +00006496 unsigned NewVReg6 = NewVReg5;
6497 if (RelocM == Reloc::PIC_) {
6498 NewVReg6 = MRI->createVirtualRegister(TRC);
6499 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6500 .addReg(ARM::CPSR, RegState::Define)
6501 .addReg(NewVReg5, RegState::Kill)
6502 .addReg(NewVReg3));
6503 }
Bill Wendling083a8eb2011-10-06 23:37:36 +00006504
6505 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6506 .addReg(NewVReg6, RegState::Kill)
6507 .addJumpTableIndex(MJTI)
6508 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006509 } else {
6510 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6511 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6512 .addFrameIndex(FI)
6513 .addImm(4)
6514 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006515
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006516 if (NumLPads < 256) {
6517 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6518 .addReg(NewVReg1)
6519 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006520 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006521 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6522 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006523 .addImm(NumLPads & 0xFFFF));
6524
6525 unsigned VReg2 = VReg1;
6526 if ((NumLPads & 0xFFFF0000) != 0) {
6527 VReg2 = MRI->createVirtualRegister(TRC);
6528 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6529 .addReg(VReg1)
6530 .addImm(NumLPads >> 16));
6531 }
6532
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006533 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6534 .addReg(NewVReg1)
6535 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006536 } else {
6537 MachineConstantPool *ConstantPool = MF->getConstantPool();
6538 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6539 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6540
6541 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006542 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006543 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006544 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006545 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6546
6547 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6548 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6549 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006550 .addConstantPoolIndex(Idx)
6551 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006552 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6553 .addReg(NewVReg1)
6554 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006555 }
6556
Bill Wendling95ce2e92011-10-06 22:53:00 +00006557 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6558 .addMBB(TrapBB)
6559 .addImm(ARMCC::HI)
6560 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006561
Bill Wendling564392b2011-10-18 22:11:18 +00006562 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006563 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006564 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006565 .addReg(NewVReg1)
6566 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006567 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6568 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006569 .addJumpTableIndex(MJTI)
6570 .addImm(UId));
6571
6572 MachineMemOperand *JTMMOLd =
6573 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6574 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006575 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006576 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006577 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6578 .addReg(NewVReg3, RegState::Kill)
6579 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006580 .addImm(0)
6581 .addMemOperand(JTMMOLd));
6582
Chad Rosierb8f307b2013-03-01 18:30:38 +00006583 if (RelocM == Reloc::PIC_) {
6584 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6585 .addReg(NewVReg5, RegState::Kill)
6586 .addReg(NewVReg4)
6587 .addJumpTableIndex(MJTI)
6588 .addImm(UId);
6589 } else {
6590 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6591 .addReg(NewVReg5, RegState::Kill)
6592 .addJumpTableIndex(MJTI)
6593 .addImm(UId);
6594 }
Bill Wendling95ce2e92011-10-06 22:53:00 +00006595 }
Bill Wendling2a850152011-10-05 00:02:33 +00006596
Bill Wendlingbb734682011-10-05 00:39:32 +00006597 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006598 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006599 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006600 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6601 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006602 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006603 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006604 }
6605
Bill Wendling24bb9252011-10-17 05:25:09 +00006606 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006607 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006608 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006609 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6610 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6611 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006612
6613 // Remove the landing pad successor from the invoke block and replace it
6614 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006615 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6616 BB->succ_end());
6617 while (!Successors.empty()) {
6618 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006619 if (SMBB->isLandingPad()) {
6620 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006621 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006622 }
6623 }
6624
6625 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006626
6627 // Find the invoke call and mark all of the callee-saved registers as
6628 // 'implicit defined' so that they're spilled. This prevents code from
6629 // moving instructions to before the EH block, where they will never be
6630 // executed.
6631 for (MachineBasicBlock::reverse_iterator
6632 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006633 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006634
6635 DenseMap<unsigned, bool> DefRegs;
6636 for (MachineInstr::mop_iterator
6637 OI = II->operands_begin(), OE = II->operands_end();
6638 OI != OE; ++OI) {
6639 if (!OI->isReg()) continue;
6640 DefRegs[OI->getReg()] = true;
6641 }
6642
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00006643 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006644
Bill Wendling5d798592011-10-14 23:55:44 +00006645 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006646 unsigned Reg = SavedRegs[i];
6647 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006648 !ARM::tGPRRegClass.contains(Reg) &&
6649 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006650 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006651 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006652 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006653 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006654 continue;
6655 if (!DefRegs[Reg])
6656 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006657 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006658
6659 break;
6660 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006661 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006662
Bill Wendlingf7b02072011-10-18 18:30:49 +00006663 // Mark all former landing pads as non-landing pads. The dispatch is the only
6664 // landing pad now.
6665 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6666 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6667 (*I)->setIsLandingPad(false);
6668
Bill Wendlingbb734682011-10-05 00:39:32 +00006669 // The instruction is gone now.
6670 MI->eraseFromParent();
6671
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006672 return MBB;
6673}
6674
Evan Cheng218977b2010-07-13 19:27:42 +00006675static
6676MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6677 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6678 E = MBB->succ_end(); I != E; ++I)
6679 if (*I != Succ)
6680 return *I;
6681 llvm_unreachable("Expecting a BB with two successors!");
6682}
6683
Manman Ren68f25572012-06-01 19:33:18 +00006684MachineBasicBlock *ARMTargetLowering::
6685EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6686 // This pseudo instruction has 3 operands: dst, src, size
6687 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6688 // Otherwise, we will generate unrolled scalar copies.
6689 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6690 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6691 MachineFunction::iterator It = BB;
6692 ++It;
6693
6694 unsigned dest = MI->getOperand(0).getReg();
6695 unsigned src = MI->getOperand(1).getReg();
6696 unsigned SizeVal = MI->getOperand(2).getImm();
6697 unsigned Align = MI->getOperand(3).getImm();
6698 DebugLoc dl = MI->getDebugLoc();
6699
6700 bool isThumb2 = Subtarget->isThumb2();
6701 MachineFunction *MF = BB->getParent();
6702 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006703 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006704
6705 const TargetRegisterClass *TRC = isThumb2 ?
6706 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6707 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006708 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006709
6710 if (Align & 1) {
6711 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6712 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6713 UnitSize = 1;
6714 } else if (Align & 2) {
6715 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6716 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6717 UnitSize = 2;
6718 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006719 // Check whether we can use NEON instructions.
Bill Wendling831737d2012-12-30 10:32:01 +00006720 if (!MF->getFunction()->getAttributes().
6721 hasAttribute(AttributeSet::FunctionIndex,
6722 Attribute::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006723 Subtarget->hasNEON()) {
6724 if ((Align % 16 == 0) && SizeVal >= 16) {
6725 ldrOpc = ARM::VLD1q32wb_fixed;
6726 strOpc = ARM::VST1q32wb_fixed;
6727 UnitSize = 16;
6728 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6729 }
6730 else if ((Align % 8 == 0) && SizeVal >= 8) {
6731 ldrOpc = ARM::VLD1d32wb_fixed;
6732 strOpc = ARM::VST1d32wb_fixed;
6733 UnitSize = 8;
6734 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6735 }
6736 }
6737 // Can't use NEON instructions.
6738 if (UnitSize == 0) {
6739 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6740 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6741 UnitSize = 4;
6742 }
Manman Ren68f25572012-06-01 19:33:18 +00006743 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006744
Manman Ren68f25572012-06-01 19:33:18 +00006745 unsigned BytesLeft = SizeVal % UnitSize;
6746 unsigned LoopSize = SizeVal - BytesLeft;
6747
6748 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6749 // Use LDR and STR to copy.
6750 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6751 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6752 unsigned srcIn = src;
6753 unsigned destIn = dest;
6754 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006755 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006756 unsigned srcOut = MRI.createVirtualRegister(TRC);
6757 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006758 if (UnitSize >= 8) {
6759 AddDefaultPred(BuildMI(*BB, MI, dl,
6760 TII->get(ldrOpc), scratch)
6761 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6762
6763 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6764 .addReg(destIn).addImm(0).addReg(scratch));
6765 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006766 AddDefaultPred(BuildMI(*BB, MI, dl,
6767 TII->get(ldrOpc), scratch)
6768 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6769
6770 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6771 .addReg(scratch).addReg(destIn)
6772 .addImm(UnitSize));
6773 } else {
6774 AddDefaultPred(BuildMI(*BB, MI, dl,
6775 TII->get(ldrOpc), scratch)
6776 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6777 .addImm(UnitSize));
6778
6779 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6780 .addReg(scratch).addReg(destIn)
6781 .addReg(0).addImm(UnitSize));
6782 }
6783 srcIn = srcOut;
6784 destIn = destOut;
6785 }
6786
6787 // Handle the leftover bytes with LDRB and STRB.
6788 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6789 // [destOut] = STRB_POST(scratch, destIn, 1)
6790 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6791 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6792 for (unsigned i = 0; i < BytesLeft; i++) {
6793 unsigned scratch = MRI.createVirtualRegister(TRC);
6794 unsigned srcOut = MRI.createVirtualRegister(TRC);
6795 unsigned destOut = MRI.createVirtualRegister(TRC);
6796 if (isThumb2) {
6797 AddDefaultPred(BuildMI(*BB, MI, dl,
6798 TII->get(ldrOpc),scratch)
6799 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6800
6801 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6802 .addReg(scratch).addReg(destIn)
6803 .addReg(0).addImm(1));
6804 } else {
6805 AddDefaultPred(BuildMI(*BB, MI, dl,
6806 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006807 .addReg(srcOut, RegState::Define).addReg(srcIn)
6808 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006809
6810 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6811 .addReg(scratch).addReg(destIn)
6812 .addReg(0).addImm(1));
6813 }
6814 srcIn = srcOut;
6815 destIn = destOut;
6816 }
6817 MI->eraseFromParent(); // The instruction is gone now.
6818 return BB;
6819 }
6820
6821 // Expand the pseudo op to a loop.
6822 // thisMBB:
6823 // ...
6824 // movw varEnd, # --> with thumb2
6825 // movt varEnd, #
6826 // ldrcp varEnd, idx --> without thumb2
6827 // fallthrough --> loopMBB
6828 // loopMBB:
6829 // PHI varPhi, varEnd, varLoop
6830 // PHI srcPhi, src, srcLoop
6831 // PHI destPhi, dst, destLoop
6832 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6833 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6834 // subs varLoop, varPhi, #UnitSize
6835 // bne loopMBB
6836 // fallthrough --> exitMBB
6837 // exitMBB:
6838 // epilogue to handle left-over bytes
6839 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6840 // [destOut] = STRB_POST(scratch, destLoop, 1)
6841 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6842 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6843 MF->insert(It, loopMBB);
6844 MF->insert(It, exitMBB);
6845
6846 // Transfer the remainder of BB and its successor edges to exitMBB.
6847 exitMBB->splice(exitMBB->begin(), BB,
6848 llvm::next(MachineBasicBlock::iterator(MI)),
6849 BB->end());
6850 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6851
6852 // Load an immediate to varEnd.
6853 unsigned varEnd = MRI.createVirtualRegister(TRC);
6854 if (isThumb2) {
6855 unsigned VReg1 = varEnd;
6856 if ((LoopSize & 0xFFFF0000) != 0)
6857 VReg1 = MRI.createVirtualRegister(TRC);
6858 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6859 .addImm(LoopSize & 0xFFFF));
6860
6861 if ((LoopSize & 0xFFFF0000) != 0)
6862 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6863 .addReg(VReg1)
6864 .addImm(LoopSize >> 16));
6865 } else {
6866 MachineConstantPool *ConstantPool = MF->getConstantPool();
6867 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6868 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6869
6870 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006871 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006872 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006873 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006874 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6875
6876 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6877 .addReg(varEnd, RegState::Define)
6878 .addConstantPoolIndex(Idx)
6879 .addImm(0));
6880 }
6881 BB->addSuccessor(loopMBB);
6882
6883 // Generate the loop body:
6884 // varPhi = PHI(varLoop, varEnd)
6885 // srcPhi = PHI(srcLoop, src)
6886 // destPhi = PHI(destLoop, dst)
6887 MachineBasicBlock *entryBB = BB;
6888 BB = loopMBB;
6889 unsigned varLoop = MRI.createVirtualRegister(TRC);
6890 unsigned varPhi = MRI.createVirtualRegister(TRC);
6891 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6892 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6893 unsigned destLoop = MRI.createVirtualRegister(TRC);
6894 unsigned destPhi = MRI.createVirtualRegister(TRC);
6895
6896 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6897 .addReg(varLoop).addMBB(loopMBB)
6898 .addReg(varEnd).addMBB(entryBB);
6899 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6900 .addReg(srcLoop).addMBB(loopMBB)
6901 .addReg(src).addMBB(entryBB);
6902 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6903 .addReg(destLoop).addMBB(loopMBB)
6904 .addReg(dest).addMBB(entryBB);
6905
6906 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6907 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006908 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6909 if (UnitSize >= 8) {
6910 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6911 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6912
6913 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6914 .addReg(destPhi).addImm(0).addReg(scratch));
6915 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006916 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6917 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6918
6919 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6920 .addReg(scratch).addReg(destPhi)
6921 .addImm(UnitSize));
6922 } else {
6923 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6924 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6925 .addImm(UnitSize));
6926
6927 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6928 .addReg(scratch).addReg(destPhi)
6929 .addReg(0).addImm(UnitSize));
6930 }
6931
6932 // Decrement loop variable by UnitSize.
6933 MachineInstrBuilder MIB = BuildMI(BB, dl,
6934 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6935 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6936 MIB->getOperand(5).setReg(ARM::CPSR);
6937 MIB->getOperand(5).setIsDef(true);
6938
6939 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6940 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6941
6942 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6943 BB->addSuccessor(loopMBB);
6944 BB->addSuccessor(exitMBB);
6945
6946 // Add epilogue to handle BytesLeft.
6947 BB = exitMBB;
6948 MachineInstr *StartOfExit = exitMBB->begin();
6949 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6950 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6951
6952 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6953 // [destOut] = STRB_POST(scratch, destLoop, 1)
6954 unsigned srcIn = srcLoop;
6955 unsigned destIn = destLoop;
6956 for (unsigned i = 0; i < BytesLeft; i++) {
6957 unsigned scratch = MRI.createVirtualRegister(TRC);
6958 unsigned srcOut = MRI.createVirtualRegister(TRC);
6959 unsigned destOut = MRI.createVirtualRegister(TRC);
6960 if (isThumb2) {
6961 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6962 TII->get(ldrOpc),scratch)
6963 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6964
6965 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6966 .addReg(scratch).addReg(destIn)
6967 .addImm(1));
6968 } else {
6969 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6970 TII->get(ldrOpc),scratch)
6971 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6972
6973 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6974 .addReg(scratch).addReg(destIn)
6975 .addReg(0).addImm(1));
6976 }
6977 srcIn = srcOut;
6978 destIn = destOut;
6979 }
6980
6981 MI->eraseFromParent(); // The instruction is gone now.
6982 return BB;
6983}
6984
Jim Grosbache801dc42009-12-12 01:40:06 +00006985MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006986ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006987 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006988 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006989 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006990 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006991 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006992 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006993 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006994 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006995 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006996 // The Thumb2 pre-indexed stores have the same MI operands, they just
6997 // define them differently in the .td files from the isel patterns, so
6998 // they need pseudos.
6999 case ARM::t2STR_preidx:
7000 MI->setDesc(TII->get(ARM::t2STR_PRE));
7001 return BB;
7002 case ARM::t2STRB_preidx:
7003 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7004 return BB;
7005 case ARM::t2STRH_preidx:
7006 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7007 return BB;
7008
Jim Grosbach19dec202011-08-05 20:35:44 +00007009 case ARM::STRi_preidx:
7010 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00007011 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00007012 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7013 // Decode the offset.
7014 unsigned Offset = MI->getOperand(4).getImm();
7015 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7016 Offset = ARM_AM::getAM2Offset(Offset);
7017 if (isSub)
7018 Offset = -Offset;
7019
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007020 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00007021 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00007022 .addOperand(MI->getOperand(0)) // Rn_wb
7023 .addOperand(MI->getOperand(1)) // Rt
7024 .addOperand(MI->getOperand(2)) // Rn
7025 .addImm(Offset) // offset (skip GPR==zero_reg)
7026 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007027 .addOperand(MI->getOperand(6))
7028 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00007029 MI->eraseFromParent();
7030 return BB;
7031 }
7032 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00007033 case ARM::STRBr_preidx:
7034 case ARM::STRH_preidx: {
7035 unsigned NewOpc;
7036 switch (MI->getOpcode()) {
7037 default: llvm_unreachable("unexpected opcode!");
7038 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7039 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7040 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7041 }
Jim Grosbach19dec202011-08-05 20:35:44 +00007042 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7043 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7044 MIB.addOperand(MI->getOperand(i));
7045 MI->eraseFromParent();
7046 return BB;
7047 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007048 case ARM::ATOMIC_LOAD_ADD_I8:
7049 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7050 case ARM::ATOMIC_LOAD_ADD_I16:
7051 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7052 case ARM::ATOMIC_LOAD_ADD_I32:
7053 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007054
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007055 case ARM::ATOMIC_LOAD_AND_I8:
7056 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7057 case ARM::ATOMIC_LOAD_AND_I16:
7058 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7059 case ARM::ATOMIC_LOAD_AND_I32:
7060 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007061
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007062 case ARM::ATOMIC_LOAD_OR_I8:
7063 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7064 case ARM::ATOMIC_LOAD_OR_I16:
7065 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7066 case ARM::ATOMIC_LOAD_OR_I32:
7067 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007068
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007069 case ARM::ATOMIC_LOAD_XOR_I8:
7070 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7071 case ARM::ATOMIC_LOAD_XOR_I16:
7072 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7073 case ARM::ATOMIC_LOAD_XOR_I32:
7074 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007075
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007076 case ARM::ATOMIC_LOAD_NAND_I8:
7077 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7078 case ARM::ATOMIC_LOAD_NAND_I16:
7079 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7080 case ARM::ATOMIC_LOAD_NAND_I32:
7081 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007082
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007083 case ARM::ATOMIC_LOAD_SUB_I8:
7084 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7085 case ARM::ATOMIC_LOAD_SUB_I16:
7086 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7087 case ARM::ATOMIC_LOAD_SUB_I32:
7088 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007089
Jim Grosbachf7da8822011-04-26 19:44:18 +00007090 case ARM::ATOMIC_LOAD_MIN_I8:
7091 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7092 case ARM::ATOMIC_LOAD_MIN_I16:
7093 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7094 case ARM::ATOMIC_LOAD_MIN_I32:
7095 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7096
7097 case ARM::ATOMIC_LOAD_MAX_I8:
7098 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7099 case ARM::ATOMIC_LOAD_MAX_I16:
7100 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7101 case ARM::ATOMIC_LOAD_MAX_I32:
7102 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7103
7104 case ARM::ATOMIC_LOAD_UMIN_I8:
7105 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7106 case ARM::ATOMIC_LOAD_UMIN_I16:
7107 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7108 case ARM::ATOMIC_LOAD_UMIN_I32:
7109 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7110
7111 case ARM::ATOMIC_LOAD_UMAX_I8:
7112 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7113 case ARM::ATOMIC_LOAD_UMAX_I16:
7114 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7115 case ARM::ATOMIC_LOAD_UMAX_I32:
7116 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7117
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007118 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7119 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7120 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00007121
7122 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7123 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7124 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007125
Eli Friedman2bdffe42011-08-31 00:31:29 +00007126
7127 case ARM::ATOMADD6432:
7128 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007129 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7130 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007131 case ARM::ATOMSUB6432:
7132 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007133 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7134 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007135 case ARM::ATOMOR6432:
7136 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007137 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007138 case ARM::ATOMXOR6432:
7139 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007140 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007141 case ARM::ATOMAND6432:
7142 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007143 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007144 case ARM::ATOMSWAP6432:
7145 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00007146 case ARM::ATOMCMPXCHG6432:
7147 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7148 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7149 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007150 case ARM::ATOMMIN6432:
7151 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7152 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7153 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007154 /*IsMinMax*/ true, ARMCC::LT);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007155 case ARM::ATOMMAX6432:
7156 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7157 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7158 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7159 /*IsMinMax*/ true, ARMCC::GE);
7160 case ARM::ATOMUMIN6432:
7161 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7162 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7163 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007164 /*IsMinMax*/ true, ARMCC::LO);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007165 case ARM::ATOMUMAX6432:
7166 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7167 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7168 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7169 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007170
Evan Cheng007ea272009-08-12 05:17:19 +00007171 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00007172 // To "insert" a SELECT_CC instruction, we actually have to insert the
7173 // diamond control-flow pattern. The incoming instruction knows the
7174 // destination vreg to set, the condition code register to branch on, the
7175 // true/false values to select between, and a branch opcode to use.
7176 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007177 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00007178 ++It;
7179
7180 // thisMBB:
7181 // ...
7182 // TrueVal = ...
7183 // cmpTY ccX, r1, r2
7184 // bCC copy1MBB
7185 // fallthrough --> copy0MBB
7186 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007187 MachineFunction *F = BB->getParent();
7188 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7189 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00007190 F->insert(It, copy0MBB);
7191 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00007192
7193 // Transfer the remainder of BB and its successor edges to sinkMBB.
7194 sinkMBB->splice(sinkMBB->begin(), BB,
7195 llvm::next(MachineBasicBlock::iterator(MI)),
7196 BB->end());
7197 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7198
Dan Gohman258c58c2010-07-06 15:49:48 +00007199 BB->addSuccessor(copy0MBB);
7200 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00007201
Dan Gohman14152b42010-07-06 20:24:04 +00007202 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7203 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7204
Evan Chenga8e29892007-01-19 07:51:42 +00007205 // copy0MBB:
7206 // %FalseValue = ...
7207 // # fallthrough to sinkMBB
7208 BB = copy0MBB;
7209
7210 // Update machine-CFG edges
7211 BB->addSuccessor(sinkMBB);
7212
7213 // sinkMBB:
7214 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7215 // ...
7216 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007217 BuildMI(*BB, BB->begin(), dl,
7218 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007219 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7220 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7221
Dan Gohman14152b42010-07-06 20:24:04 +00007222 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007223 return BB;
7224 }
Evan Cheng86198642009-08-07 00:34:42 +00007225
Evan Cheng218977b2010-07-13 19:27:42 +00007226 case ARM::BCCi64:
7227 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007228 // If there is an unconditional branch to the other successor, remove it.
7229 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007230
Evan Cheng218977b2010-07-13 19:27:42 +00007231 // Compare both parts that make up the double comparison separately for
7232 // equality.
7233 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7234
7235 unsigned LHS1 = MI->getOperand(1).getReg();
7236 unsigned LHS2 = MI->getOperand(2).getReg();
7237 if (RHSisZero) {
7238 AddDefaultPred(BuildMI(BB, dl,
7239 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7240 .addReg(LHS1).addImm(0));
7241 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7242 .addReg(LHS2).addImm(0)
7243 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7244 } else {
7245 unsigned RHS1 = MI->getOperand(3).getReg();
7246 unsigned RHS2 = MI->getOperand(4).getReg();
7247 AddDefaultPred(BuildMI(BB, dl,
7248 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7249 .addReg(LHS1).addReg(RHS1));
7250 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7251 .addReg(LHS2).addReg(RHS2)
7252 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7253 }
7254
7255 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7256 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7257 if (MI->getOperand(0).getImm() == ARMCC::NE)
7258 std::swap(destMBB, exitMBB);
7259
7260 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7261 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007262 if (isThumb2)
7263 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7264 else
7265 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007266
7267 MI->eraseFromParent(); // The pseudo instruction is gone now.
7268 return BB;
7269 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007270
Bill Wendling5bc85282011-10-17 20:37:20 +00007271 case ARM::Int_eh_sjlj_setjmp:
7272 case ARM::Int_eh_sjlj_setjmp_nofp:
7273 case ARM::tInt_eh_sjlj_setjmp:
7274 case ARM::t2Int_eh_sjlj_setjmp:
7275 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7276 EmitSjLjDispatchBlock(MI, BB);
7277 return BB;
7278
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007279 case ARM::ABS:
7280 case ARM::t2ABS: {
7281 // To insert an ABS instruction, we have to insert the
7282 // diamond control-flow pattern. The incoming instruction knows the
7283 // source vreg to test against 0, the destination vreg to set,
7284 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007285 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007286 // It transforms
7287 // V1 = ABS V0
7288 // into
7289 // V2 = MOVS V0
7290 // BCC (branch to SinkBB if V0 >= 0)
7291 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007292 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007293 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7294 MachineFunction::iterator BBI = BB;
7295 ++BBI;
7296 MachineFunction *Fn = BB->getParent();
7297 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7298 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7299 Fn->insert(BBI, RSBBB);
7300 Fn->insert(BBI, SinkBB);
7301
7302 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7303 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7304 bool isThumb2 = Subtarget->isThumb2();
7305 MachineRegisterInfo &MRI = Fn->getRegInfo();
7306 // In Thumb mode S must not be specified if source register is the SP or
7307 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007308 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7309 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7310 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007311
7312 // Transfer the remainder of BB and its successor edges to sinkMBB.
7313 SinkBB->splice(SinkBB->begin(), BB,
7314 llvm::next(MachineBasicBlock::iterator(MI)),
7315 BB->end());
7316 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7317
7318 BB->addSuccessor(RSBBB);
7319 BB->addSuccessor(SinkBB);
7320
7321 // fall through to SinkMBB
7322 RSBBB->addSuccessor(SinkBB);
7323
Manman Ren307473d2012-06-15 21:32:12 +00007324 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007325 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007326 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7327 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007328
7329 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007330 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007331 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7332 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7333
7334 // insert rsbri in RSBBB
7335 // Note: BCC and rsbri will be converted into predicated rsbmi
7336 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007337 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007338 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007339 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007340 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7341
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007342 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007343 // reuse ABSDstReg to not change uses of ABS instruction
7344 BuildMI(*SinkBB, SinkBB->begin(), dl,
7345 TII->get(ARM::PHI), ABSDstReg)
7346 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007347 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007348
7349 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007350 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007351
7352 // return last added BB
7353 return SinkBB;
7354 }
Manman Ren68f25572012-06-01 19:33:18 +00007355 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007356 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007357 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007358 }
7359}
7360
Evan Cheng37fefc22011-08-30 19:09:48 +00007361void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7362 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007363 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007364 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7365 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7366 return;
7367 }
7368
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007369 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007370 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7371 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7372 // operand is still set to noreg. If needed, set the optional operand's
7373 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007374 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007375 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007376
Andrew Trick3be654f2011-09-21 02:20:46 +00007377 // Rename pseudo opcodes.
7378 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7379 if (NewOpc) {
7380 const ARMBaseInstrInfo *TII =
7381 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007382 MCID = &TII->get(NewOpc);
7383
7384 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7385 "converted opcode should be the same except for cc_out");
7386
7387 MI->setDesc(*MCID);
7388
7389 // Add the optional cc_out operand
7390 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007391 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007392 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007393
7394 // Any ARM instruction that sets the 's' bit should specify an optional
7395 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007396 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007397 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007398 return;
7399 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007400 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7401 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007402 bool definesCPSR = false;
7403 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007404 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007405 i != e; ++i) {
7406 const MachineOperand &MO = MI->getOperand(i);
7407 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7408 definesCPSR = true;
7409 if (MO.isDead())
7410 deadCPSR = true;
7411 MI->RemoveOperand(i);
7412 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007413 }
7414 }
Andrew Trick4815d562011-09-20 03:17:40 +00007415 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007416 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007417 return;
7418 }
7419 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007420 if (deadCPSR) {
7421 assert(!MI->getOperand(ccOutIdx).getReg() &&
7422 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007423 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007424 }
Andrew Trick4815d562011-09-20 03:17:40 +00007425
Andrew Trick3be654f2011-09-21 02:20:46 +00007426 // If this instruction was defined with an optional CPSR def and its dag node
7427 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007428 MachineOperand &MO = MI->getOperand(ccOutIdx);
7429 MO.setReg(ARM::CPSR);
7430 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007431}
7432
Evan Chenga8e29892007-01-19 07:51:42 +00007433//===----------------------------------------------------------------------===//
7434// ARM Optimization Hooks
7435//===----------------------------------------------------------------------===//
7436
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007437// Helper function that checks if N is a null or all ones constant.
7438static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7439 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7440 if (!C)
7441 return false;
7442 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7443}
7444
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007445// Return true if N is conditionally 0 or all ones.
7446// Detects these expressions where cc is an i1 value:
7447//
7448// (select cc 0, y) [AllOnes=0]
7449// (select cc y, 0) [AllOnes=0]
7450// (zext cc) [AllOnes=0]
7451// (sext cc) [AllOnes=0/1]
7452// (select cc -1, y) [AllOnes=1]
7453// (select cc y, -1) [AllOnes=1]
7454//
7455// Invert is set when N is the null/all ones constant when CC is false.
7456// OtherOp is set to the alternative value of N.
7457static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7458 SDValue &CC, bool &Invert,
7459 SDValue &OtherOp,
7460 SelectionDAG &DAG) {
7461 switch (N->getOpcode()) {
7462 default: return false;
7463 case ISD::SELECT: {
7464 CC = N->getOperand(0);
7465 SDValue N1 = N->getOperand(1);
7466 SDValue N2 = N->getOperand(2);
7467 if (isZeroOrAllOnes(N1, AllOnes)) {
7468 Invert = false;
7469 OtherOp = N2;
7470 return true;
7471 }
7472 if (isZeroOrAllOnes(N2, AllOnes)) {
7473 Invert = true;
7474 OtherOp = N1;
7475 return true;
7476 }
7477 return false;
7478 }
7479 case ISD::ZERO_EXTEND:
7480 // (zext cc) can never be the all ones value.
7481 if (AllOnes)
7482 return false;
7483 // Fall through.
7484 case ISD::SIGN_EXTEND: {
7485 EVT VT = N->getValueType(0);
7486 CC = N->getOperand(0);
7487 if (CC.getValueType() != MVT::i1)
7488 return false;
7489 Invert = !AllOnes;
7490 if (AllOnes)
7491 // When looking for an AllOnes constant, N is an sext, and the 'other'
7492 // value is 0.
7493 OtherOp = DAG.getConstant(0, VT);
7494 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7495 // When looking for a 0 constant, N can be zext or sext.
7496 OtherOp = DAG.getConstant(1, VT);
7497 else
7498 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7499 return true;
7500 }
7501 }
7502}
7503
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007504// Combine a constant select operand into its use:
7505//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007506// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7507// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7508// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7509// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7510// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007511//
7512// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007513// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007514//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007515// Also recognize sext/zext from i1:
7516//
7517// (add (zext cc), x) -> (select cc (add x, 1), x)
7518// (add (sext cc), x) -> (select cc (add x, -1), x)
7519//
7520// These transformations eventually create predicated instructions.
7521//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007522// @param N The node to transform.
7523// @param Slct The N operand that is a select.
7524// @param OtherOp The other N operand (x above).
7525// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007526// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007527// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007528static
7529SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007530 TargetLowering::DAGCombinerInfo &DCI,
7531 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007532 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007533 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007534 SDValue NonConstantVal;
7535 SDValue CCOp;
7536 bool SwapSelectOps;
7537 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7538 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007539 return SDValue();
7540
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007541 // Slct is now know to be the desired identity constant when CC is true.
7542 SDValue TrueVal = OtherOp;
7543 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7544 OtherOp, NonConstantVal);
7545 // Unless SwapSelectOps says CC should be false.
7546 if (SwapSelectOps)
7547 std::swap(TrueVal, FalseVal);
7548
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007549 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007550 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007551}
7552
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007553// Attempt combineSelectAndUse on each operand of a commutative operator N.
7554static
7555SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7556 TargetLowering::DAGCombinerInfo &DCI) {
7557 SDValue N0 = N->getOperand(0);
7558 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007559 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007560 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7561 if (Result.getNode())
7562 return Result;
7563 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007564 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007565 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7566 if (Result.getNode())
7567 return Result;
7568 }
7569 return SDValue();
7570}
7571
Eric Christopherfa6f5912011-06-29 21:10:36 +00007572// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007573// (only after legalization).
7574static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7575 TargetLowering::DAGCombinerInfo &DCI,
7576 const ARMSubtarget *Subtarget) {
7577
7578 // Only perform optimization if after legalize, and if NEON is available. We
7579 // also expected both operands to be BUILD_VECTORs.
7580 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7581 || N0.getOpcode() != ISD::BUILD_VECTOR
7582 || N1.getOpcode() != ISD::BUILD_VECTOR)
7583 return SDValue();
7584
7585 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7586 EVT VT = N->getValueType(0);
7587 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7588 return SDValue();
7589
7590 // Check that the vector operands are of the right form.
7591 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7592 // operands, where N is the size of the formed vector.
7593 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7594 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007595
7596 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007597 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007598 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007599 SDValue Vec = N0->getOperand(0)->getOperand(0);
7600 SDNode *V = Vec.getNode();
7601 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007602
Eric Christopherfa6f5912011-06-29 21:10:36 +00007603 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007604 // check to see if each of their operands are an EXTRACT_VECTOR with
7605 // the same vector and appropriate index.
7606 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7607 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7608 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007609
Tanya Lattner189531f2011-06-14 23:48:48 +00007610 SDValue ExtVec0 = N0->getOperand(i);
7611 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007612
Tanya Lattner189531f2011-06-14 23:48:48 +00007613 // First operand is the vector, verify its the same.
7614 if (V != ExtVec0->getOperand(0).getNode() ||
7615 V != ExtVec1->getOperand(0).getNode())
7616 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007617
Tanya Lattner189531f2011-06-14 23:48:48 +00007618 // Second is the constant, verify its correct.
7619 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7620 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007621
Tanya Lattner189531f2011-06-14 23:48:48 +00007622 // For the constant, we want to see all the even or all the odd.
7623 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7624 || C1->getZExtValue() != nextIndex+1)
7625 return SDValue();
7626
7627 // Increment index.
7628 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007629 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007630 return SDValue();
7631 }
7632
7633 // Create VPADDL node.
7634 SelectionDAG &DAG = DCI.DAG;
7635 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007636
7637 // Build operand list.
7638 SmallVector<SDValue, 8> Ops;
7639 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7640 TLI.getPointerTy()));
7641
7642 // Input is the vector.
7643 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007644
Tanya Lattner189531f2011-06-14 23:48:48 +00007645 // Get widened type and narrowed type.
7646 MVT widenType;
7647 unsigned numElem = VT.getVectorNumElements();
7648 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7649 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7650 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7651 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7652 default:
Craig Topperbc219812012-02-07 02:50:20 +00007653 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007654 }
7655
7656 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7657 widenType, &Ops[0], Ops.size());
7658 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7659}
7660
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007661static SDValue findMUL_LOHI(SDValue V) {
7662 if (V->getOpcode() == ISD::UMUL_LOHI ||
7663 V->getOpcode() == ISD::SMUL_LOHI)
7664 return V;
7665 return SDValue();
7666}
7667
7668static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7669 TargetLowering::DAGCombinerInfo &DCI,
7670 const ARMSubtarget *Subtarget) {
7671
7672 if (Subtarget->isThumb1Only()) return SDValue();
7673
7674 // Only perform the checks after legalize when the pattern is available.
7675 if (DCI.isBeforeLegalize()) return SDValue();
7676
7677 // Look for multiply add opportunities.
7678 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7679 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7680 // a glue link from the first add to the second add.
7681 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7682 // a S/UMLAL instruction.
7683 // loAdd UMUL_LOHI
7684 // \ / :lo \ :hi
7685 // \ / \ [no multiline comment]
7686 // ADDC | hiAdd
7687 // \ :glue / /
7688 // \ / /
7689 // ADDE
7690 //
7691 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7692 SDValue AddcOp0 = AddcNode->getOperand(0);
7693 SDValue AddcOp1 = AddcNode->getOperand(1);
7694
7695 // Check if the two operands are from the same mul_lohi node.
7696 if (AddcOp0.getNode() == AddcOp1.getNode())
7697 return SDValue();
7698
7699 assert(AddcNode->getNumValues() == 2 &&
7700 AddcNode->getValueType(0) == MVT::i32 &&
7701 AddcNode->getValueType(1) == MVT::Glue &&
7702 "Expect ADDC with two result values: i32, glue");
7703
7704 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7705 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7706 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7707 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7708 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7709 return SDValue();
7710
7711 // Look for the glued ADDE.
7712 SDNode* AddeNode = AddcNode->getGluedUser();
7713 if (AddeNode == NULL)
7714 return SDValue();
7715
7716 // Make sure it is really an ADDE.
7717 if (AddeNode->getOpcode() != ISD::ADDE)
7718 return SDValue();
7719
7720 assert(AddeNode->getNumOperands() == 3 &&
7721 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7722 "ADDE node has the wrong inputs");
7723
7724 // Check for the triangle shape.
7725 SDValue AddeOp0 = AddeNode->getOperand(0);
7726 SDValue AddeOp1 = AddeNode->getOperand(1);
7727
7728 // Make sure that the ADDE operands are not coming from the same node.
7729 if (AddeOp0.getNode() == AddeOp1.getNode())
7730 return SDValue();
7731
7732 // Find the MUL_LOHI node walking up ADDE's operands.
7733 bool IsLeftOperandMUL = false;
7734 SDValue MULOp = findMUL_LOHI(AddeOp0);
7735 if (MULOp == SDValue())
7736 MULOp = findMUL_LOHI(AddeOp1);
7737 else
7738 IsLeftOperandMUL = true;
7739 if (MULOp == SDValue())
7740 return SDValue();
7741
7742 // Figure out the right opcode.
7743 unsigned Opc = MULOp->getOpcode();
7744 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7745
7746 // Figure out the high and low input values to the MLAL node.
7747 SDValue* HiMul = &MULOp;
7748 SDValue* HiAdd = NULL;
7749 SDValue* LoMul = NULL;
7750 SDValue* LowAdd = NULL;
7751
7752 if (IsLeftOperandMUL)
7753 HiAdd = &AddeOp1;
7754 else
7755 HiAdd = &AddeOp0;
7756
7757
7758 if (AddcOp0->getOpcode() == Opc) {
7759 LoMul = &AddcOp0;
7760 LowAdd = &AddcOp1;
7761 }
7762 if (AddcOp1->getOpcode() == Opc) {
7763 LoMul = &AddcOp1;
7764 LowAdd = &AddcOp0;
7765 }
7766
7767 if (LoMul == NULL)
7768 return SDValue();
7769
7770 if (LoMul->getNode() != HiMul->getNode())
7771 return SDValue();
7772
7773 // Create the merged node.
7774 SelectionDAG &DAG = DCI.DAG;
7775
7776 // Build operand list.
7777 SmallVector<SDValue, 8> Ops;
7778 Ops.push_back(LoMul->getOperand(0));
7779 Ops.push_back(LoMul->getOperand(1));
7780 Ops.push_back(*LowAdd);
7781 Ops.push_back(*HiAdd);
7782
7783 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7784 DAG.getVTList(MVT::i32, MVT::i32),
7785 &Ops[0], Ops.size());
7786
7787 // Replace the ADDs' nodes uses by the MLA node's values.
7788 SDValue HiMLALResult(MLALNode.getNode(), 1);
7789 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7790
7791 SDValue LoMLALResult(MLALNode.getNode(), 0);
7792 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7793
7794 // Return original node to notify the driver to stop replacing.
7795 SDValue resNode(AddcNode, 0);
7796 return resNode;
7797}
7798
7799/// PerformADDCCombine - Target-specific dag combine transform from
7800/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7801static SDValue PerformADDCCombine(SDNode *N,
7802 TargetLowering::DAGCombinerInfo &DCI,
7803 const ARMSubtarget *Subtarget) {
7804
7805 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7806
7807}
7808
Bob Wilson3d5792a2010-07-29 20:34:14 +00007809/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7810/// operands N0 and N1. This is a helper for PerformADDCombine that is
7811/// called with the default operands, and if that fails, with commuted
7812/// operands.
7813static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007814 TargetLowering::DAGCombinerInfo &DCI,
7815 const ARMSubtarget *Subtarget){
7816
7817 // Attempt to create vpaddl for this add.
7818 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7819 if (Result.getNode())
7820 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007821
Chris Lattnerd1980a52009-03-12 06:52:53 +00007822 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007823 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007824 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7825 if (Result.getNode()) return Result;
7826 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007827 return SDValue();
7828}
7829
Bob Wilson3d5792a2010-07-29 20:34:14 +00007830/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7831///
7832static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007833 TargetLowering::DAGCombinerInfo &DCI,
7834 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007835 SDValue N0 = N->getOperand(0);
7836 SDValue N1 = N->getOperand(1);
7837
7838 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007839 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007840 if (Result.getNode())
7841 return Result;
7842
7843 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007844 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007845}
7846
Chris Lattnerd1980a52009-03-12 06:52:53 +00007847/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007848///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007849static SDValue PerformSUBCombine(SDNode *N,
7850 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007851 SDValue N0 = N->getOperand(0);
7852 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007853
Chris Lattnerd1980a52009-03-12 06:52:53 +00007854 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007855 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007856 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7857 if (Result.getNode()) return Result;
7858 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007859
Chris Lattnerd1980a52009-03-12 06:52:53 +00007860 return SDValue();
7861}
7862
Evan Cheng463d3582011-03-31 19:38:48 +00007863/// PerformVMULCombine
7864/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7865/// special multiplier accumulator forwarding.
7866/// vmul d3, d0, d2
7867/// vmla d3, d1, d2
7868/// is faster than
7869/// vadd d3, d0, d1
7870/// vmul d3, d3, d2
7871static SDValue PerformVMULCombine(SDNode *N,
7872 TargetLowering::DAGCombinerInfo &DCI,
7873 const ARMSubtarget *Subtarget) {
7874 if (!Subtarget->hasVMLxForwarding())
7875 return SDValue();
7876
7877 SelectionDAG &DAG = DCI.DAG;
7878 SDValue N0 = N->getOperand(0);
7879 SDValue N1 = N->getOperand(1);
7880 unsigned Opcode = N0.getOpcode();
7881 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7882 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007883 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007884 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7885 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7886 return SDValue();
7887 std::swap(N0, N1);
7888 }
7889
7890 EVT VT = N->getValueType(0);
7891 DebugLoc DL = N->getDebugLoc();
7892 SDValue N00 = N0->getOperand(0);
7893 SDValue N01 = N0->getOperand(1);
7894 return DAG.getNode(Opcode, DL, VT,
7895 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7896 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7897}
7898
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007899static SDValue PerformMULCombine(SDNode *N,
7900 TargetLowering::DAGCombinerInfo &DCI,
7901 const ARMSubtarget *Subtarget) {
7902 SelectionDAG &DAG = DCI.DAG;
7903
7904 if (Subtarget->isThumb1Only())
7905 return SDValue();
7906
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007907 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7908 return SDValue();
7909
7910 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007911 if (VT.is64BitVector() || VT.is128BitVector())
7912 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007913 if (VT != MVT::i32)
7914 return SDValue();
7915
7916 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7917 if (!C)
7918 return SDValue();
7919
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007920 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007921 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007922
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007923 ShiftAmt = ShiftAmt & (32 - 1);
7924 SDValue V = N->getOperand(0);
7925 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007926
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007927 SDValue Res;
7928 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007929
7930 if (MulAmt >= 0) {
7931 if (isPowerOf2_32(MulAmt - 1)) {
7932 // (mul x, 2^N + 1) => (add (shl x, N), x)
7933 Res = DAG.getNode(ISD::ADD, DL, VT,
7934 V,
7935 DAG.getNode(ISD::SHL, DL, VT,
7936 V,
7937 DAG.getConstant(Log2_32(MulAmt - 1),
7938 MVT::i32)));
7939 } else if (isPowerOf2_32(MulAmt + 1)) {
7940 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7941 Res = DAG.getNode(ISD::SUB, DL, VT,
7942 DAG.getNode(ISD::SHL, DL, VT,
7943 V,
7944 DAG.getConstant(Log2_32(MulAmt + 1),
7945 MVT::i32)),
7946 V);
7947 } else
7948 return SDValue();
7949 } else {
7950 uint64_t MulAmtAbs = -MulAmt;
7951 if (isPowerOf2_32(MulAmtAbs + 1)) {
7952 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7953 Res = DAG.getNode(ISD::SUB, DL, VT,
7954 V,
7955 DAG.getNode(ISD::SHL, DL, VT,
7956 V,
7957 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7958 MVT::i32)));
7959 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7960 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7961 Res = DAG.getNode(ISD::ADD, DL, VT,
7962 V,
7963 DAG.getNode(ISD::SHL, DL, VT,
7964 V,
7965 DAG.getConstant(Log2_32(MulAmtAbs-1),
7966 MVT::i32)));
7967 Res = DAG.getNode(ISD::SUB, DL, VT,
7968 DAG.getConstant(0, MVT::i32),Res);
7969
7970 } else
7971 return SDValue();
7972 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007973
7974 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007975 Res = DAG.getNode(ISD::SHL, DL, VT,
7976 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007977
7978 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007979 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007980 return SDValue();
7981}
7982
Owen Anderson080c0922010-11-05 19:27:46 +00007983static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007984 TargetLowering::DAGCombinerInfo &DCI,
7985 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007986
Owen Anderson080c0922010-11-05 19:27:46 +00007987 // Attempt to use immediate-form VBIC
7988 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7989 DebugLoc dl = N->getDebugLoc();
7990 EVT VT = N->getValueType(0);
7991 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007992
Tanya Lattner0433b212011-04-07 15:24:20 +00007993 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7994 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007995
Owen Anderson080c0922010-11-05 19:27:46 +00007996 APInt SplatBits, SplatUndef;
7997 unsigned SplatBitSize;
7998 bool HasAnyUndefs;
7999 if (BVN &&
8000 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8001 if (SplatBitSize <= 64) {
8002 EVT VbicVT;
8003 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8004 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008005 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008006 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00008007 if (Val.getNode()) {
8008 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008009 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00008010 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008011 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00008012 }
8013 }
8014 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008015
Evan Chengc892aeb2012-02-23 01:19:06 +00008016 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008017 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8018 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8019 if (Result.getNode())
8020 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008021 }
8022
Owen Anderson080c0922010-11-05 19:27:46 +00008023 return SDValue();
8024}
8025
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008026/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8027static SDValue PerformORCombine(SDNode *N,
8028 TargetLowering::DAGCombinerInfo &DCI,
8029 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00008030 // Attempt to use immediate-form VORR
8031 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8032 DebugLoc dl = N->getDebugLoc();
8033 EVT VT = N->getValueType(0);
8034 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008035
Tanya Lattner0433b212011-04-07 15:24:20 +00008036 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8037 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008038
Owen Anderson60f48702010-11-03 23:15:26 +00008039 APInt SplatBits, SplatUndef;
8040 unsigned SplatBitSize;
8041 bool HasAnyUndefs;
8042 if (BVN && Subtarget->hasNEON() &&
8043 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8044 if (SplatBitSize <= 64) {
8045 EVT VorrVT;
8046 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8047 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008048 DAG, VorrVT, VT.is128BitVector(),
8049 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00008050 if (Val.getNode()) {
8051 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008052 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00008053 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008054 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00008055 }
8056 }
8057 }
8058
Evan Chengc892aeb2012-02-23 01:19:06 +00008059 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008060 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8061 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8062 if (Result.getNode())
8063 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008064 }
8065
Nadav Rotemdf832032012-08-13 18:52:44 +00008066 // The code below optimizes (or (and X, Y), Z).
8067 // The AND operand needs to have a single user to make these optimizations
8068 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008069 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00008070 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008071 return SDValue();
8072 SDValue N1 = N->getOperand(1);
8073
8074 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8075 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8076 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8077 APInt SplatUndef;
8078 unsigned SplatBitSize;
8079 bool HasAnyUndefs;
8080
8081 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8082 APInt SplatBits0;
8083 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8084 HasAnyUndefs) && !HasAnyUndefs) {
8085 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8086 APInt SplatBits1;
8087 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8088 HasAnyUndefs) && !HasAnyUndefs &&
8089 SplatBits0 == ~SplatBits1) {
8090 // Canonicalize the vector type to make instruction selection simpler.
8091 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8092 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8093 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00008094 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008095 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8096 }
8097 }
8098 }
8099
Jim Grosbach54238562010-07-17 03:30:54 +00008100 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8101 // reasonable.
8102
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008103 // BFI is only available on V6T2+
8104 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8105 return SDValue();
8106
Jim Grosbach54238562010-07-17 03:30:54 +00008107 DebugLoc DL = N->getDebugLoc();
8108 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008109 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00008110 //
8111 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008112 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008113 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008114 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008115 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00008116 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008117
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008118 if (VT != MVT::i32)
8119 return SDValue();
8120
Evan Cheng30fb13f2010-12-13 20:32:54 +00008121 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00008122
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008123 // The value and the mask need to be constants so we can verify this is
8124 // actually a bitfield set. If the mask is 0xffff, we can do better
8125 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00008126 SDValue MaskOp = N0.getOperand(1);
8127 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8128 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008129 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008130 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008131 if (Mask == 0xffff)
8132 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008133 SDValue Res;
8134 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008135 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8136 if (N1C) {
8137 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008138 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00008139 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008140
Evan Chenga9688c42010-12-11 04:11:38 +00008141 if (ARM::isBitFieldInvertedMask(Mask)) {
8142 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008143
Evan Cheng30fb13f2010-12-13 20:32:54 +00008144 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00008145 DAG.getConstant(Val, MVT::i32),
8146 DAG.getConstant(Mask, MVT::i32));
8147
8148 // Do not add new nodes to DAG combiner worklist.
8149 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008150 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008151 }
Jim Grosbach54238562010-07-17 03:30:54 +00008152 } else if (N1.getOpcode() == ISD::AND) {
8153 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008154 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8155 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00008156 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008157 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008158
Eric Christopher29aeed12011-03-26 01:21:03 +00008159 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8160 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00008161 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008162 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008163 // The pack halfword instruction works better for masks that fit it,
8164 // so use that when it's available.
8165 if (Subtarget->hasT2ExtractPack() &&
8166 (Mask == 0xffff || Mask == 0xffff0000))
8167 return SDValue();
8168 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00008169 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00008170 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00008171 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00008172 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00008173 DAG.getConstant(Mask, MVT::i32));
8174 // Do not add new nodes to DAG combiner worklist.
8175 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008176 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008177 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008178 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008179 // The pack halfword instruction works better for masks that fit it,
8180 // so use that when it's available.
8181 if (Subtarget->hasT2ExtractPack() &&
8182 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8183 return SDValue();
8184 // 2b
8185 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008186 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00008187 DAG.getConstant(lsb, MVT::i32));
8188 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00008189 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00008190 // Do not add new nodes to DAG combiner worklist.
8191 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008192 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008193 }
8194 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008195
Evan Cheng30fb13f2010-12-13 20:32:54 +00008196 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8197 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8198 ARM::isBitFieldInvertedMask(~Mask)) {
8199 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8200 // where lsb(mask) == #shamt and masked bits of B are known zero.
8201 SDValue ShAmt = N00.getOperand(1);
8202 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8203 unsigned LSB = CountTrailingZeros_32(Mask);
8204 if (ShAmtC != LSB)
8205 return SDValue();
8206
8207 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8208 DAG.getConstant(~Mask, MVT::i32));
8209
8210 // Do not add new nodes to DAG combiner worklist.
8211 DCI.CombineTo(N, Res, false);
8212 }
8213
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008214 return SDValue();
8215}
8216
Evan Chengc892aeb2012-02-23 01:19:06 +00008217static SDValue PerformXORCombine(SDNode *N,
8218 TargetLowering::DAGCombinerInfo &DCI,
8219 const ARMSubtarget *Subtarget) {
8220 EVT VT = N->getValueType(0);
8221 SelectionDAG &DAG = DCI.DAG;
8222
8223 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8224 return SDValue();
8225
8226 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008227 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8228 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8229 if (Result.getNode())
8230 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008231 }
8232
8233 return SDValue();
8234}
8235
Evan Chengbf188ae2011-06-15 01:12:31 +00008236/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8237/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008238static SDValue PerformBFICombine(SDNode *N,
8239 TargetLowering::DAGCombinerInfo &DCI) {
8240 SDValue N1 = N->getOperand(1);
8241 if (N1.getOpcode() == ISD::AND) {
8242 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8243 if (!N11C)
8244 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008245 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8246 unsigned LSB = CountTrailingZeros_32(~InvMask);
8247 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8248 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008249 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008250 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008251 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8252 N->getOperand(0), N1.getOperand(0),
8253 N->getOperand(2));
8254 }
8255 return SDValue();
8256}
8257
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008258/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8259/// ARMISD::VMOVRRD.
8260static SDValue PerformVMOVRRDCombine(SDNode *N,
8261 TargetLowering::DAGCombinerInfo &DCI) {
8262 // vmovrrd(vmovdrr x, y) -> x,y
8263 SDValue InDouble = N->getOperand(0);
8264 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8265 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008266
8267 // vmovrrd(load f64) -> (load i32), (load i32)
8268 SDNode *InNode = InDouble.getNode();
8269 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8270 InNode->getValueType(0) == MVT::f64 &&
8271 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8272 !cast<LoadSDNode>(InNode)->isVolatile()) {
8273 // TODO: Should this be done for non-FrameIndex operands?
8274 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8275
8276 SelectionDAG &DAG = DCI.DAG;
8277 DebugLoc DL = LD->getDebugLoc();
8278 SDValue BasePtr = LD->getBasePtr();
8279 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8280 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008281 LD->isNonTemporal(), LD->isInvariant(),
8282 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008283
8284 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8285 DAG.getConstant(4, MVT::i32));
8286 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8287 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008288 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008289 std::min(4U, LD->getAlignment() / 2));
8290
8291 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8292 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8293 DCI.RemoveFromWorklist(LD);
8294 DAG.DeleteNode(LD);
8295 return Result;
8296 }
8297
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008298 return SDValue();
8299}
8300
8301/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8302/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8303static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8304 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8305 SDValue Op0 = N->getOperand(0);
8306 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008307 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008308 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008309 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008310 Op1 = Op1.getOperand(0);
8311 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8312 Op0.getNode() == Op1.getNode() &&
8313 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008314 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008315 N->getValueType(0), Op0.getOperand(0));
8316 return SDValue();
8317}
8318
Bob Wilson31600902010-12-21 06:43:19 +00008319/// PerformSTORECombine - Target-specific dag combine xforms for
8320/// ISD::STORE.
8321static SDValue PerformSTORECombine(SDNode *N,
8322 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008323 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008324 if (St->isVolatile())
8325 return SDValue();
8326
Andrew Trick49b446f2012-07-18 18:34:24 +00008327 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008328 // pack all of the elements in one place. Next, store to memory in fewer
8329 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008330 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008331 EVT VT = StVal.getValueType();
8332 if (St->isTruncatingStore() && VT.isVector()) {
8333 SelectionDAG &DAG = DCI.DAG;
8334 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8335 EVT StVT = St->getMemoryVT();
8336 unsigned NumElems = VT.getVectorNumElements();
8337 assert(StVT != VT && "Cannot truncate to the same type");
8338 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8339 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8340
8341 // From, To sizes and ElemCount must be pow of two
8342 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8343
8344 // We are going to use the original vector elt for storing.
8345 // Accumulated smaller vector elements must be a multiple of the store size.
8346 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8347
8348 unsigned SizeRatio = FromEltSz / ToEltSz;
8349 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8350
8351 // Create a type on which we perform the shuffle.
8352 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8353 NumElems*SizeRatio);
8354 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8355
8356 DebugLoc DL = St->getDebugLoc();
8357 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8358 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8359 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8360
8361 // Can't shuffle using an illegal type.
8362 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8363
8364 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8365 DAG.getUNDEF(WideVec.getValueType()),
8366 ShuffleVec.data());
8367 // At this point all of the data is stored at the bottom of the
8368 // register. We now need to save it to mem.
8369
8370 // Find the largest store unit
8371 MVT StoreType = MVT::i8;
8372 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8373 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8374 MVT Tp = (MVT::SimpleValueType)tp;
8375 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8376 StoreType = Tp;
8377 }
8378 // Didn't find a legal store type.
8379 if (!TLI.isTypeLegal(StoreType))
8380 return SDValue();
8381
8382 // Bitcast the original vector into a vector of store-size units
8383 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8384 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8385 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8386 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8387 SmallVector<SDValue, 8> Chains;
8388 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8389 TLI.getPointerTy());
8390 SDValue BasePtr = St->getBasePtr();
8391
8392 // Perform one or more big stores into memory.
8393 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8394 for (unsigned I = 0; I < E; I++) {
8395 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8396 StoreType, ShuffWide,
8397 DAG.getIntPtrConstant(I));
8398 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8399 St->getPointerInfo(), St->isVolatile(),
8400 St->isNonTemporal(), St->getAlignment());
8401 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8402 Increment);
8403 Chains.push_back(Ch);
8404 }
8405 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8406 Chains.size());
8407 }
8408
8409 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008410 return SDValue();
8411
Chad Rosier96b66d62012-04-09 19:38:15 +00008412 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8413 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008414 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008415 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008416 SelectionDAG &DAG = DCI.DAG;
8417 DebugLoc DL = St->getDebugLoc();
8418 SDValue BasePtr = St->getBasePtr();
8419 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8420 StVal.getNode()->getOperand(0), BasePtr,
8421 St->getPointerInfo(), St->isVolatile(),
8422 St->isNonTemporal(), St->getAlignment());
8423
8424 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8425 DAG.getConstant(4, MVT::i32));
8426 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8427 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8428 St->isNonTemporal(),
8429 std::min(4U, St->getAlignment() / 2));
8430 }
8431
8432 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008433 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8434 return SDValue();
8435
Chad Rosier96b66d62012-04-09 19:38:15 +00008436 // Bitcast an i64 store extracted from a vector to f64.
8437 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008438 SelectionDAG &DAG = DCI.DAG;
8439 DebugLoc dl = StVal.getDebugLoc();
8440 SDValue IntVec = StVal.getOperand(0);
8441 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8442 IntVec.getValueType().getVectorNumElements());
8443 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8444 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8445 Vec, StVal.getOperand(1));
8446 dl = N->getDebugLoc();
8447 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8448 // Make the DAGCombiner fold the bitcasts.
8449 DCI.AddToWorklist(Vec.getNode());
8450 DCI.AddToWorklist(ExtElt.getNode());
8451 DCI.AddToWorklist(V.getNode());
8452 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8453 St->getPointerInfo(), St->isVolatile(),
8454 St->isNonTemporal(), St->getAlignment(),
8455 St->getTBAAInfo());
8456}
8457
8458/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8459/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8460/// i64 vector to have f64 elements, since the value can then be loaded
8461/// directly into a VFP register.
8462static bool hasNormalLoadOperand(SDNode *N) {
8463 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8464 for (unsigned i = 0; i < NumElts; ++i) {
8465 SDNode *Elt = N->getOperand(i).getNode();
8466 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8467 return true;
8468 }
8469 return false;
8470}
8471
Bob Wilson75f02882010-09-17 22:59:05 +00008472/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8473/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008474static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8475 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008476 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8477 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8478 // into a pair of GPRs, which is fine when the value is used as a scalar,
8479 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008480 SelectionDAG &DAG = DCI.DAG;
8481 if (N->getNumOperands() == 2) {
8482 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8483 if (RV.getNode())
8484 return RV;
8485 }
Bob Wilson75f02882010-09-17 22:59:05 +00008486
Bob Wilson31600902010-12-21 06:43:19 +00008487 // Load i64 elements as f64 values so that type legalization does not split
8488 // them up into i32 values.
8489 EVT VT = N->getValueType(0);
8490 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8491 return SDValue();
8492 DebugLoc dl = N->getDebugLoc();
8493 SmallVector<SDValue, 8> Ops;
8494 unsigned NumElts = VT.getVectorNumElements();
8495 for (unsigned i = 0; i < NumElts; ++i) {
8496 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8497 Ops.push_back(V);
8498 // Make the DAGCombiner fold the bitcast.
8499 DCI.AddToWorklist(V.getNode());
8500 }
8501 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8502 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8503 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8504}
8505
8506/// PerformInsertEltCombine - Target-specific dag combine xforms for
8507/// ISD::INSERT_VECTOR_ELT.
8508static SDValue PerformInsertEltCombine(SDNode *N,
8509 TargetLowering::DAGCombinerInfo &DCI) {
8510 // Bitcast an i64 load inserted into a vector to f64.
8511 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8512 EVT VT = N->getValueType(0);
8513 SDNode *Elt = N->getOperand(1).getNode();
8514 if (VT.getVectorElementType() != MVT::i64 ||
8515 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8516 return SDValue();
8517
8518 SelectionDAG &DAG = DCI.DAG;
8519 DebugLoc dl = N->getDebugLoc();
8520 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8521 VT.getVectorNumElements());
8522 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8523 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8524 // Make the DAGCombiner fold the bitcasts.
8525 DCI.AddToWorklist(Vec.getNode());
8526 DCI.AddToWorklist(V.getNode());
8527 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8528 Vec, V, N->getOperand(2));
8529 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008530}
8531
Bob Wilsonf20700c2010-10-27 20:38:28 +00008532/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8533/// ISD::VECTOR_SHUFFLE.
8534static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8535 // The LLVM shufflevector instruction does not require the shuffle mask
8536 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8537 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8538 // operands do not match the mask length, they are extended by concatenating
8539 // them with undef vectors. That is probably the right thing for other
8540 // targets, but for NEON it is better to concatenate two double-register
8541 // size vector operands into a single quad-register size vector. Do that
8542 // transformation here:
8543 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8544 // shuffle(concat(v1, v2), undef)
8545 SDValue Op0 = N->getOperand(0);
8546 SDValue Op1 = N->getOperand(1);
8547 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8548 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8549 Op0.getNumOperands() != 2 ||
8550 Op1.getNumOperands() != 2)
8551 return SDValue();
8552 SDValue Concat0Op1 = Op0.getOperand(1);
8553 SDValue Concat1Op1 = Op1.getOperand(1);
8554 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8555 Concat1Op1.getOpcode() != ISD::UNDEF)
8556 return SDValue();
8557 // Skip the transformation if any of the types are illegal.
8558 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8559 EVT VT = N->getValueType(0);
8560 if (!TLI.isTypeLegal(VT) ||
8561 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8562 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8563 return SDValue();
8564
8565 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8566 Op0.getOperand(0), Op1.getOperand(0));
8567 // Translate the shuffle mask.
8568 SmallVector<int, 16> NewMask;
8569 unsigned NumElts = VT.getVectorNumElements();
8570 unsigned HalfElts = NumElts/2;
8571 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8572 for (unsigned n = 0; n < NumElts; ++n) {
8573 int MaskElt = SVN->getMaskElt(n);
8574 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008575 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008576 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008577 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008578 NewElt = HalfElts + MaskElt - NumElts;
8579 NewMask.push_back(NewElt);
8580 }
8581 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8582 DAG.getUNDEF(VT), NewMask.data());
8583}
8584
Bob Wilson1c3ef902011-02-07 17:43:21 +00008585/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8586/// NEON load/store intrinsics to merge base address updates.
8587static SDValue CombineBaseUpdate(SDNode *N,
8588 TargetLowering::DAGCombinerInfo &DCI) {
8589 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8590 return SDValue();
8591
8592 SelectionDAG &DAG = DCI.DAG;
8593 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8594 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8595 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8596 SDValue Addr = N->getOperand(AddrOpIdx);
8597
8598 // Search for a use of the address operand that is an increment.
8599 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8600 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8601 SDNode *User = *UI;
8602 if (User->getOpcode() != ISD::ADD ||
8603 UI.getUse().getResNo() != Addr.getResNo())
8604 continue;
8605
8606 // Check that the add is independent of the load/store. Otherwise, folding
8607 // it would create a cycle.
8608 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8609 continue;
8610
8611 // Find the new opcode for the updating load/store.
8612 bool isLoad = true;
8613 bool isLaneOp = false;
8614 unsigned NewOpc = 0;
8615 unsigned NumVecs = 0;
8616 if (isIntrinsic) {
8617 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8618 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008619 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008620 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8621 NumVecs = 1; break;
8622 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8623 NumVecs = 2; break;
8624 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8625 NumVecs = 3; break;
8626 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8627 NumVecs = 4; break;
8628 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8629 NumVecs = 2; isLaneOp = true; break;
8630 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8631 NumVecs = 3; isLaneOp = true; break;
8632 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8633 NumVecs = 4; isLaneOp = true; break;
8634 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8635 NumVecs = 1; isLoad = false; break;
8636 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8637 NumVecs = 2; isLoad = false; break;
8638 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8639 NumVecs = 3; isLoad = false; break;
8640 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8641 NumVecs = 4; isLoad = false; break;
8642 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8643 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8644 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8645 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8646 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8647 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8648 }
8649 } else {
8650 isLaneOp = true;
8651 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008652 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008653 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8654 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8655 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8656 }
8657 }
8658
8659 // Find the size of memory referenced by the load/store.
8660 EVT VecTy;
8661 if (isLoad)
8662 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008663 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008664 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8665 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8666 if (isLaneOp)
8667 NumBytes /= VecTy.getVectorNumElements();
8668
8669 // If the increment is a constant, it must match the memory ref size.
8670 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8671 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8672 uint64_t IncVal = CInc->getZExtValue();
8673 if (IncVal != NumBytes)
8674 continue;
8675 } else if (NumBytes >= 3 * 16) {
8676 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8677 // separate instructions that make it harder to use a non-constant update.
8678 continue;
8679 }
8680
8681 // Create the new updating load/store node.
8682 EVT Tys[6];
8683 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8684 unsigned n;
8685 for (n = 0; n < NumResultVecs; ++n)
8686 Tys[n] = VecTy;
8687 Tys[n++] = MVT::i32;
8688 Tys[n] = MVT::Other;
8689 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8690 SmallVector<SDValue, 8> Ops;
8691 Ops.push_back(N->getOperand(0)); // incoming chain
8692 Ops.push_back(N->getOperand(AddrOpIdx));
8693 Ops.push_back(Inc);
8694 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8695 Ops.push_back(N->getOperand(i));
8696 }
8697 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8698 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8699 Ops.data(), Ops.size(),
8700 MemInt->getMemoryVT(),
8701 MemInt->getMemOperand());
8702
8703 // Update the uses.
8704 std::vector<SDValue> NewResults;
8705 for (unsigned i = 0; i < NumResultVecs; ++i) {
8706 NewResults.push_back(SDValue(UpdN.getNode(), i));
8707 }
8708 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8709 DCI.CombineTo(N, NewResults);
8710 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8711
8712 break;
Owen Anderson76706012011-04-05 21:48:57 +00008713 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008714 return SDValue();
8715}
8716
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008717/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8718/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8719/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8720/// return true.
8721static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8722 SelectionDAG &DAG = DCI.DAG;
8723 EVT VT = N->getValueType(0);
8724 // vldN-dup instructions only support 64-bit vectors for N > 1.
8725 if (!VT.is64BitVector())
8726 return false;
8727
8728 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8729 SDNode *VLD = N->getOperand(0).getNode();
8730 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8731 return false;
8732 unsigned NumVecs = 0;
8733 unsigned NewOpc = 0;
8734 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8735 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8736 NumVecs = 2;
8737 NewOpc = ARMISD::VLD2DUP;
8738 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8739 NumVecs = 3;
8740 NewOpc = ARMISD::VLD3DUP;
8741 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8742 NumVecs = 4;
8743 NewOpc = ARMISD::VLD4DUP;
8744 } else {
8745 return false;
8746 }
8747
8748 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8749 // numbers match the load.
8750 unsigned VLDLaneNo =
8751 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8752 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8753 UI != UE; ++UI) {
8754 // Ignore uses of the chain result.
8755 if (UI.getUse().getResNo() == NumVecs)
8756 continue;
8757 SDNode *User = *UI;
8758 if (User->getOpcode() != ARMISD::VDUPLANE ||
8759 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8760 return false;
8761 }
8762
8763 // Create the vldN-dup node.
8764 EVT Tys[5];
8765 unsigned n;
8766 for (n = 0; n < NumVecs; ++n)
8767 Tys[n] = VT;
8768 Tys[n] = MVT::Other;
8769 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8770 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8771 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8772 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8773 Ops, 2, VLDMemInt->getMemoryVT(),
8774 VLDMemInt->getMemOperand());
8775
8776 // Update the uses.
8777 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8778 UI != UE; ++UI) {
8779 unsigned ResNo = UI.getUse().getResNo();
8780 // Ignore uses of the chain result.
8781 if (ResNo == NumVecs)
8782 continue;
8783 SDNode *User = *UI;
8784 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8785 }
8786
8787 // Now the vldN-lane intrinsic is dead except for its chain result.
8788 // Update uses of the chain.
8789 std::vector<SDValue> VLDDupResults;
8790 for (unsigned n = 0; n < NumVecs; ++n)
8791 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8792 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8793 DCI.CombineTo(VLD, VLDDupResults);
8794
8795 return true;
8796}
8797
Bob Wilson9e82bf12010-07-14 01:22:12 +00008798/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8799/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008800static SDValue PerformVDUPLANECombine(SDNode *N,
8801 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008802 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008803
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008804 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8805 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8806 if (CombineVLDDUP(N, DCI))
8807 return SDValue(N, 0);
8808
8809 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8810 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008811 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008812 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008813 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008814 return SDValue();
8815
8816 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8817 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8818 // The canonical VMOV for a zero vector uses a 32-bit element size.
8819 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8820 unsigned EltBits;
8821 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8822 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008823 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008824 if (EltSize > VT.getVectorElementType().getSizeInBits())
8825 return SDValue();
8826
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008827 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008828}
8829
Eric Christopherfa6f5912011-06-29 21:10:36 +00008830// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008831// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8832static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8833{
Chad Rosier118c9a02011-06-28 17:26:57 +00008834 integerPart cN;
8835 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008836 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8837 I != E; I++) {
8838 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8839 if (!C)
8840 return false;
8841
Eric Christopherfa6f5912011-06-29 21:10:36 +00008842 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008843 APFloat APF = C->getValueAPF();
8844 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8845 != APFloat::opOK || !isExact)
8846 return false;
8847
8848 c0 = (I == 0) ? cN : c0;
8849 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8850 return false;
8851 }
8852 C = c0;
8853 return true;
8854}
8855
8856/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8857/// can replace combinations of VMUL and VCVT (floating-point to integer)
8858/// when the VMUL has a constant operand that is a power of 2.
8859///
8860/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8861/// vmul.f32 d16, d17, d16
8862/// vcvt.s32.f32 d16, d16
8863/// becomes:
8864/// vcvt.s32.f32 d16, d16, #3
8865static SDValue PerformVCVTCombine(SDNode *N,
8866 TargetLowering::DAGCombinerInfo &DCI,
8867 const ARMSubtarget *Subtarget) {
8868 SelectionDAG &DAG = DCI.DAG;
8869 SDValue Op = N->getOperand(0);
8870
8871 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8872 Op.getOpcode() != ISD::FMUL)
8873 return SDValue();
8874
8875 uint64_t C;
8876 SDValue N0 = Op->getOperand(0);
8877 SDValue ConstVec = Op->getOperand(1);
8878 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8879
Eric Christopherfa6f5912011-06-29 21:10:36 +00008880 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008881 !isConstVecPow2(ConstVec, isSigned, C))
8882 return SDValue();
8883
8884 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8885 Intrinsic::arm_neon_vcvtfp2fxu;
8886 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8887 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008888 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008889 DAG.getConstant(Log2_64(C), MVT::i32));
8890}
8891
8892/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8893/// can replace combinations of VCVT (integer to floating-point) and VDIV
8894/// when the VDIV has a constant operand that is a power of 2.
8895///
8896/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8897/// vcvt.f32.s32 d16, d16
8898/// vdiv.f32 d16, d17, d16
8899/// becomes:
8900/// vcvt.f32.s32 d16, d16, #3
8901static SDValue PerformVDIVCombine(SDNode *N,
8902 TargetLowering::DAGCombinerInfo &DCI,
8903 const ARMSubtarget *Subtarget) {
8904 SelectionDAG &DAG = DCI.DAG;
8905 SDValue Op = N->getOperand(0);
8906 unsigned OpOpcode = Op.getNode()->getOpcode();
8907
8908 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8909 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8910 return SDValue();
8911
8912 uint64_t C;
8913 SDValue ConstVec = N->getOperand(1);
8914 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8915
8916 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8917 !isConstVecPow2(ConstVec, isSigned, C))
8918 return SDValue();
8919
Eric Christopherfa6f5912011-06-29 21:10:36 +00008920 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008921 Intrinsic::arm_neon_vcvtfxu2fp;
8922 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8923 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008924 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008925 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8926}
8927
8928/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008929/// operand of a vector shift operation, where all the elements of the
8930/// build_vector must have the same constant integer value.
8931static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8932 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008933 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008934 Op = Op.getOperand(0);
8935 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8936 APInt SplatBits, SplatUndef;
8937 unsigned SplatBitSize;
8938 bool HasAnyUndefs;
8939 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8940 HasAnyUndefs, ElementBits) ||
8941 SplatBitSize > ElementBits)
8942 return false;
8943 Cnt = SplatBits.getSExtValue();
8944 return true;
8945}
8946
8947/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8948/// operand of a vector shift left operation. That value must be in the range:
8949/// 0 <= Value < ElementBits for a left shift; or
8950/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008951static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008952 assert(VT.isVector() && "vector shift count is not a vector type");
8953 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8954 if (! getVShiftImm(Op, ElementBits, Cnt))
8955 return false;
8956 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8957}
8958
8959/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8960/// operand of a vector shift right operation. For a shift opcode, the value
8961/// is positive, but for an intrinsic the value count must be negative. The
8962/// absolute value must be in the range:
8963/// 1 <= |Value| <= ElementBits for a right shift; or
8964/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008965static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008966 int64_t &Cnt) {
8967 assert(VT.isVector() && "vector shift count is not a vector type");
8968 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8969 if (! getVShiftImm(Op, ElementBits, Cnt))
8970 return false;
8971 if (isIntrinsic)
8972 Cnt = -Cnt;
8973 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8974}
8975
8976/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8977static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8978 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8979 switch (IntNo) {
8980 default:
8981 // Don't do anything for most intrinsics.
8982 break;
8983
8984 // Vector shifts: check for immediate versions and lower them.
8985 // Note: This is done during DAG combining instead of DAG legalizing because
8986 // the build_vectors for 64-bit vector element shift counts are generally
8987 // not legal, and it is hard to see their values after they get legalized to
8988 // loads from a constant pool.
8989 case Intrinsic::arm_neon_vshifts:
8990 case Intrinsic::arm_neon_vshiftu:
8991 case Intrinsic::arm_neon_vshiftls:
8992 case Intrinsic::arm_neon_vshiftlu:
8993 case Intrinsic::arm_neon_vshiftn:
8994 case Intrinsic::arm_neon_vrshifts:
8995 case Intrinsic::arm_neon_vrshiftu:
8996 case Intrinsic::arm_neon_vrshiftn:
8997 case Intrinsic::arm_neon_vqshifts:
8998 case Intrinsic::arm_neon_vqshiftu:
8999 case Intrinsic::arm_neon_vqshiftsu:
9000 case Intrinsic::arm_neon_vqshiftns:
9001 case Intrinsic::arm_neon_vqshiftnu:
9002 case Intrinsic::arm_neon_vqshiftnsu:
9003 case Intrinsic::arm_neon_vqrshiftns:
9004 case Intrinsic::arm_neon_vqrshiftnu:
9005 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00009006 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009007 int64_t Cnt;
9008 unsigned VShiftOpc = 0;
9009
9010 switch (IntNo) {
9011 case Intrinsic::arm_neon_vshifts:
9012 case Intrinsic::arm_neon_vshiftu:
9013 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9014 VShiftOpc = ARMISD::VSHL;
9015 break;
9016 }
9017 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9018 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9019 ARMISD::VSHRs : ARMISD::VSHRu);
9020 break;
9021 }
9022 return SDValue();
9023
9024 case Intrinsic::arm_neon_vshiftls:
9025 case Intrinsic::arm_neon_vshiftlu:
9026 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
9027 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009028 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009029
9030 case Intrinsic::arm_neon_vrshifts:
9031 case Intrinsic::arm_neon_vrshiftu:
9032 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9033 break;
9034 return SDValue();
9035
9036 case Intrinsic::arm_neon_vqshifts:
9037 case Intrinsic::arm_neon_vqshiftu:
9038 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9039 break;
9040 return SDValue();
9041
9042 case Intrinsic::arm_neon_vqshiftsu:
9043 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9044 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009045 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009046
9047 case Intrinsic::arm_neon_vshiftn:
9048 case Intrinsic::arm_neon_vrshiftn:
9049 case Intrinsic::arm_neon_vqshiftns:
9050 case Intrinsic::arm_neon_vqshiftnu:
9051 case Intrinsic::arm_neon_vqshiftnsu:
9052 case Intrinsic::arm_neon_vqrshiftns:
9053 case Intrinsic::arm_neon_vqrshiftnu:
9054 case Intrinsic::arm_neon_vqrshiftnsu:
9055 // Narrowing shifts require an immediate right shift.
9056 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9057 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00009058 llvm_unreachable("invalid shift count for narrowing vector shift "
9059 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009060
9061 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009062 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00009063 }
9064
9065 switch (IntNo) {
9066 case Intrinsic::arm_neon_vshifts:
9067 case Intrinsic::arm_neon_vshiftu:
9068 // Opcode already set above.
9069 break;
9070 case Intrinsic::arm_neon_vshiftls:
9071 case Intrinsic::arm_neon_vshiftlu:
9072 if (Cnt == VT.getVectorElementType().getSizeInBits())
9073 VShiftOpc = ARMISD::VSHLLi;
9074 else
9075 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9076 ARMISD::VSHLLs : ARMISD::VSHLLu);
9077 break;
9078 case Intrinsic::arm_neon_vshiftn:
9079 VShiftOpc = ARMISD::VSHRN; break;
9080 case Intrinsic::arm_neon_vrshifts:
9081 VShiftOpc = ARMISD::VRSHRs; break;
9082 case Intrinsic::arm_neon_vrshiftu:
9083 VShiftOpc = ARMISD::VRSHRu; break;
9084 case Intrinsic::arm_neon_vrshiftn:
9085 VShiftOpc = ARMISD::VRSHRN; break;
9086 case Intrinsic::arm_neon_vqshifts:
9087 VShiftOpc = ARMISD::VQSHLs; break;
9088 case Intrinsic::arm_neon_vqshiftu:
9089 VShiftOpc = ARMISD::VQSHLu; break;
9090 case Intrinsic::arm_neon_vqshiftsu:
9091 VShiftOpc = ARMISD::VQSHLsu; break;
9092 case Intrinsic::arm_neon_vqshiftns:
9093 VShiftOpc = ARMISD::VQSHRNs; break;
9094 case Intrinsic::arm_neon_vqshiftnu:
9095 VShiftOpc = ARMISD::VQSHRNu; break;
9096 case Intrinsic::arm_neon_vqshiftnsu:
9097 VShiftOpc = ARMISD::VQSHRNsu; break;
9098 case Intrinsic::arm_neon_vqrshiftns:
9099 VShiftOpc = ARMISD::VQRSHRNs; break;
9100 case Intrinsic::arm_neon_vqrshiftnu:
9101 VShiftOpc = ARMISD::VQRSHRNu; break;
9102 case Intrinsic::arm_neon_vqrshiftnsu:
9103 VShiftOpc = ARMISD::VQRSHRNsu; break;
9104 }
9105
9106 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009107 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009108 }
9109
9110 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00009111 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009112 int64_t Cnt;
9113 unsigned VShiftOpc = 0;
9114
9115 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9116 VShiftOpc = ARMISD::VSLI;
9117 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9118 VShiftOpc = ARMISD::VSRI;
9119 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00009120 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009121 }
9122
9123 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
9124 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009125 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009126 }
9127
9128 case Intrinsic::arm_neon_vqrshifts:
9129 case Intrinsic::arm_neon_vqrshiftu:
9130 // No immediate versions of these to check for.
9131 break;
9132 }
9133
9134 return SDValue();
9135}
9136
9137/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9138/// lowers them. As with the vector shift intrinsics, this is done during DAG
9139/// combining instead of DAG legalizing because the build_vectors for 64-bit
9140/// vector element shift counts are generally not legal, and it is hard to see
9141/// their values after they get legalized to loads from a constant pool.
9142static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9143 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00009144 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00009145 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9146 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9147 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9148 SDValue N1 = N->getOperand(1);
9149 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9150 SDValue N0 = N->getOperand(0);
9151 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9152 DAG.MaskedValueIsZero(N0.getOperand(0),
9153 APInt::getHighBitsSet(32, 16)))
9154 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
9155 }
9156 }
Bob Wilson5bafff32009-06-22 23:27:02 +00009157
9158 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00009159 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9160 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00009161 return SDValue();
9162
9163 assert(ST->hasNEON() && "unexpected vector shift");
9164 int64_t Cnt;
9165
9166 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009167 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009168
9169 case ISD::SHL:
9170 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9171 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009173 break;
9174
9175 case ISD::SRA:
9176 case ISD::SRL:
9177 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9178 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9179 ARMISD::VSHRs : ARMISD::VSHRu);
9180 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009181 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009182 }
9183 }
9184 return SDValue();
9185}
9186
9187/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9188/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9189static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9190 const ARMSubtarget *ST) {
9191 SDValue N0 = N->getOperand(0);
9192
9193 // Check for sign- and zero-extensions of vector extract operations of 8-
9194 // and 16-bit vector elements. NEON supports these directly. They are
9195 // handled during DAG combining because type legalization will promote them
9196 // to 32-bit types and it is messy to recognize the operations after that.
9197 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9198 SDValue Vec = N0.getOperand(0);
9199 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009200 EVT VT = N->getValueType(0);
9201 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009202 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9203
Owen Anderson825b72b2009-08-11 20:47:22 +00009204 if (VT == MVT::i32 &&
9205 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00009206 TLI.isTypeLegal(Vec.getValueType()) &&
9207 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009208
9209 unsigned Opc = 0;
9210 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009211 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009212 case ISD::SIGN_EXTEND:
9213 Opc = ARMISD::VGETLANEs;
9214 break;
9215 case ISD::ZERO_EXTEND:
9216 case ISD::ANY_EXTEND:
9217 Opc = ARMISD::VGETLANEu;
9218 break;
9219 }
9220 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9221 }
9222 }
9223
9224 return SDValue();
9225}
9226
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009227/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9228/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9229static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9230 const ARMSubtarget *ST) {
9231 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009232 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009233 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9234 // a NaN; only do the transformation when it matches that behavior.
9235
9236 // For now only do this when using NEON for FP operations; if using VFP, it
9237 // is not obvious that the benefit outweighs the cost of switching to the
9238 // NEON pipeline.
9239 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9240 N->getValueType(0) != MVT::f32)
9241 return SDValue();
9242
9243 SDValue CondLHS = N->getOperand(0);
9244 SDValue CondRHS = N->getOperand(1);
9245 SDValue LHS = N->getOperand(2);
9246 SDValue RHS = N->getOperand(3);
9247 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9248
9249 unsigned Opcode = 0;
9250 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009251 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009252 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009253 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009254 IsReversed = true ; // x CC y ? y : x
9255 } else {
9256 return SDValue();
9257 }
9258
Bob Wilsone742bb52010-02-24 22:15:53 +00009259 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009260 switch (CC) {
9261 default: break;
9262 case ISD::SETOLT:
9263 case ISD::SETOLE:
9264 case ISD::SETLT:
9265 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009266 case ISD::SETULT:
9267 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009268 // If LHS is NaN, an ordered comparison will be false and the result will
9269 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9270 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9271 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9272 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9273 break;
9274 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9275 // will return -0, so vmin can only be used for unsafe math or if one of
9276 // the operands is known to be nonzero.
9277 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009278 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009279 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9280 break;
9281 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009282 break;
9283
9284 case ISD::SETOGT:
9285 case ISD::SETOGE:
9286 case ISD::SETGT:
9287 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009288 case ISD::SETUGT:
9289 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009290 // If LHS is NaN, an ordered comparison will be false and the result will
9291 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9292 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9293 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9294 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9295 break;
9296 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9297 // will return +0, so vmax can only be used for unsafe math or if one of
9298 // the operands is known to be nonzero.
9299 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009300 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009301 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9302 break;
9303 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009304 break;
9305 }
9306
9307 if (!Opcode)
9308 return SDValue();
9309 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9310}
9311
Evan Chenge721f5c2011-07-13 00:42:17 +00009312/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9313SDValue
9314ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9315 SDValue Cmp = N->getOperand(4);
9316 if (Cmp.getOpcode() != ARMISD::CMPZ)
9317 // Only looking at EQ and NE cases.
9318 return SDValue();
9319
9320 EVT VT = N->getValueType(0);
9321 DebugLoc dl = N->getDebugLoc();
9322 SDValue LHS = Cmp.getOperand(0);
9323 SDValue RHS = Cmp.getOperand(1);
9324 SDValue FalseVal = N->getOperand(0);
9325 SDValue TrueVal = N->getOperand(1);
9326 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009327 ARMCC::CondCodes CC =
9328 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009329
9330 // Simplify
9331 // mov r1, r0
9332 // cmp r1, x
9333 // mov r0, y
9334 // moveq r0, x
9335 // to
9336 // cmp r0, x
9337 // movne r0, y
9338 //
9339 // mov r1, r0
9340 // cmp r1, x
9341 // mov r0, x
9342 // movne r0, y
9343 // to
9344 // cmp r0, x
9345 // movne r0, y
9346 /// FIXME: Turn this into a target neutral optimization?
9347 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009348 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009349 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9350 N->getOperand(3), Cmp);
9351 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9352 SDValue ARMcc;
9353 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9354 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9355 N->getOperand(3), NewCmp);
9356 }
9357
9358 if (Res.getNode()) {
9359 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009360 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009361 // Capture demanded bits information that would be otherwise lost.
9362 if (KnownZero == 0xfffffffe)
9363 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9364 DAG.getValueType(MVT::i1));
9365 else if (KnownZero == 0xffffff00)
9366 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9367 DAG.getValueType(MVT::i8));
9368 else if (KnownZero == 0xffff0000)
9369 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9370 DAG.getValueType(MVT::i16));
9371 }
9372
9373 return Res;
9374}
9375
Dan Gohman475871a2008-07-27 21:46:04 +00009376SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009377 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009378 switch (N->getOpcode()) {
9379 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009380 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009381 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009382 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009383 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009384 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009385 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9386 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009387 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009388 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009389 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009390 case ISD::STORE: return PerformSTORECombine(N, DCI);
9391 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9392 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009393 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009394 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009395 case ISD::FP_TO_SINT:
9396 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9397 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009398 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009399 case ISD::SHL:
9400 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009401 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009402 case ISD::SIGN_EXTEND:
9403 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009404 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9405 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009406 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009407 case ARMISD::VLD2DUP:
9408 case ARMISD::VLD3DUP:
9409 case ARMISD::VLD4DUP:
9410 return CombineBaseUpdate(N, DCI);
9411 case ISD::INTRINSIC_VOID:
9412 case ISD::INTRINSIC_W_CHAIN:
9413 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9414 case Intrinsic::arm_neon_vld1:
9415 case Intrinsic::arm_neon_vld2:
9416 case Intrinsic::arm_neon_vld3:
9417 case Intrinsic::arm_neon_vld4:
9418 case Intrinsic::arm_neon_vld2lane:
9419 case Intrinsic::arm_neon_vld3lane:
9420 case Intrinsic::arm_neon_vld4lane:
9421 case Intrinsic::arm_neon_vst1:
9422 case Intrinsic::arm_neon_vst2:
9423 case Intrinsic::arm_neon_vst3:
9424 case Intrinsic::arm_neon_vst4:
9425 case Intrinsic::arm_neon_vst2lane:
9426 case Intrinsic::arm_neon_vst3lane:
9427 case Intrinsic::arm_neon_vst4lane:
9428 return CombineBaseUpdate(N, DCI);
9429 default: break;
9430 }
9431 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009432 }
Dan Gohman475871a2008-07-27 21:46:04 +00009433 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009434}
9435
Evan Cheng31959b12011-02-02 01:06:55 +00009436bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9437 EVT VT) const {
9438 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9439}
9440
Evan Cheng376642e2012-12-10 23:21:26 +00009441bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009442 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009443 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009444
9445 switch (VT.getSimpleVT().SimpleTy) {
9446 default:
9447 return false;
9448 case MVT::i8:
9449 case MVT::i16:
Evan Cheng376642e2012-12-10 23:21:26 +00009450 case MVT::i32: {
Evan Chengd10eab02012-09-18 01:42:45 +00009451 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng376642e2012-12-10 23:21:26 +00009452 if (AllowsUnaligned) {
9453 if (Fast)
9454 *Fast = Subtarget->hasV7Ops();
9455 return true;
9456 }
9457 return false;
9458 }
Evan Chenga99c5082012-08-15 17:44:53 +00009459 case MVT::f64:
Evan Cheng376642e2012-12-10 23:21:26 +00009460 case MVT::v2f64: {
Evan Chengd10eab02012-09-18 01:42:45 +00009461 // For any little-endian targets with neon, we can support unaligned ld/st
9462 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9463 // A big-endian target may also explictly support unaligned accesses
Evan Cheng376642e2012-12-10 23:21:26 +00009464 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9465 if (Fast)
9466 *Fast = true;
9467 return true;
9468 }
9469 return false;
9470 }
Bill Wendlingaf566342009-08-15 21:21:19 +00009471 }
9472}
9473
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009474static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9475 unsigned AlignCheck) {
9476 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9477 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9478}
9479
9480EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9481 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00009482 bool IsMemset, bool ZeroMemset,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009483 bool MemcpyStrSrc,
9484 MachineFunction &MF) const {
9485 const Function *F = MF.getFunction();
9486
9487 // See if we can use NEON instructions for this...
Evan Cheng946a3a92012-12-12 02:34:41 +00009488 if ((!IsMemset || ZeroMemset) &&
Evan Cheng376642e2012-12-10 23:21:26 +00009489 Subtarget->hasNEON() &&
Bill Wendling831737d2012-12-30 10:32:01 +00009490 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9491 Attribute::NoImplicitFloat)) {
Evan Cheng376642e2012-12-10 23:21:26 +00009492 bool Fast;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009493 if (Size >= 16 &&
9494 (memOpAlign(SrcAlign, DstAlign, 16) ||
9495 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009496 return MVT::v2f64;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009497 } else if (Size >= 8 &&
9498 (memOpAlign(SrcAlign, DstAlign, 8) ||
9499 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009500 return MVT::f64;
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009501 }
9502 }
9503
Lang Hames5207bf22011-11-08 18:56:23 +00009504 // Lowering to i32/i16 if the size permits.
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009505 if (Size >= 4)
Lang Hames5207bf22011-11-08 18:56:23 +00009506 return MVT::i32;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009507 else if (Size >= 2)
Lang Hames5207bf22011-11-08 18:56:23 +00009508 return MVT::i16;
Lang Hames5207bf22011-11-08 18:56:23 +00009509
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009510 // Let the target-independent logic figure it out.
9511 return MVT::Other;
9512}
9513
Evan Cheng2766a472012-12-06 19:13:27 +00009514bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9515 if (Val.getOpcode() != ISD::LOAD)
9516 return false;
9517
9518 EVT VT1 = Val.getValueType();
9519 if (!VT1.isSimple() || !VT1.isInteger() ||
9520 !VT2.isSimple() || !VT2.isInteger())
9521 return false;
9522
9523 switch (VT1.getSimpleVT().SimpleTy) {
9524 default: break;
9525 case MVT::i1:
9526 case MVT::i8:
9527 case MVT::i16:
9528 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9529 return true;
9530 }
9531
9532 return false;
9533}
9534
Evan Chenge6c835f2009-08-14 20:09:37 +00009535static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9536 if (V < 0)
9537 return false;
9538
9539 unsigned Scale = 1;
9540 switch (VT.getSimpleVT().SimpleTy) {
9541 default: return false;
9542 case MVT::i1:
9543 case MVT::i8:
9544 // Scale == 1;
9545 break;
9546 case MVT::i16:
9547 // Scale == 2;
9548 Scale = 2;
9549 break;
9550 case MVT::i32:
9551 // Scale == 4;
9552 Scale = 4;
9553 break;
9554 }
9555
9556 if ((V & (Scale - 1)) != 0)
9557 return false;
9558 V /= Scale;
9559 return V == (V & ((1LL << 5) - 1));
9560}
9561
9562static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9563 const ARMSubtarget *Subtarget) {
9564 bool isNeg = false;
9565 if (V < 0) {
9566 isNeg = true;
9567 V = - V;
9568 }
9569
9570 switch (VT.getSimpleVT().SimpleTy) {
9571 default: return false;
9572 case MVT::i1:
9573 case MVT::i8:
9574 case MVT::i16:
9575 case MVT::i32:
9576 // + imm12 or - imm8
9577 if (isNeg)
9578 return V == (V & ((1LL << 8) - 1));
9579 return V == (V & ((1LL << 12) - 1));
9580 case MVT::f32:
9581 case MVT::f64:
9582 // Same as ARM mode. FIXME: NEON?
9583 if (!Subtarget->hasVFP2())
9584 return false;
9585 if ((V & 3) != 0)
9586 return false;
9587 V >>= 2;
9588 return V == (V & ((1LL << 8) - 1));
9589 }
9590}
9591
Evan Chengb01fad62007-03-12 23:30:29 +00009592/// isLegalAddressImmediate - Return true if the integer value can be used
9593/// as the offset of the target addressing mode for load / store of the
9594/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009595static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009596 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009597 if (V == 0)
9598 return true;
9599
Evan Cheng65011532009-03-09 19:15:00 +00009600 if (!VT.isSimple())
9601 return false;
9602
Evan Chenge6c835f2009-08-14 20:09:37 +00009603 if (Subtarget->isThumb1Only())
9604 return isLegalT1AddressImmediate(V, VT);
9605 else if (Subtarget->isThumb2())
9606 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009607
Evan Chenge6c835f2009-08-14 20:09:37 +00009608 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009609 if (V < 0)
9610 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009611 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009612 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009613 case MVT::i1:
9614 case MVT::i8:
9615 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009616 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009617 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009618 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009619 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009620 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009621 case MVT::f32:
9622 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009623 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009624 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009625 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009626 return false;
9627 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009628 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009629 }
Evan Chenga8e29892007-01-19 07:51:42 +00009630}
9631
Evan Chenge6c835f2009-08-14 20:09:37 +00009632bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9633 EVT VT) const {
9634 int Scale = AM.Scale;
9635 if (Scale < 0)
9636 return false;
9637
9638 switch (VT.getSimpleVT().SimpleTy) {
9639 default: return false;
9640 case MVT::i1:
9641 case MVT::i8:
9642 case MVT::i16:
9643 case MVT::i32:
9644 if (Scale == 1)
9645 return true;
9646 // r + r << imm
9647 Scale = Scale & ~1;
9648 return Scale == 2 || Scale == 4 || Scale == 8;
9649 case MVT::i64:
9650 // r + r
9651 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9652 return true;
9653 return false;
9654 case MVT::isVoid:
9655 // Note, we allow "void" uses (basically, uses that aren't loads or
9656 // stores), because arm allows folding a scale into many arithmetic
9657 // operations. This should be made more precise and revisited later.
9658
9659 // Allow r << imm, but the imm has to be a multiple of two.
9660 if (Scale & 1) return false;
9661 return isPowerOf2_32(Scale);
9662 }
9663}
9664
Chris Lattner37caf8c2007-04-09 23:33:39 +00009665/// isLegalAddressingMode - Return true if the addressing mode represented
9666/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009667bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009668 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009669 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009670 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009671 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009672
Chris Lattner37caf8c2007-04-09 23:33:39 +00009673 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009674 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009675 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009676
Chris Lattner37caf8c2007-04-09 23:33:39 +00009677 switch (AM.Scale) {
9678 case 0: // no scale reg, must be "r+i" or "r", or "i".
9679 break;
9680 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009681 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009682 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009683 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009684 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009685 // ARM doesn't support any R+R*scale+imm addr modes.
9686 if (AM.BaseOffs)
9687 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009688
Bob Wilson2c7dab12009-04-08 17:55:28 +00009689 if (!VT.isSimple())
9690 return false;
9691
Evan Chenge6c835f2009-08-14 20:09:37 +00009692 if (Subtarget->isThumb2())
9693 return isLegalT2ScaledAddressingMode(AM, VT);
9694
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009695 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009696 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009697 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009698 case MVT::i1:
9699 case MVT::i8:
9700 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009701 if (Scale < 0) Scale = -Scale;
9702 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009703 return true;
9704 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009705 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009706 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009707 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009708 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009709 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009710 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009711 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009712
Owen Anderson825b72b2009-08-11 20:47:22 +00009713 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009714 // Note, we allow "void" uses (basically, uses that aren't loads or
9715 // stores), because arm allows folding a scale into many arithmetic
9716 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009717
Chris Lattner37caf8c2007-04-09 23:33:39 +00009718 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009719 if (Scale & 1) return false;
9720 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009721 }
Evan Chengb01fad62007-03-12 23:30:29 +00009722 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009723 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009724}
9725
Evan Cheng77e47512009-11-11 19:05:52 +00009726/// isLegalICmpImmediate - Return true if the specified immediate is legal
9727/// icmp immediate, that is the target has icmp instructions which can compare
9728/// a register against the immediate without having to materialize the
9729/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009730bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009731 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009732 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009733 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009734 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009735 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009736 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009737 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009738}
9739
Andrew Trick8d8d9612012-07-18 18:34:27 +00009740/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9741/// *or sub* immediate, that is the target has add or sub instructions which can
9742/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009743/// immediate into a register.
9744bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009745 // Same encoding for add/sub, just flip the sign.
9746 int64_t AbsImm = llvm::abs64(Imm);
9747 if (!Subtarget->isThumb())
9748 return ARM_AM::getSOImmVal(AbsImm) != -1;
9749 if (Subtarget->isThumb2())
9750 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9751 // Thumb1 only has 8-bit unsigned immediate.
9752 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009753}
9754
Owen Andersone50ed302009-08-10 22:56:29 +00009755static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009756 bool isSEXTLoad, SDValue &Base,
9757 SDValue &Offset, bool &isInc,
9758 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009759 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9760 return false;
9761
Owen Anderson825b72b2009-08-11 20:47:22 +00009762 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009763 // AddressingMode 3
9764 Base = Ptr->getOperand(0);
9765 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009766 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009767 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009768 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009769 isInc = false;
9770 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9771 return true;
9772 }
9773 }
9774 isInc = (Ptr->getOpcode() == ISD::ADD);
9775 Offset = Ptr->getOperand(1);
9776 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009777 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009778 // AddressingMode 2
9779 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009780 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009781 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009782 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009783 isInc = false;
9784 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9785 Base = Ptr->getOperand(0);
9786 return true;
9787 }
9788 }
9789
9790 if (Ptr->getOpcode() == ISD::ADD) {
9791 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009792 ARM_AM::ShiftOpc ShOpcVal=
9793 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009794 if (ShOpcVal != ARM_AM::no_shift) {
9795 Base = Ptr->getOperand(1);
9796 Offset = Ptr->getOperand(0);
9797 } else {
9798 Base = Ptr->getOperand(0);
9799 Offset = Ptr->getOperand(1);
9800 }
9801 return true;
9802 }
9803
9804 isInc = (Ptr->getOpcode() == ISD::ADD);
9805 Base = Ptr->getOperand(0);
9806 Offset = Ptr->getOperand(1);
9807 return true;
9808 }
9809
Jim Grosbache5165492009-11-09 00:11:35 +00009810 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009811 return false;
9812}
9813
Owen Andersone50ed302009-08-10 22:56:29 +00009814static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009815 bool isSEXTLoad, SDValue &Base,
9816 SDValue &Offset, bool &isInc,
9817 SelectionDAG &DAG) {
9818 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9819 return false;
9820
9821 Base = Ptr->getOperand(0);
9822 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9823 int RHSC = (int)RHS->getZExtValue();
9824 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9825 assert(Ptr->getOpcode() == ISD::ADD);
9826 isInc = false;
9827 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9828 return true;
9829 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9830 isInc = Ptr->getOpcode() == ISD::ADD;
9831 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9832 return true;
9833 }
9834 }
9835
9836 return false;
9837}
9838
Evan Chenga8e29892007-01-19 07:51:42 +00009839/// getPreIndexedAddressParts - returns true by value, base pointer and
9840/// offset pointer and addressing mode by reference if the node's address
9841/// can be legally represented as pre-indexed load / store address.
9842bool
Dan Gohman475871a2008-07-27 21:46:04 +00009843ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9844 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009845 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009846 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009847 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009848 return false;
9849
Owen Andersone50ed302009-08-10 22:56:29 +00009850 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009851 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009852 bool isSEXTLoad = false;
9853 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9854 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009855 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009856 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9857 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9858 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009859 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009860 } else
9861 return false;
9862
9863 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009864 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009865 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009866 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9867 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009868 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009869 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009870 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009871 if (!isLegal)
9872 return false;
9873
9874 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9875 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009876}
9877
9878/// getPostIndexedAddressParts - returns true by value, base pointer and
9879/// offset pointer and addressing mode by reference if this node can be
9880/// combined with a load / store to form a post-indexed load / store.
9881bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009882 SDValue &Base,
9883 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009884 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009885 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009886 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009887 return false;
9888
Owen Andersone50ed302009-08-10 22:56:29 +00009889 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009890 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009891 bool isSEXTLoad = false;
9892 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009893 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009894 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009895 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9896 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009897 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009898 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009899 } else
9900 return false;
9901
9902 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009903 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009904 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009905 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009906 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009907 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009908 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9909 isInc, DAG);
9910 if (!isLegal)
9911 return false;
9912
Evan Cheng28dad2a2010-05-18 21:31:17 +00009913 if (Ptr != Base) {
9914 // Swap base ptr and offset to catch more post-index load / store when
9915 // it's legal. In Thumb2 mode, offset must be an immediate.
9916 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9917 !Subtarget->isThumb2())
9918 std::swap(Base, Offset);
9919
9920 // Post-indexed load / store update the base pointer.
9921 if (Ptr != Base)
9922 return false;
9923 }
9924
Evan Chenge88d5ce2009-07-02 07:28:31 +00009925 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9926 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009927}
9928
Dan Gohman475871a2008-07-27 21:46:04 +00009929void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009930 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009931 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009932 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009933 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009934 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009935 switch (Op.getOpcode()) {
9936 default: break;
9937 case ARMISD::CMOV: {
9938 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009939 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009940 if (KnownZero == 0 && KnownOne == 0) return;
9941
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009942 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009943 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009944 KnownZero &= KnownZeroRHS;
9945 KnownOne &= KnownOneRHS;
9946 return;
9947 }
9948 }
9949}
9950
9951//===----------------------------------------------------------------------===//
9952// ARM Inline Assembly Support
9953//===----------------------------------------------------------------------===//
9954
Evan Cheng55d42002011-01-08 01:24:27 +00009955bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9956 // Looking for "rev" which is V6+.
9957 if (!Subtarget->hasV6Ops())
9958 return false;
9959
9960 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9961 std::string AsmStr = IA->getAsmString();
9962 SmallVector<StringRef, 4> AsmPieces;
9963 SplitString(AsmStr, AsmPieces, ";\n");
9964
9965 switch (AsmPieces.size()) {
9966 default: return false;
9967 case 1:
9968 AsmStr = AsmPieces[0];
9969 AsmPieces.clear();
9970 SplitString(AsmStr, AsmPieces, " \t,");
9971
9972 // rev $0, $1
9973 if (AsmPieces.size() == 3 &&
9974 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9975 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009976 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009977 if (Ty && Ty->getBitWidth() == 32)
9978 return IntrinsicLowering::LowerToByteSwap(CI);
9979 }
9980 break;
9981 }
9982
9983 return false;
9984}
9985
Evan Chenga8e29892007-01-19 07:51:42 +00009986/// getConstraintType - Given a constraint letter, return the type of
9987/// constraint it is for this target.
9988ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009989ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9990 if (Constraint.size() == 1) {
9991 switch (Constraint[0]) {
9992 default: break;
9993 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009994 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009995 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009996 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009997 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009998 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009999 // An address with a single base register. Due to the way we
10000 // currently handle addresses it is the same as an 'r' memory constraint.
10001 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +000010002 }
Eric Christopher1312ca82011-06-21 22:10:57 +000010003 } else if (Constraint.size() == 2) {
10004 switch (Constraint[0]) {
10005 default: break;
10006 // All 'U+' constraints are addresses.
10007 case 'U': return C_Memory;
10008 }
Evan Chenga8e29892007-01-19 07:51:42 +000010009 }
Chris Lattner4234f572007-03-25 02:14:49 +000010010 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +000010011}
10012
John Thompson44ab89e2010-10-29 17:29:13 +000010013/// Examine constraint type and operand type and determine a weight value.
10014/// This object must already have been set up with the operand type
10015/// and the current alternative constraint selected.
10016TargetLowering::ConstraintWeight
10017ARMTargetLowering::getSingleConstraintMatchWeight(
10018 AsmOperandInfo &info, const char *constraint) const {
10019 ConstraintWeight weight = CW_Invalid;
10020 Value *CallOperandVal = info.CallOperandVal;
10021 // If we don't have a value, we can't do a match,
10022 // but allow it at the lowest weight.
10023 if (CallOperandVal == NULL)
10024 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010025 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +000010026 // Look at the constraint type.
10027 switch (*constraint) {
10028 default:
10029 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10030 break;
10031 case 'l':
10032 if (type->isIntegerTy()) {
10033 if (Subtarget->isThumb())
10034 weight = CW_SpecificReg;
10035 else
10036 weight = CW_Register;
10037 }
10038 break;
10039 case 'w':
10040 if (type->isFloatingPointTy())
10041 weight = CW_Register;
10042 break;
10043 }
10044 return weight;
10045}
10046
Eric Christopher35e6d4d2011-06-30 23:50:52 +000010047typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10048RCPair
Evan Chenga8e29892007-01-19 07:51:42 +000010049ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010050 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +000010051 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010052 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +000010053 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +000010054 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010055 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010056 return RCPair(0U, &ARM::tGPRRegClass);
10057 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +000010058 case 'h': // High regs or no regs.
10059 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010060 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +000010061 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010062 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +000010063 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010064 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +000010065 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010066 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +000010067 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010068 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +000010069 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010070 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010071 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010072 case 'x':
10073 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010074 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010075 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010076 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010077 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010078 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010079 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010080 case 't':
10081 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010082 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010083 break;
Evan Chenga8e29892007-01-19 07:51:42 +000010084 }
10085 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010086 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +000010087 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010088
Evan Chenga8e29892007-01-19 07:51:42 +000010089 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10090}
10091
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010092/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10093/// vector. If it is invalid, don't add anything to Ops.
10094void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000010095 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010096 std::vector<SDValue>&Ops,
10097 SelectionDAG &DAG) const {
10098 SDValue Result(0, 0);
10099
Eric Christopher100c8332011-06-02 23:16:42 +000010100 // Currently only support length 1 constraints.
10101 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000010102
Eric Christopher100c8332011-06-02 23:16:42 +000010103 char ConstraintLetter = Constraint[0];
10104 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010105 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +000010106 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010107 case 'I': case 'J': case 'K': case 'L':
10108 case 'M': case 'N': case 'O':
10109 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10110 if (!C)
10111 return;
10112
10113 int64_t CVal64 = C->getSExtValue();
10114 int CVal = (int) CVal64;
10115 // None of these constraints allow values larger than 32 bits. Check
10116 // that the value fits in an int.
10117 if (CVal != CVal64)
10118 return;
10119
Eric Christopher100c8332011-06-02 23:16:42 +000010120 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +000010121 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +000010122 // Constant suitable for movw, must be between 0 and
10123 // 65535.
10124 if (Subtarget->hasV6T2Ops())
10125 if (CVal >= 0 && CVal <= 65535)
10126 break;
10127 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010128 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010129 if (Subtarget->isThumb1Only()) {
10130 // This must be a constant between 0 and 255, for ADD
10131 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010132 if (CVal >= 0 && CVal <= 255)
10133 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010134 } else if (Subtarget->isThumb2()) {
10135 // A constant that can be used as an immediate value in a
10136 // data-processing instruction.
10137 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10138 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010139 } else {
10140 // A constant that can be used as an immediate value in a
10141 // data-processing instruction.
10142 if (ARM_AM::getSOImmVal(CVal) != -1)
10143 break;
10144 }
10145 return;
10146
10147 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010148 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010149 // This must be a constant between -255 and -1, for negated ADD
10150 // immediates. This can be used in GCC with an "n" modifier that
10151 // prints the negated value, for use with SUB instructions. It is
10152 // not useful otherwise but is implemented for compatibility.
10153 if (CVal >= -255 && CVal <= -1)
10154 break;
10155 } else {
10156 // This must be a constant between -4095 and 4095. It is not clear
10157 // what this constraint is intended for. Implemented for
10158 // compatibility with GCC.
10159 if (CVal >= -4095 && CVal <= 4095)
10160 break;
10161 }
10162 return;
10163
10164 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010165 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010166 // A 32-bit value where only one byte has a nonzero value. Exclude
10167 // zero to match GCC. This constraint is used by GCC internally for
10168 // constants that can be loaded with a move/shift combination.
10169 // It is not useful otherwise but is implemented for compatibility.
10170 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10171 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010172 } else if (Subtarget->isThumb2()) {
10173 // A constant whose bitwise inverse can be used as an immediate
10174 // value in a data-processing instruction. This can be used in GCC
10175 // with a "B" modifier that prints the inverted value, for use with
10176 // BIC and MVN instructions. It is not useful otherwise but is
10177 // implemented for compatibility.
10178 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10179 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010180 } else {
10181 // A constant whose bitwise inverse can be used as an immediate
10182 // value in a data-processing instruction. This can be used in GCC
10183 // with a "B" modifier that prints the inverted value, for use with
10184 // BIC and MVN instructions. It is not useful otherwise but is
10185 // implemented for compatibility.
10186 if (ARM_AM::getSOImmVal(~CVal) != -1)
10187 break;
10188 }
10189 return;
10190
10191 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010192 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010193 // This must be a constant between -7 and 7,
10194 // for 3-operand ADD/SUB immediate instructions.
10195 if (CVal >= -7 && CVal < 7)
10196 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010197 } else if (Subtarget->isThumb2()) {
10198 // A constant whose negation can be used as an immediate value in a
10199 // data-processing instruction. This can be used in GCC with an "n"
10200 // modifier that prints the negated value, for use with SUB
10201 // instructions. It is not useful otherwise but is implemented for
10202 // compatibility.
10203 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10204 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010205 } else {
10206 // A constant whose negation can be used as an immediate value in a
10207 // data-processing instruction. This can be used in GCC with an "n"
10208 // modifier that prints the negated value, for use with SUB
10209 // instructions. It is not useful otherwise but is implemented for
10210 // compatibility.
10211 if (ARM_AM::getSOImmVal(-CVal) != -1)
10212 break;
10213 }
10214 return;
10215
10216 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010217 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010218 // This must be a multiple of 4 between 0 and 1020, for
10219 // ADD sp + immediate.
10220 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10221 break;
10222 } else {
10223 // A power of two or a constant between 0 and 32. This is used in
10224 // GCC for the shift amount on shifted register operands, but it is
10225 // useful in general for any shift amounts.
10226 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10227 break;
10228 }
10229 return;
10230
10231 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010232 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010233 // This must be a constant between 0 and 31, for shift amounts.
10234 if (CVal >= 0 && CVal <= 31)
10235 break;
10236 }
10237 return;
10238
10239 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010240 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010241 // This must be a multiple of 4 between -508 and 508, for
10242 // ADD/SUB sp = sp + immediate.
10243 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10244 break;
10245 }
10246 return;
10247 }
10248 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10249 break;
10250 }
10251
10252 if (Result.getNode()) {
10253 Ops.push_back(Result);
10254 return;
10255 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010256 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010257}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010258
10259bool
10260ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10261 // The ARM target isn't yet aware of offsets.
10262 return false;
10263}
Evan Cheng39382422009-10-28 01:44:26 +000010264
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010265bool ARM::isBitFieldInvertedMask(unsigned v) {
10266 if (v == 0xffffffff)
10267 return 0;
10268 // there can be 1's on either or both "outsides", all the "inside"
10269 // bits must be 0's
10270 unsigned int lsb = 0, msb = 31;
10271 while (v & (1 << msb)) --msb;
10272 while (v & (1 << lsb)) ++lsb;
10273 for (unsigned int i = lsb; i <= msb; ++i) {
10274 if (v & (1 << i))
10275 return 0;
10276 }
10277 return 1;
10278}
10279
Evan Cheng39382422009-10-28 01:44:26 +000010280/// isFPImmLegal - Returns true if the target can instruction select the
10281/// specified FP immediate natively. If false, the legalizer will
10282/// materialize the FP immediate as a load from a constant pool.
10283bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10284 if (!Subtarget->hasVFP3())
10285 return false;
10286 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010287 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010288 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010289 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010290 return false;
10291}
Bob Wilson65ffec42010-09-21 17:56:22 +000010292
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010293/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010294/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10295/// specified in the intrinsic calls.
10296bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10297 const CallInst &I,
10298 unsigned Intrinsic) const {
10299 switch (Intrinsic) {
10300 case Intrinsic::arm_neon_vld1:
10301 case Intrinsic::arm_neon_vld2:
10302 case Intrinsic::arm_neon_vld3:
10303 case Intrinsic::arm_neon_vld4:
10304 case Intrinsic::arm_neon_vld2lane:
10305 case Intrinsic::arm_neon_vld3lane:
10306 case Intrinsic::arm_neon_vld4lane: {
10307 Info.opc = ISD::INTRINSIC_W_CHAIN;
10308 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010309 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010310 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10311 Info.ptrVal = I.getArgOperand(0);
10312 Info.offset = 0;
10313 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10314 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10315 Info.vol = false; // volatile loads with NEON intrinsics not supported
10316 Info.readMem = true;
10317 Info.writeMem = false;
10318 return true;
10319 }
10320 case Intrinsic::arm_neon_vst1:
10321 case Intrinsic::arm_neon_vst2:
10322 case Intrinsic::arm_neon_vst3:
10323 case Intrinsic::arm_neon_vst4:
10324 case Intrinsic::arm_neon_vst2lane:
10325 case Intrinsic::arm_neon_vst3lane:
10326 case Intrinsic::arm_neon_vst4lane: {
10327 Info.opc = ISD::INTRINSIC_VOID;
10328 // Conservatively set memVT to the entire set of vectors stored.
10329 unsigned NumElts = 0;
10330 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010331 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010332 if (!ArgTy->isVectorTy())
10333 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010334 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010335 }
10336 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10337 Info.ptrVal = I.getArgOperand(0);
10338 Info.offset = 0;
10339 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10340 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10341 Info.vol = false; // volatile stores with NEON intrinsics not supported
10342 Info.readMem = false;
10343 Info.writeMem = true;
10344 return true;
10345 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010346 case Intrinsic::arm_strexd: {
10347 Info.opc = ISD::INTRINSIC_W_CHAIN;
10348 Info.memVT = MVT::i64;
10349 Info.ptrVal = I.getArgOperand(2);
10350 Info.offset = 0;
10351 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010352 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010353 Info.readMem = false;
10354 Info.writeMem = true;
10355 return true;
10356 }
10357 case Intrinsic::arm_ldrexd: {
10358 Info.opc = ISD::INTRINSIC_W_CHAIN;
10359 Info.memVT = MVT::i64;
10360 Info.ptrVal = I.getArgOperand(0);
10361 Info.offset = 0;
10362 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010363 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010364 Info.readMem = true;
10365 Info.writeMem = false;
10366 return true;
10367 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010368 default:
10369 break;
10370 }
10371
10372 return false;
10373}