blob: 5d24e92f2357ec57f8dbdaead332e7c035bcd656 [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
Evan Chengc8e70452012-12-04 22:41:50 +0000567 // NEON does not have single instruction CTPOP for vectors with element
568 // types wider than 8-bits. However, custom lowering can leverage the
569 // v8i8/v16i8 vcnt instruction.
570 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
571 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
572 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
573 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
574
Jim Grosbachb302a4e2013-02-27 21:31:12 +0000575 // NEON only has FMA instructions as of VFP4.
576 if (!Subtarget->hasVFP4()) {
577 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
578 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
579 }
580
Bob Wilson1c3ef902011-02-07 17:43:21 +0000581 setTargetDAGCombine(ISD::INTRINSIC_VOID);
582 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000583 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
584 setTargetDAGCombine(ISD::SHL);
585 setTargetDAGCombine(ISD::SRL);
586 setTargetDAGCombine(ISD::SRA);
587 setTargetDAGCombine(ISD::SIGN_EXTEND);
588 setTargetDAGCombine(ISD::ZERO_EXTEND);
589 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000590 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000591 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000592 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000593 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
594 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000595 setTargetDAGCombine(ISD::FP_TO_SINT);
596 setTargetDAGCombine(ISD::FP_TO_UINT);
597 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000598
James Molloy873fd5f2012-02-20 09:24:05 +0000599 // It is legal to extload from v4i8 to v4i16 or v4i32.
600 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
601 MVT::v4i16, MVT::v2i16,
602 MVT::v2i32};
603 for (unsigned i = 0; i < 6; ++i) {
604 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
605 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
606 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
607 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000608 }
609
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000610 // ARM and Thumb2 support UMLAL/SMLAL.
611 if (!Subtarget->isThumb1Only())
612 setTargetDAGCombine(ISD::ADDC);
613
614
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000615 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000616
617 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000619
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000620 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000622
Evan Chenga8e29892007-01-19 07:51:42 +0000623 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000624 if (!Subtarget->isThumb1Only()) {
625 for (unsigned im = (unsigned)ISD::PRE_INC;
626 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setIndexedLoadAction(im, MVT::i1, Legal);
628 setIndexedLoadAction(im, MVT::i8, Legal);
629 setIndexedLoadAction(im, MVT::i16, Legal);
630 setIndexedLoadAction(im, MVT::i32, Legal);
631 setIndexedStoreAction(im, MVT::i1, Legal);
632 setIndexedStoreAction(im, MVT::i8, Legal);
633 setIndexedStoreAction(im, MVT::i16, Legal);
634 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000635 }
Evan Chenga8e29892007-01-19 07:51:42 +0000636 }
637
638 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000639 setOperationAction(ISD::MUL, MVT::i64, Expand);
640 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000641 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
643 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000644 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000645 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
646 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000647 setOperationAction(ISD::MULHS, MVT::i32, Expand);
648
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000649 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000650 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000651 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::SRL, MVT::i64, Custom);
653 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000654
Evan Cheng342e3162011-08-30 01:34:54 +0000655 if (!Subtarget->isThumb1Only()) {
656 // FIXME: We should do this for Thumb1 as well.
657 setOperationAction(ISD::ADDC, MVT::i32, Custom);
658 setOperationAction(ISD::ADDE, MVT::i32, Custom);
659 setOperationAction(ISD::SUBC, MVT::i32, Custom);
660 setOperationAction(ISD::SUBE, MVT::i32, Custom);
661 }
662
Evan Chenga8e29892007-01-19 07:51:42 +0000663 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000665 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000667 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000669
Chandler Carruth63974b22011-12-13 01:56:10 +0000670 // These just redirect to CTTZ and CTLZ on ARM.
671 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
672 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
673
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000674 // Only ARMv6 has BSWAP.
675 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000677
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000678 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
679 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
680 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000681 setOperationAction(ISD::SDIV, MVT::i32, Expand);
682 setOperationAction(ISD::UDIV, MVT::i32, Expand);
683 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::SREM, MVT::i32, Expand);
685 setOperationAction(ISD::UREM, MVT::i32, Expand);
686 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
687 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000688
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
690 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
691 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
692 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000693 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000694
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000695 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000696
Evan Chenga8e29892007-01-19 07:51:42 +0000697 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::VASTART, MVT::Other, Custom);
699 setOperationAction(ISD::VAARG, MVT::Other, Expand);
700 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
701 setOperationAction(ISD::VAEND, MVT::Other, Expand);
702 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
703 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000704
705 if (!Subtarget->isTargetDarwin()) {
706 // Non-Darwin platforms may return values in these registers via the
707 // personality function.
708 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
709 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
710 setExceptionPointerRegister(ARM::R0);
711 setExceptionSelectorRegister(ARM::R1);
712 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000713
Evan Cheng3a1588a2010-04-15 22:20:34 +0000714 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000715 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
716 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000717 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000718 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000719 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000720 // membarrier needs custom lowering; the rest are legal and handled
721 // normally.
722 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000723 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000724 // Custom lowering for 64-bit ops
725 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
726 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
727 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
728 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
729 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
Silviu Baranga35b3df62012-11-29 14:41:25 +0000730 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
731 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
732 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
733 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
734 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000735 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000736 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
737 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000738 } else {
739 // Set them all for expansion, which will force libcalls.
740 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000741 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000742 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000743 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000744 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000745 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000746 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000747 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000748 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000749 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000750 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000751 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000752 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000753 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000754 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
755 // Unordered/Monotonic case.
756 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
757 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000758 // Since the libcalls include locking, fold in the fences
759 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000760 }
Evan Chenga8e29892007-01-19 07:51:42 +0000761
Evan Cheng416941d2010-11-04 05:19:35 +0000762 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000763
Eli Friedmana2c6f452010-06-26 04:36:50 +0000764 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
765 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
767 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000768 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000770
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000771 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
772 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000773 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000774 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000775 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000776 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
777 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000778
779 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000781 if (Subtarget->isTargetDarwin()) {
782 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
783 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000784 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000785 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000786
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 setOperationAction(ISD::SETCC, MVT::i32, Expand);
788 setOperationAction(ISD::SETCC, MVT::f32, Expand);
789 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000790 setOperationAction(ISD::SELECT, MVT::i32, Custom);
791 setOperationAction(ISD::SELECT, MVT::f32, Custom);
792 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
794 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
795 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000796
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
798 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
799 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
800 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
801 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000802
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000803 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::FSIN, MVT::f64, Expand);
805 setOperationAction(ISD::FSIN, MVT::f32, Expand);
806 setOperationAction(ISD::FCOS, MVT::f32, Expand);
807 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000808 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
809 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setOperationAction(ISD::FREM, MVT::f64, Expand);
811 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000812 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
813 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
815 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000816 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 setOperationAction(ISD::FPOW, MVT::f64, Expand);
818 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000819
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000820 if (!Subtarget->hasVFP4()) {
821 setOperationAction(ISD::FMA, MVT::f64, Expand);
822 setOperationAction(ISD::FMA, MVT::f32, Expand);
823 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000824
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000825 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000826 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000827 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
828 if (Subtarget->hasVFP2()) {
829 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
830 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
831 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
832 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
833 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000834 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000835 if (!Subtarget->hasFP16()) {
836 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
837 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000838 }
Evan Cheng110cf482008-04-01 01:50:16 +0000839 }
Evan Chenga8e29892007-01-19 07:51:42 +0000840
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000841 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000842 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000843 setTargetDAGCombine(ISD::ADD);
844 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000845 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000846 setTargetDAGCombine(ISD::AND);
847 setTargetDAGCombine(ISD::OR);
848 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000849
Evan Cheng5fb468a2012-02-23 02:58:19 +0000850 if (Subtarget->hasV6Ops())
851 setTargetDAGCombine(ISD::SRL);
852
Evan Chenga8e29892007-01-19 07:51:42 +0000853 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000854
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000855 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
856 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000857 setSchedulingPreference(Sched::RegPressure);
858 else
859 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000860
Evan Cheng05219282011-01-06 06:52:41 +0000861 //// temporary - rewrite interface to use type
Jim Grosbach3450f802013-02-20 21:13:59 +0000862 MaxStoresPerMemset = 8;
863 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
864 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
865 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
866 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
867 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengf6799392010-06-26 01:52:05 +0000868
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000869 // On ARM arguments smaller than 4 bytes are extended, so all arguments
870 // are at least 4 bytes aligned.
871 setMinStackArgumentAlignment(4);
872
Jim Grosbach3450f802013-02-20 21:13:59 +0000873 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000874
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000875 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach3450f802013-02-20 21:13:59 +0000876 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000877
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000878 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000879}
880
Andrew Trick32cec0a2011-01-19 02:35:27 +0000881// FIXME: It might make sense to define the representative register class as the
882// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
883// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
884// SPR's representative would be DPR_VFP2. This should work well if register
885// pressure tracking were modified such that a register use would increment the
886// pressure of the register class's representative and all of it's super
887// classes' representatives transitively. We have not implemented this because
888// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000889// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000890// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000891std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +0000892ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Cheng4f6b4672010-07-21 06:09:07 +0000893 const TargetRegisterClass *RRC = 0;
894 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +0000895 switch (VT.SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000896 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000897 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000898 // Use DPR as representative register class for all floating point
899 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
900 // the cost is 1 for both f32 and f64.
901 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000902 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000903 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000904 // When NEON is used for SP, only half of the register file is available
905 // because operations that define both SP and DP results will be constrained
906 // to the VFP2 class (D0-D15). We currently model this constraint prior to
907 // coalescing by double-counting the SP regs. See the FIXME above.
908 if (Subtarget->useNEONForSinglePrecisionFP())
909 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000910 break;
911 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
912 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000913 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000914 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000915 break;
916 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000917 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000918 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000919 break;
920 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000921 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000922 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000923 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000924 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000925 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000926}
927
Evan Chenga8e29892007-01-19 07:51:42 +0000928const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
929 switch (Opcode) {
930 default: return 0;
931 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000932 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000933 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000934 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
935 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000936 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000937 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
938 case ARMISD::tCALL: return "ARMISD::tCALL";
939 case ARMISD::BRCOND: return "ARMISD::BRCOND";
940 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000941 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000942 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
943 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
944 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000945 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000946 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000947 case ARMISD::CMPFP: return "ARMISD::CMPFP";
948 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000949 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000950 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000951
Evan Chenga8e29892007-01-19 07:51:42 +0000952 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000953
Jim Grosbach3482c802010-01-18 19:58:49 +0000954 case ARMISD::RBIT: return "ARMISD::RBIT";
955
Bob Wilson76a312b2010-03-19 22:51:32 +0000956 case ARMISD::FTOSI: return "ARMISD::FTOSI";
957 case ARMISD::FTOUI: return "ARMISD::FTOUI";
958 case ARMISD::SITOF: return "ARMISD::SITOF";
959 case ARMISD::UITOF: return "ARMISD::UITOF";
960
Evan Chenga8e29892007-01-19 07:51:42 +0000961 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
962 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
963 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000964
Evan Cheng342e3162011-08-30 01:34:54 +0000965 case ARMISD::ADDC: return "ARMISD::ADDC";
966 case ARMISD::ADDE: return "ARMISD::ADDE";
967 case ARMISD::SUBC: return "ARMISD::SUBC";
968 case ARMISD::SUBE: return "ARMISD::SUBE";
969
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000970 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
971 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000972
Evan Chengc5942082009-10-28 06:55:03 +0000973 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
974 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
975
Dale Johannesen51e28e62010-06-03 21:09:53 +0000976 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000977
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000978 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000979
Evan Cheng86198642009-08-07 00:34:42 +0000980 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
981
Jim Grosbach3728e962009-12-10 00:11:09 +0000982 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000983 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000984
Evan Chengdfed19f2010-11-03 06:34:55 +0000985 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
986
Bob Wilson5bafff32009-06-22 23:27:02 +0000987 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000988 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000989 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000990 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
991 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000992 case ARMISD::VCGEU: return "ARMISD::VCGEU";
993 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000994 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
995 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000996 case ARMISD::VCGTU: return "ARMISD::VCGTU";
997 case ARMISD::VTST: return "ARMISD::VTST";
998
999 case ARMISD::VSHL: return "ARMISD::VSHL";
1000 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1001 case ARMISD::VSHRu: return "ARMISD::VSHRu";
1002 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
1003 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
1004 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
1005 case ARMISD::VSHRN: return "ARMISD::VSHRN";
1006 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1007 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1008 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1009 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1010 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1011 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1012 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1013 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1014 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1015 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1016 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1017 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1018 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1019 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +00001020 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +00001021 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +00001022 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +00001023 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +00001024 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +00001025 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +00001026 case ARMISD::VREV64: return "ARMISD::VREV64";
1027 case ARMISD::VREV32: return "ARMISD::VREV32";
1028 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001029 case ARMISD::VZIP: return "ARMISD::VZIP";
1030 case ARMISD::VUZP: return "ARMISD::VUZP";
1031 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +00001032 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1033 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +00001034 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1035 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00001036 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1037 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001038 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001039 case ARMISD::FMAX: return "ARMISD::FMAX";
1040 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001041 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001042 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1043 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001044 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001045 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1046 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1047 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001048 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1049 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1050 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1051 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1052 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1053 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1054 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1055 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1056 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1057 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1058 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1059 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1060 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1061 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1062 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1063 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1064 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001065 }
1066}
1067
Duncan Sands28b77e92011-09-06 19:07:46 +00001068EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1069 if (!VT.isVector()) return getPointerTy();
1070 return VT.changeVectorElementTypeToInteger();
1071}
1072
Evan Cheng06b666c2010-05-15 02:18:07 +00001073/// getRegClassFor - Return the register class that should be used for the
1074/// specified value type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001075const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001076 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1077 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1078 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001079 if (Subtarget->hasNEON()) {
1080 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001081 return &ARM::QQPRRegClass;
1082 if (VT == MVT::v8i64)
1083 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001084 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001085 return TargetLowering::getRegClassFor(VT);
1086}
1087
Eric Christopherab695882010-07-21 22:26:11 +00001088// Create a fast isel object.
1089FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001090ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1091 const TargetLibraryInfo *libInfo) const {
1092 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001093}
1094
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001095/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1096/// be used for loads / stores from the global.
1097unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1098 return (Subtarget->isThumb1Only() ? 127 : 4095);
1099}
1100
Evan Cheng1cc39842010-05-20 23:26:43 +00001101Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001102 unsigned NumVals = N->getNumValues();
1103 if (!NumVals)
1104 return Sched::RegPressure;
1105
1106 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001107 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001108 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001109 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001110 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001111 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001112 }
Evan Chengc10f5432010-05-28 23:25:23 +00001113
1114 if (!N->isMachineOpcode())
1115 return Sched::RegPressure;
1116
1117 // Load are scheduled for latency even if there instruction itinerary
1118 // is not available.
1119 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001120 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001121
Evan Chenge837dea2011-06-28 19:10:37 +00001122 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001123 return Sched::RegPressure;
1124 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001125 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001126 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001127
Evan Cheng1cc39842010-05-20 23:26:43 +00001128 return Sched::RegPressure;
1129}
1130
Evan Chenga8e29892007-01-19 07:51:42 +00001131//===----------------------------------------------------------------------===//
1132// Lowering Code
1133//===----------------------------------------------------------------------===//
1134
Evan Chenga8e29892007-01-19 07:51:42 +00001135/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1136static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1137 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001138 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001139 case ISD::SETNE: return ARMCC::NE;
1140 case ISD::SETEQ: return ARMCC::EQ;
1141 case ISD::SETGT: return ARMCC::GT;
1142 case ISD::SETGE: return ARMCC::GE;
1143 case ISD::SETLT: return ARMCC::LT;
1144 case ISD::SETLE: return ARMCC::LE;
1145 case ISD::SETUGT: return ARMCC::HI;
1146 case ISD::SETUGE: return ARMCC::HS;
1147 case ISD::SETULT: return ARMCC::LO;
1148 case ISD::SETULE: return ARMCC::LS;
1149 }
1150}
1151
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001152/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1153static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001154 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001155 CondCode2 = ARMCC::AL;
1156 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001157 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001158 case ISD::SETEQ:
1159 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1160 case ISD::SETGT:
1161 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1162 case ISD::SETGE:
1163 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1164 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001165 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001166 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1167 case ISD::SETO: CondCode = ARMCC::VC; break;
1168 case ISD::SETUO: CondCode = ARMCC::VS; break;
1169 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1170 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1171 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1172 case ISD::SETLT:
1173 case ISD::SETULT: CondCode = ARMCC::LT; break;
1174 case ISD::SETLE:
1175 case ISD::SETULE: CondCode = ARMCC::LE; break;
1176 case ISD::SETNE:
1177 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1178 }
Evan Chenga8e29892007-01-19 07:51:42 +00001179}
1180
Bob Wilson1f595bb2009-04-17 19:07:39 +00001181//===----------------------------------------------------------------------===//
1182// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001183//===----------------------------------------------------------------------===//
1184
1185#include "ARMGenCallingConv.inc"
1186
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001187/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1188/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001189CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001190 bool Return,
1191 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001192 switch (CC) {
1193 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001194 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001195 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001196 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001197 if (!Subtarget->isAAPCS_ABI())
1198 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1199 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1200 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1201 }
1202 // Fallthrough
1203 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001204 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001205 if (!Subtarget->isAAPCS_ABI())
1206 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1207 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001208 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1209 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001210 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1211 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1212 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001213 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001214 if (!isVarArg)
1215 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1216 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001217 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001218 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001219 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001220 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001221 case CallingConv::GHC:
1222 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001223 }
1224}
1225
Dan Gohman98ca4f22009-08-05 01:29:28 +00001226/// LowerCallResult - Lower the result values of a call into the
1227/// appropriate copies out of appropriate physical registers.
1228SDValue
1229ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001230 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001231 const SmallVectorImpl<ISD::InputArg> &Ins,
1232 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001233 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001234
Bob Wilson1f595bb2009-04-17 19:07:39 +00001235 // Assign locations to each value returned by this call.
1236 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001237 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1238 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001239 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001240 CCAssignFnForNode(CallConv, /* Return*/ true,
1241 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001242
1243 // Copy all of the result registers out of their specified physreg.
1244 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1245 CCValAssign VA = RVLocs[i];
1246
Bob Wilson80915242009-04-25 00:33:20 +00001247 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001248 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001249 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001251 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001252 Chain = Lo.getValue(1);
1253 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001256 InFlag);
1257 Chain = Hi.getValue(1);
1258 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001259 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001260
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 if (VA.getLocVT() == MVT::v2f64) {
1262 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1263 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1264 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001265
1266 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001268 Chain = Lo.getValue(1);
1269 InFlag = Lo.getValue(2);
1270 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001271 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001272 Chain = Hi.getValue(1);
1273 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001274 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001275 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1276 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001277 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001278 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001279 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1280 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001281 Chain = Val.getValue(1);
1282 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283 }
Bob Wilson80915242009-04-25 00:33:20 +00001284
1285 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001286 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001287 case CCValAssign::Full: break;
1288 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001289 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001290 break;
1291 }
1292
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001294 }
1295
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001297}
1298
Bob Wilsondee46d72009-04-17 20:35:10 +00001299/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001300SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1302 SDValue StackPtr, SDValue Arg,
1303 DebugLoc dl, SelectionDAG &DAG,
1304 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001305 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001306 unsigned LocMemOffset = VA.getLocMemOffset();
1307 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1308 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001309 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001310 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001311 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001312}
1313
Dan Gohman98ca4f22009-08-05 01:29:28 +00001314void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001315 SDValue Chain, SDValue &Arg,
1316 RegsToPassVector &RegsToPass,
1317 CCValAssign &VA, CCValAssign &NextVA,
1318 SDValue &StackPtr,
1319 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001320 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001321
Jim Grosbache5165492009-11-09 00:11:35 +00001322 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001324 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1325
1326 if (NextVA.isRegLoc())
1327 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1328 else {
1329 assert(NextVA.isMemLoc());
1330 if (StackPtr.getNode() == 0)
1331 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1332
Dan Gohman98ca4f22009-08-05 01:29:28 +00001333 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1334 dl, DAG, NextVA,
1335 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001336 }
1337}
1338
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001340/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1341/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001343ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001344 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001345 SelectionDAG &DAG = CLI.DAG;
1346 DebugLoc &dl = CLI.DL;
1347 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1348 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1349 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1350 SDValue Chain = CLI.Chain;
1351 SDValue Callee = CLI.Callee;
1352 bool &isTailCall = CLI.IsTailCall;
1353 CallingConv::ID CallConv = CLI.CallConv;
1354 bool doesNotRet = CLI.DoesNotReturn;
1355 bool isVarArg = CLI.IsVarArg;
1356
Dale Johannesen51e28e62010-06-03 21:09:53 +00001357 MachineFunction &MF = DAG.getMachineFunction();
1358 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1359 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001360 // Disable tail calls if they're not supported.
1361 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001362 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001363 if (isTailCall) {
1364 // Check if it's really possible to do a tail call.
1365 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1366 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001367 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001368 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1369 // detected sibcalls.
1370 if (isTailCall) {
1371 ++NumTailCalls;
1372 IsSibCall = true;
1373 }
1374 }
Evan Chenga8e29892007-01-19 07:51:42 +00001375
Bob Wilson1f595bb2009-04-17 19:07:39 +00001376 // Analyze operands of the call, assigning locations to each operand.
1377 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001378 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1379 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001381 CCAssignFnForNode(CallConv, /* Return*/ false,
1382 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001383
Bob Wilson1f595bb2009-04-17 19:07:39 +00001384 // Get a count of how many bytes are to be pushed on the stack.
1385 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001386
Dale Johannesen51e28e62010-06-03 21:09:53 +00001387 // For tail calls, memory operands are available in our caller's stack.
1388 if (IsSibCall)
1389 NumBytes = 0;
1390
Evan Chenga8e29892007-01-19 07:51:42 +00001391 // Adjust the stack pointer for the new arguments...
1392 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001393 if (!IsSibCall)
1394 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001395
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001396 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001397
Bob Wilson5bafff32009-06-22 23:27:02 +00001398 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001399 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001400
Bob Wilson1f595bb2009-04-17 19:07:39 +00001401 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001402 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001403 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1404 i != e;
1405 ++i, ++realArgIdx) {
1406 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001407 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001408 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001409 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001410
Bob Wilson1f595bb2009-04-17 19:07:39 +00001411 // Promote the value if needed.
1412 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001413 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001414 case CCValAssign::Full: break;
1415 case CCValAssign::SExt:
1416 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1417 break;
1418 case CCValAssign::ZExt:
1419 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1420 break;
1421 case CCValAssign::AExt:
1422 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1423 break;
1424 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001425 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001426 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001427 }
1428
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001429 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001430 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001431 if (VA.getLocVT() == MVT::v2f64) {
1432 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1433 DAG.getConstant(0, MVT::i32));
1434 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1435 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001436
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001438 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1439
1440 VA = ArgLocs[++i]; // skip ahead to next loc
1441 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001443 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1444 } else {
1445 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001446
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1448 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001449 }
1450 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001452 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001453 }
1454 } else if (VA.isRegLoc()) {
1455 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001456 } else if (isByVal) {
1457 assert(VA.isMemLoc());
1458 unsigned offset = 0;
1459
1460 // True if this byval aggregate will be split between registers
1461 // and memory.
1462 if (CCInfo.isFirstByValRegValid()) {
1463 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1464 unsigned int i, j;
1465 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1466 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1467 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1468 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1469 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001470 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001471 MemOpChains.push_back(Load.getValue(1));
1472 RegsToPass.push_back(std::make_pair(j, Load));
1473 }
1474 offset = ARM::R4 - CCInfo.getFirstByValReg();
1475 CCInfo.clearFirstByValReg();
1476 }
1477
Manman Ren763a75d2012-06-01 02:44:42 +00001478 if (Flags.getByValSize() - 4*offset > 0) {
1479 unsigned LocMemOffset = VA.getLocMemOffset();
1480 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1481 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1482 StkPtrOff);
1483 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1484 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1485 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1486 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001487 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001488
Manman Ren763a75d2012-06-01 02:44:42 +00001489 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001490 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001491 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1492 Ops, array_lengthof(Ops)));
1493 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001494 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001495 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001496
Dan Gohman98ca4f22009-08-05 01:29:28 +00001497 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1498 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001499 }
Evan Chenga8e29892007-01-19 07:51:42 +00001500 }
1501
1502 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001504 &MemOpChains[0], MemOpChains.size());
1505
1506 // Build a sequence of copy-to-reg nodes chained together with token chain
1507 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001509 // Tail call byval lowering might overwrite argument registers so in case of
1510 // tail call optimization the copies to registers are lowered later.
1511 if (!isTailCall)
1512 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1513 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1514 RegsToPass[i].second, InFlag);
1515 InFlag = Chain.getValue(1);
1516 }
Evan Chenga8e29892007-01-19 07:51:42 +00001517
Dale Johannesen51e28e62010-06-03 21:09:53 +00001518 // For tail calls lower the arguments to the 'real' stack slot.
1519 if (isTailCall) {
1520 // Force all the incoming stack arguments to be loaded from the stack
1521 // before any new outgoing arguments are stored to the stack, because the
1522 // outgoing stack slots may alias the incoming argument stack slots, and
1523 // the alias isn't otherwise explicit. This is slightly more conservative
1524 // than necessary, because it means that each store effectively depends
1525 // on every argument instead of just those arguments it would clobber.
1526
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001527 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001528 InFlag = SDValue();
1529 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1530 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1531 RegsToPass[i].second, InFlag);
1532 InFlag = Chain.getValue(1);
1533 }
1534 InFlag =SDValue();
1535 }
1536
Bill Wendling056292f2008-09-16 21:48:12 +00001537 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1538 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1539 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001540 bool isDirect = false;
1541 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001542 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001543 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001544
1545 if (EnableARMLongCalls) {
1546 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1547 && "long-calls with non-static relocation model!");
1548 // Handle a global address or an external symbol. If it's not one of
1549 // those, the target's already in a register, so we don't need to do
1550 // anything extra.
1551 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001552 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001553 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001554 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001555 ARMConstantPoolValue *CPV =
1556 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1557
Jim Grosbache7b52522010-04-14 22:28:31 +00001558 // Get the address of the callee into a register
1559 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1560 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1561 Callee = DAG.getLoad(getPointerTy(), dl,
1562 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001563 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001564 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001565 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1566 const char *Sym = S->getSymbol();
1567
1568 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001569 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001570 ARMConstantPoolValue *CPV =
1571 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1572 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001573 // Get the address of the callee into a register
1574 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1575 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1576 Callee = DAG.getLoad(getPointerTy(), dl,
1577 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001578 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001579 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001580 }
1581 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001582 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001583 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001584 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001585 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001586 getTargetMachine().getRelocationModel() != Reloc::Static;
1587 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001588 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001589 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001590 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001591 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001592 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001593 ARMConstantPoolValue *CPV =
1594 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001595 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001597 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001598 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001599 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001600 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001601 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001602 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001603 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001604 } else {
1605 // On ELF targets for PIC code, direct calls should go through the PLT
1606 unsigned OpFlags = 0;
1607 if (Subtarget->isTargetELF() &&
Chad Rosiera6ca7032013-02-28 19:16:42 +00001608 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach637d89f2010-09-22 23:27:36 +00001609 OpFlags = ARMII::MO_PLT;
1610 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1611 }
Bill Wendling056292f2008-09-16 21:48:12 +00001612 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001613 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001614 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001615 getTargetMachine().getRelocationModel() != Reloc::Static;
1616 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001617 // tBX takes a register source operand.
1618 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001619 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001620 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001621 ARMConstantPoolValue *CPV =
1622 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1623 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001624 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001625 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001626 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001627 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001628 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001629 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001630 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001631 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001632 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001633 } else {
1634 unsigned OpFlags = 0;
1635 // On ELF targets for PIC code, direct calls should go through the PLT
1636 if (Subtarget->isTargetELF() &&
1637 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1638 OpFlags = ARMII::MO_PLT;
1639 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1640 }
Evan Chenga8e29892007-01-19 07:51:42 +00001641 }
1642
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001643 // FIXME: handle tail calls differently.
1644 unsigned CallOpc;
Bill Wendling831737d2012-12-30 10:32:01 +00001645 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1646 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001647 if (Subtarget->isThumb()) {
1648 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001649 CallOpc = ARMISD::CALL_NOLINK;
1650 else
1651 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1652 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001653 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001654 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001655 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001656 // Emit regular call when code size is the priority
1657 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001658 // "mov lr, pc; b _foo" to avoid confusing the RSP
1659 CallOpc = ARMISD::CALL_NOLINK;
1660 else
1661 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001662 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001663
Dan Gohman475871a2008-07-27 21:46:04 +00001664 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001665 Ops.push_back(Chain);
1666 Ops.push_back(Callee);
1667
1668 // Add argument registers to the end of the list so that they are known live
1669 // into the call.
1670 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1671 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1672 RegsToPass[i].second.getValueType()));
1673
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001674 // Add a register mask operand representing the call-preserved registers.
1675 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1676 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1677 assert(Mask && "Missing call preserved mask for calling convention");
1678 Ops.push_back(DAG.getRegisterMask(Mask));
1679
Gabor Greifba36cb52008-08-28 21:40:38 +00001680 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001681 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001682
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001683 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001684 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001685 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001686
Duncan Sands4bdcb612008-07-02 17:40:58 +00001687 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001688 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001689 InFlag = Chain.getValue(1);
1690
Chris Lattnere563bbc2008-10-11 22:08:30 +00001691 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1692 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001693 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001694 InFlag = Chain.getValue(1);
1695
Bob Wilson1f595bb2009-04-17 19:07:39 +00001696 // Handle result values, copying them out of physregs into vregs that we
1697 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001698 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1699 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001700}
1701
Stuart Hastingsf222e592011-02-28 17:17:53 +00001702/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001703/// on the stack. Remember the next parameter register to allocate,
1704/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001705/// this.
1706void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001707ARMTargetLowering::HandleByVal(
1708 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001709 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1710 assert((State->getCallOrPrologue() == Prologue ||
1711 State->getCallOrPrologue() == Call) &&
1712 "unhandled ParmContext");
1713 if ((!State->isFirstByValRegValid()) &&
1714 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001715 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1716 unsigned AlignInRegs = Align / 4;
1717 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1718 for (unsigned i = 0; i < Waste; ++i)
1719 reg = State->AllocateReg(GPRArgRegs, 4);
1720 }
1721 if (reg != 0) {
1722 State->setFirstByValReg(reg);
1723 // At a call site, a byval parameter that is split between
1724 // registers and memory needs its size truncated here. In a
1725 // function prologue, such byval parameters are reassembled in
1726 // memory, and are not truncated.
1727 if (State->getCallOrPrologue() == Call) {
1728 unsigned excess = 4 * (ARM::R4 - reg);
1729 assert(size >= excess && "expected larger existing stack allocation");
1730 size -= excess;
1731 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001732 }
1733 }
1734 // Confiscate any remaining parameter registers to preclude their
1735 // assignment to subsequent parameters.
1736 while (State->AllocateReg(GPRArgRegs, 4))
1737 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001738}
1739
Dale Johannesen51e28e62010-06-03 21:09:53 +00001740/// MatchingStackOffset - Return true if the given stack call argument is
1741/// already available in the same position (relatively) of the caller's
1742/// incoming argument stack.
1743static
1744bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1745 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001746 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001747 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1748 int FI = INT_MAX;
1749 if (Arg.getOpcode() == ISD::CopyFromReg) {
1750 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001751 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001752 return false;
1753 MachineInstr *Def = MRI->getVRegDef(VR);
1754 if (!Def)
1755 return false;
1756 if (!Flags.isByVal()) {
1757 if (!TII->isLoadFromStackSlot(Def, FI))
1758 return false;
1759 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001760 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001761 }
1762 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1763 if (Flags.isByVal())
1764 // ByVal argument is passed in as a pointer but it's now being
1765 // dereferenced. e.g.
1766 // define @foo(%struct.X* %A) {
1767 // tail call @bar(%struct.X* byval %A)
1768 // }
1769 return false;
1770 SDValue Ptr = Ld->getBasePtr();
1771 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1772 if (!FINode)
1773 return false;
1774 FI = FINode->getIndex();
1775 } else
1776 return false;
1777
1778 assert(FI != INT_MAX);
1779 if (!MFI->isFixedObjectIndex(FI))
1780 return false;
1781 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1782}
1783
1784/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1785/// for tail call optimization. Targets which want to do tail call
1786/// optimization should implement this function.
1787bool
1788ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1789 CallingConv::ID CalleeCC,
1790 bool isVarArg,
1791 bool isCalleeStructRet,
1792 bool isCallerStructRet,
1793 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001794 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001795 const SmallVectorImpl<ISD::InputArg> &Ins,
1796 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001797 const Function *CallerF = DAG.getMachineFunction().getFunction();
1798 CallingConv::ID CallerCC = CallerF->getCallingConv();
1799 bool CCMatch = CallerCC == CalleeCC;
1800
1801 // Look for obvious safe cases to perform tail call optimization that do not
1802 // require ABI changes. This is what gcc calls sibcall.
1803
Jim Grosbach7616b642010-06-16 23:45:49 +00001804 // Do not sibcall optimize vararg calls unless the call site is not passing
1805 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001806 if (isVarArg && !Outs.empty())
1807 return false;
1808
1809 // Also avoid sibcall optimization if either caller or callee uses struct
1810 // return semantics.
1811 if (isCalleeStructRet || isCallerStructRet)
1812 return false;
1813
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001814 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001815 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1816 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1817 // support in the assembler and linker to be used. This would need to be
1818 // fixed to fully support tail calls in Thumb1.
1819 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001820 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1821 // LR. This means if we need to reload LR, it takes an extra instructions,
1822 // which outweighs the value of the tail call; but here we don't know yet
1823 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001824 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001825 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001826
1827 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1828 // but we need to make sure there are enough registers; the only valid
1829 // registers are the 4 used for parameters. We don't currently do this
1830 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001831 if (Subtarget->isThumb1Only())
1832 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001833
Dale Johannesen51e28e62010-06-03 21:09:53 +00001834 // If the calling conventions do not match, then we'd better make sure the
1835 // results are returned in the same way as what the caller expects.
1836 if (!CCMatch) {
1837 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001838 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1839 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001840 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1841
1842 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001843 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1844 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001845 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1846
1847 if (RVLocs1.size() != RVLocs2.size())
1848 return false;
1849 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1850 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1851 return false;
1852 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1853 return false;
1854 if (RVLocs1[i].isRegLoc()) {
1855 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1856 return false;
1857 } else {
1858 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1859 return false;
1860 }
1861 }
1862 }
1863
Manman Rene6c3cc82012-10-12 23:39:43 +00001864 // If Caller's vararg or byval argument has been split between registers and
1865 // stack, do not perform tail call, since part of the argument is in caller's
1866 // local frame.
1867 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1868 getInfo<ARMFunctionInfo>();
1869 if (AFI_Caller->getVarArgsRegSaveSize())
1870 return false;
1871
Dale Johannesen51e28e62010-06-03 21:09:53 +00001872 // If the callee takes no arguments then go on to check the results of the
1873 // call.
1874 if (!Outs.empty()) {
1875 // Check if stack adjustment is needed. For now, do not do this if any
1876 // argument is passed on the stack.
1877 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001878 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1879 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001880 CCInfo.AnalyzeCallOperands(Outs,
1881 CCAssignFnForNode(CalleeCC, false, isVarArg));
1882 if (CCInfo.getNextStackOffset()) {
1883 MachineFunction &MF = DAG.getMachineFunction();
1884
1885 // Check if the arguments are already laid out in the right way as
1886 // the caller's fixed stack objects.
1887 MachineFrameInfo *MFI = MF.getFrameInfo();
1888 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001889 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001890 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1891 i != e;
1892 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001893 CCValAssign &VA = ArgLocs[i];
1894 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001895 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001896 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001897 if (VA.getLocInfo() == CCValAssign::Indirect)
1898 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001899 if (VA.needsCustom()) {
1900 // f64 and vector types are split into multiple registers or
1901 // register/stack-slot combinations. The types will not match
1902 // the registers; give up on memory f64 refs until we figure
1903 // out what to do about this.
1904 if (!VA.isRegLoc())
1905 return false;
1906 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001907 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001908 if (RegVT == MVT::v2f64) {
1909 if (!ArgLocs[++i].isRegLoc())
1910 return false;
1911 if (!ArgLocs[++i].isRegLoc())
1912 return false;
1913 }
1914 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001915 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1916 MFI, MRI, TII))
1917 return false;
1918 }
1919 }
1920 }
1921 }
1922
1923 return true;
1924}
1925
Benjamin Kramer350c0082012-11-28 20:55:10 +00001926bool
1927ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1928 MachineFunction &MF, bool isVarArg,
1929 const SmallVectorImpl<ISD::OutputArg> &Outs,
1930 LLVMContext &Context) const {
1931 SmallVector<CCValAssign, 16> RVLocs;
1932 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1933 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1934 isVarArg));
1935}
1936
Dan Gohman98ca4f22009-08-05 01:29:28 +00001937SDValue
1938ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001939 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001940 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001941 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001942 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001943
Bob Wilsondee46d72009-04-17 20:35:10 +00001944 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001945 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001946
Bob Wilsondee46d72009-04-17 20:35:10 +00001947 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001948 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1949 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001950
Dan Gohman98ca4f22009-08-05 01:29:28 +00001951 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001952 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1953 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001954
Bob Wilson1f595bb2009-04-17 19:07:39 +00001955 SDValue Flag;
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001956 SmallVector<SDValue, 4> RetOps;
1957 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Bob Wilson1f595bb2009-04-17 19:07:39 +00001958
1959 // Copy the result values into the output registers.
1960 for (unsigned i = 0, realRVLocIdx = 0;
1961 i != RVLocs.size();
1962 ++i, ++realRVLocIdx) {
1963 CCValAssign &VA = RVLocs[i];
1964 assert(VA.isRegLoc() && "Can only return in registers!");
1965
Dan Gohmanc9403652010-07-07 15:54:55 +00001966 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001967
1968 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001969 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001970 case CCValAssign::Full: break;
1971 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001972 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001973 break;
1974 }
1975
Bob Wilson1f595bb2009-04-17 19:07:39 +00001976 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001978 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1980 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001981 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001983
1984 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1985 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001986 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00001987 VA = RVLocs[++i]; // skip ahead to next loc
1988 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1989 HalfGPRs.getValue(1), Flag);
1990 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001991 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00001992 VA = RVLocs[++i]; // skip ahead to next loc
1993
1994 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1996 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001997 }
1998 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1999 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00002000 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002002 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00002003 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002004 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002005 VA = RVLocs[++i]; // skip ahead to next loc
2006 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
2007 Flag);
2008 } else
2009 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2010
Bob Wilsondee46d72009-04-17 20:35:10 +00002011 // Guarantee that all emitted copies are
2012 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002013 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002014 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002015 }
2016
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002017 // Update chain and glue.
2018 RetOps[0] = Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002019 if (Flag.getNode())
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002020 RetOps.push_back(Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002021
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002022 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2023 RetOps.data(), RetOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002024}
2025
Evan Chengbf010eb2012-04-10 01:51:00 +00002026bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002027 if (N->getNumValues() != 1)
2028 return false;
2029 if (!N->hasNUsesOfValue(1, 0))
2030 return false;
2031
Evan Chengbf010eb2012-04-10 01:51:00 +00002032 SDValue TCChain = Chain;
2033 SDNode *Copy = *N->use_begin();
2034 if (Copy->getOpcode() == ISD::CopyToReg) {
2035 // If the copy has a glue operand, we conservatively assume it isn't safe to
2036 // perform a tail call.
2037 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2038 return false;
2039 TCChain = Copy->getOperand(0);
2040 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2041 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002042 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002043 SmallPtrSet<SDNode*, 2> Copies;
2044 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002045 UI != UE; ++UI) {
2046 if (UI->getOpcode() != ISD::CopyToReg)
2047 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002048 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002049 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002050 if (Copies.size() > 2)
2051 return false;
2052
2053 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2054 UI != UE; ++UI) {
2055 SDValue UseChain = UI->getOperand(0);
2056 if (Copies.count(UseChain.getNode()))
2057 // Second CopyToReg
2058 Copy = *UI;
2059 else
2060 // First CopyToReg
2061 TCChain = UseChain;
2062 }
2063 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002064 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002065 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002066 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002067 Copy = *Copy->use_begin();
2068 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002069 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002070 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002071 } else {
2072 return false;
2073 }
2074
Evan Cheng1bf891a2010-12-01 22:59:46 +00002075 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002076 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2077 UI != UE; ++UI) {
2078 if (UI->getOpcode() != ARMISD::RET_FLAG)
2079 return false;
2080 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002081 }
2082
Evan Chengbf010eb2012-04-10 01:51:00 +00002083 if (!HasRet)
2084 return false;
2085
2086 Chain = TCChain;
2087 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002088}
2089
Evan Cheng485fafc2011-03-21 01:19:09 +00002090bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002091 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002092 return false;
2093
2094 if (!CI->isTailCall())
2095 return false;
2096
2097 return !Subtarget->isThumb1Only();
2098}
2099
Bob Wilsonb62d2572009-11-03 00:02:05 +00002100// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2101// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2102// one of the above mentioned nodes. It has to be wrapped because otherwise
2103// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2104// be used to form addressing mode. These wrapped nodes will be selected
2105// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002106static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002107 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002108 // FIXME there is no actual debug info here
2109 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002110 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002112 if (CP->isMachineConstantPoolEntry())
2113 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2114 CP->getAlignment());
2115 else
2116 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2117 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002118 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002119}
2120
Jim Grosbache1102ca2010-07-19 17:20:38 +00002121unsigned ARMTargetLowering::getJumpTableEncoding() const {
2122 return MachineJumpTableInfo::EK_Inline;
2123}
2124
Dan Gohmand858e902010-04-17 15:26:15 +00002125SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2126 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002127 MachineFunction &MF = DAG.getMachineFunction();
2128 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2129 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002130 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002131 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002132 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002133 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2134 SDValue CPAddr;
2135 if (RelocM == Reloc::Static) {
2136 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2137 } else {
2138 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002139 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002140 ARMConstantPoolValue *CPV =
2141 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2142 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002143 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2144 }
2145 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2146 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002147 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002148 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002149 if (RelocM == Reloc::Static)
2150 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002152 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002153}
2154
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002155// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002156SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002157ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002158 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002159 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002160 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002161 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002162 MachineFunction &MF = DAG.getMachineFunction();
2163 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002164 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002165 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002166 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2167 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002168 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002169 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002170 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002171 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002172 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002173 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174
Evan Chenge7e0d622009-11-06 22:24:13 +00002175 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002176 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002177
2178 // call __tls_get_addr.
2179 ArgListTy Args;
2180 ArgListEntry Entry;
2181 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002182 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002183 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002184 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002185 TargetLowering::CallLoweringInfo CLI(Chain,
2186 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002187 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002188 0, CallingConv::C, /*isTailCall=*/false,
2189 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002190 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002191 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002192 return CallResult.first;
2193}
2194
2195// Lower ISD::GlobalTLSAddress using the "initial exec" or
2196// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002197SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002198ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002199 SelectionDAG &DAG,
2200 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002201 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002202 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002203 SDValue Offset;
2204 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002205 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002206 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002207 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002208
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002209 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002210 MachineFunction &MF = DAG.getMachineFunction();
2211 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002212 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002213 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002214 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2215 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002216 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2217 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2218 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002219 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002221 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002222 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002223 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002224 Chain = Offset.getValue(1);
2225
Evan Chenge7e0d622009-11-06 22:24:13 +00002226 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002227 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002228
Evan Cheng9eda6892009-10-31 03:39:36 +00002229 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002230 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002231 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002232 } else {
2233 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002234 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002235 ARMConstantPoolValue *CPV =
2236 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002237 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002238 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002239 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002240 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002241 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002242 }
2243
2244 // The address of the thread local variable is the add of the thread
2245 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002246 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002247}
2248
Dan Gohman475871a2008-07-27 21:46:04 +00002249SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002250ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002251 // TODO: implement the "local dynamic" model
2252 assert(Subtarget->isTargetELF() &&
2253 "TLS not implemented for non-ELF targets");
2254 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002255
2256 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2257
2258 switch (model) {
2259 case TLSModel::GeneralDynamic:
2260 case TLSModel::LocalDynamic:
2261 return LowerToTLSGeneralDynamicModel(GA, DAG);
2262 case TLSModel::InitialExec:
2263 case TLSModel::LocalExec:
2264 return LowerToTLSExecModels(GA, DAG, model);
2265 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002266 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002267}
2268
Dan Gohman475871a2008-07-27 21:46:04 +00002269SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002270 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002271 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002272 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002273 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosiera6ca7032013-02-28 19:16:42 +00002274 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002275 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002276 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002277 ARMConstantPoolConstant::Create(GV,
2278 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002279 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002280 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002281 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002282 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002283 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002284 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002285 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002286 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002287 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002288 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002289 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002290 MachinePointerInfo::getGOT(),
2291 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002292 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002293 }
2294
2295 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002296 // pair. This is always cheaper.
2297 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002298 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002299 // FIXME: Once remat is capable of dealing with instructions with register
2300 // operands, expand this into two nodes.
2301 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2302 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002303 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002304 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2305 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2306 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2307 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002308 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002309 }
2310}
2311
Dan Gohman475871a2008-07-27 21:46:04 +00002312SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002313 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002314 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002315 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002316 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002317 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002318
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002319 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2320 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002321 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002322 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002323 // FIXME: Once remat is capable of dealing with instructions with register
2324 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002325 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002326 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2327 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2328
Evan Cheng53519f02011-01-21 18:55:51 +00002329 unsigned Wrapper = (RelocM == Reloc::PIC_)
2330 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2331 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002332 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002333 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2334 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002335 MachinePointerInfo::getGOT(),
2336 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002337 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002338 }
2339
2340 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002341 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002342 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002343 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002344 } else {
Chad Rosiera6ca7032013-02-28 19:16:42 +00002345 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002346 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002347 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2348 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002349 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2350 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002351 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002352 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002354
Evan Cheng9eda6892009-10-31 03:39:36 +00002355 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002356 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002357 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002358 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002359
2360 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002361 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002362 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002363 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002364
Evan Cheng63476a82009-09-03 07:04:02 +00002365 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002366 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002367 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002368
2369 return Result;
2370}
2371
Dan Gohman475871a2008-07-27 21:46:04 +00002372SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002373 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002374 assert(Subtarget->isTargetELF() &&
2375 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002376 MachineFunction &MF = DAG.getMachineFunction();
2377 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002378 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002379 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002380 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002381 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002382 ARMConstantPoolValue *CPV =
2383 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2384 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002385 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002386 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002387 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002388 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002389 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002390 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002391 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002392}
2393
Jim Grosbach0e0da732009-05-12 23:59:14 +00002394SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002395ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2396 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002397 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002398 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2399 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002400 Op.getOperand(1), Val);
2401}
2402
2403SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002404ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2405 DebugLoc dl = Op.getDebugLoc();
2406 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2407 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2408}
2409
2410SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002411ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002412 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002413 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002414 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002415 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002416 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002417 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002418 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002419 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2420 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002421 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002422 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002423 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002424 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002425 EVT PtrVT = getPointerTy();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002426 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2427 SDValue CPAddr;
2428 unsigned PCAdj = (RelocM != Reloc::PIC_)
2429 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002430 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002431 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2432 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002433 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002434 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002435 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002436 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002437 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002438 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002439
2440 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002441 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002442 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2443 }
2444 return Result;
2445 }
Evan Cheng92e39162011-03-29 23:06:19 +00002446 case Intrinsic::arm_neon_vmulls:
2447 case Intrinsic::arm_neon_vmullu: {
2448 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2449 ? ARMISD::VMULLs : ARMISD::VMULLu;
2450 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2451 Op.getOperand(1), Op.getOperand(2));
2452 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002453 }
2454}
2455
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002456static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002457 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002458 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002459 if (!Subtarget->hasDataBarrier()) {
2460 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2461 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2462 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002463 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002464 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002465 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002466 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002467 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002468
2469 SDValue Op5 = Op.getOperand(5);
2470 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2471 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2472 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2473 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2474
2475 ARM_MB::MemBOpt DMBOpt;
2476 if (isDeviceBarrier)
2477 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2478 else
2479 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2480 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2481 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002482}
2483
Eli Friedman26689ac2011-08-03 21:06:02 +00002484
2485static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2486 const ARMSubtarget *Subtarget) {
2487 // FIXME: handle "fence singlethread" more efficiently.
2488 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002489 if (!Subtarget->hasDataBarrier()) {
2490 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2491 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2492 // here.
2493 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2494 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002495 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002496 DAG.getConstant(0, MVT::i32));
2497 }
2498
Eli Friedman26689ac2011-08-03 21:06:02 +00002499 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002500 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002501}
2502
Evan Chengdfed19f2010-11-03 06:34:55 +00002503static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2504 const ARMSubtarget *Subtarget) {
2505 // ARM pre v5TE and Thumb1 does not have preload instructions.
2506 if (!(Subtarget->isThumb2() ||
2507 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2508 // Just preserve the chain.
2509 return Op.getOperand(0);
2510
2511 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002512 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2513 if (!isRead &&
2514 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2515 // ARMv7 with MP extension has PLDW.
2516 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002517
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002518 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2519 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002520 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002521 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002522 isData = ~isData & 1;
2523 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002524
2525 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002526 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2527 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002528}
2529
Dan Gohman1e93df62010-04-17 14:41:14 +00002530static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2531 MachineFunction &MF = DAG.getMachineFunction();
2532 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2533
Evan Chenga8e29892007-01-19 07:51:42 +00002534 // vastart just stores the address of the VarArgsFrameIndex slot into the
2535 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002536 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002537 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002538 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002539 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002540 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2541 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002542}
2543
Dan Gohman475871a2008-07-27 21:46:04 +00002544SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002545ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2546 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002547 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002548 MachineFunction &MF = DAG.getMachineFunction();
2549 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2550
Craig Topper44d23822012-02-22 05:59:10 +00002551 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002552 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002553 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002554 else
Craig Topper420761a2012-04-20 07:30:17 +00002555 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002556
2557 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002558 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002559 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002560
2561 SDValue ArgValue2;
2562 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002563 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002564 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002565
2566 // Create load node to retrieve arguments from the stack.
2567 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002568 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002569 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002570 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002571 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002572 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002573 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002574 }
2575
Jim Grosbache5165492009-11-09 00:11:35 +00002576 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002577}
2578
Stuart Hastingsc7315872011-04-20 16:47:52 +00002579void
2580ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2581 unsigned &VARegSize, unsigned &VARegSaveSize)
2582 const {
2583 unsigned NumGPRs;
2584 if (CCInfo.isFirstByValRegValid())
2585 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2586 else {
2587 unsigned int firstUnalloced;
2588 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2589 sizeof(GPRArgRegs) /
2590 sizeof(GPRArgRegs[0]));
2591 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2592 }
2593
2594 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2595 VARegSize = NumGPRs * 4;
2596 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2597}
2598
2599// The remaining GPRs hold either the beginning of variable-argument
David Peixottoe68542e2013-02-13 00:36:35 +00002600// data, or the beginning of an aggregate passed by value (usually
Stuart Hastingsc7315872011-04-20 16:47:52 +00002601// byval). Either way, we allocate stack slots adjacent to the data
2602// provided by our caller, and store the unallocated registers there.
2603// If this is a variadic function, the va_list pointer will begin with
2604// these values; otherwise, this reassembles a (byval) structure that
2605// was split between registers and memory.
2606void
2607ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2608 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002609 const Value *OrigArg,
2610 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002611 unsigned ArgOffset,
2612 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002613 MachineFunction &MF = DAG.getMachineFunction();
2614 MachineFrameInfo *MFI = MF.getFrameInfo();
2615 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2616 unsigned firstRegToSaveIndex;
2617 if (CCInfo.isFirstByValRegValid())
2618 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2619 else {
2620 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2621 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2622 }
2623
2624 unsigned VARegSize, VARegSaveSize;
2625 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2626 if (VARegSaveSize) {
2627 // If this function is vararg, store any remaining integer argument regs
2628 // to their spots on the stack so that they may be loaded by deferencing
2629 // the result of va_next.
2630 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002631 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2632 ArgOffset + VARegSaveSize
2633 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002634 false));
2635 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2636 getPointerTy());
2637
2638 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002639 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002640 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002641 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002642 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002643 else
Craig Topper420761a2012-04-20 07:30:17 +00002644 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002645
2646 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2647 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2648 SDValue Store =
2649 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002650 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002651 false, false, 0);
2652 MemOps.push_back(Store);
2653 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2654 DAG.getConstant(4, getPointerTy()));
2655 }
2656 if (!MemOps.empty())
2657 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2658 &MemOps[0], MemOps.size());
2659 } else
2660 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002661 AFI->setVarArgsFrameIndex(
2662 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002663}
2664
Bob Wilson5bafff32009-06-22 23:27:02 +00002665SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002666ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002667 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002668 const SmallVectorImpl<ISD::InputArg>
2669 &Ins,
2670 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002671 SmallVectorImpl<SDValue> &InVals)
2672 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002673 MachineFunction &MF = DAG.getMachineFunction();
2674 MachineFrameInfo *MFI = MF.getFrameInfo();
2675
Bob Wilson1f595bb2009-04-17 19:07:39 +00002676 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2677
2678 // Assign locations to all of the incoming arguments.
2679 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002680 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2681 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002682 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002683 CCAssignFnForNode(CallConv, /* Return*/ false,
2684 isVarArg));
Jim Grosbach7ccf4632013-03-02 20:16:15 +00002685
Bob Wilson1f595bb2009-04-17 19:07:39 +00002686 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002687 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002688 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002689 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2690 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002691 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2692 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002693 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2694 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002695 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002696 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002697 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002698
Bob Wilson1f595bb2009-04-17 19:07:39 +00002699 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002700 // f64 and vector types are split up into multiple registers or
2701 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002702 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002703 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002704 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002705 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002706 SDValue ArgValue2;
2707 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002708 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002709 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2710 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002711 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002712 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002713 } else {
2714 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2715 Chain, DAG, dl);
2716 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002717 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2718 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002719 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002720 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002721 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2722 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002723 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002724
Bob Wilson5bafff32009-06-22 23:27:02 +00002725 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002726 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002727
Owen Anderson825b72b2009-08-11 20:47:22 +00002728 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002729 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002730 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002731 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002732 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002733 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002734 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002735 RC = AFI->isThumb1OnlyFunction() ?
2736 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2737 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002738 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002739 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002740
2741 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002742 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002743 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002744 }
2745
2746 // If this is an 8 or 16-bit value, it is really passed promoted
2747 // to 32 bits. Insert an assert[sz]ext to capture this, then
2748 // truncate to the right size.
2749 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002750 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002751 case CCValAssign::Full: break;
2752 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002753 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002754 break;
2755 case CCValAssign::SExt:
2756 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2757 DAG.getValueType(VA.getValVT()));
2758 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2759 break;
2760 case CCValAssign::ZExt:
2761 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2762 DAG.getValueType(VA.getValVT()));
2763 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2764 break;
2765 }
2766
Dan Gohman98ca4f22009-08-05 01:29:28 +00002767 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002768
2769 } else { // VA.isRegLoc()
2770
2771 // sanity check
2772 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002773 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002774
Stuart Hastingsf222e592011-02-28 17:17:53 +00002775 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002776
Stuart Hastingsf222e592011-02-28 17:17:53 +00002777 // Some Ins[] entries become multiple ArgLoc[] entries.
2778 // Process them only once.
2779 if (index != lastInsIndex)
2780 {
2781 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002782 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002783 // This can be changed with more analysis.
2784 // In case of tail call optimization mark all arguments mutable.
2785 // Since they could be overwritten by lowering of arguments in case of
2786 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002787 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002788 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2789 if (!AFI->getVarArgsFrameIndex()) {
2790 VarArgStyleRegisters(CCInfo, DAG,
2791 dl, Chain, CurOrigArg,
2792 Ins[VA.getValNo()].PartOffset,
2793 VA.getLocMemOffset(),
2794 true /*force mutable frames*/);
2795 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2796 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2797 } else {
2798 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2799 VA.getLocMemOffset(), false);
Jim Grosbach7ccf4632013-03-02 20:16:15 +00002800 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002801 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002802 } else {
2803 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2804 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002805
Stuart Hastingsf222e592011-02-28 17:17:53 +00002806 // Create load nodes to retrieve arguments from the stack.
2807 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2808 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2809 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002810 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002811 }
2812 lastInsIndex = index;
2813 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002814 }
2815 }
2816
2817 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002818 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002819 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2820 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002821
Dan Gohman98ca4f22009-08-05 01:29:28 +00002822 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002823}
2824
2825/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002826static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002827 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002828 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002829 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002830 // Maybe this has already been legalized into the constant pool?
2831 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002832 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002833 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002834 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002835 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002836 }
2837 }
2838 return false;
2839}
2840
Evan Chenga8e29892007-01-19 07:51:42 +00002841/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2842/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002843SDValue
2844ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002845 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002846 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002847 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002848 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002849 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002850 // Constant does not fit, try adjusting it by one?
2851 switch (CC) {
2852 default: break;
2853 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002854 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002855 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002856 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002857 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002858 }
2859 break;
2860 case ISD::SETULT:
2861 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002862 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002863 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002864 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002865 }
2866 break;
2867 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002868 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002869 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002870 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002871 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002872 }
2873 break;
2874 case ISD::SETULE:
2875 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002876 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002877 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002878 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002879 }
2880 break;
2881 }
2882 }
2883 }
2884
2885 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002886 ARMISD::NodeType CompareType;
2887 switch (CondCode) {
2888 default:
2889 CompareType = ARMISD::CMP;
2890 break;
2891 case ARMCC::EQ:
2892 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002893 // Uses only Z Flag
2894 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002895 break;
2896 }
Evan Cheng218977b2010-07-13 19:27:42 +00002897 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002898 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002899}
2900
2901/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002902SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002903ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002904 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002905 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002906 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002907 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002908 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002909 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2910 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002911}
2912
Bob Wilson79f56c92011-03-08 01:17:20 +00002913/// duplicateCmp - Glue values can have only one use, so this function
2914/// duplicates a comparison node.
2915SDValue
2916ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2917 unsigned Opc = Cmp.getOpcode();
2918 DebugLoc DL = Cmp.getDebugLoc();
2919 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2920 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2921
2922 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2923 Cmp = Cmp.getOperand(0);
2924 Opc = Cmp.getOpcode();
2925 if (Opc == ARMISD::CMPFP)
2926 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2927 else {
2928 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2929 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2930 }
2931 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2932}
2933
Bill Wendlingde2b1512010-08-11 08:43:16 +00002934SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2935 SDValue Cond = Op.getOperand(0);
2936 SDValue SelectTrue = Op.getOperand(1);
2937 SDValue SelectFalse = Op.getOperand(2);
2938 DebugLoc dl = Op.getDebugLoc();
2939
2940 // Convert:
2941 //
2942 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2943 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2944 //
2945 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2946 const ConstantSDNode *CMOVTrue =
2947 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2948 const ConstantSDNode *CMOVFalse =
2949 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2950
2951 if (CMOVTrue && CMOVFalse) {
2952 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2953 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2954
2955 SDValue True;
2956 SDValue False;
2957 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2958 True = SelectTrue;
2959 False = SelectFalse;
2960 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2961 True = SelectFalse;
2962 False = SelectTrue;
2963 }
2964
2965 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002966 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002967 SDValue ARMcc = Cond.getOperand(2);
2968 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002969 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002970 assert(True.getValueType() == VT);
2971 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002972 }
2973 }
2974 }
2975
Dan Gohmandb953892012-02-24 00:09:36 +00002976 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2977 // undefined bits before doing a full-word comparison with zero.
2978 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2979 DAG.getConstant(1, Cond.getValueType()));
2980
Bill Wendlingde2b1512010-08-11 08:43:16 +00002981 return DAG.getSelectCC(dl, Cond,
2982 DAG.getConstant(0, Cond.getValueType()),
2983 SelectTrue, SelectFalse, ISD::SETNE);
2984}
2985
Dan Gohmand858e902010-04-17 15:26:15 +00002986SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002987 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002988 SDValue LHS = Op.getOperand(0);
2989 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002990 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002991 SDValue TrueVal = Op.getOperand(2);
2992 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002993 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002994
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002996 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002997 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002998 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002999 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003000 }
3001
3002 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003003 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00003004
Evan Cheng218977b2010-07-13 19:27:42 +00003005 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3006 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00003008 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00003009 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003010 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003011 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00003012 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00003013 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003014 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00003015 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00003016 }
3017 return Result;
3018}
3019
Evan Cheng218977b2010-07-13 19:27:42 +00003020/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3021/// to morph to an integer compare sequence.
3022static bool canChangeToInt(SDValue Op, bool &SeenZero,
3023 const ARMSubtarget *Subtarget) {
3024 SDNode *N = Op.getNode();
3025 if (!N->hasOneUse())
3026 // Otherwise it requires moving the value from fp to integer registers.
3027 return false;
3028 if (!N->getNumValues())
3029 return false;
3030 EVT VT = Op.getValueType();
3031 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3032 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3033 // vmrs are very slow, e.g. cortex-a8.
3034 return false;
3035
3036 if (isFloatingPointZero(Op)) {
3037 SeenZero = true;
3038 return true;
3039 }
3040 return ISD::isNormalLoad(N);
3041}
3042
3043static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3044 if (isFloatingPointZero(Op))
3045 return DAG.getConstant(0, MVT::i32);
3046
3047 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3048 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003049 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003050 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003051 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003052
3053 llvm_unreachable("Unknown VFP cmp argument!");
3054}
3055
3056static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3057 SDValue &RetVal1, SDValue &RetVal2) {
3058 if (isFloatingPointZero(Op)) {
3059 RetVal1 = DAG.getConstant(0, MVT::i32);
3060 RetVal2 = DAG.getConstant(0, MVT::i32);
3061 return;
3062 }
3063
3064 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3065 SDValue Ptr = Ld->getBasePtr();
3066 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3067 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003068 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003069 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003070 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003071
3072 EVT PtrType = Ptr.getValueType();
3073 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3074 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3075 PtrType, Ptr, DAG.getConstant(4, PtrType));
3076 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3077 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003078 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003079 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003080 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003081 return;
3082 }
3083
3084 llvm_unreachable("Unknown VFP cmp argument!");
3085}
3086
3087/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3088/// f32 and even f64 comparisons to integer ones.
3089SDValue
3090ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3091 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003092 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003093 SDValue LHS = Op.getOperand(2);
3094 SDValue RHS = Op.getOperand(3);
3095 SDValue Dest = Op.getOperand(4);
3096 DebugLoc dl = Op.getDebugLoc();
3097
Evan Chengfc501a32012-03-01 23:27:13 +00003098 bool LHSSeenZero = false;
3099 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3100 bool RHSSeenZero = false;
3101 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3102 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003103 // If unsafe fp math optimization is enabled and there are no other uses of
3104 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003105 // to an integer comparison.
3106 if (CC == ISD::SETOEQ)
3107 CC = ISD::SETEQ;
3108 else if (CC == ISD::SETUNE)
3109 CC = ISD::SETNE;
3110
Evan Chengfc501a32012-03-01 23:27:13 +00003111 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003112 SDValue ARMcc;
3113 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003114 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3115 bitcastf32Toi32(LHS, DAG), Mask);
3116 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3117 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003118 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3119 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3120 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3121 Chain, Dest, ARMcc, CCR, Cmp);
3122 }
3123
3124 SDValue LHS1, LHS2;
3125 SDValue RHS1, RHS2;
3126 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3127 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003128 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3129 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003130 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3131 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003132 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003133 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3134 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3135 }
3136
3137 return SDValue();
3138}
3139
3140SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3141 SDValue Chain = Op.getOperand(0);
3142 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3143 SDValue LHS = Op.getOperand(2);
3144 SDValue RHS = Op.getOperand(3);
3145 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003146 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003147
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003149 SDValue ARMcc;
3150 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003153 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003154 }
3155
Owen Anderson825b72b2009-08-11 20:47:22 +00003156 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003157
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003158 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003159 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3160 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3161 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3162 if (Result.getNode())
3163 return Result;
3164 }
3165
Evan Chenga8e29892007-01-19 07:51:42 +00003166 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003167 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003168
Evan Cheng218977b2010-07-13 19:27:42 +00003169 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3170 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003171 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003172 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003173 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003174 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003175 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003176 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3177 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003178 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003179 }
3180 return Res;
3181}
3182
Dan Gohmand858e902010-04-17 15:26:15 +00003183SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003184 SDValue Chain = Op.getOperand(0);
3185 SDValue Table = Op.getOperand(1);
3186 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003187 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003188
Owen Andersone50ed302009-08-10 22:56:29 +00003189 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003190 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3191 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003192 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003193 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003194 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003195 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3196 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003197 if (Subtarget->isThumb2()) {
3198 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3199 // which does another jump to the destination. This also makes it easier
3200 // to translate it to TBB / TBH later.
3201 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003203 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003204 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003205 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003206 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003207 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003208 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003209 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003210 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003211 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003212 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003213 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003214 MachinePointerInfo::getJumpTable(),
3215 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003216 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003218 }
Evan Chenga8e29892007-01-19 07:51:42 +00003219}
3220
Eli Friedman14e809c2011-11-09 23:36:02 +00003221static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003222 EVT VT = Op.getValueType();
3223 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003224
James Molloy873fd5f2012-02-20 09:24:05 +00003225 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3226 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3227 return Op;
3228 return DAG.UnrollVectorOp(Op.getNode());
3229 }
3230
3231 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3232 "Invalid type for custom lowering!");
3233 if (VT != MVT::v4i16)
3234 return DAG.UnrollVectorOp(Op.getNode());
3235
3236 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3237 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003238}
3239
Bob Wilson76a312b2010-03-19 22:51:32 +00003240static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003241 EVT VT = Op.getValueType();
3242 if (VT.isVector())
3243 return LowerVectorFP_TO_INT(Op, DAG);
3244
Bob Wilson76a312b2010-03-19 22:51:32 +00003245 DebugLoc dl = Op.getDebugLoc();
3246 unsigned Opc;
3247
3248 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003249 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003250 case ISD::FP_TO_SINT:
3251 Opc = ARMISD::FTOSI;
3252 break;
3253 case ISD::FP_TO_UINT:
3254 Opc = ARMISD::FTOUI;
3255 break;
3256 }
3257 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003258 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003259}
3260
Cameron Zwarich3007d332011-03-29 21:41:55 +00003261static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3262 EVT VT = Op.getValueType();
3263 DebugLoc dl = Op.getDebugLoc();
3264
Eli Friedman14e809c2011-11-09 23:36:02 +00003265 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3266 if (VT.getVectorElementType() == MVT::f32)
3267 return Op;
3268 return DAG.UnrollVectorOp(Op.getNode());
3269 }
3270
Duncan Sands1f6a3292011-08-12 14:54:45 +00003271 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3272 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003273 if (VT != MVT::v4f32)
3274 return DAG.UnrollVectorOp(Op.getNode());
3275
3276 unsigned CastOpc;
3277 unsigned Opc;
3278 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003279 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003280 case ISD::SINT_TO_FP:
3281 CastOpc = ISD::SIGN_EXTEND;
3282 Opc = ISD::SINT_TO_FP;
3283 break;
3284 case ISD::UINT_TO_FP:
3285 CastOpc = ISD::ZERO_EXTEND;
3286 Opc = ISD::UINT_TO_FP;
3287 break;
3288 }
3289
3290 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3291 return DAG.getNode(Opc, dl, VT, Op);
3292}
3293
Bob Wilson76a312b2010-03-19 22:51:32 +00003294static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3295 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003296 if (VT.isVector())
3297 return LowerVectorINT_TO_FP(Op, DAG);
3298
Bob Wilson76a312b2010-03-19 22:51:32 +00003299 DebugLoc dl = Op.getDebugLoc();
3300 unsigned Opc;
3301
3302 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003303 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003304 case ISD::SINT_TO_FP:
3305 Opc = ARMISD::SITOF;
3306 break;
3307 case ISD::UINT_TO_FP:
3308 Opc = ARMISD::UITOF;
3309 break;
3310 }
3311
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003312 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003313 return DAG.getNode(Opc, dl, VT, Op);
3314}
3315
Evan Cheng515fe3a2010-07-08 02:08:50 +00003316SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003317 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003318 SDValue Tmp0 = Op.getOperand(0);
3319 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003320 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003321 EVT VT = Op.getValueType();
3322 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003323 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3324 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3325 bool UseNEON = !InGPR && Subtarget->hasNEON();
3326
3327 if (UseNEON) {
3328 // Use VBSL to copy the sign bit.
3329 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3330 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3331 DAG.getTargetConstant(EncodedVal, MVT::i32));
3332 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3333 if (VT == MVT::f64)
3334 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3335 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3336 DAG.getConstant(32, MVT::i32));
3337 else /*if (VT == MVT::f32)*/
3338 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3339 if (SrcVT == MVT::f32) {
3340 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3341 if (VT == MVT::f64)
3342 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3343 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3344 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003345 } else if (VT == MVT::f32)
3346 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3347 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3348 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003349 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3350 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3351
3352 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3353 MVT::i32);
3354 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3355 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3356 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003357
Evan Chenge573fb32011-02-23 02:24:55 +00003358 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3359 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3360 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003361 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003362 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3363 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3364 DAG.getConstant(0, MVT::i32));
3365 } else {
3366 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3367 }
3368
3369 return Res;
3370 }
Evan Chengc143dd42011-02-11 02:28:55 +00003371
3372 // Bitcast operand 1 to i32.
3373 if (SrcVT == MVT::f64)
3374 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3375 &Tmp1, 1).getValue(1);
3376 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3377
Evan Chenge573fb32011-02-23 02:24:55 +00003378 // Or in the signbit with integer operations.
3379 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3380 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3381 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3382 if (VT == MVT::f32) {
3383 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3384 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3385 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3386 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003387 }
3388
Evan Chenge573fb32011-02-23 02:24:55 +00003389 // f64: Or the high part with signbit and then combine two parts.
3390 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3391 &Tmp0, 1);
3392 SDValue Lo = Tmp0.getValue(0);
3393 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3394 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3395 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003396}
3397
Evan Cheng2457f2c2010-05-22 01:47:14 +00003398SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3399 MachineFunction &MF = DAG.getMachineFunction();
3400 MachineFrameInfo *MFI = MF.getFrameInfo();
3401 MFI->setReturnAddressIsTaken(true);
3402
3403 EVT VT = Op.getValueType();
3404 DebugLoc dl = Op.getDebugLoc();
3405 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3406 if (Depth) {
3407 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3408 SDValue Offset = DAG.getConstant(4, MVT::i32);
3409 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3410 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003411 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003412 }
3413
3414 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003415 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003416 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3417}
3418
Dan Gohmand858e902010-04-17 15:26:15 +00003419SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003420 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3421 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003422
Owen Andersone50ed302009-08-10 22:56:29 +00003423 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003424 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3425 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003426 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003427 ? ARM::R7 : ARM::R11;
3428 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3429 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003430 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3431 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003432 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003433 return FrameAddr;
3434}
3435
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003436/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003437/// expand a bit convert where either the source or destination type is i64 to
3438/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3439/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3440/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003441static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003442 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3443 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003444 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003445
Bob Wilson9f3f0612010-04-17 05:30:19 +00003446 // This function is only supposed to be called for i64 types, either as the
3447 // source or destination of the bit convert.
3448 EVT SrcVT = Op.getValueType();
3449 EVT DstVT = N->getValueType(0);
3450 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003451 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003452
Bob Wilson9f3f0612010-04-17 05:30:19 +00003453 // Turn i64->f64 into VMOVDRR.
3454 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3456 DAG.getConstant(0, MVT::i32));
3457 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3458 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003459 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003460 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003461 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003462
Jim Grosbache5165492009-11-09 00:11:35 +00003463 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003464 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3465 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3466 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3467 // Merge the pieces into a single i64 value.
3468 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3469 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003470
Bob Wilson9f3f0612010-04-17 05:30:19 +00003471 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003472}
3473
Bob Wilson5bafff32009-06-22 23:27:02 +00003474/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003475/// Zero vectors are used to represent vector negation and in those cases
3476/// will be implemented with the NEON VNEG instruction. However, VNEG does
3477/// not support i64 elements, so sometimes the zero vectors will need to be
3478/// explicitly constructed. Regardless, use a canonical VMOV to create the
3479/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003480static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003481 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003482 // The canonical modified immediate encoding of a zero vector is....0!
3483 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3484 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3485 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003486 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003487}
3488
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003489/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3490/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003491SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3492 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003493 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3494 EVT VT = Op.getValueType();
3495 unsigned VTBits = VT.getSizeInBits();
3496 DebugLoc dl = Op.getDebugLoc();
3497 SDValue ShOpLo = Op.getOperand(0);
3498 SDValue ShOpHi = Op.getOperand(1);
3499 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003500 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003501 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003502
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003503 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3504
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003505 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3506 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3507 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3508 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3509 DAG.getConstant(VTBits, MVT::i32));
3510 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3511 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003512 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003513
3514 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3515 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003516 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003517 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003518 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003519 CCR, Cmp);
3520
3521 SDValue Ops[2] = { Lo, Hi };
3522 return DAG.getMergeValues(Ops, 2, dl);
3523}
3524
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003525/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3526/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003527SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3528 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003529 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3530 EVT VT = Op.getValueType();
3531 unsigned VTBits = VT.getSizeInBits();
3532 DebugLoc dl = Op.getDebugLoc();
3533 SDValue ShOpLo = Op.getOperand(0);
3534 SDValue ShOpHi = Op.getOperand(1);
3535 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003536 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003537
3538 assert(Op.getOpcode() == ISD::SHL_PARTS);
3539 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3540 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3541 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3542 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3543 DAG.getConstant(VTBits, MVT::i32));
3544 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3545 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3546
3547 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3548 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3549 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003550 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003551 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003552 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003553 CCR, Cmp);
3554
3555 SDValue Ops[2] = { Lo, Hi };
3556 return DAG.getMergeValues(Ops, 2, dl);
3557}
3558
Jim Grosbach4725ca72010-09-08 03:54:02 +00003559SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003560 SelectionDAG &DAG) const {
3561 // The rounding mode is in bits 23:22 of the FPSCR.
3562 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3563 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3564 // so that the shift + and get folded into a bitfield extract.
3565 DebugLoc dl = Op.getDebugLoc();
3566 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3567 DAG.getConstant(Intrinsic::arm_get_fpscr,
3568 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003569 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003570 DAG.getConstant(1U << 22, MVT::i32));
3571 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3572 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003573 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003574 DAG.getConstant(3, MVT::i32));
3575}
3576
Jim Grosbach3482c802010-01-18 19:58:49 +00003577static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3578 const ARMSubtarget *ST) {
3579 EVT VT = N->getValueType(0);
3580 DebugLoc dl = N->getDebugLoc();
3581
3582 if (!ST->hasV6T2Ops())
3583 return SDValue();
3584
3585 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3586 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3587}
3588
Evan Chengc8e70452012-12-04 22:41:50 +00003589/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3590/// for each 16-bit element from operand, repeated. The basic idea is to
3591/// leverage vcnt to get the 8-bit counts, gather and add the results.
3592///
3593/// Trace for v4i16:
3594/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3595/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3596/// 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 +00003597/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengc8e70452012-12-04 22:41:50 +00003598/// [b0 b1 b2 b3 b4 b5 b6 b7]
3599/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3600/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3601/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3602static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3603 EVT VT = N->getValueType(0);
3604 DebugLoc DL = N->getDebugLoc();
3605
3606 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3607 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3608 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3609 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3610 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3611 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3612}
3613
3614/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3615/// bit-count for each 16-bit element from the operand. We need slightly
3616/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3617/// 64/128-bit registers.
Jim Grosbach7ccf4632013-03-02 20:16:15 +00003618///
Evan Chengc8e70452012-12-04 22:41:50 +00003619/// Trace for v4i16:
3620/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3621/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3622/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3623/// v4i16:Extracted = [k0 k1 k2 k3 ]
3624static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3625 EVT VT = N->getValueType(0);
3626 DebugLoc DL = N->getDebugLoc();
3627
3628 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3629 if (VT.is64BitVector()) {
3630 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3631 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3632 DAG.getIntPtrConstant(0));
3633 } else {
3634 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3635 BitCounts, DAG.getIntPtrConstant(0));
3636 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3637 }
3638}
3639
3640/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3641/// bit-count for each 32-bit element from the operand. The idea here is
3642/// to split the vector into 16-bit elements, leverage the 16-bit count
3643/// routine, and then combine the results.
3644///
3645/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3646/// input = [v0 v1 ] (vi: 32-bit elements)
3647/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3648/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach7ccf4632013-03-02 20:16:15 +00003649/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengc8e70452012-12-04 22:41:50 +00003650/// [k0 k1 k2 k3 ]
3651/// N1 =+[k1 k0 k3 k2 ]
3652/// [k0 k2 k1 k3 ]
3653/// N2 =+[k1 k3 k0 k2 ]
3654/// [k0 k2 k1 k3 ]
3655/// Extended =+[k1 k3 k0 k2 ]
3656/// [k0 k2 ]
3657/// Extracted=+[k1 k3 ]
3658///
3659static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3660 EVT VT = N->getValueType(0);
3661 DebugLoc DL = N->getDebugLoc();
3662
3663 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3664
3665 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3666 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3667 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3668 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3669 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3670
3671 if (VT.is64BitVector()) {
3672 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3673 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3674 DAG.getIntPtrConstant(0));
3675 } else {
3676 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3677 DAG.getIntPtrConstant(0));
3678 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3679 }
3680}
3681
3682static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3683 const ARMSubtarget *ST) {
3684 EVT VT = N->getValueType(0);
3685
3686 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay105ab4f2012-12-04 23:54:02 +00003687 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3688 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengc8e70452012-12-04 22:41:50 +00003689 "Unexpected type for custom ctpop lowering");
3690
3691 if (VT.getVectorElementType() == MVT::i32)
3692 return lowerCTPOP32BitElements(N, DAG);
3693 else
3694 return lowerCTPOP16BitElements(N, DAG);
3695}
3696
Bob Wilson5bafff32009-06-22 23:27:02 +00003697static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3698 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003699 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003700 DebugLoc dl = N->getDebugLoc();
3701
Bob Wilsond5448bb2010-11-18 21:16:28 +00003702 if (!VT.isVector())
3703 return SDValue();
3704
Bob Wilson5bafff32009-06-22 23:27:02 +00003705 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003706 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003707
Bob Wilsond5448bb2010-11-18 21:16:28 +00003708 // Left shifts translate directly to the vshiftu intrinsic.
3709 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003710 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003711 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3712 N->getOperand(0), N->getOperand(1));
3713
3714 assert((N->getOpcode() == ISD::SRA ||
3715 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3716
3717 // NEON uses the same intrinsics for both left and right shifts. For
3718 // right shifts, the shift amounts are negative, so negate the vector of
3719 // shift amounts.
3720 EVT ShiftVT = N->getOperand(1).getValueType();
3721 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3722 getZeroVector(ShiftVT, DAG, dl),
3723 N->getOperand(1));
3724 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3725 Intrinsic::arm_neon_vshifts :
3726 Intrinsic::arm_neon_vshiftu);
3727 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3728 DAG.getConstant(vshiftInt, MVT::i32),
3729 N->getOperand(0), NegatedCount);
3730}
3731
3732static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3733 const ARMSubtarget *ST) {
3734 EVT VT = N->getValueType(0);
3735 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003736
Eli Friedmance392eb2009-08-22 03:13:10 +00003737 // We can get here for a node like i32 = ISD::SHL i32, i64
3738 if (VT != MVT::i64)
3739 return SDValue();
3740
3741 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003742 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003743
Chris Lattner27a6c732007-11-24 07:07:01 +00003744 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3745 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003746 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003747 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003748
Chris Lattner27a6c732007-11-24 07:07:01 +00003749 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003750 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003751
Chris Lattner27a6c732007-11-24 07:07:01 +00003752 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003754 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003756 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003757
Chris Lattner27a6c732007-11-24 07:07:01 +00003758 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3759 // captures the result into a carry flag.
3760 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003761 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003762
Chris Lattner27a6c732007-11-24 07:07:01 +00003763 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003764 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003765
Chris Lattner27a6c732007-11-24 07:07:01 +00003766 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003768}
3769
Bob Wilson5bafff32009-06-22 23:27:02 +00003770static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3771 SDValue TmpOp0, TmpOp1;
3772 bool Invert = false;
3773 bool Swap = false;
3774 unsigned Opc = 0;
3775
3776 SDValue Op0 = Op.getOperand(0);
3777 SDValue Op1 = Op.getOperand(1);
3778 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003779 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003780 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3781 DebugLoc dl = Op.getDebugLoc();
3782
3783 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3784 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003785 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003786 case ISD::SETUNE:
3787 case ISD::SETNE: Invert = true; // Fallthrough
3788 case ISD::SETOEQ:
3789 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3790 case ISD::SETOLT:
3791 case ISD::SETLT: Swap = true; // Fallthrough
3792 case ISD::SETOGT:
3793 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3794 case ISD::SETOLE:
3795 case ISD::SETLE: Swap = true; // Fallthrough
3796 case ISD::SETOGE:
3797 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3798 case ISD::SETUGE: Swap = true; // Fallthrough
3799 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3800 case ISD::SETUGT: Swap = true; // Fallthrough
3801 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3802 case ISD::SETUEQ: Invert = true; // Fallthrough
3803 case ISD::SETONE:
3804 // Expand this to (OLT | OGT).
3805 TmpOp0 = Op0;
3806 TmpOp1 = Op1;
3807 Opc = ISD::OR;
3808 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3809 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3810 break;
3811 case ISD::SETUO: Invert = true; // Fallthrough
3812 case ISD::SETO:
3813 // Expand this to (OLT | OGE).
3814 TmpOp0 = Op0;
3815 TmpOp1 = Op1;
3816 Opc = ISD::OR;
3817 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3818 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3819 break;
3820 }
3821 } else {
3822 // Integer comparisons.
3823 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003824 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003825 case ISD::SETNE: Invert = true;
3826 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3827 case ISD::SETLT: Swap = true;
3828 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3829 case ISD::SETLE: Swap = true;
3830 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3831 case ISD::SETULT: Swap = true;
3832 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3833 case ISD::SETULE: Swap = true;
3834 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3835 }
3836
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003837 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003838 if (Opc == ARMISD::VCEQ) {
3839
3840 SDValue AndOp;
3841 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3842 AndOp = Op0;
3843 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3844 AndOp = Op1;
3845
3846 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003847 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003848 AndOp = AndOp.getOperand(0);
3849
3850 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3851 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003852 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3853 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003854 Invert = !Invert;
3855 }
3856 }
3857 }
3858
3859 if (Swap)
3860 std::swap(Op0, Op1);
3861
Owen Andersonc24cb352010-11-08 23:21:22 +00003862 // If one of the operands is a constant vector zero, attempt to fold the
3863 // comparison to a specialized compare-against-zero form.
3864 SDValue SingleOp;
3865 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3866 SingleOp = Op0;
3867 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3868 if (Opc == ARMISD::VCGE)
3869 Opc = ARMISD::VCLEZ;
3870 else if (Opc == ARMISD::VCGT)
3871 Opc = ARMISD::VCLTZ;
3872 SingleOp = Op1;
3873 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003874
Owen Andersonc24cb352010-11-08 23:21:22 +00003875 SDValue Result;
3876 if (SingleOp.getNode()) {
3877 switch (Opc) {
3878 case ARMISD::VCEQ:
3879 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3880 case ARMISD::VCGE:
3881 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3882 case ARMISD::VCLEZ:
3883 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3884 case ARMISD::VCGT:
3885 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3886 case ARMISD::VCLTZ:
3887 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3888 default:
3889 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3890 }
3891 } else {
3892 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3893 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003894
3895 if (Invert)
3896 Result = DAG.getNOT(dl, Result, VT);
3897
3898 return Result;
3899}
3900
Bob Wilsond3c42842010-06-14 22:19:57 +00003901/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3902/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003903/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003904static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3905 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003906 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003907 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003908
Bob Wilson827b2102010-06-15 19:05:35 +00003909 // SplatBitSize is set to the smallest size that splats the vector, so a
3910 // zero vector will always have SplatBitSize == 8. However, NEON modified
3911 // immediate instructions others than VMOV do not support the 8-bit encoding
3912 // of a zero vector, and the default encoding of zero is supposed to be the
3913 // 32-bit version.
3914 if (SplatBits == 0)
3915 SplatBitSize = 32;
3916
Bob Wilson5bafff32009-06-22 23:27:02 +00003917 switch (SplatBitSize) {
3918 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003919 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003920 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003921 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003922 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003923 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003924 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003925 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003926 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003927
3928 case 16:
3929 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003930 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003931 if ((SplatBits & ~0xff) == 0) {
3932 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003933 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003934 Imm = SplatBits;
3935 break;
3936 }
3937 if ((SplatBits & ~0xff00) == 0) {
3938 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003939 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003940 Imm = SplatBits >> 8;
3941 break;
3942 }
3943 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003944
3945 case 32:
3946 // NEON's 32-bit VMOV supports splat values where:
3947 // * only one byte is nonzero, or
3948 // * the least significant byte is 0xff and the second byte is nonzero, or
3949 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003950 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003951 if ((SplatBits & ~0xff) == 0) {
3952 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003953 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003954 Imm = SplatBits;
3955 break;
3956 }
3957 if ((SplatBits & ~0xff00) == 0) {
3958 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003959 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003960 Imm = SplatBits >> 8;
3961 break;
3962 }
3963 if ((SplatBits & ~0xff0000) == 0) {
3964 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003965 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003966 Imm = SplatBits >> 16;
3967 break;
3968 }
3969 if ((SplatBits & ~0xff000000) == 0) {
3970 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003971 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003972 Imm = SplatBits >> 24;
3973 break;
3974 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003975
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003976 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3977 if (type == OtherModImm) return SDValue();
3978
Bob Wilson5bafff32009-06-22 23:27:02 +00003979 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003980 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3981 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003982 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003983 Imm = SplatBits >> 8;
3984 SplatBits |= 0xff;
3985 break;
3986 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003987
3988 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003989 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3990 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003991 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003992 Imm = SplatBits >> 16;
3993 SplatBits |= 0xffff;
3994 break;
3995 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003996
3997 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3998 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3999 // VMOV.I32. A (very) minor optimization would be to replicate the value
4000 // and fall through here to test for a valid 64-bit splat. But, then the
4001 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00004002 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004003
4004 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004005 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00004006 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004007 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00004008 uint64_t BitMask = 0xff;
4009 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004010 unsigned ImmMask = 1;
4011 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00004012 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00004013 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004014 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004015 Imm |= ImmMask;
4016 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004017 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00004018 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004019 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004020 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00004021 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00004022 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004023 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004024 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00004025 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00004026 break;
4027 }
4028
Bob Wilson1a913ed2010-06-11 21:34:50 +00004029 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00004030 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00004031 }
4032
Bob Wilsoncba270d2010-07-13 21:16:48 +00004033 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4034 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00004035}
4036
Lang Hamesc0a9f822012-03-29 21:56:11 +00004037SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4038 const ARMSubtarget *ST) const {
4039 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
4040 return SDValue();
4041
4042 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4043 assert(Op.getValueType() == MVT::f32 &&
4044 "ConstantFP custom lowering should only occur for f32.");
4045
4046 // Try splatting with a VMOV.f32...
4047 APFloat FPVal = CFP->getValueAPF();
4048 int ImmVal = ARM_AM::getFP32Imm(FPVal);
4049 if (ImmVal != -1) {
4050 DebugLoc DL = Op.getDebugLoc();
4051 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4052 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4053 NewVal);
4054 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4055 DAG.getConstant(0, MVT::i32));
4056 }
4057
4058 // If that fails, try a VMOV.i32
4059 EVT VMovVT;
4060 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
4061 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
4062 VMOVModImm);
4063 if (NewVal != SDValue()) {
4064 DebugLoc DL = Op.getDebugLoc();
4065 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4066 NewVal);
4067 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4068 VecConstant);
4069 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4070 DAG.getConstant(0, MVT::i32));
4071 }
4072
4073 // Finally, try a VMVN.i32
4074 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
4075 VMVNModImm);
4076 if (NewVal != SDValue()) {
4077 DebugLoc DL = Op.getDebugLoc();
4078 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4079 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4080 VecConstant);
4081 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4082 DAG.getConstant(0, MVT::i32));
4083 }
4084
4085 return SDValue();
4086}
4087
Quentin Colombet43934ae2012-11-02 21:32:17 +00004088// check if an VEXT instruction can handle the shuffle mask when the
4089// vector sources of the shuffle are the same.
4090static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4091 unsigned NumElts = VT.getVectorNumElements();
4092
4093 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4094 if (M[0] < 0)
4095 return false;
4096
4097 Imm = M[0];
4098
4099 // If this is a VEXT shuffle, the immediate value is the index of the first
4100 // element. The other shuffle indices must be the successive elements after
4101 // the first one.
4102 unsigned ExpectedElt = Imm;
4103 for (unsigned i = 1; i < NumElts; ++i) {
4104 // Increment the expected index. If it wraps around, just follow it
4105 // back to index zero and keep going.
4106 ++ExpectedElt;
4107 if (ExpectedElt == NumElts)
4108 ExpectedElt = 0;
4109
4110 if (M[i] < 0) continue; // ignore UNDEF indices
4111 if (ExpectedElt != static_cast<unsigned>(M[i]))
4112 return false;
4113 }
4114
4115 return true;
4116}
4117
Lang Hamesc0a9f822012-03-29 21:56:11 +00004118
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004119static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004120 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004121 unsigned NumElts = VT.getVectorNumElements();
4122 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004123
4124 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4125 if (M[0] < 0)
4126 return false;
4127
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004128 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004129
4130 // If this is a VEXT shuffle, the immediate value is the index of the first
4131 // element. The other shuffle indices must be the successive elements after
4132 // the first one.
4133 unsigned ExpectedElt = Imm;
4134 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004135 // Increment the expected index. If it wraps around, it may still be
4136 // a VEXT but the source vectors must be swapped.
4137 ExpectedElt += 1;
4138 if (ExpectedElt == NumElts * 2) {
4139 ExpectedElt = 0;
4140 ReverseVEXT = true;
4141 }
4142
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004143 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004144 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004145 return false;
4146 }
4147
4148 // Adjust the index value if the source operands will be swapped.
4149 if (ReverseVEXT)
4150 Imm -= NumElts;
4151
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004152 return true;
4153}
4154
Bob Wilson8bb9e482009-07-26 00:39:34 +00004155/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4156/// instruction with the specified blocksize. (The order of the elements
4157/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004158static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004159 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4160 "Only possible block sizes for VREV are: 16, 32, 64");
4161
Bob Wilson8bb9e482009-07-26 00:39:34 +00004162 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004163 if (EltSz == 64)
4164 return false;
4165
4166 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004167 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004168 // If the first shuffle index is UNDEF, be optimistic.
4169 if (M[0] < 0)
4170 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004171
4172 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4173 return false;
4174
4175 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004176 if (M[i] < 0) continue; // ignore UNDEF indices
4177 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004178 return false;
4179 }
4180
4181 return true;
4182}
4183
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004184static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004185 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4186 // range, then 0 is placed into the resulting vector. So pretty much any mask
4187 // of 8 elements can work here.
4188 return VT == MVT::v8i8 && M.size() == 8;
4189}
4190
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004191static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004192 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4193 if (EltSz == 64)
4194 return false;
4195
Bob Wilsonc692cb72009-08-21 20:54:19 +00004196 unsigned NumElts = VT.getVectorNumElements();
4197 WhichResult = (M[0] == 0 ? 0 : 1);
4198 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004199 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4200 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004201 return false;
4202 }
4203 return true;
4204}
4205
Bob Wilson324f4f12009-12-03 06:40:55 +00004206/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4207/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4208/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004209static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004210 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4211 if (EltSz == 64)
4212 return false;
4213
4214 unsigned NumElts = VT.getVectorNumElements();
4215 WhichResult = (M[0] == 0 ? 0 : 1);
4216 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004217 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4218 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004219 return false;
4220 }
4221 return true;
4222}
4223
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004224static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004225 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4226 if (EltSz == 64)
4227 return false;
4228
Bob Wilsonc692cb72009-08-21 20:54:19 +00004229 unsigned NumElts = VT.getVectorNumElements();
4230 WhichResult = (M[0] == 0 ? 0 : 1);
4231 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004232 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004233 if ((unsigned) M[i] != 2 * i + WhichResult)
4234 return false;
4235 }
4236
4237 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004238 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004239 return false;
4240
4241 return true;
4242}
4243
Bob Wilson324f4f12009-12-03 06:40:55 +00004244/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4245/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4246/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004247static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004248 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4249 if (EltSz == 64)
4250 return false;
4251
4252 unsigned Half = VT.getVectorNumElements() / 2;
4253 WhichResult = (M[0] == 0 ? 0 : 1);
4254 for (unsigned j = 0; j != 2; ++j) {
4255 unsigned Idx = WhichResult;
4256 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004257 int MIdx = M[i + j * Half];
4258 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004259 return false;
4260 Idx += 2;
4261 }
4262 }
4263
4264 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4265 if (VT.is64BitVector() && EltSz == 32)
4266 return false;
4267
4268 return true;
4269}
4270
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004271static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004272 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4273 if (EltSz == 64)
4274 return false;
4275
Bob Wilsonc692cb72009-08-21 20:54:19 +00004276 unsigned NumElts = VT.getVectorNumElements();
4277 WhichResult = (M[0] == 0 ? 0 : 1);
4278 unsigned Idx = WhichResult * NumElts / 2;
4279 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004280 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4281 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004282 return false;
4283 Idx += 1;
4284 }
4285
4286 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004287 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004288 return false;
4289
4290 return true;
4291}
4292
Bob Wilson324f4f12009-12-03 06:40:55 +00004293/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4294/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4295/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004296static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004297 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4298 if (EltSz == 64)
4299 return false;
4300
4301 unsigned NumElts = VT.getVectorNumElements();
4302 WhichResult = (M[0] == 0 ? 0 : 1);
4303 unsigned Idx = WhichResult * NumElts / 2;
4304 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004305 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4306 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004307 return false;
4308 Idx += 1;
4309 }
4310
4311 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4312 if (VT.is64BitVector() && EltSz == 32)
4313 return false;
4314
4315 return true;
4316}
4317
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004318/// \return true if this is a reverse operation on an vector.
4319static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4320 unsigned NumElts = VT.getVectorNumElements();
4321 // Make sure the mask has the right size.
4322 if (NumElts != M.size())
4323 return false;
4324
4325 // Look for <15, ..., 3, -1, 1, 0>.
4326 for (unsigned i = 0; i != NumElts; ++i)
4327 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4328 return false;
4329
4330 return true;
4331}
4332
Dale Johannesenf630c712010-07-29 20:10:08 +00004333// If N is an integer constant that can be moved into a register in one
4334// instruction, return an SDValue of such a constant (will become a MOV
4335// instruction). Otherwise return null.
4336static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4337 const ARMSubtarget *ST, DebugLoc dl) {
4338 uint64_t Val;
4339 if (!isa<ConstantSDNode>(N))
4340 return SDValue();
4341 Val = cast<ConstantSDNode>(N)->getZExtValue();
4342
4343 if (ST->isThumb1Only()) {
4344 if (Val <= 255 || ~Val <= 255)
4345 return DAG.getConstant(Val, MVT::i32);
4346 } else {
4347 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4348 return DAG.getConstant(Val, MVT::i32);
4349 }
4350 return SDValue();
4351}
4352
Bob Wilson5bafff32009-06-22 23:27:02 +00004353// If this is a case we can't handle, return null and let the default
4354// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004355SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4356 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004357 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004358 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004359 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004360
4361 APInt SplatBits, SplatUndef;
4362 unsigned SplatBitSize;
4363 bool HasAnyUndefs;
4364 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004365 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004366 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004367 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004368 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004369 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004370 DAG, VmovVT, VT.is128BitVector(),
4371 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004372 if (Val.getNode()) {
4373 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004374 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004375 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004376
4377 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004378 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004379 Val = isNEONModifiedImm(NegatedImm,
4380 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004381 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004382 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004383 if (Val.getNode()) {
4384 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004385 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004386 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004387
4388 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004389 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004390 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004391 if (ImmVal != -1) {
4392 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4393 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4394 }
4395 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004396 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004397 }
4398
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004399 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004400 //
4401 // As an optimisation, even if more than one value is used it may be more
4402 // profitable to splat with one value then change some lanes.
4403 //
4404 // Heuristically we decide to do this if the vector has a "dominant" value,
4405 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004406 unsigned NumElts = VT.getVectorNumElements();
4407 bool isOnlyLowElement = true;
4408 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004409 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004410 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004411
4412 // Map of the number of times a particular SDValue appears in the
4413 // element list.
James Molloy95154342012-09-06 10:32:08 +00004414 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004415 SDValue Value;
4416 for (unsigned i = 0; i < NumElts; ++i) {
4417 SDValue V = Op.getOperand(i);
4418 if (V.getOpcode() == ISD::UNDEF)
4419 continue;
4420 if (i > 0)
4421 isOnlyLowElement = false;
4422 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4423 isConstant = false;
4424
James Molloyba8562a2012-09-06 09:55:02 +00004425 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004426 unsigned &Count = ValueCounts[V];
Jim Grosbach7ccf4632013-03-02 20:16:15 +00004427
James Molloyba8562a2012-09-06 09:55:02 +00004428 // Is this value dominant? (takes up more than half of the lanes)
4429 if (++Count > (NumElts / 2)) {
4430 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004431 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004432 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004433 }
James Molloyba8562a2012-09-06 09:55:02 +00004434 if (ValueCounts.size() != 1)
4435 usesOnlyOneValue = false;
4436 if (!Value.getNode() && ValueCounts.size() > 0)
4437 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004438
James Molloyba8562a2012-09-06 09:55:02 +00004439 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004440 return DAG.getUNDEF(VT);
4441
4442 if (isOnlyLowElement)
4443 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4444
Dale Johannesenf630c712010-07-29 20:10:08 +00004445 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4446
Dale Johannesen575cd142010-10-19 20:00:17 +00004447 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4448 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004449 if (hasDominantValue && EltSize <= 32) {
4450 if (!isConstant) {
4451 SDValue N;
4452
4453 // If we are VDUPing a value that comes directly from a vector, that will
4454 // cause an unnecessary move to and from a GPR, where instead we could
4455 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004456 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4457 // We need to create a new undef vector to use for the VDUPLANE if the
4458 // size of the vector from which we get the value is different than the
4459 // size of the vector that we need to create. We will insert the element
4460 // such that the register coalescer will remove unnecessary copies.
4461 if (VT != Value->getOperand(0).getValueType()) {
4462 ConstantSDNode *constIndex;
4463 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4464 assert(constIndex && "The index is not a constant!");
4465 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4466 VT.getVectorNumElements();
4467 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4468 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4469 Value, DAG.getConstant(index, MVT::i32)),
4470 DAG.getConstant(index, MVT::i32));
Jim Grosbach65da9f12013-03-02 20:16:19 +00004471 } else
Silviu Barangabb1078e2012-10-15 09:41:32 +00004472 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004473 Value->getOperand(0), Value->getOperand(1));
Jim Grosbach65da9f12013-03-02 20:16:19 +00004474 } else
James Molloyba8562a2012-09-06 09:55:02 +00004475 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4476
4477 if (!usesOnlyOneValue) {
4478 // The dominant value was splatted as 'N', but we now have to insert
4479 // all differing elements.
4480 for (unsigned I = 0; I < NumElts; ++I) {
4481 if (Op.getOperand(I) == Value)
4482 continue;
4483 SmallVector<SDValue, 3> Ops;
4484 Ops.push_back(N);
4485 Ops.push_back(Op.getOperand(I));
4486 Ops.push_back(DAG.getConstant(I, MVT::i32));
4487 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4488 }
4489 }
4490 return N;
4491 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004492 if (VT.getVectorElementType().isFloatingPoint()) {
4493 SmallVector<SDValue, 8> Ops;
4494 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004495 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004496 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004497 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4498 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004499 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4500 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004501 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004502 }
James Molloyba8562a2012-09-06 09:55:02 +00004503 if (usesOnlyOneValue) {
4504 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4505 if (isConstant && Val.getNode())
Jim Grosbach7ccf4632013-03-02 20:16:15 +00004506 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloyba8562a2012-09-06 09:55:02 +00004507 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004508 }
4509
4510 // If all elements are constants and the case above didn't get hit, fall back
4511 // to the default expansion, which will generate a load from the constant
4512 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004513 if (isConstant)
4514 return SDValue();
4515
Bob Wilson11a1dff2011-01-07 21:37:30 +00004516 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4517 if (NumElts >= 4) {
4518 SDValue shuffle = ReconstructShuffle(Op, DAG);
4519 if (shuffle != SDValue())
4520 return shuffle;
4521 }
4522
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004523 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004524 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4525 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004526 if (EltSize >= 32) {
4527 // Do the expansion with floating-point types, since that is what the VFP
4528 // registers are defined to use, and since i64 is not legal.
4529 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4530 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004531 SmallVector<SDValue, 8> Ops;
4532 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004533 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004534 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004535 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004536 }
4537
4538 return SDValue();
4539}
4540
Bob Wilson11a1dff2011-01-07 21:37:30 +00004541// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004542// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004543SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4544 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004545 DebugLoc dl = Op.getDebugLoc();
4546 EVT VT = Op.getValueType();
4547 unsigned NumElts = VT.getVectorNumElements();
4548
4549 SmallVector<SDValue, 2> SourceVecs;
4550 SmallVector<unsigned, 2> MinElts;
4551 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004552
Bob Wilson11a1dff2011-01-07 21:37:30 +00004553 for (unsigned i = 0; i < NumElts; ++i) {
4554 SDValue V = Op.getOperand(i);
4555 if (V.getOpcode() == ISD::UNDEF)
4556 continue;
4557 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4558 // A shuffle can only come from building a vector from various
4559 // elements of other vectors.
4560 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004561 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4562 VT.getVectorElementType()) {
4563 // This code doesn't know how to handle shuffles where the vector
4564 // element types do not match (this happens because type legalization
4565 // promotes the return type of EXTRACT_VECTOR_ELT).
4566 // FIXME: It might be appropriate to extend this code to handle
4567 // mismatched types.
4568 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004569 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004570
Bob Wilson11a1dff2011-01-07 21:37:30 +00004571 // Record this extraction against the appropriate vector if possible...
4572 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004573 // If the element number isn't a constant, we can't effectively
4574 // analyze what's going on.
4575 if (!isa<ConstantSDNode>(V.getOperand(1)))
4576 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004577 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4578 bool FoundSource = false;
4579 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4580 if (SourceVecs[j] == SourceVec) {
4581 if (MinElts[j] > EltNo)
4582 MinElts[j] = EltNo;
4583 if (MaxElts[j] < EltNo)
4584 MaxElts[j] = EltNo;
4585 FoundSource = true;
4586 break;
4587 }
4588 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004589
Bob Wilson11a1dff2011-01-07 21:37:30 +00004590 // Or record a new source if not...
4591 if (!FoundSource) {
4592 SourceVecs.push_back(SourceVec);
4593 MinElts.push_back(EltNo);
4594 MaxElts.push_back(EltNo);
4595 }
4596 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004597
Bob Wilson11a1dff2011-01-07 21:37:30 +00004598 // Currently only do something sane when at most two source vectors
4599 // involved.
4600 if (SourceVecs.size() > 2)
4601 return SDValue();
4602
4603 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4604 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004605
Bob Wilson11a1dff2011-01-07 21:37:30 +00004606 // This loop extracts the usage patterns of the source vectors
4607 // and prepares appropriate SDValues for a shuffle if possible.
4608 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4609 if (SourceVecs[i].getValueType() == VT) {
4610 // No VEXT necessary
4611 ShuffleSrcs[i] = SourceVecs[i];
4612 VEXTOffsets[i] = 0;
4613 continue;
4614 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4615 // It probably isn't worth padding out a smaller vector just to
4616 // break it down again in a shuffle.
4617 return SDValue();
4618 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004619
Bob Wilson11a1dff2011-01-07 21:37:30 +00004620 // Since only 64-bit and 128-bit vectors are legal on ARM and
4621 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004622 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4623 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004624
Bob Wilson11a1dff2011-01-07 21:37:30 +00004625 if (MaxElts[i] - MinElts[i] >= NumElts) {
4626 // Span too large for a VEXT to cope
4627 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004628 }
4629
Bob Wilson11a1dff2011-01-07 21:37:30 +00004630 if (MinElts[i] >= NumElts) {
4631 // The extraction can just take the second half
4632 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004633 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4634 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004635 DAG.getIntPtrConstant(NumElts));
4636 } else if (MaxElts[i] < NumElts) {
4637 // The extraction can just take the first half
4638 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004639 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4640 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004641 DAG.getIntPtrConstant(0));
4642 } else {
4643 // An actual VEXT is needed
4644 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004645 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4646 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004647 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004648 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4649 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004650 DAG.getIntPtrConstant(NumElts));
4651 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4652 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4653 }
4654 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004655
Bob Wilson11a1dff2011-01-07 21:37:30 +00004656 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004657
Bob Wilson11a1dff2011-01-07 21:37:30 +00004658 for (unsigned i = 0; i < NumElts; ++i) {
4659 SDValue Entry = Op.getOperand(i);
4660 if (Entry.getOpcode() == ISD::UNDEF) {
4661 Mask.push_back(-1);
4662 continue;
4663 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004664
Bob Wilson11a1dff2011-01-07 21:37:30 +00004665 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004666 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4667 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004668 if (ExtractVec == SourceVecs[0]) {
4669 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4670 } else {
4671 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4672 }
4673 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004674
Bob Wilson11a1dff2011-01-07 21:37:30 +00004675 // Final check before we try to produce nonsense...
4676 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004677 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4678 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004679
Bob Wilson11a1dff2011-01-07 21:37:30 +00004680 return SDValue();
4681}
4682
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004683/// isShuffleMaskLegal - Targets can use this to indicate that they only
4684/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4685/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4686/// are assumed to be legal.
4687bool
4688ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4689 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004690 if (VT.getVectorNumElements() == 4 &&
4691 (VT.is128BitVector() || VT.is64BitVector())) {
4692 unsigned PFIndexes[4];
4693 for (unsigned i = 0; i != 4; ++i) {
4694 if (M[i] < 0)
4695 PFIndexes[i] = 8;
4696 else
4697 PFIndexes[i] = M[i];
4698 }
4699
4700 // Compute the index in the perfect shuffle table.
4701 unsigned PFTableIndex =
4702 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4703 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4704 unsigned Cost = (PFEntry >> 30);
4705
4706 if (Cost <= 4)
4707 return true;
4708 }
4709
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004710 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004711 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004712
Bob Wilson53dd2452010-06-07 23:53:38 +00004713 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4714 return (EltSize >= 32 ||
4715 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004716 isVREVMask(M, VT, 64) ||
4717 isVREVMask(M, VT, 32) ||
4718 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004719 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004720 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004721 isVTRNMask(M, VT, WhichResult) ||
4722 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004723 isVZIPMask(M, VT, WhichResult) ||
4724 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4725 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004726 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
4727 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004728}
4729
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004730/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4731/// the specified operations to build the shuffle.
4732static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4733 SDValue RHS, SelectionDAG &DAG,
4734 DebugLoc dl) {
4735 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4736 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4737 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4738
4739 enum {
4740 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4741 OP_VREV,
4742 OP_VDUP0,
4743 OP_VDUP1,
4744 OP_VDUP2,
4745 OP_VDUP3,
4746 OP_VEXT1,
4747 OP_VEXT2,
4748 OP_VEXT3,
4749 OP_VUZPL, // VUZP, left result
4750 OP_VUZPR, // VUZP, right result
4751 OP_VZIPL, // VZIP, left result
4752 OP_VZIPR, // VZIP, right result
4753 OP_VTRNL, // VTRN, left result
4754 OP_VTRNR // VTRN, right result
4755 };
4756
4757 if (OpNum == OP_COPY) {
4758 if (LHSID == (1*9+2)*9+3) return LHS;
4759 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4760 return RHS;
4761 }
4762
4763 SDValue OpLHS, OpRHS;
4764 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4765 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4766 EVT VT = OpLHS.getValueType();
4767
4768 switch (OpNum) {
4769 default: llvm_unreachable("Unknown shuffle opcode!");
4770 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004771 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004772 if (VT.getVectorElementType() == MVT::i32 ||
4773 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004774 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4775 // vrev <4 x i16> -> VREV32
4776 if (VT.getVectorElementType() == MVT::i16)
4777 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4778 // vrev <4 x i8> -> VREV16
4779 assert(VT.getVectorElementType() == MVT::i8);
4780 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004781 case OP_VDUP0:
4782 case OP_VDUP1:
4783 case OP_VDUP2:
4784 case OP_VDUP3:
4785 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004786 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004787 case OP_VEXT1:
4788 case OP_VEXT2:
4789 case OP_VEXT3:
4790 return DAG.getNode(ARMISD::VEXT, dl, VT,
4791 OpLHS, OpRHS,
4792 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4793 case OP_VUZPL:
4794 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004795 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004796 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4797 case OP_VZIPL:
4798 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004799 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004800 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4801 case OP_VTRNL:
4802 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004803 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4804 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004805 }
4806}
4807
Bill Wendling69a05a72011-03-14 23:02:38 +00004808static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004809 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004810 SelectionDAG &DAG) {
4811 // Check to see if we can use the VTBL instruction.
4812 SDValue V1 = Op.getOperand(0);
4813 SDValue V2 = Op.getOperand(1);
4814 DebugLoc DL = Op.getDebugLoc();
4815
4816 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004817 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004818 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4819 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4820
4821 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4822 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4823 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4824 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004825
Owen Anderson76706012011-04-05 21:48:57 +00004826 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004827 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4828 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004829}
4830
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004831static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
4832 SelectionDAG &DAG) {
4833 DebugLoc DL = Op.getDebugLoc();
4834 SDValue OpLHS = Op.getOperand(0);
4835 EVT VT = OpLHS.getValueType();
4836
4837 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
4838 "Expect an v8i16/v16i8 type");
4839 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
4840 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
4841 // extract the first 8 bytes into the top double word and the last 8 bytes
4842 // into the bottom double word. The v8i16 case is similar.
4843 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
4844 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
4845 DAG.getConstant(ExtractNum, MVT::i32));
4846}
4847
Bob Wilson5bafff32009-06-22 23:27:02 +00004848static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004849 SDValue V1 = Op.getOperand(0);
4850 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004851 DebugLoc dl = Op.getDebugLoc();
4852 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004853 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004854
Bob Wilson28865062009-08-13 02:13:04 +00004855 // Convert shuffles that are directly supported on NEON to target-specific
4856 // DAG nodes, instead of keeping them as shuffles and matching them again
4857 // during code selection. This is more efficient and avoids the possibility
4858 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004859 // FIXME: floating-point vectors should be canonicalized to integer vectors
4860 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004861 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004862
Bob Wilson53dd2452010-06-07 23:53:38 +00004863 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4864 if (EltSize <= 32) {
4865 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4866 int Lane = SVN->getSplatIndex();
4867 // If this is undef splat, generate it via "just" vdup, if possible.
4868 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004869
Dan Gohman65fd6562011-11-03 21:49:52 +00004870 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004871 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4872 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4873 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004874 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4875 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4876 // reaches it).
4877 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4878 !isa<ConstantSDNode>(V1.getOperand(0))) {
4879 bool IsScalarToVector = true;
4880 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4881 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4882 IsScalarToVector = false;
4883 break;
4884 }
4885 if (IsScalarToVector)
4886 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4887 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004888 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4889 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004890 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004891
4892 bool ReverseVEXT;
4893 unsigned Imm;
4894 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4895 if (ReverseVEXT)
4896 std::swap(V1, V2);
4897 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4898 DAG.getConstant(Imm, MVT::i32));
4899 }
4900
4901 if (isVREVMask(ShuffleMask, VT, 64))
4902 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4903 if (isVREVMask(ShuffleMask, VT, 32))
4904 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4905 if (isVREVMask(ShuffleMask, VT, 16))
4906 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4907
Quentin Colombet43934ae2012-11-02 21:32:17 +00004908 if (V2->getOpcode() == ISD::UNDEF &&
4909 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4910 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4911 DAG.getConstant(Imm, MVT::i32));
4912 }
4913
Bob Wilson53dd2452010-06-07 23:53:38 +00004914 // Check for Neon shuffles that modify both input vectors in place.
4915 // If both results are used, i.e., if there are two shuffles with the same
4916 // source operands and with masks corresponding to both results of one of
4917 // these operations, DAG memoization will ensure that a single node is
4918 // used for both shuffles.
4919 unsigned WhichResult;
4920 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4921 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4922 V1, V2).getValue(WhichResult);
4923 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4924 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4925 V1, V2).getValue(WhichResult);
4926 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4927 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4928 V1, V2).getValue(WhichResult);
4929
4930 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4931 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4932 V1, V1).getValue(WhichResult);
4933 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4934 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4935 V1, V1).getValue(WhichResult);
4936 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4937 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4938 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004939 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004940
Bob Wilsonc692cb72009-08-21 20:54:19 +00004941 // If the shuffle is not directly supported and it has 4 elements, use
4942 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004943 unsigned NumElts = VT.getVectorNumElements();
4944 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004945 unsigned PFIndexes[4];
4946 for (unsigned i = 0; i != 4; ++i) {
4947 if (ShuffleMask[i] < 0)
4948 PFIndexes[i] = 8;
4949 else
4950 PFIndexes[i] = ShuffleMask[i];
4951 }
4952
4953 // Compute the index in the perfect shuffle table.
4954 unsigned PFTableIndex =
4955 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004956 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4957 unsigned Cost = (PFEntry >> 30);
4958
4959 if (Cost <= 4)
4960 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4961 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004962
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004963 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004964 if (EltSize >= 32) {
4965 // Do the expansion with floating-point types, since that is what the VFP
4966 // registers are defined to use, and since i64 is not legal.
4967 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4968 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004969 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4970 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004971 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004972 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004973 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004974 Ops.push_back(DAG.getUNDEF(EltVT));
4975 else
4976 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4977 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4978 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4979 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004980 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004981 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004982 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004983 }
4984
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004985 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
4986 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
4987
Bill Wendling69a05a72011-03-14 23:02:38 +00004988 if (VT == MVT::v8i8) {
4989 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4990 if (NewOp.getNode())
4991 return NewOp;
4992 }
4993
Bob Wilson22cac0d2009-08-14 05:16:33 +00004994 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004995}
4996
Eli Friedman5c89cb82011-10-24 23:08:52 +00004997static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4998 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4999 SDValue Lane = Op.getOperand(2);
5000 if (!isa<ConstantSDNode>(Lane))
5001 return SDValue();
5002
5003 return Op;
5004}
5005
Bob Wilson5bafff32009-06-22 23:27:02 +00005006static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00005007 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00005008 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00005009 if (!isa<ConstantSDNode>(Lane))
5010 return SDValue();
5011
5012 SDValue Vec = Op.getOperand(0);
5013 if (Op.getValueType() == MVT::i32 &&
5014 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
5015 DebugLoc dl = Op.getDebugLoc();
5016 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5017 }
5018
5019 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00005020}
5021
Bob Wilsona6d65862009-08-03 20:36:38 +00005022static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5023 // The only time a CONCAT_VECTORS operation can have legal types is when
5024 // two 64-bit vectors are concatenated to a 128-bit vector.
5025 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5026 "unexpected CONCAT_VECTORS");
5027 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005028 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00005029 SDValue Op0 = Op.getOperand(0);
5030 SDValue Op1 = Op.getOperand(1);
5031 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005033 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00005034 DAG.getIntPtrConstant(0));
5035 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005036 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005037 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00005038 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005039 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00005040}
5041
Bob Wilson626613d2010-11-23 19:38:38 +00005042/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5043/// element has been zero/sign-extended, depending on the isSigned parameter,
5044/// from an integer type half its size.
5045static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5046 bool isSigned) {
5047 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5048 EVT VT = N->getValueType(0);
5049 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5050 SDNode *BVN = N->getOperand(0).getNode();
5051 if (BVN->getValueType(0) != MVT::v4i32 ||
5052 BVN->getOpcode() != ISD::BUILD_VECTOR)
5053 return false;
5054 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5055 unsigned HiElt = 1 - LoElt;
5056 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5057 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5058 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5059 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5060 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5061 return false;
5062 if (isSigned) {
5063 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5064 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5065 return true;
5066 } else {
5067 if (Hi0->isNullValue() && Hi1->isNullValue())
5068 return true;
5069 }
5070 return false;
5071 }
5072
5073 if (N->getOpcode() != ISD::BUILD_VECTOR)
5074 return false;
5075
5076 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5077 SDNode *Elt = N->getOperand(i).getNode();
5078 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5079 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5080 unsigned HalfSize = EltSize / 2;
5081 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00005082 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005083 return false;
5084 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00005085 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005086 return false;
5087 }
5088 continue;
5089 }
5090 return false;
5091 }
5092
5093 return true;
5094}
5095
5096/// isSignExtended - Check if a node is a vector value that is sign-extended
5097/// or a constant BUILD_VECTOR with sign-extended elements.
5098static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5099 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5100 return true;
5101 if (isExtendedBUILD_VECTOR(N, DAG, true))
5102 return true;
5103 return false;
5104}
5105
5106/// isZeroExtended - Check if a node is a vector value that is zero-extended
5107/// or a constant BUILD_VECTOR with zero-extended elements.
5108static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5109 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5110 return true;
5111 if (isExtendedBUILD_VECTOR(N, DAG, false))
5112 return true;
5113 return false;
5114}
5115
Sebastian Popcb495302012-11-30 19:08:04 +00005116/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5117/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5118/// We insert the required extension here to get the vector to fill a D register.
5119static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5120 const EVT &OrigTy,
5121 const EVT &ExtTy,
5122 unsigned ExtOpcode) {
5123 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5124 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5125 // 64-bits we need to insert a new extension so that it will be 64-bits.
5126 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5127 if (OrigTy.getSizeInBits() >= 64)
5128 return N;
5129
5130 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5131 MVT::SimpleValueType OrigSimpleTy = OrigTy.getSimpleVT().SimpleTy;
5132 EVT NewVT;
5133 switch (OrigSimpleTy) {
5134 default: llvm_unreachable("Unexpected Orig Vector Type");
5135 case MVT::v2i8:
5136 case MVT::v2i16:
5137 NewVT = MVT::v2i32;
5138 break;
5139 case MVT::v4i8:
5140 NewVT = MVT::v4i16;
5141 break;
5142 }
5143 return DAG.getNode(ExtOpcode, N->getDebugLoc(), NewVT, N);
5144}
5145
5146/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5147/// does not do any sign/zero extension. If the original vector is less
5148/// than 64 bits, an appropriate extension will be added after the load to
5149/// reach a total size of 64 bits. We have to add the extension separately
5150/// because ARM does not have a sign/zero extending load for vectors.
5151static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5152 SDValue NonExtendingLoad =
5153 DAG.getLoad(LD->getMemoryVT(), LD->getDebugLoc(), LD->getChain(),
5154 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5155 LD->isNonTemporal(), LD->isInvariant(),
5156 LD->getAlignment());
5157 unsigned ExtOp = 0;
5158 switch (LD->getExtensionType()) {
5159 default: llvm_unreachable("Unexpected LoadExtType");
5160 case ISD::EXTLOAD:
5161 case ISD::SEXTLOAD: ExtOp = ISD::SIGN_EXTEND; break;
5162 case ISD::ZEXTLOAD: ExtOp = ISD::ZERO_EXTEND; break;
5163 }
5164 MVT::SimpleValueType MemType = LD->getMemoryVT().getSimpleVT().SimpleTy;
5165 MVT::SimpleValueType ExtType = LD->getValueType(0).getSimpleVT().SimpleTy;
5166 return AddRequiredExtensionForVMULL(NonExtendingLoad, DAG,
5167 MemType, ExtType, ExtOp);
5168}
5169
5170/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5171/// extending load, or BUILD_VECTOR with extended elements, return the
5172/// unextended value. The unextended vector should be 64 bits so that it can
5173/// be used as an operand to a VMULL instruction. If the original vector size
5174/// before extension is less than 64 bits we add a an extension to resize
5175/// the vector to 64 bits.
5176static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005177 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popcb495302012-11-30 19:08:04 +00005178 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5179 N->getOperand(0)->getValueType(0),
5180 N->getValueType(0),
5181 N->getOpcode());
5182
Bob Wilson626613d2010-11-23 19:38:38 +00005183 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popcb495302012-11-30 19:08:04 +00005184 return SkipLoadExtensionForVMULL(LD, DAG);
5185
Bob Wilson626613d2010-11-23 19:38:38 +00005186 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5187 // have been legalized as a BITCAST from v4i32.
5188 if (N->getOpcode() == ISD::BITCAST) {
5189 SDNode *BVN = N->getOperand(0).getNode();
5190 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5191 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5192 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5193 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
5194 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5195 }
5196 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5197 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5198 EVT VT = N->getValueType(0);
5199 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5200 unsigned NumElts = VT.getVectorNumElements();
5201 MVT TruncVT = MVT::getIntegerVT(EltSize);
5202 SmallVector<SDValue, 8> Ops;
5203 for (unsigned i = 0; i != NumElts; ++i) {
5204 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5205 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00005206 // Element types smaller than 32 bits are not legal, so use i32 elements.
5207 // The values are implicitly truncated so sext vs. zext doesn't matter.
5208 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00005209 }
5210 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5211 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005212}
5213
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005214static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5215 unsigned Opcode = N->getOpcode();
5216 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5217 SDNode *N0 = N->getOperand(0).getNode();
5218 SDNode *N1 = N->getOperand(1).getNode();
5219 return N0->hasOneUse() && N1->hasOneUse() &&
5220 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5221 }
5222 return false;
5223}
5224
5225static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5226 unsigned Opcode = N->getOpcode();
5227 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5228 SDNode *N0 = N->getOperand(0).getNode();
5229 SDNode *N1 = N->getOperand(1).getNode();
5230 return N0->hasOneUse() && N1->hasOneUse() &&
5231 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5232 }
5233 return false;
5234}
5235
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005236static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5237 // Multiplications are only custom-lowered for 128-bit vectors so that
5238 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5239 EVT VT = Op.getValueType();
Sebastian Popcb495302012-11-30 19:08:04 +00005240 assert(VT.is128BitVector() && VT.isInteger() &&
5241 "unexpected type for custom-lowering ISD::MUL");
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005242 SDNode *N0 = Op.getOperand(0).getNode();
5243 SDNode *N1 = Op.getOperand(1).getNode();
5244 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005245 bool isMLA = false;
5246 bool isN0SExt = isSignExtended(N0, DAG);
5247 bool isN1SExt = isSignExtended(N1, DAG);
5248 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005249 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005250 else {
5251 bool isN0ZExt = isZeroExtended(N0, DAG);
5252 bool isN1ZExt = isZeroExtended(N1, DAG);
5253 if (isN0ZExt && isN1ZExt)
5254 NewOpc = ARMISD::VMULLu;
5255 else if (isN1SExt || isN1ZExt) {
5256 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5257 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5258 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5259 NewOpc = ARMISD::VMULLs;
5260 isMLA = true;
5261 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5262 NewOpc = ARMISD::VMULLu;
5263 isMLA = true;
5264 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5265 std::swap(N0, N1);
5266 NewOpc = ARMISD::VMULLu;
5267 isMLA = true;
5268 }
5269 }
5270
5271 if (!NewOpc) {
5272 if (VT == MVT::v2i64)
5273 // Fall through to expand this. It is not legal.
5274 return SDValue();
5275 else
5276 // Other vector multiplications are legal.
5277 return Op;
5278 }
5279 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005280
5281 // Legalize to a VMULL instruction.
5282 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005283 SDValue Op0;
Sebastian Popcb495302012-11-30 19:08:04 +00005284 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005285 if (!isMLA) {
Sebastian Popcb495302012-11-30 19:08:04 +00005286 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005287 assert(Op0.getValueType().is64BitVector() &&
5288 Op1.getValueType().is64BitVector() &&
5289 "unexpected types for extended operands to VMULL");
5290 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5291 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005292
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005293 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5294 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5295 // vmull q0, d4, d6
5296 // vmlal q0, d5, d6
5297 // is faster than
5298 // vaddl q0, d4, d5
5299 // vmovl q1, d6
5300 // vmul q0, q0, q1
Sebastian Popcb495302012-11-30 19:08:04 +00005301 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5302 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005303 EVT Op1VT = Op1.getValueType();
5304 return DAG.getNode(N0->getOpcode(), DL, VT,
5305 DAG.getNode(NewOpc, DL, VT,
5306 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5307 DAG.getNode(NewOpc, DL, VT,
5308 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005309}
5310
Owen Anderson76706012011-04-05 21:48:57 +00005311static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005312LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5313 // Convert to float
5314 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5315 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5316 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5317 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5318 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5319 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5320 // Get reciprocal estimate.
5321 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005322 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005323 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5324 // Because char has a smaller range than uchar, we can actually get away
5325 // without any newton steps. This requires that we use a weird bias
5326 // of 0xb000, however (again, this has been exhaustively tested).
5327 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5328 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5329 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5330 Y = DAG.getConstant(0xb000, MVT::i32);
5331 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5332 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5333 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5334 // Convert back to short.
5335 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5336 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5337 return X;
5338}
5339
Owen Anderson76706012011-04-05 21:48:57 +00005340static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005341LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5342 SDValue N2;
5343 // Convert to float.
5344 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5345 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5346 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5347 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5348 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5349 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005350
Nate Begeman7973f352011-02-11 20:53:29 +00005351 // Use reciprocal estimate and one refinement step.
5352 // float4 recip = vrecpeq_f32(yf);
5353 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005354 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005355 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005356 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005357 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5358 N1, N2);
5359 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5360 // Because short has a smaller range than ushort, we can actually get away
5361 // with only a single newton step. This requires that we use a weird bias
5362 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005363 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005364 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5365 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005366 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005367 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5368 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5369 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5370 // Convert back to integer and return.
5371 // return vmovn_s32(vcvt_s32_f32(result));
5372 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5373 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5374 return N0;
5375}
5376
5377static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5378 EVT VT = Op.getValueType();
5379 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5380 "unexpected type for custom-lowering ISD::SDIV");
5381
5382 DebugLoc dl = Op.getDebugLoc();
5383 SDValue N0 = Op.getOperand(0);
5384 SDValue N1 = Op.getOperand(1);
5385 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005386
Nate Begeman7973f352011-02-11 20:53:29 +00005387 if (VT == MVT::v8i8) {
5388 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5389 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005390
Nate Begeman7973f352011-02-11 20:53:29 +00005391 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5392 DAG.getIntPtrConstant(4));
5393 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005394 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005395 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5396 DAG.getIntPtrConstant(0));
5397 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5398 DAG.getIntPtrConstant(0));
5399
5400 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5401 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5402
5403 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5404 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005405
Nate Begeman7973f352011-02-11 20:53:29 +00005406 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5407 return N0;
5408 }
5409 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5410}
5411
5412static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5413 EVT VT = Op.getValueType();
5414 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5415 "unexpected type for custom-lowering ISD::UDIV");
5416
5417 DebugLoc dl = Op.getDebugLoc();
5418 SDValue N0 = Op.getOperand(0);
5419 SDValue N1 = Op.getOperand(1);
5420 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005421
Nate Begeman7973f352011-02-11 20:53:29 +00005422 if (VT == MVT::v8i8) {
5423 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5424 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005425
Nate Begeman7973f352011-02-11 20:53:29 +00005426 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5427 DAG.getIntPtrConstant(4));
5428 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005429 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005430 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5431 DAG.getIntPtrConstant(0));
5432 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5433 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005434
Nate Begeman7973f352011-02-11 20:53:29 +00005435 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5436 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005437
Nate Begeman7973f352011-02-11 20:53:29 +00005438 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5439 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005440
5441 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005442 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5443 N0);
5444 return N0;
5445 }
Owen Anderson76706012011-04-05 21:48:57 +00005446
Nate Begeman7973f352011-02-11 20:53:29 +00005447 // v4i16 sdiv ... Convert to float.
5448 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5449 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5450 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5451 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5452 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005453 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005454
5455 // Use reciprocal estimate and two refinement steps.
5456 // float4 recip = vrecpeq_f32(yf);
5457 // recip *= vrecpsq_f32(yf, recip);
5458 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005459 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005460 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005461 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005462 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005463 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005464 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005465 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005466 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005467 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005468 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5469 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5470 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5471 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005472 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005473 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5474 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5475 N1 = DAG.getConstant(2, MVT::i32);
5476 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5477 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5478 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5479 // Convert back to integer and return.
5480 // return vmovn_u32(vcvt_s32_f32(result));
5481 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5482 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5483 return N0;
5484}
5485
Evan Cheng342e3162011-08-30 01:34:54 +00005486static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5487 EVT VT = Op.getNode()->getValueType(0);
5488 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5489
5490 unsigned Opc;
5491 bool ExtraOp = false;
5492 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005493 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005494 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5495 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5496 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5497 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5498 }
5499
5500 if (!ExtraOp)
5501 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5502 Op.getOperand(1));
5503 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5504 Op.getOperand(1), Op.getOperand(2));
5505}
5506
Eli Friedman74bf18c2011-09-15 22:26:18 +00005507static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005508 // Monotonic load/store is legal for all targets
5509 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5510 return Op;
5511
5512 // Aquire/Release load/store is not legal for targets without a
5513 // dmb or equivalent available.
5514 return SDValue();
5515}
5516
5517
Eli Friedman2bdffe42011-08-31 00:31:29 +00005518static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005519ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5520 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005521 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005522 assert (Node->getValueType(0) == MVT::i64 &&
5523 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005524
Eli Friedman4d3f3292011-08-31 17:52:22 +00005525 SmallVector<SDValue, 6> Ops;
5526 Ops.push_back(Node->getOperand(0)); // Chain
5527 Ops.push_back(Node->getOperand(1)); // Ptr
5528 // Low part of Val1
5529 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5530 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5531 // High part of Val1
5532 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5533 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005534 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005535 // High part of Val1
5536 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5537 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5538 // High part of Val2
5539 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5540 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5541 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005542 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5543 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005544 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005545 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005546 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005547 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5548 Results.push_back(Result.getValue(2));
5549}
5550
Dan Gohmand858e902010-04-17 15:26:15 +00005551SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005552 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005553 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005554 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005555 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005556 case ISD::GlobalAddress:
5557 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5558 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005559 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005560 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005561 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5562 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005563 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005564 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005565 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005566 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005567 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005568 case ISD::SINT_TO_FP:
5569 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5570 case ISD::FP_TO_SINT:
5571 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005572 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005573 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005574 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005575 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005576 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005577 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005578 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5579 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005580 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005581 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005582 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005583 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005584 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005585 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005586 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005587 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengc8e70452012-12-04 22:41:50 +00005588 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005589 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005590 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005591 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005592 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005593 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005594 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005595 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005596 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005597 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005598 case ISD::SDIV: return LowerSDIV(Op, DAG);
5599 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005600 case ISD::ADDC:
5601 case ISD::ADDE:
5602 case ISD::SUBC:
5603 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005604 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005605 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005606 }
Evan Chenga8e29892007-01-19 07:51:42 +00005607}
5608
Duncan Sands1607f052008-12-01 11:39:25 +00005609/// ReplaceNodeResults - Replace the results of node with an illegal result
5610/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005611void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5612 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005613 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005614 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005615 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005616 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005617 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005618 case ISD::BITCAST:
5619 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005620 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005621 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005622 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005623 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005624 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005625 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005626 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005627 return;
5628 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005629 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005630 return;
5631 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005632 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005633 return;
5634 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005635 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005636 return;
5637 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005638 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005639 return;
5640 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005641 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005642 return;
5643 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005644 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005645 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005646 case ISD::ATOMIC_CMP_SWAP:
5647 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5648 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005649 case ISD::ATOMIC_LOAD_MIN:
5650 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5651 return;
5652 case ISD::ATOMIC_LOAD_UMIN:
5653 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5654 return;
5655 case ISD::ATOMIC_LOAD_MAX:
5656 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5657 return;
5658 case ISD::ATOMIC_LOAD_UMAX:
5659 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5660 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005661 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005662 if (Res.getNode())
5663 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005664}
Chris Lattner27a6c732007-11-24 07:07:01 +00005665
Evan Chenga8e29892007-01-19 07:51:42 +00005666//===----------------------------------------------------------------------===//
5667// ARM Scheduler Hooks
5668//===----------------------------------------------------------------------===//
5669
5670MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005671ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5672 MachineBasicBlock *BB,
5673 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005674 unsigned dest = MI->getOperand(0).getReg();
5675 unsigned ptr = MI->getOperand(1).getReg();
5676 unsigned oldval = MI->getOperand(2).getReg();
5677 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005678 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5679 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005680 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005681
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005682 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005683 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5684 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5685 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005686
5687 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005688 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5689 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5690 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005691 }
5692
Jim Grosbach5278eb82009-12-11 01:42:04 +00005693 unsigned ldrOpc, strOpc;
5694 switch (Size) {
5695 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005696 case 1:
5697 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005698 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005699 break;
5700 case 2:
5701 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5702 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5703 break;
5704 case 4:
5705 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5706 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5707 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005708 }
5709
5710 MachineFunction *MF = BB->getParent();
5711 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5712 MachineFunction::iterator It = BB;
5713 ++It; // insert the new blocks after the current block
5714
5715 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5716 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5717 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5718 MF->insert(It, loop1MBB);
5719 MF->insert(It, loop2MBB);
5720 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005721
5722 // Transfer the remainder of BB and its successor edges to exitMBB.
5723 exitMBB->splice(exitMBB->begin(), BB,
5724 llvm::next(MachineBasicBlock::iterator(MI)),
5725 BB->end());
5726 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005727
5728 // thisMBB:
5729 // ...
5730 // fallthrough --> loop1MBB
5731 BB->addSuccessor(loop1MBB);
5732
5733 // loop1MBB:
5734 // ldrex dest, [ptr]
5735 // cmp dest, oldval
5736 // bne exitMBB
5737 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005738 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5739 if (ldrOpc == ARM::t2LDREX)
5740 MIB.addImm(0);
5741 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005742 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005743 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005744 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5745 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005746 BB->addSuccessor(loop2MBB);
5747 BB->addSuccessor(exitMBB);
5748
5749 // loop2MBB:
5750 // strex scratch, newval, [ptr]
5751 // cmp scratch, #0
5752 // bne loop1MBB
5753 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005754 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5755 if (strOpc == ARM::t2STREX)
5756 MIB.addImm(0);
5757 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005758 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005759 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005760 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5761 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005762 BB->addSuccessor(loop1MBB);
5763 BB->addSuccessor(exitMBB);
5764
5765 // exitMBB:
5766 // ...
5767 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005768
Dan Gohman14152b42010-07-06 20:24:04 +00005769 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005770
Jim Grosbach5278eb82009-12-11 01:42:04 +00005771 return BB;
5772}
5773
5774MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005775ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5776 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005777 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5778 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5779
5780 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005781 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005782 MachineFunction::iterator It = BB;
5783 ++It;
5784
5785 unsigned dest = MI->getOperand(0).getReg();
5786 unsigned ptr = MI->getOperand(1).getReg();
5787 unsigned incr = MI->getOperand(2).getReg();
5788 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005789 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005790
5791 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5792 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005793 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5794 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005795 }
5796
Jim Grosbachc3c23542009-12-14 04:22:04 +00005797 unsigned ldrOpc, strOpc;
5798 switch (Size) {
5799 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005800 case 1:
5801 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005802 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005803 break;
5804 case 2:
5805 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5806 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5807 break;
5808 case 4:
5809 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5810 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5811 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005812 }
5813
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005814 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5815 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5816 MF->insert(It, loopMBB);
5817 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005818
5819 // Transfer the remainder of BB and its successor edges to exitMBB.
5820 exitMBB->splice(exitMBB->begin(), BB,
5821 llvm::next(MachineBasicBlock::iterator(MI)),
5822 BB->end());
5823 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005824
Craig Topper420761a2012-04-20 07:30:17 +00005825 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005826 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005827 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005828 unsigned scratch = MRI.createVirtualRegister(TRC);
5829 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005830
5831 // thisMBB:
5832 // ...
5833 // fallthrough --> loopMBB
5834 BB->addSuccessor(loopMBB);
5835
5836 // loopMBB:
5837 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005838 // <binop> scratch2, dest, incr
5839 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005840 // cmp scratch, #0
5841 // bne- loopMBB
5842 // fallthrough --> exitMBB
5843 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005844 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5845 if (ldrOpc == ARM::t2LDREX)
5846 MIB.addImm(0);
5847 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005848 if (BinOpcode) {
5849 // operand order needs to go the other way for NAND
5850 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5851 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5852 addReg(incr).addReg(dest)).addReg(0);
5853 else
5854 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5855 addReg(dest).addReg(incr)).addReg(0);
5856 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005857
Jim Grosbachb6aed502011-09-09 18:37:27 +00005858 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5859 if (strOpc == ARM::t2STREX)
5860 MIB.addImm(0);
5861 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005862 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005863 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005864 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5865 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005866
5867 BB->addSuccessor(loopMBB);
5868 BB->addSuccessor(exitMBB);
5869
5870 // exitMBB:
5871 // ...
5872 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005873
Dan Gohman14152b42010-07-06 20:24:04 +00005874 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005875
Jim Grosbachc3c23542009-12-14 04:22:04 +00005876 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005877}
5878
Jim Grosbachf7da8822011-04-26 19:44:18 +00005879MachineBasicBlock *
5880ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5881 MachineBasicBlock *BB,
5882 unsigned Size,
5883 bool signExtend,
5884 ARMCC::CondCodes Cond) const {
5885 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5886
5887 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5888 MachineFunction *MF = BB->getParent();
5889 MachineFunction::iterator It = BB;
5890 ++It;
5891
5892 unsigned dest = MI->getOperand(0).getReg();
5893 unsigned ptr = MI->getOperand(1).getReg();
5894 unsigned incr = MI->getOperand(2).getReg();
5895 unsigned oldval = dest;
5896 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005897 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005898
5899 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5900 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005901 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5902 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005903 }
5904
Jim Grosbachf7da8822011-04-26 19:44:18 +00005905 unsigned ldrOpc, strOpc, extendOpc;
5906 switch (Size) {
5907 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5908 case 1:
5909 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5910 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005911 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005912 break;
5913 case 2:
5914 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5915 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005916 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005917 break;
5918 case 4:
5919 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5920 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5921 extendOpc = 0;
5922 break;
5923 }
5924
5925 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5926 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5927 MF->insert(It, loopMBB);
5928 MF->insert(It, exitMBB);
5929
5930 // Transfer the remainder of BB and its successor edges to exitMBB.
5931 exitMBB->splice(exitMBB->begin(), BB,
5932 llvm::next(MachineBasicBlock::iterator(MI)),
5933 BB->end());
5934 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5935
Craig Topper420761a2012-04-20 07:30:17 +00005936 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005937 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005938 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005939 unsigned scratch = MRI.createVirtualRegister(TRC);
5940 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005941
5942 // thisMBB:
5943 // ...
5944 // fallthrough --> loopMBB
5945 BB->addSuccessor(loopMBB);
5946
5947 // loopMBB:
5948 // ldrex dest, ptr
5949 // (sign extend dest, if required)
5950 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005951 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005952 // strex scratch, scratch2, ptr
5953 // cmp scratch, #0
5954 // bne- loopMBB
5955 // fallthrough --> exitMBB
5956 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005957 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5958 if (ldrOpc == ARM::t2LDREX)
5959 MIB.addImm(0);
5960 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005961
5962 // Sign extend the value, if necessary.
5963 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005964 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005965 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5966 .addReg(dest)
5967 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005968 }
5969
5970 // Build compare and cmov instructions.
5971 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5972 .addReg(oldval).addReg(incr));
5973 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005974 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005975
Jim Grosbachb6aed502011-09-09 18:37:27 +00005976 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5977 if (strOpc == ARM::t2STREX)
5978 MIB.addImm(0);
5979 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005980 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5981 .addReg(scratch).addImm(0));
5982 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5983 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5984
5985 BB->addSuccessor(loopMBB);
5986 BB->addSuccessor(exitMBB);
5987
5988 // exitMBB:
5989 // ...
5990 BB = exitMBB;
5991
5992 MI->eraseFromParent(); // The instruction is gone now.
5993
5994 return BB;
5995}
5996
Eli Friedman2bdffe42011-08-31 00:31:29 +00005997MachineBasicBlock *
5998ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5999 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00006000 bool NeedsCarry, bool IsCmpxchg,
6001 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006002 // This also handles ATOMIC_SWAP, indicated by Op1==0.
6003 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6004
6005 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6006 MachineFunction *MF = BB->getParent();
6007 MachineFunction::iterator It = BB;
6008 ++It;
6009
6010 unsigned destlo = MI->getOperand(0).getReg();
6011 unsigned desthi = MI->getOperand(1).getReg();
6012 unsigned ptr = MI->getOperand(2).getReg();
6013 unsigned vallo = MI->getOperand(3).getReg();
6014 unsigned valhi = MI->getOperand(4).getReg();
6015 DebugLoc dl = MI->getDebugLoc();
6016 bool isThumb2 = Subtarget->isThumb2();
6017
6018 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6019 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00006020 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
6021 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
6022 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006023 }
6024
Eli Friedman2bdffe42011-08-31 00:31:29 +00006025 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00006026 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006027 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006028 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006029 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006030 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006031 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006032
Eli Friedman2bdffe42011-08-31 00:31:29 +00006033 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006034 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
6035 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006036 MF->insert(It, exitMBB);
6037
6038 // Transfer the remainder of BB and its successor edges to exitMBB.
6039 exitMBB->splice(exitMBB->begin(), BB,
6040 llvm::next(MachineBasicBlock::iterator(MI)),
6041 BB->end());
6042 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6043
Craig Topper420761a2012-04-20 07:30:17 +00006044 const TargetRegisterClass *TRC = isThumb2 ?
6045 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6046 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006047 unsigned storesuccess = MRI.createVirtualRegister(TRC);
6048
6049 // thisMBB:
6050 // ...
6051 // fallthrough --> loopMBB
6052 BB->addSuccessor(loopMBB);
6053
6054 // loopMBB:
6055 // ldrexd r2, r3, ptr
6056 // <binopa> r0, r2, incr
6057 // <binopb> r1, r3, incr
6058 // strexd storesuccess, r0, r1, ptr
6059 // cmp storesuccess, #0
6060 // bne- loopMBB
6061 // fallthrough --> exitMBB
Eli Friedman2bdffe42011-08-31 00:31:29 +00006062 BB = loopMBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006063
Eli Friedman2bdffe42011-08-31 00:31:29 +00006064 // Load
Tim Northover0adfded2013-01-29 09:06:13 +00006065 if (isThumb2) {
6066 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2LDREXD))
6067 .addReg(destlo, RegState::Define)
6068 .addReg(desthi, RegState::Define)
6069 .addReg(ptr));
6070 } else {
6071 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6072 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDREXD))
6073 .addReg(GPRPair0, RegState::Define).addReg(ptr));
6074 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
6075 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6076 .addReg(GPRPair0, 0, ARM::gsub_0);
6077 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6078 .addReg(GPRPair0, 0, ARM::gsub_1);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006079 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006080
Tim Northover0adfded2013-01-29 09:06:13 +00006081 unsigned StoreLo, StoreHi;
Eli Friedman4d3f3292011-08-31 17:52:22 +00006082 if (IsCmpxchg) {
6083 // Add early exit
6084 for (unsigned i = 0; i < 2; i++) {
6085 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6086 ARM::CMPrr))
6087 .addReg(i == 0 ? destlo : desthi)
6088 .addReg(i == 0 ? vallo : valhi));
6089 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6090 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6091 BB->addSuccessor(exitMBB);
6092 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6093 BB = (i == 0 ? contBB : cont2BB);
6094 }
6095
6096 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006097 StoreLo = MI->getOperand(5).getReg();
6098 StoreHi = MI->getOperand(6).getReg();
Eli Friedman4d3f3292011-08-31 17:52:22 +00006099 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006100 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006101 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6102 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00006103 .addReg(destlo).addReg(vallo))
6104 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006105 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6106 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006107 .addReg(desthi).addReg(valhi))
6108 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006109
Tim Northover0adfded2013-01-29 09:06:13 +00006110 StoreLo = tmpRegLo;
6111 StoreHi = tmpRegHi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006112 } else {
6113 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006114 StoreLo = vallo;
6115 StoreHi = valhi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006116 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00006117 if (IsMinMax) {
6118 // Compare and branch to exit block.
6119 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6120 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6121 BB->addSuccessor(exitMBB);
6122 BB->addSuccessor(contBB);
6123 BB = contBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006124 StoreLo = vallo;
6125 StoreHi = valhi;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006126 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006127
6128 // Store
Tim Northover0adfded2013-01-29 09:06:13 +00006129 if (isThumb2) {
6130 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2STREXD), storesuccess)
6131 .addReg(StoreLo).addReg(StoreHi).addReg(ptr));
6132 } else {
6133 // Marshal a pair...
6134 unsigned StorePair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6135 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6136 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6137 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6138 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6139 .addReg(UndefPair)
6140 .addReg(StoreLo)
6141 .addImm(ARM::gsub_0);
6142 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), StorePair)
6143 .addReg(r1)
6144 .addReg(StoreHi)
6145 .addImm(ARM::gsub_1);
6146
6147 // ...and store it
6148 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::STREXD), storesuccess)
6149 .addReg(StorePair).addReg(ptr));
6150 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006151 // Cmp+jump
6152 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6153 .addReg(storesuccess).addImm(0));
6154 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6155 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6156
6157 BB->addSuccessor(loopMBB);
6158 BB->addSuccessor(exitMBB);
6159
6160 // exitMBB:
6161 // ...
6162 BB = exitMBB;
6163
6164 MI->eraseFromParent(); // The instruction is gone now.
6165
6166 return BB;
6167}
6168
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006169/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6170/// registers the function context.
6171void ARMTargetLowering::
6172SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6173 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006174 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6175 DebugLoc dl = MI->getDebugLoc();
6176 MachineFunction *MF = MBB->getParent();
6177 MachineRegisterInfo *MRI = &MF->getRegInfo();
6178 MachineConstantPool *MCP = MF->getConstantPool();
6179 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6180 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006181
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006182 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006183 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006184
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006185 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006186 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006187 ARMConstantPoolValue *CPV =
6188 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6189 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6190
Craig Topper420761a2012-04-20 07:30:17 +00006191 const TargetRegisterClass *TRC = isThumb ?
6192 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6193 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006194
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006195 // Grab constant pool and fixed stack memory operands.
6196 MachineMemOperand *CPMMO =
6197 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6198 MachineMemOperand::MOLoad, 4, 4);
6199
6200 MachineMemOperand *FIMMOSt =
6201 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6202 MachineMemOperand::MOStore, 4, 4);
6203
6204 // Load the address of the dispatch MBB into the jump buffer.
6205 if (isThumb2) {
6206 // Incoming value: jbuf
6207 // ldr.n r5, LCPI1_1
6208 // orr r5, r5, #1
6209 // add r5, pc
6210 // str r5, [$jbuf, #+4] ; &jbuf[1]
6211 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6212 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6213 .addConstantPoolIndex(CPI)
6214 .addMemOperand(CPMMO));
6215 // Set the low bit because of thumb mode.
6216 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6217 AddDefaultCC(
6218 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6219 .addReg(NewVReg1, RegState::Kill)
6220 .addImm(0x01)));
6221 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6222 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6223 .addReg(NewVReg2, RegState::Kill)
6224 .addImm(PCLabelId);
6225 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6226 .addReg(NewVReg3, RegState::Kill)
6227 .addFrameIndex(FI)
6228 .addImm(36) // &jbuf[1] :: pc
6229 .addMemOperand(FIMMOSt));
6230 } else if (isThumb) {
6231 // Incoming value: jbuf
6232 // ldr.n r1, LCPI1_4
6233 // add r1, pc
6234 // mov r2, #1
6235 // orrs r1, r2
6236 // add r2, $jbuf, #+4 ; &jbuf[1]
6237 // str r1, [r2]
6238 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6239 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6240 .addConstantPoolIndex(CPI)
6241 .addMemOperand(CPMMO));
6242 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6243 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6244 .addReg(NewVReg1, RegState::Kill)
6245 .addImm(PCLabelId);
6246 // Set the low bit because of thumb mode.
6247 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6248 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6249 .addReg(ARM::CPSR, RegState::Define)
6250 .addImm(1));
6251 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6252 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6253 .addReg(ARM::CPSR, RegState::Define)
6254 .addReg(NewVReg2, RegState::Kill)
6255 .addReg(NewVReg3, RegState::Kill));
6256 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6257 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6258 .addFrameIndex(FI)
6259 .addImm(36)); // &jbuf[1] :: pc
6260 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6261 .addReg(NewVReg4, RegState::Kill)
6262 .addReg(NewVReg5, RegState::Kill)
6263 .addImm(0)
6264 .addMemOperand(FIMMOSt));
6265 } else {
6266 // Incoming value: jbuf
6267 // ldr r1, LCPI1_1
6268 // add r1, pc, r1
6269 // str r1, [$jbuf, #+4] ; &jbuf[1]
6270 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6271 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6272 .addConstantPoolIndex(CPI)
6273 .addImm(0)
6274 .addMemOperand(CPMMO));
6275 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6276 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6277 .addReg(NewVReg1, RegState::Kill)
6278 .addImm(PCLabelId));
6279 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6280 .addReg(NewVReg2, RegState::Kill)
6281 .addFrameIndex(FI)
6282 .addImm(36) // &jbuf[1] :: pc
6283 .addMemOperand(FIMMOSt));
6284 }
6285}
6286
6287MachineBasicBlock *ARMTargetLowering::
6288EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6289 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6290 DebugLoc dl = MI->getDebugLoc();
6291 MachineFunction *MF = MBB->getParent();
6292 MachineRegisterInfo *MRI = &MF->getRegInfo();
6293 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6294 MachineFrameInfo *MFI = MF->getFrameInfo();
6295 int FI = MFI->getFunctionContextIndex();
6296
Craig Topper420761a2012-04-20 07:30:17 +00006297 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6298 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006299 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006300
Bill Wendling04f15b42011-10-06 21:29:56 +00006301 // Get a mapping of the call site numbers to all of the landing pads they're
6302 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006303 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6304 unsigned MaxCSNum = 0;
6305 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006306 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6307 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006308 if (!BB->isLandingPad()) continue;
6309
6310 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6311 // pad.
6312 for (MachineBasicBlock::iterator
6313 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6314 if (!II->isEHLabel()) continue;
6315
6316 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006317 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006318
Bill Wendling5cbef192011-10-05 23:28:57 +00006319 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6320 for (SmallVectorImpl<unsigned>::iterator
6321 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6322 CSI != CSE; ++CSI) {
6323 CallSiteNumToLPad[*CSI].push_back(BB);
6324 MaxCSNum = std::max(MaxCSNum, *CSI);
6325 }
Bill Wendling2a850152011-10-05 00:02:33 +00006326 break;
6327 }
6328 }
6329
6330 // Get an ordered list of the machine basic blocks for the jump table.
6331 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006332 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006333 LPadList.reserve(CallSiteNumToLPad.size());
6334 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6335 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6336 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006337 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006338 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006339 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6340 }
Bill Wendling2a850152011-10-05 00:02:33 +00006341 }
6342
Bill Wendling5cbef192011-10-05 23:28:57 +00006343 assert(!LPadList.empty() &&
6344 "No landing pad destinations for the dispatch jump table!");
6345
Bill Wendling04f15b42011-10-06 21:29:56 +00006346 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006347 MachineJumpTableInfo *JTI =
6348 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6349 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6350 unsigned UId = AFI->createJumpTableUId();
Chad Rosierb8f307b2013-03-01 18:30:38 +00006351 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling2a850152011-10-05 00:02:33 +00006352
Bill Wendling04f15b42011-10-06 21:29:56 +00006353 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006354
6355 // Shove the dispatch's address into the return slot in the function context.
6356 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6357 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006358
Bill Wendlingbb734682011-10-05 00:39:32 +00006359 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky0f156af2013-01-30 16:30:19 +00006360 unsigned trap_opcode;
Chad Rosier279706e2013-02-28 18:54:27 +00006361 if (Subtarget->isThumb())
Eli Bendersky0f156af2013-01-30 16:30:19 +00006362 trap_opcode = ARM::tTRAP;
Chad Rosier279706e2013-02-28 18:54:27 +00006363 else
6364 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6365
Eli Bendersky0f156af2013-01-30 16:30:19 +00006366 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendlingbb734682011-10-05 00:39:32 +00006367 DispatchBB->addSuccessor(TrapBB);
6368
6369 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6370 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006371
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006372 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006373 MF->insert(MF->end(), DispatchBB);
6374 MF->insert(MF->end(), DispContBB);
6375 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006376
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006377 // Insert code into the entry block that creates and registers the function
6378 // context.
6379 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6380
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006381 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006382 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006383 MachineMemOperand::MOLoad |
6384 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006385
Chad Rosiere7bd5192012-11-06 23:05:24 +00006386 MachineInstrBuilder MIB;
6387 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6388
6389 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6390 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6391
6392 // Add a register mask with no preserved registers. This results in all
6393 // registers being marked as clobbered.
6394 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006395
Bill Wendling952cb502011-10-18 22:49:07 +00006396 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006397 if (Subtarget->isThumb2()) {
6398 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6399 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6400 .addFrameIndex(FI)
6401 .addImm(4)
6402 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006403
Bill Wendling952cb502011-10-18 22:49:07 +00006404 if (NumLPads < 256) {
6405 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6406 .addReg(NewVReg1)
6407 .addImm(LPadList.size()));
6408 } else {
6409 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6410 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006411 .addImm(NumLPads & 0xFFFF));
6412
6413 unsigned VReg2 = VReg1;
6414 if ((NumLPads & 0xFFFF0000) != 0) {
6415 VReg2 = MRI->createVirtualRegister(TRC);
6416 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6417 .addReg(VReg1)
6418 .addImm(NumLPads >> 16));
6419 }
6420
Bill Wendling952cb502011-10-18 22:49:07 +00006421 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6422 .addReg(NewVReg1)
6423 .addReg(VReg2));
6424 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006425
Bill Wendling95ce2e92011-10-06 22:53:00 +00006426 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6427 .addMBB(TrapBB)
6428 .addImm(ARMCC::HI)
6429 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006430
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006431 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6432 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006433 .addJumpTableIndex(MJTI)
6434 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006435
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006436 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006437 AddDefaultCC(
6438 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006439 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6440 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006441 .addReg(NewVReg1)
6442 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6443
6444 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006445 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006446 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006447 .addJumpTableIndex(MJTI)
6448 .addImm(UId);
6449 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006450 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6451 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6452 .addFrameIndex(FI)
6453 .addImm(1)
6454 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006455
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006456 if (NumLPads < 256) {
6457 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6458 .addReg(NewVReg1)
6459 .addImm(NumLPads));
6460 } else {
6461 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006462 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6463 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6464
6465 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006466 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006467 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006468 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006469 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006470
6471 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6472 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6473 .addReg(VReg1, RegState::Define)
6474 .addConstantPoolIndex(Idx));
6475 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6476 .addReg(NewVReg1)
6477 .addReg(VReg1));
6478 }
6479
Bill Wendling083a8eb2011-10-06 23:37:36 +00006480 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6481 .addMBB(TrapBB)
6482 .addImm(ARMCC::HI)
6483 .addReg(ARM::CPSR);
6484
6485 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6486 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6487 .addReg(ARM::CPSR, RegState::Define)
6488 .addReg(NewVReg1)
6489 .addImm(2));
6490
6491 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006492 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006493 .addJumpTableIndex(MJTI)
6494 .addImm(UId));
6495
6496 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6497 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6498 .addReg(ARM::CPSR, RegState::Define)
6499 .addReg(NewVReg2, RegState::Kill)
6500 .addReg(NewVReg3));
6501
6502 MachineMemOperand *JTMMOLd =
6503 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6504 MachineMemOperand::MOLoad, 4, 4);
6505
6506 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6507 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6508 .addReg(NewVReg4, RegState::Kill)
6509 .addImm(0)
6510 .addMemOperand(JTMMOLd));
6511
Chad Rosierb8f307b2013-03-01 18:30:38 +00006512 unsigned NewVReg6 = NewVReg5;
6513 if (RelocM == Reloc::PIC_) {
6514 NewVReg6 = MRI->createVirtualRegister(TRC);
6515 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6516 .addReg(ARM::CPSR, RegState::Define)
6517 .addReg(NewVReg5, RegState::Kill)
6518 .addReg(NewVReg3));
6519 }
Bill Wendling083a8eb2011-10-06 23:37:36 +00006520
6521 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6522 .addReg(NewVReg6, RegState::Kill)
6523 .addJumpTableIndex(MJTI)
6524 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006525 } else {
6526 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6527 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6528 .addFrameIndex(FI)
6529 .addImm(4)
6530 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006531
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006532 if (NumLPads < 256) {
6533 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6534 .addReg(NewVReg1)
6535 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006536 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006537 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6538 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006539 .addImm(NumLPads & 0xFFFF));
6540
6541 unsigned VReg2 = VReg1;
6542 if ((NumLPads & 0xFFFF0000) != 0) {
6543 VReg2 = MRI->createVirtualRegister(TRC);
6544 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6545 .addReg(VReg1)
6546 .addImm(NumLPads >> 16));
6547 }
6548
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006549 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6550 .addReg(NewVReg1)
6551 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006552 } else {
6553 MachineConstantPool *ConstantPool = MF->getConstantPool();
6554 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6555 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6556
6557 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006558 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006559 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006560 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006561 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6562
6563 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6564 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6565 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006566 .addConstantPoolIndex(Idx)
6567 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006568 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6569 .addReg(NewVReg1)
6570 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006571 }
6572
Bill Wendling95ce2e92011-10-06 22:53:00 +00006573 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6574 .addMBB(TrapBB)
6575 .addImm(ARMCC::HI)
6576 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006577
Bill Wendling564392b2011-10-18 22:11:18 +00006578 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006579 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006580 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006581 .addReg(NewVReg1)
6582 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006583 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6584 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006585 .addJumpTableIndex(MJTI)
6586 .addImm(UId));
6587
6588 MachineMemOperand *JTMMOLd =
6589 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6590 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006591 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006592 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006593 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6594 .addReg(NewVReg3, RegState::Kill)
6595 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006596 .addImm(0)
6597 .addMemOperand(JTMMOLd));
6598
Chad Rosierb8f307b2013-03-01 18:30:38 +00006599 if (RelocM == Reloc::PIC_) {
6600 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6601 .addReg(NewVReg5, RegState::Kill)
6602 .addReg(NewVReg4)
6603 .addJumpTableIndex(MJTI)
6604 .addImm(UId);
6605 } else {
6606 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6607 .addReg(NewVReg5, RegState::Kill)
6608 .addJumpTableIndex(MJTI)
6609 .addImm(UId);
6610 }
Bill Wendling95ce2e92011-10-06 22:53:00 +00006611 }
Bill Wendling2a850152011-10-05 00:02:33 +00006612
Bill Wendlingbb734682011-10-05 00:39:32 +00006613 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006614 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006615 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006616 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6617 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006618 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006619 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006620 }
6621
Bill Wendling24bb9252011-10-17 05:25:09 +00006622 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006623 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006624 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006625 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6626 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6627 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006628
6629 // Remove the landing pad successor from the invoke block and replace it
6630 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006631 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6632 BB->succ_end());
6633 while (!Successors.empty()) {
6634 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006635 if (SMBB->isLandingPad()) {
6636 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006637 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006638 }
6639 }
6640
6641 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006642
6643 // Find the invoke call and mark all of the callee-saved registers as
6644 // 'implicit defined' so that they're spilled. This prevents code from
6645 // moving instructions to before the EH block, where they will never be
6646 // executed.
6647 for (MachineBasicBlock::reverse_iterator
6648 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006649 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006650
6651 DenseMap<unsigned, bool> DefRegs;
6652 for (MachineInstr::mop_iterator
6653 OI = II->operands_begin(), OE = II->operands_end();
6654 OI != OE; ++OI) {
6655 if (!OI->isReg()) continue;
6656 DefRegs[OI->getReg()] = true;
6657 }
6658
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00006659 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006660
Bill Wendling5d798592011-10-14 23:55:44 +00006661 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006662 unsigned Reg = SavedRegs[i];
6663 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006664 !ARM::tGPRRegClass.contains(Reg) &&
6665 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006666 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006667 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006668 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006669 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006670 continue;
6671 if (!DefRegs[Reg])
6672 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006673 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006674
6675 break;
6676 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006677 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006678
Bill Wendlingf7b02072011-10-18 18:30:49 +00006679 // Mark all former landing pads as non-landing pads. The dispatch is the only
6680 // landing pad now.
6681 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6682 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6683 (*I)->setIsLandingPad(false);
6684
Bill Wendlingbb734682011-10-05 00:39:32 +00006685 // The instruction is gone now.
6686 MI->eraseFromParent();
6687
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006688 return MBB;
6689}
6690
Evan Cheng218977b2010-07-13 19:27:42 +00006691static
6692MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6693 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6694 E = MBB->succ_end(); I != E; ++I)
6695 if (*I != Succ)
6696 return *I;
6697 llvm_unreachable("Expecting a BB with two successors!");
6698}
6699
Manman Ren68f25572012-06-01 19:33:18 +00006700MachineBasicBlock *ARMTargetLowering::
6701EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6702 // This pseudo instruction has 3 operands: dst, src, size
6703 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6704 // Otherwise, we will generate unrolled scalar copies.
6705 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6706 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6707 MachineFunction::iterator It = BB;
6708 ++It;
6709
6710 unsigned dest = MI->getOperand(0).getReg();
6711 unsigned src = MI->getOperand(1).getReg();
6712 unsigned SizeVal = MI->getOperand(2).getImm();
6713 unsigned Align = MI->getOperand(3).getImm();
6714 DebugLoc dl = MI->getDebugLoc();
6715
6716 bool isThumb2 = Subtarget->isThumb2();
6717 MachineFunction *MF = BB->getParent();
6718 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006719 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006720
6721 const TargetRegisterClass *TRC = isThumb2 ?
6722 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6723 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006724 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006725
6726 if (Align & 1) {
6727 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6728 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6729 UnitSize = 1;
6730 } else if (Align & 2) {
6731 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6732 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6733 UnitSize = 2;
6734 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006735 // Check whether we can use NEON instructions.
Bill Wendling831737d2012-12-30 10:32:01 +00006736 if (!MF->getFunction()->getAttributes().
6737 hasAttribute(AttributeSet::FunctionIndex,
6738 Attribute::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006739 Subtarget->hasNEON()) {
6740 if ((Align % 16 == 0) && SizeVal >= 16) {
6741 ldrOpc = ARM::VLD1q32wb_fixed;
6742 strOpc = ARM::VST1q32wb_fixed;
6743 UnitSize = 16;
6744 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6745 }
6746 else if ((Align % 8 == 0) && SizeVal >= 8) {
6747 ldrOpc = ARM::VLD1d32wb_fixed;
6748 strOpc = ARM::VST1d32wb_fixed;
6749 UnitSize = 8;
6750 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6751 }
6752 }
6753 // Can't use NEON instructions.
6754 if (UnitSize == 0) {
6755 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6756 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6757 UnitSize = 4;
6758 }
Manman Ren68f25572012-06-01 19:33:18 +00006759 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006760
Manman Ren68f25572012-06-01 19:33:18 +00006761 unsigned BytesLeft = SizeVal % UnitSize;
6762 unsigned LoopSize = SizeVal - BytesLeft;
6763
6764 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6765 // Use LDR and STR to copy.
6766 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6767 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6768 unsigned srcIn = src;
6769 unsigned destIn = dest;
6770 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006771 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006772 unsigned srcOut = MRI.createVirtualRegister(TRC);
6773 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006774 if (UnitSize >= 8) {
6775 AddDefaultPred(BuildMI(*BB, MI, dl,
6776 TII->get(ldrOpc), scratch)
6777 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6778
6779 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6780 .addReg(destIn).addImm(0).addReg(scratch));
6781 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006782 AddDefaultPred(BuildMI(*BB, MI, dl,
6783 TII->get(ldrOpc), scratch)
6784 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6785
6786 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6787 .addReg(scratch).addReg(destIn)
6788 .addImm(UnitSize));
6789 } else {
6790 AddDefaultPred(BuildMI(*BB, MI, dl,
6791 TII->get(ldrOpc), scratch)
6792 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6793 .addImm(UnitSize));
6794
6795 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6796 .addReg(scratch).addReg(destIn)
6797 .addReg(0).addImm(UnitSize));
6798 }
6799 srcIn = srcOut;
6800 destIn = destOut;
6801 }
6802
6803 // Handle the leftover bytes with LDRB and STRB.
6804 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6805 // [destOut] = STRB_POST(scratch, destIn, 1)
6806 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6807 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6808 for (unsigned i = 0; i < BytesLeft; i++) {
6809 unsigned scratch = MRI.createVirtualRegister(TRC);
6810 unsigned srcOut = MRI.createVirtualRegister(TRC);
6811 unsigned destOut = MRI.createVirtualRegister(TRC);
6812 if (isThumb2) {
6813 AddDefaultPred(BuildMI(*BB, MI, dl,
6814 TII->get(ldrOpc),scratch)
6815 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6816
6817 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6818 .addReg(scratch).addReg(destIn)
6819 .addReg(0).addImm(1));
6820 } else {
6821 AddDefaultPred(BuildMI(*BB, MI, dl,
6822 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006823 .addReg(srcOut, RegState::Define).addReg(srcIn)
6824 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006825
6826 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6827 .addReg(scratch).addReg(destIn)
6828 .addReg(0).addImm(1));
6829 }
6830 srcIn = srcOut;
6831 destIn = destOut;
6832 }
6833 MI->eraseFromParent(); // The instruction is gone now.
6834 return BB;
6835 }
6836
6837 // Expand the pseudo op to a loop.
6838 // thisMBB:
6839 // ...
6840 // movw varEnd, # --> with thumb2
6841 // movt varEnd, #
6842 // ldrcp varEnd, idx --> without thumb2
6843 // fallthrough --> loopMBB
6844 // loopMBB:
6845 // PHI varPhi, varEnd, varLoop
6846 // PHI srcPhi, src, srcLoop
6847 // PHI destPhi, dst, destLoop
6848 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6849 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6850 // subs varLoop, varPhi, #UnitSize
6851 // bne loopMBB
6852 // fallthrough --> exitMBB
6853 // exitMBB:
6854 // epilogue to handle left-over bytes
6855 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6856 // [destOut] = STRB_POST(scratch, destLoop, 1)
6857 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6858 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6859 MF->insert(It, loopMBB);
6860 MF->insert(It, exitMBB);
6861
6862 // Transfer the remainder of BB and its successor edges to exitMBB.
6863 exitMBB->splice(exitMBB->begin(), BB,
6864 llvm::next(MachineBasicBlock::iterator(MI)),
6865 BB->end());
6866 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6867
6868 // Load an immediate to varEnd.
6869 unsigned varEnd = MRI.createVirtualRegister(TRC);
6870 if (isThumb2) {
6871 unsigned VReg1 = varEnd;
6872 if ((LoopSize & 0xFFFF0000) != 0)
6873 VReg1 = MRI.createVirtualRegister(TRC);
6874 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6875 .addImm(LoopSize & 0xFFFF));
6876
6877 if ((LoopSize & 0xFFFF0000) != 0)
6878 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6879 .addReg(VReg1)
6880 .addImm(LoopSize >> 16));
6881 } else {
6882 MachineConstantPool *ConstantPool = MF->getConstantPool();
6883 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6884 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6885
6886 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006887 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006888 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006889 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006890 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6891
6892 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6893 .addReg(varEnd, RegState::Define)
6894 .addConstantPoolIndex(Idx)
6895 .addImm(0));
6896 }
6897 BB->addSuccessor(loopMBB);
6898
6899 // Generate the loop body:
6900 // varPhi = PHI(varLoop, varEnd)
6901 // srcPhi = PHI(srcLoop, src)
6902 // destPhi = PHI(destLoop, dst)
6903 MachineBasicBlock *entryBB = BB;
6904 BB = loopMBB;
6905 unsigned varLoop = MRI.createVirtualRegister(TRC);
6906 unsigned varPhi = MRI.createVirtualRegister(TRC);
6907 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6908 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6909 unsigned destLoop = MRI.createVirtualRegister(TRC);
6910 unsigned destPhi = MRI.createVirtualRegister(TRC);
6911
6912 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6913 .addReg(varLoop).addMBB(loopMBB)
6914 .addReg(varEnd).addMBB(entryBB);
6915 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6916 .addReg(srcLoop).addMBB(loopMBB)
6917 .addReg(src).addMBB(entryBB);
6918 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6919 .addReg(destLoop).addMBB(loopMBB)
6920 .addReg(dest).addMBB(entryBB);
6921
6922 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6923 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006924 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6925 if (UnitSize >= 8) {
6926 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6927 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6928
6929 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6930 .addReg(destPhi).addImm(0).addReg(scratch));
6931 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006932 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6933 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6934
6935 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6936 .addReg(scratch).addReg(destPhi)
6937 .addImm(UnitSize));
6938 } else {
6939 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6940 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6941 .addImm(UnitSize));
6942
6943 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6944 .addReg(scratch).addReg(destPhi)
6945 .addReg(0).addImm(UnitSize));
6946 }
6947
6948 // Decrement loop variable by UnitSize.
6949 MachineInstrBuilder MIB = BuildMI(BB, dl,
6950 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6951 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6952 MIB->getOperand(5).setReg(ARM::CPSR);
6953 MIB->getOperand(5).setIsDef(true);
6954
6955 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6956 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6957
6958 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6959 BB->addSuccessor(loopMBB);
6960 BB->addSuccessor(exitMBB);
6961
6962 // Add epilogue to handle BytesLeft.
6963 BB = exitMBB;
6964 MachineInstr *StartOfExit = exitMBB->begin();
6965 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6966 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6967
6968 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6969 // [destOut] = STRB_POST(scratch, destLoop, 1)
6970 unsigned srcIn = srcLoop;
6971 unsigned destIn = destLoop;
6972 for (unsigned i = 0; i < BytesLeft; i++) {
6973 unsigned scratch = MRI.createVirtualRegister(TRC);
6974 unsigned srcOut = MRI.createVirtualRegister(TRC);
6975 unsigned destOut = MRI.createVirtualRegister(TRC);
6976 if (isThumb2) {
6977 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6978 TII->get(ldrOpc),scratch)
6979 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6980
6981 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6982 .addReg(scratch).addReg(destIn)
6983 .addImm(1));
6984 } else {
6985 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6986 TII->get(ldrOpc),scratch)
6987 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6988
6989 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6990 .addReg(scratch).addReg(destIn)
6991 .addReg(0).addImm(1));
6992 }
6993 srcIn = srcOut;
6994 destIn = destOut;
6995 }
6996
6997 MI->eraseFromParent(); // The instruction is gone now.
6998 return BB;
6999}
7000
Jim Grosbache801dc42009-12-12 01:40:06 +00007001MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007002ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00007003 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00007004 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00007005 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007006 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00007007 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00007008 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00007009 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00007010 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00007011 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00007012 // The Thumb2 pre-indexed stores have the same MI operands, they just
7013 // define them differently in the .td files from the isel patterns, so
7014 // they need pseudos.
7015 case ARM::t2STR_preidx:
7016 MI->setDesc(TII->get(ARM::t2STR_PRE));
7017 return BB;
7018 case ARM::t2STRB_preidx:
7019 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7020 return BB;
7021 case ARM::t2STRH_preidx:
7022 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7023 return BB;
7024
Jim Grosbach19dec202011-08-05 20:35:44 +00007025 case ARM::STRi_preidx:
7026 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00007027 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00007028 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7029 // Decode the offset.
7030 unsigned Offset = MI->getOperand(4).getImm();
7031 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7032 Offset = ARM_AM::getAM2Offset(Offset);
7033 if (isSub)
7034 Offset = -Offset;
7035
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007036 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00007037 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00007038 .addOperand(MI->getOperand(0)) // Rn_wb
7039 .addOperand(MI->getOperand(1)) // Rt
7040 .addOperand(MI->getOperand(2)) // Rn
7041 .addImm(Offset) // offset (skip GPR==zero_reg)
7042 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007043 .addOperand(MI->getOperand(6))
7044 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00007045 MI->eraseFromParent();
7046 return BB;
7047 }
7048 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00007049 case ARM::STRBr_preidx:
7050 case ARM::STRH_preidx: {
7051 unsigned NewOpc;
7052 switch (MI->getOpcode()) {
7053 default: llvm_unreachable("unexpected opcode!");
7054 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7055 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7056 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7057 }
Jim Grosbach19dec202011-08-05 20:35:44 +00007058 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7059 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7060 MIB.addOperand(MI->getOperand(i));
7061 MI->eraseFromParent();
7062 return BB;
7063 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007064 case ARM::ATOMIC_LOAD_ADD_I8:
7065 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7066 case ARM::ATOMIC_LOAD_ADD_I16:
7067 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7068 case ARM::ATOMIC_LOAD_ADD_I32:
7069 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007070
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007071 case ARM::ATOMIC_LOAD_AND_I8:
7072 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7073 case ARM::ATOMIC_LOAD_AND_I16:
7074 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7075 case ARM::ATOMIC_LOAD_AND_I32:
7076 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007077
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007078 case ARM::ATOMIC_LOAD_OR_I8:
7079 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7080 case ARM::ATOMIC_LOAD_OR_I16:
7081 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7082 case ARM::ATOMIC_LOAD_OR_I32:
7083 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007084
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007085 case ARM::ATOMIC_LOAD_XOR_I8:
7086 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7087 case ARM::ATOMIC_LOAD_XOR_I16:
7088 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7089 case ARM::ATOMIC_LOAD_XOR_I32:
7090 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007091
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007092 case ARM::ATOMIC_LOAD_NAND_I8:
7093 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7094 case ARM::ATOMIC_LOAD_NAND_I16:
7095 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7096 case ARM::ATOMIC_LOAD_NAND_I32:
7097 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007098
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007099 case ARM::ATOMIC_LOAD_SUB_I8:
7100 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7101 case ARM::ATOMIC_LOAD_SUB_I16:
7102 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7103 case ARM::ATOMIC_LOAD_SUB_I32:
7104 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007105
Jim Grosbachf7da8822011-04-26 19:44:18 +00007106 case ARM::ATOMIC_LOAD_MIN_I8:
7107 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7108 case ARM::ATOMIC_LOAD_MIN_I16:
7109 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7110 case ARM::ATOMIC_LOAD_MIN_I32:
7111 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7112
7113 case ARM::ATOMIC_LOAD_MAX_I8:
7114 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7115 case ARM::ATOMIC_LOAD_MAX_I16:
7116 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7117 case ARM::ATOMIC_LOAD_MAX_I32:
7118 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7119
7120 case ARM::ATOMIC_LOAD_UMIN_I8:
7121 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7122 case ARM::ATOMIC_LOAD_UMIN_I16:
7123 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7124 case ARM::ATOMIC_LOAD_UMIN_I32:
7125 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7126
7127 case ARM::ATOMIC_LOAD_UMAX_I8:
7128 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7129 case ARM::ATOMIC_LOAD_UMAX_I16:
7130 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7131 case ARM::ATOMIC_LOAD_UMAX_I32:
7132 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7133
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007134 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7135 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7136 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00007137
7138 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7139 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7140 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007141
Eli Friedman2bdffe42011-08-31 00:31:29 +00007142
7143 case ARM::ATOMADD6432:
7144 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007145 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7146 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007147 case ARM::ATOMSUB6432:
7148 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007149 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7150 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007151 case ARM::ATOMOR6432:
7152 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007153 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007154 case ARM::ATOMXOR6432:
7155 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007156 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007157 case ARM::ATOMAND6432:
7158 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007159 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007160 case ARM::ATOMSWAP6432:
7161 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00007162 case ARM::ATOMCMPXCHG6432:
7163 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7164 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7165 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007166 case ARM::ATOMMIN6432:
7167 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7168 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7169 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007170 /*IsMinMax*/ true, ARMCC::LT);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007171 case ARM::ATOMMAX6432:
7172 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7173 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7174 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7175 /*IsMinMax*/ true, ARMCC::GE);
7176 case ARM::ATOMUMIN6432:
7177 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7178 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7179 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007180 /*IsMinMax*/ true, ARMCC::LO);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007181 case ARM::ATOMUMAX6432:
7182 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7183 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7184 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7185 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007186
Evan Cheng007ea272009-08-12 05:17:19 +00007187 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00007188 // To "insert" a SELECT_CC instruction, we actually have to insert the
7189 // diamond control-flow pattern. The incoming instruction knows the
7190 // destination vreg to set, the condition code register to branch on, the
7191 // true/false values to select between, and a branch opcode to use.
7192 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007193 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00007194 ++It;
7195
7196 // thisMBB:
7197 // ...
7198 // TrueVal = ...
7199 // cmpTY ccX, r1, r2
7200 // bCC copy1MBB
7201 // fallthrough --> copy0MBB
7202 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007203 MachineFunction *F = BB->getParent();
7204 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7205 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00007206 F->insert(It, copy0MBB);
7207 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00007208
7209 // Transfer the remainder of BB and its successor edges to sinkMBB.
7210 sinkMBB->splice(sinkMBB->begin(), BB,
7211 llvm::next(MachineBasicBlock::iterator(MI)),
7212 BB->end());
7213 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7214
Dan Gohman258c58c2010-07-06 15:49:48 +00007215 BB->addSuccessor(copy0MBB);
7216 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00007217
Dan Gohman14152b42010-07-06 20:24:04 +00007218 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7219 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7220
Evan Chenga8e29892007-01-19 07:51:42 +00007221 // copy0MBB:
7222 // %FalseValue = ...
7223 // # fallthrough to sinkMBB
7224 BB = copy0MBB;
7225
7226 // Update machine-CFG edges
7227 BB->addSuccessor(sinkMBB);
7228
7229 // sinkMBB:
7230 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7231 // ...
7232 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007233 BuildMI(*BB, BB->begin(), dl,
7234 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007235 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7236 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7237
Dan Gohman14152b42010-07-06 20:24:04 +00007238 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007239 return BB;
7240 }
Evan Cheng86198642009-08-07 00:34:42 +00007241
Evan Cheng218977b2010-07-13 19:27:42 +00007242 case ARM::BCCi64:
7243 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007244 // If there is an unconditional branch to the other successor, remove it.
7245 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007246
Evan Cheng218977b2010-07-13 19:27:42 +00007247 // Compare both parts that make up the double comparison separately for
7248 // equality.
7249 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7250
7251 unsigned LHS1 = MI->getOperand(1).getReg();
7252 unsigned LHS2 = MI->getOperand(2).getReg();
7253 if (RHSisZero) {
7254 AddDefaultPred(BuildMI(BB, dl,
7255 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7256 .addReg(LHS1).addImm(0));
7257 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7258 .addReg(LHS2).addImm(0)
7259 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7260 } else {
7261 unsigned RHS1 = MI->getOperand(3).getReg();
7262 unsigned RHS2 = MI->getOperand(4).getReg();
7263 AddDefaultPred(BuildMI(BB, dl,
7264 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7265 .addReg(LHS1).addReg(RHS1));
7266 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7267 .addReg(LHS2).addReg(RHS2)
7268 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7269 }
7270
7271 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7272 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7273 if (MI->getOperand(0).getImm() == ARMCC::NE)
7274 std::swap(destMBB, exitMBB);
7275
7276 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7277 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007278 if (isThumb2)
7279 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7280 else
7281 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007282
7283 MI->eraseFromParent(); // The pseudo instruction is gone now.
7284 return BB;
7285 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007286
Bill Wendling5bc85282011-10-17 20:37:20 +00007287 case ARM::Int_eh_sjlj_setjmp:
7288 case ARM::Int_eh_sjlj_setjmp_nofp:
7289 case ARM::tInt_eh_sjlj_setjmp:
7290 case ARM::t2Int_eh_sjlj_setjmp:
7291 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7292 EmitSjLjDispatchBlock(MI, BB);
7293 return BB;
7294
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007295 case ARM::ABS:
7296 case ARM::t2ABS: {
7297 // To insert an ABS instruction, we have to insert the
7298 // diamond control-flow pattern. The incoming instruction knows the
7299 // source vreg to test against 0, the destination vreg to set,
7300 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007301 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007302 // It transforms
7303 // V1 = ABS V0
7304 // into
7305 // V2 = MOVS V0
7306 // BCC (branch to SinkBB if V0 >= 0)
7307 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007308 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007309 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7310 MachineFunction::iterator BBI = BB;
7311 ++BBI;
7312 MachineFunction *Fn = BB->getParent();
7313 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7314 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7315 Fn->insert(BBI, RSBBB);
7316 Fn->insert(BBI, SinkBB);
7317
7318 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7319 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7320 bool isThumb2 = Subtarget->isThumb2();
7321 MachineRegisterInfo &MRI = Fn->getRegInfo();
7322 // In Thumb mode S must not be specified if source register is the SP or
7323 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007324 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7325 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7326 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007327
7328 // Transfer the remainder of BB and its successor edges to sinkMBB.
7329 SinkBB->splice(SinkBB->begin(), BB,
7330 llvm::next(MachineBasicBlock::iterator(MI)),
7331 BB->end());
7332 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7333
7334 BB->addSuccessor(RSBBB);
7335 BB->addSuccessor(SinkBB);
7336
7337 // fall through to SinkMBB
7338 RSBBB->addSuccessor(SinkBB);
7339
Manman Ren307473d2012-06-15 21:32:12 +00007340 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007341 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007342 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7343 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007344
7345 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007346 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007347 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7348 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7349
7350 // insert rsbri in RSBBB
7351 // Note: BCC and rsbri will be converted into predicated rsbmi
7352 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007353 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007354 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007355 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007356 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7357
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007358 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007359 // reuse ABSDstReg to not change uses of ABS instruction
7360 BuildMI(*SinkBB, SinkBB->begin(), dl,
7361 TII->get(ARM::PHI), ABSDstReg)
7362 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007363 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007364
7365 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007366 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007367
7368 // return last added BB
7369 return SinkBB;
7370 }
Manman Ren68f25572012-06-01 19:33:18 +00007371 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007372 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007373 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007374 }
7375}
7376
Evan Cheng37fefc22011-08-30 19:09:48 +00007377void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7378 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007379 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007380 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7381 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7382 return;
7383 }
7384
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007385 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007386 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7387 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7388 // operand is still set to noreg. If needed, set the optional operand's
7389 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007390 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007391 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007392
Andrew Trick3be654f2011-09-21 02:20:46 +00007393 // Rename pseudo opcodes.
7394 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7395 if (NewOpc) {
7396 const ARMBaseInstrInfo *TII =
7397 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007398 MCID = &TII->get(NewOpc);
7399
7400 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7401 "converted opcode should be the same except for cc_out");
7402
7403 MI->setDesc(*MCID);
7404
7405 // Add the optional cc_out operand
7406 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007407 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007408 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007409
7410 // Any ARM instruction that sets the 's' bit should specify an optional
7411 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007412 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007413 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007414 return;
7415 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007416 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7417 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007418 bool definesCPSR = false;
7419 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007420 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007421 i != e; ++i) {
7422 const MachineOperand &MO = MI->getOperand(i);
7423 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7424 definesCPSR = true;
7425 if (MO.isDead())
7426 deadCPSR = true;
7427 MI->RemoveOperand(i);
7428 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007429 }
7430 }
Andrew Trick4815d562011-09-20 03:17:40 +00007431 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007432 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007433 return;
7434 }
7435 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007436 if (deadCPSR) {
7437 assert(!MI->getOperand(ccOutIdx).getReg() &&
7438 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007439 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007440 }
Andrew Trick4815d562011-09-20 03:17:40 +00007441
Andrew Trick3be654f2011-09-21 02:20:46 +00007442 // If this instruction was defined with an optional CPSR def and its dag node
7443 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007444 MachineOperand &MO = MI->getOperand(ccOutIdx);
7445 MO.setReg(ARM::CPSR);
7446 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007447}
7448
Evan Chenga8e29892007-01-19 07:51:42 +00007449//===----------------------------------------------------------------------===//
7450// ARM Optimization Hooks
7451//===----------------------------------------------------------------------===//
7452
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007453// Helper function that checks if N is a null or all ones constant.
7454static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7455 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7456 if (!C)
7457 return false;
7458 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7459}
7460
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007461// Return true if N is conditionally 0 or all ones.
7462// Detects these expressions where cc is an i1 value:
7463//
7464// (select cc 0, y) [AllOnes=0]
7465// (select cc y, 0) [AllOnes=0]
7466// (zext cc) [AllOnes=0]
7467// (sext cc) [AllOnes=0/1]
7468// (select cc -1, y) [AllOnes=1]
7469// (select cc y, -1) [AllOnes=1]
7470//
7471// Invert is set when N is the null/all ones constant when CC is false.
7472// OtherOp is set to the alternative value of N.
7473static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7474 SDValue &CC, bool &Invert,
7475 SDValue &OtherOp,
7476 SelectionDAG &DAG) {
7477 switch (N->getOpcode()) {
7478 default: return false;
7479 case ISD::SELECT: {
7480 CC = N->getOperand(0);
7481 SDValue N1 = N->getOperand(1);
7482 SDValue N2 = N->getOperand(2);
7483 if (isZeroOrAllOnes(N1, AllOnes)) {
7484 Invert = false;
7485 OtherOp = N2;
7486 return true;
7487 }
7488 if (isZeroOrAllOnes(N2, AllOnes)) {
7489 Invert = true;
7490 OtherOp = N1;
7491 return true;
7492 }
7493 return false;
7494 }
7495 case ISD::ZERO_EXTEND:
7496 // (zext cc) can never be the all ones value.
7497 if (AllOnes)
7498 return false;
7499 // Fall through.
7500 case ISD::SIGN_EXTEND: {
7501 EVT VT = N->getValueType(0);
7502 CC = N->getOperand(0);
7503 if (CC.getValueType() != MVT::i1)
7504 return false;
7505 Invert = !AllOnes;
7506 if (AllOnes)
7507 // When looking for an AllOnes constant, N is an sext, and the 'other'
7508 // value is 0.
7509 OtherOp = DAG.getConstant(0, VT);
7510 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7511 // When looking for a 0 constant, N can be zext or sext.
7512 OtherOp = DAG.getConstant(1, VT);
7513 else
7514 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7515 return true;
7516 }
7517 }
7518}
7519
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007520// Combine a constant select operand into its use:
7521//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007522// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7523// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7524// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7525// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7526// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007527//
7528// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007529// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007530//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007531// Also recognize sext/zext from i1:
7532//
7533// (add (zext cc), x) -> (select cc (add x, 1), x)
7534// (add (sext cc), x) -> (select cc (add x, -1), x)
7535//
7536// These transformations eventually create predicated instructions.
7537//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007538// @param N The node to transform.
7539// @param Slct The N operand that is a select.
7540// @param OtherOp The other N operand (x above).
7541// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007542// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007543// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007544static
7545SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007546 TargetLowering::DAGCombinerInfo &DCI,
7547 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007548 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007549 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007550 SDValue NonConstantVal;
7551 SDValue CCOp;
7552 bool SwapSelectOps;
7553 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7554 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007555 return SDValue();
7556
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007557 // Slct is now know to be the desired identity constant when CC is true.
7558 SDValue TrueVal = OtherOp;
7559 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7560 OtherOp, NonConstantVal);
7561 // Unless SwapSelectOps says CC should be false.
7562 if (SwapSelectOps)
7563 std::swap(TrueVal, FalseVal);
7564
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007565 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007566 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007567}
7568
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007569// Attempt combineSelectAndUse on each operand of a commutative operator N.
7570static
7571SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7572 TargetLowering::DAGCombinerInfo &DCI) {
7573 SDValue N0 = N->getOperand(0);
7574 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007575 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007576 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7577 if (Result.getNode())
7578 return Result;
7579 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007580 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007581 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7582 if (Result.getNode())
7583 return Result;
7584 }
7585 return SDValue();
7586}
7587
Eric Christopherfa6f5912011-06-29 21:10:36 +00007588// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007589// (only after legalization).
7590static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7591 TargetLowering::DAGCombinerInfo &DCI,
7592 const ARMSubtarget *Subtarget) {
7593
7594 // Only perform optimization if after legalize, and if NEON is available. We
7595 // also expected both operands to be BUILD_VECTORs.
7596 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7597 || N0.getOpcode() != ISD::BUILD_VECTOR
7598 || N1.getOpcode() != ISD::BUILD_VECTOR)
7599 return SDValue();
7600
7601 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7602 EVT VT = N->getValueType(0);
7603 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7604 return SDValue();
7605
7606 // Check that the vector operands are of the right form.
7607 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7608 // operands, where N is the size of the formed vector.
7609 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7610 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007611
7612 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007613 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007614 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007615 SDValue Vec = N0->getOperand(0)->getOperand(0);
7616 SDNode *V = Vec.getNode();
7617 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007618
Eric Christopherfa6f5912011-06-29 21:10:36 +00007619 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007620 // check to see if each of their operands are an EXTRACT_VECTOR with
7621 // the same vector and appropriate index.
7622 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7623 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7624 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007625
Tanya Lattner189531f2011-06-14 23:48:48 +00007626 SDValue ExtVec0 = N0->getOperand(i);
7627 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007628
Tanya Lattner189531f2011-06-14 23:48:48 +00007629 // First operand is the vector, verify its the same.
7630 if (V != ExtVec0->getOperand(0).getNode() ||
7631 V != ExtVec1->getOperand(0).getNode())
7632 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007633
Tanya Lattner189531f2011-06-14 23:48:48 +00007634 // Second is the constant, verify its correct.
7635 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7636 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007637
Tanya Lattner189531f2011-06-14 23:48:48 +00007638 // For the constant, we want to see all the even or all the odd.
7639 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7640 || C1->getZExtValue() != nextIndex+1)
7641 return SDValue();
7642
7643 // Increment index.
7644 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007645 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007646 return SDValue();
7647 }
7648
7649 // Create VPADDL node.
7650 SelectionDAG &DAG = DCI.DAG;
7651 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007652
7653 // Build operand list.
7654 SmallVector<SDValue, 8> Ops;
7655 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7656 TLI.getPointerTy()));
7657
7658 // Input is the vector.
7659 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007660
Tanya Lattner189531f2011-06-14 23:48:48 +00007661 // Get widened type and narrowed type.
7662 MVT widenType;
7663 unsigned numElem = VT.getVectorNumElements();
7664 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7665 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7666 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7667 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7668 default:
Craig Topperbc219812012-02-07 02:50:20 +00007669 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007670 }
7671
7672 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7673 widenType, &Ops[0], Ops.size());
7674 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7675}
7676
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007677static SDValue findMUL_LOHI(SDValue V) {
7678 if (V->getOpcode() == ISD::UMUL_LOHI ||
7679 V->getOpcode() == ISD::SMUL_LOHI)
7680 return V;
7681 return SDValue();
7682}
7683
7684static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7685 TargetLowering::DAGCombinerInfo &DCI,
7686 const ARMSubtarget *Subtarget) {
7687
7688 if (Subtarget->isThumb1Only()) return SDValue();
7689
7690 // Only perform the checks after legalize when the pattern is available.
7691 if (DCI.isBeforeLegalize()) return SDValue();
7692
7693 // Look for multiply add opportunities.
7694 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7695 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7696 // a glue link from the first add to the second add.
7697 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7698 // a S/UMLAL instruction.
7699 // loAdd UMUL_LOHI
7700 // \ / :lo \ :hi
7701 // \ / \ [no multiline comment]
7702 // ADDC | hiAdd
7703 // \ :glue / /
7704 // \ / /
7705 // ADDE
7706 //
7707 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7708 SDValue AddcOp0 = AddcNode->getOperand(0);
7709 SDValue AddcOp1 = AddcNode->getOperand(1);
7710
7711 // Check if the two operands are from the same mul_lohi node.
7712 if (AddcOp0.getNode() == AddcOp1.getNode())
7713 return SDValue();
7714
7715 assert(AddcNode->getNumValues() == 2 &&
7716 AddcNode->getValueType(0) == MVT::i32 &&
7717 AddcNode->getValueType(1) == MVT::Glue &&
7718 "Expect ADDC with two result values: i32, glue");
7719
7720 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7721 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7722 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7723 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7724 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7725 return SDValue();
7726
7727 // Look for the glued ADDE.
7728 SDNode* AddeNode = AddcNode->getGluedUser();
7729 if (AddeNode == NULL)
7730 return SDValue();
7731
7732 // Make sure it is really an ADDE.
7733 if (AddeNode->getOpcode() != ISD::ADDE)
7734 return SDValue();
7735
7736 assert(AddeNode->getNumOperands() == 3 &&
7737 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7738 "ADDE node has the wrong inputs");
7739
7740 // Check for the triangle shape.
7741 SDValue AddeOp0 = AddeNode->getOperand(0);
7742 SDValue AddeOp1 = AddeNode->getOperand(1);
7743
7744 // Make sure that the ADDE operands are not coming from the same node.
7745 if (AddeOp0.getNode() == AddeOp1.getNode())
7746 return SDValue();
7747
7748 // Find the MUL_LOHI node walking up ADDE's operands.
7749 bool IsLeftOperandMUL = false;
7750 SDValue MULOp = findMUL_LOHI(AddeOp0);
7751 if (MULOp == SDValue())
7752 MULOp = findMUL_LOHI(AddeOp1);
7753 else
7754 IsLeftOperandMUL = true;
7755 if (MULOp == SDValue())
7756 return SDValue();
7757
7758 // Figure out the right opcode.
7759 unsigned Opc = MULOp->getOpcode();
7760 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7761
7762 // Figure out the high and low input values to the MLAL node.
7763 SDValue* HiMul = &MULOp;
7764 SDValue* HiAdd = NULL;
7765 SDValue* LoMul = NULL;
7766 SDValue* LowAdd = NULL;
7767
7768 if (IsLeftOperandMUL)
7769 HiAdd = &AddeOp1;
7770 else
7771 HiAdd = &AddeOp0;
7772
7773
7774 if (AddcOp0->getOpcode() == Opc) {
7775 LoMul = &AddcOp0;
7776 LowAdd = &AddcOp1;
7777 }
7778 if (AddcOp1->getOpcode() == Opc) {
7779 LoMul = &AddcOp1;
7780 LowAdd = &AddcOp0;
7781 }
7782
7783 if (LoMul == NULL)
7784 return SDValue();
7785
7786 if (LoMul->getNode() != HiMul->getNode())
7787 return SDValue();
7788
7789 // Create the merged node.
7790 SelectionDAG &DAG = DCI.DAG;
7791
7792 // Build operand list.
7793 SmallVector<SDValue, 8> Ops;
7794 Ops.push_back(LoMul->getOperand(0));
7795 Ops.push_back(LoMul->getOperand(1));
7796 Ops.push_back(*LowAdd);
7797 Ops.push_back(*HiAdd);
7798
7799 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7800 DAG.getVTList(MVT::i32, MVT::i32),
7801 &Ops[0], Ops.size());
7802
7803 // Replace the ADDs' nodes uses by the MLA node's values.
7804 SDValue HiMLALResult(MLALNode.getNode(), 1);
7805 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7806
7807 SDValue LoMLALResult(MLALNode.getNode(), 0);
7808 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7809
7810 // Return original node to notify the driver to stop replacing.
7811 SDValue resNode(AddcNode, 0);
7812 return resNode;
7813}
7814
7815/// PerformADDCCombine - Target-specific dag combine transform from
7816/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7817static SDValue PerformADDCCombine(SDNode *N,
7818 TargetLowering::DAGCombinerInfo &DCI,
7819 const ARMSubtarget *Subtarget) {
7820
7821 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7822
7823}
7824
Bob Wilson3d5792a2010-07-29 20:34:14 +00007825/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7826/// operands N0 and N1. This is a helper for PerformADDCombine that is
7827/// called with the default operands, and if that fails, with commuted
7828/// operands.
7829static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007830 TargetLowering::DAGCombinerInfo &DCI,
7831 const ARMSubtarget *Subtarget){
7832
7833 // Attempt to create vpaddl for this add.
7834 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7835 if (Result.getNode())
7836 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007837
Chris Lattnerd1980a52009-03-12 06:52:53 +00007838 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007839 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007840 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7841 if (Result.getNode()) return Result;
7842 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007843 return SDValue();
7844}
7845
Bob Wilson3d5792a2010-07-29 20:34:14 +00007846/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7847///
7848static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007849 TargetLowering::DAGCombinerInfo &DCI,
7850 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007851 SDValue N0 = N->getOperand(0);
7852 SDValue N1 = N->getOperand(1);
7853
7854 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007855 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007856 if (Result.getNode())
7857 return Result;
7858
7859 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007860 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007861}
7862
Chris Lattnerd1980a52009-03-12 06:52:53 +00007863/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007864///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007865static SDValue PerformSUBCombine(SDNode *N,
7866 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007867 SDValue N0 = N->getOperand(0);
7868 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007869
Chris Lattnerd1980a52009-03-12 06:52:53 +00007870 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007871 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007872 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7873 if (Result.getNode()) return Result;
7874 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007875
Chris Lattnerd1980a52009-03-12 06:52:53 +00007876 return SDValue();
7877}
7878
Evan Cheng463d3582011-03-31 19:38:48 +00007879/// PerformVMULCombine
7880/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7881/// special multiplier accumulator forwarding.
7882/// vmul d3, d0, d2
7883/// vmla d3, d1, d2
7884/// is faster than
7885/// vadd d3, d0, d1
7886/// vmul d3, d3, d2
7887static SDValue PerformVMULCombine(SDNode *N,
7888 TargetLowering::DAGCombinerInfo &DCI,
7889 const ARMSubtarget *Subtarget) {
7890 if (!Subtarget->hasVMLxForwarding())
7891 return SDValue();
7892
7893 SelectionDAG &DAG = DCI.DAG;
7894 SDValue N0 = N->getOperand(0);
7895 SDValue N1 = N->getOperand(1);
7896 unsigned Opcode = N0.getOpcode();
7897 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7898 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007899 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007900 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7901 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7902 return SDValue();
7903 std::swap(N0, N1);
7904 }
7905
7906 EVT VT = N->getValueType(0);
7907 DebugLoc DL = N->getDebugLoc();
7908 SDValue N00 = N0->getOperand(0);
7909 SDValue N01 = N0->getOperand(1);
7910 return DAG.getNode(Opcode, DL, VT,
7911 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7912 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7913}
7914
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007915static SDValue PerformMULCombine(SDNode *N,
7916 TargetLowering::DAGCombinerInfo &DCI,
7917 const ARMSubtarget *Subtarget) {
7918 SelectionDAG &DAG = DCI.DAG;
7919
7920 if (Subtarget->isThumb1Only())
7921 return SDValue();
7922
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007923 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7924 return SDValue();
7925
7926 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007927 if (VT.is64BitVector() || VT.is128BitVector())
7928 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007929 if (VT != MVT::i32)
7930 return SDValue();
7931
7932 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7933 if (!C)
7934 return SDValue();
7935
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007936 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007937 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007938
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007939 ShiftAmt = ShiftAmt & (32 - 1);
7940 SDValue V = N->getOperand(0);
7941 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007942
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007943 SDValue Res;
7944 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007945
7946 if (MulAmt >= 0) {
7947 if (isPowerOf2_32(MulAmt - 1)) {
7948 // (mul x, 2^N + 1) => (add (shl x, N), x)
7949 Res = DAG.getNode(ISD::ADD, DL, VT,
7950 V,
7951 DAG.getNode(ISD::SHL, DL, VT,
7952 V,
7953 DAG.getConstant(Log2_32(MulAmt - 1),
7954 MVT::i32)));
7955 } else if (isPowerOf2_32(MulAmt + 1)) {
7956 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7957 Res = DAG.getNode(ISD::SUB, DL, VT,
7958 DAG.getNode(ISD::SHL, DL, VT,
7959 V,
7960 DAG.getConstant(Log2_32(MulAmt + 1),
7961 MVT::i32)),
7962 V);
7963 } else
7964 return SDValue();
7965 } else {
7966 uint64_t MulAmtAbs = -MulAmt;
7967 if (isPowerOf2_32(MulAmtAbs + 1)) {
7968 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7969 Res = DAG.getNode(ISD::SUB, DL, VT,
7970 V,
7971 DAG.getNode(ISD::SHL, DL, VT,
7972 V,
7973 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7974 MVT::i32)));
7975 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7976 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7977 Res = DAG.getNode(ISD::ADD, DL, VT,
7978 V,
7979 DAG.getNode(ISD::SHL, DL, VT,
7980 V,
7981 DAG.getConstant(Log2_32(MulAmtAbs-1),
7982 MVT::i32)));
7983 Res = DAG.getNode(ISD::SUB, DL, VT,
7984 DAG.getConstant(0, MVT::i32),Res);
7985
7986 } else
7987 return SDValue();
7988 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007989
7990 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007991 Res = DAG.getNode(ISD::SHL, DL, VT,
7992 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007993
7994 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007995 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007996 return SDValue();
7997}
7998
Owen Anderson080c0922010-11-05 19:27:46 +00007999static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00008000 TargetLowering::DAGCombinerInfo &DCI,
8001 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00008002
Owen Anderson080c0922010-11-05 19:27:46 +00008003 // Attempt to use immediate-form VBIC
8004 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8005 DebugLoc dl = N->getDebugLoc();
8006 EVT VT = N->getValueType(0);
8007 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008008
Tanya Lattner0433b212011-04-07 15:24:20 +00008009 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8010 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008011
Owen Anderson080c0922010-11-05 19:27:46 +00008012 APInt SplatBits, SplatUndef;
8013 unsigned SplatBitSize;
8014 bool HasAnyUndefs;
8015 if (BVN &&
8016 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8017 if (SplatBitSize <= 64) {
8018 EVT VbicVT;
8019 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8020 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008021 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008022 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00008023 if (Val.getNode()) {
8024 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008025 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00008026 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008027 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00008028 }
8029 }
8030 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008031
Evan Chengc892aeb2012-02-23 01:19:06 +00008032 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008033 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8034 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8035 if (Result.getNode())
8036 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008037 }
8038
Owen Anderson080c0922010-11-05 19:27:46 +00008039 return SDValue();
8040}
8041
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008042/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8043static SDValue PerformORCombine(SDNode *N,
8044 TargetLowering::DAGCombinerInfo &DCI,
8045 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00008046 // Attempt to use immediate-form VORR
8047 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8048 DebugLoc dl = N->getDebugLoc();
8049 EVT VT = N->getValueType(0);
8050 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008051
Tanya Lattner0433b212011-04-07 15:24:20 +00008052 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8053 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008054
Owen Anderson60f48702010-11-03 23:15:26 +00008055 APInt SplatBits, SplatUndef;
8056 unsigned SplatBitSize;
8057 bool HasAnyUndefs;
8058 if (BVN && Subtarget->hasNEON() &&
8059 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8060 if (SplatBitSize <= 64) {
8061 EVT VorrVT;
8062 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8063 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008064 DAG, VorrVT, VT.is128BitVector(),
8065 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00008066 if (Val.getNode()) {
8067 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008068 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00008069 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008070 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00008071 }
8072 }
8073 }
8074
Evan Chengc892aeb2012-02-23 01:19:06 +00008075 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008076 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8077 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8078 if (Result.getNode())
8079 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008080 }
8081
Nadav Rotemdf832032012-08-13 18:52:44 +00008082 // The code below optimizes (or (and X, Y), Z).
8083 // The AND operand needs to have a single user to make these optimizations
8084 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008085 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00008086 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008087 return SDValue();
8088 SDValue N1 = N->getOperand(1);
8089
8090 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8091 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8092 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8093 APInt SplatUndef;
8094 unsigned SplatBitSize;
8095 bool HasAnyUndefs;
8096
8097 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8098 APInt SplatBits0;
8099 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8100 HasAnyUndefs) && !HasAnyUndefs) {
8101 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8102 APInt SplatBits1;
8103 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8104 HasAnyUndefs) && !HasAnyUndefs &&
8105 SplatBits0 == ~SplatBits1) {
8106 // Canonicalize the vector type to make instruction selection simpler.
8107 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8108 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8109 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00008110 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008111 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8112 }
8113 }
8114 }
8115
Jim Grosbach54238562010-07-17 03:30:54 +00008116 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8117 // reasonable.
8118
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008119 // BFI is only available on V6T2+
8120 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8121 return SDValue();
8122
Jim Grosbach54238562010-07-17 03:30:54 +00008123 DebugLoc DL = N->getDebugLoc();
8124 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008125 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00008126 //
8127 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008128 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008129 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008130 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008131 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00008132 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008133
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008134 if (VT != MVT::i32)
8135 return SDValue();
8136
Evan Cheng30fb13f2010-12-13 20:32:54 +00008137 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00008138
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008139 // The value and the mask need to be constants so we can verify this is
8140 // actually a bitfield set. If the mask is 0xffff, we can do better
8141 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00008142 SDValue MaskOp = N0.getOperand(1);
8143 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8144 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008145 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008146 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008147 if (Mask == 0xffff)
8148 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008149 SDValue Res;
8150 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008151 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8152 if (N1C) {
8153 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008154 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00008155 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008156
Evan Chenga9688c42010-12-11 04:11:38 +00008157 if (ARM::isBitFieldInvertedMask(Mask)) {
8158 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008159
Evan Cheng30fb13f2010-12-13 20:32:54 +00008160 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00008161 DAG.getConstant(Val, MVT::i32),
8162 DAG.getConstant(Mask, MVT::i32));
8163
8164 // Do not add new nodes to DAG combiner worklist.
8165 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008166 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008167 }
Jim Grosbach54238562010-07-17 03:30:54 +00008168 } else if (N1.getOpcode() == ISD::AND) {
8169 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008170 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8171 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00008172 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008173 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008174
Eric Christopher29aeed12011-03-26 01:21:03 +00008175 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8176 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00008177 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008178 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008179 // The pack halfword instruction works better for masks that fit it,
8180 // so use that when it's available.
8181 if (Subtarget->hasT2ExtractPack() &&
8182 (Mask == 0xffff || Mask == 0xffff0000))
8183 return SDValue();
8184 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00008185 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00008186 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00008187 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00008188 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00008189 DAG.getConstant(Mask, MVT::i32));
8190 // Do not add new nodes to DAG combiner worklist.
8191 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008192 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008193 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008194 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008195 // The pack halfword instruction works better for masks that fit it,
8196 // so use that when it's available.
8197 if (Subtarget->hasT2ExtractPack() &&
8198 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8199 return SDValue();
8200 // 2b
8201 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008202 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00008203 DAG.getConstant(lsb, MVT::i32));
8204 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00008205 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00008206 // Do not add new nodes to DAG combiner worklist.
8207 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008208 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008209 }
8210 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008211
Evan Cheng30fb13f2010-12-13 20:32:54 +00008212 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8213 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8214 ARM::isBitFieldInvertedMask(~Mask)) {
8215 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8216 // where lsb(mask) == #shamt and masked bits of B are known zero.
8217 SDValue ShAmt = N00.getOperand(1);
8218 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8219 unsigned LSB = CountTrailingZeros_32(Mask);
8220 if (ShAmtC != LSB)
8221 return SDValue();
8222
8223 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8224 DAG.getConstant(~Mask, MVT::i32));
8225
8226 // Do not add new nodes to DAG combiner worklist.
8227 DCI.CombineTo(N, Res, false);
8228 }
8229
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008230 return SDValue();
8231}
8232
Evan Chengc892aeb2012-02-23 01:19:06 +00008233static SDValue PerformXORCombine(SDNode *N,
8234 TargetLowering::DAGCombinerInfo &DCI,
8235 const ARMSubtarget *Subtarget) {
8236 EVT VT = N->getValueType(0);
8237 SelectionDAG &DAG = DCI.DAG;
8238
8239 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8240 return SDValue();
8241
8242 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008243 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8244 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8245 if (Result.getNode())
8246 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008247 }
8248
8249 return SDValue();
8250}
8251
Evan Chengbf188ae2011-06-15 01:12:31 +00008252/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8253/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008254static SDValue PerformBFICombine(SDNode *N,
8255 TargetLowering::DAGCombinerInfo &DCI) {
8256 SDValue N1 = N->getOperand(1);
8257 if (N1.getOpcode() == ISD::AND) {
8258 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8259 if (!N11C)
8260 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008261 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8262 unsigned LSB = CountTrailingZeros_32(~InvMask);
8263 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8264 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008265 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008266 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008267 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8268 N->getOperand(0), N1.getOperand(0),
8269 N->getOperand(2));
8270 }
8271 return SDValue();
8272}
8273
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008274/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8275/// ARMISD::VMOVRRD.
8276static SDValue PerformVMOVRRDCombine(SDNode *N,
8277 TargetLowering::DAGCombinerInfo &DCI) {
8278 // vmovrrd(vmovdrr x, y) -> x,y
8279 SDValue InDouble = N->getOperand(0);
8280 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8281 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008282
8283 // vmovrrd(load f64) -> (load i32), (load i32)
8284 SDNode *InNode = InDouble.getNode();
8285 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8286 InNode->getValueType(0) == MVT::f64 &&
8287 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8288 !cast<LoadSDNode>(InNode)->isVolatile()) {
8289 // TODO: Should this be done for non-FrameIndex operands?
8290 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8291
8292 SelectionDAG &DAG = DCI.DAG;
8293 DebugLoc DL = LD->getDebugLoc();
8294 SDValue BasePtr = LD->getBasePtr();
8295 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8296 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008297 LD->isNonTemporal(), LD->isInvariant(),
8298 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008299
8300 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8301 DAG.getConstant(4, MVT::i32));
8302 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8303 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008304 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008305 std::min(4U, LD->getAlignment() / 2));
8306
8307 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8308 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8309 DCI.RemoveFromWorklist(LD);
8310 DAG.DeleteNode(LD);
8311 return Result;
8312 }
8313
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008314 return SDValue();
8315}
8316
8317/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8318/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8319static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8320 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8321 SDValue Op0 = N->getOperand(0);
8322 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008323 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008324 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008325 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008326 Op1 = Op1.getOperand(0);
8327 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8328 Op0.getNode() == Op1.getNode() &&
8329 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008330 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008331 N->getValueType(0), Op0.getOperand(0));
8332 return SDValue();
8333}
8334
Bob Wilson31600902010-12-21 06:43:19 +00008335/// PerformSTORECombine - Target-specific dag combine xforms for
8336/// ISD::STORE.
8337static SDValue PerformSTORECombine(SDNode *N,
8338 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008339 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008340 if (St->isVolatile())
8341 return SDValue();
8342
Andrew Trick49b446f2012-07-18 18:34:24 +00008343 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008344 // pack all of the elements in one place. Next, store to memory in fewer
8345 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008346 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008347 EVT VT = StVal.getValueType();
8348 if (St->isTruncatingStore() && VT.isVector()) {
8349 SelectionDAG &DAG = DCI.DAG;
8350 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8351 EVT StVT = St->getMemoryVT();
8352 unsigned NumElems = VT.getVectorNumElements();
8353 assert(StVT != VT && "Cannot truncate to the same type");
8354 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8355 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8356
8357 // From, To sizes and ElemCount must be pow of two
8358 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8359
8360 // We are going to use the original vector elt for storing.
8361 // Accumulated smaller vector elements must be a multiple of the store size.
8362 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8363
8364 unsigned SizeRatio = FromEltSz / ToEltSz;
8365 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8366
8367 // Create a type on which we perform the shuffle.
8368 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8369 NumElems*SizeRatio);
8370 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8371
8372 DebugLoc DL = St->getDebugLoc();
8373 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8374 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8375 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8376
8377 // Can't shuffle using an illegal type.
8378 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8379
8380 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8381 DAG.getUNDEF(WideVec.getValueType()),
8382 ShuffleVec.data());
8383 // At this point all of the data is stored at the bottom of the
8384 // register. We now need to save it to mem.
8385
8386 // Find the largest store unit
8387 MVT StoreType = MVT::i8;
8388 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8389 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8390 MVT Tp = (MVT::SimpleValueType)tp;
8391 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8392 StoreType = Tp;
8393 }
8394 // Didn't find a legal store type.
8395 if (!TLI.isTypeLegal(StoreType))
8396 return SDValue();
8397
8398 // Bitcast the original vector into a vector of store-size units
8399 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8400 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8401 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8402 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8403 SmallVector<SDValue, 8> Chains;
8404 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8405 TLI.getPointerTy());
8406 SDValue BasePtr = St->getBasePtr();
8407
8408 // Perform one or more big stores into memory.
8409 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8410 for (unsigned I = 0; I < E; I++) {
8411 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8412 StoreType, ShuffWide,
8413 DAG.getIntPtrConstant(I));
8414 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8415 St->getPointerInfo(), St->isVolatile(),
8416 St->isNonTemporal(), St->getAlignment());
8417 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8418 Increment);
8419 Chains.push_back(Ch);
8420 }
8421 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8422 Chains.size());
8423 }
8424
8425 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008426 return SDValue();
8427
Chad Rosier96b66d62012-04-09 19:38:15 +00008428 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8429 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008430 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008431 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008432 SelectionDAG &DAG = DCI.DAG;
8433 DebugLoc DL = St->getDebugLoc();
8434 SDValue BasePtr = St->getBasePtr();
8435 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8436 StVal.getNode()->getOperand(0), BasePtr,
8437 St->getPointerInfo(), St->isVolatile(),
8438 St->isNonTemporal(), St->getAlignment());
8439
8440 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8441 DAG.getConstant(4, MVT::i32));
8442 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8443 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8444 St->isNonTemporal(),
8445 std::min(4U, St->getAlignment() / 2));
8446 }
8447
8448 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008449 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8450 return SDValue();
8451
Chad Rosier96b66d62012-04-09 19:38:15 +00008452 // Bitcast an i64 store extracted from a vector to f64.
8453 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008454 SelectionDAG &DAG = DCI.DAG;
8455 DebugLoc dl = StVal.getDebugLoc();
8456 SDValue IntVec = StVal.getOperand(0);
8457 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8458 IntVec.getValueType().getVectorNumElements());
8459 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8460 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8461 Vec, StVal.getOperand(1));
8462 dl = N->getDebugLoc();
8463 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8464 // Make the DAGCombiner fold the bitcasts.
8465 DCI.AddToWorklist(Vec.getNode());
8466 DCI.AddToWorklist(ExtElt.getNode());
8467 DCI.AddToWorklist(V.getNode());
8468 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8469 St->getPointerInfo(), St->isVolatile(),
8470 St->isNonTemporal(), St->getAlignment(),
8471 St->getTBAAInfo());
8472}
8473
8474/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8475/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8476/// i64 vector to have f64 elements, since the value can then be loaded
8477/// directly into a VFP register.
8478static bool hasNormalLoadOperand(SDNode *N) {
8479 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8480 for (unsigned i = 0; i < NumElts; ++i) {
8481 SDNode *Elt = N->getOperand(i).getNode();
8482 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8483 return true;
8484 }
8485 return false;
8486}
8487
Bob Wilson75f02882010-09-17 22:59:05 +00008488/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8489/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008490static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8491 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008492 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8493 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8494 // into a pair of GPRs, which is fine when the value is used as a scalar,
8495 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008496 SelectionDAG &DAG = DCI.DAG;
8497 if (N->getNumOperands() == 2) {
8498 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8499 if (RV.getNode())
8500 return RV;
8501 }
Bob Wilson75f02882010-09-17 22:59:05 +00008502
Bob Wilson31600902010-12-21 06:43:19 +00008503 // Load i64 elements as f64 values so that type legalization does not split
8504 // them up into i32 values.
8505 EVT VT = N->getValueType(0);
8506 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8507 return SDValue();
8508 DebugLoc dl = N->getDebugLoc();
8509 SmallVector<SDValue, 8> Ops;
8510 unsigned NumElts = VT.getVectorNumElements();
8511 for (unsigned i = 0; i < NumElts; ++i) {
8512 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8513 Ops.push_back(V);
8514 // Make the DAGCombiner fold the bitcast.
8515 DCI.AddToWorklist(V.getNode());
8516 }
8517 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8518 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8519 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8520}
8521
8522/// PerformInsertEltCombine - Target-specific dag combine xforms for
8523/// ISD::INSERT_VECTOR_ELT.
8524static SDValue PerformInsertEltCombine(SDNode *N,
8525 TargetLowering::DAGCombinerInfo &DCI) {
8526 // Bitcast an i64 load inserted into a vector to f64.
8527 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8528 EVT VT = N->getValueType(0);
8529 SDNode *Elt = N->getOperand(1).getNode();
8530 if (VT.getVectorElementType() != MVT::i64 ||
8531 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8532 return SDValue();
8533
8534 SelectionDAG &DAG = DCI.DAG;
8535 DebugLoc dl = N->getDebugLoc();
8536 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8537 VT.getVectorNumElements());
8538 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8539 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8540 // Make the DAGCombiner fold the bitcasts.
8541 DCI.AddToWorklist(Vec.getNode());
8542 DCI.AddToWorklist(V.getNode());
8543 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8544 Vec, V, N->getOperand(2));
8545 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008546}
8547
Bob Wilsonf20700c2010-10-27 20:38:28 +00008548/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8549/// ISD::VECTOR_SHUFFLE.
8550static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8551 // The LLVM shufflevector instruction does not require the shuffle mask
8552 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8553 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8554 // operands do not match the mask length, they are extended by concatenating
8555 // them with undef vectors. That is probably the right thing for other
8556 // targets, but for NEON it is better to concatenate two double-register
8557 // size vector operands into a single quad-register size vector. Do that
8558 // transformation here:
8559 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8560 // shuffle(concat(v1, v2), undef)
8561 SDValue Op0 = N->getOperand(0);
8562 SDValue Op1 = N->getOperand(1);
8563 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8564 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8565 Op0.getNumOperands() != 2 ||
8566 Op1.getNumOperands() != 2)
8567 return SDValue();
8568 SDValue Concat0Op1 = Op0.getOperand(1);
8569 SDValue Concat1Op1 = Op1.getOperand(1);
8570 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8571 Concat1Op1.getOpcode() != ISD::UNDEF)
8572 return SDValue();
8573 // Skip the transformation if any of the types are illegal.
8574 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8575 EVT VT = N->getValueType(0);
8576 if (!TLI.isTypeLegal(VT) ||
8577 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8578 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8579 return SDValue();
8580
8581 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8582 Op0.getOperand(0), Op1.getOperand(0));
8583 // Translate the shuffle mask.
8584 SmallVector<int, 16> NewMask;
8585 unsigned NumElts = VT.getVectorNumElements();
8586 unsigned HalfElts = NumElts/2;
8587 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8588 for (unsigned n = 0; n < NumElts; ++n) {
8589 int MaskElt = SVN->getMaskElt(n);
8590 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008591 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008592 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008593 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008594 NewElt = HalfElts + MaskElt - NumElts;
8595 NewMask.push_back(NewElt);
8596 }
8597 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8598 DAG.getUNDEF(VT), NewMask.data());
8599}
8600
Bob Wilson1c3ef902011-02-07 17:43:21 +00008601/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8602/// NEON load/store intrinsics to merge base address updates.
8603static SDValue CombineBaseUpdate(SDNode *N,
8604 TargetLowering::DAGCombinerInfo &DCI) {
8605 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8606 return SDValue();
8607
8608 SelectionDAG &DAG = DCI.DAG;
8609 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8610 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8611 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8612 SDValue Addr = N->getOperand(AddrOpIdx);
8613
8614 // Search for a use of the address operand that is an increment.
8615 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8616 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8617 SDNode *User = *UI;
8618 if (User->getOpcode() != ISD::ADD ||
8619 UI.getUse().getResNo() != Addr.getResNo())
8620 continue;
8621
8622 // Check that the add is independent of the load/store. Otherwise, folding
8623 // it would create a cycle.
8624 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8625 continue;
8626
8627 // Find the new opcode for the updating load/store.
8628 bool isLoad = true;
8629 bool isLaneOp = false;
8630 unsigned NewOpc = 0;
8631 unsigned NumVecs = 0;
8632 if (isIntrinsic) {
8633 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8634 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008635 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008636 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8637 NumVecs = 1; break;
8638 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8639 NumVecs = 2; break;
8640 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8641 NumVecs = 3; break;
8642 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8643 NumVecs = 4; break;
8644 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8645 NumVecs = 2; isLaneOp = true; break;
8646 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8647 NumVecs = 3; isLaneOp = true; break;
8648 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8649 NumVecs = 4; isLaneOp = true; break;
8650 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8651 NumVecs = 1; isLoad = false; break;
8652 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8653 NumVecs = 2; isLoad = false; break;
8654 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8655 NumVecs = 3; isLoad = false; break;
8656 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8657 NumVecs = 4; isLoad = false; break;
8658 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8659 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8660 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8661 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8662 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8663 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8664 }
8665 } else {
8666 isLaneOp = true;
8667 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008668 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008669 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8670 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8671 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8672 }
8673 }
8674
8675 // Find the size of memory referenced by the load/store.
8676 EVT VecTy;
8677 if (isLoad)
8678 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008679 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008680 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8681 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8682 if (isLaneOp)
8683 NumBytes /= VecTy.getVectorNumElements();
8684
8685 // If the increment is a constant, it must match the memory ref size.
8686 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8687 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8688 uint64_t IncVal = CInc->getZExtValue();
8689 if (IncVal != NumBytes)
8690 continue;
8691 } else if (NumBytes >= 3 * 16) {
8692 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8693 // separate instructions that make it harder to use a non-constant update.
8694 continue;
8695 }
8696
8697 // Create the new updating load/store node.
8698 EVT Tys[6];
8699 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8700 unsigned n;
8701 for (n = 0; n < NumResultVecs; ++n)
8702 Tys[n] = VecTy;
8703 Tys[n++] = MVT::i32;
8704 Tys[n] = MVT::Other;
8705 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8706 SmallVector<SDValue, 8> Ops;
8707 Ops.push_back(N->getOperand(0)); // incoming chain
8708 Ops.push_back(N->getOperand(AddrOpIdx));
8709 Ops.push_back(Inc);
8710 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8711 Ops.push_back(N->getOperand(i));
8712 }
8713 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8714 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8715 Ops.data(), Ops.size(),
8716 MemInt->getMemoryVT(),
8717 MemInt->getMemOperand());
8718
8719 // Update the uses.
8720 std::vector<SDValue> NewResults;
8721 for (unsigned i = 0; i < NumResultVecs; ++i) {
8722 NewResults.push_back(SDValue(UpdN.getNode(), i));
8723 }
8724 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8725 DCI.CombineTo(N, NewResults);
8726 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8727
8728 break;
Owen Anderson76706012011-04-05 21:48:57 +00008729 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008730 return SDValue();
8731}
8732
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008733/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8734/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8735/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8736/// return true.
8737static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8738 SelectionDAG &DAG = DCI.DAG;
8739 EVT VT = N->getValueType(0);
8740 // vldN-dup instructions only support 64-bit vectors for N > 1.
8741 if (!VT.is64BitVector())
8742 return false;
8743
8744 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8745 SDNode *VLD = N->getOperand(0).getNode();
8746 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8747 return false;
8748 unsigned NumVecs = 0;
8749 unsigned NewOpc = 0;
8750 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8751 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8752 NumVecs = 2;
8753 NewOpc = ARMISD::VLD2DUP;
8754 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8755 NumVecs = 3;
8756 NewOpc = ARMISD::VLD3DUP;
8757 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8758 NumVecs = 4;
8759 NewOpc = ARMISD::VLD4DUP;
8760 } else {
8761 return false;
8762 }
8763
8764 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8765 // numbers match the load.
8766 unsigned VLDLaneNo =
8767 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8768 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8769 UI != UE; ++UI) {
8770 // Ignore uses of the chain result.
8771 if (UI.getUse().getResNo() == NumVecs)
8772 continue;
8773 SDNode *User = *UI;
8774 if (User->getOpcode() != ARMISD::VDUPLANE ||
8775 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8776 return false;
8777 }
8778
8779 // Create the vldN-dup node.
8780 EVT Tys[5];
8781 unsigned n;
8782 for (n = 0; n < NumVecs; ++n)
8783 Tys[n] = VT;
8784 Tys[n] = MVT::Other;
8785 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8786 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8787 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8788 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8789 Ops, 2, VLDMemInt->getMemoryVT(),
8790 VLDMemInt->getMemOperand());
8791
8792 // Update the uses.
8793 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8794 UI != UE; ++UI) {
8795 unsigned ResNo = UI.getUse().getResNo();
8796 // Ignore uses of the chain result.
8797 if (ResNo == NumVecs)
8798 continue;
8799 SDNode *User = *UI;
8800 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8801 }
8802
8803 // Now the vldN-lane intrinsic is dead except for its chain result.
8804 // Update uses of the chain.
8805 std::vector<SDValue> VLDDupResults;
8806 for (unsigned n = 0; n < NumVecs; ++n)
8807 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8808 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8809 DCI.CombineTo(VLD, VLDDupResults);
8810
8811 return true;
8812}
8813
Bob Wilson9e82bf12010-07-14 01:22:12 +00008814/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8815/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008816static SDValue PerformVDUPLANECombine(SDNode *N,
8817 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008818 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008819
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008820 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8821 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8822 if (CombineVLDDUP(N, DCI))
8823 return SDValue(N, 0);
8824
8825 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8826 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008827 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008828 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008829 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008830 return SDValue();
8831
8832 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8833 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8834 // The canonical VMOV for a zero vector uses a 32-bit element size.
8835 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8836 unsigned EltBits;
8837 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8838 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008839 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008840 if (EltSize > VT.getVectorElementType().getSizeInBits())
8841 return SDValue();
8842
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008843 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008844}
8845
Eric Christopherfa6f5912011-06-29 21:10:36 +00008846// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008847// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8848static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8849{
Chad Rosier118c9a02011-06-28 17:26:57 +00008850 integerPart cN;
8851 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008852 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8853 I != E; I++) {
8854 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8855 if (!C)
8856 return false;
8857
Eric Christopherfa6f5912011-06-29 21:10:36 +00008858 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008859 APFloat APF = C->getValueAPF();
8860 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8861 != APFloat::opOK || !isExact)
8862 return false;
8863
8864 c0 = (I == 0) ? cN : c0;
8865 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8866 return false;
8867 }
8868 C = c0;
8869 return true;
8870}
8871
8872/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8873/// can replace combinations of VMUL and VCVT (floating-point to integer)
8874/// when the VMUL has a constant operand that is a power of 2.
8875///
8876/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8877/// vmul.f32 d16, d17, d16
8878/// vcvt.s32.f32 d16, d16
8879/// becomes:
8880/// vcvt.s32.f32 d16, d16, #3
8881static SDValue PerformVCVTCombine(SDNode *N,
8882 TargetLowering::DAGCombinerInfo &DCI,
8883 const ARMSubtarget *Subtarget) {
8884 SelectionDAG &DAG = DCI.DAG;
8885 SDValue Op = N->getOperand(0);
8886
8887 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8888 Op.getOpcode() != ISD::FMUL)
8889 return SDValue();
8890
8891 uint64_t C;
8892 SDValue N0 = Op->getOperand(0);
8893 SDValue ConstVec = Op->getOperand(1);
8894 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8895
Eric Christopherfa6f5912011-06-29 21:10:36 +00008896 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008897 !isConstVecPow2(ConstVec, isSigned, C))
8898 return SDValue();
8899
8900 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8901 Intrinsic::arm_neon_vcvtfp2fxu;
8902 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8903 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008904 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008905 DAG.getConstant(Log2_64(C), MVT::i32));
8906}
8907
8908/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8909/// can replace combinations of VCVT (integer to floating-point) and VDIV
8910/// when the VDIV has a constant operand that is a power of 2.
8911///
8912/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8913/// vcvt.f32.s32 d16, d16
8914/// vdiv.f32 d16, d17, d16
8915/// becomes:
8916/// vcvt.f32.s32 d16, d16, #3
8917static SDValue PerformVDIVCombine(SDNode *N,
8918 TargetLowering::DAGCombinerInfo &DCI,
8919 const ARMSubtarget *Subtarget) {
8920 SelectionDAG &DAG = DCI.DAG;
8921 SDValue Op = N->getOperand(0);
8922 unsigned OpOpcode = Op.getNode()->getOpcode();
8923
8924 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8925 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8926 return SDValue();
8927
8928 uint64_t C;
8929 SDValue ConstVec = N->getOperand(1);
8930 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8931
8932 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8933 !isConstVecPow2(ConstVec, isSigned, C))
8934 return SDValue();
8935
Eric Christopherfa6f5912011-06-29 21:10:36 +00008936 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008937 Intrinsic::arm_neon_vcvtfxu2fp;
8938 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8939 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008940 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008941 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8942}
8943
8944/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008945/// operand of a vector shift operation, where all the elements of the
8946/// build_vector must have the same constant integer value.
8947static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8948 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008949 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008950 Op = Op.getOperand(0);
8951 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8952 APInt SplatBits, SplatUndef;
8953 unsigned SplatBitSize;
8954 bool HasAnyUndefs;
8955 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8956 HasAnyUndefs, ElementBits) ||
8957 SplatBitSize > ElementBits)
8958 return false;
8959 Cnt = SplatBits.getSExtValue();
8960 return true;
8961}
8962
8963/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8964/// operand of a vector shift left operation. That value must be in the range:
8965/// 0 <= Value < ElementBits for a left shift; or
8966/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008967static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008968 assert(VT.isVector() && "vector shift count is not a vector type");
8969 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8970 if (! getVShiftImm(Op, ElementBits, Cnt))
8971 return false;
8972 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8973}
8974
8975/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8976/// operand of a vector shift right operation. For a shift opcode, the value
8977/// is positive, but for an intrinsic the value count must be negative. The
8978/// absolute value must be in the range:
8979/// 1 <= |Value| <= ElementBits for a right shift; or
8980/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008981static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008982 int64_t &Cnt) {
8983 assert(VT.isVector() && "vector shift count is not a vector type");
8984 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8985 if (! getVShiftImm(Op, ElementBits, Cnt))
8986 return false;
8987 if (isIntrinsic)
8988 Cnt = -Cnt;
8989 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8990}
8991
8992/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8993static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8994 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8995 switch (IntNo) {
8996 default:
8997 // Don't do anything for most intrinsics.
8998 break;
8999
9000 // Vector shifts: check for immediate versions and lower them.
9001 // Note: This is done during DAG combining instead of DAG legalizing because
9002 // the build_vectors for 64-bit vector element shift counts are generally
9003 // not legal, and it is hard to see their values after they get legalized to
9004 // loads from a constant pool.
9005 case Intrinsic::arm_neon_vshifts:
9006 case Intrinsic::arm_neon_vshiftu:
9007 case Intrinsic::arm_neon_vshiftls:
9008 case Intrinsic::arm_neon_vshiftlu:
9009 case Intrinsic::arm_neon_vshiftn:
9010 case Intrinsic::arm_neon_vrshifts:
9011 case Intrinsic::arm_neon_vrshiftu:
9012 case Intrinsic::arm_neon_vrshiftn:
9013 case Intrinsic::arm_neon_vqshifts:
9014 case Intrinsic::arm_neon_vqshiftu:
9015 case Intrinsic::arm_neon_vqshiftsu:
9016 case Intrinsic::arm_neon_vqshiftns:
9017 case Intrinsic::arm_neon_vqshiftnu:
9018 case Intrinsic::arm_neon_vqshiftnsu:
9019 case Intrinsic::arm_neon_vqrshiftns:
9020 case Intrinsic::arm_neon_vqrshiftnu:
9021 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00009022 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009023 int64_t Cnt;
9024 unsigned VShiftOpc = 0;
9025
9026 switch (IntNo) {
9027 case Intrinsic::arm_neon_vshifts:
9028 case Intrinsic::arm_neon_vshiftu:
9029 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9030 VShiftOpc = ARMISD::VSHL;
9031 break;
9032 }
9033 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9034 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9035 ARMISD::VSHRs : ARMISD::VSHRu);
9036 break;
9037 }
9038 return SDValue();
9039
9040 case Intrinsic::arm_neon_vshiftls:
9041 case Intrinsic::arm_neon_vshiftlu:
9042 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
9043 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009044 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009045
9046 case Intrinsic::arm_neon_vrshifts:
9047 case Intrinsic::arm_neon_vrshiftu:
9048 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9049 break;
9050 return SDValue();
9051
9052 case Intrinsic::arm_neon_vqshifts:
9053 case Intrinsic::arm_neon_vqshiftu:
9054 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9055 break;
9056 return SDValue();
9057
9058 case Intrinsic::arm_neon_vqshiftsu:
9059 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9060 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009061 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009062
9063 case Intrinsic::arm_neon_vshiftn:
9064 case Intrinsic::arm_neon_vrshiftn:
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:
9071 // Narrowing shifts require an immediate right shift.
9072 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9073 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00009074 llvm_unreachable("invalid shift count for narrowing vector shift "
9075 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009076
9077 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009078 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00009079 }
9080
9081 switch (IntNo) {
9082 case Intrinsic::arm_neon_vshifts:
9083 case Intrinsic::arm_neon_vshiftu:
9084 // Opcode already set above.
9085 break;
9086 case Intrinsic::arm_neon_vshiftls:
9087 case Intrinsic::arm_neon_vshiftlu:
9088 if (Cnt == VT.getVectorElementType().getSizeInBits())
9089 VShiftOpc = ARMISD::VSHLLi;
9090 else
9091 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9092 ARMISD::VSHLLs : ARMISD::VSHLLu);
9093 break;
9094 case Intrinsic::arm_neon_vshiftn:
9095 VShiftOpc = ARMISD::VSHRN; break;
9096 case Intrinsic::arm_neon_vrshifts:
9097 VShiftOpc = ARMISD::VRSHRs; break;
9098 case Intrinsic::arm_neon_vrshiftu:
9099 VShiftOpc = ARMISD::VRSHRu; break;
9100 case Intrinsic::arm_neon_vrshiftn:
9101 VShiftOpc = ARMISD::VRSHRN; break;
9102 case Intrinsic::arm_neon_vqshifts:
9103 VShiftOpc = ARMISD::VQSHLs; break;
9104 case Intrinsic::arm_neon_vqshiftu:
9105 VShiftOpc = ARMISD::VQSHLu; break;
9106 case Intrinsic::arm_neon_vqshiftsu:
9107 VShiftOpc = ARMISD::VQSHLsu; break;
9108 case Intrinsic::arm_neon_vqshiftns:
9109 VShiftOpc = ARMISD::VQSHRNs; break;
9110 case Intrinsic::arm_neon_vqshiftnu:
9111 VShiftOpc = ARMISD::VQSHRNu; break;
9112 case Intrinsic::arm_neon_vqshiftnsu:
9113 VShiftOpc = ARMISD::VQSHRNsu; break;
9114 case Intrinsic::arm_neon_vqrshiftns:
9115 VShiftOpc = ARMISD::VQRSHRNs; break;
9116 case Intrinsic::arm_neon_vqrshiftnu:
9117 VShiftOpc = ARMISD::VQRSHRNu; break;
9118 case Intrinsic::arm_neon_vqrshiftnsu:
9119 VShiftOpc = ARMISD::VQRSHRNsu; break;
9120 }
9121
9122 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009123 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009124 }
9125
9126 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00009127 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009128 int64_t Cnt;
9129 unsigned VShiftOpc = 0;
9130
9131 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9132 VShiftOpc = ARMISD::VSLI;
9133 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9134 VShiftOpc = ARMISD::VSRI;
9135 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00009136 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009137 }
9138
9139 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
9140 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009141 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009142 }
9143
9144 case Intrinsic::arm_neon_vqrshifts:
9145 case Intrinsic::arm_neon_vqrshiftu:
9146 // No immediate versions of these to check for.
9147 break;
9148 }
9149
9150 return SDValue();
9151}
9152
9153/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9154/// lowers them. As with the vector shift intrinsics, this is done during DAG
9155/// combining instead of DAG legalizing because the build_vectors for 64-bit
9156/// vector element shift counts are generally not legal, and it is hard to see
9157/// their values after they get legalized to loads from a constant pool.
9158static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9159 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00009160 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00009161 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9162 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9163 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9164 SDValue N1 = N->getOperand(1);
9165 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9166 SDValue N0 = N->getOperand(0);
9167 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9168 DAG.MaskedValueIsZero(N0.getOperand(0),
9169 APInt::getHighBitsSet(32, 16)))
9170 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
9171 }
9172 }
Bob Wilson5bafff32009-06-22 23:27:02 +00009173
9174 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00009175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9176 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00009177 return SDValue();
9178
9179 assert(ST->hasNEON() && "unexpected vector shift");
9180 int64_t Cnt;
9181
9182 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009183 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009184
9185 case ISD::SHL:
9186 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9187 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009188 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009189 break;
9190
9191 case ISD::SRA:
9192 case ISD::SRL:
9193 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9194 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9195 ARMISD::VSHRs : ARMISD::VSHRu);
9196 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009197 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009198 }
9199 }
9200 return SDValue();
9201}
9202
9203/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9204/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9205static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9206 const ARMSubtarget *ST) {
9207 SDValue N0 = N->getOperand(0);
9208
9209 // Check for sign- and zero-extensions of vector extract operations of 8-
9210 // and 16-bit vector elements. NEON supports these directly. They are
9211 // handled during DAG combining because type legalization will promote them
9212 // to 32-bit types and it is messy to recognize the operations after that.
9213 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9214 SDValue Vec = N0.getOperand(0);
9215 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009216 EVT VT = N->getValueType(0);
9217 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009218 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9219
Owen Anderson825b72b2009-08-11 20:47:22 +00009220 if (VT == MVT::i32 &&
9221 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00009222 TLI.isTypeLegal(Vec.getValueType()) &&
9223 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009224
9225 unsigned Opc = 0;
9226 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009227 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009228 case ISD::SIGN_EXTEND:
9229 Opc = ARMISD::VGETLANEs;
9230 break;
9231 case ISD::ZERO_EXTEND:
9232 case ISD::ANY_EXTEND:
9233 Opc = ARMISD::VGETLANEu;
9234 break;
9235 }
9236 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9237 }
9238 }
9239
9240 return SDValue();
9241}
9242
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009243/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9244/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9245static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9246 const ARMSubtarget *ST) {
9247 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009248 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009249 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9250 // a NaN; only do the transformation when it matches that behavior.
9251
9252 // For now only do this when using NEON for FP operations; if using VFP, it
9253 // is not obvious that the benefit outweighs the cost of switching to the
9254 // NEON pipeline.
9255 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9256 N->getValueType(0) != MVT::f32)
9257 return SDValue();
9258
9259 SDValue CondLHS = N->getOperand(0);
9260 SDValue CondRHS = N->getOperand(1);
9261 SDValue LHS = N->getOperand(2);
9262 SDValue RHS = N->getOperand(3);
9263 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9264
9265 unsigned Opcode = 0;
9266 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009267 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009268 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009269 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009270 IsReversed = true ; // x CC y ? y : x
9271 } else {
9272 return SDValue();
9273 }
9274
Bob Wilsone742bb52010-02-24 22:15:53 +00009275 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009276 switch (CC) {
9277 default: break;
9278 case ISD::SETOLT:
9279 case ISD::SETOLE:
9280 case ISD::SETLT:
9281 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009282 case ISD::SETULT:
9283 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009284 // If LHS is NaN, an ordered comparison will be false and the result will
9285 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9286 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9287 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9288 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9289 break;
9290 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9291 // will return -0, so vmin can only be used for unsafe math or if one of
9292 // the operands is known to be nonzero.
9293 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009294 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009295 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9296 break;
9297 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009298 break;
9299
9300 case ISD::SETOGT:
9301 case ISD::SETOGE:
9302 case ISD::SETGT:
9303 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009304 case ISD::SETUGT:
9305 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009306 // If LHS is NaN, an ordered comparison will be false and the result will
9307 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9308 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9309 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9310 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9311 break;
9312 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9313 // will return +0, so vmax can only be used for unsafe math or if one of
9314 // the operands is known to be nonzero.
9315 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009316 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009317 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9318 break;
9319 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009320 break;
9321 }
9322
9323 if (!Opcode)
9324 return SDValue();
9325 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9326}
9327
Evan Chenge721f5c2011-07-13 00:42:17 +00009328/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9329SDValue
9330ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9331 SDValue Cmp = N->getOperand(4);
9332 if (Cmp.getOpcode() != ARMISD::CMPZ)
9333 // Only looking at EQ and NE cases.
9334 return SDValue();
9335
9336 EVT VT = N->getValueType(0);
9337 DebugLoc dl = N->getDebugLoc();
9338 SDValue LHS = Cmp.getOperand(0);
9339 SDValue RHS = Cmp.getOperand(1);
9340 SDValue FalseVal = N->getOperand(0);
9341 SDValue TrueVal = N->getOperand(1);
9342 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009343 ARMCC::CondCodes CC =
9344 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009345
9346 // Simplify
9347 // mov r1, r0
9348 // cmp r1, x
9349 // mov r0, y
9350 // moveq r0, x
9351 // to
9352 // cmp r0, x
9353 // movne r0, y
9354 //
9355 // mov r1, r0
9356 // cmp r1, x
9357 // mov r0, x
9358 // movne r0, y
9359 // to
9360 // cmp r0, x
9361 // movne r0, y
9362 /// FIXME: Turn this into a target neutral optimization?
9363 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009364 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009365 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9366 N->getOperand(3), Cmp);
9367 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9368 SDValue ARMcc;
9369 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9370 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9371 N->getOperand(3), NewCmp);
9372 }
9373
9374 if (Res.getNode()) {
9375 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009376 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009377 // Capture demanded bits information that would be otherwise lost.
9378 if (KnownZero == 0xfffffffe)
9379 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9380 DAG.getValueType(MVT::i1));
9381 else if (KnownZero == 0xffffff00)
9382 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9383 DAG.getValueType(MVT::i8));
9384 else if (KnownZero == 0xffff0000)
9385 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9386 DAG.getValueType(MVT::i16));
9387 }
9388
9389 return Res;
9390}
9391
Dan Gohman475871a2008-07-27 21:46:04 +00009392SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009393 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009394 switch (N->getOpcode()) {
9395 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009396 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009397 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009398 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009399 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009400 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009401 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9402 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009403 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009404 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009405 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009406 case ISD::STORE: return PerformSTORECombine(N, DCI);
9407 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9408 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009409 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009410 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009411 case ISD::FP_TO_SINT:
9412 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9413 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009414 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009415 case ISD::SHL:
9416 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009417 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009418 case ISD::SIGN_EXTEND:
9419 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009420 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9421 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009422 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009423 case ARMISD::VLD2DUP:
9424 case ARMISD::VLD3DUP:
9425 case ARMISD::VLD4DUP:
9426 return CombineBaseUpdate(N, DCI);
9427 case ISD::INTRINSIC_VOID:
9428 case ISD::INTRINSIC_W_CHAIN:
9429 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9430 case Intrinsic::arm_neon_vld1:
9431 case Intrinsic::arm_neon_vld2:
9432 case Intrinsic::arm_neon_vld3:
9433 case Intrinsic::arm_neon_vld4:
9434 case Intrinsic::arm_neon_vld2lane:
9435 case Intrinsic::arm_neon_vld3lane:
9436 case Intrinsic::arm_neon_vld4lane:
9437 case Intrinsic::arm_neon_vst1:
9438 case Intrinsic::arm_neon_vst2:
9439 case Intrinsic::arm_neon_vst3:
9440 case Intrinsic::arm_neon_vst4:
9441 case Intrinsic::arm_neon_vst2lane:
9442 case Intrinsic::arm_neon_vst3lane:
9443 case Intrinsic::arm_neon_vst4lane:
9444 return CombineBaseUpdate(N, DCI);
9445 default: break;
9446 }
9447 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009448 }
Dan Gohman475871a2008-07-27 21:46:04 +00009449 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009450}
9451
Evan Cheng31959b12011-02-02 01:06:55 +00009452bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9453 EVT VT) const {
9454 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9455}
9456
Evan Cheng376642e2012-12-10 23:21:26 +00009457bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009458 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009459 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009460
9461 switch (VT.getSimpleVT().SimpleTy) {
9462 default:
9463 return false;
9464 case MVT::i8:
9465 case MVT::i16:
Evan Cheng376642e2012-12-10 23:21:26 +00009466 case MVT::i32: {
Evan Chengd10eab02012-09-18 01:42:45 +00009467 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng376642e2012-12-10 23:21:26 +00009468 if (AllowsUnaligned) {
9469 if (Fast)
9470 *Fast = Subtarget->hasV7Ops();
9471 return true;
9472 }
9473 return false;
9474 }
Evan Chenga99c5082012-08-15 17:44:53 +00009475 case MVT::f64:
Evan Cheng376642e2012-12-10 23:21:26 +00009476 case MVT::v2f64: {
Evan Chengd10eab02012-09-18 01:42:45 +00009477 // For any little-endian targets with neon, we can support unaligned ld/st
9478 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9479 // A big-endian target may also explictly support unaligned accesses
Evan Cheng376642e2012-12-10 23:21:26 +00009480 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9481 if (Fast)
9482 *Fast = true;
9483 return true;
9484 }
9485 return false;
9486 }
Bill Wendlingaf566342009-08-15 21:21:19 +00009487 }
9488}
9489
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009490static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9491 unsigned AlignCheck) {
9492 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9493 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9494}
9495
9496EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9497 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00009498 bool IsMemset, bool ZeroMemset,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009499 bool MemcpyStrSrc,
9500 MachineFunction &MF) const {
9501 const Function *F = MF.getFunction();
9502
9503 // See if we can use NEON instructions for this...
Evan Cheng946a3a92012-12-12 02:34:41 +00009504 if ((!IsMemset || ZeroMemset) &&
Evan Cheng376642e2012-12-10 23:21:26 +00009505 Subtarget->hasNEON() &&
Bill Wendling831737d2012-12-30 10:32:01 +00009506 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9507 Attribute::NoImplicitFloat)) {
Evan Cheng376642e2012-12-10 23:21:26 +00009508 bool Fast;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009509 if (Size >= 16 &&
9510 (memOpAlign(SrcAlign, DstAlign, 16) ||
9511 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009512 return MVT::v2f64;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009513 } else if (Size >= 8 &&
9514 (memOpAlign(SrcAlign, DstAlign, 8) ||
9515 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009516 return MVT::f64;
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009517 }
9518 }
9519
Lang Hames5207bf22011-11-08 18:56:23 +00009520 // Lowering to i32/i16 if the size permits.
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009521 if (Size >= 4)
Lang Hames5207bf22011-11-08 18:56:23 +00009522 return MVT::i32;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009523 else if (Size >= 2)
Lang Hames5207bf22011-11-08 18:56:23 +00009524 return MVT::i16;
Lang Hames5207bf22011-11-08 18:56:23 +00009525
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009526 // Let the target-independent logic figure it out.
9527 return MVT::Other;
9528}
9529
Evan Cheng2766a472012-12-06 19:13:27 +00009530bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9531 if (Val.getOpcode() != ISD::LOAD)
9532 return false;
9533
9534 EVT VT1 = Val.getValueType();
9535 if (!VT1.isSimple() || !VT1.isInteger() ||
9536 !VT2.isSimple() || !VT2.isInteger())
9537 return false;
9538
9539 switch (VT1.getSimpleVT().SimpleTy) {
9540 default: break;
9541 case MVT::i1:
9542 case MVT::i8:
9543 case MVT::i16:
9544 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9545 return true;
9546 }
9547
9548 return false;
9549}
9550
Evan Chenge6c835f2009-08-14 20:09:37 +00009551static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9552 if (V < 0)
9553 return false;
9554
9555 unsigned Scale = 1;
9556 switch (VT.getSimpleVT().SimpleTy) {
9557 default: return false;
9558 case MVT::i1:
9559 case MVT::i8:
9560 // Scale == 1;
9561 break;
9562 case MVT::i16:
9563 // Scale == 2;
9564 Scale = 2;
9565 break;
9566 case MVT::i32:
9567 // Scale == 4;
9568 Scale = 4;
9569 break;
9570 }
9571
9572 if ((V & (Scale - 1)) != 0)
9573 return false;
9574 V /= Scale;
9575 return V == (V & ((1LL << 5) - 1));
9576}
9577
9578static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9579 const ARMSubtarget *Subtarget) {
9580 bool isNeg = false;
9581 if (V < 0) {
9582 isNeg = true;
9583 V = - V;
9584 }
9585
9586 switch (VT.getSimpleVT().SimpleTy) {
9587 default: return false;
9588 case MVT::i1:
9589 case MVT::i8:
9590 case MVT::i16:
9591 case MVT::i32:
9592 // + imm12 or - imm8
9593 if (isNeg)
9594 return V == (V & ((1LL << 8) - 1));
9595 return V == (V & ((1LL << 12) - 1));
9596 case MVT::f32:
9597 case MVT::f64:
9598 // Same as ARM mode. FIXME: NEON?
9599 if (!Subtarget->hasVFP2())
9600 return false;
9601 if ((V & 3) != 0)
9602 return false;
9603 V >>= 2;
9604 return V == (V & ((1LL << 8) - 1));
9605 }
9606}
9607
Evan Chengb01fad62007-03-12 23:30:29 +00009608/// isLegalAddressImmediate - Return true if the integer value can be used
9609/// as the offset of the target addressing mode for load / store of the
9610/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009611static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009612 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009613 if (V == 0)
9614 return true;
9615
Evan Cheng65011532009-03-09 19:15:00 +00009616 if (!VT.isSimple())
9617 return false;
9618
Evan Chenge6c835f2009-08-14 20:09:37 +00009619 if (Subtarget->isThumb1Only())
9620 return isLegalT1AddressImmediate(V, VT);
9621 else if (Subtarget->isThumb2())
9622 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009623
Evan Chenge6c835f2009-08-14 20:09:37 +00009624 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009625 if (V < 0)
9626 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009627 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009628 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009629 case MVT::i1:
9630 case MVT::i8:
9631 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009632 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009633 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009634 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009635 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009636 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009637 case MVT::f32:
9638 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009639 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009640 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009641 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009642 return false;
9643 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009644 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009645 }
Evan Chenga8e29892007-01-19 07:51:42 +00009646}
9647
Evan Chenge6c835f2009-08-14 20:09:37 +00009648bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9649 EVT VT) const {
9650 int Scale = AM.Scale;
9651 if (Scale < 0)
9652 return false;
9653
9654 switch (VT.getSimpleVT().SimpleTy) {
9655 default: return false;
9656 case MVT::i1:
9657 case MVT::i8:
9658 case MVT::i16:
9659 case MVT::i32:
9660 if (Scale == 1)
9661 return true;
9662 // r + r << imm
9663 Scale = Scale & ~1;
9664 return Scale == 2 || Scale == 4 || Scale == 8;
9665 case MVT::i64:
9666 // r + r
9667 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9668 return true;
9669 return false;
9670 case MVT::isVoid:
9671 // Note, we allow "void" uses (basically, uses that aren't loads or
9672 // stores), because arm allows folding a scale into many arithmetic
9673 // operations. This should be made more precise and revisited later.
9674
9675 // Allow r << imm, but the imm has to be a multiple of two.
9676 if (Scale & 1) return false;
9677 return isPowerOf2_32(Scale);
9678 }
9679}
9680
Chris Lattner37caf8c2007-04-09 23:33:39 +00009681/// isLegalAddressingMode - Return true if the addressing mode represented
9682/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009683bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009684 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009685 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009686 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009687 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009688
Chris Lattner37caf8c2007-04-09 23:33:39 +00009689 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009690 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009691 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009692
Chris Lattner37caf8c2007-04-09 23:33:39 +00009693 switch (AM.Scale) {
9694 case 0: // no scale reg, must be "r+i" or "r", or "i".
9695 break;
9696 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009697 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009698 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009699 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009700 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009701 // ARM doesn't support any R+R*scale+imm addr modes.
9702 if (AM.BaseOffs)
9703 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009704
Bob Wilson2c7dab12009-04-08 17:55:28 +00009705 if (!VT.isSimple())
9706 return false;
9707
Evan Chenge6c835f2009-08-14 20:09:37 +00009708 if (Subtarget->isThumb2())
9709 return isLegalT2ScaledAddressingMode(AM, VT);
9710
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009711 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009712 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009713 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009714 case MVT::i1:
9715 case MVT::i8:
9716 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009717 if (Scale < 0) Scale = -Scale;
9718 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009719 return true;
9720 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009721 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009722 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009723 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009724 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009725 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009726 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009727 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009728
Owen Anderson825b72b2009-08-11 20:47:22 +00009729 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009730 // Note, we allow "void" uses (basically, uses that aren't loads or
9731 // stores), because arm allows folding a scale into many arithmetic
9732 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009733
Chris Lattner37caf8c2007-04-09 23:33:39 +00009734 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009735 if (Scale & 1) return false;
9736 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009737 }
Evan Chengb01fad62007-03-12 23:30:29 +00009738 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009739 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009740}
9741
Evan Cheng77e47512009-11-11 19:05:52 +00009742/// isLegalICmpImmediate - Return true if the specified immediate is legal
9743/// icmp immediate, that is the target has icmp instructions which can compare
9744/// a register against the immediate without having to materialize the
9745/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009746bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009747 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009748 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009749 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009750 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009751 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009752 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009753 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009754}
9755
Andrew Trick8d8d9612012-07-18 18:34:27 +00009756/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9757/// *or sub* immediate, that is the target has add or sub instructions which can
9758/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009759/// immediate into a register.
9760bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009761 // Same encoding for add/sub, just flip the sign.
9762 int64_t AbsImm = llvm::abs64(Imm);
9763 if (!Subtarget->isThumb())
9764 return ARM_AM::getSOImmVal(AbsImm) != -1;
9765 if (Subtarget->isThumb2())
9766 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9767 // Thumb1 only has 8-bit unsigned immediate.
9768 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009769}
9770
Owen Andersone50ed302009-08-10 22:56:29 +00009771static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009772 bool isSEXTLoad, SDValue &Base,
9773 SDValue &Offset, bool &isInc,
9774 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009775 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9776 return false;
9777
Owen Anderson825b72b2009-08-11 20:47:22 +00009778 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009779 // AddressingMode 3
9780 Base = Ptr->getOperand(0);
9781 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009782 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009783 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009784 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009785 isInc = false;
9786 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9787 return true;
9788 }
9789 }
9790 isInc = (Ptr->getOpcode() == ISD::ADD);
9791 Offset = Ptr->getOperand(1);
9792 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009793 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009794 // AddressingMode 2
9795 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009796 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009797 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009798 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009799 isInc = false;
9800 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9801 Base = Ptr->getOperand(0);
9802 return true;
9803 }
9804 }
9805
9806 if (Ptr->getOpcode() == ISD::ADD) {
9807 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009808 ARM_AM::ShiftOpc ShOpcVal=
9809 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009810 if (ShOpcVal != ARM_AM::no_shift) {
9811 Base = Ptr->getOperand(1);
9812 Offset = Ptr->getOperand(0);
9813 } else {
9814 Base = Ptr->getOperand(0);
9815 Offset = Ptr->getOperand(1);
9816 }
9817 return true;
9818 }
9819
9820 isInc = (Ptr->getOpcode() == ISD::ADD);
9821 Base = Ptr->getOperand(0);
9822 Offset = Ptr->getOperand(1);
9823 return true;
9824 }
9825
Jim Grosbache5165492009-11-09 00:11:35 +00009826 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009827 return false;
9828}
9829
Owen Andersone50ed302009-08-10 22:56:29 +00009830static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009831 bool isSEXTLoad, SDValue &Base,
9832 SDValue &Offset, bool &isInc,
9833 SelectionDAG &DAG) {
9834 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9835 return false;
9836
9837 Base = Ptr->getOperand(0);
9838 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9839 int RHSC = (int)RHS->getZExtValue();
9840 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9841 assert(Ptr->getOpcode() == ISD::ADD);
9842 isInc = false;
9843 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9844 return true;
9845 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9846 isInc = Ptr->getOpcode() == ISD::ADD;
9847 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9848 return true;
9849 }
9850 }
9851
9852 return false;
9853}
9854
Evan Chenga8e29892007-01-19 07:51:42 +00009855/// getPreIndexedAddressParts - returns true by value, base pointer and
9856/// offset pointer and addressing mode by reference if the node's address
9857/// can be legally represented as pre-indexed load / store address.
9858bool
Dan Gohman475871a2008-07-27 21:46:04 +00009859ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9860 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009861 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009862 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009863 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009864 return false;
9865
Owen Andersone50ed302009-08-10 22:56:29 +00009866 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009867 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009868 bool isSEXTLoad = false;
9869 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9870 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009871 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009872 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9873 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9874 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009875 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009876 } else
9877 return false;
9878
9879 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009880 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009881 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009882 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9883 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009884 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009885 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009886 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009887 if (!isLegal)
9888 return false;
9889
9890 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9891 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009892}
9893
9894/// getPostIndexedAddressParts - returns true by value, base pointer and
9895/// offset pointer and addressing mode by reference if this node can be
9896/// combined with a load / store to form a post-indexed load / store.
9897bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009898 SDValue &Base,
9899 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009900 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009901 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009902 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009903 return false;
9904
Owen Andersone50ed302009-08-10 22:56:29 +00009905 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009906 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009907 bool isSEXTLoad = false;
9908 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009909 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009910 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009911 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9912 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009913 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009914 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009915 } else
9916 return false;
9917
9918 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009919 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009920 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009921 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009922 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009923 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009924 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9925 isInc, DAG);
9926 if (!isLegal)
9927 return false;
9928
Evan Cheng28dad2a2010-05-18 21:31:17 +00009929 if (Ptr != Base) {
9930 // Swap base ptr and offset to catch more post-index load / store when
9931 // it's legal. In Thumb2 mode, offset must be an immediate.
9932 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9933 !Subtarget->isThumb2())
9934 std::swap(Base, Offset);
9935
9936 // Post-indexed load / store update the base pointer.
9937 if (Ptr != Base)
9938 return false;
9939 }
9940
Evan Chenge88d5ce2009-07-02 07:28:31 +00009941 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9942 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009943}
9944
Dan Gohman475871a2008-07-27 21:46:04 +00009945void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009946 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009947 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009948 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009949 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009950 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009951 switch (Op.getOpcode()) {
9952 default: break;
9953 case ARMISD::CMOV: {
9954 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009955 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009956 if (KnownZero == 0 && KnownOne == 0) return;
9957
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009958 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009959 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009960 KnownZero &= KnownZeroRHS;
9961 KnownOne &= KnownOneRHS;
9962 return;
9963 }
9964 }
9965}
9966
9967//===----------------------------------------------------------------------===//
9968// ARM Inline Assembly Support
9969//===----------------------------------------------------------------------===//
9970
Evan Cheng55d42002011-01-08 01:24:27 +00009971bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9972 // Looking for "rev" which is V6+.
9973 if (!Subtarget->hasV6Ops())
9974 return false;
9975
9976 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9977 std::string AsmStr = IA->getAsmString();
9978 SmallVector<StringRef, 4> AsmPieces;
9979 SplitString(AsmStr, AsmPieces, ";\n");
9980
9981 switch (AsmPieces.size()) {
9982 default: return false;
9983 case 1:
9984 AsmStr = AsmPieces[0];
9985 AsmPieces.clear();
9986 SplitString(AsmStr, AsmPieces, " \t,");
9987
9988 // rev $0, $1
9989 if (AsmPieces.size() == 3 &&
9990 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9991 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009992 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009993 if (Ty && Ty->getBitWidth() == 32)
9994 return IntrinsicLowering::LowerToByteSwap(CI);
9995 }
9996 break;
9997 }
9998
9999 return false;
10000}
10001
Evan Chenga8e29892007-01-19 07:51:42 +000010002/// getConstraintType - Given a constraint letter, return the type of
10003/// constraint it is for this target.
10004ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010005ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10006 if (Constraint.size() == 1) {
10007 switch (Constraint[0]) {
10008 default: break;
10009 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010010 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +000010011 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010012 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010013 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +000010014 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +000010015 // An address with a single base register. Due to the way we
10016 // currently handle addresses it is the same as an 'r' memory constraint.
10017 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +000010018 }
Eric Christopher1312ca82011-06-21 22:10:57 +000010019 } else if (Constraint.size() == 2) {
10020 switch (Constraint[0]) {
10021 default: break;
10022 // All 'U+' constraints are addresses.
10023 case 'U': return C_Memory;
10024 }
Evan Chenga8e29892007-01-19 07:51:42 +000010025 }
Chris Lattner4234f572007-03-25 02:14:49 +000010026 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +000010027}
10028
John Thompson44ab89e2010-10-29 17:29:13 +000010029/// Examine constraint type and operand type and determine a weight value.
10030/// This object must already have been set up with the operand type
10031/// and the current alternative constraint selected.
10032TargetLowering::ConstraintWeight
10033ARMTargetLowering::getSingleConstraintMatchWeight(
10034 AsmOperandInfo &info, const char *constraint) const {
10035 ConstraintWeight weight = CW_Invalid;
10036 Value *CallOperandVal = info.CallOperandVal;
10037 // If we don't have a value, we can't do a match,
10038 // but allow it at the lowest weight.
10039 if (CallOperandVal == NULL)
10040 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010041 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +000010042 // Look at the constraint type.
10043 switch (*constraint) {
10044 default:
10045 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10046 break;
10047 case 'l':
10048 if (type->isIntegerTy()) {
10049 if (Subtarget->isThumb())
10050 weight = CW_SpecificReg;
10051 else
10052 weight = CW_Register;
10053 }
10054 break;
10055 case 'w':
10056 if (type->isFloatingPointTy())
10057 weight = CW_Register;
10058 break;
10059 }
10060 return weight;
10061}
10062
Eric Christopher35e6d4d2011-06-30 23:50:52 +000010063typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10064RCPair
Evan Chenga8e29892007-01-19 07:51:42 +000010065ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010066 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +000010067 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010068 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +000010069 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +000010070 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010071 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010072 return RCPair(0U, &ARM::tGPRRegClass);
10073 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +000010074 case 'h': // High regs or no regs.
10075 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010076 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +000010077 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010078 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +000010079 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010080 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +000010081 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010082 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +000010083 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010084 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +000010085 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010086 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010087 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010088 case 'x':
10089 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010090 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010091 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010092 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010093 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010094 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010095 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010096 case 't':
10097 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010098 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010099 break;
Evan Chenga8e29892007-01-19 07:51:42 +000010100 }
10101 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010102 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +000010103 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010104
Evan Chenga8e29892007-01-19 07:51:42 +000010105 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10106}
10107
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010108/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10109/// vector. If it is invalid, don't add anything to Ops.
10110void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000010111 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010112 std::vector<SDValue>&Ops,
10113 SelectionDAG &DAG) const {
10114 SDValue Result(0, 0);
10115
Eric Christopher100c8332011-06-02 23:16:42 +000010116 // Currently only support length 1 constraints.
10117 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000010118
Eric Christopher100c8332011-06-02 23:16:42 +000010119 char ConstraintLetter = Constraint[0];
10120 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010121 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +000010122 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010123 case 'I': case 'J': case 'K': case 'L':
10124 case 'M': case 'N': case 'O':
10125 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10126 if (!C)
10127 return;
10128
10129 int64_t CVal64 = C->getSExtValue();
10130 int CVal = (int) CVal64;
10131 // None of these constraints allow values larger than 32 bits. Check
10132 // that the value fits in an int.
10133 if (CVal != CVal64)
10134 return;
10135
Eric Christopher100c8332011-06-02 23:16:42 +000010136 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +000010137 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +000010138 // Constant suitable for movw, must be between 0 and
10139 // 65535.
10140 if (Subtarget->hasV6T2Ops())
10141 if (CVal >= 0 && CVal <= 65535)
10142 break;
10143 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010144 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010145 if (Subtarget->isThumb1Only()) {
10146 // This must be a constant between 0 and 255, for ADD
10147 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010148 if (CVal >= 0 && CVal <= 255)
10149 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010150 } else if (Subtarget->isThumb2()) {
10151 // A constant that can be used as an immediate value in a
10152 // data-processing instruction.
10153 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10154 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010155 } else {
10156 // A constant that can be used as an immediate value in a
10157 // data-processing instruction.
10158 if (ARM_AM::getSOImmVal(CVal) != -1)
10159 break;
10160 }
10161 return;
10162
10163 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010164 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010165 // This must be a constant between -255 and -1, for negated ADD
10166 // immediates. This can be used in GCC with an "n" modifier that
10167 // prints the negated value, for use with SUB instructions. It is
10168 // not useful otherwise but is implemented for compatibility.
10169 if (CVal >= -255 && CVal <= -1)
10170 break;
10171 } else {
10172 // This must be a constant between -4095 and 4095. It is not clear
10173 // what this constraint is intended for. Implemented for
10174 // compatibility with GCC.
10175 if (CVal >= -4095 && CVal <= 4095)
10176 break;
10177 }
10178 return;
10179
10180 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010181 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010182 // A 32-bit value where only one byte has a nonzero value. Exclude
10183 // zero to match GCC. This constraint is used by GCC internally for
10184 // constants that can be loaded with a move/shift combination.
10185 // It is not useful otherwise but is implemented for compatibility.
10186 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10187 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010188 } else if (Subtarget->isThumb2()) {
10189 // A constant whose bitwise inverse can be used as an immediate
10190 // value in a data-processing instruction. This can be used in GCC
10191 // with a "B" modifier that prints the inverted value, for use with
10192 // BIC and MVN instructions. It is not useful otherwise but is
10193 // implemented for compatibility.
10194 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10195 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010196 } else {
10197 // A constant whose bitwise inverse can be used as an immediate
10198 // value in a data-processing instruction. This can be used in GCC
10199 // with a "B" modifier that prints the inverted value, for use with
10200 // BIC and MVN instructions. It is not useful otherwise but is
10201 // implemented for compatibility.
10202 if (ARM_AM::getSOImmVal(~CVal) != -1)
10203 break;
10204 }
10205 return;
10206
10207 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010208 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010209 // This must be a constant between -7 and 7,
10210 // for 3-operand ADD/SUB immediate instructions.
10211 if (CVal >= -7 && CVal < 7)
10212 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010213 } else if (Subtarget->isThumb2()) {
10214 // A constant whose negation can be used as an immediate value in a
10215 // data-processing instruction. This can be used in GCC with an "n"
10216 // modifier that prints the negated value, for use with SUB
10217 // instructions. It is not useful otherwise but is implemented for
10218 // compatibility.
10219 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10220 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010221 } else {
10222 // A constant whose negation can be used as an immediate value in a
10223 // data-processing instruction. This can be used in GCC with an "n"
10224 // modifier that prints the negated value, for use with SUB
10225 // instructions. It is not useful otherwise but is implemented for
10226 // compatibility.
10227 if (ARM_AM::getSOImmVal(-CVal) != -1)
10228 break;
10229 }
10230 return;
10231
10232 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010233 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010234 // This must be a multiple of 4 between 0 and 1020, for
10235 // ADD sp + immediate.
10236 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10237 break;
10238 } else {
10239 // A power of two or a constant between 0 and 32. This is used in
10240 // GCC for the shift amount on shifted register operands, but it is
10241 // useful in general for any shift amounts.
10242 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10243 break;
10244 }
10245 return;
10246
10247 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010248 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010249 // This must be a constant between 0 and 31, for shift amounts.
10250 if (CVal >= 0 && CVal <= 31)
10251 break;
10252 }
10253 return;
10254
10255 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010256 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010257 // This must be a multiple of 4 between -508 and 508, for
10258 // ADD/SUB sp = sp + immediate.
10259 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10260 break;
10261 }
10262 return;
10263 }
10264 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10265 break;
10266 }
10267
10268 if (Result.getNode()) {
10269 Ops.push_back(Result);
10270 return;
10271 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010272 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010273}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010274
10275bool
10276ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10277 // The ARM target isn't yet aware of offsets.
10278 return false;
10279}
Evan Cheng39382422009-10-28 01:44:26 +000010280
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010281bool ARM::isBitFieldInvertedMask(unsigned v) {
10282 if (v == 0xffffffff)
10283 return 0;
10284 // there can be 1's on either or both "outsides", all the "inside"
10285 // bits must be 0's
10286 unsigned int lsb = 0, msb = 31;
10287 while (v & (1 << msb)) --msb;
10288 while (v & (1 << lsb)) ++lsb;
10289 for (unsigned int i = lsb; i <= msb; ++i) {
10290 if (v & (1 << i))
10291 return 0;
10292 }
10293 return 1;
10294}
10295
Evan Cheng39382422009-10-28 01:44:26 +000010296/// isFPImmLegal - Returns true if the target can instruction select the
10297/// specified FP immediate natively. If false, the legalizer will
10298/// materialize the FP immediate as a load from a constant pool.
10299bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10300 if (!Subtarget->hasVFP3())
10301 return false;
10302 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010303 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010304 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010305 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010306 return false;
10307}
Bob Wilson65ffec42010-09-21 17:56:22 +000010308
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010309/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010310/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10311/// specified in the intrinsic calls.
10312bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10313 const CallInst &I,
10314 unsigned Intrinsic) const {
10315 switch (Intrinsic) {
10316 case Intrinsic::arm_neon_vld1:
10317 case Intrinsic::arm_neon_vld2:
10318 case Intrinsic::arm_neon_vld3:
10319 case Intrinsic::arm_neon_vld4:
10320 case Intrinsic::arm_neon_vld2lane:
10321 case Intrinsic::arm_neon_vld3lane:
10322 case Intrinsic::arm_neon_vld4lane: {
10323 Info.opc = ISD::INTRINSIC_W_CHAIN;
10324 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010325 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010326 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10327 Info.ptrVal = I.getArgOperand(0);
10328 Info.offset = 0;
10329 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10330 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10331 Info.vol = false; // volatile loads with NEON intrinsics not supported
10332 Info.readMem = true;
10333 Info.writeMem = false;
10334 return true;
10335 }
10336 case Intrinsic::arm_neon_vst1:
10337 case Intrinsic::arm_neon_vst2:
10338 case Intrinsic::arm_neon_vst3:
10339 case Intrinsic::arm_neon_vst4:
10340 case Intrinsic::arm_neon_vst2lane:
10341 case Intrinsic::arm_neon_vst3lane:
10342 case Intrinsic::arm_neon_vst4lane: {
10343 Info.opc = ISD::INTRINSIC_VOID;
10344 // Conservatively set memVT to the entire set of vectors stored.
10345 unsigned NumElts = 0;
10346 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010347 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010348 if (!ArgTy->isVectorTy())
10349 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010350 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010351 }
10352 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10353 Info.ptrVal = I.getArgOperand(0);
10354 Info.offset = 0;
10355 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10356 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10357 Info.vol = false; // volatile stores with NEON intrinsics not supported
10358 Info.readMem = false;
10359 Info.writeMem = true;
10360 return true;
10361 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010362 case Intrinsic::arm_strexd: {
10363 Info.opc = ISD::INTRINSIC_W_CHAIN;
10364 Info.memVT = MVT::i64;
10365 Info.ptrVal = I.getArgOperand(2);
10366 Info.offset = 0;
10367 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010368 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010369 Info.readMem = false;
10370 Info.writeMem = true;
10371 return true;
10372 }
10373 case Intrinsic::arm_ldrexd: {
10374 Info.opc = ISD::INTRINSIC_W_CHAIN;
10375 Info.memVT = MVT::i64;
10376 Info.ptrVal = I.getArgOperand(0);
10377 Info.offset = 0;
10378 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010379 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010380 Info.readMem = true;
10381 Info.writeMem = false;
10382 return true;
10383 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010384 default:
10385 break;
10386 }
10387
10388 return false;
10389}