blob: 46af4571a069dc32859b08bff73d6a6aef80dbf0 [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);
Arnold Schwaighofer21c0aa72013-03-02 19:38:33 +0000507 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000508
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000509 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
510 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
511 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
512 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
513 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
515 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
516 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
517 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
518 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000519 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
520 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
521 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
522 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000523 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000524
Arnold Schwaighofer21c0aa72013-03-02 19:38:33 +0000525 // Mark v2f32 intrinsics.
526 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
527 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
528 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
529 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
530 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
531 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
532 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
533 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
534 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
535 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
536 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
537 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
538 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
539 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
540 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
541
Bob Wilson642b3292009-09-16 00:32:15 +0000542 // Neon does not support some operations on v1i64 and v2i64 types.
543 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000544 // Custom handling for some quad-vector types to detect VMULL.
545 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
546 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
547 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000548 // Custom handling for some vector types to avoid expensive expansions
549 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
550 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
551 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
552 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000553 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
554 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000555 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000556 // a destination type that is wider than the source, and nor does
557 // it have a FP_TO_[SU]INT instruction with a narrower destination than
558 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000559 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
560 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000561 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
562 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000563
Eli Friedman846ce8e2012-11-15 22:44:27 +0000564 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman43147af2012-11-17 01:52:46 +0000565 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedman846ce8e2012-11-15 22:44:27 +0000566
Renato Golin5ad5f592013-03-19 08:15:38 +0000567 // Custom expand long extensions to vectors.
568 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
569 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
570 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
571 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
572 setOperationAction(ISD::SIGN_EXTEND, MVT::v16i32, Custom);
573 setOperationAction(ISD::ZERO_EXTEND, MVT::v16i32, Custom);
574 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i64, Custom);
575 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i64, Custom);
576
Evan Chengc8e70452012-12-04 22:41:50 +0000577 // NEON does not have single instruction CTPOP for vectors with element
578 // types wider than 8-bits. However, custom lowering can leverage the
579 // v8i8/v16i8 vcnt instruction.
580 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
581 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
582 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
583 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
584
Jim Grosbachb302a4e2013-02-27 21:31:12 +0000585 // NEON only has FMA instructions as of VFP4.
586 if (!Subtarget->hasVFP4()) {
587 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
588 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
589 }
590
Bob Wilson1c3ef902011-02-07 17:43:21 +0000591 setTargetDAGCombine(ISD::INTRINSIC_VOID);
592 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000593 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
594 setTargetDAGCombine(ISD::SHL);
595 setTargetDAGCombine(ISD::SRL);
596 setTargetDAGCombine(ISD::SRA);
597 setTargetDAGCombine(ISD::SIGN_EXTEND);
598 setTargetDAGCombine(ISD::ZERO_EXTEND);
599 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000600 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000601 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000602 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000603 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
604 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000605 setTargetDAGCombine(ISD::FP_TO_SINT);
606 setTargetDAGCombine(ISD::FP_TO_UINT);
607 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000608
James Molloy873fd5f2012-02-20 09:24:05 +0000609 // It is legal to extload from v4i8 to v4i16 or v4i32.
610 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
611 MVT::v4i16, MVT::v2i16,
612 MVT::v2i32};
613 for (unsigned i = 0; i < 6; ++i) {
614 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
615 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
616 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
617 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000618 }
619
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000620 // ARM and Thumb2 support UMLAL/SMLAL.
621 if (!Subtarget->isThumb1Only())
622 setTargetDAGCombine(ISD::ADDC);
623
624
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000625 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000626
627 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000629
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000630 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000632
Evan Chenga8e29892007-01-19 07:51:42 +0000633 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000634 if (!Subtarget->isThumb1Only()) {
635 for (unsigned im = (unsigned)ISD::PRE_INC;
636 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setIndexedLoadAction(im, MVT::i1, Legal);
638 setIndexedLoadAction(im, MVT::i8, Legal);
639 setIndexedLoadAction(im, MVT::i16, Legal);
640 setIndexedLoadAction(im, MVT::i32, Legal);
641 setIndexedStoreAction(im, MVT::i1, Legal);
642 setIndexedStoreAction(im, MVT::i8, Legal);
643 setIndexedStoreAction(im, MVT::i16, Legal);
644 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000645 }
Evan Chenga8e29892007-01-19 07:51:42 +0000646 }
647
648 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000649 setOperationAction(ISD::MUL, MVT::i64, Expand);
650 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000651 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
653 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000654 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000655 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
656 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000657 setOperationAction(ISD::MULHS, MVT::i32, Expand);
658
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000659 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000660 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000661 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::SRL, MVT::i64, Custom);
663 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000664
Evan Cheng342e3162011-08-30 01:34:54 +0000665 if (!Subtarget->isThumb1Only()) {
666 // FIXME: We should do this for Thumb1 as well.
667 setOperationAction(ISD::ADDC, MVT::i32, Custom);
668 setOperationAction(ISD::ADDE, MVT::i32, Custom);
669 setOperationAction(ISD::SUBC, MVT::i32, Custom);
670 setOperationAction(ISD::SUBE, MVT::i32, Custom);
671 }
672
Evan Chenga8e29892007-01-19 07:51:42 +0000673 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000675 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000677 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000679
Chandler Carruth63974b22011-12-13 01:56:10 +0000680 // These just redirect to CTTZ and CTLZ on ARM.
681 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
682 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
683
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000684 // Only ARMv6 has BSWAP.
685 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000687
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000688 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
689 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
690 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000691 setOperationAction(ISD::SDIV, MVT::i32, Expand);
692 setOperationAction(ISD::UDIV, MVT::i32, Expand);
693 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::SREM, MVT::i32, Expand);
695 setOperationAction(ISD::UREM, MVT::i32, Expand);
696 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
697 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
700 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
701 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
702 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000703 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000704
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000705 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000706
Evan Chenga8e29892007-01-19 07:51:42 +0000707 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000708 setOperationAction(ISD::VASTART, MVT::Other, Custom);
709 setOperationAction(ISD::VAARG, MVT::Other, Expand);
710 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
711 setOperationAction(ISD::VAEND, MVT::Other, Expand);
712 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
713 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000714
715 if (!Subtarget->isTargetDarwin()) {
716 // Non-Darwin platforms may return values in these registers via the
717 // personality function.
718 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
719 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
720 setExceptionPointerRegister(ARM::R0);
721 setExceptionSelectorRegister(ARM::R1);
722 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000723
Evan Cheng3a1588a2010-04-15 22:20:34 +0000724 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000725 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
726 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000727 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000728 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000729 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000730 // membarrier needs custom lowering; the rest are legal and handled
731 // normally.
Eli Friedman14648462011-07-27 22:21:52 +0000732 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000733 // Custom lowering for 64-bit ops
734 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
735 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
736 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
737 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
738 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
Silviu Baranga35b3df62012-11-29 14:41:25 +0000739 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
740 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
741 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
742 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
743 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000744 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000745 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
746 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000747 } else {
748 // Set them all for expansion, which will force libcalls.
Eli Friedman14648462011-07-27 22:21:52 +0000749 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000750 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000751 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000752 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000753 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000754 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000755 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000756 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000757 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000758 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000759 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000760 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000761 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000762 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
763 // Unordered/Monotonic case.
764 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
765 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach68741be2010-06-18 22:35:32 +0000766 }
Evan Chenga8e29892007-01-19 07:51:42 +0000767
Evan Cheng416941d2010-11-04 05:19:35 +0000768 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000769
Eli Friedmana2c6f452010-06-26 04:36:50 +0000770 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
771 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
773 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000774 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000776
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000777 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
778 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000779 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000780 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000781 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000782 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
783 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000784
785 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000787 if (Subtarget->isTargetDarwin()) {
788 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
789 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000790 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000791 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000792
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 setOperationAction(ISD::SETCC, MVT::i32, Expand);
794 setOperationAction(ISD::SETCC, MVT::f32, Expand);
795 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000796 setOperationAction(ISD::SELECT, MVT::i32, Custom);
797 setOperationAction(ISD::SELECT, MVT::f32, Custom);
798 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
800 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
801 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000802
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
804 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
805 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
806 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
807 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000808
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000809 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setOperationAction(ISD::FSIN, MVT::f64, Expand);
811 setOperationAction(ISD::FSIN, MVT::f32, Expand);
812 setOperationAction(ISD::FCOS, MVT::f32, Expand);
813 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000814 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
815 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 setOperationAction(ISD::FREM, MVT::f64, Expand);
817 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000818 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
819 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
821 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000822 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 setOperationAction(ISD::FPOW, MVT::f64, Expand);
824 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000825
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000826 if (!Subtarget->hasVFP4()) {
827 setOperationAction(ISD::FMA, MVT::f64, Expand);
828 setOperationAction(ISD::FMA, MVT::f32, Expand);
829 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000830
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000831 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000832 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000833 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
834 if (Subtarget->hasVFP2()) {
835 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
836 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
837 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
838 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
839 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000840 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000841 if (!Subtarget->hasFP16()) {
842 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
843 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000844 }
Evan Cheng110cf482008-04-01 01:50:16 +0000845 }
Evan Chenga8e29892007-01-19 07:51:42 +0000846
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000847 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000848 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000849 setTargetDAGCombine(ISD::ADD);
850 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000851 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000852 setTargetDAGCombine(ISD::AND);
853 setTargetDAGCombine(ISD::OR);
854 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000855
Evan Cheng5fb468a2012-02-23 02:58:19 +0000856 if (Subtarget->hasV6Ops())
857 setTargetDAGCombine(ISD::SRL);
858
Evan Chenga8e29892007-01-19 07:51:42 +0000859 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000860
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000861 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
862 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000863 setSchedulingPreference(Sched::RegPressure);
864 else
865 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000866
Evan Cheng05219282011-01-06 06:52:41 +0000867 //// temporary - rewrite interface to use type
Jim Grosbach3450f802013-02-20 21:13:59 +0000868 MaxStoresPerMemset = 8;
869 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
870 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
871 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
872 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
873 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengf6799392010-06-26 01:52:05 +0000874
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000875 // On ARM arguments smaller than 4 bytes are extended, so all arguments
876 // are at least 4 bytes aligned.
877 setMinStackArgumentAlignment(4);
878
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000879 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach3450f802013-02-20 21:13:59 +0000880 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000881
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000882 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000883}
884
Andrew Trick32cec0a2011-01-19 02:35:27 +0000885// FIXME: It might make sense to define the representative register class as the
886// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
887// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
888// SPR's representative would be DPR_VFP2. This should work well if register
889// pressure tracking were modified such that a register use would increment the
890// pressure of the register class's representative and all of it's super
891// classes' representatives transitively. We have not implemented this because
892// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000893// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000894// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000895std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +0000896ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Cheng4f6b4672010-07-21 06:09:07 +0000897 const TargetRegisterClass *RRC = 0;
898 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +0000899 switch (VT.SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000900 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000901 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000902 // Use DPR as representative register class for all floating point
903 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
904 // the cost is 1 for both f32 and f64.
905 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000906 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000907 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000908 // When NEON is used for SP, only half of the register file is available
909 // because operations that define both SP and DP results will be constrained
910 // to the VFP2 class (D0-D15). We currently model this constraint prior to
911 // coalescing by double-counting the SP regs. See the FIXME above.
912 if (Subtarget->useNEONForSinglePrecisionFP())
913 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000914 break;
915 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
916 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000917 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000918 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000919 break;
920 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000921 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000922 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000923 break;
924 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000925 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000926 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000927 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000928 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000929 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000930}
931
Evan Chenga8e29892007-01-19 07:51:42 +0000932const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
933 switch (Opcode) {
934 default: return 0;
935 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000936 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000937 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000938 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
939 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000940 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000941 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
942 case ARMISD::tCALL: return "ARMISD::tCALL";
943 case ARMISD::BRCOND: return "ARMISD::BRCOND";
944 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000945 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000946 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
947 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
948 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000949 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000950 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000951 case ARMISD::CMPFP: return "ARMISD::CMPFP";
952 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000953 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000954 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000955
Evan Chenga8e29892007-01-19 07:51:42 +0000956 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000957
Jim Grosbach3482c802010-01-18 19:58:49 +0000958 case ARMISD::RBIT: return "ARMISD::RBIT";
959
Bob Wilson76a312b2010-03-19 22:51:32 +0000960 case ARMISD::FTOSI: return "ARMISD::FTOSI";
961 case ARMISD::FTOUI: return "ARMISD::FTOUI";
962 case ARMISD::SITOF: return "ARMISD::SITOF";
963 case ARMISD::UITOF: return "ARMISD::UITOF";
964
Evan Chenga8e29892007-01-19 07:51:42 +0000965 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
966 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
967 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000968
Evan Cheng342e3162011-08-30 01:34:54 +0000969 case ARMISD::ADDC: return "ARMISD::ADDC";
970 case ARMISD::ADDE: return "ARMISD::ADDE";
971 case ARMISD::SUBC: return "ARMISD::SUBC";
972 case ARMISD::SUBE: return "ARMISD::SUBE";
973
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000974 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
975 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000976
Evan Chengc5942082009-10-28 06:55:03 +0000977 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
978 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
979
Dale Johannesen51e28e62010-06-03 21:09:53 +0000980 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000981
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000982 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000983
Evan Cheng86198642009-08-07 00:34:42 +0000984 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
985
Jim Grosbach3728e962009-12-10 00:11:09 +0000986 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000987 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000988
Evan Chengdfed19f2010-11-03 06:34:55 +0000989 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
990
Bob Wilson5bafff32009-06-22 23:27:02 +0000991 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000992 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000993 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000994 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
995 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000996 case ARMISD::VCGEU: return "ARMISD::VCGEU";
997 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000998 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
999 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +00001000 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1001 case ARMISD::VTST: return "ARMISD::VTST";
1002
1003 case ARMISD::VSHL: return "ARMISD::VSHL";
1004 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1005 case ARMISD::VSHRu: return "ARMISD::VSHRu";
1006 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
1007 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
1008 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
1009 case ARMISD::VSHRN: return "ARMISD::VSHRN";
1010 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1011 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1012 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1013 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1014 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1015 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1016 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1017 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1018 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1019 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1020 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1021 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1022 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1023 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +00001024 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +00001025 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +00001026 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +00001027 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +00001028 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +00001029 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +00001030 case ARMISD::VREV64: return "ARMISD::VREV64";
1031 case ARMISD::VREV32: return "ARMISD::VREV32";
1032 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001033 case ARMISD::VZIP: return "ARMISD::VZIP";
1034 case ARMISD::VUZP: return "ARMISD::VUZP";
1035 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +00001036 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1037 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +00001038 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1039 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00001040 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1041 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001042 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001043 case ARMISD::FMAX: return "ARMISD::FMAX";
1044 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001045 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001046 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1047 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001048 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001049 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1050 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1051 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001052 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1053 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1054 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1055 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1056 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1057 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1058 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1059 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1060 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1061 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1062 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1063 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1064 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1065 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1066 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1067 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1068 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001069 }
1070}
1071
Duncan Sands28b77e92011-09-06 19:07:46 +00001072EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1073 if (!VT.isVector()) return getPointerTy();
1074 return VT.changeVectorElementTypeToInteger();
1075}
1076
Evan Cheng06b666c2010-05-15 02:18:07 +00001077/// getRegClassFor - Return the register class that should be used for the
1078/// specified value type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001079const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001080 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1081 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1082 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001083 if (Subtarget->hasNEON()) {
1084 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001085 return &ARM::QQPRRegClass;
1086 if (VT == MVT::v8i64)
1087 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001088 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001089 return TargetLowering::getRegClassFor(VT);
1090}
1091
Eric Christopherab695882010-07-21 22:26:11 +00001092// Create a fast isel object.
1093FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001094ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1095 const TargetLibraryInfo *libInfo) const {
1096 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001097}
1098
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001099/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1100/// be used for loads / stores from the global.
1101unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1102 return (Subtarget->isThumb1Only() ? 127 : 4095);
1103}
1104
Evan Cheng1cc39842010-05-20 23:26:43 +00001105Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001106 unsigned NumVals = N->getNumValues();
1107 if (!NumVals)
1108 return Sched::RegPressure;
1109
1110 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001111 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001112 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001113 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001114 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001115 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001116 }
Evan Chengc10f5432010-05-28 23:25:23 +00001117
1118 if (!N->isMachineOpcode())
1119 return Sched::RegPressure;
1120
1121 // Load are scheduled for latency even if there instruction itinerary
1122 // is not available.
1123 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001124 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001125
Evan Chenge837dea2011-06-28 19:10:37 +00001126 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001127 return Sched::RegPressure;
1128 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001129 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001130 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001131
Evan Cheng1cc39842010-05-20 23:26:43 +00001132 return Sched::RegPressure;
1133}
1134
Evan Chenga8e29892007-01-19 07:51:42 +00001135//===----------------------------------------------------------------------===//
1136// Lowering Code
1137//===----------------------------------------------------------------------===//
1138
Evan Chenga8e29892007-01-19 07:51:42 +00001139/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1140static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1141 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001142 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001143 case ISD::SETNE: return ARMCC::NE;
1144 case ISD::SETEQ: return ARMCC::EQ;
1145 case ISD::SETGT: return ARMCC::GT;
1146 case ISD::SETGE: return ARMCC::GE;
1147 case ISD::SETLT: return ARMCC::LT;
1148 case ISD::SETLE: return ARMCC::LE;
1149 case ISD::SETUGT: return ARMCC::HI;
1150 case ISD::SETUGE: return ARMCC::HS;
1151 case ISD::SETULT: return ARMCC::LO;
1152 case ISD::SETULE: return ARMCC::LS;
1153 }
1154}
1155
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001156/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1157static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001158 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001159 CondCode2 = ARMCC::AL;
1160 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001161 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001162 case ISD::SETEQ:
1163 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1164 case ISD::SETGT:
1165 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1166 case ISD::SETGE:
1167 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1168 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001169 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001170 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1171 case ISD::SETO: CondCode = ARMCC::VC; break;
1172 case ISD::SETUO: CondCode = ARMCC::VS; break;
1173 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1174 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1175 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1176 case ISD::SETLT:
1177 case ISD::SETULT: CondCode = ARMCC::LT; break;
1178 case ISD::SETLE:
1179 case ISD::SETULE: CondCode = ARMCC::LE; break;
1180 case ISD::SETNE:
1181 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1182 }
Evan Chenga8e29892007-01-19 07:51:42 +00001183}
1184
Bob Wilson1f595bb2009-04-17 19:07:39 +00001185//===----------------------------------------------------------------------===//
1186// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001187//===----------------------------------------------------------------------===//
1188
1189#include "ARMGenCallingConv.inc"
1190
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001191/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1192/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001193CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001194 bool Return,
1195 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001196 switch (CC) {
1197 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001198 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001199 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001200 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001201 if (!Subtarget->isAAPCS_ABI())
1202 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1203 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1204 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1205 }
1206 // Fallthrough
1207 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001208 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001209 if (!Subtarget->isAAPCS_ABI())
1210 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1211 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001212 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1213 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001214 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1215 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1216 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001217 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001218 if (!isVarArg)
1219 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1220 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001221 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001222 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001223 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001224 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001225 case CallingConv::GHC:
1226 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001227 }
1228}
1229
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230/// LowerCallResult - Lower the result values of a call into the
1231/// appropriate copies out of appropriate physical registers.
1232SDValue
1233ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001234 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235 const SmallVectorImpl<ISD::InputArg> &Ins,
1236 DebugLoc dl, SelectionDAG &DAG,
Stephen Lin456ca042013-04-20 05:14:40 +00001237 SmallVectorImpl<SDValue> &InVals,
1238 bool isThisReturn, SDValue ThisVal) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001239
Bob Wilson1f595bb2009-04-17 19:07:39 +00001240 // Assign locations to each value returned by this call.
1241 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001242 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1243 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001244 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001245 CCAssignFnForNode(CallConv, /* Return*/ true,
1246 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001247
1248 // Copy all of the result registers out of their specified physreg.
1249 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1250 CCValAssign VA = RVLocs[i];
1251
Stephen Lin456ca042013-04-20 05:14:40 +00001252 // Pass 'this' value directly from the argument to return value, to avoid
1253 // reg unit interference
1254 if (i == 0 && isThisReturn) {
Stephen Lin81fef022013-04-23 19:42:25 +00001255 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1256 "unexpected return calling convention register assignment");
Stephen Lin456ca042013-04-20 05:14:40 +00001257 InVals.push_back(ThisVal);
1258 continue;
1259 }
1260
Bob Wilson80915242009-04-25 00:33:20 +00001261 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001262 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001263 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001264 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001266 Chain = Lo.getValue(1);
1267 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001270 InFlag);
1271 Chain = Hi.getValue(1);
1272 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001273 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001274
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 if (VA.getLocVT() == MVT::v2f64) {
1276 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1277 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1278 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001279
1280 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001282 Chain = Lo.getValue(1);
1283 InFlag = Lo.getValue(2);
1284 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001285 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001286 Chain = Hi.getValue(1);
1287 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001288 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1290 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001291 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001292 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001293 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1294 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001295 Chain = Val.getValue(1);
1296 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001297 }
Bob Wilson80915242009-04-25 00:33:20 +00001298
1299 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001300 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001301 case CCValAssign::Full: break;
1302 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001303 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001304 break;
1305 }
1306
Dan Gohman98ca4f22009-08-05 01:29:28 +00001307 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001308 }
1309
Dan Gohman98ca4f22009-08-05 01:29:28 +00001310 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001311}
1312
Bob Wilsondee46d72009-04-17 20:35:10 +00001313/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001314SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1316 SDValue StackPtr, SDValue Arg,
1317 DebugLoc dl, SelectionDAG &DAG,
1318 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001319 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001320 unsigned LocMemOffset = VA.getLocMemOffset();
1321 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1322 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001323 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001324 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001325 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001326}
1327
Dan Gohman98ca4f22009-08-05 01:29:28 +00001328void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001329 SDValue Chain, SDValue &Arg,
1330 RegsToPassVector &RegsToPass,
1331 CCValAssign &VA, CCValAssign &NextVA,
1332 SDValue &StackPtr,
1333 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001334 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001335
Jim Grosbache5165492009-11-09 00:11:35 +00001336 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001338 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1339
1340 if (NextVA.isRegLoc())
1341 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1342 else {
1343 assert(NextVA.isMemLoc());
1344 if (StackPtr.getNode() == 0)
1345 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1346
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1348 dl, DAG, NextVA,
1349 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001350 }
1351}
1352
Dan Gohman98ca4f22009-08-05 01:29:28 +00001353/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001354/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1355/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001357ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001358 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001359 SelectionDAG &DAG = CLI.DAG;
1360 DebugLoc &dl = CLI.DL;
1361 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1362 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1363 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1364 SDValue Chain = CLI.Chain;
1365 SDValue Callee = CLI.Callee;
1366 bool &isTailCall = CLI.IsTailCall;
1367 CallingConv::ID CallConv = CLI.CallConv;
1368 bool doesNotRet = CLI.DoesNotReturn;
1369 bool isVarArg = CLI.IsVarArg;
1370
Dale Johannesen51e28e62010-06-03 21:09:53 +00001371 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001372 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1373 bool isThisReturn = false;
1374 bool isSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001375 // Disable tail calls if they're not supported.
1376 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001377 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001378 if (isTailCall) {
1379 // Check if it's really possible to do a tail call.
1380 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001381 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001382 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001383 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1384 // detected sibcalls.
1385 if (isTailCall) {
1386 ++NumTailCalls;
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001387 isSibCall = true;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001388 }
1389 }
Evan Chenga8e29892007-01-19 07:51:42 +00001390
Bob Wilson1f595bb2009-04-17 19:07:39 +00001391 // Analyze operands of the call, assigning locations to each operand.
1392 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001393 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1394 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001396 CCAssignFnForNode(CallConv, /* Return*/ false,
1397 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001398
Bob Wilson1f595bb2009-04-17 19:07:39 +00001399 // Get a count of how many bytes are to be pushed on the stack.
1400 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001401
Dale Johannesen51e28e62010-06-03 21:09:53 +00001402 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001403 if (isSibCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001404 NumBytes = 0;
1405
Evan Chenga8e29892007-01-19 07:51:42 +00001406 // Adjust the stack pointer for the new arguments...
1407 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001408 if (!isSibCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001409 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001410
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001411 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001412
Bob Wilson5bafff32009-06-22 23:27:02 +00001413 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001414 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001415
Bob Wilson1f595bb2009-04-17 19:07:39 +00001416 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001417 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001418 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1419 i != e;
1420 ++i, ++realArgIdx) {
1421 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001422 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001423 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001424 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001425
Bob Wilson1f595bb2009-04-17 19:07:39 +00001426 // Promote the value if needed.
1427 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001428 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001429 case CCValAssign::Full: break;
1430 case CCValAssign::SExt:
1431 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1432 break;
1433 case CCValAssign::ZExt:
1434 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1435 break;
1436 case CCValAssign::AExt:
1437 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1438 break;
1439 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001440 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001441 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001442 }
1443
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001444 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001445 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001446 if (VA.getLocVT() == MVT::v2f64) {
1447 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1448 DAG.getConstant(0, MVT::i32));
1449 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1450 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001451
Dan Gohman98ca4f22009-08-05 01:29:28 +00001452 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001453 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1454
1455 VA = ArgLocs[++i]; // skip ahead to next loc
1456 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001457 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001458 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1459 } else {
1460 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001461
Dan Gohman98ca4f22009-08-05 01:29:28 +00001462 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1463 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001464 }
1465 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001467 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001468 }
1469 } else if (VA.isRegLoc()) {
Stephen Lin81fef022013-04-23 19:42:25 +00001470 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1471 assert(VA.getLocVT() == MVT::i32 &&
1472 "unexpected calling convention register assignment");
1473 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Lin456ca042013-04-20 05:14:40 +00001474 "unexpected use of 'returned'");
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001475 isThisReturn = true;
Stephen Lin456ca042013-04-20 05:14:40 +00001476 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001477 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001478 } else if (isByVal) {
1479 assert(VA.isMemLoc());
1480 unsigned offset = 0;
1481
1482 // True if this byval aggregate will be split between registers
1483 // and memory.
1484 if (CCInfo.isFirstByValRegValid()) {
1485 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1486 unsigned int i, j;
1487 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1488 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1489 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1490 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1491 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001492 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001493 MemOpChains.push_back(Load.getValue(1));
1494 RegsToPass.push_back(std::make_pair(j, Load));
1495 }
1496 offset = ARM::R4 - CCInfo.getFirstByValReg();
1497 CCInfo.clearFirstByValReg();
1498 }
1499
Manman Ren763a75d2012-06-01 02:44:42 +00001500 if (Flags.getByValSize() - 4*offset > 0) {
1501 unsigned LocMemOffset = VA.getLocMemOffset();
1502 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1503 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1504 StkPtrOff);
1505 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1506 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1507 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1508 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001509 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001510
Manman Ren763a75d2012-06-01 02:44:42 +00001511 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001512 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001513 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1514 Ops, array_lengthof(Ops)));
1515 }
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001516 } else if (!isSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001517 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001518
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1520 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001521 }
Evan Chenga8e29892007-01-19 07:51:42 +00001522 }
1523
1524 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001526 &MemOpChains[0], MemOpChains.size());
1527
1528 // Build a sequence of copy-to-reg nodes chained together with token chain
1529 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001530 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001531 // Tail call byval lowering might overwrite argument registers so in case of
1532 // tail call optimization the copies to registers are lowered later.
1533 if (!isTailCall)
1534 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1535 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1536 RegsToPass[i].second, InFlag);
1537 InFlag = Chain.getValue(1);
1538 }
Evan Chenga8e29892007-01-19 07:51:42 +00001539
Dale Johannesen51e28e62010-06-03 21:09:53 +00001540 // For tail calls lower the arguments to the 'real' stack slot.
1541 if (isTailCall) {
1542 // Force all the incoming stack arguments to be loaded from the stack
1543 // before any new outgoing arguments are stored to the stack, because the
1544 // outgoing stack slots may alias the incoming argument stack slots, and
1545 // the alias isn't otherwise explicit. This is slightly more conservative
1546 // than necessary, because it means that each store effectively depends
1547 // on every argument instead of just those arguments it would clobber.
1548
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001549 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001550 InFlag = SDValue();
1551 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1552 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1553 RegsToPass[i].second, InFlag);
1554 InFlag = Chain.getValue(1);
1555 }
Stephen Lin69394f22013-04-20 00:47:48 +00001556 InFlag = SDValue();
Dale Johannesen51e28e62010-06-03 21:09:53 +00001557 }
1558
Bill Wendling056292f2008-09-16 21:48:12 +00001559 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1560 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1561 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001562 bool isDirect = false;
1563 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001564 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001565 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001566
1567 if (EnableARMLongCalls) {
1568 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1569 && "long-calls with non-static relocation model!");
1570 // Handle a global address or an external symbol. If it's not one of
1571 // those, the target's already in a register, so we don't need to do
1572 // anything extra.
1573 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001574 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001575 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001576 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001577 ARMConstantPoolValue *CPV =
1578 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1579
Jim Grosbache7b52522010-04-14 22:28:31 +00001580 // Get the address of the callee into a register
1581 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1582 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1583 Callee = DAG.getLoad(getPointerTy(), dl,
1584 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001585 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001586 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001587 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1588 const char *Sym = S->getSymbol();
1589
1590 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001591 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001592 ARMConstantPoolValue *CPV =
1593 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1594 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001595 // Get the address of the callee into a register
1596 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1597 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1598 Callee = DAG.getLoad(getPointerTy(), dl,
1599 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001600 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001601 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001602 }
1603 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001604 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001605 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001606 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001607 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001608 getTargetMachine().getRelocationModel() != Reloc::Static;
1609 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001610 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001611 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001612 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001613 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001614 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001615 ARMConstantPoolValue *CPV =
1616 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001617 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001619 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001620 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001621 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001622 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001623 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001624 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001625 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001626 } else {
1627 // On ELF targets for PIC code, direct calls should go through the PLT
1628 unsigned OpFlags = 0;
1629 if (Subtarget->isTargetELF() &&
Chad Rosiera6ca7032013-02-28 19:16:42 +00001630 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach637d89f2010-09-22 23:27:36 +00001631 OpFlags = ARMII::MO_PLT;
1632 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1633 }
Bill Wendling056292f2008-09-16 21:48:12 +00001634 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001635 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001636 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001637 getTargetMachine().getRelocationModel() != Reloc::Static;
1638 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001639 // tBX takes a register source operand.
1640 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001641 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001642 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001643 ARMConstantPoolValue *CPV =
1644 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1645 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001646 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001648 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001649 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001650 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001651 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001652 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001653 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001654 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001655 } else {
1656 unsigned OpFlags = 0;
1657 // On ELF targets for PIC code, direct calls should go through the PLT
1658 if (Subtarget->isTargetELF() &&
1659 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1660 OpFlags = ARMII::MO_PLT;
1661 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1662 }
Evan Chenga8e29892007-01-19 07:51:42 +00001663 }
1664
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001665 // FIXME: handle tail calls differently.
1666 unsigned CallOpc;
Bill Wendling831737d2012-12-30 10:32:01 +00001667 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1668 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001669 if (Subtarget->isThumb()) {
1670 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001671 CallOpc = ARMISD::CALL_NOLINK;
1672 else
1673 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1674 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001675 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001676 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001677 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001678 // Emit regular call when code size is the priority
1679 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001680 // "mov lr, pc; b _foo" to avoid confusing the RSP
1681 CallOpc = ARMISD::CALL_NOLINK;
1682 else
1683 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001684 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001685
Dan Gohman475871a2008-07-27 21:46:04 +00001686 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001687 Ops.push_back(Chain);
1688 Ops.push_back(Callee);
1689
1690 // Add argument registers to the end of the list so that they are known live
1691 // into the call.
1692 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1693 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1694 RegsToPass[i].second.getValueType()));
1695
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001696 // Add a register mask operand representing the call-preserved registers.
Stephen Lin456ca042013-04-20 05:14:40 +00001697 const uint32_t *Mask;
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001698 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
Stephen Lin456ca042013-04-20 05:14:40 +00001699 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001700 if (isThisReturn)
Stephen Lin456ca042013-04-20 05:14:40 +00001701 // For 'this' returns, use the R0-preserving mask
1702 Mask = ARI->getThisReturnPreservedMask(CallConv);
1703 else
1704 Mask = ARI->getCallPreservedMask(CallConv);
1705
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001706 assert(Mask && "Missing call preserved mask for calling convention");
1707 Ops.push_back(DAG.getRegisterMask(Mask));
1708
Gabor Greifba36cb52008-08-28 21:40:38 +00001709 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001710 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001711
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001712 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001713 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001714 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001715
Duncan Sands4bdcb612008-07-02 17:40:58 +00001716 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001717 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001718 InFlag = Chain.getValue(1);
1719
Chris Lattnere563bbc2008-10-11 22:08:30 +00001720 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1721 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001723 InFlag = Chain.getValue(1);
1724
Bob Wilson1f595bb2009-04-17 19:07:39 +00001725 // Handle result values, copying them out of physregs into vregs that we
1726 // return.
Stephen Lin456ca042013-04-20 05:14:40 +00001727 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin3a1b4f82013-04-23 19:30:12 +00001728 InVals, isThisReturn,
1729 isThisReturn ? OutVals[0] : SDValue());
Evan Chenga8e29892007-01-19 07:51:42 +00001730}
1731
Stuart Hastingsf222e592011-02-28 17:17:53 +00001732/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001733/// on the stack. Remember the next parameter register to allocate,
1734/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001735/// this.
1736void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001737ARMTargetLowering::HandleByVal(
1738 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001739 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1740 assert((State->getCallOrPrologue() == Prologue ||
1741 State->getCallOrPrologue() == Call) &&
1742 "unhandled ParmContext");
1743 if ((!State->isFirstByValRegValid()) &&
Stepan Dyatkovskiy78e3c902013-04-22 13:06:52 +00001744 (!Subtarget->isAAPCS_ABI() || State->getNextStackOffset() == 0) &&
Stuart Hastingsc7315872011-04-20 16:47:52 +00001745 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001746 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1747 unsigned AlignInRegs = Align / 4;
1748 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1749 for (unsigned i = 0; i < Waste; ++i)
1750 reg = State->AllocateReg(GPRArgRegs, 4);
1751 }
1752 if (reg != 0) {
1753 State->setFirstByValReg(reg);
1754 // At a call site, a byval parameter that is split between
1755 // registers and memory needs its size truncated here. In a
1756 // function prologue, such byval parameters are reassembled in
1757 // memory, and are not truncated.
1758 if (State->getCallOrPrologue() == Call) {
1759 unsigned excess = 4 * (ARM::R4 - reg);
1760 assert(size >= excess && "expected larger existing stack allocation");
1761 size -= excess;
1762 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001763 }
1764 }
1765 // Confiscate any remaining parameter registers to preclude their
1766 // assignment to subsequent parameters.
1767 while (State->AllocateReg(GPRArgRegs, 4))
1768 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001769}
1770
Dale Johannesen51e28e62010-06-03 21:09:53 +00001771/// MatchingStackOffset - Return true if the given stack call argument is
1772/// already available in the same position (relatively) of the caller's
1773/// incoming argument stack.
1774static
1775bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1776 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001777 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001778 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1779 int FI = INT_MAX;
1780 if (Arg.getOpcode() == ISD::CopyFromReg) {
1781 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001782 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001783 return false;
1784 MachineInstr *Def = MRI->getVRegDef(VR);
1785 if (!Def)
1786 return false;
1787 if (!Flags.isByVal()) {
1788 if (!TII->isLoadFromStackSlot(Def, FI))
1789 return false;
1790 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001791 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001792 }
1793 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1794 if (Flags.isByVal())
1795 // ByVal argument is passed in as a pointer but it's now being
1796 // dereferenced. e.g.
1797 // define @foo(%struct.X* %A) {
1798 // tail call @bar(%struct.X* byval %A)
1799 // }
1800 return false;
1801 SDValue Ptr = Ld->getBasePtr();
1802 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1803 if (!FINode)
1804 return false;
1805 FI = FINode->getIndex();
1806 } else
1807 return false;
1808
1809 assert(FI != INT_MAX);
1810 if (!MFI->isFixedObjectIndex(FI))
1811 return false;
1812 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1813}
1814
1815/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1816/// for tail call optimization. Targets which want to do tail call
1817/// optimization should implement this function.
1818bool
1819ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1820 CallingConv::ID CalleeCC,
1821 bool isVarArg,
1822 bool isCalleeStructRet,
1823 bool isCallerStructRet,
1824 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001825 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001826 const SmallVectorImpl<ISD::InputArg> &Ins,
1827 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001828 const Function *CallerF = DAG.getMachineFunction().getFunction();
1829 CallingConv::ID CallerCC = CallerF->getCallingConv();
1830 bool CCMatch = CallerCC == CalleeCC;
1831
1832 // Look for obvious safe cases to perform tail call optimization that do not
1833 // require ABI changes. This is what gcc calls sibcall.
1834
Jim Grosbach7616b642010-06-16 23:45:49 +00001835 // Do not sibcall optimize vararg calls unless the call site is not passing
1836 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001837 if (isVarArg && !Outs.empty())
1838 return false;
1839
1840 // Also avoid sibcall optimization if either caller or callee uses struct
1841 // return semantics.
1842 if (isCalleeStructRet || isCallerStructRet)
1843 return false;
1844
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001845 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001846 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1847 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1848 // support in the assembler and linker to be used. This would need to be
1849 // fixed to fully support tail calls in Thumb1.
1850 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001851 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1852 // LR. This means if we need to reload LR, it takes an extra instructions,
1853 // which outweighs the value of the tail call; but here we don't know yet
1854 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001855 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001856 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001857
1858 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1859 // but we need to make sure there are enough registers; the only valid
1860 // registers are the 4 used for parameters. We don't currently do this
1861 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001862 if (Subtarget->isThumb1Only())
1863 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001864
Dale Johannesen51e28e62010-06-03 21:09:53 +00001865 // If the calling conventions do not match, then we'd better make sure the
1866 // results are returned in the same way as what the caller expects.
1867 if (!CCMatch) {
1868 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001869 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1870 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001871 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1872
1873 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001874 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1875 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001876 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1877
1878 if (RVLocs1.size() != RVLocs2.size())
1879 return false;
1880 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1881 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1882 return false;
1883 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1884 return false;
1885 if (RVLocs1[i].isRegLoc()) {
1886 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1887 return false;
1888 } else {
1889 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1890 return false;
1891 }
1892 }
1893 }
1894
Manman Rene6c3cc82012-10-12 23:39:43 +00001895 // If Caller's vararg or byval argument has been split between registers and
1896 // stack, do not perform tail call, since part of the argument is in caller's
1897 // local frame.
1898 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1899 getInfo<ARMFunctionInfo>();
1900 if (AFI_Caller->getVarArgsRegSaveSize())
1901 return false;
1902
Dale Johannesen51e28e62010-06-03 21:09:53 +00001903 // If the callee takes no arguments then go on to check the results of the
1904 // call.
1905 if (!Outs.empty()) {
1906 // Check if stack adjustment is needed. For now, do not do this if any
1907 // argument is passed on the stack.
1908 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001909 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1910 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001911 CCInfo.AnalyzeCallOperands(Outs,
1912 CCAssignFnForNode(CalleeCC, false, isVarArg));
1913 if (CCInfo.getNextStackOffset()) {
1914 MachineFunction &MF = DAG.getMachineFunction();
1915
1916 // Check if the arguments are already laid out in the right way as
1917 // the caller's fixed stack objects.
1918 MachineFrameInfo *MFI = MF.getFrameInfo();
1919 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001920 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001921 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1922 i != e;
1923 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001924 CCValAssign &VA = ArgLocs[i];
1925 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001926 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001927 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001928 if (VA.getLocInfo() == CCValAssign::Indirect)
1929 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001930 if (VA.needsCustom()) {
1931 // f64 and vector types are split into multiple registers or
1932 // register/stack-slot combinations. The types will not match
1933 // the registers; give up on memory f64 refs until we figure
1934 // out what to do about this.
1935 if (!VA.isRegLoc())
1936 return false;
1937 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001938 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001939 if (RegVT == MVT::v2f64) {
1940 if (!ArgLocs[++i].isRegLoc())
1941 return false;
1942 if (!ArgLocs[++i].isRegLoc())
1943 return false;
1944 }
1945 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001946 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1947 MFI, MRI, TII))
1948 return false;
1949 }
1950 }
1951 }
1952 }
1953
1954 return true;
1955}
1956
Benjamin Kramer350c0082012-11-28 20:55:10 +00001957bool
1958ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1959 MachineFunction &MF, bool isVarArg,
1960 const SmallVectorImpl<ISD::OutputArg> &Outs,
1961 LLVMContext &Context) const {
1962 SmallVector<CCValAssign, 16> RVLocs;
1963 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1964 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1965 isVarArg));
1966}
1967
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968SDValue
1969ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001970 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001972 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001973 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001974
Bob Wilsondee46d72009-04-17 20:35:10 +00001975 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001976 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001977
Bob Wilsondee46d72009-04-17 20:35:10 +00001978 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001979 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1980 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001981
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001983 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1984 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001985
Bob Wilson1f595bb2009-04-17 19:07:39 +00001986 SDValue Flag;
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001987 SmallVector<SDValue, 4> RetOps;
1988 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Bob Wilson1f595bb2009-04-17 19:07:39 +00001989
1990 // Copy the result values into the output registers.
1991 for (unsigned i = 0, realRVLocIdx = 0;
1992 i != RVLocs.size();
1993 ++i, ++realRVLocIdx) {
1994 CCValAssign &VA = RVLocs[i];
1995 assert(VA.isRegLoc() && "Can only return in registers!");
1996
Dan Gohmanc9403652010-07-07 15:54:55 +00001997 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001998
1999 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002000 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002001 case CCValAssign::Full: break;
2002 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002003 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002004 break;
2005 }
2006
Bob Wilson1f595bb2009-04-17 19:07:39 +00002007 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002009 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00002010 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2011 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00002012 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00002014
2015 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
2016 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002017 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00002018 VA = RVLocs[++i]; // skip ahead to next loc
2019 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2020 HalfGPRs.getValue(1), Flag);
2021 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002022 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00002023 VA = RVLocs[++i]; // skip ahead to next loc
2024
2025 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2027 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00002028 }
2029 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2030 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00002031 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002033 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00002034 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002035 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002036 VA = RVLocs[++i]; // skip ahead to next loc
2037 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
2038 Flag);
2039 } else
2040 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2041
Bob Wilsondee46d72009-04-17 20:35:10 +00002042 // Guarantee that all emitted copies are
2043 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002044 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002045 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002046 }
2047
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002048 // Update chain and glue.
2049 RetOps[0] = Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002050 if (Flag.getNode())
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002051 RetOps.push_back(Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002052
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002053 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2054 RetOps.data(), RetOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002055}
2056
Evan Chengbf010eb2012-04-10 01:51:00 +00002057bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002058 if (N->getNumValues() != 1)
2059 return false;
2060 if (!N->hasNUsesOfValue(1, 0))
2061 return false;
2062
Evan Chengbf010eb2012-04-10 01:51:00 +00002063 SDValue TCChain = Chain;
2064 SDNode *Copy = *N->use_begin();
2065 if (Copy->getOpcode() == ISD::CopyToReg) {
2066 // If the copy has a glue operand, we conservatively assume it isn't safe to
2067 // perform a tail call.
2068 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2069 return false;
2070 TCChain = Copy->getOperand(0);
2071 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2072 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002073 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002074 SmallPtrSet<SDNode*, 2> Copies;
2075 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002076 UI != UE; ++UI) {
2077 if (UI->getOpcode() != ISD::CopyToReg)
2078 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002079 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002080 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002081 if (Copies.size() > 2)
2082 return false;
2083
2084 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2085 UI != UE; ++UI) {
2086 SDValue UseChain = UI->getOperand(0);
2087 if (Copies.count(UseChain.getNode()))
2088 // Second CopyToReg
2089 Copy = *UI;
2090 else
2091 // First CopyToReg
2092 TCChain = UseChain;
2093 }
2094 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002095 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002096 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002097 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002098 Copy = *Copy->use_begin();
2099 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002100 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002101 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002102 } else {
2103 return false;
2104 }
2105
Evan Cheng1bf891a2010-12-01 22:59:46 +00002106 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002107 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2108 UI != UE; ++UI) {
2109 if (UI->getOpcode() != ARMISD::RET_FLAG)
2110 return false;
2111 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002112 }
2113
Evan Chengbf010eb2012-04-10 01:51:00 +00002114 if (!HasRet)
2115 return false;
2116
2117 Chain = TCChain;
2118 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002119}
2120
Evan Cheng485fafc2011-03-21 01:19:09 +00002121bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002122 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002123 return false;
2124
2125 if (!CI->isTailCall())
2126 return false;
2127
2128 return !Subtarget->isThumb1Only();
2129}
2130
Bob Wilsonb62d2572009-11-03 00:02:05 +00002131// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2132// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2133// one of the above mentioned nodes. It has to be wrapped because otherwise
2134// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2135// be used to form addressing mode. These wrapped nodes will be selected
2136// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002137static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002138 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002139 // FIXME there is no actual debug info here
2140 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002141 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002143 if (CP->isMachineConstantPoolEntry())
2144 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2145 CP->getAlignment());
2146 else
2147 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2148 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002150}
2151
Jim Grosbache1102ca2010-07-19 17:20:38 +00002152unsigned ARMTargetLowering::getJumpTableEncoding() const {
2153 return MachineJumpTableInfo::EK_Inline;
2154}
2155
Dan Gohmand858e902010-04-17 15:26:15 +00002156SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2157 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002158 MachineFunction &MF = DAG.getMachineFunction();
2159 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2160 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002161 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002162 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002163 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002164 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2165 SDValue CPAddr;
2166 if (RelocM == Reloc::Static) {
2167 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2168 } else {
2169 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002170 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002171 ARMConstantPoolValue *CPV =
2172 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2173 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002174 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2175 }
2176 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2177 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002178 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002179 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002180 if (RelocM == Reloc::Static)
2181 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002182 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002183 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002184}
2185
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002186// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002187SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002188ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002189 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002190 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002191 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002192 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002193 MachineFunction &MF = DAG.getMachineFunction();
2194 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002195 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002196 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002197 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2198 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002199 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002200 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002201 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002202 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002203 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002204 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002205
Evan Chenge7e0d622009-11-06 22:24:13 +00002206 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002207 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002208
2209 // call __tls_get_addr.
2210 ArgListTy Args;
2211 ArgListEntry Entry;
2212 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002213 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002214 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002215 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002216 TargetLowering::CallLoweringInfo CLI(Chain,
2217 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002218 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002219 0, CallingConv::C, /*isTailCall=*/false,
2220 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002221 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002222 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002223 return CallResult.first;
2224}
2225
2226// Lower ISD::GlobalTLSAddress using the "initial exec" or
2227// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002228SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002229ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002230 SelectionDAG &DAG,
2231 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002232 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002233 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002234 SDValue Offset;
2235 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002236 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002237 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002238 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002239
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002240 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002241 MachineFunction &MF = DAG.getMachineFunction();
2242 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002243 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002244 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002245 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2246 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002247 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2248 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2249 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002250 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002251 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002252 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002253 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002254 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002255 Chain = Offset.getValue(1);
2256
Evan Chenge7e0d622009-11-06 22:24:13 +00002257 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002258 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002259
Evan Cheng9eda6892009-10-31 03:39:36 +00002260 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002261 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002262 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002263 } else {
2264 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002265 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002266 ARMConstantPoolValue *CPV =
2267 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002268 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002269 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002270 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002271 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002272 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002273 }
2274
2275 // The address of the thread local variable is the add of the thread
2276 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002277 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002278}
2279
Dan Gohman475871a2008-07-27 21:46:04 +00002280SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002281ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002282 // TODO: implement the "local dynamic" model
2283 assert(Subtarget->isTargetELF() &&
2284 "TLS not implemented for non-ELF targets");
2285 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002286
2287 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2288
2289 switch (model) {
2290 case TLSModel::GeneralDynamic:
2291 case TLSModel::LocalDynamic:
2292 return LowerToTLSGeneralDynamicModel(GA, DAG);
2293 case TLSModel::InitialExec:
2294 case TLSModel::LocalExec:
2295 return LowerToTLSExecModels(GA, DAG, model);
2296 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002297 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002298}
2299
Dan Gohman475871a2008-07-27 21:46:04 +00002300SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002301 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002302 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002303 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002304 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosiera6ca7032013-02-28 19:16:42 +00002305 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002306 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002307 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002308 ARMConstantPoolConstant::Create(GV,
2309 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002310 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002311 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002312 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002313 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002314 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002315 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002316 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002317 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002318 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002319 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002320 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002321 MachinePointerInfo::getGOT(),
2322 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002323 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002324 }
2325
2326 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002327 // pair. This is always cheaper.
2328 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002329 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002330 // FIXME: Once remat is capable of dealing with instructions with register
2331 // operands, expand this into two nodes.
2332 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2333 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002334 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002335 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2336 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2337 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2338 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002339 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002340 }
2341}
2342
Dan Gohman475871a2008-07-27 21:46:04 +00002343SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002344 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002345 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002346 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002347 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002348 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002349
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002350 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2351 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002352 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002353 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002354 // FIXME: Once remat is capable of dealing with instructions with register
2355 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002356 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002357 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2358 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2359
Evan Cheng53519f02011-01-21 18:55:51 +00002360 unsigned Wrapper = (RelocM == Reloc::PIC_)
2361 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2362 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002363 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002364 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2365 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002366 MachinePointerInfo::getGOT(),
2367 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002368 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002369 }
2370
2371 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002373 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002374 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002375 } else {
Chad Rosiera6ca7032013-02-28 19:16:42 +00002376 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002377 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002378 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2379 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002380 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2381 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002382 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002383 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002384 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002385
Evan Cheng9eda6892009-10-31 03:39:36 +00002386 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002387 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002388 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002389 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002390
2391 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002392 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002393 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002394 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002395
Evan Cheng63476a82009-09-03 07:04:02 +00002396 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002397 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002398 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002399
2400 return Result;
2401}
2402
Dan Gohman475871a2008-07-27 21:46:04 +00002403SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002404 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002405 assert(Subtarget->isTargetELF() &&
2406 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002407 MachineFunction &MF = DAG.getMachineFunction();
2408 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002409 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002410 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002411 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002412 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002413 ARMConstantPoolValue *CPV =
2414 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2415 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002416 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002417 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002418 SDValue Result = 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);
Evan Chenge7e0d622009-11-06 22:24:13 +00002421 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002422 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002423}
2424
Jim Grosbach0e0da732009-05-12 23:59:14 +00002425SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002426ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2427 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002428 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002429 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2430 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002431 Op.getOperand(1), Val);
2432}
2433
2434SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002435ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2436 DebugLoc dl = Op.getDebugLoc();
2437 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2438 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2439}
2440
2441SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002442ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002443 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002444 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002445 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002446 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002447 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002448 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002449 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002450 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2451 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002452 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002453 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002454 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002455 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002456 EVT PtrVT = getPointerTy();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002457 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2458 SDValue CPAddr;
2459 unsigned PCAdj = (RelocM != Reloc::PIC_)
2460 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002461 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002462 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2463 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002464 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002466 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002467 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002468 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002469 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002470
2471 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002472 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002473 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2474 }
2475 return Result;
2476 }
Evan Cheng92e39162011-03-29 23:06:19 +00002477 case Intrinsic::arm_neon_vmulls:
2478 case Intrinsic::arm_neon_vmullu: {
2479 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2480 ? ARMISD::VMULLs : ARMISD::VMULLu;
2481 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2482 Op.getOperand(1), Op.getOperand(2));
2483 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002484 }
2485}
2486
Eli Friedman26689ac2011-08-03 21:06:02 +00002487static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2488 const ARMSubtarget *Subtarget) {
2489 // FIXME: handle "fence singlethread" more efficiently.
2490 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002491 if (!Subtarget->hasDataBarrier()) {
2492 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2493 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2494 // here.
2495 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2496 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002497 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002498 DAG.getConstant(0, MVT::i32));
2499 }
2500
Eli Friedman26689ac2011-08-03 21:06:02 +00002501 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002502 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002503}
2504
Evan Chengdfed19f2010-11-03 06:34:55 +00002505static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2506 const ARMSubtarget *Subtarget) {
2507 // ARM pre v5TE and Thumb1 does not have preload instructions.
2508 if (!(Subtarget->isThumb2() ||
2509 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2510 // Just preserve the chain.
2511 return Op.getOperand(0);
2512
2513 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002514 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2515 if (!isRead &&
2516 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2517 // ARMv7 with MP extension has PLDW.
2518 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002519
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002520 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2521 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002522 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002523 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002524 isData = ~isData & 1;
2525 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002526
2527 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002528 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2529 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002530}
2531
Dan Gohman1e93df62010-04-17 14:41:14 +00002532static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2533 MachineFunction &MF = DAG.getMachineFunction();
2534 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2535
Evan Chenga8e29892007-01-19 07:51:42 +00002536 // vastart just stores the address of the VarArgsFrameIndex slot into the
2537 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002538 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002539 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002540 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002541 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002542 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2543 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002544}
2545
Dan Gohman475871a2008-07-27 21:46:04 +00002546SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002547ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2548 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002549 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002550 MachineFunction &MF = DAG.getMachineFunction();
2551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2552
Craig Topper44d23822012-02-22 05:59:10 +00002553 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002554 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002555 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002556 else
Craig Topper420761a2012-04-20 07:30:17 +00002557 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002558
2559 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002560 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002561 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002562
2563 SDValue ArgValue2;
2564 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002565 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002566 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002567
2568 // Create load node to retrieve arguments from the stack.
2569 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002570 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002571 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002572 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002573 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002574 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002575 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002576 }
2577
Jim Grosbache5165492009-11-09 00:11:35 +00002578 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002579}
2580
Stuart Hastingsc7315872011-04-20 16:47:52 +00002581void
2582ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2583 unsigned &VARegSize, unsigned &VARegSaveSize)
2584 const {
2585 unsigned NumGPRs;
2586 if (CCInfo.isFirstByValRegValid())
2587 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2588 else {
2589 unsigned int firstUnalloced;
2590 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2591 sizeof(GPRArgRegs) /
2592 sizeof(GPRArgRegs[0]));
2593 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2594 }
2595
2596 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2597 VARegSize = NumGPRs * 4;
2598 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2599}
2600
2601// The remaining GPRs hold either the beginning of variable-argument
David Peixottoe68542e2013-02-13 00:36:35 +00002602// data, or the beginning of an aggregate passed by value (usually
Stuart Hastingsc7315872011-04-20 16:47:52 +00002603// byval). Either way, we allocate stack slots adjacent to the data
2604// provided by our caller, and store the unallocated registers there.
2605// If this is a variadic function, the va_list pointer will begin with
2606// these values; otherwise, this reassembles a (byval) structure that
2607// was split between registers and memory.
2608void
2609ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2610 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002611 const Value *OrigArg,
2612 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002613 unsigned ArgOffset,
2614 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002615 MachineFunction &MF = DAG.getMachineFunction();
2616 MachineFrameInfo *MFI = MF.getFrameInfo();
2617 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2618 unsigned firstRegToSaveIndex;
2619 if (CCInfo.isFirstByValRegValid())
2620 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2621 else {
2622 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2623 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2624 }
2625
2626 unsigned VARegSize, VARegSaveSize;
2627 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2628 if (VARegSaveSize) {
2629 // If this function is vararg, store any remaining integer argument regs
2630 // to their spots on the stack so that they may be loaded by deferencing
2631 // the result of va_next.
2632 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002633 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2634 ArgOffset + VARegSaveSize
2635 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002636 false));
2637 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2638 getPointerTy());
2639
2640 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002641 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002642 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002643 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002644 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002645 else
Craig Topper420761a2012-04-20 07:30:17 +00002646 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002647
2648 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2649 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2650 SDValue Store =
2651 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002652 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002653 false, false, 0);
2654 MemOps.push_back(Store);
2655 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2656 DAG.getConstant(4, getPointerTy()));
2657 }
2658 if (!MemOps.empty())
2659 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2660 &MemOps[0], MemOps.size());
2661 } else
2662 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002663 AFI->setVarArgsFrameIndex(
2664 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002665}
2666
Bob Wilson5bafff32009-06-22 23:27:02 +00002667SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002668ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002669 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002670 const SmallVectorImpl<ISD::InputArg>
2671 &Ins,
2672 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002673 SmallVectorImpl<SDValue> &InVals)
2674 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002675 MachineFunction &MF = DAG.getMachineFunction();
2676 MachineFrameInfo *MFI = MF.getFrameInfo();
2677
Bob Wilson1f595bb2009-04-17 19:07:39 +00002678 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2679
2680 // Assign locations to all of the incoming arguments.
2681 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002682 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2683 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002684 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002685 CCAssignFnForNode(CallConv, /* Return*/ false,
2686 isVarArg));
Jim Grosbach7ccf4632013-03-02 20:16:15 +00002687
Bob Wilson1f595bb2009-04-17 19:07:39 +00002688 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002689 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002690 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002691 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2692 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002693 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2694 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002695 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2696 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002697 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002698 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002699 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002700
Bob Wilson1f595bb2009-04-17 19:07:39 +00002701 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002702 // f64 and vector types are split up into multiple registers or
2703 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002704 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002705 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002707 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002708 SDValue ArgValue2;
2709 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002710 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002711 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2712 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002713 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002714 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002715 } else {
2716 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2717 Chain, DAG, dl);
2718 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002719 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2720 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002721 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002722 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002723 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2724 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002726
Bob Wilson5bafff32009-06-22 23:27:02 +00002727 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002728 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002729
Owen Anderson825b72b2009-08-11 20:47:22 +00002730 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002731 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002732 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002733 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002734 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002735 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002736 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002737 RC = AFI->isThumb1OnlyFunction() ?
2738 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2739 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002740 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002741 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002742
2743 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002744 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002745 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002746 }
2747
2748 // If this is an 8 or 16-bit value, it is really passed promoted
2749 // to 32 bits. Insert an assert[sz]ext to capture this, then
2750 // truncate to the right size.
2751 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002752 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002753 case CCValAssign::Full: break;
2754 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002755 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002756 break;
2757 case CCValAssign::SExt:
2758 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2759 DAG.getValueType(VA.getValVT()));
2760 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2761 break;
2762 case CCValAssign::ZExt:
2763 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2764 DAG.getValueType(VA.getValVT()));
2765 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2766 break;
2767 }
2768
Dan Gohman98ca4f22009-08-05 01:29:28 +00002769 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002770
2771 } else { // VA.isRegLoc()
2772
2773 // sanity check
2774 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002775 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002776
Stuart Hastingsf222e592011-02-28 17:17:53 +00002777 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002778
Stuart Hastingsf222e592011-02-28 17:17:53 +00002779 // Some Ins[] entries become multiple ArgLoc[] entries.
2780 // Process them only once.
2781 if (index != lastInsIndex)
2782 {
2783 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002784 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002785 // This can be changed with more analysis.
2786 // In case of tail call optimization mark all arguments mutable.
2787 // Since they could be overwritten by lowering of arguments in case of
2788 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002789 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002790 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2791 if (!AFI->getVarArgsFrameIndex()) {
2792 VarArgStyleRegisters(CCInfo, DAG,
2793 dl, Chain, CurOrigArg,
2794 Ins[VA.getValNo()].PartOffset,
2795 VA.getLocMemOffset(),
2796 true /*force mutable frames*/);
2797 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2798 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2799 } else {
2800 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2801 VA.getLocMemOffset(), false);
Jim Grosbach7ccf4632013-03-02 20:16:15 +00002802 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002803 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002804 } else {
2805 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2806 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002807
Stuart Hastingsf222e592011-02-28 17:17:53 +00002808 // Create load nodes to retrieve arguments from the stack.
2809 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2810 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2811 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002812 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002813 }
2814 lastInsIndex = index;
2815 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002816 }
2817 }
2818
2819 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002820 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002821 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2822 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002823
Dan Gohman98ca4f22009-08-05 01:29:28 +00002824 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002825}
2826
2827/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002828static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002829 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002830 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002831 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002832 // Maybe this has already been legalized into the constant pool?
2833 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002834 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002835 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002836 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002837 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002838 }
2839 }
2840 return false;
2841}
2842
Evan Chenga8e29892007-01-19 07:51:42 +00002843/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2844/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002845SDValue
2846ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002847 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002848 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002849 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002850 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002851 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002852 // Constant does not fit, try adjusting it by one?
2853 switch (CC) {
2854 default: break;
2855 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002856 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002857 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002858 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002860 }
2861 break;
2862 case ISD::SETULT:
2863 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002864 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002865 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002866 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002867 }
2868 break;
2869 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002870 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002871 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002872 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002873 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002874 }
2875 break;
2876 case ISD::SETULE:
2877 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002878 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002879 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002880 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002881 }
2882 break;
2883 }
2884 }
2885 }
2886
2887 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002888 ARMISD::NodeType CompareType;
2889 switch (CondCode) {
2890 default:
2891 CompareType = ARMISD::CMP;
2892 break;
2893 case ARMCC::EQ:
2894 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002895 // Uses only Z Flag
2896 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002897 break;
2898 }
Evan Cheng218977b2010-07-13 19:27:42 +00002899 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002900 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002901}
2902
2903/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002904SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002905ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002906 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002907 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002908 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002909 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002910 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002911 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2912 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002913}
2914
Bob Wilson79f56c92011-03-08 01:17:20 +00002915/// duplicateCmp - Glue values can have only one use, so this function
2916/// duplicates a comparison node.
2917SDValue
2918ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2919 unsigned Opc = Cmp.getOpcode();
2920 DebugLoc DL = Cmp.getDebugLoc();
2921 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2922 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2923
2924 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2925 Cmp = Cmp.getOperand(0);
2926 Opc = Cmp.getOpcode();
2927 if (Opc == ARMISD::CMPFP)
2928 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2929 else {
2930 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2931 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2932 }
2933 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2934}
2935
Bill Wendlingde2b1512010-08-11 08:43:16 +00002936SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2937 SDValue Cond = Op.getOperand(0);
2938 SDValue SelectTrue = Op.getOperand(1);
2939 SDValue SelectFalse = Op.getOperand(2);
2940 DebugLoc dl = Op.getDebugLoc();
2941
2942 // Convert:
2943 //
2944 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2945 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2946 //
2947 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2948 const ConstantSDNode *CMOVTrue =
2949 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2950 const ConstantSDNode *CMOVFalse =
2951 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2952
2953 if (CMOVTrue && CMOVFalse) {
2954 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2955 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2956
2957 SDValue True;
2958 SDValue False;
2959 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2960 True = SelectTrue;
2961 False = SelectFalse;
2962 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2963 True = SelectFalse;
2964 False = SelectTrue;
2965 }
2966
2967 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002968 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002969 SDValue ARMcc = Cond.getOperand(2);
2970 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002971 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002972 assert(True.getValueType() == VT);
2973 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002974 }
2975 }
2976 }
2977
Dan Gohmandb953892012-02-24 00:09:36 +00002978 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2979 // undefined bits before doing a full-word comparison with zero.
2980 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2981 DAG.getConstant(1, Cond.getValueType()));
2982
Bill Wendlingde2b1512010-08-11 08:43:16 +00002983 return DAG.getSelectCC(dl, Cond,
2984 DAG.getConstant(0, Cond.getValueType()),
2985 SelectTrue, SelectFalse, ISD::SETNE);
2986}
2987
Dan Gohmand858e902010-04-17 15:26:15 +00002988SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002989 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue LHS = Op.getOperand(0);
2991 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002992 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002993 SDValue TrueVal = Op.getOperand(2);
2994 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002995 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002996
Owen Anderson825b72b2009-08-11 20:47:22 +00002997 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002998 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002999 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003000 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00003001 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003002 }
3003
3004 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003005 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00003006
Evan Cheng218977b2010-07-13 19:27:42 +00003007 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3008 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003009 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00003010 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00003011 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003012 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003013 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00003014 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00003015 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003016 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00003017 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00003018 }
3019 return Result;
3020}
3021
Evan Cheng218977b2010-07-13 19:27:42 +00003022/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3023/// to morph to an integer compare sequence.
3024static bool canChangeToInt(SDValue Op, bool &SeenZero,
3025 const ARMSubtarget *Subtarget) {
3026 SDNode *N = Op.getNode();
3027 if (!N->hasOneUse())
3028 // Otherwise it requires moving the value from fp to integer registers.
3029 return false;
3030 if (!N->getNumValues())
3031 return false;
3032 EVT VT = Op.getValueType();
3033 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3034 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3035 // vmrs are very slow, e.g. cortex-a8.
3036 return false;
3037
3038 if (isFloatingPointZero(Op)) {
3039 SeenZero = true;
3040 return true;
3041 }
3042 return ISD::isNormalLoad(N);
3043}
3044
3045static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3046 if (isFloatingPointZero(Op))
3047 return DAG.getConstant(0, MVT::i32);
3048
3049 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3050 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003051 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003052 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003053 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003054
3055 llvm_unreachable("Unknown VFP cmp argument!");
3056}
3057
3058static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3059 SDValue &RetVal1, SDValue &RetVal2) {
3060 if (isFloatingPointZero(Op)) {
3061 RetVal1 = DAG.getConstant(0, MVT::i32);
3062 RetVal2 = DAG.getConstant(0, MVT::i32);
3063 return;
3064 }
3065
3066 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3067 SDValue Ptr = Ld->getBasePtr();
3068 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3069 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003070 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003071 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003072 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003073
3074 EVT PtrType = Ptr.getValueType();
3075 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3076 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3077 PtrType, Ptr, DAG.getConstant(4, PtrType));
3078 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3079 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003080 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003081 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003082 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003083 return;
3084 }
3085
3086 llvm_unreachable("Unknown VFP cmp argument!");
3087}
3088
3089/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3090/// f32 and even f64 comparisons to integer ones.
3091SDValue
3092ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3093 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003094 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003095 SDValue LHS = Op.getOperand(2);
3096 SDValue RHS = Op.getOperand(3);
3097 SDValue Dest = Op.getOperand(4);
3098 DebugLoc dl = Op.getDebugLoc();
3099
Evan Chengfc501a32012-03-01 23:27:13 +00003100 bool LHSSeenZero = false;
3101 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3102 bool RHSSeenZero = false;
3103 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3104 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003105 // If unsafe fp math optimization is enabled and there are no other uses of
3106 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003107 // to an integer comparison.
3108 if (CC == ISD::SETOEQ)
3109 CC = ISD::SETEQ;
3110 else if (CC == ISD::SETUNE)
3111 CC = ISD::SETNE;
3112
Evan Chengfc501a32012-03-01 23:27:13 +00003113 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003114 SDValue ARMcc;
3115 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003116 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3117 bitcastf32Toi32(LHS, DAG), Mask);
3118 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3119 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003120 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3121 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3122 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3123 Chain, Dest, ARMcc, CCR, Cmp);
3124 }
3125
3126 SDValue LHS1, LHS2;
3127 SDValue RHS1, RHS2;
3128 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3129 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003130 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3131 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003132 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3133 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003134 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003135 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3136 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3137 }
3138
3139 return SDValue();
3140}
3141
3142SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3143 SDValue Chain = Op.getOperand(0);
3144 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3145 SDValue LHS = Op.getOperand(2);
3146 SDValue RHS = Op.getOperand(3);
3147 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003148 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003149
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003151 SDValue ARMcc;
3152 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003155 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003156 }
3157
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003159
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003160 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003161 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3162 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3163 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3164 if (Result.getNode())
3165 return Result;
3166 }
3167
Evan Chenga8e29892007-01-19 07:51:42 +00003168 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003169 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003170
Evan Cheng218977b2010-07-13 19:27:42 +00003171 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3172 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003173 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003174 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003175 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003176 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003177 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003178 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3179 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003180 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003181 }
3182 return Res;
3183}
3184
Dan Gohmand858e902010-04-17 15:26:15 +00003185SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003186 SDValue Chain = Op.getOperand(0);
3187 SDValue Table = Op.getOperand(1);
3188 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003189 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003190
Owen Andersone50ed302009-08-10 22:56:29 +00003191 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003192 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3193 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003194 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003195 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003197 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3198 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003199 if (Subtarget->isThumb2()) {
3200 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3201 // which does another jump to the destination. This also makes it easier
3202 // to translate it to TBB / TBH later.
3203 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003205 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003206 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003207 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003208 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003209 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003210 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003211 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003212 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003213 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003214 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003215 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003216 MachinePointerInfo::getJumpTable(),
3217 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003218 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003220 }
Evan Chenga8e29892007-01-19 07:51:42 +00003221}
3222
Eli Friedman14e809c2011-11-09 23:36:02 +00003223static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003224 EVT VT = Op.getValueType();
3225 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003226
James Molloy873fd5f2012-02-20 09:24:05 +00003227 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3228 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3229 return Op;
3230 return DAG.UnrollVectorOp(Op.getNode());
3231 }
3232
3233 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3234 "Invalid type for custom lowering!");
3235 if (VT != MVT::v4i16)
3236 return DAG.UnrollVectorOp(Op.getNode());
3237
3238 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3239 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003240}
3241
Bob Wilson76a312b2010-03-19 22:51:32 +00003242static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003243 EVT VT = Op.getValueType();
3244 if (VT.isVector())
3245 return LowerVectorFP_TO_INT(Op, DAG);
3246
Bob Wilson76a312b2010-03-19 22:51:32 +00003247 DebugLoc dl = Op.getDebugLoc();
3248 unsigned Opc;
3249
3250 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003251 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003252 case ISD::FP_TO_SINT:
3253 Opc = ARMISD::FTOSI;
3254 break;
3255 case ISD::FP_TO_UINT:
3256 Opc = ARMISD::FTOUI;
3257 break;
3258 }
3259 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003260 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003261}
3262
Cameron Zwarich3007d332011-03-29 21:41:55 +00003263static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3264 EVT VT = Op.getValueType();
3265 DebugLoc dl = Op.getDebugLoc();
3266
Eli Friedman14e809c2011-11-09 23:36:02 +00003267 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3268 if (VT.getVectorElementType() == MVT::f32)
3269 return Op;
3270 return DAG.UnrollVectorOp(Op.getNode());
3271 }
3272
Duncan Sands1f6a3292011-08-12 14:54:45 +00003273 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3274 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003275 if (VT != MVT::v4f32)
3276 return DAG.UnrollVectorOp(Op.getNode());
3277
3278 unsigned CastOpc;
3279 unsigned Opc;
3280 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003281 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003282 case ISD::SINT_TO_FP:
3283 CastOpc = ISD::SIGN_EXTEND;
3284 Opc = ISD::SINT_TO_FP;
3285 break;
3286 case ISD::UINT_TO_FP:
3287 CastOpc = ISD::ZERO_EXTEND;
3288 Opc = ISD::UINT_TO_FP;
3289 break;
3290 }
3291
3292 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3293 return DAG.getNode(Opc, dl, VT, Op);
3294}
3295
Bob Wilson76a312b2010-03-19 22:51:32 +00003296static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3297 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003298 if (VT.isVector())
3299 return LowerVectorINT_TO_FP(Op, DAG);
3300
Bob Wilson76a312b2010-03-19 22:51:32 +00003301 DebugLoc dl = Op.getDebugLoc();
3302 unsigned Opc;
3303
3304 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003305 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003306 case ISD::SINT_TO_FP:
3307 Opc = ARMISD::SITOF;
3308 break;
3309 case ISD::UINT_TO_FP:
3310 Opc = ARMISD::UITOF;
3311 break;
3312 }
3313
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003314 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003315 return DAG.getNode(Opc, dl, VT, Op);
3316}
3317
Evan Cheng515fe3a2010-07-08 02:08:50 +00003318SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003319 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003320 SDValue Tmp0 = Op.getOperand(0);
3321 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003322 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003323 EVT VT = Op.getValueType();
3324 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003325 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3326 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3327 bool UseNEON = !InGPR && Subtarget->hasNEON();
3328
3329 if (UseNEON) {
3330 // Use VBSL to copy the sign bit.
3331 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3332 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3333 DAG.getTargetConstant(EncodedVal, MVT::i32));
3334 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3335 if (VT == MVT::f64)
3336 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3337 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3338 DAG.getConstant(32, MVT::i32));
3339 else /*if (VT == MVT::f32)*/
3340 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3341 if (SrcVT == MVT::f32) {
3342 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3343 if (VT == MVT::f64)
3344 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3345 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3346 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003347 } else if (VT == MVT::f32)
3348 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3349 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3350 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003351 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3352 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3353
3354 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3355 MVT::i32);
3356 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3357 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3358 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003359
Evan Chenge573fb32011-02-23 02:24:55 +00003360 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3361 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3362 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003363 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003364 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3365 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3366 DAG.getConstant(0, MVT::i32));
3367 } else {
3368 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3369 }
3370
3371 return Res;
3372 }
Evan Chengc143dd42011-02-11 02:28:55 +00003373
3374 // Bitcast operand 1 to i32.
3375 if (SrcVT == MVT::f64)
3376 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3377 &Tmp1, 1).getValue(1);
3378 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3379
Evan Chenge573fb32011-02-23 02:24:55 +00003380 // Or in the signbit with integer operations.
3381 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3382 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3383 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3384 if (VT == MVT::f32) {
3385 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3386 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3387 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3388 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003389 }
3390
Evan Chenge573fb32011-02-23 02:24:55 +00003391 // f64: Or the high part with signbit and then combine two parts.
3392 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3393 &Tmp0, 1);
3394 SDValue Lo = Tmp0.getValue(0);
3395 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3396 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3397 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003398}
3399
Evan Cheng2457f2c2010-05-22 01:47:14 +00003400SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3401 MachineFunction &MF = DAG.getMachineFunction();
3402 MachineFrameInfo *MFI = MF.getFrameInfo();
3403 MFI->setReturnAddressIsTaken(true);
3404
3405 EVT VT = Op.getValueType();
3406 DebugLoc dl = Op.getDebugLoc();
3407 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3408 if (Depth) {
3409 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3410 SDValue Offset = DAG.getConstant(4, MVT::i32);
3411 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3412 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003413 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003414 }
3415
3416 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003417 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003418 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3419}
3420
Dan Gohmand858e902010-04-17 15:26:15 +00003421SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003422 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3423 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003424
Owen Andersone50ed302009-08-10 22:56:29 +00003425 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003426 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3427 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003428 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003429 ? ARM::R7 : ARM::R11;
3430 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3431 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003432 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3433 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003434 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003435 return FrameAddr;
3436}
3437
Renato Golin5ad5f592013-03-19 08:15:38 +00003438/// Custom Expand long vector extensions, where size(DestVec) > 2*size(SrcVec),
3439/// and size(DestVec) > 128-bits.
3440/// This is achieved by doing the one extension from the SrcVec, splitting the
3441/// result, extending these parts, and then concatenating these into the
3442/// destination.
3443static SDValue ExpandVectorExtension(SDNode *N, SelectionDAG &DAG) {
3444 SDValue Op = N->getOperand(0);
3445 EVT SrcVT = Op.getValueType();
3446 EVT DestVT = N->getValueType(0);
3447
3448 assert(DestVT.getSizeInBits() > 128 &&
3449 "Custom sext/zext expansion needs >128-bit vector.");
3450 // If this is a normal length extension, use the default expansion.
3451 if (SrcVT.getSizeInBits()*4 != DestVT.getSizeInBits() &&
3452 SrcVT.getSizeInBits()*8 != DestVT.getSizeInBits())
3453 return SDValue();
3454
3455 DebugLoc dl = N->getDebugLoc();
3456 unsigned SrcEltSize = SrcVT.getVectorElementType().getSizeInBits();
3457 unsigned DestEltSize = DestVT.getVectorElementType().getSizeInBits();
3458 unsigned NumElts = SrcVT.getVectorNumElements();
3459 LLVMContext &Ctx = *DAG.getContext();
3460 SDValue Mid, SplitLo, SplitHi, ExtLo, ExtHi;
3461
3462 EVT MidVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, SrcEltSize*2),
3463 NumElts);
3464 EVT SplitVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, SrcEltSize*2),
3465 NumElts/2);
3466 EVT ExtVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, DestEltSize),
3467 NumElts/2);
3468
3469 Mid = DAG.getNode(N->getOpcode(), dl, MidVT, Op);
3470 SplitLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SplitVT, Mid,
3471 DAG.getIntPtrConstant(0));
3472 SplitHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SplitVT, Mid,
3473 DAG.getIntPtrConstant(NumElts/2));
3474 ExtLo = DAG.getNode(N->getOpcode(), dl, ExtVT, SplitLo);
3475 ExtHi = DAG.getNode(N->getOpcode(), dl, ExtVT, SplitHi);
3476 return DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, ExtLo, ExtHi);
3477}
3478
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003479/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003480/// expand a bit convert where either the source or destination type is i64 to
3481/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3482/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3483/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003484static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003485 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3486 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003487 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003488
Bob Wilson9f3f0612010-04-17 05:30:19 +00003489 // This function is only supposed to be called for i64 types, either as the
3490 // source or destination of the bit convert.
3491 EVT SrcVT = Op.getValueType();
3492 EVT DstVT = N->getValueType(0);
3493 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003494 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003495
Bob Wilson9f3f0612010-04-17 05:30:19 +00003496 // Turn i64->f64 into VMOVDRR.
3497 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3499 DAG.getConstant(0, MVT::i32));
3500 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3501 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003502 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003503 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003504 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003505
Jim Grosbache5165492009-11-09 00:11:35 +00003506 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003507 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3508 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3509 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3510 // Merge the pieces into a single i64 value.
3511 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3512 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003513
Bob Wilson9f3f0612010-04-17 05:30:19 +00003514 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003515}
3516
Bob Wilson5bafff32009-06-22 23:27:02 +00003517/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003518/// Zero vectors are used to represent vector negation and in those cases
3519/// will be implemented with the NEON VNEG instruction. However, VNEG does
3520/// not support i64 elements, so sometimes the zero vectors will need to be
3521/// explicitly constructed. Regardless, use a canonical VMOV to create the
3522/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003523static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003524 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003525 // The canonical modified immediate encoding of a zero vector is....0!
3526 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3527 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3528 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003529 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003530}
3531
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003532/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3533/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003534SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3535 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003536 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3537 EVT VT = Op.getValueType();
3538 unsigned VTBits = VT.getSizeInBits();
3539 DebugLoc dl = Op.getDebugLoc();
3540 SDValue ShOpLo = Op.getOperand(0);
3541 SDValue ShOpHi = Op.getOperand(1);
3542 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003543 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003544 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003545
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003546 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3547
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003548 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3549 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3550 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3551 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3552 DAG.getConstant(VTBits, MVT::i32));
3553 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3554 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003555 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003556
3557 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3558 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003559 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003560 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003561 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003562 CCR, Cmp);
3563
3564 SDValue Ops[2] = { Lo, Hi };
3565 return DAG.getMergeValues(Ops, 2, dl);
3566}
3567
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003568/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3569/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003570SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3571 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003572 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3573 EVT VT = Op.getValueType();
3574 unsigned VTBits = VT.getSizeInBits();
3575 DebugLoc dl = Op.getDebugLoc();
3576 SDValue ShOpLo = Op.getOperand(0);
3577 SDValue ShOpHi = Op.getOperand(1);
3578 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003579 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003580
3581 assert(Op.getOpcode() == ISD::SHL_PARTS);
3582 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3583 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3584 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3585 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3586 DAG.getConstant(VTBits, MVT::i32));
3587 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3588 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3589
3590 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3591 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3592 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003593 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003594 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003595 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003596 CCR, Cmp);
3597
3598 SDValue Ops[2] = { Lo, Hi };
3599 return DAG.getMergeValues(Ops, 2, dl);
3600}
3601
Jim Grosbach4725ca72010-09-08 03:54:02 +00003602SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003603 SelectionDAG &DAG) const {
3604 // The rounding mode is in bits 23:22 of the FPSCR.
3605 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3606 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3607 // so that the shift + and get folded into a bitfield extract.
3608 DebugLoc dl = Op.getDebugLoc();
3609 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3610 DAG.getConstant(Intrinsic::arm_get_fpscr,
3611 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003612 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003613 DAG.getConstant(1U << 22, MVT::i32));
3614 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3615 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003616 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003617 DAG.getConstant(3, MVT::i32));
3618}
3619
Jim Grosbach3482c802010-01-18 19:58:49 +00003620static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3621 const ARMSubtarget *ST) {
3622 EVT VT = N->getValueType(0);
3623 DebugLoc dl = N->getDebugLoc();
3624
3625 if (!ST->hasV6T2Ops())
3626 return SDValue();
3627
3628 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3629 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3630}
3631
Evan Chengc8e70452012-12-04 22:41:50 +00003632/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3633/// for each 16-bit element from operand, repeated. The basic idea is to
3634/// leverage vcnt to get the 8-bit counts, gather and add the results.
3635///
3636/// Trace for v4i16:
3637/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3638/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3639/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
Jim Grosbach7ccf4632013-03-02 20:16:15 +00003640/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengc8e70452012-12-04 22:41:50 +00003641/// [b0 b1 b2 b3 b4 b5 b6 b7]
3642/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3643/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3644/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3645static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3646 EVT VT = N->getValueType(0);
3647 DebugLoc DL = N->getDebugLoc();
3648
3649 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3650 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3651 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3652 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3653 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3654 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3655}
3656
3657/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3658/// bit-count for each 16-bit element from the operand. We need slightly
3659/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3660/// 64/128-bit registers.
Jim Grosbach7ccf4632013-03-02 20:16:15 +00003661///
Evan Chengc8e70452012-12-04 22:41:50 +00003662/// Trace for v4i16:
3663/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3664/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3665/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3666/// v4i16:Extracted = [k0 k1 k2 k3 ]
3667static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3668 EVT VT = N->getValueType(0);
3669 DebugLoc DL = N->getDebugLoc();
3670
3671 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3672 if (VT.is64BitVector()) {
3673 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3674 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3675 DAG.getIntPtrConstant(0));
3676 } else {
3677 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3678 BitCounts, DAG.getIntPtrConstant(0));
3679 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3680 }
3681}
3682
3683/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3684/// bit-count for each 32-bit element from the operand. The idea here is
3685/// to split the vector into 16-bit elements, leverage the 16-bit count
3686/// routine, and then combine the results.
3687///
3688/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3689/// input = [v0 v1 ] (vi: 32-bit elements)
3690/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3691/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach7ccf4632013-03-02 20:16:15 +00003692/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengc8e70452012-12-04 22:41:50 +00003693/// [k0 k1 k2 k3 ]
3694/// N1 =+[k1 k0 k3 k2 ]
3695/// [k0 k2 k1 k3 ]
3696/// N2 =+[k1 k3 k0 k2 ]
3697/// [k0 k2 k1 k3 ]
3698/// Extended =+[k1 k3 k0 k2 ]
3699/// [k0 k2 ]
3700/// Extracted=+[k1 k3 ]
3701///
3702static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3703 EVT VT = N->getValueType(0);
3704 DebugLoc DL = N->getDebugLoc();
3705
3706 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3707
3708 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3709 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3710 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3711 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3712 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3713
3714 if (VT.is64BitVector()) {
3715 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3716 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3717 DAG.getIntPtrConstant(0));
3718 } else {
3719 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3720 DAG.getIntPtrConstant(0));
3721 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3722 }
3723}
3724
3725static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3726 const ARMSubtarget *ST) {
3727 EVT VT = N->getValueType(0);
3728
3729 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay105ab4f2012-12-04 23:54:02 +00003730 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3731 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengc8e70452012-12-04 22:41:50 +00003732 "Unexpected type for custom ctpop lowering");
3733
3734 if (VT.getVectorElementType() == MVT::i32)
3735 return lowerCTPOP32BitElements(N, DAG);
3736 else
3737 return lowerCTPOP16BitElements(N, DAG);
3738}
3739
Bob Wilson5bafff32009-06-22 23:27:02 +00003740static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3741 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003742 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003743 DebugLoc dl = N->getDebugLoc();
3744
Bob Wilsond5448bb2010-11-18 21:16:28 +00003745 if (!VT.isVector())
3746 return SDValue();
3747
Bob Wilson5bafff32009-06-22 23:27:02 +00003748 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003749 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003750
Bob Wilsond5448bb2010-11-18 21:16:28 +00003751 // Left shifts translate directly to the vshiftu intrinsic.
3752 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003754 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3755 N->getOperand(0), N->getOperand(1));
3756
3757 assert((N->getOpcode() == ISD::SRA ||
3758 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3759
3760 // NEON uses the same intrinsics for both left and right shifts. For
3761 // right shifts, the shift amounts are negative, so negate the vector of
3762 // shift amounts.
3763 EVT ShiftVT = N->getOperand(1).getValueType();
3764 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3765 getZeroVector(ShiftVT, DAG, dl),
3766 N->getOperand(1));
3767 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3768 Intrinsic::arm_neon_vshifts :
3769 Intrinsic::arm_neon_vshiftu);
3770 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3771 DAG.getConstant(vshiftInt, MVT::i32),
3772 N->getOperand(0), NegatedCount);
3773}
3774
3775static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3776 const ARMSubtarget *ST) {
3777 EVT VT = N->getValueType(0);
3778 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003779
Eli Friedmance392eb2009-08-22 03:13:10 +00003780 // We can get here for a node like i32 = ISD::SHL i32, i64
3781 if (VT != MVT::i64)
3782 return SDValue();
3783
3784 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003785 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003786
Chris Lattner27a6c732007-11-24 07:07:01 +00003787 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3788 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003789 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003790 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003791
Chris Lattner27a6c732007-11-24 07:07:01 +00003792 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003793 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003794
Chris Lattner27a6c732007-11-24 07:07:01 +00003795 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003797 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003799 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003800
Chris Lattner27a6c732007-11-24 07:07:01 +00003801 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3802 // captures the result into a carry flag.
3803 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003804 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003805
Chris Lattner27a6c732007-11-24 07:07:01 +00003806 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003808
Chris Lattner27a6c732007-11-24 07:07:01 +00003809 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003811}
3812
Bob Wilson5bafff32009-06-22 23:27:02 +00003813static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3814 SDValue TmpOp0, TmpOp1;
3815 bool Invert = false;
3816 bool Swap = false;
3817 unsigned Opc = 0;
3818
3819 SDValue Op0 = Op.getOperand(0);
3820 SDValue Op1 = Op.getOperand(1);
3821 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003822 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003823 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3824 DebugLoc dl = Op.getDebugLoc();
3825
3826 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3827 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003828 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003829 case ISD::SETUNE:
3830 case ISD::SETNE: Invert = true; // Fallthrough
3831 case ISD::SETOEQ:
3832 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3833 case ISD::SETOLT:
3834 case ISD::SETLT: Swap = true; // Fallthrough
3835 case ISD::SETOGT:
3836 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3837 case ISD::SETOLE:
3838 case ISD::SETLE: Swap = true; // Fallthrough
3839 case ISD::SETOGE:
3840 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3841 case ISD::SETUGE: Swap = true; // Fallthrough
3842 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3843 case ISD::SETUGT: Swap = true; // Fallthrough
3844 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3845 case ISD::SETUEQ: Invert = true; // Fallthrough
3846 case ISD::SETONE:
3847 // Expand this to (OLT | OGT).
3848 TmpOp0 = Op0;
3849 TmpOp1 = Op1;
3850 Opc = ISD::OR;
3851 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3852 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3853 break;
3854 case ISD::SETUO: Invert = true; // Fallthrough
3855 case ISD::SETO:
3856 // Expand this to (OLT | OGE).
3857 TmpOp0 = Op0;
3858 TmpOp1 = Op1;
3859 Opc = ISD::OR;
3860 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3861 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3862 break;
3863 }
3864 } else {
3865 // Integer comparisons.
3866 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003867 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003868 case ISD::SETNE: Invert = true;
3869 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3870 case ISD::SETLT: Swap = true;
3871 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3872 case ISD::SETLE: Swap = true;
3873 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3874 case ISD::SETULT: Swap = true;
3875 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3876 case ISD::SETULE: Swap = true;
3877 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3878 }
3879
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003880 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003881 if (Opc == ARMISD::VCEQ) {
3882
3883 SDValue AndOp;
3884 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3885 AndOp = Op0;
3886 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3887 AndOp = Op1;
3888
3889 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003890 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003891 AndOp = AndOp.getOperand(0);
3892
3893 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3894 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003895 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3896 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003897 Invert = !Invert;
3898 }
3899 }
3900 }
3901
3902 if (Swap)
3903 std::swap(Op0, Op1);
3904
Owen Andersonc24cb352010-11-08 23:21:22 +00003905 // If one of the operands is a constant vector zero, attempt to fold the
3906 // comparison to a specialized compare-against-zero form.
3907 SDValue SingleOp;
3908 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3909 SingleOp = Op0;
3910 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3911 if (Opc == ARMISD::VCGE)
3912 Opc = ARMISD::VCLEZ;
3913 else if (Opc == ARMISD::VCGT)
3914 Opc = ARMISD::VCLTZ;
3915 SingleOp = Op1;
3916 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003917
Owen Andersonc24cb352010-11-08 23:21:22 +00003918 SDValue Result;
3919 if (SingleOp.getNode()) {
3920 switch (Opc) {
3921 case ARMISD::VCEQ:
3922 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3923 case ARMISD::VCGE:
3924 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3925 case ARMISD::VCLEZ:
3926 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3927 case ARMISD::VCGT:
3928 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3929 case ARMISD::VCLTZ:
3930 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3931 default:
3932 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3933 }
3934 } else {
3935 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3936 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003937
3938 if (Invert)
3939 Result = DAG.getNOT(dl, Result, VT);
3940
3941 return Result;
3942}
3943
Bob Wilsond3c42842010-06-14 22:19:57 +00003944/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3945/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003946/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003947static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3948 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003949 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003950 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003951
Bob Wilson827b2102010-06-15 19:05:35 +00003952 // SplatBitSize is set to the smallest size that splats the vector, so a
3953 // zero vector will always have SplatBitSize == 8. However, NEON modified
3954 // immediate instructions others than VMOV do not support the 8-bit encoding
3955 // of a zero vector, and the default encoding of zero is supposed to be the
3956 // 32-bit version.
3957 if (SplatBits == 0)
3958 SplatBitSize = 32;
3959
Bob Wilson5bafff32009-06-22 23:27:02 +00003960 switch (SplatBitSize) {
3961 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003962 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003963 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003964 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003965 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003966 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003967 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003968 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003969 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003970
3971 case 16:
3972 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003973 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003974 if ((SplatBits & ~0xff) == 0) {
3975 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003976 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003977 Imm = SplatBits;
3978 break;
3979 }
3980 if ((SplatBits & ~0xff00) == 0) {
3981 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003982 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003983 Imm = SplatBits >> 8;
3984 break;
3985 }
3986 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003987
3988 case 32:
3989 // NEON's 32-bit VMOV supports splat values where:
3990 // * only one byte is nonzero, or
3991 // * the least significant byte is 0xff and the second byte is nonzero, or
3992 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003993 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003994 if ((SplatBits & ~0xff) == 0) {
3995 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003996 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003997 Imm = SplatBits;
3998 break;
3999 }
4000 if ((SplatBits & ~0xff00) == 0) {
4001 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004002 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004003 Imm = SplatBits >> 8;
4004 break;
4005 }
4006 if ((SplatBits & ~0xff0000) == 0) {
4007 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004008 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004009 Imm = SplatBits >> 16;
4010 break;
4011 }
4012 if ((SplatBits & ~0xff000000) == 0) {
4013 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004014 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004015 Imm = SplatBits >> 24;
4016 break;
4017 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004018
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004019 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4020 if (type == OtherModImm) return SDValue();
4021
Bob Wilson5bafff32009-06-22 23:27:02 +00004022 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00004023 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4024 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004025 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004026 Imm = SplatBits >> 8;
4027 SplatBits |= 0xff;
4028 break;
4029 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004030
4031 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00004032 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4033 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004034 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004035 Imm = SplatBits >> 16;
4036 SplatBits |= 0xffff;
4037 break;
4038 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004039
4040 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4041 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4042 // VMOV.I32. A (very) minor optimization would be to replicate the value
4043 // and fall through here to test for a valid 64-bit splat. But, then the
4044 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00004045 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004046
4047 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004048 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00004049 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004050 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00004051 uint64_t BitMask = 0xff;
4052 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004053 unsigned ImmMask = 1;
4054 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00004055 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00004056 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004057 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004058 Imm |= ImmMask;
4059 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004060 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00004061 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004062 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004063 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00004064 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00004065 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004066 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004067 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00004068 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00004069 break;
4070 }
4071
Bob Wilson1a913ed2010-06-11 21:34:50 +00004072 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00004073 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00004074 }
4075
Bob Wilsoncba270d2010-07-13 21:16:48 +00004076 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4077 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00004078}
4079
Lang Hamesc0a9f822012-03-29 21:56:11 +00004080SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4081 const ARMSubtarget *ST) const {
4082 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
4083 return SDValue();
4084
4085 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4086 assert(Op.getValueType() == MVT::f32 &&
4087 "ConstantFP custom lowering should only occur for f32.");
4088
4089 // Try splatting with a VMOV.f32...
4090 APFloat FPVal = CFP->getValueAPF();
4091 int ImmVal = ARM_AM::getFP32Imm(FPVal);
4092 if (ImmVal != -1) {
4093 DebugLoc DL = Op.getDebugLoc();
4094 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4095 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4096 NewVal);
4097 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4098 DAG.getConstant(0, MVT::i32));
4099 }
4100
4101 // If that fails, try a VMOV.i32
4102 EVT VMovVT;
4103 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
4104 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
4105 VMOVModImm);
4106 if (NewVal != SDValue()) {
4107 DebugLoc DL = Op.getDebugLoc();
4108 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4109 NewVal);
4110 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4111 VecConstant);
4112 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4113 DAG.getConstant(0, MVT::i32));
4114 }
4115
4116 // Finally, try a VMVN.i32
4117 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
4118 VMVNModImm);
4119 if (NewVal != SDValue()) {
4120 DebugLoc DL = Op.getDebugLoc();
4121 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4122 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4123 VecConstant);
4124 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4125 DAG.getConstant(0, MVT::i32));
4126 }
4127
4128 return SDValue();
4129}
4130
Quentin Colombet43934ae2012-11-02 21:32:17 +00004131// check if an VEXT instruction can handle the shuffle mask when the
4132// vector sources of the shuffle are the same.
4133static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4134 unsigned NumElts = VT.getVectorNumElements();
4135
4136 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4137 if (M[0] < 0)
4138 return false;
4139
4140 Imm = M[0];
4141
4142 // If this is a VEXT shuffle, the immediate value is the index of the first
4143 // element. The other shuffle indices must be the successive elements after
4144 // the first one.
4145 unsigned ExpectedElt = Imm;
4146 for (unsigned i = 1; i < NumElts; ++i) {
4147 // Increment the expected index. If it wraps around, just follow it
4148 // back to index zero and keep going.
4149 ++ExpectedElt;
4150 if (ExpectedElt == NumElts)
4151 ExpectedElt = 0;
4152
4153 if (M[i] < 0) continue; // ignore UNDEF indices
4154 if (ExpectedElt != static_cast<unsigned>(M[i]))
4155 return false;
4156 }
4157
4158 return true;
4159}
4160
Lang Hamesc0a9f822012-03-29 21:56:11 +00004161
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004162static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004163 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004164 unsigned NumElts = VT.getVectorNumElements();
4165 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004166
4167 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4168 if (M[0] < 0)
4169 return false;
4170
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004171 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004172
4173 // If this is a VEXT shuffle, the immediate value is the index of the first
4174 // element. The other shuffle indices must be the successive elements after
4175 // the first one.
4176 unsigned ExpectedElt = Imm;
4177 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004178 // Increment the expected index. If it wraps around, it may still be
4179 // a VEXT but the source vectors must be swapped.
4180 ExpectedElt += 1;
4181 if (ExpectedElt == NumElts * 2) {
4182 ExpectedElt = 0;
4183 ReverseVEXT = true;
4184 }
4185
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004186 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004187 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004188 return false;
4189 }
4190
4191 // Adjust the index value if the source operands will be swapped.
4192 if (ReverseVEXT)
4193 Imm -= NumElts;
4194
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004195 return true;
4196}
4197
Bob Wilson8bb9e482009-07-26 00:39:34 +00004198/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4199/// instruction with the specified blocksize. (The order of the elements
4200/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004201static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004202 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4203 "Only possible block sizes for VREV are: 16, 32, 64");
4204
Bob Wilson8bb9e482009-07-26 00:39:34 +00004205 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004206 if (EltSz == 64)
4207 return false;
4208
4209 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004210 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004211 // If the first shuffle index is UNDEF, be optimistic.
4212 if (M[0] < 0)
4213 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004214
4215 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4216 return false;
4217
4218 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004219 if (M[i] < 0) continue; // ignore UNDEF indices
4220 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004221 return false;
4222 }
4223
4224 return true;
4225}
4226
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004227static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004228 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4229 // range, then 0 is placed into the resulting vector. So pretty much any mask
4230 // of 8 elements can work here.
4231 return VT == MVT::v8i8 && M.size() == 8;
4232}
4233
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004234static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004235 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4236 if (EltSz == 64)
4237 return false;
4238
Bob Wilsonc692cb72009-08-21 20:54:19 +00004239 unsigned NumElts = VT.getVectorNumElements();
4240 WhichResult = (M[0] == 0 ? 0 : 1);
4241 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004242 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4243 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004244 return false;
4245 }
4246 return true;
4247}
4248
Bob Wilson324f4f12009-12-03 06:40:55 +00004249/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4250/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4251/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004252static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004253 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4254 if (EltSz == 64)
4255 return false;
4256
4257 unsigned NumElts = VT.getVectorNumElements();
4258 WhichResult = (M[0] == 0 ? 0 : 1);
4259 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004260 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4261 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004262 return false;
4263 }
4264 return true;
4265}
4266
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004267static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004268 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4269 if (EltSz == 64)
4270 return false;
4271
Bob Wilsonc692cb72009-08-21 20:54:19 +00004272 unsigned NumElts = VT.getVectorNumElements();
4273 WhichResult = (M[0] == 0 ? 0 : 1);
4274 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004275 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004276 if ((unsigned) M[i] != 2 * i + WhichResult)
4277 return false;
4278 }
4279
4280 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004281 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004282 return false;
4283
4284 return true;
4285}
4286
Bob Wilson324f4f12009-12-03 06:40:55 +00004287/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4288/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4289/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004290static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004291 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4292 if (EltSz == 64)
4293 return false;
4294
4295 unsigned Half = VT.getVectorNumElements() / 2;
4296 WhichResult = (M[0] == 0 ? 0 : 1);
4297 for (unsigned j = 0; j != 2; ++j) {
4298 unsigned Idx = WhichResult;
4299 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004300 int MIdx = M[i + j * Half];
4301 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004302 return false;
4303 Idx += 2;
4304 }
4305 }
4306
4307 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4308 if (VT.is64BitVector() && EltSz == 32)
4309 return false;
4310
4311 return true;
4312}
4313
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004314static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004315 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4316 if (EltSz == 64)
4317 return false;
4318
Bob Wilsonc692cb72009-08-21 20:54:19 +00004319 unsigned NumElts = VT.getVectorNumElements();
4320 WhichResult = (M[0] == 0 ? 0 : 1);
4321 unsigned Idx = WhichResult * NumElts / 2;
4322 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004323 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4324 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004325 return false;
4326 Idx += 1;
4327 }
4328
4329 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004330 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004331 return false;
4332
4333 return true;
4334}
4335
Bob Wilson324f4f12009-12-03 06:40:55 +00004336/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4337/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4338/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004339static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004340 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4341 if (EltSz == 64)
4342 return false;
4343
4344 unsigned NumElts = VT.getVectorNumElements();
4345 WhichResult = (M[0] == 0 ? 0 : 1);
4346 unsigned Idx = WhichResult * NumElts / 2;
4347 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004348 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4349 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004350 return false;
4351 Idx += 1;
4352 }
4353
4354 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4355 if (VT.is64BitVector() && EltSz == 32)
4356 return false;
4357
4358 return true;
4359}
4360
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004361/// \return true if this is a reverse operation on an vector.
4362static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4363 unsigned NumElts = VT.getVectorNumElements();
4364 // Make sure the mask has the right size.
4365 if (NumElts != M.size())
4366 return false;
4367
4368 // Look for <15, ..., 3, -1, 1, 0>.
4369 for (unsigned i = 0; i != NumElts; ++i)
4370 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4371 return false;
4372
4373 return true;
4374}
4375
Dale Johannesenf630c712010-07-29 20:10:08 +00004376// If N is an integer constant that can be moved into a register in one
4377// instruction, return an SDValue of such a constant (will become a MOV
4378// instruction). Otherwise return null.
4379static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4380 const ARMSubtarget *ST, DebugLoc dl) {
4381 uint64_t Val;
4382 if (!isa<ConstantSDNode>(N))
4383 return SDValue();
4384 Val = cast<ConstantSDNode>(N)->getZExtValue();
4385
4386 if (ST->isThumb1Only()) {
4387 if (Val <= 255 || ~Val <= 255)
4388 return DAG.getConstant(Val, MVT::i32);
4389 } else {
4390 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4391 return DAG.getConstant(Val, MVT::i32);
4392 }
4393 return SDValue();
4394}
4395
Bob Wilson5bafff32009-06-22 23:27:02 +00004396// If this is a case we can't handle, return null and let the default
4397// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004398SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4399 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004400 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004401 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004402 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004403
4404 APInt SplatBits, SplatUndef;
4405 unsigned SplatBitSize;
4406 bool HasAnyUndefs;
4407 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004408 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004409 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004410 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004411 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004412 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004413 DAG, VmovVT, VT.is128BitVector(),
4414 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004415 if (Val.getNode()) {
4416 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004417 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004418 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004419
4420 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004421 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004422 Val = isNEONModifiedImm(NegatedImm,
4423 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004424 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004425 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004426 if (Val.getNode()) {
4427 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004428 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004429 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004430
4431 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004432 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004433 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004434 if (ImmVal != -1) {
4435 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4436 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4437 }
4438 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004439 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004440 }
4441
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004442 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004443 //
4444 // As an optimisation, even if more than one value is used it may be more
4445 // profitable to splat with one value then change some lanes.
4446 //
4447 // Heuristically we decide to do this if the vector has a "dominant" value,
4448 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004449 unsigned NumElts = VT.getVectorNumElements();
4450 bool isOnlyLowElement = true;
4451 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004452 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004453 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004454
4455 // Map of the number of times a particular SDValue appears in the
4456 // element list.
James Molloy95154342012-09-06 10:32:08 +00004457 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004458 SDValue Value;
4459 for (unsigned i = 0; i < NumElts; ++i) {
4460 SDValue V = Op.getOperand(i);
4461 if (V.getOpcode() == ISD::UNDEF)
4462 continue;
4463 if (i > 0)
4464 isOnlyLowElement = false;
4465 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4466 isConstant = false;
4467
James Molloyba8562a2012-09-06 09:55:02 +00004468 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004469 unsigned &Count = ValueCounts[V];
Jim Grosbach7ccf4632013-03-02 20:16:15 +00004470
James Molloyba8562a2012-09-06 09:55:02 +00004471 // Is this value dominant? (takes up more than half of the lanes)
4472 if (++Count > (NumElts / 2)) {
4473 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004474 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004475 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004476 }
James Molloyba8562a2012-09-06 09:55:02 +00004477 if (ValueCounts.size() != 1)
4478 usesOnlyOneValue = false;
4479 if (!Value.getNode() && ValueCounts.size() > 0)
4480 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004481
James Molloyba8562a2012-09-06 09:55:02 +00004482 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004483 return DAG.getUNDEF(VT);
4484
4485 if (isOnlyLowElement)
4486 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4487
Dale Johannesenf630c712010-07-29 20:10:08 +00004488 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4489
Dale Johannesen575cd142010-10-19 20:00:17 +00004490 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4491 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004492 if (hasDominantValue && EltSize <= 32) {
4493 if (!isConstant) {
4494 SDValue N;
4495
4496 // If we are VDUPing a value that comes directly from a vector, that will
4497 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbach7bf504c2013-03-02 20:16:24 +00004498 // just use VDUPLANE. We can only do this if the lane being extracted
4499 // is at a constant index, as the VDUP from lane instructions only have
4500 // constant-index forms.
4501 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4502 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangabb1078e2012-10-15 09:41:32 +00004503 // We need to create a new undef vector to use for the VDUPLANE if the
4504 // size of the vector from which we get the value is different than the
4505 // size of the vector that we need to create. We will insert the element
4506 // such that the register coalescer will remove unnecessary copies.
4507 if (VT != Value->getOperand(0).getValueType()) {
4508 ConstantSDNode *constIndex;
4509 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4510 assert(constIndex && "The index is not a constant!");
4511 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4512 VT.getVectorNumElements();
4513 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4514 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4515 Value, DAG.getConstant(index, MVT::i32)),
4516 DAG.getConstant(index, MVT::i32));
Jim Grosbach65da9f12013-03-02 20:16:19 +00004517 } else
Silviu Barangabb1078e2012-10-15 09:41:32 +00004518 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004519 Value->getOperand(0), Value->getOperand(1));
Jim Grosbach65da9f12013-03-02 20:16:19 +00004520 } else
James Molloyba8562a2012-09-06 09:55:02 +00004521 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4522
4523 if (!usesOnlyOneValue) {
4524 // The dominant value was splatted as 'N', but we now have to insert
4525 // all differing elements.
4526 for (unsigned I = 0; I < NumElts; ++I) {
4527 if (Op.getOperand(I) == Value)
4528 continue;
4529 SmallVector<SDValue, 3> Ops;
4530 Ops.push_back(N);
4531 Ops.push_back(Op.getOperand(I));
4532 Ops.push_back(DAG.getConstant(I, MVT::i32));
4533 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4534 }
4535 }
4536 return N;
4537 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004538 if (VT.getVectorElementType().isFloatingPoint()) {
4539 SmallVector<SDValue, 8> Ops;
4540 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004541 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004542 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004543 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4544 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004545 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4546 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004547 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004548 }
James Molloyba8562a2012-09-06 09:55:02 +00004549 if (usesOnlyOneValue) {
4550 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4551 if (isConstant && Val.getNode())
Jim Grosbach7ccf4632013-03-02 20:16:15 +00004552 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloyba8562a2012-09-06 09:55:02 +00004553 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004554 }
4555
4556 // If all elements are constants and the case above didn't get hit, fall back
4557 // to the default expansion, which will generate a load from the constant
4558 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004559 if (isConstant)
4560 return SDValue();
4561
Bob Wilson11a1dff2011-01-07 21:37:30 +00004562 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4563 if (NumElts >= 4) {
4564 SDValue shuffle = ReconstructShuffle(Op, DAG);
4565 if (shuffle != SDValue())
4566 return shuffle;
4567 }
4568
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004569 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004570 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4571 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004572 if (EltSize >= 32) {
4573 // Do the expansion with floating-point types, since that is what the VFP
4574 // registers are defined to use, and since i64 is not legal.
4575 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4576 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004577 SmallVector<SDValue, 8> Ops;
4578 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004579 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004580 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004581 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004582 }
4583
4584 return SDValue();
4585}
4586
Bob Wilson11a1dff2011-01-07 21:37:30 +00004587// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004588// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004589SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4590 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004591 DebugLoc dl = Op.getDebugLoc();
4592 EVT VT = Op.getValueType();
4593 unsigned NumElts = VT.getVectorNumElements();
4594
4595 SmallVector<SDValue, 2> SourceVecs;
4596 SmallVector<unsigned, 2> MinElts;
4597 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004598
Bob Wilson11a1dff2011-01-07 21:37:30 +00004599 for (unsigned i = 0; i < NumElts; ++i) {
4600 SDValue V = Op.getOperand(i);
4601 if (V.getOpcode() == ISD::UNDEF)
4602 continue;
4603 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4604 // A shuffle can only come from building a vector from various
4605 // elements of other vectors.
4606 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004607 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4608 VT.getVectorElementType()) {
4609 // This code doesn't know how to handle shuffles where the vector
4610 // element types do not match (this happens because type legalization
4611 // promotes the return type of EXTRACT_VECTOR_ELT).
4612 // FIXME: It might be appropriate to extend this code to handle
4613 // mismatched types.
4614 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004615 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004616
Bob Wilson11a1dff2011-01-07 21:37:30 +00004617 // Record this extraction against the appropriate vector if possible...
4618 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004619 // If the element number isn't a constant, we can't effectively
4620 // analyze what's going on.
4621 if (!isa<ConstantSDNode>(V.getOperand(1)))
4622 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004623 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4624 bool FoundSource = false;
4625 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4626 if (SourceVecs[j] == SourceVec) {
4627 if (MinElts[j] > EltNo)
4628 MinElts[j] = EltNo;
4629 if (MaxElts[j] < EltNo)
4630 MaxElts[j] = EltNo;
4631 FoundSource = true;
4632 break;
4633 }
4634 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004635
Bob Wilson11a1dff2011-01-07 21:37:30 +00004636 // Or record a new source if not...
4637 if (!FoundSource) {
4638 SourceVecs.push_back(SourceVec);
4639 MinElts.push_back(EltNo);
4640 MaxElts.push_back(EltNo);
4641 }
4642 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004643
Bob Wilson11a1dff2011-01-07 21:37:30 +00004644 // Currently only do something sane when at most two source vectors
4645 // involved.
4646 if (SourceVecs.size() > 2)
4647 return SDValue();
4648
4649 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4650 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004651
Bob Wilson11a1dff2011-01-07 21:37:30 +00004652 // This loop extracts the usage patterns of the source vectors
4653 // and prepares appropriate SDValues for a shuffle if possible.
4654 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4655 if (SourceVecs[i].getValueType() == VT) {
4656 // No VEXT necessary
4657 ShuffleSrcs[i] = SourceVecs[i];
4658 VEXTOffsets[i] = 0;
4659 continue;
4660 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4661 // It probably isn't worth padding out a smaller vector just to
4662 // break it down again in a shuffle.
4663 return SDValue();
4664 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004665
Bob Wilson11a1dff2011-01-07 21:37:30 +00004666 // Since only 64-bit and 128-bit vectors are legal on ARM and
4667 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004668 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4669 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004670
Bob Wilson11a1dff2011-01-07 21:37:30 +00004671 if (MaxElts[i] - MinElts[i] >= NumElts) {
4672 // Span too large for a VEXT to cope
4673 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004674 }
4675
Bob Wilson11a1dff2011-01-07 21:37:30 +00004676 if (MinElts[i] >= NumElts) {
4677 // The extraction can just take the second half
4678 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004679 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4680 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004681 DAG.getIntPtrConstant(NumElts));
4682 } else if (MaxElts[i] < NumElts) {
4683 // The extraction can just take the first half
4684 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004685 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4686 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004687 DAG.getIntPtrConstant(0));
4688 } else {
4689 // An actual VEXT is needed
4690 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004691 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4692 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004693 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004694 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4695 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004696 DAG.getIntPtrConstant(NumElts));
4697 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4698 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4699 }
4700 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004701
Bob Wilson11a1dff2011-01-07 21:37:30 +00004702 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004703
Bob Wilson11a1dff2011-01-07 21:37:30 +00004704 for (unsigned i = 0; i < NumElts; ++i) {
4705 SDValue Entry = Op.getOperand(i);
4706 if (Entry.getOpcode() == ISD::UNDEF) {
4707 Mask.push_back(-1);
4708 continue;
4709 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004710
Bob Wilson11a1dff2011-01-07 21:37:30 +00004711 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004712 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4713 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004714 if (ExtractVec == SourceVecs[0]) {
4715 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4716 } else {
4717 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4718 }
4719 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004720
Bob Wilson11a1dff2011-01-07 21:37:30 +00004721 // Final check before we try to produce nonsense...
4722 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004723 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4724 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004725
Bob Wilson11a1dff2011-01-07 21:37:30 +00004726 return SDValue();
4727}
4728
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004729/// isShuffleMaskLegal - Targets can use this to indicate that they only
4730/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4731/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4732/// are assumed to be legal.
4733bool
4734ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4735 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004736 if (VT.getVectorNumElements() == 4 &&
4737 (VT.is128BitVector() || VT.is64BitVector())) {
4738 unsigned PFIndexes[4];
4739 for (unsigned i = 0; i != 4; ++i) {
4740 if (M[i] < 0)
4741 PFIndexes[i] = 8;
4742 else
4743 PFIndexes[i] = M[i];
4744 }
4745
4746 // Compute the index in the perfect shuffle table.
4747 unsigned PFTableIndex =
4748 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4749 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4750 unsigned Cost = (PFEntry >> 30);
4751
4752 if (Cost <= 4)
4753 return true;
4754 }
4755
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004756 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004757 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004758
Bob Wilson53dd2452010-06-07 23:53:38 +00004759 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4760 return (EltSize >= 32 ||
4761 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004762 isVREVMask(M, VT, 64) ||
4763 isVREVMask(M, VT, 32) ||
4764 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004765 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004766 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004767 isVTRNMask(M, VT, WhichResult) ||
4768 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004769 isVZIPMask(M, VT, WhichResult) ||
4770 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4771 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004772 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
4773 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004774}
4775
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004776/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4777/// the specified operations to build the shuffle.
4778static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4779 SDValue RHS, SelectionDAG &DAG,
4780 DebugLoc dl) {
4781 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4782 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4783 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4784
4785 enum {
4786 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4787 OP_VREV,
4788 OP_VDUP0,
4789 OP_VDUP1,
4790 OP_VDUP2,
4791 OP_VDUP3,
4792 OP_VEXT1,
4793 OP_VEXT2,
4794 OP_VEXT3,
4795 OP_VUZPL, // VUZP, left result
4796 OP_VUZPR, // VUZP, right result
4797 OP_VZIPL, // VZIP, left result
4798 OP_VZIPR, // VZIP, right result
4799 OP_VTRNL, // VTRN, left result
4800 OP_VTRNR // VTRN, right result
4801 };
4802
4803 if (OpNum == OP_COPY) {
4804 if (LHSID == (1*9+2)*9+3) return LHS;
4805 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4806 return RHS;
4807 }
4808
4809 SDValue OpLHS, OpRHS;
4810 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4811 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4812 EVT VT = OpLHS.getValueType();
4813
4814 switch (OpNum) {
4815 default: llvm_unreachable("Unknown shuffle opcode!");
4816 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004817 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004818 if (VT.getVectorElementType() == MVT::i32 ||
4819 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004820 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4821 // vrev <4 x i16> -> VREV32
4822 if (VT.getVectorElementType() == MVT::i16)
4823 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4824 // vrev <4 x i8> -> VREV16
4825 assert(VT.getVectorElementType() == MVT::i8);
4826 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004827 case OP_VDUP0:
4828 case OP_VDUP1:
4829 case OP_VDUP2:
4830 case OP_VDUP3:
4831 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004832 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004833 case OP_VEXT1:
4834 case OP_VEXT2:
4835 case OP_VEXT3:
4836 return DAG.getNode(ARMISD::VEXT, dl, VT,
4837 OpLHS, OpRHS,
4838 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4839 case OP_VUZPL:
4840 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004841 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004842 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4843 case OP_VZIPL:
4844 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004845 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004846 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4847 case OP_VTRNL:
4848 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004849 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4850 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004851 }
4852}
4853
Bill Wendling69a05a72011-03-14 23:02:38 +00004854static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004855 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004856 SelectionDAG &DAG) {
4857 // Check to see if we can use the VTBL instruction.
4858 SDValue V1 = Op.getOperand(0);
4859 SDValue V2 = Op.getOperand(1);
4860 DebugLoc DL = Op.getDebugLoc();
4861
4862 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004863 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004864 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4865 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4866
4867 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4868 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4869 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4870 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004871
Owen Anderson76706012011-04-05 21:48:57 +00004872 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004873 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4874 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004875}
4876
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004877static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
4878 SelectionDAG &DAG) {
4879 DebugLoc DL = Op.getDebugLoc();
4880 SDValue OpLHS = Op.getOperand(0);
4881 EVT VT = OpLHS.getValueType();
4882
4883 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
4884 "Expect an v8i16/v16i8 type");
4885 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
4886 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
4887 // extract the first 8 bytes into the top double word and the last 8 bytes
4888 // into the bottom double word. The v8i16 case is similar.
4889 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
4890 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
4891 DAG.getConstant(ExtractNum, MVT::i32));
4892}
4893
Bob Wilson5bafff32009-06-22 23:27:02 +00004894static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004895 SDValue V1 = Op.getOperand(0);
4896 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004897 DebugLoc dl = Op.getDebugLoc();
4898 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004899 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004900
Bob Wilson28865062009-08-13 02:13:04 +00004901 // Convert shuffles that are directly supported on NEON to target-specific
4902 // DAG nodes, instead of keeping them as shuffles and matching them again
4903 // during code selection. This is more efficient and avoids the possibility
4904 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004905 // FIXME: floating-point vectors should be canonicalized to integer vectors
4906 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004907 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004908
Bob Wilson53dd2452010-06-07 23:53:38 +00004909 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4910 if (EltSize <= 32) {
4911 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4912 int Lane = SVN->getSplatIndex();
4913 // If this is undef splat, generate it via "just" vdup, if possible.
4914 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004915
Dan Gohman65fd6562011-11-03 21:49:52 +00004916 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004917 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4918 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4919 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004920 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4921 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4922 // reaches it).
4923 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4924 !isa<ConstantSDNode>(V1.getOperand(0))) {
4925 bool IsScalarToVector = true;
4926 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4927 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4928 IsScalarToVector = false;
4929 break;
4930 }
4931 if (IsScalarToVector)
4932 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4933 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004934 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4935 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004936 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004937
4938 bool ReverseVEXT;
4939 unsigned Imm;
4940 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4941 if (ReverseVEXT)
4942 std::swap(V1, V2);
4943 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4944 DAG.getConstant(Imm, MVT::i32));
4945 }
4946
4947 if (isVREVMask(ShuffleMask, VT, 64))
4948 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4949 if (isVREVMask(ShuffleMask, VT, 32))
4950 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4951 if (isVREVMask(ShuffleMask, VT, 16))
4952 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4953
Quentin Colombet43934ae2012-11-02 21:32:17 +00004954 if (V2->getOpcode() == ISD::UNDEF &&
4955 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4956 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4957 DAG.getConstant(Imm, MVT::i32));
4958 }
4959
Bob Wilson53dd2452010-06-07 23:53:38 +00004960 // Check for Neon shuffles that modify both input vectors in place.
4961 // If both results are used, i.e., if there are two shuffles with the same
4962 // source operands and with masks corresponding to both results of one of
4963 // these operations, DAG memoization will ensure that a single node is
4964 // used for both shuffles.
4965 unsigned WhichResult;
4966 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4967 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4968 V1, V2).getValue(WhichResult);
4969 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4970 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4971 V1, V2).getValue(WhichResult);
4972 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4973 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4974 V1, V2).getValue(WhichResult);
4975
4976 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4977 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4978 V1, V1).getValue(WhichResult);
4979 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4980 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4981 V1, V1).getValue(WhichResult);
4982 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4983 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4984 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004985 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004986
Bob Wilsonc692cb72009-08-21 20:54:19 +00004987 // If the shuffle is not directly supported and it has 4 elements, use
4988 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004989 unsigned NumElts = VT.getVectorNumElements();
4990 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004991 unsigned PFIndexes[4];
4992 for (unsigned i = 0; i != 4; ++i) {
4993 if (ShuffleMask[i] < 0)
4994 PFIndexes[i] = 8;
4995 else
4996 PFIndexes[i] = ShuffleMask[i];
4997 }
4998
4999 // Compute the index in the perfect shuffle table.
5000 unsigned PFTableIndex =
5001 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00005002 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5003 unsigned Cost = (PFEntry >> 30);
5004
5005 if (Cost <= 4)
5006 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5007 }
Bob Wilsond8e17572009-08-12 22:31:50 +00005008
Bob Wilson40cbe7d2010-06-04 00:04:02 +00005009 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00005010 if (EltSize >= 32) {
5011 // Do the expansion with floating-point types, since that is what the VFP
5012 // registers are defined to use, and since i64 is not legal.
5013 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5014 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005015 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5016 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00005017 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00005018 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00005019 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00005020 Ops.push_back(DAG.getUNDEF(EltVT));
5021 else
5022 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5023 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5024 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5025 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00005026 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00005027 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005028 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00005029 }
5030
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00005031 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5032 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5033
Bill Wendling69a05a72011-03-14 23:02:38 +00005034 if (VT == MVT::v8i8) {
5035 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5036 if (NewOp.getNode())
5037 return NewOp;
5038 }
5039
Bob Wilson22cac0d2009-08-14 05:16:33 +00005040 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00005041}
5042
Eli Friedman5c89cb82011-10-24 23:08:52 +00005043static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5044 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5045 SDValue Lane = Op.getOperand(2);
5046 if (!isa<ConstantSDNode>(Lane))
5047 return SDValue();
5048
5049 return Op;
5050}
5051
Bob Wilson5bafff32009-06-22 23:27:02 +00005052static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00005053 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00005054 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00005055 if (!isa<ConstantSDNode>(Lane))
5056 return SDValue();
5057
5058 SDValue Vec = Op.getOperand(0);
5059 if (Op.getValueType() == MVT::i32 &&
5060 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
5061 DebugLoc dl = Op.getDebugLoc();
5062 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5063 }
5064
5065 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00005066}
5067
Bob Wilsona6d65862009-08-03 20:36:38 +00005068static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5069 // The only time a CONCAT_VECTORS operation can have legal types is when
5070 // two 64-bit vectors are concatenated to a 128-bit vector.
5071 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5072 "unexpected CONCAT_VECTORS");
5073 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00005075 SDValue Op0 = Op.getOperand(0);
5076 SDValue Op1 = Op.getOperand(1);
5077 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005078 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005079 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00005080 DAG.getIntPtrConstant(0));
5081 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005082 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005083 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00005084 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005085 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00005086}
5087
Bob Wilson626613d2010-11-23 19:38:38 +00005088/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5089/// element has been zero/sign-extended, depending on the isSigned parameter,
5090/// from an integer type half its size.
5091static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5092 bool isSigned) {
5093 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5094 EVT VT = N->getValueType(0);
5095 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5096 SDNode *BVN = N->getOperand(0).getNode();
5097 if (BVN->getValueType(0) != MVT::v4i32 ||
5098 BVN->getOpcode() != ISD::BUILD_VECTOR)
5099 return false;
5100 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5101 unsigned HiElt = 1 - LoElt;
5102 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5103 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5104 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5105 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5106 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5107 return false;
5108 if (isSigned) {
5109 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5110 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5111 return true;
5112 } else {
5113 if (Hi0->isNullValue() && Hi1->isNullValue())
5114 return true;
5115 }
5116 return false;
5117 }
5118
5119 if (N->getOpcode() != ISD::BUILD_VECTOR)
5120 return false;
5121
5122 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5123 SDNode *Elt = N->getOperand(i).getNode();
5124 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5125 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5126 unsigned HalfSize = EltSize / 2;
5127 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00005128 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005129 return false;
5130 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00005131 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005132 return false;
5133 }
5134 continue;
5135 }
5136 return false;
5137 }
5138
5139 return true;
5140}
5141
5142/// isSignExtended - Check if a node is a vector value that is sign-extended
5143/// or a constant BUILD_VECTOR with sign-extended elements.
5144static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5145 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5146 return true;
5147 if (isExtendedBUILD_VECTOR(N, DAG, true))
5148 return true;
5149 return false;
5150}
5151
5152/// isZeroExtended - Check if a node is a vector value that is zero-extended
5153/// or a constant BUILD_VECTOR with zero-extended elements.
5154static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5155 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5156 return true;
5157 if (isExtendedBUILD_VECTOR(N, DAG, false))
5158 return true;
5159 return false;
5160}
5161
Sebastian Popcb495302012-11-30 19:08:04 +00005162/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5163/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5164/// We insert the required extension here to get the vector to fill a D register.
5165static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5166 const EVT &OrigTy,
5167 const EVT &ExtTy,
5168 unsigned ExtOpcode) {
5169 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5170 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5171 // 64-bits we need to insert a new extension so that it will be 64-bits.
5172 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5173 if (OrigTy.getSizeInBits() >= 64)
5174 return N;
5175
5176 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5177 MVT::SimpleValueType OrigSimpleTy = OrigTy.getSimpleVT().SimpleTy;
5178 EVT NewVT;
5179 switch (OrigSimpleTy) {
5180 default: llvm_unreachable("Unexpected Orig Vector Type");
5181 case MVT::v2i8:
5182 case MVT::v2i16:
5183 NewVT = MVT::v2i32;
5184 break;
5185 case MVT::v4i8:
5186 NewVT = MVT::v4i16;
5187 break;
5188 }
5189 return DAG.getNode(ExtOpcode, N->getDebugLoc(), NewVT, N);
5190}
5191
5192/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5193/// does not do any sign/zero extension. If the original vector is less
5194/// than 64 bits, an appropriate extension will be added after the load to
5195/// reach a total size of 64 bits. We have to add the extension separately
5196/// because ARM does not have a sign/zero extending load for vectors.
5197static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5198 SDValue NonExtendingLoad =
5199 DAG.getLoad(LD->getMemoryVT(), LD->getDebugLoc(), LD->getChain(),
5200 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5201 LD->isNonTemporal(), LD->isInvariant(),
5202 LD->getAlignment());
5203 unsigned ExtOp = 0;
5204 switch (LD->getExtensionType()) {
5205 default: llvm_unreachable("Unexpected LoadExtType");
5206 case ISD::EXTLOAD:
5207 case ISD::SEXTLOAD: ExtOp = ISD::SIGN_EXTEND; break;
5208 case ISD::ZEXTLOAD: ExtOp = ISD::ZERO_EXTEND; break;
5209 }
5210 MVT::SimpleValueType MemType = LD->getMemoryVT().getSimpleVT().SimpleTy;
5211 MVT::SimpleValueType ExtType = LD->getValueType(0).getSimpleVT().SimpleTy;
5212 return AddRequiredExtensionForVMULL(NonExtendingLoad, DAG,
5213 MemType, ExtType, ExtOp);
5214}
5215
5216/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5217/// extending load, or BUILD_VECTOR with extended elements, return the
5218/// unextended value. The unextended vector should be 64 bits so that it can
5219/// be used as an operand to a VMULL instruction. If the original vector size
5220/// before extension is less than 64 bits we add a an extension to resize
5221/// the vector to 64 bits.
5222static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005223 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popcb495302012-11-30 19:08:04 +00005224 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5225 N->getOperand(0)->getValueType(0),
5226 N->getValueType(0),
5227 N->getOpcode());
5228
Bob Wilson626613d2010-11-23 19:38:38 +00005229 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popcb495302012-11-30 19:08:04 +00005230 return SkipLoadExtensionForVMULL(LD, DAG);
5231
Bob Wilson626613d2010-11-23 19:38:38 +00005232 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5233 // have been legalized as a BITCAST from v4i32.
5234 if (N->getOpcode() == ISD::BITCAST) {
5235 SDNode *BVN = N->getOperand(0).getNode();
5236 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5237 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5238 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5239 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
5240 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5241 }
5242 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5243 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5244 EVT VT = N->getValueType(0);
5245 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5246 unsigned NumElts = VT.getVectorNumElements();
5247 MVT TruncVT = MVT::getIntegerVT(EltSize);
5248 SmallVector<SDValue, 8> Ops;
5249 for (unsigned i = 0; i != NumElts; ++i) {
5250 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5251 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00005252 // Element types smaller than 32 bits are not legal, so use i32 elements.
5253 // The values are implicitly truncated so sext vs. zext doesn't matter.
5254 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00005255 }
5256 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5257 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005258}
5259
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005260static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5261 unsigned Opcode = N->getOpcode();
5262 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5263 SDNode *N0 = N->getOperand(0).getNode();
5264 SDNode *N1 = N->getOperand(1).getNode();
5265 return N0->hasOneUse() && N1->hasOneUse() &&
5266 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5267 }
5268 return false;
5269}
5270
5271static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5272 unsigned Opcode = N->getOpcode();
5273 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5274 SDNode *N0 = N->getOperand(0).getNode();
5275 SDNode *N1 = N->getOperand(1).getNode();
5276 return N0->hasOneUse() && N1->hasOneUse() &&
5277 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5278 }
5279 return false;
5280}
5281
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005282static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5283 // Multiplications are only custom-lowered for 128-bit vectors so that
5284 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5285 EVT VT = Op.getValueType();
Sebastian Popcb495302012-11-30 19:08:04 +00005286 assert(VT.is128BitVector() && VT.isInteger() &&
5287 "unexpected type for custom-lowering ISD::MUL");
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005288 SDNode *N0 = Op.getOperand(0).getNode();
5289 SDNode *N1 = Op.getOperand(1).getNode();
5290 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005291 bool isMLA = false;
5292 bool isN0SExt = isSignExtended(N0, DAG);
5293 bool isN1SExt = isSignExtended(N1, DAG);
5294 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005295 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005296 else {
5297 bool isN0ZExt = isZeroExtended(N0, DAG);
5298 bool isN1ZExt = isZeroExtended(N1, DAG);
5299 if (isN0ZExt && isN1ZExt)
5300 NewOpc = ARMISD::VMULLu;
5301 else if (isN1SExt || isN1ZExt) {
5302 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5303 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5304 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5305 NewOpc = ARMISD::VMULLs;
5306 isMLA = true;
5307 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5308 NewOpc = ARMISD::VMULLu;
5309 isMLA = true;
5310 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5311 std::swap(N0, N1);
5312 NewOpc = ARMISD::VMULLu;
5313 isMLA = true;
5314 }
5315 }
5316
5317 if (!NewOpc) {
5318 if (VT == MVT::v2i64)
5319 // Fall through to expand this. It is not legal.
5320 return SDValue();
5321 else
5322 // Other vector multiplications are legal.
5323 return Op;
5324 }
5325 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005326
5327 // Legalize to a VMULL instruction.
5328 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005329 SDValue Op0;
Sebastian Popcb495302012-11-30 19:08:04 +00005330 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005331 if (!isMLA) {
Sebastian Popcb495302012-11-30 19:08:04 +00005332 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005333 assert(Op0.getValueType().is64BitVector() &&
5334 Op1.getValueType().is64BitVector() &&
5335 "unexpected types for extended operands to VMULL");
5336 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5337 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005338
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005339 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5340 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5341 // vmull q0, d4, d6
5342 // vmlal q0, d5, d6
5343 // is faster than
5344 // vaddl q0, d4, d5
5345 // vmovl q1, d6
5346 // vmul q0, q0, q1
Sebastian Popcb495302012-11-30 19:08:04 +00005347 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5348 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005349 EVT Op1VT = Op1.getValueType();
5350 return DAG.getNode(N0->getOpcode(), DL, VT,
5351 DAG.getNode(NewOpc, DL, VT,
5352 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5353 DAG.getNode(NewOpc, DL, VT,
5354 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005355}
5356
Owen Anderson76706012011-04-05 21:48:57 +00005357static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005358LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5359 // Convert to float
5360 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5361 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5362 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5363 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5364 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5365 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5366 // Get reciprocal estimate.
5367 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005368 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005369 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5370 // Because char has a smaller range than uchar, we can actually get away
5371 // without any newton steps. This requires that we use a weird bias
5372 // of 0xb000, however (again, this has been exhaustively tested).
5373 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5374 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5375 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5376 Y = DAG.getConstant(0xb000, MVT::i32);
5377 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5378 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5379 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5380 // Convert back to short.
5381 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5382 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5383 return X;
5384}
5385
Owen Anderson76706012011-04-05 21:48:57 +00005386static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005387LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5388 SDValue N2;
5389 // Convert to float.
5390 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5391 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5392 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5393 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5394 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5395 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005396
Nate Begeman7973f352011-02-11 20:53:29 +00005397 // Use reciprocal estimate and one refinement step.
5398 // float4 recip = vrecpeq_f32(yf);
5399 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005400 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005401 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005402 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005403 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5404 N1, N2);
5405 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5406 // Because short has a smaller range than ushort, we can actually get away
5407 // with only a single newton step. This requires that we use a weird bias
5408 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005409 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005410 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5411 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005412 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005413 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5414 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5415 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5416 // Convert back to integer and return.
5417 // return vmovn_s32(vcvt_s32_f32(result));
5418 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5419 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5420 return N0;
5421}
5422
5423static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5424 EVT VT = Op.getValueType();
5425 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5426 "unexpected type for custom-lowering ISD::SDIV");
5427
5428 DebugLoc dl = Op.getDebugLoc();
5429 SDValue N0 = Op.getOperand(0);
5430 SDValue N1 = Op.getOperand(1);
5431 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005432
Nate Begeman7973f352011-02-11 20:53:29 +00005433 if (VT == MVT::v8i8) {
5434 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5435 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005436
Nate Begeman7973f352011-02-11 20:53:29 +00005437 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5438 DAG.getIntPtrConstant(4));
5439 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005440 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005441 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5442 DAG.getIntPtrConstant(0));
5443 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5444 DAG.getIntPtrConstant(0));
5445
5446 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5447 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5448
5449 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5450 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005451
Nate Begeman7973f352011-02-11 20:53:29 +00005452 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5453 return N0;
5454 }
5455 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5456}
5457
5458static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5459 EVT VT = Op.getValueType();
5460 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5461 "unexpected type for custom-lowering ISD::UDIV");
5462
5463 DebugLoc dl = Op.getDebugLoc();
5464 SDValue N0 = Op.getOperand(0);
5465 SDValue N1 = Op.getOperand(1);
5466 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005467
Nate Begeman7973f352011-02-11 20:53:29 +00005468 if (VT == MVT::v8i8) {
5469 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5470 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005471
Nate Begeman7973f352011-02-11 20:53:29 +00005472 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5473 DAG.getIntPtrConstant(4));
5474 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005475 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005476 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5477 DAG.getIntPtrConstant(0));
5478 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5479 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005480
Nate Begeman7973f352011-02-11 20:53:29 +00005481 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5482 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005483
Nate Begeman7973f352011-02-11 20:53:29 +00005484 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5485 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005486
5487 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005488 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5489 N0);
5490 return N0;
5491 }
Owen Anderson76706012011-04-05 21:48:57 +00005492
Nate Begeman7973f352011-02-11 20:53:29 +00005493 // v4i16 sdiv ... Convert to float.
5494 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5495 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5496 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5497 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5498 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005499 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005500
5501 // Use reciprocal estimate and two refinement steps.
5502 // float4 recip = vrecpeq_f32(yf);
5503 // recip *= vrecpsq_f32(yf, recip);
5504 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005505 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005506 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005507 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005508 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005509 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005510 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005511 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005512 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005513 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005514 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5515 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5516 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5517 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005518 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005519 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5520 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5521 N1 = DAG.getConstant(2, MVT::i32);
5522 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5523 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5524 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5525 // Convert back to integer and return.
5526 // return vmovn_u32(vcvt_s32_f32(result));
5527 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5528 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5529 return N0;
5530}
5531
Evan Cheng342e3162011-08-30 01:34:54 +00005532static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5533 EVT VT = Op.getNode()->getValueType(0);
5534 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5535
5536 unsigned Opc;
5537 bool ExtraOp = false;
5538 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005539 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005540 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5541 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5542 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5543 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5544 }
5545
5546 if (!ExtraOp)
5547 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5548 Op.getOperand(1));
5549 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5550 Op.getOperand(1), Op.getOperand(2));
5551}
5552
Eli Friedman74bf18c2011-09-15 22:26:18 +00005553static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005554 // Monotonic load/store is legal for all targets
5555 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5556 return Op;
5557
5558 // Aquire/Release load/store is not legal for targets without a
5559 // dmb or equivalent available.
5560 return SDValue();
5561}
5562
5563
Eli Friedman2bdffe42011-08-31 00:31:29 +00005564static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005565ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5566 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005567 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005568 assert (Node->getValueType(0) == MVT::i64 &&
5569 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005570
Eli Friedman4d3f3292011-08-31 17:52:22 +00005571 SmallVector<SDValue, 6> Ops;
5572 Ops.push_back(Node->getOperand(0)); // Chain
5573 Ops.push_back(Node->getOperand(1)); // Ptr
5574 // Low part of Val1
5575 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5576 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5577 // High part of Val1
5578 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5579 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005580 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005581 // High part of Val1
5582 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5583 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5584 // High part of Val2
5585 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5586 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5587 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005588 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5589 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005590 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005591 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005592 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005593 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5594 Results.push_back(Result.getValue(2));
5595}
5596
Dan Gohmand858e902010-04-17 15:26:15 +00005597SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005598 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005599 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005600 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005601 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005602 case ISD::GlobalAddress:
5603 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5604 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005605 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005606 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005607 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5608 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005609 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005610 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +00005611 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005612 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005613 case ISD::SINT_TO_FP:
5614 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5615 case ISD::FP_TO_SINT:
5616 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005617 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005618 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005619 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005620 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005621 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005622 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005623 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5624 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005625 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005626 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005627 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005628 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005629 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005630 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005631 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005632 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengc8e70452012-12-04 22:41:50 +00005633 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005634 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005635 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005636 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005637 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005638 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005639 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005640 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005641 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005642 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005643 case ISD::SDIV: return LowerSDIV(Op, DAG);
5644 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005645 case ISD::ADDC:
5646 case ISD::ADDE:
5647 case ISD::SUBC:
5648 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005649 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005650 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005651 }
Evan Chenga8e29892007-01-19 07:51:42 +00005652}
5653
Duncan Sands1607f052008-12-01 11:39:25 +00005654/// ReplaceNodeResults - Replace the results of node with an illegal result
5655/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005656void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5657 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005658 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005659 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005660 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005661 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005662 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005663 case ISD::BITCAST:
5664 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005665 break;
Renato Golin5ad5f592013-03-19 08:15:38 +00005666 case ISD::SIGN_EXTEND:
5667 case ISD::ZERO_EXTEND:
5668 Res = ExpandVectorExtension(N, DAG);
5669 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005670 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005671 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005672 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005673 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005674 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005675 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005676 return;
5677 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005678 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005679 return;
5680 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005681 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005682 return;
5683 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005684 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005685 return;
5686 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005687 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005688 return;
5689 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005690 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005691 return;
5692 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005693 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005694 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005695 case ISD::ATOMIC_CMP_SWAP:
5696 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5697 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005698 case ISD::ATOMIC_LOAD_MIN:
5699 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5700 return;
5701 case ISD::ATOMIC_LOAD_UMIN:
5702 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5703 return;
5704 case ISD::ATOMIC_LOAD_MAX:
5705 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5706 return;
5707 case ISD::ATOMIC_LOAD_UMAX:
5708 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5709 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005710 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005711 if (Res.getNode())
5712 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005713}
Chris Lattner27a6c732007-11-24 07:07:01 +00005714
Evan Chenga8e29892007-01-19 07:51:42 +00005715//===----------------------------------------------------------------------===//
5716// ARM Scheduler Hooks
5717//===----------------------------------------------------------------------===//
5718
5719MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005720ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5721 MachineBasicBlock *BB,
5722 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005723 unsigned dest = MI->getOperand(0).getReg();
5724 unsigned ptr = MI->getOperand(1).getReg();
5725 unsigned oldval = MI->getOperand(2).getReg();
5726 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005727 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5728 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005729 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005730
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005731 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005732 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5733 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5734 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005735
5736 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005737 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5738 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5739 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005740 }
5741
Jim Grosbach5278eb82009-12-11 01:42:04 +00005742 unsigned ldrOpc, strOpc;
5743 switch (Size) {
5744 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005745 case 1:
5746 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005747 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005748 break;
5749 case 2:
5750 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5751 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5752 break;
5753 case 4:
5754 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5755 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5756 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005757 }
5758
5759 MachineFunction *MF = BB->getParent();
5760 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5761 MachineFunction::iterator It = BB;
5762 ++It; // insert the new blocks after the current block
5763
5764 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5765 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5766 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5767 MF->insert(It, loop1MBB);
5768 MF->insert(It, loop2MBB);
5769 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005770
5771 // Transfer the remainder of BB and its successor edges to exitMBB.
5772 exitMBB->splice(exitMBB->begin(), BB,
5773 llvm::next(MachineBasicBlock::iterator(MI)),
5774 BB->end());
5775 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005776
5777 // thisMBB:
5778 // ...
5779 // fallthrough --> loop1MBB
5780 BB->addSuccessor(loop1MBB);
5781
5782 // loop1MBB:
5783 // ldrex dest, [ptr]
5784 // cmp dest, oldval
5785 // bne exitMBB
5786 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005787 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5788 if (ldrOpc == ARM::t2LDREX)
5789 MIB.addImm(0);
5790 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005791 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005792 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005793 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5794 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005795 BB->addSuccessor(loop2MBB);
5796 BB->addSuccessor(exitMBB);
5797
5798 // loop2MBB:
5799 // strex scratch, newval, [ptr]
5800 // cmp scratch, #0
5801 // bne loop1MBB
5802 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005803 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5804 if (strOpc == ARM::t2STREX)
5805 MIB.addImm(0);
5806 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005807 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005808 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005809 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5810 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005811 BB->addSuccessor(loop1MBB);
5812 BB->addSuccessor(exitMBB);
5813
5814 // exitMBB:
5815 // ...
5816 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005817
Dan Gohman14152b42010-07-06 20:24:04 +00005818 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005819
Jim Grosbach5278eb82009-12-11 01:42:04 +00005820 return BB;
5821}
5822
5823MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005824ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5825 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005826 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5827 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5828
5829 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005830 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005831 MachineFunction::iterator It = BB;
5832 ++It;
5833
5834 unsigned dest = MI->getOperand(0).getReg();
5835 unsigned ptr = MI->getOperand(1).getReg();
5836 unsigned incr = MI->getOperand(2).getReg();
5837 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005838 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005839
5840 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5841 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005842 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5843 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005844 }
5845
Jim Grosbachc3c23542009-12-14 04:22:04 +00005846 unsigned ldrOpc, strOpc;
5847 switch (Size) {
5848 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005849 case 1:
5850 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005851 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005852 break;
5853 case 2:
5854 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5855 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5856 break;
5857 case 4:
5858 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5859 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5860 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005861 }
5862
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005863 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5864 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5865 MF->insert(It, loopMBB);
5866 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005867
5868 // Transfer the remainder of BB and its successor edges to exitMBB.
5869 exitMBB->splice(exitMBB->begin(), BB,
5870 llvm::next(MachineBasicBlock::iterator(MI)),
5871 BB->end());
5872 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005873
Craig Topper420761a2012-04-20 07:30:17 +00005874 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005875 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005876 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005877 unsigned scratch = MRI.createVirtualRegister(TRC);
5878 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005879
5880 // thisMBB:
5881 // ...
5882 // fallthrough --> loopMBB
5883 BB->addSuccessor(loopMBB);
5884
5885 // loopMBB:
5886 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005887 // <binop> scratch2, dest, incr
5888 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005889 // cmp scratch, #0
5890 // bne- loopMBB
5891 // fallthrough --> exitMBB
5892 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005893 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5894 if (ldrOpc == ARM::t2LDREX)
5895 MIB.addImm(0);
5896 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005897 if (BinOpcode) {
5898 // operand order needs to go the other way for NAND
5899 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5900 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5901 addReg(incr).addReg(dest)).addReg(0);
5902 else
5903 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5904 addReg(dest).addReg(incr)).addReg(0);
5905 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005906
Jim Grosbachb6aed502011-09-09 18:37:27 +00005907 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5908 if (strOpc == ARM::t2STREX)
5909 MIB.addImm(0);
5910 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005911 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005912 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005913 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5914 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005915
5916 BB->addSuccessor(loopMBB);
5917 BB->addSuccessor(exitMBB);
5918
5919 // exitMBB:
5920 // ...
5921 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005922
Dan Gohman14152b42010-07-06 20:24:04 +00005923 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005924
Jim Grosbachc3c23542009-12-14 04:22:04 +00005925 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005926}
5927
Jim Grosbachf7da8822011-04-26 19:44:18 +00005928MachineBasicBlock *
5929ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5930 MachineBasicBlock *BB,
5931 unsigned Size,
5932 bool signExtend,
5933 ARMCC::CondCodes Cond) const {
5934 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5935
5936 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5937 MachineFunction *MF = BB->getParent();
5938 MachineFunction::iterator It = BB;
5939 ++It;
5940
5941 unsigned dest = MI->getOperand(0).getReg();
5942 unsigned ptr = MI->getOperand(1).getReg();
5943 unsigned incr = MI->getOperand(2).getReg();
5944 unsigned oldval = dest;
5945 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005946 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005947
5948 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5949 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005950 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5951 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005952 }
5953
Jim Grosbachf7da8822011-04-26 19:44:18 +00005954 unsigned ldrOpc, strOpc, extendOpc;
5955 switch (Size) {
5956 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5957 case 1:
5958 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5959 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005960 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005961 break;
5962 case 2:
5963 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5964 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005965 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005966 break;
5967 case 4:
5968 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5969 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5970 extendOpc = 0;
5971 break;
5972 }
5973
5974 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5975 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5976 MF->insert(It, loopMBB);
5977 MF->insert(It, exitMBB);
5978
5979 // Transfer the remainder of BB and its successor edges to exitMBB.
5980 exitMBB->splice(exitMBB->begin(), BB,
5981 llvm::next(MachineBasicBlock::iterator(MI)),
5982 BB->end());
5983 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5984
Craig Topper420761a2012-04-20 07:30:17 +00005985 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005986 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005987 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005988 unsigned scratch = MRI.createVirtualRegister(TRC);
5989 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005990
5991 // thisMBB:
5992 // ...
5993 // fallthrough --> loopMBB
5994 BB->addSuccessor(loopMBB);
5995
5996 // loopMBB:
5997 // ldrex dest, ptr
5998 // (sign extend dest, if required)
5999 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00006000 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00006001 // strex scratch, scratch2, ptr
6002 // cmp scratch, #0
6003 // bne- loopMBB
6004 // fallthrough --> exitMBB
6005 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00006006 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
6007 if (ldrOpc == ARM::t2LDREX)
6008 MIB.addImm(0);
6009 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00006010
6011 // Sign extend the value, if necessary.
6012 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00006013 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00006014 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
6015 .addReg(dest)
6016 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00006017 }
6018
6019 // Build compare and cmov instructions.
6020 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6021 .addReg(oldval).addReg(incr));
6022 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00006023 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00006024
Jim Grosbachb6aed502011-09-09 18:37:27 +00006025 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
6026 if (strOpc == ARM::t2STREX)
6027 MIB.addImm(0);
6028 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00006029 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6030 .addReg(scratch).addImm(0));
6031 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6032 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6033
6034 BB->addSuccessor(loopMBB);
6035 BB->addSuccessor(exitMBB);
6036
6037 // exitMBB:
6038 // ...
6039 BB = exitMBB;
6040
6041 MI->eraseFromParent(); // The instruction is gone now.
6042
6043 return BB;
6044}
6045
Eli Friedman2bdffe42011-08-31 00:31:29 +00006046MachineBasicBlock *
6047ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
6048 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00006049 bool NeedsCarry, bool IsCmpxchg,
6050 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006051 // This also handles ATOMIC_SWAP, indicated by Op1==0.
6052 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6053
6054 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6055 MachineFunction *MF = BB->getParent();
6056 MachineFunction::iterator It = BB;
6057 ++It;
6058
6059 unsigned destlo = MI->getOperand(0).getReg();
6060 unsigned desthi = MI->getOperand(1).getReg();
6061 unsigned ptr = MI->getOperand(2).getReg();
6062 unsigned vallo = MI->getOperand(3).getReg();
6063 unsigned valhi = MI->getOperand(4).getReg();
6064 DebugLoc dl = MI->getDebugLoc();
6065 bool isThumb2 = Subtarget->isThumb2();
6066
6067 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6068 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00006069 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
6070 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
6071 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006072 }
6073
Eli Friedman2bdffe42011-08-31 00:31:29 +00006074 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00006075 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006076 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006077 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006078 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006079 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006080 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006081
Eli Friedman2bdffe42011-08-31 00:31:29 +00006082 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006083 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
6084 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006085 MF->insert(It, exitMBB);
6086
6087 // Transfer the remainder of BB and its successor edges to exitMBB.
6088 exitMBB->splice(exitMBB->begin(), BB,
6089 llvm::next(MachineBasicBlock::iterator(MI)),
6090 BB->end());
6091 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6092
Craig Topper420761a2012-04-20 07:30:17 +00006093 const TargetRegisterClass *TRC = isThumb2 ?
6094 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6095 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006096 unsigned storesuccess = MRI.createVirtualRegister(TRC);
6097
6098 // thisMBB:
6099 // ...
6100 // fallthrough --> loopMBB
6101 BB->addSuccessor(loopMBB);
6102
6103 // loopMBB:
6104 // ldrexd r2, r3, ptr
6105 // <binopa> r0, r2, incr
6106 // <binopb> r1, r3, incr
6107 // strexd storesuccess, r0, r1, ptr
6108 // cmp storesuccess, #0
6109 // bne- loopMBB
6110 // fallthrough --> exitMBB
Eli Friedman2bdffe42011-08-31 00:31:29 +00006111 BB = loopMBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006112
Eli Friedman2bdffe42011-08-31 00:31:29 +00006113 // Load
Tim Northover0adfded2013-01-29 09:06:13 +00006114 if (isThumb2) {
6115 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2LDREXD))
6116 .addReg(destlo, RegState::Define)
6117 .addReg(desthi, RegState::Define)
6118 .addReg(ptr));
6119 } else {
6120 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6121 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDREXD))
6122 .addReg(GPRPair0, RegState::Define).addReg(ptr));
6123 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
6124 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6125 .addReg(GPRPair0, 0, ARM::gsub_0);
6126 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6127 .addReg(GPRPair0, 0, ARM::gsub_1);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006128 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006129
Tim Northover0adfded2013-01-29 09:06:13 +00006130 unsigned StoreLo, StoreHi;
Eli Friedman4d3f3292011-08-31 17:52:22 +00006131 if (IsCmpxchg) {
6132 // Add early exit
6133 for (unsigned i = 0; i < 2; i++) {
6134 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6135 ARM::CMPrr))
6136 .addReg(i == 0 ? destlo : desthi)
6137 .addReg(i == 0 ? vallo : valhi));
6138 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6139 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6140 BB->addSuccessor(exitMBB);
6141 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6142 BB = (i == 0 ? contBB : cont2BB);
6143 }
6144
6145 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006146 StoreLo = MI->getOperand(5).getReg();
6147 StoreHi = MI->getOperand(6).getReg();
Eli Friedman4d3f3292011-08-31 17:52:22 +00006148 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006149 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006150 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6151 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00006152 .addReg(destlo).addReg(vallo))
6153 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006154 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6155 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006156 .addReg(desthi).addReg(valhi))
6157 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006158
Tim Northover0adfded2013-01-29 09:06:13 +00006159 StoreLo = tmpRegLo;
6160 StoreHi = tmpRegHi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006161 } else {
6162 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006163 StoreLo = vallo;
6164 StoreHi = valhi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006165 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00006166 if (IsMinMax) {
6167 // Compare and branch to exit block.
6168 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6169 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6170 BB->addSuccessor(exitMBB);
6171 BB->addSuccessor(contBB);
6172 BB = contBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006173 StoreLo = vallo;
6174 StoreHi = valhi;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006175 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006176
6177 // Store
Tim Northover0adfded2013-01-29 09:06:13 +00006178 if (isThumb2) {
6179 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2STREXD), storesuccess)
6180 .addReg(StoreLo).addReg(StoreHi).addReg(ptr));
6181 } else {
6182 // Marshal a pair...
6183 unsigned StorePair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6184 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6185 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6186 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6187 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6188 .addReg(UndefPair)
6189 .addReg(StoreLo)
6190 .addImm(ARM::gsub_0);
6191 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), StorePair)
6192 .addReg(r1)
6193 .addReg(StoreHi)
6194 .addImm(ARM::gsub_1);
6195
6196 // ...and store it
6197 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::STREXD), storesuccess)
6198 .addReg(StorePair).addReg(ptr));
6199 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006200 // Cmp+jump
6201 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6202 .addReg(storesuccess).addImm(0));
6203 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6204 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6205
6206 BB->addSuccessor(loopMBB);
6207 BB->addSuccessor(exitMBB);
6208
6209 // exitMBB:
6210 // ...
6211 BB = exitMBB;
6212
6213 MI->eraseFromParent(); // The instruction is gone now.
6214
6215 return BB;
6216}
6217
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006218/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6219/// registers the function context.
6220void ARMTargetLowering::
6221SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6222 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006223 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6224 DebugLoc dl = MI->getDebugLoc();
6225 MachineFunction *MF = MBB->getParent();
6226 MachineRegisterInfo *MRI = &MF->getRegInfo();
6227 MachineConstantPool *MCP = MF->getConstantPool();
6228 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6229 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006230
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006231 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006232 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006233
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006234 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006235 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006236 ARMConstantPoolValue *CPV =
6237 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6238 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6239
Craig Topper420761a2012-04-20 07:30:17 +00006240 const TargetRegisterClass *TRC = isThumb ?
6241 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6242 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006243
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006244 // Grab constant pool and fixed stack memory operands.
6245 MachineMemOperand *CPMMO =
6246 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6247 MachineMemOperand::MOLoad, 4, 4);
6248
6249 MachineMemOperand *FIMMOSt =
6250 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6251 MachineMemOperand::MOStore, 4, 4);
6252
6253 // Load the address of the dispatch MBB into the jump buffer.
6254 if (isThumb2) {
6255 // Incoming value: jbuf
6256 // ldr.n r5, LCPI1_1
6257 // orr r5, r5, #1
6258 // add r5, pc
6259 // str r5, [$jbuf, #+4] ; &jbuf[1]
6260 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6261 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6262 .addConstantPoolIndex(CPI)
6263 .addMemOperand(CPMMO));
6264 // Set the low bit because of thumb mode.
6265 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6266 AddDefaultCC(
6267 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6268 .addReg(NewVReg1, RegState::Kill)
6269 .addImm(0x01)));
6270 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6271 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6272 .addReg(NewVReg2, RegState::Kill)
6273 .addImm(PCLabelId);
6274 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6275 .addReg(NewVReg3, RegState::Kill)
6276 .addFrameIndex(FI)
6277 .addImm(36) // &jbuf[1] :: pc
6278 .addMemOperand(FIMMOSt));
6279 } else if (isThumb) {
6280 // Incoming value: jbuf
6281 // ldr.n r1, LCPI1_4
6282 // add r1, pc
6283 // mov r2, #1
6284 // orrs r1, r2
6285 // add r2, $jbuf, #+4 ; &jbuf[1]
6286 // str r1, [r2]
6287 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6288 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6289 .addConstantPoolIndex(CPI)
6290 .addMemOperand(CPMMO));
6291 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6292 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6293 .addReg(NewVReg1, RegState::Kill)
6294 .addImm(PCLabelId);
6295 // Set the low bit because of thumb mode.
6296 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6297 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6298 .addReg(ARM::CPSR, RegState::Define)
6299 .addImm(1));
6300 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6301 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6302 .addReg(ARM::CPSR, RegState::Define)
6303 .addReg(NewVReg2, RegState::Kill)
6304 .addReg(NewVReg3, RegState::Kill));
6305 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6306 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6307 .addFrameIndex(FI)
6308 .addImm(36)); // &jbuf[1] :: pc
6309 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6310 .addReg(NewVReg4, RegState::Kill)
6311 .addReg(NewVReg5, RegState::Kill)
6312 .addImm(0)
6313 .addMemOperand(FIMMOSt));
6314 } else {
6315 // Incoming value: jbuf
6316 // ldr r1, LCPI1_1
6317 // add r1, pc, r1
6318 // str r1, [$jbuf, #+4] ; &jbuf[1]
6319 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6320 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6321 .addConstantPoolIndex(CPI)
6322 .addImm(0)
6323 .addMemOperand(CPMMO));
6324 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6325 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6326 .addReg(NewVReg1, RegState::Kill)
6327 .addImm(PCLabelId));
6328 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6329 .addReg(NewVReg2, RegState::Kill)
6330 .addFrameIndex(FI)
6331 .addImm(36) // &jbuf[1] :: pc
6332 .addMemOperand(FIMMOSt));
6333 }
6334}
6335
6336MachineBasicBlock *ARMTargetLowering::
6337EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6338 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6339 DebugLoc dl = MI->getDebugLoc();
6340 MachineFunction *MF = MBB->getParent();
6341 MachineRegisterInfo *MRI = &MF->getRegInfo();
6342 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6343 MachineFrameInfo *MFI = MF->getFrameInfo();
6344 int FI = MFI->getFunctionContextIndex();
6345
Craig Topper420761a2012-04-20 07:30:17 +00006346 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6347 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006348 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006349
Bill Wendling04f15b42011-10-06 21:29:56 +00006350 // Get a mapping of the call site numbers to all of the landing pads they're
6351 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006352 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6353 unsigned MaxCSNum = 0;
6354 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006355 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6356 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006357 if (!BB->isLandingPad()) continue;
6358
6359 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6360 // pad.
6361 for (MachineBasicBlock::iterator
6362 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6363 if (!II->isEHLabel()) continue;
6364
6365 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006366 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006367
Bill Wendling5cbef192011-10-05 23:28:57 +00006368 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6369 for (SmallVectorImpl<unsigned>::iterator
6370 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6371 CSI != CSE; ++CSI) {
6372 CallSiteNumToLPad[*CSI].push_back(BB);
6373 MaxCSNum = std::max(MaxCSNum, *CSI);
6374 }
Bill Wendling2a850152011-10-05 00:02:33 +00006375 break;
6376 }
6377 }
6378
6379 // Get an ordered list of the machine basic blocks for the jump table.
6380 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006381 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006382 LPadList.reserve(CallSiteNumToLPad.size());
6383 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6384 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6385 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006386 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006387 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006388 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6389 }
Bill Wendling2a850152011-10-05 00:02:33 +00006390 }
6391
Bill Wendling5cbef192011-10-05 23:28:57 +00006392 assert(!LPadList.empty() &&
6393 "No landing pad destinations for the dispatch jump table!");
6394
Bill Wendling04f15b42011-10-06 21:29:56 +00006395 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006396 MachineJumpTableInfo *JTI =
6397 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6398 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6399 unsigned UId = AFI->createJumpTableUId();
Chad Rosierb8f307b2013-03-01 18:30:38 +00006400 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling2a850152011-10-05 00:02:33 +00006401
Bill Wendling04f15b42011-10-06 21:29:56 +00006402 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006403
6404 // Shove the dispatch's address into the return slot in the function context.
6405 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6406 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006407
Bill Wendlingbb734682011-10-05 00:39:32 +00006408 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky0f156af2013-01-30 16:30:19 +00006409 unsigned trap_opcode;
Chad Rosier279706e2013-02-28 18:54:27 +00006410 if (Subtarget->isThumb())
Eli Bendersky0f156af2013-01-30 16:30:19 +00006411 trap_opcode = ARM::tTRAP;
Chad Rosier279706e2013-02-28 18:54:27 +00006412 else
6413 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6414
Eli Bendersky0f156af2013-01-30 16:30:19 +00006415 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendlingbb734682011-10-05 00:39:32 +00006416 DispatchBB->addSuccessor(TrapBB);
6417
6418 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6419 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006420
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006421 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006422 MF->insert(MF->end(), DispatchBB);
6423 MF->insert(MF->end(), DispContBB);
6424 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006425
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006426 // Insert code into the entry block that creates and registers the function
6427 // context.
6428 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6429
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006430 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006431 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006432 MachineMemOperand::MOLoad |
6433 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006434
Chad Rosiere7bd5192012-11-06 23:05:24 +00006435 MachineInstrBuilder MIB;
6436 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6437
6438 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6439 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6440
6441 // Add a register mask with no preserved registers. This results in all
6442 // registers being marked as clobbered.
6443 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006444
Bill Wendling952cb502011-10-18 22:49:07 +00006445 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006446 if (Subtarget->isThumb2()) {
6447 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6448 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6449 .addFrameIndex(FI)
6450 .addImm(4)
6451 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006452
Bill Wendling952cb502011-10-18 22:49:07 +00006453 if (NumLPads < 256) {
6454 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6455 .addReg(NewVReg1)
6456 .addImm(LPadList.size()));
6457 } else {
6458 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6459 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006460 .addImm(NumLPads & 0xFFFF));
6461
6462 unsigned VReg2 = VReg1;
6463 if ((NumLPads & 0xFFFF0000) != 0) {
6464 VReg2 = MRI->createVirtualRegister(TRC);
6465 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6466 .addReg(VReg1)
6467 .addImm(NumLPads >> 16));
6468 }
6469
Bill Wendling952cb502011-10-18 22:49:07 +00006470 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6471 .addReg(NewVReg1)
6472 .addReg(VReg2));
6473 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006474
Bill Wendling95ce2e92011-10-06 22:53:00 +00006475 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6476 .addMBB(TrapBB)
6477 .addImm(ARMCC::HI)
6478 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006479
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006480 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6481 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006482 .addJumpTableIndex(MJTI)
6483 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006484
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006485 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006486 AddDefaultCC(
6487 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006488 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6489 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006490 .addReg(NewVReg1)
6491 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6492
6493 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006494 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006495 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006496 .addJumpTableIndex(MJTI)
6497 .addImm(UId);
6498 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006499 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6500 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6501 .addFrameIndex(FI)
6502 .addImm(1)
6503 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006504
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006505 if (NumLPads < 256) {
6506 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6507 .addReg(NewVReg1)
6508 .addImm(NumLPads));
6509 } else {
6510 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006511 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6512 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6513
6514 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006515 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006516 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006517 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006518 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006519
6520 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6521 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6522 .addReg(VReg1, RegState::Define)
6523 .addConstantPoolIndex(Idx));
6524 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6525 .addReg(NewVReg1)
6526 .addReg(VReg1));
6527 }
6528
Bill Wendling083a8eb2011-10-06 23:37:36 +00006529 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6530 .addMBB(TrapBB)
6531 .addImm(ARMCC::HI)
6532 .addReg(ARM::CPSR);
6533
6534 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6535 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6536 .addReg(ARM::CPSR, RegState::Define)
6537 .addReg(NewVReg1)
6538 .addImm(2));
6539
6540 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006541 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006542 .addJumpTableIndex(MJTI)
6543 .addImm(UId));
6544
6545 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6546 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6547 .addReg(ARM::CPSR, RegState::Define)
6548 .addReg(NewVReg2, RegState::Kill)
6549 .addReg(NewVReg3));
6550
6551 MachineMemOperand *JTMMOLd =
6552 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6553 MachineMemOperand::MOLoad, 4, 4);
6554
6555 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6556 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6557 .addReg(NewVReg4, RegState::Kill)
6558 .addImm(0)
6559 .addMemOperand(JTMMOLd));
6560
Chad Rosierb8f307b2013-03-01 18:30:38 +00006561 unsigned NewVReg6 = NewVReg5;
6562 if (RelocM == Reloc::PIC_) {
6563 NewVReg6 = MRI->createVirtualRegister(TRC);
6564 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6565 .addReg(ARM::CPSR, RegState::Define)
6566 .addReg(NewVReg5, RegState::Kill)
6567 .addReg(NewVReg3));
6568 }
Bill Wendling083a8eb2011-10-06 23:37:36 +00006569
6570 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6571 .addReg(NewVReg6, RegState::Kill)
6572 .addJumpTableIndex(MJTI)
6573 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006574 } else {
6575 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6576 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6577 .addFrameIndex(FI)
6578 .addImm(4)
6579 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006580
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006581 if (NumLPads < 256) {
6582 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6583 .addReg(NewVReg1)
6584 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006585 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006586 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6587 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006588 .addImm(NumLPads & 0xFFFF));
6589
6590 unsigned VReg2 = VReg1;
6591 if ((NumLPads & 0xFFFF0000) != 0) {
6592 VReg2 = MRI->createVirtualRegister(TRC);
6593 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6594 .addReg(VReg1)
6595 .addImm(NumLPads >> 16));
6596 }
6597
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006598 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6599 .addReg(NewVReg1)
6600 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006601 } else {
6602 MachineConstantPool *ConstantPool = MF->getConstantPool();
6603 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6604 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6605
6606 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006607 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006608 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006609 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006610 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6611
6612 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6613 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6614 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006615 .addConstantPoolIndex(Idx)
6616 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006617 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6618 .addReg(NewVReg1)
6619 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006620 }
6621
Bill Wendling95ce2e92011-10-06 22:53:00 +00006622 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6623 .addMBB(TrapBB)
6624 .addImm(ARMCC::HI)
6625 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006626
Bill Wendling564392b2011-10-18 22:11:18 +00006627 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006628 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006629 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006630 .addReg(NewVReg1)
6631 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006632 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6633 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006634 .addJumpTableIndex(MJTI)
6635 .addImm(UId));
6636
6637 MachineMemOperand *JTMMOLd =
6638 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6639 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006640 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006641 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006642 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6643 .addReg(NewVReg3, RegState::Kill)
6644 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006645 .addImm(0)
6646 .addMemOperand(JTMMOLd));
6647
Chad Rosierb8f307b2013-03-01 18:30:38 +00006648 if (RelocM == Reloc::PIC_) {
6649 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6650 .addReg(NewVReg5, RegState::Kill)
6651 .addReg(NewVReg4)
6652 .addJumpTableIndex(MJTI)
6653 .addImm(UId);
6654 } else {
6655 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6656 .addReg(NewVReg5, RegState::Kill)
6657 .addJumpTableIndex(MJTI)
6658 .addImm(UId);
6659 }
Bill Wendling95ce2e92011-10-06 22:53:00 +00006660 }
Bill Wendling2a850152011-10-05 00:02:33 +00006661
Bill Wendlingbb734682011-10-05 00:39:32 +00006662 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006663 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006664 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006665 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6666 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006667 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006668 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006669 }
6670
Bill Wendling24bb9252011-10-17 05:25:09 +00006671 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006672 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006673 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006674 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6675 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6676 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006677
6678 // Remove the landing pad successor from the invoke block and replace it
6679 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006680 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6681 BB->succ_end());
6682 while (!Successors.empty()) {
6683 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006684 if (SMBB->isLandingPad()) {
6685 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006686 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006687 }
6688 }
6689
6690 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006691
6692 // Find the invoke call and mark all of the callee-saved registers as
6693 // 'implicit defined' so that they're spilled. This prevents code from
6694 // moving instructions to before the EH block, where they will never be
6695 // executed.
6696 for (MachineBasicBlock::reverse_iterator
6697 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006698 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006699
6700 DenseMap<unsigned, bool> DefRegs;
6701 for (MachineInstr::mop_iterator
6702 OI = II->operands_begin(), OE = II->operands_end();
6703 OI != OE; ++OI) {
6704 if (!OI->isReg()) continue;
6705 DefRegs[OI->getReg()] = true;
6706 }
6707
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00006708 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006709
Bill Wendling5d798592011-10-14 23:55:44 +00006710 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006711 unsigned Reg = SavedRegs[i];
6712 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006713 !ARM::tGPRRegClass.contains(Reg) &&
6714 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006715 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006716 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006717 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006718 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006719 continue;
6720 if (!DefRegs[Reg])
6721 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006722 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006723
6724 break;
6725 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006726 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006727
Bill Wendlingf7b02072011-10-18 18:30:49 +00006728 // Mark all former landing pads as non-landing pads. The dispatch is the only
6729 // landing pad now.
6730 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6731 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6732 (*I)->setIsLandingPad(false);
6733
Bill Wendlingbb734682011-10-05 00:39:32 +00006734 // The instruction is gone now.
6735 MI->eraseFromParent();
6736
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006737 return MBB;
6738}
6739
Evan Cheng218977b2010-07-13 19:27:42 +00006740static
6741MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6742 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6743 E = MBB->succ_end(); I != E; ++I)
6744 if (*I != Succ)
6745 return *I;
6746 llvm_unreachable("Expecting a BB with two successors!");
6747}
6748
Manman Ren68f25572012-06-01 19:33:18 +00006749MachineBasicBlock *ARMTargetLowering::
6750EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6751 // This pseudo instruction has 3 operands: dst, src, size
6752 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6753 // Otherwise, we will generate unrolled scalar copies.
6754 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6755 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6756 MachineFunction::iterator It = BB;
6757 ++It;
6758
6759 unsigned dest = MI->getOperand(0).getReg();
6760 unsigned src = MI->getOperand(1).getReg();
6761 unsigned SizeVal = MI->getOperand(2).getImm();
6762 unsigned Align = MI->getOperand(3).getImm();
6763 DebugLoc dl = MI->getDebugLoc();
6764
6765 bool isThumb2 = Subtarget->isThumb2();
6766 MachineFunction *MF = BB->getParent();
6767 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006768 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006769
6770 const TargetRegisterClass *TRC = isThumb2 ?
6771 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6772 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006773 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006774
6775 if (Align & 1) {
6776 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6777 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6778 UnitSize = 1;
6779 } else if (Align & 2) {
6780 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6781 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6782 UnitSize = 2;
6783 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006784 // Check whether we can use NEON instructions.
Bill Wendling831737d2012-12-30 10:32:01 +00006785 if (!MF->getFunction()->getAttributes().
6786 hasAttribute(AttributeSet::FunctionIndex,
6787 Attribute::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006788 Subtarget->hasNEON()) {
6789 if ((Align % 16 == 0) && SizeVal >= 16) {
6790 ldrOpc = ARM::VLD1q32wb_fixed;
6791 strOpc = ARM::VST1q32wb_fixed;
6792 UnitSize = 16;
6793 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6794 }
6795 else if ((Align % 8 == 0) && SizeVal >= 8) {
6796 ldrOpc = ARM::VLD1d32wb_fixed;
6797 strOpc = ARM::VST1d32wb_fixed;
6798 UnitSize = 8;
6799 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6800 }
6801 }
6802 // Can't use NEON instructions.
6803 if (UnitSize == 0) {
6804 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6805 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6806 UnitSize = 4;
6807 }
Manman Ren68f25572012-06-01 19:33:18 +00006808 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006809
Manman Ren68f25572012-06-01 19:33:18 +00006810 unsigned BytesLeft = SizeVal % UnitSize;
6811 unsigned LoopSize = SizeVal - BytesLeft;
6812
6813 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6814 // Use LDR and STR to copy.
6815 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6816 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6817 unsigned srcIn = src;
6818 unsigned destIn = dest;
6819 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006820 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006821 unsigned srcOut = MRI.createVirtualRegister(TRC);
6822 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006823 if (UnitSize >= 8) {
6824 AddDefaultPred(BuildMI(*BB, MI, dl,
6825 TII->get(ldrOpc), scratch)
6826 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6827
6828 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6829 .addReg(destIn).addImm(0).addReg(scratch));
6830 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006831 AddDefaultPred(BuildMI(*BB, MI, dl,
6832 TII->get(ldrOpc), scratch)
6833 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6834
6835 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6836 .addReg(scratch).addReg(destIn)
6837 .addImm(UnitSize));
6838 } else {
6839 AddDefaultPred(BuildMI(*BB, MI, dl,
6840 TII->get(ldrOpc), scratch)
6841 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6842 .addImm(UnitSize));
6843
6844 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6845 .addReg(scratch).addReg(destIn)
6846 .addReg(0).addImm(UnitSize));
6847 }
6848 srcIn = srcOut;
6849 destIn = destOut;
6850 }
6851
6852 // Handle the leftover bytes with LDRB and STRB.
6853 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6854 // [destOut] = STRB_POST(scratch, destIn, 1)
6855 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6856 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6857 for (unsigned i = 0; i < BytesLeft; i++) {
6858 unsigned scratch = MRI.createVirtualRegister(TRC);
6859 unsigned srcOut = MRI.createVirtualRegister(TRC);
6860 unsigned destOut = MRI.createVirtualRegister(TRC);
6861 if (isThumb2) {
6862 AddDefaultPred(BuildMI(*BB, MI, dl,
6863 TII->get(ldrOpc),scratch)
6864 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6865
6866 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6867 .addReg(scratch).addReg(destIn)
6868 .addReg(0).addImm(1));
6869 } else {
6870 AddDefaultPred(BuildMI(*BB, MI, dl,
6871 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006872 .addReg(srcOut, RegState::Define).addReg(srcIn)
6873 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006874
6875 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6876 .addReg(scratch).addReg(destIn)
6877 .addReg(0).addImm(1));
6878 }
6879 srcIn = srcOut;
6880 destIn = destOut;
6881 }
6882 MI->eraseFromParent(); // The instruction is gone now.
6883 return BB;
6884 }
6885
6886 // Expand the pseudo op to a loop.
6887 // thisMBB:
6888 // ...
6889 // movw varEnd, # --> with thumb2
6890 // movt varEnd, #
6891 // ldrcp varEnd, idx --> without thumb2
6892 // fallthrough --> loopMBB
6893 // loopMBB:
6894 // PHI varPhi, varEnd, varLoop
6895 // PHI srcPhi, src, srcLoop
6896 // PHI destPhi, dst, destLoop
6897 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6898 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6899 // subs varLoop, varPhi, #UnitSize
6900 // bne loopMBB
6901 // fallthrough --> exitMBB
6902 // exitMBB:
6903 // epilogue to handle left-over bytes
6904 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6905 // [destOut] = STRB_POST(scratch, destLoop, 1)
6906 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6907 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6908 MF->insert(It, loopMBB);
6909 MF->insert(It, exitMBB);
6910
6911 // Transfer the remainder of BB and its successor edges to exitMBB.
6912 exitMBB->splice(exitMBB->begin(), BB,
6913 llvm::next(MachineBasicBlock::iterator(MI)),
6914 BB->end());
6915 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6916
6917 // Load an immediate to varEnd.
6918 unsigned varEnd = MRI.createVirtualRegister(TRC);
6919 if (isThumb2) {
6920 unsigned VReg1 = varEnd;
6921 if ((LoopSize & 0xFFFF0000) != 0)
6922 VReg1 = MRI.createVirtualRegister(TRC);
6923 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6924 .addImm(LoopSize & 0xFFFF));
6925
6926 if ((LoopSize & 0xFFFF0000) != 0)
6927 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6928 .addReg(VReg1)
6929 .addImm(LoopSize >> 16));
6930 } else {
6931 MachineConstantPool *ConstantPool = MF->getConstantPool();
6932 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6933 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6934
6935 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006936 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006937 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006938 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006939 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6940
6941 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6942 .addReg(varEnd, RegState::Define)
6943 .addConstantPoolIndex(Idx)
6944 .addImm(0));
6945 }
6946 BB->addSuccessor(loopMBB);
6947
6948 // Generate the loop body:
6949 // varPhi = PHI(varLoop, varEnd)
6950 // srcPhi = PHI(srcLoop, src)
6951 // destPhi = PHI(destLoop, dst)
6952 MachineBasicBlock *entryBB = BB;
6953 BB = loopMBB;
6954 unsigned varLoop = MRI.createVirtualRegister(TRC);
6955 unsigned varPhi = MRI.createVirtualRegister(TRC);
6956 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6957 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6958 unsigned destLoop = MRI.createVirtualRegister(TRC);
6959 unsigned destPhi = MRI.createVirtualRegister(TRC);
6960
6961 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6962 .addReg(varLoop).addMBB(loopMBB)
6963 .addReg(varEnd).addMBB(entryBB);
6964 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6965 .addReg(srcLoop).addMBB(loopMBB)
6966 .addReg(src).addMBB(entryBB);
6967 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6968 .addReg(destLoop).addMBB(loopMBB)
6969 .addReg(dest).addMBB(entryBB);
6970
6971 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6972 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006973 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6974 if (UnitSize >= 8) {
6975 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6976 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6977
6978 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6979 .addReg(destPhi).addImm(0).addReg(scratch));
6980 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006981 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6982 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6983
6984 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6985 .addReg(scratch).addReg(destPhi)
6986 .addImm(UnitSize));
6987 } else {
6988 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6989 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6990 .addImm(UnitSize));
6991
6992 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6993 .addReg(scratch).addReg(destPhi)
6994 .addReg(0).addImm(UnitSize));
6995 }
6996
6997 // Decrement loop variable by UnitSize.
6998 MachineInstrBuilder MIB = BuildMI(BB, dl,
6999 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7000 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7001 MIB->getOperand(5).setReg(ARM::CPSR);
7002 MIB->getOperand(5).setIsDef(true);
7003
7004 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7005 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
7006
7007 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7008 BB->addSuccessor(loopMBB);
7009 BB->addSuccessor(exitMBB);
7010
7011 // Add epilogue to handle BytesLeft.
7012 BB = exitMBB;
7013 MachineInstr *StartOfExit = exitMBB->begin();
7014 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
7015 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
7016
7017 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7018 // [destOut] = STRB_POST(scratch, destLoop, 1)
7019 unsigned srcIn = srcLoop;
7020 unsigned destIn = destLoop;
7021 for (unsigned i = 0; i < BytesLeft; i++) {
7022 unsigned scratch = MRI.createVirtualRegister(TRC);
7023 unsigned srcOut = MRI.createVirtualRegister(TRC);
7024 unsigned destOut = MRI.createVirtualRegister(TRC);
7025 if (isThumb2) {
7026 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
7027 TII->get(ldrOpc),scratch)
7028 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
7029
7030 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
7031 .addReg(scratch).addReg(destIn)
7032 .addImm(1));
7033 } else {
7034 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
7035 TII->get(ldrOpc),scratch)
7036 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
7037
7038 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
7039 .addReg(scratch).addReg(destIn)
7040 .addReg(0).addImm(1));
7041 }
7042 srcIn = srcOut;
7043 destIn = destOut;
7044 }
7045
7046 MI->eraseFromParent(); // The instruction is gone now.
7047 return BB;
7048}
7049
Jim Grosbache801dc42009-12-12 01:40:06 +00007050MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007051ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00007052 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007053 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00007054 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007055 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00007056 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00007057 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00007058 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00007059 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00007060 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00007061 // The Thumb2 pre-indexed stores have the same MI operands, they just
7062 // define them differently in the .td files from the isel patterns, so
7063 // they need pseudos.
7064 case ARM::t2STR_preidx:
7065 MI->setDesc(TII->get(ARM::t2STR_PRE));
7066 return BB;
7067 case ARM::t2STRB_preidx:
7068 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7069 return BB;
7070 case ARM::t2STRH_preidx:
7071 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7072 return BB;
7073
Jim Grosbach19dec202011-08-05 20:35:44 +00007074 case ARM::STRi_preidx:
7075 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00007076 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00007077 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7078 // Decode the offset.
7079 unsigned Offset = MI->getOperand(4).getImm();
7080 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7081 Offset = ARM_AM::getAM2Offset(Offset);
7082 if (isSub)
7083 Offset = -Offset;
7084
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007085 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00007086 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00007087 .addOperand(MI->getOperand(0)) // Rn_wb
7088 .addOperand(MI->getOperand(1)) // Rt
7089 .addOperand(MI->getOperand(2)) // Rn
7090 .addImm(Offset) // offset (skip GPR==zero_reg)
7091 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007092 .addOperand(MI->getOperand(6))
7093 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00007094 MI->eraseFromParent();
7095 return BB;
7096 }
7097 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00007098 case ARM::STRBr_preidx:
7099 case ARM::STRH_preidx: {
7100 unsigned NewOpc;
7101 switch (MI->getOpcode()) {
7102 default: llvm_unreachable("unexpected opcode!");
7103 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7104 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7105 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7106 }
Jim Grosbach19dec202011-08-05 20:35:44 +00007107 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7108 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7109 MIB.addOperand(MI->getOperand(i));
7110 MI->eraseFromParent();
7111 return BB;
7112 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007113 case ARM::ATOMIC_LOAD_ADD_I8:
7114 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7115 case ARM::ATOMIC_LOAD_ADD_I16:
7116 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7117 case ARM::ATOMIC_LOAD_ADD_I32:
7118 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007119
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007120 case ARM::ATOMIC_LOAD_AND_I8:
7121 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7122 case ARM::ATOMIC_LOAD_AND_I16:
7123 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7124 case ARM::ATOMIC_LOAD_AND_I32:
7125 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007126
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007127 case ARM::ATOMIC_LOAD_OR_I8:
7128 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7129 case ARM::ATOMIC_LOAD_OR_I16:
7130 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7131 case ARM::ATOMIC_LOAD_OR_I32:
7132 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007133
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007134 case ARM::ATOMIC_LOAD_XOR_I8:
7135 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7136 case ARM::ATOMIC_LOAD_XOR_I16:
7137 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7138 case ARM::ATOMIC_LOAD_XOR_I32:
7139 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007140
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007141 case ARM::ATOMIC_LOAD_NAND_I8:
7142 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7143 case ARM::ATOMIC_LOAD_NAND_I16:
7144 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7145 case ARM::ATOMIC_LOAD_NAND_I32:
7146 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007147
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007148 case ARM::ATOMIC_LOAD_SUB_I8:
7149 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7150 case ARM::ATOMIC_LOAD_SUB_I16:
7151 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7152 case ARM::ATOMIC_LOAD_SUB_I32:
7153 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007154
Jim Grosbachf7da8822011-04-26 19:44:18 +00007155 case ARM::ATOMIC_LOAD_MIN_I8:
7156 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7157 case ARM::ATOMIC_LOAD_MIN_I16:
7158 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7159 case ARM::ATOMIC_LOAD_MIN_I32:
7160 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7161
7162 case ARM::ATOMIC_LOAD_MAX_I8:
7163 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7164 case ARM::ATOMIC_LOAD_MAX_I16:
7165 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7166 case ARM::ATOMIC_LOAD_MAX_I32:
7167 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7168
7169 case ARM::ATOMIC_LOAD_UMIN_I8:
7170 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7171 case ARM::ATOMIC_LOAD_UMIN_I16:
7172 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7173 case ARM::ATOMIC_LOAD_UMIN_I32:
7174 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7175
7176 case ARM::ATOMIC_LOAD_UMAX_I8:
7177 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7178 case ARM::ATOMIC_LOAD_UMAX_I16:
7179 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7180 case ARM::ATOMIC_LOAD_UMAX_I32:
7181 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7182
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007183 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7184 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7185 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00007186
7187 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7188 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7189 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007190
Eli Friedman2bdffe42011-08-31 00:31:29 +00007191
7192 case ARM::ATOMADD6432:
7193 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007194 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7195 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007196 case ARM::ATOMSUB6432:
7197 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007198 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7199 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007200 case ARM::ATOMOR6432:
7201 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007202 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007203 case ARM::ATOMXOR6432:
7204 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007205 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007206 case ARM::ATOMAND6432:
7207 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007208 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007209 case ARM::ATOMSWAP6432:
7210 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00007211 case ARM::ATOMCMPXCHG6432:
7212 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7213 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7214 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007215 case ARM::ATOMMIN6432:
7216 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7217 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7218 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007219 /*IsMinMax*/ true, ARMCC::LT);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007220 case ARM::ATOMMAX6432:
7221 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7222 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7223 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7224 /*IsMinMax*/ true, ARMCC::GE);
7225 case ARM::ATOMUMIN6432:
7226 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7227 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7228 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007229 /*IsMinMax*/ true, ARMCC::LO);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007230 case ARM::ATOMUMAX6432:
7231 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7232 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7233 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7234 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007235
Evan Cheng007ea272009-08-12 05:17:19 +00007236 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00007237 // To "insert" a SELECT_CC instruction, we actually have to insert the
7238 // diamond control-flow pattern. The incoming instruction knows the
7239 // destination vreg to set, the condition code register to branch on, the
7240 // true/false values to select between, and a branch opcode to use.
7241 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007242 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00007243 ++It;
7244
7245 // thisMBB:
7246 // ...
7247 // TrueVal = ...
7248 // cmpTY ccX, r1, r2
7249 // bCC copy1MBB
7250 // fallthrough --> copy0MBB
7251 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007252 MachineFunction *F = BB->getParent();
7253 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7254 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00007255 F->insert(It, copy0MBB);
7256 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00007257
7258 // Transfer the remainder of BB and its successor edges to sinkMBB.
7259 sinkMBB->splice(sinkMBB->begin(), BB,
7260 llvm::next(MachineBasicBlock::iterator(MI)),
7261 BB->end());
7262 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7263
Dan Gohman258c58c2010-07-06 15:49:48 +00007264 BB->addSuccessor(copy0MBB);
7265 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00007266
Dan Gohman14152b42010-07-06 20:24:04 +00007267 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7268 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7269
Evan Chenga8e29892007-01-19 07:51:42 +00007270 // copy0MBB:
7271 // %FalseValue = ...
7272 // # fallthrough to sinkMBB
7273 BB = copy0MBB;
7274
7275 // Update machine-CFG edges
7276 BB->addSuccessor(sinkMBB);
7277
7278 // sinkMBB:
7279 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7280 // ...
7281 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007282 BuildMI(*BB, BB->begin(), dl,
7283 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007284 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7285 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7286
Dan Gohman14152b42010-07-06 20:24:04 +00007287 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007288 return BB;
7289 }
Evan Cheng86198642009-08-07 00:34:42 +00007290
Evan Cheng218977b2010-07-13 19:27:42 +00007291 case ARM::BCCi64:
7292 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007293 // If there is an unconditional branch to the other successor, remove it.
7294 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007295
Evan Cheng218977b2010-07-13 19:27:42 +00007296 // Compare both parts that make up the double comparison separately for
7297 // equality.
7298 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7299
7300 unsigned LHS1 = MI->getOperand(1).getReg();
7301 unsigned LHS2 = MI->getOperand(2).getReg();
7302 if (RHSisZero) {
7303 AddDefaultPred(BuildMI(BB, dl,
7304 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7305 .addReg(LHS1).addImm(0));
7306 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7307 .addReg(LHS2).addImm(0)
7308 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7309 } else {
7310 unsigned RHS1 = MI->getOperand(3).getReg();
7311 unsigned RHS2 = MI->getOperand(4).getReg();
7312 AddDefaultPred(BuildMI(BB, dl,
7313 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7314 .addReg(LHS1).addReg(RHS1));
7315 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7316 .addReg(LHS2).addReg(RHS2)
7317 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7318 }
7319
7320 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7321 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7322 if (MI->getOperand(0).getImm() == ARMCC::NE)
7323 std::swap(destMBB, exitMBB);
7324
7325 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7326 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007327 if (isThumb2)
7328 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7329 else
7330 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007331
7332 MI->eraseFromParent(); // The pseudo instruction is gone now.
7333 return BB;
7334 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007335
Bill Wendling5bc85282011-10-17 20:37:20 +00007336 case ARM::Int_eh_sjlj_setjmp:
7337 case ARM::Int_eh_sjlj_setjmp_nofp:
7338 case ARM::tInt_eh_sjlj_setjmp:
7339 case ARM::t2Int_eh_sjlj_setjmp:
7340 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7341 EmitSjLjDispatchBlock(MI, BB);
7342 return BB;
7343
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007344 case ARM::ABS:
7345 case ARM::t2ABS: {
7346 // To insert an ABS instruction, we have to insert the
7347 // diamond control-flow pattern. The incoming instruction knows the
7348 // source vreg to test against 0, the destination vreg to set,
7349 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007350 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007351 // It transforms
7352 // V1 = ABS V0
7353 // into
7354 // V2 = MOVS V0
7355 // BCC (branch to SinkBB if V0 >= 0)
7356 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007357 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007358 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7359 MachineFunction::iterator BBI = BB;
7360 ++BBI;
7361 MachineFunction *Fn = BB->getParent();
7362 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7363 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7364 Fn->insert(BBI, RSBBB);
7365 Fn->insert(BBI, SinkBB);
7366
7367 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7368 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7369 bool isThumb2 = Subtarget->isThumb2();
7370 MachineRegisterInfo &MRI = Fn->getRegInfo();
7371 // In Thumb mode S must not be specified if source register is the SP or
7372 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007373 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7374 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7375 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007376
7377 // Transfer the remainder of BB and its successor edges to sinkMBB.
7378 SinkBB->splice(SinkBB->begin(), BB,
7379 llvm::next(MachineBasicBlock::iterator(MI)),
7380 BB->end());
7381 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7382
7383 BB->addSuccessor(RSBBB);
7384 BB->addSuccessor(SinkBB);
7385
7386 // fall through to SinkMBB
7387 RSBBB->addSuccessor(SinkBB);
7388
Manman Ren307473d2012-06-15 21:32:12 +00007389 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007390 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007391 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7392 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007393
7394 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007395 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007396 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7397 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7398
7399 // insert rsbri in RSBBB
7400 // Note: BCC and rsbri will be converted into predicated rsbmi
7401 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007402 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007403 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007404 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007405 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7406
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007407 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007408 // reuse ABSDstReg to not change uses of ABS instruction
7409 BuildMI(*SinkBB, SinkBB->begin(), dl,
7410 TII->get(ARM::PHI), ABSDstReg)
7411 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007412 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007413
7414 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007415 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007416
7417 // return last added BB
7418 return SinkBB;
7419 }
Manman Ren68f25572012-06-01 19:33:18 +00007420 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007421 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007422 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007423 }
7424}
7425
Evan Cheng37fefc22011-08-30 19:09:48 +00007426void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7427 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007428 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007429 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7430 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7431 return;
7432 }
7433
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007434 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007435 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7436 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7437 // operand is still set to noreg. If needed, set the optional operand's
7438 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007439 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007440 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007441
Andrew Trick3be654f2011-09-21 02:20:46 +00007442 // Rename pseudo opcodes.
7443 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7444 if (NewOpc) {
7445 const ARMBaseInstrInfo *TII =
7446 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007447 MCID = &TII->get(NewOpc);
7448
7449 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7450 "converted opcode should be the same except for cc_out");
7451
7452 MI->setDesc(*MCID);
7453
7454 // Add the optional cc_out operand
7455 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007456 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007457 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007458
7459 // Any ARM instruction that sets the 's' bit should specify an optional
7460 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007461 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007462 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007463 return;
7464 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007465 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7466 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007467 bool definesCPSR = false;
7468 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007469 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007470 i != e; ++i) {
7471 const MachineOperand &MO = MI->getOperand(i);
7472 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7473 definesCPSR = true;
7474 if (MO.isDead())
7475 deadCPSR = true;
7476 MI->RemoveOperand(i);
7477 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007478 }
7479 }
Andrew Trick4815d562011-09-20 03:17:40 +00007480 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007481 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007482 return;
7483 }
7484 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007485 if (deadCPSR) {
7486 assert(!MI->getOperand(ccOutIdx).getReg() &&
7487 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007488 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007489 }
Andrew Trick4815d562011-09-20 03:17:40 +00007490
Andrew Trick3be654f2011-09-21 02:20:46 +00007491 // If this instruction was defined with an optional CPSR def and its dag node
7492 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007493 MachineOperand &MO = MI->getOperand(ccOutIdx);
7494 MO.setReg(ARM::CPSR);
7495 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007496}
7497
Evan Chenga8e29892007-01-19 07:51:42 +00007498//===----------------------------------------------------------------------===//
7499// ARM Optimization Hooks
7500//===----------------------------------------------------------------------===//
7501
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007502// Helper function that checks if N is a null or all ones constant.
7503static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7504 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7505 if (!C)
7506 return false;
7507 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7508}
7509
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007510// Return true if N is conditionally 0 or all ones.
7511// Detects these expressions where cc is an i1 value:
7512//
7513// (select cc 0, y) [AllOnes=0]
7514// (select cc y, 0) [AllOnes=0]
7515// (zext cc) [AllOnes=0]
7516// (sext cc) [AllOnes=0/1]
7517// (select cc -1, y) [AllOnes=1]
7518// (select cc y, -1) [AllOnes=1]
7519//
7520// Invert is set when N is the null/all ones constant when CC is false.
7521// OtherOp is set to the alternative value of N.
7522static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7523 SDValue &CC, bool &Invert,
7524 SDValue &OtherOp,
7525 SelectionDAG &DAG) {
7526 switch (N->getOpcode()) {
7527 default: return false;
7528 case ISD::SELECT: {
7529 CC = N->getOperand(0);
7530 SDValue N1 = N->getOperand(1);
7531 SDValue N2 = N->getOperand(2);
7532 if (isZeroOrAllOnes(N1, AllOnes)) {
7533 Invert = false;
7534 OtherOp = N2;
7535 return true;
7536 }
7537 if (isZeroOrAllOnes(N2, AllOnes)) {
7538 Invert = true;
7539 OtherOp = N1;
7540 return true;
7541 }
7542 return false;
7543 }
7544 case ISD::ZERO_EXTEND:
7545 // (zext cc) can never be the all ones value.
7546 if (AllOnes)
7547 return false;
7548 // Fall through.
7549 case ISD::SIGN_EXTEND: {
7550 EVT VT = N->getValueType(0);
7551 CC = N->getOperand(0);
7552 if (CC.getValueType() != MVT::i1)
7553 return false;
7554 Invert = !AllOnes;
7555 if (AllOnes)
7556 // When looking for an AllOnes constant, N is an sext, and the 'other'
7557 // value is 0.
7558 OtherOp = DAG.getConstant(0, VT);
7559 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7560 // When looking for a 0 constant, N can be zext or sext.
7561 OtherOp = DAG.getConstant(1, VT);
7562 else
7563 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7564 return true;
7565 }
7566 }
7567}
7568
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007569// Combine a constant select operand into its use:
7570//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007571// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7572// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7573// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7574// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7575// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007576//
7577// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007578// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007579//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007580// Also recognize sext/zext from i1:
7581//
7582// (add (zext cc), x) -> (select cc (add x, 1), x)
7583// (add (sext cc), x) -> (select cc (add x, -1), x)
7584//
7585// These transformations eventually create predicated instructions.
7586//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007587// @param N The node to transform.
7588// @param Slct The N operand that is a select.
7589// @param OtherOp The other N operand (x above).
7590// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007591// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007592// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007593static
7594SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007595 TargetLowering::DAGCombinerInfo &DCI,
7596 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007597 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007598 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007599 SDValue NonConstantVal;
7600 SDValue CCOp;
7601 bool SwapSelectOps;
7602 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7603 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007604 return SDValue();
7605
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007606 // Slct is now know to be the desired identity constant when CC is true.
7607 SDValue TrueVal = OtherOp;
7608 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7609 OtherOp, NonConstantVal);
7610 // Unless SwapSelectOps says CC should be false.
7611 if (SwapSelectOps)
7612 std::swap(TrueVal, FalseVal);
7613
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007614 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007615 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007616}
7617
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007618// Attempt combineSelectAndUse on each operand of a commutative operator N.
7619static
7620SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7621 TargetLowering::DAGCombinerInfo &DCI) {
7622 SDValue N0 = N->getOperand(0);
7623 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007624 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007625 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7626 if (Result.getNode())
7627 return Result;
7628 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007629 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007630 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7631 if (Result.getNode())
7632 return Result;
7633 }
7634 return SDValue();
7635}
7636
Eric Christopherfa6f5912011-06-29 21:10:36 +00007637// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007638// (only after legalization).
7639static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7640 TargetLowering::DAGCombinerInfo &DCI,
7641 const ARMSubtarget *Subtarget) {
7642
7643 // Only perform optimization if after legalize, and if NEON is available. We
7644 // also expected both operands to be BUILD_VECTORs.
7645 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7646 || N0.getOpcode() != ISD::BUILD_VECTOR
7647 || N1.getOpcode() != ISD::BUILD_VECTOR)
7648 return SDValue();
7649
7650 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7651 EVT VT = N->getValueType(0);
7652 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7653 return SDValue();
7654
7655 // Check that the vector operands are of the right form.
7656 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7657 // operands, where N is the size of the formed vector.
7658 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7659 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007660
7661 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007662 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007663 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007664 SDValue Vec = N0->getOperand(0)->getOperand(0);
7665 SDNode *V = Vec.getNode();
7666 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007667
Eric Christopherfa6f5912011-06-29 21:10:36 +00007668 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007669 // check to see if each of their operands are an EXTRACT_VECTOR with
7670 // the same vector and appropriate index.
7671 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7672 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7673 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007674
Tanya Lattner189531f2011-06-14 23:48:48 +00007675 SDValue ExtVec0 = N0->getOperand(i);
7676 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007677
Tanya Lattner189531f2011-06-14 23:48:48 +00007678 // First operand is the vector, verify its the same.
7679 if (V != ExtVec0->getOperand(0).getNode() ||
7680 V != ExtVec1->getOperand(0).getNode())
7681 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007682
Tanya Lattner189531f2011-06-14 23:48:48 +00007683 // Second is the constant, verify its correct.
7684 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7685 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007686
Tanya Lattner189531f2011-06-14 23:48:48 +00007687 // For the constant, we want to see all the even or all the odd.
7688 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7689 || C1->getZExtValue() != nextIndex+1)
7690 return SDValue();
7691
7692 // Increment index.
7693 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007694 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007695 return SDValue();
7696 }
7697
7698 // Create VPADDL node.
7699 SelectionDAG &DAG = DCI.DAG;
7700 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007701
7702 // Build operand list.
7703 SmallVector<SDValue, 8> Ops;
7704 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7705 TLI.getPointerTy()));
7706
7707 // Input is the vector.
7708 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007709
Tanya Lattner189531f2011-06-14 23:48:48 +00007710 // Get widened type and narrowed type.
7711 MVT widenType;
7712 unsigned numElem = VT.getVectorNumElements();
7713 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7714 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7715 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7716 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7717 default:
Craig Topperbc219812012-02-07 02:50:20 +00007718 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007719 }
7720
7721 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7722 widenType, &Ops[0], Ops.size());
7723 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7724}
7725
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007726static SDValue findMUL_LOHI(SDValue V) {
7727 if (V->getOpcode() == ISD::UMUL_LOHI ||
7728 V->getOpcode() == ISD::SMUL_LOHI)
7729 return V;
7730 return SDValue();
7731}
7732
7733static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7734 TargetLowering::DAGCombinerInfo &DCI,
7735 const ARMSubtarget *Subtarget) {
7736
7737 if (Subtarget->isThumb1Only()) return SDValue();
7738
7739 // Only perform the checks after legalize when the pattern is available.
7740 if (DCI.isBeforeLegalize()) return SDValue();
7741
7742 // Look for multiply add opportunities.
7743 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7744 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7745 // a glue link from the first add to the second add.
7746 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7747 // a S/UMLAL instruction.
7748 // loAdd UMUL_LOHI
7749 // \ / :lo \ :hi
7750 // \ / \ [no multiline comment]
7751 // ADDC | hiAdd
7752 // \ :glue / /
7753 // \ / /
7754 // ADDE
7755 //
7756 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7757 SDValue AddcOp0 = AddcNode->getOperand(0);
7758 SDValue AddcOp1 = AddcNode->getOperand(1);
7759
7760 // Check if the two operands are from the same mul_lohi node.
7761 if (AddcOp0.getNode() == AddcOp1.getNode())
7762 return SDValue();
7763
7764 assert(AddcNode->getNumValues() == 2 &&
7765 AddcNode->getValueType(0) == MVT::i32 &&
7766 AddcNode->getValueType(1) == MVT::Glue &&
7767 "Expect ADDC with two result values: i32, glue");
7768
7769 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7770 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7771 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7772 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7773 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7774 return SDValue();
7775
7776 // Look for the glued ADDE.
7777 SDNode* AddeNode = AddcNode->getGluedUser();
7778 if (AddeNode == NULL)
7779 return SDValue();
7780
7781 // Make sure it is really an ADDE.
7782 if (AddeNode->getOpcode() != ISD::ADDE)
7783 return SDValue();
7784
7785 assert(AddeNode->getNumOperands() == 3 &&
7786 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7787 "ADDE node has the wrong inputs");
7788
7789 // Check for the triangle shape.
7790 SDValue AddeOp0 = AddeNode->getOperand(0);
7791 SDValue AddeOp1 = AddeNode->getOperand(1);
7792
7793 // Make sure that the ADDE operands are not coming from the same node.
7794 if (AddeOp0.getNode() == AddeOp1.getNode())
7795 return SDValue();
7796
7797 // Find the MUL_LOHI node walking up ADDE's operands.
7798 bool IsLeftOperandMUL = false;
7799 SDValue MULOp = findMUL_LOHI(AddeOp0);
7800 if (MULOp == SDValue())
7801 MULOp = findMUL_LOHI(AddeOp1);
7802 else
7803 IsLeftOperandMUL = true;
7804 if (MULOp == SDValue())
7805 return SDValue();
7806
7807 // Figure out the right opcode.
7808 unsigned Opc = MULOp->getOpcode();
7809 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7810
7811 // Figure out the high and low input values to the MLAL node.
7812 SDValue* HiMul = &MULOp;
7813 SDValue* HiAdd = NULL;
7814 SDValue* LoMul = NULL;
7815 SDValue* LowAdd = NULL;
7816
7817 if (IsLeftOperandMUL)
7818 HiAdd = &AddeOp1;
7819 else
7820 HiAdd = &AddeOp0;
7821
7822
7823 if (AddcOp0->getOpcode() == Opc) {
7824 LoMul = &AddcOp0;
7825 LowAdd = &AddcOp1;
7826 }
7827 if (AddcOp1->getOpcode() == Opc) {
7828 LoMul = &AddcOp1;
7829 LowAdd = &AddcOp0;
7830 }
7831
7832 if (LoMul == NULL)
7833 return SDValue();
7834
7835 if (LoMul->getNode() != HiMul->getNode())
7836 return SDValue();
7837
7838 // Create the merged node.
7839 SelectionDAG &DAG = DCI.DAG;
7840
7841 // Build operand list.
7842 SmallVector<SDValue, 8> Ops;
7843 Ops.push_back(LoMul->getOperand(0));
7844 Ops.push_back(LoMul->getOperand(1));
7845 Ops.push_back(*LowAdd);
7846 Ops.push_back(*HiAdd);
7847
7848 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7849 DAG.getVTList(MVT::i32, MVT::i32),
7850 &Ops[0], Ops.size());
7851
7852 // Replace the ADDs' nodes uses by the MLA node's values.
7853 SDValue HiMLALResult(MLALNode.getNode(), 1);
7854 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7855
7856 SDValue LoMLALResult(MLALNode.getNode(), 0);
7857 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7858
7859 // Return original node to notify the driver to stop replacing.
7860 SDValue resNode(AddcNode, 0);
7861 return resNode;
7862}
7863
7864/// PerformADDCCombine - Target-specific dag combine transform from
7865/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7866static SDValue PerformADDCCombine(SDNode *N,
7867 TargetLowering::DAGCombinerInfo &DCI,
7868 const ARMSubtarget *Subtarget) {
7869
7870 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7871
7872}
7873
Bob Wilson3d5792a2010-07-29 20:34:14 +00007874/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7875/// operands N0 and N1. This is a helper for PerformADDCombine that is
7876/// called with the default operands, and if that fails, with commuted
7877/// operands.
7878static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007879 TargetLowering::DAGCombinerInfo &DCI,
7880 const ARMSubtarget *Subtarget){
7881
7882 // Attempt to create vpaddl for this add.
7883 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7884 if (Result.getNode())
7885 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007886
Chris Lattnerd1980a52009-03-12 06:52:53 +00007887 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007888 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007889 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7890 if (Result.getNode()) return Result;
7891 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007892 return SDValue();
7893}
7894
Bob Wilson3d5792a2010-07-29 20:34:14 +00007895/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7896///
7897static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007898 TargetLowering::DAGCombinerInfo &DCI,
7899 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007900 SDValue N0 = N->getOperand(0);
7901 SDValue N1 = N->getOperand(1);
7902
7903 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007904 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007905 if (Result.getNode())
7906 return Result;
7907
7908 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007909 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007910}
7911
Chris Lattnerd1980a52009-03-12 06:52:53 +00007912/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007913///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007914static SDValue PerformSUBCombine(SDNode *N,
7915 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007916 SDValue N0 = N->getOperand(0);
7917 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007918
Chris Lattnerd1980a52009-03-12 06:52:53 +00007919 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007920 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007921 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7922 if (Result.getNode()) return Result;
7923 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007924
Chris Lattnerd1980a52009-03-12 06:52:53 +00007925 return SDValue();
7926}
7927
Evan Cheng463d3582011-03-31 19:38:48 +00007928/// PerformVMULCombine
7929/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7930/// special multiplier accumulator forwarding.
7931/// vmul d3, d0, d2
7932/// vmla d3, d1, d2
7933/// is faster than
7934/// vadd d3, d0, d1
7935/// vmul d3, d3, d2
7936static SDValue PerformVMULCombine(SDNode *N,
7937 TargetLowering::DAGCombinerInfo &DCI,
7938 const ARMSubtarget *Subtarget) {
7939 if (!Subtarget->hasVMLxForwarding())
7940 return SDValue();
7941
7942 SelectionDAG &DAG = DCI.DAG;
7943 SDValue N0 = N->getOperand(0);
7944 SDValue N1 = N->getOperand(1);
7945 unsigned Opcode = N0.getOpcode();
7946 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7947 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007948 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007949 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7950 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7951 return SDValue();
7952 std::swap(N0, N1);
7953 }
7954
7955 EVT VT = N->getValueType(0);
7956 DebugLoc DL = N->getDebugLoc();
7957 SDValue N00 = N0->getOperand(0);
7958 SDValue N01 = N0->getOperand(1);
7959 return DAG.getNode(Opcode, DL, VT,
7960 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7961 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7962}
7963
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007964static SDValue PerformMULCombine(SDNode *N,
7965 TargetLowering::DAGCombinerInfo &DCI,
7966 const ARMSubtarget *Subtarget) {
7967 SelectionDAG &DAG = DCI.DAG;
7968
7969 if (Subtarget->isThumb1Only())
7970 return SDValue();
7971
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007972 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7973 return SDValue();
7974
7975 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007976 if (VT.is64BitVector() || VT.is128BitVector())
7977 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007978 if (VT != MVT::i32)
7979 return SDValue();
7980
7981 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7982 if (!C)
7983 return SDValue();
7984
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007985 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007986 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007987
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007988 ShiftAmt = ShiftAmt & (32 - 1);
7989 SDValue V = N->getOperand(0);
7990 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007991
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007992 SDValue Res;
7993 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007994
7995 if (MulAmt >= 0) {
7996 if (isPowerOf2_32(MulAmt - 1)) {
7997 // (mul x, 2^N + 1) => (add (shl x, N), x)
7998 Res = DAG.getNode(ISD::ADD, DL, VT,
7999 V,
8000 DAG.getNode(ISD::SHL, DL, VT,
8001 V,
8002 DAG.getConstant(Log2_32(MulAmt - 1),
8003 MVT::i32)));
8004 } else if (isPowerOf2_32(MulAmt + 1)) {
8005 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8006 Res = DAG.getNode(ISD::SUB, DL, VT,
8007 DAG.getNode(ISD::SHL, DL, VT,
8008 V,
8009 DAG.getConstant(Log2_32(MulAmt + 1),
8010 MVT::i32)),
8011 V);
8012 } else
8013 return SDValue();
8014 } else {
8015 uint64_t MulAmtAbs = -MulAmt;
8016 if (isPowerOf2_32(MulAmtAbs + 1)) {
8017 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8018 Res = DAG.getNode(ISD::SUB, DL, VT,
8019 V,
8020 DAG.getNode(ISD::SHL, DL, VT,
8021 V,
8022 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8023 MVT::i32)));
8024 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8025 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8026 Res = DAG.getNode(ISD::ADD, DL, VT,
8027 V,
8028 DAG.getNode(ISD::SHL, DL, VT,
8029 V,
8030 DAG.getConstant(Log2_32(MulAmtAbs-1),
8031 MVT::i32)));
8032 Res = DAG.getNode(ISD::SUB, DL, VT,
8033 DAG.getConstant(0, MVT::i32),Res);
8034
8035 } else
8036 return SDValue();
8037 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00008038
8039 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00008040 Res = DAG.getNode(ISD::SHL, DL, VT,
8041 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008042
8043 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00008044 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008045 return SDValue();
8046}
8047
Owen Anderson080c0922010-11-05 19:27:46 +00008048static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00008049 TargetLowering::DAGCombinerInfo &DCI,
8050 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00008051
Owen Anderson080c0922010-11-05 19:27:46 +00008052 // Attempt to use immediate-form VBIC
8053 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8054 DebugLoc dl = N->getDebugLoc();
8055 EVT VT = N->getValueType(0);
8056 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008057
Tanya Lattner0433b212011-04-07 15:24:20 +00008058 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8059 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008060
Owen Anderson080c0922010-11-05 19:27:46 +00008061 APInt SplatBits, SplatUndef;
8062 unsigned SplatBitSize;
8063 bool HasAnyUndefs;
8064 if (BVN &&
8065 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8066 if (SplatBitSize <= 64) {
8067 EVT VbicVT;
8068 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8069 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008070 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008071 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00008072 if (Val.getNode()) {
8073 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008074 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00008075 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008076 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00008077 }
8078 }
8079 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008080
Evan Chengc892aeb2012-02-23 01:19:06 +00008081 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008082 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8083 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8084 if (Result.getNode())
8085 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008086 }
8087
Owen Anderson080c0922010-11-05 19:27:46 +00008088 return SDValue();
8089}
8090
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008091/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8092static SDValue PerformORCombine(SDNode *N,
8093 TargetLowering::DAGCombinerInfo &DCI,
8094 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00008095 // Attempt to use immediate-form VORR
8096 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8097 DebugLoc dl = N->getDebugLoc();
8098 EVT VT = N->getValueType(0);
8099 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008100
Tanya Lattner0433b212011-04-07 15:24:20 +00008101 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8102 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008103
Owen Anderson60f48702010-11-03 23:15:26 +00008104 APInt SplatBits, SplatUndef;
8105 unsigned SplatBitSize;
8106 bool HasAnyUndefs;
8107 if (BVN && Subtarget->hasNEON() &&
8108 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8109 if (SplatBitSize <= 64) {
8110 EVT VorrVT;
8111 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8112 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008113 DAG, VorrVT, VT.is128BitVector(),
8114 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00008115 if (Val.getNode()) {
8116 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008117 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00008118 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008119 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00008120 }
8121 }
8122 }
8123
Evan Chengc892aeb2012-02-23 01:19:06 +00008124 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008125 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8126 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8127 if (Result.getNode())
8128 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008129 }
8130
Nadav Rotemdf832032012-08-13 18:52:44 +00008131 // The code below optimizes (or (and X, Y), Z).
8132 // The AND operand needs to have a single user to make these optimizations
8133 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008134 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00008135 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008136 return SDValue();
8137 SDValue N1 = N->getOperand(1);
8138
8139 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8140 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8141 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8142 APInt SplatUndef;
8143 unsigned SplatBitSize;
8144 bool HasAnyUndefs;
8145
8146 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8147 APInt SplatBits0;
8148 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8149 HasAnyUndefs) && !HasAnyUndefs) {
8150 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8151 APInt SplatBits1;
8152 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8153 HasAnyUndefs) && !HasAnyUndefs &&
8154 SplatBits0 == ~SplatBits1) {
8155 // Canonicalize the vector type to make instruction selection simpler.
8156 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8157 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8158 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00008159 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008160 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8161 }
8162 }
8163 }
8164
Jim Grosbach54238562010-07-17 03:30:54 +00008165 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8166 // reasonable.
8167
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008168 // BFI is only available on V6T2+
8169 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8170 return SDValue();
8171
Jim Grosbach54238562010-07-17 03:30:54 +00008172 DebugLoc DL = N->getDebugLoc();
8173 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008174 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00008175 //
8176 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008177 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008178 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008179 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008180 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00008181 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008182
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008183 if (VT != MVT::i32)
8184 return SDValue();
8185
Evan Cheng30fb13f2010-12-13 20:32:54 +00008186 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00008187
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008188 // The value and the mask need to be constants so we can verify this is
8189 // actually a bitfield set. If the mask is 0xffff, we can do better
8190 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00008191 SDValue MaskOp = N0.getOperand(1);
8192 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8193 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008194 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008195 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008196 if (Mask == 0xffff)
8197 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008198 SDValue Res;
8199 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008200 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8201 if (N1C) {
8202 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008203 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00008204 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008205
Evan Chenga9688c42010-12-11 04:11:38 +00008206 if (ARM::isBitFieldInvertedMask(Mask)) {
8207 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008208
Evan Cheng30fb13f2010-12-13 20:32:54 +00008209 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00008210 DAG.getConstant(Val, MVT::i32),
8211 DAG.getConstant(Mask, MVT::i32));
8212
8213 // Do not add new nodes to DAG combiner worklist.
8214 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008215 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008216 }
Jim Grosbach54238562010-07-17 03:30:54 +00008217 } else if (N1.getOpcode() == ISD::AND) {
8218 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008219 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8220 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00008221 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008222 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008223
Eric Christopher29aeed12011-03-26 01:21:03 +00008224 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8225 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00008226 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008227 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008228 // The pack halfword instruction works better for masks that fit it,
8229 // so use that when it's available.
8230 if (Subtarget->hasT2ExtractPack() &&
8231 (Mask == 0xffff || Mask == 0xffff0000))
8232 return SDValue();
8233 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00008234 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00008235 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00008236 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00008237 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00008238 DAG.getConstant(Mask, MVT::i32));
8239 // Do not add new nodes to DAG combiner worklist.
8240 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008241 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008242 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008243 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008244 // The pack halfword instruction works better for masks that fit it,
8245 // so use that when it's available.
8246 if (Subtarget->hasT2ExtractPack() &&
8247 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8248 return SDValue();
8249 // 2b
8250 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008251 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00008252 DAG.getConstant(lsb, MVT::i32));
8253 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00008254 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00008255 // Do not add new nodes to DAG combiner worklist.
8256 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008257 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008258 }
8259 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008260
Evan Cheng30fb13f2010-12-13 20:32:54 +00008261 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8262 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8263 ARM::isBitFieldInvertedMask(~Mask)) {
8264 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8265 // where lsb(mask) == #shamt and masked bits of B are known zero.
8266 SDValue ShAmt = N00.getOperand(1);
8267 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8268 unsigned LSB = CountTrailingZeros_32(Mask);
8269 if (ShAmtC != LSB)
8270 return SDValue();
8271
8272 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8273 DAG.getConstant(~Mask, MVT::i32));
8274
8275 // Do not add new nodes to DAG combiner worklist.
8276 DCI.CombineTo(N, Res, false);
8277 }
8278
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008279 return SDValue();
8280}
8281
Evan Chengc892aeb2012-02-23 01:19:06 +00008282static SDValue PerformXORCombine(SDNode *N,
8283 TargetLowering::DAGCombinerInfo &DCI,
8284 const ARMSubtarget *Subtarget) {
8285 EVT VT = N->getValueType(0);
8286 SelectionDAG &DAG = DCI.DAG;
8287
8288 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8289 return SDValue();
8290
8291 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008292 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8293 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8294 if (Result.getNode())
8295 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008296 }
8297
8298 return SDValue();
8299}
8300
Evan Chengbf188ae2011-06-15 01:12:31 +00008301/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8302/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008303static SDValue PerformBFICombine(SDNode *N,
8304 TargetLowering::DAGCombinerInfo &DCI) {
8305 SDValue N1 = N->getOperand(1);
8306 if (N1.getOpcode() == ISD::AND) {
8307 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8308 if (!N11C)
8309 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008310 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8311 unsigned LSB = CountTrailingZeros_32(~InvMask);
8312 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8313 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008314 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008315 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008316 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8317 N->getOperand(0), N1.getOperand(0),
8318 N->getOperand(2));
8319 }
8320 return SDValue();
8321}
8322
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008323/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8324/// ARMISD::VMOVRRD.
8325static SDValue PerformVMOVRRDCombine(SDNode *N,
8326 TargetLowering::DAGCombinerInfo &DCI) {
8327 // vmovrrd(vmovdrr x, y) -> x,y
8328 SDValue InDouble = N->getOperand(0);
8329 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8330 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008331
8332 // vmovrrd(load f64) -> (load i32), (load i32)
8333 SDNode *InNode = InDouble.getNode();
8334 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8335 InNode->getValueType(0) == MVT::f64 &&
8336 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8337 !cast<LoadSDNode>(InNode)->isVolatile()) {
8338 // TODO: Should this be done for non-FrameIndex operands?
8339 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8340
8341 SelectionDAG &DAG = DCI.DAG;
8342 DebugLoc DL = LD->getDebugLoc();
8343 SDValue BasePtr = LD->getBasePtr();
8344 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8345 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008346 LD->isNonTemporal(), LD->isInvariant(),
8347 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008348
8349 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8350 DAG.getConstant(4, MVT::i32));
8351 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8352 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008353 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008354 std::min(4U, LD->getAlignment() / 2));
8355
8356 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8357 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8358 DCI.RemoveFromWorklist(LD);
8359 DAG.DeleteNode(LD);
8360 return Result;
8361 }
8362
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008363 return SDValue();
8364}
8365
8366/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8367/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8368static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8369 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8370 SDValue Op0 = N->getOperand(0);
8371 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008372 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008373 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008374 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008375 Op1 = Op1.getOperand(0);
8376 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8377 Op0.getNode() == Op1.getNode() &&
8378 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008379 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008380 N->getValueType(0), Op0.getOperand(0));
8381 return SDValue();
8382}
8383
Bob Wilson31600902010-12-21 06:43:19 +00008384/// PerformSTORECombine - Target-specific dag combine xforms for
8385/// ISD::STORE.
8386static SDValue PerformSTORECombine(SDNode *N,
8387 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008388 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008389 if (St->isVolatile())
8390 return SDValue();
8391
Andrew Trick49b446f2012-07-18 18:34:24 +00008392 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008393 // pack all of the elements in one place. Next, store to memory in fewer
8394 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008395 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008396 EVT VT = StVal.getValueType();
8397 if (St->isTruncatingStore() && VT.isVector()) {
8398 SelectionDAG &DAG = DCI.DAG;
8399 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8400 EVT StVT = St->getMemoryVT();
8401 unsigned NumElems = VT.getVectorNumElements();
8402 assert(StVT != VT && "Cannot truncate to the same type");
8403 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8404 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8405
8406 // From, To sizes and ElemCount must be pow of two
8407 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8408
8409 // We are going to use the original vector elt for storing.
8410 // Accumulated smaller vector elements must be a multiple of the store size.
8411 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8412
8413 unsigned SizeRatio = FromEltSz / ToEltSz;
8414 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8415
8416 // Create a type on which we perform the shuffle.
8417 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8418 NumElems*SizeRatio);
8419 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8420
8421 DebugLoc DL = St->getDebugLoc();
8422 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8423 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8424 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8425
8426 // Can't shuffle using an illegal type.
8427 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8428
8429 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8430 DAG.getUNDEF(WideVec.getValueType()),
8431 ShuffleVec.data());
8432 // At this point all of the data is stored at the bottom of the
8433 // register. We now need to save it to mem.
8434
8435 // Find the largest store unit
8436 MVT StoreType = MVT::i8;
8437 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8438 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8439 MVT Tp = (MVT::SimpleValueType)tp;
8440 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8441 StoreType = Tp;
8442 }
8443 // Didn't find a legal store type.
8444 if (!TLI.isTypeLegal(StoreType))
8445 return SDValue();
8446
8447 // Bitcast the original vector into a vector of store-size units
8448 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8449 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8450 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8451 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8452 SmallVector<SDValue, 8> Chains;
8453 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8454 TLI.getPointerTy());
8455 SDValue BasePtr = St->getBasePtr();
8456
8457 // Perform one or more big stores into memory.
8458 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8459 for (unsigned I = 0; I < E; I++) {
8460 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8461 StoreType, ShuffWide,
8462 DAG.getIntPtrConstant(I));
8463 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8464 St->getPointerInfo(), St->isVolatile(),
8465 St->isNonTemporal(), St->getAlignment());
8466 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8467 Increment);
8468 Chains.push_back(Ch);
8469 }
8470 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8471 Chains.size());
8472 }
8473
8474 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008475 return SDValue();
8476
Chad Rosier96b66d62012-04-09 19:38:15 +00008477 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8478 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008479 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008480 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008481 SelectionDAG &DAG = DCI.DAG;
8482 DebugLoc DL = St->getDebugLoc();
8483 SDValue BasePtr = St->getBasePtr();
8484 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8485 StVal.getNode()->getOperand(0), BasePtr,
8486 St->getPointerInfo(), St->isVolatile(),
8487 St->isNonTemporal(), St->getAlignment());
8488
8489 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8490 DAG.getConstant(4, MVT::i32));
8491 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8492 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8493 St->isNonTemporal(),
8494 std::min(4U, St->getAlignment() / 2));
8495 }
8496
8497 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008498 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8499 return SDValue();
8500
Chad Rosier96b66d62012-04-09 19:38:15 +00008501 // Bitcast an i64 store extracted from a vector to f64.
8502 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008503 SelectionDAG &DAG = DCI.DAG;
8504 DebugLoc dl = StVal.getDebugLoc();
8505 SDValue IntVec = StVal.getOperand(0);
8506 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8507 IntVec.getValueType().getVectorNumElements());
8508 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8509 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8510 Vec, StVal.getOperand(1));
8511 dl = N->getDebugLoc();
8512 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8513 // Make the DAGCombiner fold the bitcasts.
8514 DCI.AddToWorklist(Vec.getNode());
8515 DCI.AddToWorklist(ExtElt.getNode());
8516 DCI.AddToWorklist(V.getNode());
8517 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8518 St->getPointerInfo(), St->isVolatile(),
8519 St->isNonTemporal(), St->getAlignment(),
8520 St->getTBAAInfo());
8521}
8522
8523/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8524/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8525/// i64 vector to have f64 elements, since the value can then be loaded
8526/// directly into a VFP register.
8527static bool hasNormalLoadOperand(SDNode *N) {
8528 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8529 for (unsigned i = 0; i < NumElts; ++i) {
8530 SDNode *Elt = N->getOperand(i).getNode();
8531 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8532 return true;
8533 }
8534 return false;
8535}
8536
Bob Wilson75f02882010-09-17 22:59:05 +00008537/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8538/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008539static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8540 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008541 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8542 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8543 // into a pair of GPRs, which is fine when the value is used as a scalar,
8544 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008545 SelectionDAG &DAG = DCI.DAG;
8546 if (N->getNumOperands() == 2) {
8547 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8548 if (RV.getNode())
8549 return RV;
8550 }
Bob Wilson75f02882010-09-17 22:59:05 +00008551
Bob Wilson31600902010-12-21 06:43:19 +00008552 // Load i64 elements as f64 values so that type legalization does not split
8553 // them up into i32 values.
8554 EVT VT = N->getValueType(0);
8555 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8556 return SDValue();
8557 DebugLoc dl = N->getDebugLoc();
8558 SmallVector<SDValue, 8> Ops;
8559 unsigned NumElts = VT.getVectorNumElements();
8560 for (unsigned i = 0; i < NumElts; ++i) {
8561 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8562 Ops.push_back(V);
8563 // Make the DAGCombiner fold the bitcast.
8564 DCI.AddToWorklist(V.getNode());
8565 }
8566 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8567 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8568 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8569}
8570
8571/// PerformInsertEltCombine - Target-specific dag combine xforms for
8572/// ISD::INSERT_VECTOR_ELT.
8573static SDValue PerformInsertEltCombine(SDNode *N,
8574 TargetLowering::DAGCombinerInfo &DCI) {
8575 // Bitcast an i64 load inserted into a vector to f64.
8576 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8577 EVT VT = N->getValueType(0);
8578 SDNode *Elt = N->getOperand(1).getNode();
8579 if (VT.getVectorElementType() != MVT::i64 ||
8580 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8581 return SDValue();
8582
8583 SelectionDAG &DAG = DCI.DAG;
8584 DebugLoc dl = N->getDebugLoc();
8585 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8586 VT.getVectorNumElements());
8587 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8588 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8589 // Make the DAGCombiner fold the bitcasts.
8590 DCI.AddToWorklist(Vec.getNode());
8591 DCI.AddToWorklist(V.getNode());
8592 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8593 Vec, V, N->getOperand(2));
8594 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008595}
8596
Bob Wilsonf20700c2010-10-27 20:38:28 +00008597/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8598/// ISD::VECTOR_SHUFFLE.
8599static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8600 // The LLVM shufflevector instruction does not require the shuffle mask
8601 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8602 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8603 // operands do not match the mask length, they are extended by concatenating
8604 // them with undef vectors. That is probably the right thing for other
8605 // targets, but for NEON it is better to concatenate two double-register
8606 // size vector operands into a single quad-register size vector. Do that
8607 // transformation here:
8608 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8609 // shuffle(concat(v1, v2), undef)
8610 SDValue Op0 = N->getOperand(0);
8611 SDValue Op1 = N->getOperand(1);
8612 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8613 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8614 Op0.getNumOperands() != 2 ||
8615 Op1.getNumOperands() != 2)
8616 return SDValue();
8617 SDValue Concat0Op1 = Op0.getOperand(1);
8618 SDValue Concat1Op1 = Op1.getOperand(1);
8619 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8620 Concat1Op1.getOpcode() != ISD::UNDEF)
8621 return SDValue();
8622 // Skip the transformation if any of the types are illegal.
8623 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8624 EVT VT = N->getValueType(0);
8625 if (!TLI.isTypeLegal(VT) ||
8626 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8627 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8628 return SDValue();
8629
8630 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8631 Op0.getOperand(0), Op1.getOperand(0));
8632 // Translate the shuffle mask.
8633 SmallVector<int, 16> NewMask;
8634 unsigned NumElts = VT.getVectorNumElements();
8635 unsigned HalfElts = NumElts/2;
8636 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8637 for (unsigned n = 0; n < NumElts; ++n) {
8638 int MaskElt = SVN->getMaskElt(n);
8639 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008640 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008641 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008642 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008643 NewElt = HalfElts + MaskElt - NumElts;
8644 NewMask.push_back(NewElt);
8645 }
8646 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8647 DAG.getUNDEF(VT), NewMask.data());
8648}
8649
Bob Wilson1c3ef902011-02-07 17:43:21 +00008650/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8651/// NEON load/store intrinsics to merge base address updates.
8652static SDValue CombineBaseUpdate(SDNode *N,
8653 TargetLowering::DAGCombinerInfo &DCI) {
8654 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8655 return SDValue();
8656
8657 SelectionDAG &DAG = DCI.DAG;
8658 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8659 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8660 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8661 SDValue Addr = N->getOperand(AddrOpIdx);
8662
8663 // Search for a use of the address operand that is an increment.
8664 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8665 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8666 SDNode *User = *UI;
8667 if (User->getOpcode() != ISD::ADD ||
8668 UI.getUse().getResNo() != Addr.getResNo())
8669 continue;
8670
8671 // Check that the add is independent of the load/store. Otherwise, folding
8672 // it would create a cycle.
8673 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8674 continue;
8675
8676 // Find the new opcode for the updating load/store.
8677 bool isLoad = true;
8678 bool isLaneOp = false;
8679 unsigned NewOpc = 0;
8680 unsigned NumVecs = 0;
8681 if (isIntrinsic) {
8682 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8683 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008684 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008685 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8686 NumVecs = 1; break;
8687 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8688 NumVecs = 2; break;
8689 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8690 NumVecs = 3; break;
8691 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8692 NumVecs = 4; break;
8693 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8694 NumVecs = 2; isLaneOp = true; break;
8695 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8696 NumVecs = 3; isLaneOp = true; break;
8697 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8698 NumVecs = 4; isLaneOp = true; break;
8699 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8700 NumVecs = 1; isLoad = false; break;
8701 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8702 NumVecs = 2; isLoad = false; break;
8703 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8704 NumVecs = 3; isLoad = false; break;
8705 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8706 NumVecs = 4; isLoad = false; break;
8707 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8708 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8709 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8710 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8711 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8712 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8713 }
8714 } else {
8715 isLaneOp = true;
8716 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008717 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008718 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8719 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8720 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8721 }
8722 }
8723
8724 // Find the size of memory referenced by the load/store.
8725 EVT VecTy;
8726 if (isLoad)
8727 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008728 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008729 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8730 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8731 if (isLaneOp)
8732 NumBytes /= VecTy.getVectorNumElements();
8733
8734 // If the increment is a constant, it must match the memory ref size.
8735 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8736 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8737 uint64_t IncVal = CInc->getZExtValue();
8738 if (IncVal != NumBytes)
8739 continue;
8740 } else if (NumBytes >= 3 * 16) {
8741 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8742 // separate instructions that make it harder to use a non-constant update.
8743 continue;
8744 }
8745
8746 // Create the new updating load/store node.
8747 EVT Tys[6];
8748 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8749 unsigned n;
8750 for (n = 0; n < NumResultVecs; ++n)
8751 Tys[n] = VecTy;
8752 Tys[n++] = MVT::i32;
8753 Tys[n] = MVT::Other;
8754 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8755 SmallVector<SDValue, 8> Ops;
8756 Ops.push_back(N->getOperand(0)); // incoming chain
8757 Ops.push_back(N->getOperand(AddrOpIdx));
8758 Ops.push_back(Inc);
8759 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8760 Ops.push_back(N->getOperand(i));
8761 }
8762 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8763 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8764 Ops.data(), Ops.size(),
8765 MemInt->getMemoryVT(),
8766 MemInt->getMemOperand());
8767
8768 // Update the uses.
8769 std::vector<SDValue> NewResults;
8770 for (unsigned i = 0; i < NumResultVecs; ++i) {
8771 NewResults.push_back(SDValue(UpdN.getNode(), i));
8772 }
8773 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8774 DCI.CombineTo(N, NewResults);
8775 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8776
8777 break;
Owen Anderson76706012011-04-05 21:48:57 +00008778 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008779 return SDValue();
8780}
8781
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008782/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8783/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8784/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8785/// return true.
8786static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8787 SelectionDAG &DAG = DCI.DAG;
8788 EVT VT = N->getValueType(0);
8789 // vldN-dup instructions only support 64-bit vectors for N > 1.
8790 if (!VT.is64BitVector())
8791 return false;
8792
8793 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8794 SDNode *VLD = N->getOperand(0).getNode();
8795 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8796 return false;
8797 unsigned NumVecs = 0;
8798 unsigned NewOpc = 0;
8799 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8800 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8801 NumVecs = 2;
8802 NewOpc = ARMISD::VLD2DUP;
8803 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8804 NumVecs = 3;
8805 NewOpc = ARMISD::VLD3DUP;
8806 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8807 NumVecs = 4;
8808 NewOpc = ARMISD::VLD4DUP;
8809 } else {
8810 return false;
8811 }
8812
8813 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8814 // numbers match the load.
8815 unsigned VLDLaneNo =
8816 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8817 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8818 UI != UE; ++UI) {
8819 // Ignore uses of the chain result.
8820 if (UI.getUse().getResNo() == NumVecs)
8821 continue;
8822 SDNode *User = *UI;
8823 if (User->getOpcode() != ARMISD::VDUPLANE ||
8824 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8825 return false;
8826 }
8827
8828 // Create the vldN-dup node.
8829 EVT Tys[5];
8830 unsigned n;
8831 for (n = 0; n < NumVecs; ++n)
8832 Tys[n] = VT;
8833 Tys[n] = MVT::Other;
8834 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8835 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8836 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8837 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8838 Ops, 2, VLDMemInt->getMemoryVT(),
8839 VLDMemInt->getMemOperand());
8840
8841 // Update the uses.
8842 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8843 UI != UE; ++UI) {
8844 unsigned ResNo = UI.getUse().getResNo();
8845 // Ignore uses of the chain result.
8846 if (ResNo == NumVecs)
8847 continue;
8848 SDNode *User = *UI;
8849 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8850 }
8851
8852 // Now the vldN-lane intrinsic is dead except for its chain result.
8853 // Update uses of the chain.
8854 std::vector<SDValue> VLDDupResults;
8855 for (unsigned n = 0; n < NumVecs; ++n)
8856 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8857 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8858 DCI.CombineTo(VLD, VLDDupResults);
8859
8860 return true;
8861}
8862
Bob Wilson9e82bf12010-07-14 01:22:12 +00008863/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8864/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008865static SDValue PerformVDUPLANECombine(SDNode *N,
8866 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008867 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008868
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008869 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8870 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8871 if (CombineVLDDUP(N, DCI))
8872 return SDValue(N, 0);
8873
8874 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8875 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008876 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008877 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008878 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008879 return SDValue();
8880
8881 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8882 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8883 // The canonical VMOV for a zero vector uses a 32-bit element size.
8884 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8885 unsigned EltBits;
8886 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8887 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008888 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008889 if (EltSize > VT.getVectorElementType().getSizeInBits())
8890 return SDValue();
8891
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008892 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008893}
8894
Eric Christopherfa6f5912011-06-29 21:10:36 +00008895// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008896// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8897static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8898{
Chad Rosier118c9a02011-06-28 17:26:57 +00008899 integerPart cN;
8900 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008901 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8902 I != E; I++) {
8903 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8904 if (!C)
8905 return false;
8906
Eric Christopherfa6f5912011-06-29 21:10:36 +00008907 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008908 APFloat APF = C->getValueAPF();
8909 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8910 != APFloat::opOK || !isExact)
8911 return false;
8912
8913 c0 = (I == 0) ? cN : c0;
8914 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8915 return false;
8916 }
8917 C = c0;
8918 return true;
8919}
8920
8921/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8922/// can replace combinations of VMUL and VCVT (floating-point to integer)
8923/// when the VMUL has a constant operand that is a power of 2.
8924///
8925/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8926/// vmul.f32 d16, d17, d16
8927/// vcvt.s32.f32 d16, d16
8928/// becomes:
8929/// vcvt.s32.f32 d16, d16, #3
8930static SDValue PerformVCVTCombine(SDNode *N,
8931 TargetLowering::DAGCombinerInfo &DCI,
8932 const ARMSubtarget *Subtarget) {
8933 SelectionDAG &DAG = DCI.DAG;
8934 SDValue Op = N->getOperand(0);
8935
8936 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8937 Op.getOpcode() != ISD::FMUL)
8938 return SDValue();
8939
8940 uint64_t C;
8941 SDValue N0 = Op->getOperand(0);
8942 SDValue ConstVec = Op->getOperand(1);
8943 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8944
Eric Christopherfa6f5912011-06-29 21:10:36 +00008945 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008946 !isConstVecPow2(ConstVec, isSigned, C))
8947 return SDValue();
8948
8949 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8950 Intrinsic::arm_neon_vcvtfp2fxu;
8951 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8952 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008953 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008954 DAG.getConstant(Log2_64(C), MVT::i32));
8955}
8956
8957/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8958/// can replace combinations of VCVT (integer to floating-point) and VDIV
8959/// when the VDIV has a constant operand that is a power of 2.
8960///
8961/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8962/// vcvt.f32.s32 d16, d16
8963/// vdiv.f32 d16, d17, d16
8964/// becomes:
8965/// vcvt.f32.s32 d16, d16, #3
8966static SDValue PerformVDIVCombine(SDNode *N,
8967 TargetLowering::DAGCombinerInfo &DCI,
8968 const ARMSubtarget *Subtarget) {
8969 SelectionDAG &DAG = DCI.DAG;
8970 SDValue Op = N->getOperand(0);
8971 unsigned OpOpcode = Op.getNode()->getOpcode();
8972
8973 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8974 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8975 return SDValue();
8976
8977 uint64_t C;
8978 SDValue ConstVec = N->getOperand(1);
8979 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8980
8981 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8982 !isConstVecPow2(ConstVec, isSigned, C))
8983 return SDValue();
8984
Eric Christopherfa6f5912011-06-29 21:10:36 +00008985 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008986 Intrinsic::arm_neon_vcvtfxu2fp;
8987 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8988 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008989 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008990 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8991}
8992
8993/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008994/// operand of a vector shift operation, where all the elements of the
8995/// build_vector must have the same constant integer value.
8996static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8997 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008998 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008999 Op = Op.getOperand(0);
9000 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9001 APInt SplatBits, SplatUndef;
9002 unsigned SplatBitSize;
9003 bool HasAnyUndefs;
9004 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9005 HasAnyUndefs, ElementBits) ||
9006 SplatBitSize > ElementBits)
9007 return false;
9008 Cnt = SplatBits.getSExtValue();
9009 return true;
9010}
9011
9012/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9013/// operand of a vector shift left operation. That value must be in the range:
9014/// 0 <= Value < ElementBits for a left shift; or
9015/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00009016static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009017 assert(VT.isVector() && "vector shift count is not a vector type");
9018 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9019 if (! getVShiftImm(Op, ElementBits, Cnt))
9020 return false;
9021 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9022}
9023
9024/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9025/// operand of a vector shift right operation. For a shift opcode, the value
9026/// is positive, but for an intrinsic the value count must be negative. The
9027/// absolute value must be in the range:
9028/// 1 <= |Value| <= ElementBits for a right shift; or
9029/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00009030static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00009031 int64_t &Cnt) {
9032 assert(VT.isVector() && "vector shift count is not a vector type");
9033 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9034 if (! getVShiftImm(Op, ElementBits, Cnt))
9035 return false;
9036 if (isIntrinsic)
9037 Cnt = -Cnt;
9038 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9039}
9040
9041/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9042static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9043 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9044 switch (IntNo) {
9045 default:
9046 // Don't do anything for most intrinsics.
9047 break;
9048
9049 // Vector shifts: check for immediate versions and lower them.
9050 // Note: This is done during DAG combining instead of DAG legalizing because
9051 // the build_vectors for 64-bit vector element shift counts are generally
9052 // not legal, and it is hard to see their values after they get legalized to
9053 // loads from a constant pool.
9054 case Intrinsic::arm_neon_vshifts:
9055 case Intrinsic::arm_neon_vshiftu:
9056 case Intrinsic::arm_neon_vshiftls:
9057 case Intrinsic::arm_neon_vshiftlu:
9058 case Intrinsic::arm_neon_vshiftn:
9059 case Intrinsic::arm_neon_vrshifts:
9060 case Intrinsic::arm_neon_vrshiftu:
9061 case Intrinsic::arm_neon_vrshiftn:
9062 case Intrinsic::arm_neon_vqshifts:
9063 case Intrinsic::arm_neon_vqshiftu:
9064 case Intrinsic::arm_neon_vqshiftsu:
9065 case Intrinsic::arm_neon_vqshiftns:
9066 case Intrinsic::arm_neon_vqshiftnu:
9067 case Intrinsic::arm_neon_vqshiftnsu:
9068 case Intrinsic::arm_neon_vqrshiftns:
9069 case Intrinsic::arm_neon_vqrshiftnu:
9070 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00009071 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009072 int64_t Cnt;
9073 unsigned VShiftOpc = 0;
9074
9075 switch (IntNo) {
9076 case Intrinsic::arm_neon_vshifts:
9077 case Intrinsic::arm_neon_vshiftu:
9078 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9079 VShiftOpc = ARMISD::VSHL;
9080 break;
9081 }
9082 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9083 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9084 ARMISD::VSHRs : ARMISD::VSHRu);
9085 break;
9086 }
9087 return SDValue();
9088
9089 case Intrinsic::arm_neon_vshiftls:
9090 case Intrinsic::arm_neon_vshiftlu:
9091 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
9092 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009093 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009094
9095 case Intrinsic::arm_neon_vrshifts:
9096 case Intrinsic::arm_neon_vrshiftu:
9097 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9098 break;
9099 return SDValue();
9100
9101 case Intrinsic::arm_neon_vqshifts:
9102 case Intrinsic::arm_neon_vqshiftu:
9103 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9104 break;
9105 return SDValue();
9106
9107 case Intrinsic::arm_neon_vqshiftsu:
9108 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9109 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009110 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009111
9112 case Intrinsic::arm_neon_vshiftn:
9113 case Intrinsic::arm_neon_vrshiftn:
9114 case Intrinsic::arm_neon_vqshiftns:
9115 case Intrinsic::arm_neon_vqshiftnu:
9116 case Intrinsic::arm_neon_vqshiftnsu:
9117 case Intrinsic::arm_neon_vqrshiftns:
9118 case Intrinsic::arm_neon_vqrshiftnu:
9119 case Intrinsic::arm_neon_vqrshiftnsu:
9120 // Narrowing shifts require an immediate right shift.
9121 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9122 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00009123 llvm_unreachable("invalid shift count for narrowing vector shift "
9124 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009125
9126 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009127 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00009128 }
9129
9130 switch (IntNo) {
9131 case Intrinsic::arm_neon_vshifts:
9132 case Intrinsic::arm_neon_vshiftu:
9133 // Opcode already set above.
9134 break;
9135 case Intrinsic::arm_neon_vshiftls:
9136 case Intrinsic::arm_neon_vshiftlu:
9137 if (Cnt == VT.getVectorElementType().getSizeInBits())
9138 VShiftOpc = ARMISD::VSHLLi;
9139 else
9140 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9141 ARMISD::VSHLLs : ARMISD::VSHLLu);
9142 break;
9143 case Intrinsic::arm_neon_vshiftn:
9144 VShiftOpc = ARMISD::VSHRN; break;
9145 case Intrinsic::arm_neon_vrshifts:
9146 VShiftOpc = ARMISD::VRSHRs; break;
9147 case Intrinsic::arm_neon_vrshiftu:
9148 VShiftOpc = ARMISD::VRSHRu; break;
9149 case Intrinsic::arm_neon_vrshiftn:
9150 VShiftOpc = ARMISD::VRSHRN; break;
9151 case Intrinsic::arm_neon_vqshifts:
9152 VShiftOpc = ARMISD::VQSHLs; break;
9153 case Intrinsic::arm_neon_vqshiftu:
9154 VShiftOpc = ARMISD::VQSHLu; break;
9155 case Intrinsic::arm_neon_vqshiftsu:
9156 VShiftOpc = ARMISD::VQSHLsu; break;
9157 case Intrinsic::arm_neon_vqshiftns:
9158 VShiftOpc = ARMISD::VQSHRNs; break;
9159 case Intrinsic::arm_neon_vqshiftnu:
9160 VShiftOpc = ARMISD::VQSHRNu; break;
9161 case Intrinsic::arm_neon_vqshiftnsu:
9162 VShiftOpc = ARMISD::VQSHRNsu; break;
9163 case Intrinsic::arm_neon_vqrshiftns:
9164 VShiftOpc = ARMISD::VQRSHRNs; break;
9165 case Intrinsic::arm_neon_vqrshiftnu:
9166 VShiftOpc = ARMISD::VQRSHRNu; break;
9167 case Intrinsic::arm_neon_vqrshiftnsu:
9168 VShiftOpc = ARMISD::VQRSHRNsu; break;
9169 }
9170
9171 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009173 }
9174
9175 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00009176 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009177 int64_t Cnt;
9178 unsigned VShiftOpc = 0;
9179
9180 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9181 VShiftOpc = ARMISD::VSLI;
9182 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9183 VShiftOpc = ARMISD::VSRI;
9184 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00009185 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009186 }
9187
9188 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
9189 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009191 }
9192
9193 case Intrinsic::arm_neon_vqrshifts:
9194 case Intrinsic::arm_neon_vqrshiftu:
9195 // No immediate versions of these to check for.
9196 break;
9197 }
9198
9199 return SDValue();
9200}
9201
9202/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9203/// lowers them. As with the vector shift intrinsics, this is done during DAG
9204/// combining instead of DAG legalizing because the build_vectors for 64-bit
9205/// vector element shift counts are generally not legal, and it is hard to see
9206/// their values after they get legalized to loads from a constant pool.
9207static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9208 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00009209 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00009210 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9211 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9212 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9213 SDValue N1 = N->getOperand(1);
9214 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9215 SDValue N0 = N->getOperand(0);
9216 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9217 DAG.MaskedValueIsZero(N0.getOperand(0),
9218 APInt::getHighBitsSet(32, 16)))
9219 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
9220 }
9221 }
Bob Wilson5bafff32009-06-22 23:27:02 +00009222
9223 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00009224 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9225 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00009226 return SDValue();
9227
9228 assert(ST->hasNEON() && "unexpected vector shift");
9229 int64_t Cnt;
9230
9231 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009232 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009233
9234 case ISD::SHL:
9235 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9236 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009237 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009238 break;
9239
9240 case ISD::SRA:
9241 case ISD::SRL:
9242 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9243 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9244 ARMISD::VSHRs : ARMISD::VSHRu);
9245 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009246 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009247 }
9248 }
9249 return SDValue();
9250}
9251
9252/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9253/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9254static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9255 const ARMSubtarget *ST) {
9256 SDValue N0 = N->getOperand(0);
9257
9258 // Check for sign- and zero-extensions of vector extract operations of 8-
9259 // and 16-bit vector elements. NEON supports these directly. They are
9260 // handled during DAG combining because type legalization will promote them
9261 // to 32-bit types and it is messy to recognize the operations after that.
9262 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9263 SDValue Vec = N0.getOperand(0);
9264 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009265 EVT VT = N->getValueType(0);
9266 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009267 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9268
Owen Anderson825b72b2009-08-11 20:47:22 +00009269 if (VT == MVT::i32 &&
9270 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00009271 TLI.isTypeLegal(Vec.getValueType()) &&
9272 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009273
9274 unsigned Opc = 0;
9275 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009276 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009277 case ISD::SIGN_EXTEND:
9278 Opc = ARMISD::VGETLANEs;
9279 break;
9280 case ISD::ZERO_EXTEND:
9281 case ISD::ANY_EXTEND:
9282 Opc = ARMISD::VGETLANEu;
9283 break;
9284 }
9285 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9286 }
9287 }
9288
9289 return SDValue();
9290}
9291
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009292/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9293/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9294static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9295 const ARMSubtarget *ST) {
9296 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009297 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009298 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9299 // a NaN; only do the transformation when it matches that behavior.
9300
9301 // For now only do this when using NEON for FP operations; if using VFP, it
9302 // is not obvious that the benefit outweighs the cost of switching to the
9303 // NEON pipeline.
9304 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9305 N->getValueType(0) != MVT::f32)
9306 return SDValue();
9307
9308 SDValue CondLHS = N->getOperand(0);
9309 SDValue CondRHS = N->getOperand(1);
9310 SDValue LHS = N->getOperand(2);
9311 SDValue RHS = N->getOperand(3);
9312 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9313
9314 unsigned Opcode = 0;
9315 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009316 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009317 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009318 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009319 IsReversed = true ; // x CC y ? y : x
9320 } else {
9321 return SDValue();
9322 }
9323
Bob Wilsone742bb52010-02-24 22:15:53 +00009324 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009325 switch (CC) {
9326 default: break;
9327 case ISD::SETOLT:
9328 case ISD::SETOLE:
9329 case ISD::SETLT:
9330 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009331 case ISD::SETULT:
9332 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009333 // If LHS is NaN, an ordered comparison will be false and the result will
9334 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9335 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9336 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9337 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9338 break;
9339 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9340 // will return -0, so vmin can only be used for unsafe math or if one of
9341 // the operands is known to be nonzero.
9342 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009343 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009344 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9345 break;
9346 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009347 break;
9348
9349 case ISD::SETOGT:
9350 case ISD::SETOGE:
9351 case ISD::SETGT:
9352 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009353 case ISD::SETUGT:
9354 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009355 // If LHS is NaN, an ordered comparison will be false and the result will
9356 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9357 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9358 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9359 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9360 break;
9361 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9362 // will return +0, so vmax can only be used for unsafe math or if one of
9363 // the operands is known to be nonzero.
9364 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009365 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009366 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9367 break;
9368 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009369 break;
9370 }
9371
9372 if (!Opcode)
9373 return SDValue();
9374 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9375}
9376
Evan Chenge721f5c2011-07-13 00:42:17 +00009377/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9378SDValue
9379ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9380 SDValue Cmp = N->getOperand(4);
9381 if (Cmp.getOpcode() != ARMISD::CMPZ)
9382 // Only looking at EQ and NE cases.
9383 return SDValue();
9384
9385 EVT VT = N->getValueType(0);
9386 DebugLoc dl = N->getDebugLoc();
9387 SDValue LHS = Cmp.getOperand(0);
9388 SDValue RHS = Cmp.getOperand(1);
9389 SDValue FalseVal = N->getOperand(0);
9390 SDValue TrueVal = N->getOperand(1);
9391 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009392 ARMCC::CondCodes CC =
9393 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009394
9395 // Simplify
9396 // mov r1, r0
9397 // cmp r1, x
9398 // mov r0, y
9399 // moveq r0, x
9400 // to
9401 // cmp r0, x
9402 // movne r0, y
9403 //
9404 // mov r1, r0
9405 // cmp r1, x
9406 // mov r0, x
9407 // movne r0, y
9408 // to
9409 // cmp r0, x
9410 // movne r0, y
9411 /// FIXME: Turn this into a target neutral optimization?
9412 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009413 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009414 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9415 N->getOperand(3), Cmp);
9416 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9417 SDValue ARMcc;
9418 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9419 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9420 N->getOperand(3), NewCmp);
9421 }
9422
9423 if (Res.getNode()) {
9424 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009425 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009426 // Capture demanded bits information that would be otherwise lost.
9427 if (KnownZero == 0xfffffffe)
9428 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9429 DAG.getValueType(MVT::i1));
9430 else if (KnownZero == 0xffffff00)
9431 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9432 DAG.getValueType(MVT::i8));
9433 else if (KnownZero == 0xffff0000)
9434 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9435 DAG.getValueType(MVT::i16));
9436 }
9437
9438 return Res;
9439}
9440
Dan Gohman475871a2008-07-27 21:46:04 +00009441SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009442 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009443 switch (N->getOpcode()) {
9444 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009445 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009446 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009447 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009448 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009449 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009450 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9451 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009452 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009453 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009454 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009455 case ISD::STORE: return PerformSTORECombine(N, DCI);
9456 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9457 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009458 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009459 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009460 case ISD::FP_TO_SINT:
9461 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9462 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009463 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009464 case ISD::SHL:
9465 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009466 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009467 case ISD::SIGN_EXTEND:
9468 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009469 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9470 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009471 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009472 case ARMISD::VLD2DUP:
9473 case ARMISD::VLD3DUP:
9474 case ARMISD::VLD4DUP:
9475 return CombineBaseUpdate(N, DCI);
9476 case ISD::INTRINSIC_VOID:
9477 case ISD::INTRINSIC_W_CHAIN:
9478 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9479 case Intrinsic::arm_neon_vld1:
9480 case Intrinsic::arm_neon_vld2:
9481 case Intrinsic::arm_neon_vld3:
9482 case Intrinsic::arm_neon_vld4:
9483 case Intrinsic::arm_neon_vld2lane:
9484 case Intrinsic::arm_neon_vld3lane:
9485 case Intrinsic::arm_neon_vld4lane:
9486 case Intrinsic::arm_neon_vst1:
9487 case Intrinsic::arm_neon_vst2:
9488 case Intrinsic::arm_neon_vst3:
9489 case Intrinsic::arm_neon_vst4:
9490 case Intrinsic::arm_neon_vst2lane:
9491 case Intrinsic::arm_neon_vst3lane:
9492 case Intrinsic::arm_neon_vst4lane:
9493 return CombineBaseUpdate(N, DCI);
9494 default: break;
9495 }
9496 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009497 }
Dan Gohman475871a2008-07-27 21:46:04 +00009498 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009499}
9500
Evan Cheng31959b12011-02-02 01:06:55 +00009501bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9502 EVT VT) const {
9503 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9504}
9505
Evan Cheng376642e2012-12-10 23:21:26 +00009506bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009507 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009508 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009509
9510 switch (VT.getSimpleVT().SimpleTy) {
9511 default:
9512 return false;
9513 case MVT::i8:
9514 case MVT::i16:
Evan Cheng376642e2012-12-10 23:21:26 +00009515 case MVT::i32: {
Evan Chengd10eab02012-09-18 01:42:45 +00009516 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng376642e2012-12-10 23:21:26 +00009517 if (AllowsUnaligned) {
9518 if (Fast)
9519 *Fast = Subtarget->hasV7Ops();
9520 return true;
9521 }
9522 return false;
9523 }
Evan Chenga99c5082012-08-15 17:44:53 +00009524 case MVT::f64:
Evan Cheng376642e2012-12-10 23:21:26 +00009525 case MVT::v2f64: {
Evan Chengd10eab02012-09-18 01:42:45 +00009526 // For any little-endian targets with neon, we can support unaligned ld/st
9527 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9528 // A big-endian target may also explictly support unaligned accesses
Evan Cheng376642e2012-12-10 23:21:26 +00009529 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9530 if (Fast)
9531 *Fast = true;
9532 return true;
9533 }
9534 return false;
9535 }
Bill Wendlingaf566342009-08-15 21:21:19 +00009536 }
9537}
9538
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009539static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9540 unsigned AlignCheck) {
9541 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9542 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9543}
9544
9545EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9546 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00009547 bool IsMemset, bool ZeroMemset,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009548 bool MemcpyStrSrc,
9549 MachineFunction &MF) const {
9550 const Function *F = MF.getFunction();
9551
9552 // See if we can use NEON instructions for this...
Evan Cheng946a3a92012-12-12 02:34:41 +00009553 if ((!IsMemset || ZeroMemset) &&
Evan Cheng376642e2012-12-10 23:21:26 +00009554 Subtarget->hasNEON() &&
Bill Wendling831737d2012-12-30 10:32:01 +00009555 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9556 Attribute::NoImplicitFloat)) {
Evan Cheng376642e2012-12-10 23:21:26 +00009557 bool Fast;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009558 if (Size >= 16 &&
9559 (memOpAlign(SrcAlign, DstAlign, 16) ||
9560 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009561 return MVT::v2f64;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009562 } else if (Size >= 8 &&
9563 (memOpAlign(SrcAlign, DstAlign, 8) ||
9564 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009565 return MVT::f64;
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009566 }
9567 }
9568
Lang Hames5207bf22011-11-08 18:56:23 +00009569 // Lowering to i32/i16 if the size permits.
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009570 if (Size >= 4)
Lang Hames5207bf22011-11-08 18:56:23 +00009571 return MVT::i32;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009572 else if (Size >= 2)
Lang Hames5207bf22011-11-08 18:56:23 +00009573 return MVT::i16;
Lang Hames5207bf22011-11-08 18:56:23 +00009574
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009575 // Let the target-independent logic figure it out.
9576 return MVT::Other;
9577}
9578
Evan Cheng2766a472012-12-06 19:13:27 +00009579bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9580 if (Val.getOpcode() != ISD::LOAD)
9581 return false;
9582
9583 EVT VT1 = Val.getValueType();
9584 if (!VT1.isSimple() || !VT1.isInteger() ||
9585 !VT2.isSimple() || !VT2.isInteger())
9586 return false;
9587
9588 switch (VT1.getSimpleVT().SimpleTy) {
9589 default: break;
9590 case MVT::i1:
9591 case MVT::i8:
9592 case MVT::i16:
9593 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9594 return true;
9595 }
9596
9597 return false;
9598}
9599
Evan Chenge6c835f2009-08-14 20:09:37 +00009600static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9601 if (V < 0)
9602 return false;
9603
9604 unsigned Scale = 1;
9605 switch (VT.getSimpleVT().SimpleTy) {
9606 default: return false;
9607 case MVT::i1:
9608 case MVT::i8:
9609 // Scale == 1;
9610 break;
9611 case MVT::i16:
9612 // Scale == 2;
9613 Scale = 2;
9614 break;
9615 case MVT::i32:
9616 // Scale == 4;
9617 Scale = 4;
9618 break;
9619 }
9620
9621 if ((V & (Scale - 1)) != 0)
9622 return false;
9623 V /= Scale;
9624 return V == (V & ((1LL << 5) - 1));
9625}
9626
9627static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9628 const ARMSubtarget *Subtarget) {
9629 bool isNeg = false;
9630 if (V < 0) {
9631 isNeg = true;
9632 V = - V;
9633 }
9634
9635 switch (VT.getSimpleVT().SimpleTy) {
9636 default: return false;
9637 case MVT::i1:
9638 case MVT::i8:
9639 case MVT::i16:
9640 case MVT::i32:
9641 // + imm12 or - imm8
9642 if (isNeg)
9643 return V == (V & ((1LL << 8) - 1));
9644 return V == (V & ((1LL << 12) - 1));
9645 case MVT::f32:
9646 case MVT::f64:
9647 // Same as ARM mode. FIXME: NEON?
9648 if (!Subtarget->hasVFP2())
9649 return false;
9650 if ((V & 3) != 0)
9651 return false;
9652 V >>= 2;
9653 return V == (V & ((1LL << 8) - 1));
9654 }
9655}
9656
Evan Chengb01fad62007-03-12 23:30:29 +00009657/// isLegalAddressImmediate - Return true if the integer value can be used
9658/// as the offset of the target addressing mode for load / store of the
9659/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009660static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009661 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009662 if (V == 0)
9663 return true;
9664
Evan Cheng65011532009-03-09 19:15:00 +00009665 if (!VT.isSimple())
9666 return false;
9667
Evan Chenge6c835f2009-08-14 20:09:37 +00009668 if (Subtarget->isThumb1Only())
9669 return isLegalT1AddressImmediate(V, VT);
9670 else if (Subtarget->isThumb2())
9671 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009672
Evan Chenge6c835f2009-08-14 20:09:37 +00009673 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009674 if (V < 0)
9675 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009676 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009677 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009678 case MVT::i1:
9679 case MVT::i8:
9680 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009681 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009682 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009683 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009684 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009685 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009686 case MVT::f32:
9687 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009688 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009689 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009690 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009691 return false;
9692 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009693 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009694 }
Evan Chenga8e29892007-01-19 07:51:42 +00009695}
9696
Evan Chenge6c835f2009-08-14 20:09:37 +00009697bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9698 EVT VT) const {
9699 int Scale = AM.Scale;
9700 if (Scale < 0)
9701 return false;
9702
9703 switch (VT.getSimpleVT().SimpleTy) {
9704 default: return false;
9705 case MVT::i1:
9706 case MVT::i8:
9707 case MVT::i16:
9708 case MVT::i32:
9709 if (Scale == 1)
9710 return true;
9711 // r + r << imm
9712 Scale = Scale & ~1;
9713 return Scale == 2 || Scale == 4 || Scale == 8;
9714 case MVT::i64:
9715 // r + r
9716 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9717 return true;
9718 return false;
9719 case MVT::isVoid:
9720 // Note, we allow "void" uses (basically, uses that aren't loads or
9721 // stores), because arm allows folding a scale into many arithmetic
9722 // operations. This should be made more precise and revisited later.
9723
9724 // Allow r << imm, but the imm has to be a multiple of two.
9725 if (Scale & 1) return false;
9726 return isPowerOf2_32(Scale);
9727 }
9728}
9729
Chris Lattner37caf8c2007-04-09 23:33:39 +00009730/// isLegalAddressingMode - Return true if the addressing mode represented
9731/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009732bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009733 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009734 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009735 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009736 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009737
Chris Lattner37caf8c2007-04-09 23:33:39 +00009738 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009739 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009740 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009741
Chris Lattner37caf8c2007-04-09 23:33:39 +00009742 switch (AM.Scale) {
9743 case 0: // no scale reg, must be "r+i" or "r", or "i".
9744 break;
9745 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009746 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009747 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009748 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009749 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009750 // ARM doesn't support any R+R*scale+imm addr modes.
9751 if (AM.BaseOffs)
9752 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009753
Bob Wilson2c7dab12009-04-08 17:55:28 +00009754 if (!VT.isSimple())
9755 return false;
9756
Evan Chenge6c835f2009-08-14 20:09:37 +00009757 if (Subtarget->isThumb2())
9758 return isLegalT2ScaledAddressingMode(AM, VT);
9759
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009760 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009762 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009763 case MVT::i1:
9764 case MVT::i8:
9765 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009766 if (Scale < 0) Scale = -Scale;
9767 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009768 return true;
9769 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009770 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009771 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009772 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009773 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009774 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009775 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009776 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009777
Owen Anderson825b72b2009-08-11 20:47:22 +00009778 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009779 // Note, we allow "void" uses (basically, uses that aren't loads or
9780 // stores), because arm allows folding a scale into many arithmetic
9781 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009782
Chris Lattner37caf8c2007-04-09 23:33:39 +00009783 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009784 if (Scale & 1) return false;
9785 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009786 }
Evan Chengb01fad62007-03-12 23:30:29 +00009787 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009788 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009789}
9790
Evan Cheng77e47512009-11-11 19:05:52 +00009791/// isLegalICmpImmediate - Return true if the specified immediate is legal
9792/// icmp immediate, that is the target has icmp instructions which can compare
9793/// a register against the immediate without having to materialize the
9794/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009795bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009796 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009797 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009798 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009799 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009800 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009801 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009802 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009803}
9804
Andrew Trick8d8d9612012-07-18 18:34:27 +00009805/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9806/// *or sub* immediate, that is the target has add or sub instructions which can
9807/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009808/// immediate into a register.
9809bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009810 // Same encoding for add/sub, just flip the sign.
9811 int64_t AbsImm = llvm::abs64(Imm);
9812 if (!Subtarget->isThumb())
9813 return ARM_AM::getSOImmVal(AbsImm) != -1;
9814 if (Subtarget->isThumb2())
9815 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9816 // Thumb1 only has 8-bit unsigned immediate.
9817 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009818}
9819
Owen Andersone50ed302009-08-10 22:56:29 +00009820static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009821 bool isSEXTLoad, SDValue &Base,
9822 SDValue &Offset, bool &isInc,
9823 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009824 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9825 return false;
9826
Owen Anderson825b72b2009-08-11 20:47:22 +00009827 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009828 // AddressingMode 3
9829 Base = Ptr->getOperand(0);
9830 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009831 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009832 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009833 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009834 isInc = false;
9835 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9836 return true;
9837 }
9838 }
9839 isInc = (Ptr->getOpcode() == ISD::ADD);
9840 Offset = Ptr->getOperand(1);
9841 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009842 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009843 // AddressingMode 2
9844 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009845 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009846 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009847 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009848 isInc = false;
9849 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9850 Base = Ptr->getOperand(0);
9851 return true;
9852 }
9853 }
9854
9855 if (Ptr->getOpcode() == ISD::ADD) {
9856 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009857 ARM_AM::ShiftOpc ShOpcVal=
9858 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009859 if (ShOpcVal != ARM_AM::no_shift) {
9860 Base = Ptr->getOperand(1);
9861 Offset = Ptr->getOperand(0);
9862 } else {
9863 Base = Ptr->getOperand(0);
9864 Offset = Ptr->getOperand(1);
9865 }
9866 return true;
9867 }
9868
9869 isInc = (Ptr->getOpcode() == ISD::ADD);
9870 Base = Ptr->getOperand(0);
9871 Offset = Ptr->getOperand(1);
9872 return true;
9873 }
9874
Jim Grosbache5165492009-11-09 00:11:35 +00009875 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009876 return false;
9877}
9878
Owen Andersone50ed302009-08-10 22:56:29 +00009879static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009880 bool isSEXTLoad, SDValue &Base,
9881 SDValue &Offset, bool &isInc,
9882 SelectionDAG &DAG) {
9883 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9884 return false;
9885
9886 Base = Ptr->getOperand(0);
9887 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9888 int RHSC = (int)RHS->getZExtValue();
9889 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9890 assert(Ptr->getOpcode() == ISD::ADD);
9891 isInc = false;
9892 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9893 return true;
9894 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9895 isInc = Ptr->getOpcode() == ISD::ADD;
9896 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9897 return true;
9898 }
9899 }
9900
9901 return false;
9902}
9903
Evan Chenga8e29892007-01-19 07:51:42 +00009904/// getPreIndexedAddressParts - returns true by value, base pointer and
9905/// offset pointer and addressing mode by reference if the node's address
9906/// can be legally represented as pre-indexed load / store address.
9907bool
Dan Gohman475871a2008-07-27 21:46:04 +00009908ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9909 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009910 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009911 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009912 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009913 return false;
9914
Owen Andersone50ed302009-08-10 22:56:29 +00009915 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009916 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009917 bool isSEXTLoad = false;
9918 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9919 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009920 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009921 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9922 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9923 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009924 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009925 } else
9926 return false;
9927
9928 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009929 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009930 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009931 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9932 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009933 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009934 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009935 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009936 if (!isLegal)
9937 return false;
9938
9939 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9940 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009941}
9942
9943/// getPostIndexedAddressParts - returns true by value, base pointer and
9944/// offset pointer and addressing mode by reference if this node can be
9945/// combined with a load / store to form a post-indexed load / store.
9946bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009947 SDValue &Base,
9948 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009949 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009950 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009951 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009952 return false;
9953
Owen Andersone50ed302009-08-10 22:56:29 +00009954 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009955 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009956 bool isSEXTLoad = false;
9957 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009958 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009959 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009960 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9961 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009962 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009963 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009964 } else
9965 return false;
9966
9967 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009968 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009969 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009970 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009971 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009972 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009973 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9974 isInc, DAG);
9975 if (!isLegal)
9976 return false;
9977
Evan Cheng28dad2a2010-05-18 21:31:17 +00009978 if (Ptr != Base) {
9979 // Swap base ptr and offset to catch more post-index load / store when
9980 // it's legal. In Thumb2 mode, offset must be an immediate.
9981 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9982 !Subtarget->isThumb2())
9983 std::swap(Base, Offset);
9984
9985 // Post-indexed load / store update the base pointer.
9986 if (Ptr != Base)
9987 return false;
9988 }
9989
Evan Chenge88d5ce2009-07-02 07:28:31 +00009990 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9991 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009992}
9993
Dan Gohman475871a2008-07-27 21:46:04 +00009994void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009995 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009996 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009997 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009998 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009999 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +000010000 switch (Op.getOpcode()) {
10001 default: break;
10002 case ARMISD::CMOV: {
10003 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000010004 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +000010005 if (KnownZero == 0 && KnownOne == 0) return;
10006
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010007 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000010008 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +000010009 KnownZero &= KnownZeroRHS;
10010 KnownOne &= KnownOneRHS;
10011 return;
10012 }
10013 }
10014}
10015
10016//===----------------------------------------------------------------------===//
10017// ARM Inline Assembly Support
10018//===----------------------------------------------------------------------===//
10019
Evan Cheng55d42002011-01-08 01:24:27 +000010020bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10021 // Looking for "rev" which is V6+.
10022 if (!Subtarget->hasV6Ops())
10023 return false;
10024
10025 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10026 std::string AsmStr = IA->getAsmString();
10027 SmallVector<StringRef, 4> AsmPieces;
10028 SplitString(AsmStr, AsmPieces, ";\n");
10029
10030 switch (AsmPieces.size()) {
10031 default: return false;
10032 case 1:
10033 AsmStr = AsmPieces[0];
10034 AsmPieces.clear();
10035 SplitString(AsmStr, AsmPieces, " \t,");
10036
10037 // rev $0, $1
10038 if (AsmPieces.size() == 3 &&
10039 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10040 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010041 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +000010042 if (Ty && Ty->getBitWidth() == 32)
10043 return IntrinsicLowering::LowerToByteSwap(CI);
10044 }
10045 break;
10046 }
10047
10048 return false;
10049}
10050
Evan Chenga8e29892007-01-19 07:51:42 +000010051/// getConstraintType - Given a constraint letter, return the type of
10052/// constraint it is for this target.
10053ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010054ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10055 if (Constraint.size() == 1) {
10056 switch (Constraint[0]) {
10057 default: break;
10058 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010059 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +000010060 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010061 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010062 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +000010063 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +000010064 // An address with a single base register. Due to the way we
10065 // currently handle addresses it is the same as an 'r' memory constraint.
10066 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +000010067 }
Eric Christopher1312ca82011-06-21 22:10:57 +000010068 } else if (Constraint.size() == 2) {
10069 switch (Constraint[0]) {
10070 default: break;
10071 // All 'U+' constraints are addresses.
10072 case 'U': return C_Memory;
10073 }
Evan Chenga8e29892007-01-19 07:51:42 +000010074 }
Chris Lattner4234f572007-03-25 02:14:49 +000010075 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +000010076}
10077
John Thompson44ab89e2010-10-29 17:29:13 +000010078/// Examine constraint type and operand type and determine a weight value.
10079/// This object must already have been set up with the operand type
10080/// and the current alternative constraint selected.
10081TargetLowering::ConstraintWeight
10082ARMTargetLowering::getSingleConstraintMatchWeight(
10083 AsmOperandInfo &info, const char *constraint) const {
10084 ConstraintWeight weight = CW_Invalid;
10085 Value *CallOperandVal = info.CallOperandVal;
10086 // If we don't have a value, we can't do a match,
10087 // but allow it at the lowest weight.
10088 if (CallOperandVal == NULL)
10089 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010090 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +000010091 // Look at the constraint type.
10092 switch (*constraint) {
10093 default:
10094 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10095 break;
10096 case 'l':
10097 if (type->isIntegerTy()) {
10098 if (Subtarget->isThumb())
10099 weight = CW_SpecificReg;
10100 else
10101 weight = CW_Register;
10102 }
10103 break;
10104 case 'w':
10105 if (type->isFloatingPointTy())
10106 weight = CW_Register;
10107 break;
10108 }
10109 return weight;
10110}
10111
Eric Christopher35e6d4d2011-06-30 23:50:52 +000010112typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10113RCPair
Evan Chenga8e29892007-01-19 07:51:42 +000010114ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010115 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +000010116 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010117 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +000010118 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +000010119 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010120 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010121 return RCPair(0U, &ARM::tGPRRegClass);
10122 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +000010123 case 'h': // High regs or no regs.
10124 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010125 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +000010126 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010127 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +000010128 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010129 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +000010130 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010131 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +000010132 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010133 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +000010134 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010135 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010136 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010137 case 'x':
10138 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010139 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010140 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010141 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010142 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010143 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010144 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010145 case 't':
10146 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010147 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010148 break;
Evan Chenga8e29892007-01-19 07:51:42 +000010149 }
10150 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010151 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +000010152 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010153
Evan Chenga8e29892007-01-19 07:51:42 +000010154 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10155}
10156
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010157/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10158/// vector. If it is invalid, don't add anything to Ops.
10159void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000010160 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010161 std::vector<SDValue>&Ops,
10162 SelectionDAG &DAG) const {
10163 SDValue Result(0, 0);
10164
Eric Christopher100c8332011-06-02 23:16:42 +000010165 // Currently only support length 1 constraints.
10166 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000010167
Eric Christopher100c8332011-06-02 23:16:42 +000010168 char ConstraintLetter = Constraint[0];
10169 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010170 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +000010171 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010172 case 'I': case 'J': case 'K': case 'L':
10173 case 'M': case 'N': case 'O':
10174 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10175 if (!C)
10176 return;
10177
10178 int64_t CVal64 = C->getSExtValue();
10179 int CVal = (int) CVal64;
10180 // None of these constraints allow values larger than 32 bits. Check
10181 // that the value fits in an int.
10182 if (CVal != CVal64)
10183 return;
10184
Eric Christopher100c8332011-06-02 23:16:42 +000010185 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +000010186 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +000010187 // Constant suitable for movw, must be between 0 and
10188 // 65535.
10189 if (Subtarget->hasV6T2Ops())
10190 if (CVal >= 0 && CVal <= 65535)
10191 break;
10192 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010193 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010194 if (Subtarget->isThumb1Only()) {
10195 // This must be a constant between 0 and 255, for ADD
10196 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010197 if (CVal >= 0 && CVal <= 255)
10198 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010199 } else if (Subtarget->isThumb2()) {
10200 // A constant that can be used as an immediate value in a
10201 // data-processing instruction.
10202 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10203 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010204 } else {
10205 // A constant that can be used as an immediate value in a
10206 // data-processing instruction.
10207 if (ARM_AM::getSOImmVal(CVal) != -1)
10208 break;
10209 }
10210 return;
10211
10212 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010213 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010214 // This must be a constant between -255 and -1, for negated ADD
10215 // immediates. This can be used in GCC with an "n" modifier that
10216 // prints the negated value, for use with SUB instructions. It is
10217 // not useful otherwise but is implemented for compatibility.
10218 if (CVal >= -255 && CVal <= -1)
10219 break;
10220 } else {
10221 // This must be a constant between -4095 and 4095. It is not clear
10222 // what this constraint is intended for. Implemented for
10223 // compatibility with GCC.
10224 if (CVal >= -4095 && CVal <= 4095)
10225 break;
10226 }
10227 return;
10228
10229 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010230 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010231 // A 32-bit value where only one byte has a nonzero value. Exclude
10232 // zero to match GCC. This constraint is used by GCC internally for
10233 // constants that can be loaded with a move/shift combination.
10234 // It is not useful otherwise but is implemented for compatibility.
10235 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10236 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010237 } else if (Subtarget->isThumb2()) {
10238 // A constant whose bitwise inverse can be used as an immediate
10239 // value in a data-processing instruction. This can be used in GCC
10240 // with a "B" modifier that prints the inverted value, for use with
10241 // BIC and MVN instructions. It is not useful otherwise but is
10242 // implemented for compatibility.
10243 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10244 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010245 } else {
10246 // A constant whose bitwise inverse can be used as an immediate
10247 // value in a data-processing instruction. This can be used in GCC
10248 // with a "B" modifier that prints the inverted value, for use with
10249 // BIC and MVN instructions. It is not useful otherwise but is
10250 // implemented for compatibility.
10251 if (ARM_AM::getSOImmVal(~CVal) != -1)
10252 break;
10253 }
10254 return;
10255
10256 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010257 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010258 // This must be a constant between -7 and 7,
10259 // for 3-operand ADD/SUB immediate instructions.
10260 if (CVal >= -7 && CVal < 7)
10261 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010262 } else if (Subtarget->isThumb2()) {
10263 // A constant whose negation can be used as an immediate value in a
10264 // data-processing instruction. This can be used in GCC with an "n"
10265 // modifier that prints the negated value, for use with SUB
10266 // instructions. It is not useful otherwise but is implemented for
10267 // compatibility.
10268 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10269 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010270 } else {
10271 // A constant whose negation can be used as an immediate value in a
10272 // data-processing instruction. This can be used in GCC with an "n"
10273 // modifier that prints the negated value, for use with SUB
10274 // instructions. It is not useful otherwise but is implemented for
10275 // compatibility.
10276 if (ARM_AM::getSOImmVal(-CVal) != -1)
10277 break;
10278 }
10279 return;
10280
10281 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010282 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010283 // This must be a multiple of 4 between 0 and 1020, for
10284 // ADD sp + immediate.
10285 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10286 break;
10287 } else {
10288 // A power of two or a constant between 0 and 32. This is used in
10289 // GCC for the shift amount on shifted register operands, but it is
10290 // useful in general for any shift amounts.
10291 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10292 break;
10293 }
10294 return;
10295
10296 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010297 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010298 // This must be a constant between 0 and 31, for shift amounts.
10299 if (CVal >= 0 && CVal <= 31)
10300 break;
10301 }
10302 return;
10303
10304 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010305 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010306 // This must be a multiple of 4 between -508 and 508, for
10307 // ADD/SUB sp = sp + immediate.
10308 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10309 break;
10310 }
10311 return;
10312 }
10313 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10314 break;
10315 }
10316
10317 if (Result.getNode()) {
10318 Ops.push_back(Result);
10319 return;
10320 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010321 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010322}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010323
10324bool
10325ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10326 // The ARM target isn't yet aware of offsets.
10327 return false;
10328}
Evan Cheng39382422009-10-28 01:44:26 +000010329
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010330bool ARM::isBitFieldInvertedMask(unsigned v) {
10331 if (v == 0xffffffff)
10332 return 0;
10333 // there can be 1's on either or both "outsides", all the "inside"
10334 // bits must be 0's
10335 unsigned int lsb = 0, msb = 31;
10336 while (v & (1 << msb)) --msb;
10337 while (v & (1 << lsb)) ++lsb;
10338 for (unsigned int i = lsb; i <= msb; ++i) {
10339 if (v & (1 << i))
10340 return 0;
10341 }
10342 return 1;
10343}
10344
Evan Cheng39382422009-10-28 01:44:26 +000010345/// isFPImmLegal - Returns true if the target can instruction select the
10346/// specified FP immediate natively. If false, the legalizer will
10347/// materialize the FP immediate as a load from a constant pool.
10348bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10349 if (!Subtarget->hasVFP3())
10350 return false;
10351 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010352 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010353 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010354 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010355 return false;
10356}
Bob Wilson65ffec42010-09-21 17:56:22 +000010357
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010358/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010359/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10360/// specified in the intrinsic calls.
10361bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10362 const CallInst &I,
10363 unsigned Intrinsic) const {
10364 switch (Intrinsic) {
10365 case Intrinsic::arm_neon_vld1:
10366 case Intrinsic::arm_neon_vld2:
10367 case Intrinsic::arm_neon_vld3:
10368 case Intrinsic::arm_neon_vld4:
10369 case Intrinsic::arm_neon_vld2lane:
10370 case Intrinsic::arm_neon_vld3lane:
10371 case Intrinsic::arm_neon_vld4lane: {
10372 Info.opc = ISD::INTRINSIC_W_CHAIN;
10373 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010374 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010375 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10376 Info.ptrVal = I.getArgOperand(0);
10377 Info.offset = 0;
10378 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10379 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10380 Info.vol = false; // volatile loads with NEON intrinsics not supported
10381 Info.readMem = true;
10382 Info.writeMem = false;
10383 return true;
10384 }
10385 case Intrinsic::arm_neon_vst1:
10386 case Intrinsic::arm_neon_vst2:
10387 case Intrinsic::arm_neon_vst3:
10388 case Intrinsic::arm_neon_vst4:
10389 case Intrinsic::arm_neon_vst2lane:
10390 case Intrinsic::arm_neon_vst3lane:
10391 case Intrinsic::arm_neon_vst4lane: {
10392 Info.opc = ISD::INTRINSIC_VOID;
10393 // Conservatively set memVT to the entire set of vectors stored.
10394 unsigned NumElts = 0;
10395 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010396 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010397 if (!ArgTy->isVectorTy())
10398 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010399 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010400 }
10401 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10402 Info.ptrVal = I.getArgOperand(0);
10403 Info.offset = 0;
10404 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10405 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10406 Info.vol = false; // volatile stores with NEON intrinsics not supported
10407 Info.readMem = false;
10408 Info.writeMem = true;
10409 return true;
10410 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010411 case Intrinsic::arm_strexd: {
10412 Info.opc = ISD::INTRINSIC_W_CHAIN;
10413 Info.memVT = MVT::i64;
10414 Info.ptrVal = I.getArgOperand(2);
10415 Info.offset = 0;
10416 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010417 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010418 Info.readMem = false;
10419 Info.writeMem = true;
10420 return true;
10421 }
10422 case Intrinsic::arm_ldrexd: {
10423 Info.opc = ISD::INTRINSIC_W_CHAIN;
10424 Info.memVT = MVT::i64;
10425 Info.ptrVal = I.getArgOperand(0);
10426 Info.offset = 0;
10427 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010428 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010429 Info.readMem = true;
10430 Info.writeMem = false;
10431 return true;
10432 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010433 default:
10434 break;
10435 }
10436
10437 return false;
10438}