blob: 60a07a474faad0b15e051cc4bfae79883641a882 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Dale Johannesen51e28e62010-06-03 21:09:53 +000015#define DEBUG_TYPE "arm-isel"
Craig Topperc1f6f422012-03-17 07:33:42 +000016#include "ARMISelLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000029#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
41#include "llvm/IR/Instruction.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Type.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000046#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000049#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000050#include "llvm/Target/TargetOptions.h"
Evan Chenga8e29892007-01-19 07:51:42 +000051using namespace llvm;
52
Dale Johannesen51e28e62010-06-03 21:09:53 +000053STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000054STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren763a75d2012-06-01 02:44:42 +000055STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesen51e28e62010-06-03 21:09:53 +000056
Bob Wilson703af3a2010-08-13 22:43:33 +000057// This option should go away when tail calls fully work.
58static cl::opt<bool>
59EnableARMTailCalls("arm-tail-calls", cl::Hidden,
60 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
61 cl::init(false));
62
Eric Christopher836c6242010-12-15 23:47:29 +000063cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000064EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000065 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000066 cl::init(false));
67
Evan Cheng46df4eb2010-06-16 07:35:02 +000068static cl::opt<bool>
69ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
71 cl::init(true));
72
Benjamin Kramer0861f572011-11-26 23:01:57 +000073namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000074 class ARMCCState : public CCState {
75 public:
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
77 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
78 LLVMContext &C, ParmContext PC)
79 : CCState(CC, isVarArg, MF, TM, locs, C) {
80 assert(((PC == Call) || (PC == Prologue)) &&
81 "ARMCCState users must specify whether their context is call"
82 "or prologue generation.");
83 CallOrPrologue = PC;
84 }
85 };
86}
87
Stuart Hastingsc7315872011-04-20 16:47:52 +000088// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000089static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000090 ARM::R0, ARM::R1, ARM::R2, ARM::R3
91};
92
Craig Topper0faf46c2012-08-12 03:16:37 +000093void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
94 MVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +000096 setOperationAction(ISD::LOAD, VT, Promote);
97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson5bafff32009-06-22 23:27:02 +000098
Craig Topper0faf46c2012-08-12 03:16:37 +000099 setOperationAction(ISD::STORE, VT, Promote);
100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
102
Craig Topper0faf46c2012-08-12 03:16:37 +0000103 MVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper0faf46c2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SETCC, VT, Custom);
106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000108 if (ElemTy == MVT::i32) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
110 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
111 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
112 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000113 } else {
Craig Topper0faf46c2012-08-12 03:16:37 +0000114 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
115 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
116 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
117 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000118 }
Craig Topper0faf46c2012-08-12 03:16:37 +0000119 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
120 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
121 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
123 setOperationAction(ISD::SELECT, VT, Expand);
124 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach4346fa92012-10-12 22:59:21 +0000125 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper0faf46c2012-08-12 03:16:37 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 if (VT.isInteger()) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000128 setOperationAction(ISD::SHL, VT, Custom);
129 setOperationAction(ISD::SRA, VT, Custom);
130 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 }
132
133 // Promote all bit-wise operations.
134 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000135 setOperationAction(ISD::AND, VT, Promote);
136 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
137 setOperationAction(ISD::OR, VT, Promote);
138 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
139 setOperationAction(ISD::XOR, VT, Promote);
140 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000141 }
Bob Wilson16330762009-09-16 00:17:28 +0000142
143 // Neon does not support vector divide/remainder operations.
Craig Topper0faf46c2012-08-12 03:16:37 +0000144 setOperationAction(ISD::SDIV, VT, Expand);
145 setOperationAction(ISD::UDIV, VT, Expand);
146 setOperationAction(ISD::FDIV, VT, Expand);
147 setOperationAction(ISD::SREM, VT, Expand);
148 setOperationAction(ISD::UREM, VT, Expand);
149 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000150}
151
Craig Topper0faf46c2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000155}
156
Craig Topper0faf46c2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000158 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000160}
161
Chris Lattnerf0144122009-07-28 03:13:23 +0000162static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
163 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000164 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000165
Chris Lattner80ec2792009-08-02 00:34:36 +0000166 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000167}
168
Evan Chenga8e29892007-01-19 07:51:42 +0000169ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000170 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000171 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000172 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000173 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000174
Duncan Sands28b77e92011-09-06 19:07:46 +0000175 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
176
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000178 // Uses VFP for Thumb libfuncs if available.
179 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
180 // Single-precision floating-point arithmetic.
181 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
182 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
183 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
184 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000185
Evan Chengb1df8f22007-04-27 08:15:43 +0000186 // Double-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
188 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
189 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
190 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000191
Evan Chengb1df8f22007-04-27 08:15:43 +0000192 // Single-precision comparisons.
193 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
194 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
195 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
196 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
197 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
198 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
199 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
200 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000201
Evan Chengb1df8f22007-04-27 08:15:43 +0000202 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000210
Evan Chengb1df8f22007-04-27 08:15:43 +0000211 // Double-precision comparisons.
212 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
213 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
214 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
215 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
216 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
217 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
218 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
219 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000220
Evan Chengb1df8f22007-04-27 08:15:43 +0000221 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000229
Evan Chengb1df8f22007-04-27 08:15:43 +0000230 // Floating-point to integer conversions.
231 // i64 conversions are done via library routines even when generating VFP
232 // instructions, so use the same ones.
233 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
234 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
235 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
236 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000237
Evan Chengb1df8f22007-04-27 08:15:43 +0000238 // Conversions between floating types.
239 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
240 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
241
242 // Integer to floating-point conversions.
243 // i64 conversions are done via library routines even when generating VFP
244 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000245 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
246 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000247 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
248 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
249 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
250 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
251 }
Evan Chenga8e29892007-01-19 07:51:42 +0000252 }
253
Bob Wilson2f954612009-05-22 17:38:41 +0000254 // These libcalls are not available in 32-bit.
255 setLibcallName(RTLIB::SHL_I128, 0);
256 setLibcallName(RTLIB::SRL_I128, 0);
257 setLibcallName(RTLIB::SRA_I128, 0);
258
Evan Cheng07043272012-02-21 20:46:00 +0000259 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000260 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000261 // RTABI chapter 4.1.2, Table 2
262 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
263 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
264 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
265 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
266 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
268 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
269 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
270
271 // Double-precision floating-point comparison helper functions
272 // RTABI chapter 4.1.2, Table 3
273 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
274 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
275 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
276 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
277 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
278 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
279 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
280 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
281 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
282 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
284 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
286 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
287 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
288 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
289 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
297
298 // Single-precision floating-point arithmetic helper functions
299 // RTABI chapter 4.1.2, Table 4
300 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
301 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
302 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
303 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
304 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
306 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
307 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
308
309 // Single-precision floating-point comparison helper functions
310 // RTABI chapter 4.1.2, Table 5
311 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
312 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
313 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
314 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
315 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
316 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
317 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
318 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
319 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
320 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
322 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
324 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
325 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
326 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
327 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
335
336 // Floating-point to integer conversions.
337 // RTABI chapter 4.1.2, Table 6
338 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
339 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
340 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
341 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
342 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
343 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
345 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
346 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
354
355 // Conversions between floating types.
356 // RTABI chapter 4.1.2, Table 7
357 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
358 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
359 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000360 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000361
362 // Integer to floating-point conversions.
363 // RTABI chapter 4.1.2, Table 8
364 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
365 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
366 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
367 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
368 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
369 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
370 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
371 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
372 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
380
381 // Long long helper functions
382 // RTABI chapter 4.2, Table 9
383 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000384 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
385 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
386 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
387 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
393
394 // Integer division functions
395 // RTABI chapter 4.3.1
396 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
397 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
398 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000399 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000400 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
401 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
402 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000403 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000404 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
406 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000407 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000408 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000410 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000411 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000412
413 // Memory operations
414 // RTABI chapter 4.3.4
415 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
416 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
417 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000418 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
420 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000421 }
422
Bob Wilson2fef4572011-10-07 16:59:21 +0000423 // Use divmod compiler-rt calls for iOS 5.0 and later.
424 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
425 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
426 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
427 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
428 }
429
David Goodwinf1daf7d2009-07-08 23:10:31 +0000430 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000432 else
Craig Topper420761a2012-04-20 07:30:17 +0000433 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000434 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
435 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000436 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000437 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000438 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000439
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000441 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000442
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
447 setTruncStoreAction((MVT::SimpleValueType)VT,
448 (MVT::SimpleValueType)InnerVT, Expand);
449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
452 }
453
Lang Hames45b5f882012-03-15 18:49:02 +0000454 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
455
Bob Wilson5bafff32009-06-22 23:27:02 +0000456 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 addDRTypeForNEON(MVT::v2f32);
458 addDRTypeForNEON(MVT::v8i8);
459 addDRTypeForNEON(MVT::v4i16);
460 addDRTypeForNEON(MVT::v2i32);
461 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000462
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 addQRTypeForNEON(MVT::v4f32);
464 addQRTypeForNEON(MVT::v2f64);
465 addQRTypeForNEON(MVT::v16i8);
466 addQRTypeForNEON(MVT::v8i16);
467 addQRTypeForNEON(MVT::v4i32);
468 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000469
Bob Wilson74dc72e2009-09-15 23:55:57 +0000470 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
471 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000472 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
473 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000474 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
475 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
476 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000477 // FIXME: Code duplication: FDIV and FREM are expanded always, see
478 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000479 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
480 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000481 // FIXME: Create unittest.
482 // In another words, find a way when "copysign" appears in DAG with vector
483 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000484 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000485 // FIXME: Code duplication: SETCC has custom operation action, see
486 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000487 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000488 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000489 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
490 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
491 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
492 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
493 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
494 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
495 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
498 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
499 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
500 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000501 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000502 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
503 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
504 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
505 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
506 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000507
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000508 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
509 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
510 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
512 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
515 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
517 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000518 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
519 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
520 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
521 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000522 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000523
Bob Wilson642b3292009-09-16 00:32:15 +0000524 // Neon does not support some operations on v1i64 and v2i64 types.
525 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000526 // Custom handling for some quad-vector types to detect VMULL.
527 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
528 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
529 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000530 // Custom handling for some vector types to avoid expensive expansions
531 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
533 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
534 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000535 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
536 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000537 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000538 // a destination type that is wider than the source, and nor does
539 // it have a FP_TO_[SU]INT instruction with a narrower destination than
540 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000541 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
542 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000543 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
544 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000545
Eli Friedman846ce8e2012-11-15 22:44:27 +0000546 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman43147af2012-11-17 01:52:46 +0000547 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedman846ce8e2012-11-15 22:44:27 +0000548
Evan Chengc8e70452012-12-04 22:41:50 +0000549 // NEON does not have single instruction CTPOP for vectors with element
550 // types wider than 8-bits. However, custom lowering can leverage the
551 // v8i8/v16i8 vcnt instruction.
552 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
553 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
554 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
555 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
556
Jim Grosbachb302a4e2013-02-27 21:31:12 +0000557 // NEON only has FMA instructions as of VFP4.
558 if (!Subtarget->hasVFP4()) {
559 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
560 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
561 }
562
Bob Wilson1c3ef902011-02-07 17:43:21 +0000563 setTargetDAGCombine(ISD::INTRINSIC_VOID);
564 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000565 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
566 setTargetDAGCombine(ISD::SHL);
567 setTargetDAGCombine(ISD::SRL);
568 setTargetDAGCombine(ISD::SRA);
569 setTargetDAGCombine(ISD::SIGN_EXTEND);
570 setTargetDAGCombine(ISD::ZERO_EXTEND);
571 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000572 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000573 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000574 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000575 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
576 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000577 setTargetDAGCombine(ISD::FP_TO_SINT);
578 setTargetDAGCombine(ISD::FP_TO_UINT);
579 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000580
James Molloy873fd5f2012-02-20 09:24:05 +0000581 // It is legal to extload from v4i8 to v4i16 or v4i32.
582 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
583 MVT::v4i16, MVT::v2i16,
584 MVT::v2i32};
585 for (unsigned i = 0; i < 6; ++i) {
586 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
587 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
588 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
589 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000590 }
591
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000592 // ARM and Thumb2 support UMLAL/SMLAL.
593 if (!Subtarget->isThumb1Only())
594 setTargetDAGCombine(ISD::ADDC);
595
596
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000597 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000598
599 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000601
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000602 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000604
Evan Chenga8e29892007-01-19 07:51:42 +0000605 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000606 if (!Subtarget->isThumb1Only()) {
607 for (unsigned im = (unsigned)ISD::PRE_INC;
608 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000609 setIndexedLoadAction(im, MVT::i1, Legal);
610 setIndexedLoadAction(im, MVT::i8, Legal);
611 setIndexedLoadAction(im, MVT::i16, Legal);
612 setIndexedLoadAction(im, MVT::i32, Legal);
613 setIndexedStoreAction(im, MVT::i1, Legal);
614 setIndexedStoreAction(im, MVT::i8, Legal);
615 setIndexedStoreAction(im, MVT::i16, Legal);
616 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000617 }
Evan Chenga8e29892007-01-19 07:51:42 +0000618 }
619
620 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000621 setOperationAction(ISD::MUL, MVT::i64, Expand);
622 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000623 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
625 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000626 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000627 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
628 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000629 setOperationAction(ISD::MULHS, MVT::i32, Expand);
630
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000631 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000632 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000633 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::SRL, MVT::i64, Custom);
635 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000636
Evan Cheng342e3162011-08-30 01:34:54 +0000637 if (!Subtarget->isThumb1Only()) {
638 // FIXME: We should do this for Thumb1 as well.
639 setOperationAction(ISD::ADDC, MVT::i32, Custom);
640 setOperationAction(ISD::ADDE, MVT::i32, Custom);
641 setOperationAction(ISD::SUBC, MVT::i32, Custom);
642 setOperationAction(ISD::SUBE, MVT::i32, Custom);
643 }
644
Evan Chenga8e29892007-01-19 07:51:42 +0000645 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000647 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000649 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000651
Chandler Carruth63974b22011-12-13 01:56:10 +0000652 // These just redirect to CTTZ and CTLZ on ARM.
653 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
654 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
655
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000656 // Only ARMv6 has BSWAP.
657 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000659
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000660 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
661 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
662 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000663 setOperationAction(ISD::SDIV, MVT::i32, Expand);
664 setOperationAction(ISD::UDIV, MVT::i32, Expand);
665 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::SREM, MVT::i32, Expand);
667 setOperationAction(ISD::UREM, MVT::i32, Expand);
668 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
669 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000670
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
672 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
673 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
674 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000675 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000676
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000677 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000678
Evan Chenga8e29892007-01-19 07:51:42 +0000679 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::VASTART, MVT::Other, Custom);
681 setOperationAction(ISD::VAARG, MVT::Other, Expand);
682 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
683 setOperationAction(ISD::VAEND, MVT::Other, Expand);
684 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
685 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000686
687 if (!Subtarget->isTargetDarwin()) {
688 // Non-Darwin platforms may return values in these registers via the
689 // personality function.
690 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
691 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
692 setExceptionPointerRegister(ARM::R0);
693 setExceptionSelectorRegister(ARM::R1);
694 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000695
Evan Cheng3a1588a2010-04-15 22:20:34 +0000696 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000697 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
698 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000699 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000700 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000701 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 // membarrier needs custom lowering; the rest are legal and handled
703 // normally.
704 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000705 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000706 // Custom lowering for 64-bit ops
707 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
708 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
709 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
710 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
711 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
Silviu Baranga35b3df62012-11-29 14:41:25 +0000712 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
713 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
714 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
715 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
716 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000717 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000718 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
719 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000720 } else {
721 // Set them all for expansion, which will force libcalls.
722 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000723 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000724 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000725 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000726 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000727 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000728 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000729 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000730 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000731 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000732 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000733 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000734 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000735 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000736 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
737 // Unordered/Monotonic case.
738 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
739 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000740 // Since the libcalls include locking, fold in the fences
741 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000742 }
Evan Chenga8e29892007-01-19 07:51:42 +0000743
Evan Cheng416941d2010-11-04 05:19:35 +0000744 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000745
Eli Friedmana2c6f452010-06-26 04:36:50 +0000746 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
747 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
749 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000750 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000752
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000753 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
754 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000755 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000756 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000757 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000758 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
759 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000760
761 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000763 if (Subtarget->isTargetDarwin()) {
764 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
765 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000766 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000767 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::SETCC, MVT::i32, Expand);
770 setOperationAction(ISD::SETCC, MVT::f32, Expand);
771 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000772 setOperationAction(ISD::SELECT, MVT::i32, Custom);
773 setOperationAction(ISD::SELECT, MVT::f32, Custom);
774 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
776 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
777 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000778
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
780 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
781 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
782 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
783 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000784
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000785 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::FSIN, MVT::f64, Expand);
787 setOperationAction(ISD::FSIN, MVT::f32, Expand);
788 setOperationAction(ISD::FCOS, MVT::f32, Expand);
789 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000790 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
791 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::FREM, MVT::f64, Expand);
793 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000794 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
795 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
797 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000798 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 setOperationAction(ISD::FPOW, MVT::f64, Expand);
800 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000801
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000802 if (!Subtarget->hasVFP4()) {
803 setOperationAction(ISD::FMA, MVT::f64, Expand);
804 setOperationAction(ISD::FMA, MVT::f32, Expand);
805 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000806
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000807 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000808 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000809 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
810 if (Subtarget->hasVFP2()) {
811 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
812 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
813 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
814 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
815 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000816 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000817 if (!Subtarget->hasFP16()) {
818 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
819 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000820 }
Evan Cheng110cf482008-04-01 01:50:16 +0000821 }
Evan Chenga8e29892007-01-19 07:51:42 +0000822
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000823 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000824 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000825 setTargetDAGCombine(ISD::ADD);
826 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000827 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000828 setTargetDAGCombine(ISD::AND);
829 setTargetDAGCombine(ISD::OR);
830 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000831
Evan Cheng5fb468a2012-02-23 02:58:19 +0000832 if (Subtarget->hasV6Ops())
833 setTargetDAGCombine(ISD::SRL);
834
Evan Chenga8e29892007-01-19 07:51:42 +0000835 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000836
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000837 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
838 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000839 setSchedulingPreference(Sched::RegPressure);
840 else
841 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000842
Evan Cheng05219282011-01-06 06:52:41 +0000843 //// temporary - rewrite interface to use type
Jim Grosbach3450f802013-02-20 21:13:59 +0000844 MaxStoresPerMemset = 8;
845 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
846 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
847 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
848 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
849 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengf6799392010-06-26 01:52:05 +0000850
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000851 // On ARM arguments smaller than 4 bytes are extended, so all arguments
852 // are at least 4 bytes aligned.
853 setMinStackArgumentAlignment(4);
854
Jim Grosbach3450f802013-02-20 21:13:59 +0000855 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000856
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000857 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach3450f802013-02-20 21:13:59 +0000858 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000859
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000860 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000861}
862
Andrew Trick32cec0a2011-01-19 02:35:27 +0000863// FIXME: It might make sense to define the representative register class as the
864// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
865// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
866// SPR's representative would be DPR_VFP2. This should work well if register
867// pressure tracking were modified such that a register use would increment the
868// pressure of the register class's representative and all of it's super
869// classes' representatives transitively. We have not implemented this because
870// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000871// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000872// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +0000874ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Cheng4f6b4672010-07-21 06:09:07 +0000875 const TargetRegisterClass *RRC = 0;
876 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +0000877 switch (VT.SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000878 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000879 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000880 // Use DPR as representative register class for all floating point
881 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
882 // the cost is 1 for both f32 and f64.
883 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000884 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000885 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000886 // When NEON is used for SP, only half of the register file is available
887 // because operations that define both SP and DP results will be constrained
888 // to the VFP2 class (D0-D15). We currently model this constraint prior to
889 // coalescing by double-counting the SP regs. See the FIXME above.
890 if (Subtarget->useNEONForSinglePrecisionFP())
891 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000892 break;
893 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
894 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000895 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000896 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000897 break;
898 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000899 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000900 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000901 break;
902 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000903 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000904 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000905 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000906 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000907 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000908}
909
Evan Chenga8e29892007-01-19 07:51:42 +0000910const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
911 switch (Opcode) {
912 default: return 0;
913 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000914 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000915 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000916 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
917 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000918 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000919 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
920 case ARMISD::tCALL: return "ARMISD::tCALL";
921 case ARMISD::BRCOND: return "ARMISD::BRCOND";
922 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000923 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000924 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
925 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
926 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000927 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000928 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000929 case ARMISD::CMPFP: return "ARMISD::CMPFP";
930 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000931 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000932 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000933
Evan Chenga8e29892007-01-19 07:51:42 +0000934 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000935
Jim Grosbach3482c802010-01-18 19:58:49 +0000936 case ARMISD::RBIT: return "ARMISD::RBIT";
937
Bob Wilson76a312b2010-03-19 22:51:32 +0000938 case ARMISD::FTOSI: return "ARMISD::FTOSI";
939 case ARMISD::FTOUI: return "ARMISD::FTOUI";
940 case ARMISD::SITOF: return "ARMISD::SITOF";
941 case ARMISD::UITOF: return "ARMISD::UITOF";
942
Evan Chenga8e29892007-01-19 07:51:42 +0000943 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
944 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
945 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000946
Evan Cheng342e3162011-08-30 01:34:54 +0000947 case ARMISD::ADDC: return "ARMISD::ADDC";
948 case ARMISD::ADDE: return "ARMISD::ADDE";
949 case ARMISD::SUBC: return "ARMISD::SUBC";
950 case ARMISD::SUBE: return "ARMISD::SUBE";
951
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000952 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
953 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000954
Evan Chengc5942082009-10-28 06:55:03 +0000955 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
956 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
957
Dale Johannesen51e28e62010-06-03 21:09:53 +0000958 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000959
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000960 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000961
Evan Cheng86198642009-08-07 00:34:42 +0000962 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
963
Jim Grosbach3728e962009-12-10 00:11:09 +0000964 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000965 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000966
Evan Chengdfed19f2010-11-03 06:34:55 +0000967 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
968
Bob Wilson5bafff32009-06-22 23:27:02 +0000969 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000970 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000971 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000972 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
973 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000974 case ARMISD::VCGEU: return "ARMISD::VCGEU";
975 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000976 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
977 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000978 case ARMISD::VCGTU: return "ARMISD::VCGTU";
979 case ARMISD::VTST: return "ARMISD::VTST";
980
981 case ARMISD::VSHL: return "ARMISD::VSHL";
982 case ARMISD::VSHRs: return "ARMISD::VSHRs";
983 case ARMISD::VSHRu: return "ARMISD::VSHRu";
984 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
985 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
986 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
987 case ARMISD::VSHRN: return "ARMISD::VSHRN";
988 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
989 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
990 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
991 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
992 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
993 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
994 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
995 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
996 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
997 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
998 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
999 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1000 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1001 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +00001002 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +00001003 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +00001004 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +00001005 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +00001006 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +00001007 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +00001008 case ARMISD::VREV64: return "ARMISD::VREV64";
1009 case ARMISD::VREV32: return "ARMISD::VREV32";
1010 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001011 case ARMISD::VZIP: return "ARMISD::VZIP";
1012 case ARMISD::VUZP: return "ARMISD::VUZP";
1013 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +00001014 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1015 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +00001016 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1017 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00001018 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1019 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001020 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001021 case ARMISD::FMAX: return "ARMISD::FMAX";
1022 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001023 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001024 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1025 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001026 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001027 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1028 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1029 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001030 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1031 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1032 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1033 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1034 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1035 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1036 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1037 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1038 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1039 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1040 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1041 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1042 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1043 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1044 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1045 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1046 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001047 }
1048}
1049
Duncan Sands28b77e92011-09-06 19:07:46 +00001050EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1051 if (!VT.isVector()) return getPointerTy();
1052 return VT.changeVectorElementTypeToInteger();
1053}
1054
Evan Cheng06b666c2010-05-15 02:18:07 +00001055/// getRegClassFor - Return the register class that should be used for the
1056/// specified value type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001057const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001058 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1059 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1060 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001061 if (Subtarget->hasNEON()) {
1062 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001063 return &ARM::QQPRRegClass;
1064 if (VT == MVT::v8i64)
1065 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001066 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001067 return TargetLowering::getRegClassFor(VT);
1068}
1069
Eric Christopherab695882010-07-21 22:26:11 +00001070// Create a fast isel object.
1071FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001072ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1073 const TargetLibraryInfo *libInfo) const {
1074 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001075}
1076
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001077/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1078/// be used for loads / stores from the global.
1079unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1080 return (Subtarget->isThumb1Only() ? 127 : 4095);
1081}
1082
Evan Cheng1cc39842010-05-20 23:26:43 +00001083Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001084 unsigned NumVals = N->getNumValues();
1085 if (!NumVals)
1086 return Sched::RegPressure;
1087
1088 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001089 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001090 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001091 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001092 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001093 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001094 }
Evan Chengc10f5432010-05-28 23:25:23 +00001095
1096 if (!N->isMachineOpcode())
1097 return Sched::RegPressure;
1098
1099 // Load are scheduled for latency even if there instruction itinerary
1100 // is not available.
1101 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001102 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001103
Evan Chenge837dea2011-06-28 19:10:37 +00001104 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001105 return Sched::RegPressure;
1106 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001107 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001108 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001109
Evan Cheng1cc39842010-05-20 23:26:43 +00001110 return Sched::RegPressure;
1111}
1112
Evan Chenga8e29892007-01-19 07:51:42 +00001113//===----------------------------------------------------------------------===//
1114// Lowering Code
1115//===----------------------------------------------------------------------===//
1116
Evan Chenga8e29892007-01-19 07:51:42 +00001117/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1118static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1119 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001120 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001121 case ISD::SETNE: return ARMCC::NE;
1122 case ISD::SETEQ: return ARMCC::EQ;
1123 case ISD::SETGT: return ARMCC::GT;
1124 case ISD::SETGE: return ARMCC::GE;
1125 case ISD::SETLT: return ARMCC::LT;
1126 case ISD::SETLE: return ARMCC::LE;
1127 case ISD::SETUGT: return ARMCC::HI;
1128 case ISD::SETUGE: return ARMCC::HS;
1129 case ISD::SETULT: return ARMCC::LO;
1130 case ISD::SETULE: return ARMCC::LS;
1131 }
1132}
1133
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001134/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1135static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001136 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001137 CondCode2 = ARMCC::AL;
1138 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001139 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001140 case ISD::SETEQ:
1141 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1142 case ISD::SETGT:
1143 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1144 case ISD::SETGE:
1145 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1146 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001147 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001148 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1149 case ISD::SETO: CondCode = ARMCC::VC; break;
1150 case ISD::SETUO: CondCode = ARMCC::VS; break;
1151 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1152 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1153 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1154 case ISD::SETLT:
1155 case ISD::SETULT: CondCode = ARMCC::LT; break;
1156 case ISD::SETLE:
1157 case ISD::SETULE: CondCode = ARMCC::LE; break;
1158 case ISD::SETNE:
1159 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1160 }
Evan Chenga8e29892007-01-19 07:51:42 +00001161}
1162
Bob Wilson1f595bb2009-04-17 19:07:39 +00001163//===----------------------------------------------------------------------===//
1164// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001165//===----------------------------------------------------------------------===//
1166
1167#include "ARMGenCallingConv.inc"
1168
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1170/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001171CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001172 bool Return,
1173 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001174 switch (CC) {
1175 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001176 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001177 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001178 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001179 if (!Subtarget->isAAPCS_ABI())
1180 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1181 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1182 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1183 }
1184 // Fallthrough
1185 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001186 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001187 if (!Subtarget->isAAPCS_ABI())
1188 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1189 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001190 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1191 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001192 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1193 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1194 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001195 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001196 if (!isVarArg)
1197 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1198 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001199 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001200 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001201 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001202 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001203 case CallingConv::GHC:
1204 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001205 }
1206}
1207
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208/// LowerCallResult - Lower the result values of a call into the
1209/// appropriate copies out of appropriate physical registers.
1210SDValue
1211ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001212 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 const SmallVectorImpl<ISD::InputArg> &Ins,
1214 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001215 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216
Bob Wilson1f595bb2009-04-17 19:07:39 +00001217 // Assign locations to each value returned by this call.
1218 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001219 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1220 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001222 CCAssignFnForNode(CallConv, /* Return*/ true,
1223 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224
1225 // Copy all of the result registers out of their specified physreg.
1226 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1227 CCValAssign VA = RVLocs[i];
1228
Bob Wilson80915242009-04-25 00:33:20 +00001229 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001231 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001232 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001233 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001234 Chain = Lo.getValue(1);
1235 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001236 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001238 InFlag);
1239 Chain = Hi.getValue(1);
1240 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001241 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001242
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 if (VA.getLocVT() == MVT::v2f64) {
1244 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1245 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1246 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001247
1248 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001250 Chain = Lo.getValue(1);
1251 InFlag = Lo.getValue(2);
1252 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001254 Chain = Hi.getValue(1);
1255 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001256 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1258 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001259 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001260 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001261 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1262 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001263 Chain = Val.getValue(1);
1264 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 }
Bob Wilson80915242009-04-25 00:33:20 +00001266
1267 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001268 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001269 case CCValAssign::Full: break;
1270 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001271 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001272 break;
1273 }
1274
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276 }
1277
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001279}
1280
Bob Wilsondee46d72009-04-17 20:35:10 +00001281/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1284 SDValue StackPtr, SDValue Arg,
1285 DebugLoc dl, SelectionDAG &DAG,
1286 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001287 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001288 unsigned LocMemOffset = VA.getLocMemOffset();
1289 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1290 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001291 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001292 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001293 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001294}
1295
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001297 SDValue Chain, SDValue &Arg,
1298 RegsToPassVector &RegsToPass,
1299 CCValAssign &VA, CCValAssign &NextVA,
1300 SDValue &StackPtr,
1301 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001302 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001303
Jim Grosbache5165492009-11-09 00:11:35 +00001304 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001305 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1307
1308 if (NextVA.isRegLoc())
1309 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1310 else {
1311 assert(NextVA.isMemLoc());
1312 if (StackPtr.getNode() == 0)
1313 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1316 dl, DAG, NextVA,
1317 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001318 }
1319}
1320
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001322/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1323/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001324SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001325ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001326 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001327 SelectionDAG &DAG = CLI.DAG;
1328 DebugLoc &dl = CLI.DL;
1329 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1330 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1331 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1332 SDValue Chain = CLI.Chain;
1333 SDValue Callee = CLI.Callee;
1334 bool &isTailCall = CLI.IsTailCall;
1335 CallingConv::ID CallConv = CLI.CallConv;
1336 bool doesNotRet = CLI.DoesNotReturn;
1337 bool isVarArg = CLI.IsVarArg;
1338
Dale Johannesen51e28e62010-06-03 21:09:53 +00001339 MachineFunction &MF = DAG.getMachineFunction();
1340 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1341 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001342 // Disable tail calls if they're not supported.
1343 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001344 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001345 if (isTailCall) {
1346 // Check if it's really possible to do a tail call.
1347 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1348 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001349 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001350 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1351 // detected sibcalls.
1352 if (isTailCall) {
1353 ++NumTailCalls;
1354 IsSibCall = true;
1355 }
1356 }
Evan Chenga8e29892007-01-19 07:51:42 +00001357
Bob Wilson1f595bb2009-04-17 19:07:39 +00001358 // Analyze operands of the call, assigning locations to each operand.
1359 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001360 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1361 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001363 CCAssignFnForNode(CallConv, /* Return*/ false,
1364 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001365
Bob Wilson1f595bb2009-04-17 19:07:39 +00001366 // Get a count of how many bytes are to be pushed on the stack.
1367 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001368
Dale Johannesen51e28e62010-06-03 21:09:53 +00001369 // For tail calls, memory operands are available in our caller's stack.
1370 if (IsSibCall)
1371 NumBytes = 0;
1372
Evan Chenga8e29892007-01-19 07:51:42 +00001373 // Adjust the stack pointer for the new arguments...
1374 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001375 if (!IsSibCall)
1376 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001377
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001378 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001379
Bob Wilson5bafff32009-06-22 23:27:02 +00001380 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001381 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001382
Bob Wilson1f595bb2009-04-17 19:07:39 +00001383 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001384 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001385 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1386 i != e;
1387 ++i, ++realArgIdx) {
1388 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001389 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001391 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001392
Bob Wilson1f595bb2009-04-17 19:07:39 +00001393 // Promote the value if needed.
1394 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001395 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001396 case CCValAssign::Full: break;
1397 case CCValAssign::SExt:
1398 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1399 break;
1400 case CCValAssign::ZExt:
1401 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1402 break;
1403 case CCValAssign::AExt:
1404 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1405 break;
1406 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001408 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001409 }
1410
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001411 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001412 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001413 if (VA.getLocVT() == MVT::v2f64) {
1414 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1415 DAG.getConstant(0, MVT::i32));
1416 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1417 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001418
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001420 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1421
1422 VA = ArgLocs[++i]; // skip ahead to next loc
1423 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001424 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001425 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1426 } else {
1427 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001428
Dan Gohman98ca4f22009-08-05 01:29:28 +00001429 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1430 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001431 }
1432 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001434 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001435 }
1436 } else if (VA.isRegLoc()) {
1437 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001438 } else if (isByVal) {
1439 assert(VA.isMemLoc());
1440 unsigned offset = 0;
1441
1442 // True if this byval aggregate will be split between registers
1443 // and memory.
1444 if (CCInfo.isFirstByValRegValid()) {
1445 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1446 unsigned int i, j;
1447 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1448 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1449 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1450 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1451 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001452 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001453 MemOpChains.push_back(Load.getValue(1));
1454 RegsToPass.push_back(std::make_pair(j, Load));
1455 }
1456 offset = ARM::R4 - CCInfo.getFirstByValReg();
1457 CCInfo.clearFirstByValReg();
1458 }
1459
Manman Ren763a75d2012-06-01 02:44:42 +00001460 if (Flags.getByValSize() - 4*offset > 0) {
1461 unsigned LocMemOffset = VA.getLocMemOffset();
1462 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1463 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1464 StkPtrOff);
1465 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1466 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1467 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1468 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001469 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001470
Manman Ren763a75d2012-06-01 02:44:42 +00001471 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001472 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001473 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1474 Ops, array_lengthof(Ops)));
1475 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001476 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001477 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1480 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001481 }
Evan Chenga8e29892007-01-19 07:51:42 +00001482 }
1483
1484 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001486 &MemOpChains[0], MemOpChains.size());
1487
1488 // Build a sequence of copy-to-reg nodes chained together with token chain
1489 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001490 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001491 // Tail call byval lowering might overwrite argument registers so in case of
1492 // tail call optimization the copies to registers are lowered later.
1493 if (!isTailCall)
1494 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1495 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1496 RegsToPass[i].second, InFlag);
1497 InFlag = Chain.getValue(1);
1498 }
Evan Chenga8e29892007-01-19 07:51:42 +00001499
Dale Johannesen51e28e62010-06-03 21:09:53 +00001500 // For tail calls lower the arguments to the 'real' stack slot.
1501 if (isTailCall) {
1502 // Force all the incoming stack arguments to be loaded from the stack
1503 // before any new outgoing arguments are stored to the stack, because the
1504 // outgoing stack slots may alias the incoming argument stack slots, and
1505 // the alias isn't otherwise explicit. This is slightly more conservative
1506 // than necessary, because it means that each store effectively depends
1507 // on every argument instead of just those arguments it would clobber.
1508
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001509 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001510 InFlag = SDValue();
1511 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1512 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1513 RegsToPass[i].second, InFlag);
1514 InFlag = Chain.getValue(1);
1515 }
1516 InFlag =SDValue();
1517 }
1518
Bill Wendling056292f2008-09-16 21:48:12 +00001519 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1520 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1521 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001522 bool isDirect = false;
1523 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001524 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001525 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001526
1527 if (EnableARMLongCalls) {
1528 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1529 && "long-calls with non-static relocation model!");
1530 // Handle a global address or an external symbol. If it's not one of
1531 // those, the target's already in a register, so we don't need to do
1532 // anything extra.
1533 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001534 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001535 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001536 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001537 ARMConstantPoolValue *CPV =
1538 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1539
Jim Grosbache7b52522010-04-14 22:28:31 +00001540 // Get the address of the callee into a register
1541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1543 Callee = DAG.getLoad(getPointerTy(), dl,
1544 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001545 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001546 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001547 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1548 const char *Sym = S->getSymbol();
1549
1550 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001551 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001552 ARMConstantPoolValue *CPV =
1553 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1554 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001555 // Get the address of the callee into a register
1556 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1557 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1558 Callee = DAG.getLoad(getPointerTy(), dl,
1559 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001560 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001561 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001562 }
1563 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001564 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001565 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001566 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001567 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001568 getTargetMachine().getRelocationModel() != Reloc::Static;
1569 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001570 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001571 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001572 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001573 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001574 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001575 ARMConstantPoolValue *CPV =
1576 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001577 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001579 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001580 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001581 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001582 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001583 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001584 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001585 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001586 } else {
1587 // On ELF targets for PIC code, direct calls should go through the PLT
1588 unsigned OpFlags = 0;
1589 if (Subtarget->isTargetELF() &&
1590 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1591 OpFlags = ARMII::MO_PLT;
1592 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1593 }
Bill Wendling056292f2008-09-16 21:48:12 +00001594 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001595 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001596 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001597 getTargetMachine().getRelocationModel() != Reloc::Static;
1598 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001599 // tBX takes a register source operand.
1600 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001601 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001602 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001603 ARMConstantPoolValue *CPV =
1604 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1605 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001606 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001607 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001608 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001609 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001610 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001611 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001612 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001613 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001614 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001615 } else {
1616 unsigned OpFlags = 0;
1617 // On ELF targets for PIC code, direct calls should go through the PLT
1618 if (Subtarget->isTargetELF() &&
1619 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1620 OpFlags = ARMII::MO_PLT;
1621 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1622 }
Evan Chenga8e29892007-01-19 07:51:42 +00001623 }
1624
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001625 // FIXME: handle tail calls differently.
1626 unsigned CallOpc;
Bill Wendling831737d2012-12-30 10:32:01 +00001627 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1628 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001629 if (Subtarget->isThumb()) {
1630 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001631 CallOpc = ARMISD::CALL_NOLINK;
1632 else
1633 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1634 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001635 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001636 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001637 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001638 // Emit regular call when code size is the priority
1639 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001640 // "mov lr, pc; b _foo" to avoid confusing the RSP
1641 CallOpc = ARMISD::CALL_NOLINK;
1642 else
1643 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001644 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001645
Dan Gohman475871a2008-07-27 21:46:04 +00001646 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001647 Ops.push_back(Chain);
1648 Ops.push_back(Callee);
1649
1650 // Add argument registers to the end of the list so that they are known live
1651 // into the call.
1652 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1653 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1654 RegsToPass[i].second.getValueType()));
1655
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001656 // Add a register mask operand representing the call-preserved registers.
1657 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1658 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1659 assert(Mask && "Missing call preserved mask for calling convention");
1660 Ops.push_back(DAG.getRegisterMask(Mask));
1661
Gabor Greifba36cb52008-08-28 21:40:38 +00001662 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001663 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001664
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001665 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001666 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001667 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001668
Duncan Sands4bdcb612008-07-02 17:40:58 +00001669 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001670 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001671 InFlag = Chain.getValue(1);
1672
Chris Lattnere563bbc2008-10-11 22:08:30 +00001673 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1674 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001676 InFlag = Chain.getValue(1);
1677
Bob Wilson1f595bb2009-04-17 19:07:39 +00001678 // Handle result values, copying them out of physregs into vregs that we
1679 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001680 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1681 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001682}
1683
Stuart Hastingsf222e592011-02-28 17:17:53 +00001684/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001685/// on the stack. Remember the next parameter register to allocate,
1686/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001687/// this.
1688void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001689ARMTargetLowering::HandleByVal(
1690 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001691 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1692 assert((State->getCallOrPrologue() == Prologue ||
1693 State->getCallOrPrologue() == Call) &&
1694 "unhandled ParmContext");
1695 if ((!State->isFirstByValRegValid()) &&
1696 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001697 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1698 unsigned AlignInRegs = Align / 4;
1699 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1700 for (unsigned i = 0; i < Waste; ++i)
1701 reg = State->AllocateReg(GPRArgRegs, 4);
1702 }
1703 if (reg != 0) {
1704 State->setFirstByValReg(reg);
1705 // At a call site, a byval parameter that is split between
1706 // registers and memory needs its size truncated here. In a
1707 // function prologue, such byval parameters are reassembled in
1708 // memory, and are not truncated.
1709 if (State->getCallOrPrologue() == Call) {
1710 unsigned excess = 4 * (ARM::R4 - reg);
1711 assert(size >= excess && "expected larger existing stack allocation");
1712 size -= excess;
1713 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001714 }
1715 }
1716 // Confiscate any remaining parameter registers to preclude their
1717 // assignment to subsequent parameters.
1718 while (State->AllocateReg(GPRArgRegs, 4))
1719 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001720}
1721
Dale Johannesen51e28e62010-06-03 21:09:53 +00001722/// MatchingStackOffset - Return true if the given stack call argument is
1723/// already available in the same position (relatively) of the caller's
1724/// incoming argument stack.
1725static
1726bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1727 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001728 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001729 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1730 int FI = INT_MAX;
1731 if (Arg.getOpcode() == ISD::CopyFromReg) {
1732 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001733 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001734 return false;
1735 MachineInstr *Def = MRI->getVRegDef(VR);
1736 if (!Def)
1737 return false;
1738 if (!Flags.isByVal()) {
1739 if (!TII->isLoadFromStackSlot(Def, FI))
1740 return false;
1741 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001742 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001743 }
1744 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1745 if (Flags.isByVal())
1746 // ByVal argument is passed in as a pointer but it's now being
1747 // dereferenced. e.g.
1748 // define @foo(%struct.X* %A) {
1749 // tail call @bar(%struct.X* byval %A)
1750 // }
1751 return false;
1752 SDValue Ptr = Ld->getBasePtr();
1753 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1754 if (!FINode)
1755 return false;
1756 FI = FINode->getIndex();
1757 } else
1758 return false;
1759
1760 assert(FI != INT_MAX);
1761 if (!MFI->isFixedObjectIndex(FI))
1762 return false;
1763 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1764}
1765
1766/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1767/// for tail call optimization. Targets which want to do tail call
1768/// optimization should implement this function.
1769bool
1770ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1771 CallingConv::ID CalleeCC,
1772 bool isVarArg,
1773 bool isCalleeStructRet,
1774 bool isCallerStructRet,
1775 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001776 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001777 const SmallVectorImpl<ISD::InputArg> &Ins,
1778 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001779 const Function *CallerF = DAG.getMachineFunction().getFunction();
1780 CallingConv::ID CallerCC = CallerF->getCallingConv();
1781 bool CCMatch = CallerCC == CalleeCC;
1782
1783 // Look for obvious safe cases to perform tail call optimization that do not
1784 // require ABI changes. This is what gcc calls sibcall.
1785
Jim Grosbach7616b642010-06-16 23:45:49 +00001786 // Do not sibcall optimize vararg calls unless the call site is not passing
1787 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001788 if (isVarArg && !Outs.empty())
1789 return false;
1790
1791 // Also avoid sibcall optimization if either caller or callee uses struct
1792 // return semantics.
1793 if (isCalleeStructRet || isCallerStructRet)
1794 return false;
1795
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001796 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001797 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1798 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1799 // support in the assembler and linker to be used. This would need to be
1800 // fixed to fully support tail calls in Thumb1.
1801 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001802 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1803 // LR. This means if we need to reload LR, it takes an extra instructions,
1804 // which outweighs the value of the tail call; but here we don't know yet
1805 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001806 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001807 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001808
1809 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1810 // but we need to make sure there are enough registers; the only valid
1811 // registers are the 4 used for parameters. We don't currently do this
1812 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001813 if (Subtarget->isThumb1Only())
1814 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001815
Dale Johannesen51e28e62010-06-03 21:09:53 +00001816 // If the calling conventions do not match, then we'd better make sure the
1817 // results are returned in the same way as what the caller expects.
1818 if (!CCMatch) {
1819 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001820 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1821 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001822 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1823
1824 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001825 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1826 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001827 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1828
1829 if (RVLocs1.size() != RVLocs2.size())
1830 return false;
1831 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1832 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1833 return false;
1834 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1835 return false;
1836 if (RVLocs1[i].isRegLoc()) {
1837 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1838 return false;
1839 } else {
1840 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1841 return false;
1842 }
1843 }
1844 }
1845
Manman Rene6c3cc82012-10-12 23:39:43 +00001846 // If Caller's vararg or byval argument has been split between registers and
1847 // stack, do not perform tail call, since part of the argument is in caller's
1848 // local frame.
1849 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1850 getInfo<ARMFunctionInfo>();
1851 if (AFI_Caller->getVarArgsRegSaveSize())
1852 return false;
1853
Dale Johannesen51e28e62010-06-03 21:09:53 +00001854 // If the callee takes no arguments then go on to check the results of the
1855 // call.
1856 if (!Outs.empty()) {
1857 // Check if stack adjustment is needed. For now, do not do this if any
1858 // argument is passed on the stack.
1859 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001860 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1861 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001862 CCInfo.AnalyzeCallOperands(Outs,
1863 CCAssignFnForNode(CalleeCC, false, isVarArg));
1864 if (CCInfo.getNextStackOffset()) {
1865 MachineFunction &MF = DAG.getMachineFunction();
1866
1867 // Check if the arguments are already laid out in the right way as
1868 // the caller's fixed stack objects.
1869 MachineFrameInfo *MFI = MF.getFrameInfo();
1870 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001871 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001872 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1873 i != e;
1874 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001875 CCValAssign &VA = ArgLocs[i];
1876 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001877 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001878 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001879 if (VA.getLocInfo() == CCValAssign::Indirect)
1880 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001881 if (VA.needsCustom()) {
1882 // f64 and vector types are split into multiple registers or
1883 // register/stack-slot combinations. The types will not match
1884 // the registers; give up on memory f64 refs until we figure
1885 // out what to do about this.
1886 if (!VA.isRegLoc())
1887 return false;
1888 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001889 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001890 if (RegVT == MVT::v2f64) {
1891 if (!ArgLocs[++i].isRegLoc())
1892 return false;
1893 if (!ArgLocs[++i].isRegLoc())
1894 return false;
1895 }
1896 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001897 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1898 MFI, MRI, TII))
1899 return false;
1900 }
1901 }
1902 }
1903 }
1904
1905 return true;
1906}
1907
Benjamin Kramer350c0082012-11-28 20:55:10 +00001908bool
1909ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1910 MachineFunction &MF, bool isVarArg,
1911 const SmallVectorImpl<ISD::OutputArg> &Outs,
1912 LLVMContext &Context) const {
1913 SmallVector<CCValAssign, 16> RVLocs;
1914 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1915 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1916 isVarArg));
1917}
1918
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919SDValue
1920ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001921 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001922 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001923 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001924 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001925
Bob Wilsondee46d72009-04-17 20:35:10 +00001926 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001928
Bob Wilsondee46d72009-04-17 20:35:10 +00001929 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001930 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1931 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001932
Dan Gohman98ca4f22009-08-05 01:29:28 +00001933 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001934 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1935 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001936
Bob Wilson1f595bb2009-04-17 19:07:39 +00001937 SDValue Flag;
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001938 SmallVector<SDValue, 4> RetOps;
1939 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Bob Wilson1f595bb2009-04-17 19:07:39 +00001940
1941 // Copy the result values into the output registers.
1942 for (unsigned i = 0, realRVLocIdx = 0;
1943 i != RVLocs.size();
1944 ++i, ++realRVLocIdx) {
1945 CCValAssign &VA = RVLocs[i];
1946 assert(VA.isRegLoc() && "Can only return in registers!");
1947
Dan Gohmanc9403652010-07-07 15:54:55 +00001948 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001949
1950 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001951 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001952 case CCValAssign::Full: break;
1953 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001954 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001955 break;
1956 }
1957
Bob Wilson1f595bb2009-04-17 19:07:39 +00001958 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001960 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1962 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001963 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001964 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001965
1966 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1967 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001968 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00001969 VA = RVLocs[++i]; // skip ahead to next loc
1970 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1971 HalfGPRs.getValue(1), Flag);
1972 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001973 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson5bafff32009-06-22 23:27:02 +00001974 VA = RVLocs[++i]; // skip ahead to next loc
1975
1976 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1978 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001979 }
1980 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1981 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001982 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001984 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001985 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001986 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001987 VA = RVLocs[++i]; // skip ahead to next loc
1988 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1989 Flag);
1990 } else
1991 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1992
Bob Wilsondee46d72009-04-17 20:35:10 +00001993 // Guarantee that all emitted copies are
1994 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001995 Flag = Chain.getValue(1);
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001996 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001997 }
1998
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00001999 // Update chain and glue.
2000 RetOps[0] = Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002001 if (Flag.getNode())
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002002 RetOps.push_back(Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002003
Jakob Stoklund Olesenfc743272013-02-05 18:08:40 +00002004 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2005 RetOps.data(), RetOps.size());
Evan Chenga8e29892007-01-19 07:51:42 +00002006}
2007
Evan Chengbf010eb2012-04-10 01:51:00 +00002008bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002009 if (N->getNumValues() != 1)
2010 return false;
2011 if (!N->hasNUsesOfValue(1, 0))
2012 return false;
2013
Evan Chengbf010eb2012-04-10 01:51:00 +00002014 SDValue TCChain = Chain;
2015 SDNode *Copy = *N->use_begin();
2016 if (Copy->getOpcode() == ISD::CopyToReg) {
2017 // If the copy has a glue operand, we conservatively assume it isn't safe to
2018 // perform a tail call.
2019 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2020 return false;
2021 TCChain = Copy->getOperand(0);
2022 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2023 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002024 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002025 SmallPtrSet<SDNode*, 2> Copies;
2026 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002027 UI != UE; ++UI) {
2028 if (UI->getOpcode() != ISD::CopyToReg)
2029 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002030 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002031 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002032 if (Copies.size() > 2)
2033 return false;
2034
2035 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2036 UI != UE; ++UI) {
2037 SDValue UseChain = UI->getOperand(0);
2038 if (Copies.count(UseChain.getNode()))
2039 // Second CopyToReg
2040 Copy = *UI;
2041 else
2042 // First CopyToReg
2043 TCChain = UseChain;
2044 }
2045 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002046 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002047 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002048 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002049 Copy = *Copy->use_begin();
2050 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002051 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002052 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002053 } else {
2054 return false;
2055 }
2056
Evan Cheng1bf891a2010-12-01 22:59:46 +00002057 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002058 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2059 UI != UE; ++UI) {
2060 if (UI->getOpcode() != ARMISD::RET_FLAG)
2061 return false;
2062 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002063 }
2064
Evan Chengbf010eb2012-04-10 01:51:00 +00002065 if (!HasRet)
2066 return false;
2067
2068 Chain = TCChain;
2069 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002070}
2071
Evan Cheng485fafc2011-03-21 01:19:09 +00002072bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002073 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002074 return false;
2075
2076 if (!CI->isTailCall())
2077 return false;
2078
2079 return !Subtarget->isThumb1Only();
2080}
2081
Bob Wilsonb62d2572009-11-03 00:02:05 +00002082// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2083// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2084// one of the above mentioned nodes. It has to be wrapped because otherwise
2085// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2086// be used to form addressing mode. These wrapped nodes will be selected
2087// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002088static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002089 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002090 // FIXME there is no actual debug info here
2091 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002092 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002093 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002094 if (CP->isMachineConstantPoolEntry())
2095 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2096 CP->getAlignment());
2097 else
2098 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2099 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002101}
2102
Jim Grosbache1102ca2010-07-19 17:20:38 +00002103unsigned ARMTargetLowering::getJumpTableEncoding() const {
2104 return MachineJumpTableInfo::EK_Inline;
2105}
2106
Dan Gohmand858e902010-04-17 15:26:15 +00002107SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2108 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002109 MachineFunction &MF = DAG.getMachineFunction();
2110 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2111 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002112 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002113 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002114 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002115 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2116 SDValue CPAddr;
2117 if (RelocM == Reloc::Static) {
2118 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2119 } else {
2120 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002121 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002122 ARMConstantPoolValue *CPV =
2123 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2124 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002125 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2126 }
2127 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2128 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002129 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002130 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002131 if (RelocM == Reloc::Static)
2132 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002133 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002134 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002135}
2136
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002137// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002138SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002139ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002140 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002141 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002142 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002143 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002144 MachineFunction &MF = DAG.getMachineFunction();
2145 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002146 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002147 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002148 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2149 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002150 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002151 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002152 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002153 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002154 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002156
Evan Chenge7e0d622009-11-06 22:24:13 +00002157 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002158 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159
2160 // call __tls_get_addr.
2161 ArgListTy Args;
2162 ArgListEntry Entry;
2163 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002164 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002165 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002166 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002167 TargetLowering::CallLoweringInfo CLI(Chain,
2168 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002169 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002170 0, CallingConv::C, /*isTailCall=*/false,
2171 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002172 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002173 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174 return CallResult.first;
2175}
2176
2177// Lower ISD::GlobalTLSAddress using the "initial exec" or
2178// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002179SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002180ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002181 SelectionDAG &DAG,
2182 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002183 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002184 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002185 SDValue Offset;
2186 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002188 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002189 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002190
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002191 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002192 MachineFunction &MF = DAG.getMachineFunction();
2193 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002194 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002195 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002196 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2197 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002198 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2199 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2200 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002201 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002202 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002203 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002204 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002205 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002206 Chain = Offset.getValue(1);
2207
Evan Chenge7e0d622009-11-06 22:24:13 +00002208 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002209 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002210
Evan Cheng9eda6892009-10-31 03:39:36 +00002211 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002212 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002213 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002214 } else {
2215 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002216 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002217 ARMConstantPoolValue *CPV =
2218 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002219 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002220 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002221 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002222 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002223 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002224 }
2225
2226 // The address of the thread local variable is the add of the thread
2227 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002228 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002229}
2230
Dan Gohman475871a2008-07-27 21:46:04 +00002231SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002232ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002233 // TODO: implement the "local dynamic" model
2234 assert(Subtarget->isTargetELF() &&
2235 "TLS not implemented for non-ELF targets");
2236 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002237
2238 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2239
2240 switch (model) {
2241 case TLSModel::GeneralDynamic:
2242 case TLSModel::LocalDynamic:
2243 return LowerToTLSGeneralDynamicModel(GA, DAG);
2244 case TLSModel::InitialExec:
2245 case TLSModel::LocalExec:
2246 return LowerToTLSExecModels(GA, DAG, model);
2247 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002248 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002249}
2250
Dan Gohman475871a2008-07-27 21:46:04 +00002251SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002252 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002253 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002254 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002255 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002256 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2257 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002258 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002259 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002260 ARMConstantPoolConstant::Create(GV,
2261 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002262 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002263 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002264 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002265 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002266 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002267 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002268 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002269 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002270 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002271 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002272 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002273 MachinePointerInfo::getGOT(),
2274 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002275 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002276 }
2277
2278 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002279 // pair. This is always cheaper.
2280 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002281 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002282 // FIXME: Once remat is capable of dealing with instructions with register
2283 // operands, expand this into two nodes.
2284 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2285 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002286 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002287 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2288 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2289 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2290 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002291 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002292 }
2293}
2294
Dan Gohman475871a2008-07-27 21:46:04 +00002295SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002296 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002297 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002298 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002299 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002300 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002301 MachineFunction &MF = DAG.getMachineFunction();
2302 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2303
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002304 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2305 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002306 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002307 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002308 // FIXME: Once remat is capable of dealing with instructions with register
2309 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002310 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002311 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2312 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2313
Evan Cheng53519f02011-01-21 18:55:51 +00002314 unsigned Wrapper = (RelocM == Reloc::PIC_)
2315 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2316 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002317 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002318 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2319 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002320 MachinePointerInfo::getGOT(),
2321 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002322 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002323 }
2324
2325 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002326 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002327 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002328 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002329 } else {
2330 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002331 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2332 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002333 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2334 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002335 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002336 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002337 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002338
Evan Cheng9eda6892009-10-31 03:39:36 +00002339 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002340 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002341 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002342 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002343
2344 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002345 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002346 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002347 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002348
Evan Cheng63476a82009-09-03 07:04:02 +00002349 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002350 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002351 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002352
2353 return Result;
2354}
2355
Dan Gohman475871a2008-07-27 21:46:04 +00002356SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002357 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002358 assert(Subtarget->isTargetELF() &&
2359 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002360 MachineFunction &MF = DAG.getMachineFunction();
2361 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002362 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002363 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002364 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002365 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002366 ARMConstantPoolValue *CPV =
2367 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2368 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002369 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002370 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002371 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002372 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002373 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002374 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002375 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002376}
2377
Jim Grosbach0e0da732009-05-12 23:59:14 +00002378SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002379ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2380 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002381 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002382 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2383 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002384 Op.getOperand(1), Val);
2385}
2386
2387SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002388ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2389 DebugLoc dl = Op.getDebugLoc();
2390 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2391 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2392}
2393
2394SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002395ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002396 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002397 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002398 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002399 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002400 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002401 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002402 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002403 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2404 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002405 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002406 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002407 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002408 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002409 EVT PtrVT = getPointerTy();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002410 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2411 SDValue CPAddr;
2412 unsigned PCAdj = (RelocM != Reloc::PIC_)
2413 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002414 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002415 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2416 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002417 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002418 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002419 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002420 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002421 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002422 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002423
2424 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002425 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002426 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2427 }
2428 return Result;
2429 }
Evan Cheng92e39162011-03-29 23:06:19 +00002430 case Intrinsic::arm_neon_vmulls:
2431 case Intrinsic::arm_neon_vmullu: {
2432 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2433 ? ARMISD::VMULLs : ARMISD::VMULLu;
2434 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2435 Op.getOperand(1), Op.getOperand(2));
2436 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002437 }
2438}
2439
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002440static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002441 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002442 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002443 if (!Subtarget->hasDataBarrier()) {
2444 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2445 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2446 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002447 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002448 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002449 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002450 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002451 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002452
2453 SDValue Op5 = Op.getOperand(5);
2454 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2455 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2456 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2457 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2458
2459 ARM_MB::MemBOpt DMBOpt;
2460 if (isDeviceBarrier)
2461 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2462 else
2463 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2464 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2465 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002466}
2467
Eli Friedman26689ac2011-08-03 21:06:02 +00002468
2469static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2470 const ARMSubtarget *Subtarget) {
2471 // FIXME: handle "fence singlethread" more efficiently.
2472 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002473 if (!Subtarget->hasDataBarrier()) {
2474 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2475 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2476 // here.
2477 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2478 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002479 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002480 DAG.getConstant(0, MVT::i32));
2481 }
2482
Eli Friedman26689ac2011-08-03 21:06:02 +00002483 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002484 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002485}
2486
Evan Chengdfed19f2010-11-03 06:34:55 +00002487static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2488 const ARMSubtarget *Subtarget) {
2489 // ARM pre v5TE and Thumb1 does not have preload instructions.
2490 if (!(Subtarget->isThumb2() ||
2491 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2492 // Just preserve the chain.
2493 return Op.getOperand(0);
2494
2495 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002496 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2497 if (!isRead &&
2498 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2499 // ARMv7 with MP extension has PLDW.
2500 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002501
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002502 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2503 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002504 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002505 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002506 isData = ~isData & 1;
2507 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002508
2509 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002510 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2511 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002512}
2513
Dan Gohman1e93df62010-04-17 14:41:14 +00002514static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2515 MachineFunction &MF = DAG.getMachineFunction();
2516 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2517
Evan Chenga8e29892007-01-19 07:51:42 +00002518 // vastart just stores the address of the VarArgsFrameIndex slot into the
2519 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002520 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002521 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002522 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002523 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002524 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2525 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002526}
2527
Dan Gohman475871a2008-07-27 21:46:04 +00002528SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002529ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2530 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002531 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002532 MachineFunction &MF = DAG.getMachineFunction();
2533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2534
Craig Topper44d23822012-02-22 05:59:10 +00002535 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002536 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002537 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002538 else
Craig Topper420761a2012-04-20 07:30:17 +00002539 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002540
2541 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002542 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002543 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002544
2545 SDValue ArgValue2;
2546 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002547 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002548 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002549
2550 // Create load node to retrieve arguments from the stack.
2551 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002552 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002553 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002554 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002555 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002556 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002557 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002558 }
2559
Jim Grosbache5165492009-11-09 00:11:35 +00002560 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002561}
2562
Stuart Hastingsc7315872011-04-20 16:47:52 +00002563void
2564ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2565 unsigned &VARegSize, unsigned &VARegSaveSize)
2566 const {
2567 unsigned NumGPRs;
2568 if (CCInfo.isFirstByValRegValid())
2569 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2570 else {
2571 unsigned int firstUnalloced;
2572 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2573 sizeof(GPRArgRegs) /
2574 sizeof(GPRArgRegs[0]));
2575 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2576 }
2577
2578 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2579 VARegSize = NumGPRs * 4;
2580 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2581}
2582
2583// The remaining GPRs hold either the beginning of variable-argument
David Peixottoe68542e2013-02-13 00:36:35 +00002584// data, or the beginning of an aggregate passed by value (usually
Stuart Hastingsc7315872011-04-20 16:47:52 +00002585// byval). Either way, we allocate stack slots adjacent to the data
2586// provided by our caller, and store the unallocated registers there.
2587// If this is a variadic function, the va_list pointer will begin with
2588// these values; otherwise, this reassembles a (byval) structure that
2589// was split between registers and memory.
2590void
2591ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2592 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002593 const Value *OrigArg,
2594 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002595 unsigned ArgOffset,
2596 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002597 MachineFunction &MF = DAG.getMachineFunction();
2598 MachineFrameInfo *MFI = MF.getFrameInfo();
2599 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2600 unsigned firstRegToSaveIndex;
2601 if (CCInfo.isFirstByValRegValid())
2602 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2603 else {
2604 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2605 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2606 }
2607
2608 unsigned VARegSize, VARegSaveSize;
2609 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2610 if (VARegSaveSize) {
2611 // If this function is vararg, store any remaining integer argument regs
2612 // to their spots on the stack so that they may be loaded by deferencing
2613 // the result of va_next.
2614 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002615 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2616 ArgOffset + VARegSaveSize
2617 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002618 false));
2619 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2620 getPointerTy());
2621
2622 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002623 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002624 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002625 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002626 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002627 else
Craig Topper420761a2012-04-20 07:30:17 +00002628 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002629
2630 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2631 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2632 SDValue Store =
2633 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002634 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002635 false, false, 0);
2636 MemOps.push_back(Store);
2637 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2638 DAG.getConstant(4, getPointerTy()));
2639 }
2640 if (!MemOps.empty())
2641 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2642 &MemOps[0], MemOps.size());
2643 } else
2644 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002645 AFI->setVarArgsFrameIndex(
2646 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002647}
2648
Bob Wilson5bafff32009-06-22 23:27:02 +00002649SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002651 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002652 const SmallVectorImpl<ISD::InputArg>
2653 &Ins,
2654 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002655 SmallVectorImpl<SDValue> &InVals)
2656 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002657 MachineFunction &MF = DAG.getMachineFunction();
2658 MachineFrameInfo *MFI = MF.getFrameInfo();
2659
Bob Wilson1f595bb2009-04-17 19:07:39 +00002660 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2661
2662 // Assign locations to all of the incoming arguments.
2663 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002664 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2665 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002666 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002667 CCAssignFnForNode(CallConv, /* Return*/ false,
2668 isVarArg));
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002669
Bob Wilson1f595bb2009-04-17 19:07:39 +00002670 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002671 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002672 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002673 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2674 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002675 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2676 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002677 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2678 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002679 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002680 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002681 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002682
Bob Wilson1f595bb2009-04-17 19:07:39 +00002683 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002684 // f64 and vector types are split up into multiple registers or
2685 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002686 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002687 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002688 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002689 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002690 SDValue ArgValue2;
2691 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002692 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002693 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2694 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002695 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002696 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002697 } else {
2698 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2699 Chain, DAG, dl);
2700 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002701 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2702 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002703 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002704 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002705 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2706 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002707 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002708
Bob Wilson5bafff32009-06-22 23:27:02 +00002709 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002710 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002711
Owen Anderson825b72b2009-08-11 20:47:22 +00002712 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002713 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002715 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002716 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002717 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002718 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002719 RC = AFI->isThumb1OnlyFunction() ?
2720 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2721 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002722 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002723 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002724
2725 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002726 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002727 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002728 }
2729
2730 // If this is an 8 or 16-bit value, it is really passed promoted
2731 // to 32 bits. Insert an assert[sz]ext to capture this, then
2732 // truncate to the right size.
2733 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002734 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002735 case CCValAssign::Full: break;
2736 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002737 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002738 break;
2739 case CCValAssign::SExt:
2740 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2741 DAG.getValueType(VA.getValVT()));
2742 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2743 break;
2744 case CCValAssign::ZExt:
2745 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2746 DAG.getValueType(VA.getValVT()));
2747 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2748 break;
2749 }
2750
Dan Gohman98ca4f22009-08-05 01:29:28 +00002751 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002752
2753 } else { // VA.isRegLoc()
2754
2755 // sanity check
2756 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002757 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002758
Stuart Hastingsf222e592011-02-28 17:17:53 +00002759 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002760
Stuart Hastingsf222e592011-02-28 17:17:53 +00002761 // Some Ins[] entries become multiple ArgLoc[] entries.
2762 // Process them only once.
2763 if (index != lastInsIndex)
2764 {
2765 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002766 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002767 // This can be changed with more analysis.
2768 // In case of tail call optimization mark all arguments mutable.
2769 // Since they could be overwritten by lowering of arguments in case of
2770 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002771 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002772 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2773 if (!AFI->getVarArgsFrameIndex()) {
2774 VarArgStyleRegisters(CCInfo, DAG,
2775 dl, Chain, CurOrigArg,
2776 Ins[VA.getValNo()].PartOffset,
2777 VA.getLocMemOffset(),
2778 true /*force mutable frames*/);
2779 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2780 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2781 } else {
2782 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2783 VA.getLocMemOffset(), false);
2784 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2785 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002786 } else {
2787 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2788 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002789
Stuart Hastingsf222e592011-02-28 17:17:53 +00002790 // Create load nodes to retrieve arguments from the stack.
2791 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2792 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2793 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002794 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002795 }
2796 lastInsIndex = index;
2797 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002798 }
2799 }
2800
2801 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002802 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002803 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2804 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002805
Dan Gohman98ca4f22009-08-05 01:29:28 +00002806 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002807}
2808
2809/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002810static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002811 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002812 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002813 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002814 // Maybe this has already been legalized into the constant pool?
2815 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002816 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002817 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002818 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002819 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002820 }
2821 }
2822 return false;
2823}
2824
Evan Chenga8e29892007-01-19 07:51:42 +00002825/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2826/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002827SDValue
2828ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002829 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002830 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002831 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002832 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002833 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002834 // Constant does not fit, try adjusting it by one?
2835 switch (CC) {
2836 default: break;
2837 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002838 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002839 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002840 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002841 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002842 }
2843 break;
2844 case ISD::SETULT:
2845 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002846 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002847 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002848 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002849 }
2850 break;
2851 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002852 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002853 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002854 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002855 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002856 }
2857 break;
2858 case ISD::SETULE:
2859 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002860 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002861 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002862 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002863 }
2864 break;
2865 }
2866 }
2867 }
2868
2869 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002870 ARMISD::NodeType CompareType;
2871 switch (CondCode) {
2872 default:
2873 CompareType = ARMISD::CMP;
2874 break;
2875 case ARMCC::EQ:
2876 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002877 // Uses only Z Flag
2878 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002879 break;
2880 }
Evan Cheng218977b2010-07-13 19:27:42 +00002881 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002882 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002883}
2884
2885/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002886SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002887ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002888 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002889 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002890 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002891 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002892 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002893 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2894 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002895}
2896
Bob Wilson79f56c92011-03-08 01:17:20 +00002897/// duplicateCmp - Glue values can have only one use, so this function
2898/// duplicates a comparison node.
2899SDValue
2900ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2901 unsigned Opc = Cmp.getOpcode();
2902 DebugLoc DL = Cmp.getDebugLoc();
2903 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2904 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2905
2906 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2907 Cmp = Cmp.getOperand(0);
2908 Opc = Cmp.getOpcode();
2909 if (Opc == ARMISD::CMPFP)
2910 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2911 else {
2912 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2913 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2914 }
2915 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2916}
2917
Bill Wendlingde2b1512010-08-11 08:43:16 +00002918SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2919 SDValue Cond = Op.getOperand(0);
2920 SDValue SelectTrue = Op.getOperand(1);
2921 SDValue SelectFalse = Op.getOperand(2);
2922 DebugLoc dl = Op.getDebugLoc();
2923
2924 // Convert:
2925 //
2926 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2927 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2928 //
2929 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2930 const ConstantSDNode *CMOVTrue =
2931 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2932 const ConstantSDNode *CMOVFalse =
2933 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2934
2935 if (CMOVTrue && CMOVFalse) {
2936 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2937 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2938
2939 SDValue True;
2940 SDValue False;
2941 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2942 True = SelectTrue;
2943 False = SelectFalse;
2944 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2945 True = SelectFalse;
2946 False = SelectTrue;
2947 }
2948
2949 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002950 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002951 SDValue ARMcc = Cond.getOperand(2);
2952 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002953 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002954 assert(True.getValueType() == VT);
2955 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002956 }
2957 }
2958 }
2959
Dan Gohmandb953892012-02-24 00:09:36 +00002960 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2961 // undefined bits before doing a full-word comparison with zero.
2962 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2963 DAG.getConstant(1, Cond.getValueType()));
2964
Bill Wendlingde2b1512010-08-11 08:43:16 +00002965 return DAG.getSelectCC(dl, Cond,
2966 DAG.getConstant(0, Cond.getValueType()),
2967 SelectTrue, SelectFalse, ISD::SETNE);
2968}
2969
Dan Gohmand858e902010-04-17 15:26:15 +00002970SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002971 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002972 SDValue LHS = Op.getOperand(0);
2973 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002974 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002975 SDValue TrueVal = Op.getOperand(2);
2976 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002977 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002978
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002980 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002982 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002983 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002984 }
2985
2986 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002987 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002988
Evan Cheng218977b2010-07-13 19:27:42 +00002989 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2990 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002991 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002992 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002993 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002994 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002995 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002996 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002997 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002998 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002999 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00003000 }
3001 return Result;
3002}
3003
Evan Cheng218977b2010-07-13 19:27:42 +00003004/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3005/// to morph to an integer compare sequence.
3006static bool canChangeToInt(SDValue Op, bool &SeenZero,
3007 const ARMSubtarget *Subtarget) {
3008 SDNode *N = Op.getNode();
3009 if (!N->hasOneUse())
3010 // Otherwise it requires moving the value from fp to integer registers.
3011 return false;
3012 if (!N->getNumValues())
3013 return false;
3014 EVT VT = Op.getValueType();
3015 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3016 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3017 // vmrs are very slow, e.g. cortex-a8.
3018 return false;
3019
3020 if (isFloatingPointZero(Op)) {
3021 SeenZero = true;
3022 return true;
3023 }
3024 return ISD::isNormalLoad(N);
3025}
3026
3027static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3028 if (isFloatingPointZero(Op))
3029 return DAG.getConstant(0, MVT::i32);
3030
3031 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3032 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003033 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003034 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003035 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003036
3037 llvm_unreachable("Unknown VFP cmp argument!");
3038}
3039
3040static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3041 SDValue &RetVal1, SDValue &RetVal2) {
3042 if (isFloatingPointZero(Op)) {
3043 RetVal1 = DAG.getConstant(0, MVT::i32);
3044 RetVal2 = DAG.getConstant(0, MVT::i32);
3045 return;
3046 }
3047
3048 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3049 SDValue Ptr = Ld->getBasePtr();
3050 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3051 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003052 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003053 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003054 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003055
3056 EVT PtrType = Ptr.getValueType();
3057 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3058 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3059 PtrType, Ptr, DAG.getConstant(4, PtrType));
3060 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3061 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003062 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003063 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003064 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003065 return;
3066 }
3067
3068 llvm_unreachable("Unknown VFP cmp argument!");
3069}
3070
3071/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3072/// f32 and even f64 comparisons to integer ones.
3073SDValue
3074ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3075 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003076 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003077 SDValue LHS = Op.getOperand(2);
3078 SDValue RHS = Op.getOperand(3);
3079 SDValue Dest = Op.getOperand(4);
3080 DebugLoc dl = Op.getDebugLoc();
3081
Evan Chengfc501a32012-03-01 23:27:13 +00003082 bool LHSSeenZero = false;
3083 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3084 bool RHSSeenZero = false;
3085 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3086 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003087 // If unsafe fp math optimization is enabled and there are no other uses of
3088 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003089 // to an integer comparison.
3090 if (CC == ISD::SETOEQ)
3091 CC = ISD::SETEQ;
3092 else if (CC == ISD::SETUNE)
3093 CC = ISD::SETNE;
3094
Evan Chengfc501a32012-03-01 23:27:13 +00003095 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003096 SDValue ARMcc;
3097 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003098 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3099 bitcastf32Toi32(LHS, DAG), Mask);
3100 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3101 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003102 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3103 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3104 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3105 Chain, Dest, ARMcc, CCR, Cmp);
3106 }
3107
3108 SDValue LHS1, LHS2;
3109 SDValue RHS1, RHS2;
3110 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3111 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003112 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3113 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003114 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3115 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003116 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003117 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3118 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3119 }
3120
3121 return SDValue();
3122}
3123
3124SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3125 SDValue Chain = Op.getOperand(0);
3126 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3127 SDValue LHS = Op.getOperand(2);
3128 SDValue RHS = Op.getOperand(3);
3129 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003130 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003131
Owen Anderson825b72b2009-08-11 20:47:22 +00003132 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003133 SDValue ARMcc;
3134 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003137 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003138 }
3139
Owen Anderson825b72b2009-08-11 20:47:22 +00003140 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003141
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003142 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003143 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3144 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3145 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3146 if (Result.getNode())
3147 return Result;
3148 }
3149
Evan Chenga8e29892007-01-19 07:51:42 +00003150 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003151 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003152
Evan Cheng218977b2010-07-13 19:27:42 +00003153 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3154 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003156 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003157 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003158 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003159 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003160 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3161 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003162 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003163 }
3164 return Res;
3165}
3166
Dan Gohmand858e902010-04-17 15:26:15 +00003167SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003168 SDValue Chain = Op.getOperand(0);
3169 SDValue Table = Op.getOperand(1);
3170 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003171 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003172
Owen Andersone50ed302009-08-10 22:56:29 +00003173 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003174 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3175 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003176 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003178 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003179 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3180 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003181 if (Subtarget->isThumb2()) {
3182 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3183 // which does another jump to the destination. This also makes it easier
3184 // to translate it to TBB / TBH later.
3185 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003186 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003187 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003188 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003189 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003190 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003191 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003192 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003193 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003194 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003196 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003197 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003198 MachinePointerInfo::getJumpTable(),
3199 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003200 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003201 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003202 }
Evan Chenga8e29892007-01-19 07:51:42 +00003203}
3204
Eli Friedman14e809c2011-11-09 23:36:02 +00003205static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003206 EVT VT = Op.getValueType();
3207 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003208
James Molloy873fd5f2012-02-20 09:24:05 +00003209 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3210 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3211 return Op;
3212 return DAG.UnrollVectorOp(Op.getNode());
3213 }
3214
3215 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3216 "Invalid type for custom lowering!");
3217 if (VT != MVT::v4i16)
3218 return DAG.UnrollVectorOp(Op.getNode());
3219
3220 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3221 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003222}
3223
Bob Wilson76a312b2010-03-19 22:51:32 +00003224static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003225 EVT VT = Op.getValueType();
3226 if (VT.isVector())
3227 return LowerVectorFP_TO_INT(Op, DAG);
3228
Bob Wilson76a312b2010-03-19 22:51:32 +00003229 DebugLoc dl = Op.getDebugLoc();
3230 unsigned Opc;
3231
3232 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003233 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003234 case ISD::FP_TO_SINT:
3235 Opc = ARMISD::FTOSI;
3236 break;
3237 case ISD::FP_TO_UINT:
3238 Opc = ARMISD::FTOUI;
3239 break;
3240 }
3241 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003242 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003243}
3244
Cameron Zwarich3007d332011-03-29 21:41:55 +00003245static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3246 EVT VT = Op.getValueType();
3247 DebugLoc dl = Op.getDebugLoc();
3248
Eli Friedman14e809c2011-11-09 23:36:02 +00003249 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3250 if (VT.getVectorElementType() == MVT::f32)
3251 return Op;
3252 return DAG.UnrollVectorOp(Op.getNode());
3253 }
3254
Duncan Sands1f6a3292011-08-12 14:54:45 +00003255 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3256 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003257 if (VT != MVT::v4f32)
3258 return DAG.UnrollVectorOp(Op.getNode());
3259
3260 unsigned CastOpc;
3261 unsigned Opc;
3262 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003263 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003264 case ISD::SINT_TO_FP:
3265 CastOpc = ISD::SIGN_EXTEND;
3266 Opc = ISD::SINT_TO_FP;
3267 break;
3268 case ISD::UINT_TO_FP:
3269 CastOpc = ISD::ZERO_EXTEND;
3270 Opc = ISD::UINT_TO_FP;
3271 break;
3272 }
3273
3274 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3275 return DAG.getNode(Opc, dl, VT, Op);
3276}
3277
Bob Wilson76a312b2010-03-19 22:51:32 +00003278static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3279 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003280 if (VT.isVector())
3281 return LowerVectorINT_TO_FP(Op, DAG);
3282
Bob Wilson76a312b2010-03-19 22:51:32 +00003283 DebugLoc dl = Op.getDebugLoc();
3284 unsigned Opc;
3285
3286 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003287 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003288 case ISD::SINT_TO_FP:
3289 Opc = ARMISD::SITOF;
3290 break;
3291 case ISD::UINT_TO_FP:
3292 Opc = ARMISD::UITOF;
3293 break;
3294 }
3295
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003296 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003297 return DAG.getNode(Opc, dl, VT, Op);
3298}
3299
Evan Cheng515fe3a2010-07-08 02:08:50 +00003300SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003301 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003302 SDValue Tmp0 = Op.getOperand(0);
3303 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003304 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003305 EVT VT = Op.getValueType();
3306 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003307 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3308 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3309 bool UseNEON = !InGPR && Subtarget->hasNEON();
3310
3311 if (UseNEON) {
3312 // Use VBSL to copy the sign bit.
3313 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3314 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3315 DAG.getTargetConstant(EncodedVal, MVT::i32));
3316 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3317 if (VT == MVT::f64)
3318 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3319 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3320 DAG.getConstant(32, MVT::i32));
3321 else /*if (VT == MVT::f32)*/
3322 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3323 if (SrcVT == MVT::f32) {
3324 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3325 if (VT == MVT::f64)
3326 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3327 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3328 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003329 } else if (VT == MVT::f32)
3330 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3331 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3332 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003333 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3334 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3335
3336 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3337 MVT::i32);
3338 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3339 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3340 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003341
Evan Chenge573fb32011-02-23 02:24:55 +00003342 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3343 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3344 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003345 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003346 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3347 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3348 DAG.getConstant(0, MVT::i32));
3349 } else {
3350 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3351 }
3352
3353 return Res;
3354 }
Evan Chengc143dd42011-02-11 02:28:55 +00003355
3356 // Bitcast operand 1 to i32.
3357 if (SrcVT == MVT::f64)
3358 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3359 &Tmp1, 1).getValue(1);
3360 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3361
Evan Chenge573fb32011-02-23 02:24:55 +00003362 // Or in the signbit with integer operations.
3363 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3364 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3365 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3366 if (VT == MVT::f32) {
3367 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3368 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3369 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3370 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003371 }
3372
Evan Chenge573fb32011-02-23 02:24:55 +00003373 // f64: Or the high part with signbit and then combine two parts.
3374 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3375 &Tmp0, 1);
3376 SDValue Lo = Tmp0.getValue(0);
3377 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3378 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3379 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003380}
3381
Evan Cheng2457f2c2010-05-22 01:47:14 +00003382SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3383 MachineFunction &MF = DAG.getMachineFunction();
3384 MachineFrameInfo *MFI = MF.getFrameInfo();
3385 MFI->setReturnAddressIsTaken(true);
3386
3387 EVT VT = Op.getValueType();
3388 DebugLoc dl = Op.getDebugLoc();
3389 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3390 if (Depth) {
3391 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3392 SDValue Offset = DAG.getConstant(4, MVT::i32);
3393 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3394 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003395 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003396 }
3397
3398 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003399 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003400 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3401}
3402
Dan Gohmand858e902010-04-17 15:26:15 +00003403SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003404 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3405 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003406
Owen Andersone50ed302009-08-10 22:56:29 +00003407 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003408 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3409 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003410 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003411 ? ARM::R7 : ARM::R11;
3412 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3413 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003414 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3415 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003416 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003417 return FrameAddr;
3418}
3419
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003420/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003421/// expand a bit convert where either the source or destination type is i64 to
3422/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3423/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3424/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003425static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003426 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3427 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003428 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003429
Bob Wilson9f3f0612010-04-17 05:30:19 +00003430 // This function is only supposed to be called for i64 types, either as the
3431 // source or destination of the bit convert.
3432 EVT SrcVT = Op.getValueType();
3433 EVT DstVT = N->getValueType(0);
3434 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003435 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003436
Bob Wilson9f3f0612010-04-17 05:30:19 +00003437 // Turn i64->f64 into VMOVDRR.
3438 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003439 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3440 DAG.getConstant(0, MVT::i32));
3441 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3442 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003443 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003444 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003445 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003446
Jim Grosbache5165492009-11-09 00:11:35 +00003447 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003448 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3449 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3450 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3451 // Merge the pieces into a single i64 value.
3452 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3453 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003454
Bob Wilson9f3f0612010-04-17 05:30:19 +00003455 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003456}
3457
Bob Wilson5bafff32009-06-22 23:27:02 +00003458/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003459/// Zero vectors are used to represent vector negation and in those cases
3460/// will be implemented with the NEON VNEG instruction. However, VNEG does
3461/// not support i64 elements, so sometimes the zero vectors will need to be
3462/// explicitly constructed. Regardless, use a canonical VMOV to create the
3463/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003464static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003465 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003466 // The canonical modified immediate encoding of a zero vector is....0!
3467 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3468 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3469 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003470 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003471}
3472
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003473/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3474/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003475SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3476 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003477 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3478 EVT VT = Op.getValueType();
3479 unsigned VTBits = VT.getSizeInBits();
3480 DebugLoc dl = Op.getDebugLoc();
3481 SDValue ShOpLo = Op.getOperand(0);
3482 SDValue ShOpHi = Op.getOperand(1);
3483 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003484 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003485 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003486
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003487 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3488
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003489 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3490 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3491 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3492 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3493 DAG.getConstant(VTBits, MVT::i32));
3494 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3495 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003496 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003497
3498 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3499 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003500 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003501 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003502 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003503 CCR, Cmp);
3504
3505 SDValue Ops[2] = { Lo, Hi };
3506 return DAG.getMergeValues(Ops, 2, dl);
3507}
3508
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003509/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3510/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003511SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3512 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003513 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3514 EVT VT = Op.getValueType();
3515 unsigned VTBits = VT.getSizeInBits();
3516 DebugLoc dl = Op.getDebugLoc();
3517 SDValue ShOpLo = Op.getOperand(0);
3518 SDValue ShOpHi = Op.getOperand(1);
3519 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003520 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003521
3522 assert(Op.getOpcode() == ISD::SHL_PARTS);
3523 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3524 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3525 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3526 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3527 DAG.getConstant(VTBits, MVT::i32));
3528 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3529 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3530
3531 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3532 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3533 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003534 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003535 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003536 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003537 CCR, Cmp);
3538
3539 SDValue Ops[2] = { Lo, Hi };
3540 return DAG.getMergeValues(Ops, 2, dl);
3541}
3542
Jim Grosbach4725ca72010-09-08 03:54:02 +00003543SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003544 SelectionDAG &DAG) const {
3545 // The rounding mode is in bits 23:22 of the FPSCR.
3546 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3547 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3548 // so that the shift + and get folded into a bitfield extract.
3549 DebugLoc dl = Op.getDebugLoc();
3550 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3551 DAG.getConstant(Intrinsic::arm_get_fpscr,
3552 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003553 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003554 DAG.getConstant(1U << 22, MVT::i32));
3555 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3556 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003557 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003558 DAG.getConstant(3, MVT::i32));
3559}
3560
Jim Grosbach3482c802010-01-18 19:58:49 +00003561static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3562 const ARMSubtarget *ST) {
3563 EVT VT = N->getValueType(0);
3564 DebugLoc dl = N->getDebugLoc();
3565
3566 if (!ST->hasV6T2Ops())
3567 return SDValue();
3568
3569 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3570 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3571}
3572
Evan Chengc8e70452012-12-04 22:41:50 +00003573/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3574/// for each 16-bit element from operand, repeated. The basic idea is to
3575/// leverage vcnt to get the 8-bit counts, gather and add the results.
3576///
3577/// Trace for v4i16:
3578/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3579/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3580/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
3581/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
3582/// [b0 b1 b2 b3 b4 b5 b6 b7]
3583/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3584/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3585/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3586static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3587 EVT VT = N->getValueType(0);
3588 DebugLoc DL = N->getDebugLoc();
3589
3590 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3591 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3592 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3593 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3594 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3595 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3596}
3597
3598/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3599/// bit-count for each 16-bit element from the operand. We need slightly
3600/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3601/// 64/128-bit registers.
3602///
3603/// Trace for v4i16:
3604/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3605/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3606/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3607/// v4i16:Extracted = [k0 k1 k2 k3 ]
3608static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3609 EVT VT = N->getValueType(0);
3610 DebugLoc DL = N->getDebugLoc();
3611
3612 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3613 if (VT.is64BitVector()) {
3614 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3615 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3616 DAG.getIntPtrConstant(0));
3617 } else {
3618 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3619 BitCounts, DAG.getIntPtrConstant(0));
3620 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3621 }
3622}
3623
3624/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3625/// bit-count for each 32-bit element from the operand. The idea here is
3626/// to split the vector into 16-bit elements, leverage the 16-bit count
3627/// routine, and then combine the results.
3628///
3629/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3630/// input = [v0 v1 ] (vi: 32-bit elements)
3631/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3632/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
3633/// vrev: N0 = [k1 k0 k3 k2 ]
3634/// [k0 k1 k2 k3 ]
3635/// N1 =+[k1 k0 k3 k2 ]
3636/// [k0 k2 k1 k3 ]
3637/// N2 =+[k1 k3 k0 k2 ]
3638/// [k0 k2 k1 k3 ]
3639/// Extended =+[k1 k3 k0 k2 ]
3640/// [k0 k2 ]
3641/// Extracted=+[k1 k3 ]
3642///
3643static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3644 EVT VT = N->getValueType(0);
3645 DebugLoc DL = N->getDebugLoc();
3646
3647 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3648
3649 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3650 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3651 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3652 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3653 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3654
3655 if (VT.is64BitVector()) {
3656 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3657 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3658 DAG.getIntPtrConstant(0));
3659 } else {
3660 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3661 DAG.getIntPtrConstant(0));
3662 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3663 }
3664}
3665
3666static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3667 const ARMSubtarget *ST) {
3668 EVT VT = N->getValueType(0);
3669
3670 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay105ab4f2012-12-04 23:54:02 +00003671 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3672 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengc8e70452012-12-04 22:41:50 +00003673 "Unexpected type for custom ctpop lowering");
3674
3675 if (VT.getVectorElementType() == MVT::i32)
3676 return lowerCTPOP32BitElements(N, DAG);
3677 else
3678 return lowerCTPOP16BitElements(N, DAG);
3679}
3680
Bob Wilson5bafff32009-06-22 23:27:02 +00003681static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3682 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003683 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003684 DebugLoc dl = N->getDebugLoc();
3685
Bob Wilsond5448bb2010-11-18 21:16:28 +00003686 if (!VT.isVector())
3687 return SDValue();
3688
Bob Wilson5bafff32009-06-22 23:27:02 +00003689 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003690 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003691
Bob Wilsond5448bb2010-11-18 21:16:28 +00003692 // Left shifts translate directly to the vshiftu intrinsic.
3693 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003694 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003695 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3696 N->getOperand(0), N->getOperand(1));
3697
3698 assert((N->getOpcode() == ISD::SRA ||
3699 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3700
3701 // NEON uses the same intrinsics for both left and right shifts. For
3702 // right shifts, the shift amounts are negative, so negate the vector of
3703 // shift amounts.
3704 EVT ShiftVT = N->getOperand(1).getValueType();
3705 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3706 getZeroVector(ShiftVT, DAG, dl),
3707 N->getOperand(1));
3708 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3709 Intrinsic::arm_neon_vshifts :
3710 Intrinsic::arm_neon_vshiftu);
3711 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3712 DAG.getConstant(vshiftInt, MVT::i32),
3713 N->getOperand(0), NegatedCount);
3714}
3715
3716static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3717 const ARMSubtarget *ST) {
3718 EVT VT = N->getValueType(0);
3719 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003720
Eli Friedmance392eb2009-08-22 03:13:10 +00003721 // We can get here for a node like i32 = ISD::SHL i32, i64
3722 if (VT != MVT::i64)
3723 return SDValue();
3724
3725 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003726 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003727
Chris Lattner27a6c732007-11-24 07:07:01 +00003728 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3729 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003730 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003731 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003732
Chris Lattner27a6c732007-11-24 07:07:01 +00003733 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003734 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003735
Chris Lattner27a6c732007-11-24 07:07:01 +00003736 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003738 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003739 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003740 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003741
Chris Lattner27a6c732007-11-24 07:07:01 +00003742 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3743 // captures the result into a carry flag.
3744 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003745 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003746
Chris Lattner27a6c732007-11-24 07:07:01 +00003747 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003749
Chris Lattner27a6c732007-11-24 07:07:01 +00003750 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003751 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003752}
3753
Bob Wilson5bafff32009-06-22 23:27:02 +00003754static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3755 SDValue TmpOp0, TmpOp1;
3756 bool Invert = false;
3757 bool Swap = false;
3758 unsigned Opc = 0;
3759
3760 SDValue Op0 = Op.getOperand(0);
3761 SDValue Op1 = Op.getOperand(1);
3762 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003763 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003764 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3765 DebugLoc dl = Op.getDebugLoc();
3766
3767 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3768 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003769 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003770 case ISD::SETUNE:
3771 case ISD::SETNE: Invert = true; // Fallthrough
3772 case ISD::SETOEQ:
3773 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3774 case ISD::SETOLT:
3775 case ISD::SETLT: Swap = true; // Fallthrough
3776 case ISD::SETOGT:
3777 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3778 case ISD::SETOLE:
3779 case ISD::SETLE: Swap = true; // Fallthrough
3780 case ISD::SETOGE:
3781 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3782 case ISD::SETUGE: Swap = true; // Fallthrough
3783 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3784 case ISD::SETUGT: Swap = true; // Fallthrough
3785 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3786 case ISD::SETUEQ: Invert = true; // Fallthrough
3787 case ISD::SETONE:
3788 // Expand this to (OLT | OGT).
3789 TmpOp0 = Op0;
3790 TmpOp1 = Op1;
3791 Opc = ISD::OR;
3792 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3793 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3794 break;
3795 case ISD::SETUO: Invert = true; // Fallthrough
3796 case ISD::SETO:
3797 // Expand this to (OLT | OGE).
3798 TmpOp0 = Op0;
3799 TmpOp1 = Op1;
3800 Opc = ISD::OR;
3801 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3802 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3803 break;
3804 }
3805 } else {
3806 // Integer comparisons.
3807 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003808 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003809 case ISD::SETNE: Invert = true;
3810 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3811 case ISD::SETLT: Swap = true;
3812 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3813 case ISD::SETLE: Swap = true;
3814 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3815 case ISD::SETULT: Swap = true;
3816 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3817 case ISD::SETULE: Swap = true;
3818 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3819 }
3820
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003821 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003822 if (Opc == ARMISD::VCEQ) {
3823
3824 SDValue AndOp;
3825 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3826 AndOp = Op0;
3827 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3828 AndOp = Op1;
3829
3830 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003831 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003832 AndOp = AndOp.getOperand(0);
3833
3834 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3835 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003836 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3837 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003838 Invert = !Invert;
3839 }
3840 }
3841 }
3842
3843 if (Swap)
3844 std::swap(Op0, Op1);
3845
Owen Andersonc24cb352010-11-08 23:21:22 +00003846 // If one of the operands is a constant vector zero, attempt to fold the
3847 // comparison to a specialized compare-against-zero form.
3848 SDValue SingleOp;
3849 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3850 SingleOp = Op0;
3851 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3852 if (Opc == ARMISD::VCGE)
3853 Opc = ARMISD::VCLEZ;
3854 else if (Opc == ARMISD::VCGT)
3855 Opc = ARMISD::VCLTZ;
3856 SingleOp = Op1;
3857 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003858
Owen Andersonc24cb352010-11-08 23:21:22 +00003859 SDValue Result;
3860 if (SingleOp.getNode()) {
3861 switch (Opc) {
3862 case ARMISD::VCEQ:
3863 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3864 case ARMISD::VCGE:
3865 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3866 case ARMISD::VCLEZ:
3867 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3868 case ARMISD::VCGT:
3869 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3870 case ARMISD::VCLTZ:
3871 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3872 default:
3873 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3874 }
3875 } else {
3876 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3877 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003878
3879 if (Invert)
3880 Result = DAG.getNOT(dl, Result, VT);
3881
3882 return Result;
3883}
3884
Bob Wilsond3c42842010-06-14 22:19:57 +00003885/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3886/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003887/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003888static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3889 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003890 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003891 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003892
Bob Wilson827b2102010-06-15 19:05:35 +00003893 // SplatBitSize is set to the smallest size that splats the vector, so a
3894 // zero vector will always have SplatBitSize == 8. However, NEON modified
3895 // immediate instructions others than VMOV do not support the 8-bit encoding
3896 // of a zero vector, and the default encoding of zero is supposed to be the
3897 // 32-bit version.
3898 if (SplatBits == 0)
3899 SplatBitSize = 32;
3900
Bob Wilson5bafff32009-06-22 23:27:02 +00003901 switch (SplatBitSize) {
3902 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003903 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003904 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003905 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003906 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003907 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003908 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003909 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003910 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003911
3912 case 16:
3913 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003914 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003915 if ((SplatBits & ~0xff) == 0) {
3916 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003917 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003918 Imm = SplatBits;
3919 break;
3920 }
3921 if ((SplatBits & ~0xff00) == 0) {
3922 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003923 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003924 Imm = SplatBits >> 8;
3925 break;
3926 }
3927 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003928
3929 case 32:
3930 // NEON's 32-bit VMOV supports splat values where:
3931 // * only one byte is nonzero, or
3932 // * the least significant byte is 0xff and the second byte is nonzero, or
3933 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003934 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003935 if ((SplatBits & ~0xff) == 0) {
3936 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003937 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003938 Imm = SplatBits;
3939 break;
3940 }
3941 if ((SplatBits & ~0xff00) == 0) {
3942 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003943 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003944 Imm = SplatBits >> 8;
3945 break;
3946 }
3947 if ((SplatBits & ~0xff0000) == 0) {
3948 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003949 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003950 Imm = SplatBits >> 16;
3951 break;
3952 }
3953 if ((SplatBits & ~0xff000000) == 0) {
3954 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003955 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003956 Imm = SplatBits >> 24;
3957 break;
3958 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003959
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003960 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3961 if (type == OtherModImm) return SDValue();
3962
Bob Wilson5bafff32009-06-22 23:27:02 +00003963 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003964 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3965 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003966 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003967 Imm = SplatBits >> 8;
3968 SplatBits |= 0xff;
3969 break;
3970 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003971
3972 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003973 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3974 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003975 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003976 Imm = SplatBits >> 16;
3977 SplatBits |= 0xffff;
3978 break;
3979 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003980
3981 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3982 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3983 // VMOV.I32. A (very) minor optimization would be to replicate the value
3984 // and fall through here to test for a valid 64-bit splat. But, then the
3985 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003986 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003987
3988 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003989 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003990 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003991 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003992 uint64_t BitMask = 0xff;
3993 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003994 unsigned ImmMask = 1;
3995 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003996 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003997 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003998 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003999 Imm |= ImmMask;
4000 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00004001 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00004002 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004003 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004004 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00004005 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00004006 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004007 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004008 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00004009 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00004010 break;
4011 }
4012
Bob Wilson1a913ed2010-06-11 21:34:50 +00004013 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00004014 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00004015 }
4016
Bob Wilsoncba270d2010-07-13 21:16:48 +00004017 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4018 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00004019}
4020
Lang Hamesc0a9f822012-03-29 21:56:11 +00004021SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4022 const ARMSubtarget *ST) const {
4023 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
4024 return SDValue();
4025
4026 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4027 assert(Op.getValueType() == MVT::f32 &&
4028 "ConstantFP custom lowering should only occur for f32.");
4029
4030 // Try splatting with a VMOV.f32...
4031 APFloat FPVal = CFP->getValueAPF();
4032 int ImmVal = ARM_AM::getFP32Imm(FPVal);
4033 if (ImmVal != -1) {
4034 DebugLoc DL = Op.getDebugLoc();
4035 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4036 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4037 NewVal);
4038 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4039 DAG.getConstant(0, MVT::i32));
4040 }
4041
4042 // If that fails, try a VMOV.i32
4043 EVT VMovVT;
4044 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
4045 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
4046 VMOVModImm);
4047 if (NewVal != SDValue()) {
4048 DebugLoc DL = Op.getDebugLoc();
4049 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4050 NewVal);
4051 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4052 VecConstant);
4053 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4054 DAG.getConstant(0, MVT::i32));
4055 }
4056
4057 // Finally, try a VMVN.i32
4058 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
4059 VMVNModImm);
4060 if (NewVal != SDValue()) {
4061 DebugLoc DL = Op.getDebugLoc();
4062 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4063 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4064 VecConstant);
4065 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4066 DAG.getConstant(0, MVT::i32));
4067 }
4068
4069 return SDValue();
4070}
4071
Quentin Colombet43934ae2012-11-02 21:32:17 +00004072// check if an VEXT instruction can handle the shuffle mask when the
4073// vector sources of the shuffle are the same.
4074static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4075 unsigned NumElts = VT.getVectorNumElements();
4076
4077 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4078 if (M[0] < 0)
4079 return false;
4080
4081 Imm = M[0];
4082
4083 // If this is a VEXT shuffle, the immediate value is the index of the first
4084 // element. The other shuffle indices must be the successive elements after
4085 // the first one.
4086 unsigned ExpectedElt = Imm;
4087 for (unsigned i = 1; i < NumElts; ++i) {
4088 // Increment the expected index. If it wraps around, just follow it
4089 // back to index zero and keep going.
4090 ++ExpectedElt;
4091 if (ExpectedElt == NumElts)
4092 ExpectedElt = 0;
4093
4094 if (M[i] < 0) continue; // ignore UNDEF indices
4095 if (ExpectedElt != static_cast<unsigned>(M[i]))
4096 return false;
4097 }
4098
4099 return true;
4100}
4101
Lang Hamesc0a9f822012-03-29 21:56:11 +00004102
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004103static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004104 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004105 unsigned NumElts = VT.getVectorNumElements();
4106 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004107
4108 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4109 if (M[0] < 0)
4110 return false;
4111
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004112 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004113
4114 // If this is a VEXT shuffle, the immediate value is the index of the first
4115 // element. The other shuffle indices must be the successive elements after
4116 // the first one.
4117 unsigned ExpectedElt = Imm;
4118 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004119 // Increment the expected index. If it wraps around, it may still be
4120 // a VEXT but the source vectors must be swapped.
4121 ExpectedElt += 1;
4122 if (ExpectedElt == NumElts * 2) {
4123 ExpectedElt = 0;
4124 ReverseVEXT = true;
4125 }
4126
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004127 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004128 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004129 return false;
4130 }
4131
4132 // Adjust the index value if the source operands will be swapped.
4133 if (ReverseVEXT)
4134 Imm -= NumElts;
4135
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004136 return true;
4137}
4138
Bob Wilson8bb9e482009-07-26 00:39:34 +00004139/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4140/// instruction with the specified blocksize. (The order of the elements
4141/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004142static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004143 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4144 "Only possible block sizes for VREV are: 16, 32, 64");
4145
Bob Wilson8bb9e482009-07-26 00:39:34 +00004146 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004147 if (EltSz == 64)
4148 return false;
4149
4150 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004151 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004152 // If the first shuffle index is UNDEF, be optimistic.
4153 if (M[0] < 0)
4154 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004155
4156 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4157 return false;
4158
4159 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004160 if (M[i] < 0) continue; // ignore UNDEF indices
4161 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004162 return false;
4163 }
4164
4165 return true;
4166}
4167
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004168static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004169 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4170 // range, then 0 is placed into the resulting vector. So pretty much any mask
4171 // of 8 elements can work here.
4172 return VT == MVT::v8i8 && M.size() == 8;
4173}
4174
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004175static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004176 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4177 if (EltSz == 64)
4178 return false;
4179
Bob Wilsonc692cb72009-08-21 20:54:19 +00004180 unsigned NumElts = VT.getVectorNumElements();
4181 WhichResult = (M[0] == 0 ? 0 : 1);
4182 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004183 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4184 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004185 return false;
4186 }
4187 return true;
4188}
4189
Bob Wilson324f4f12009-12-03 06:40:55 +00004190/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4191/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4192/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004193static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004194 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4195 if (EltSz == 64)
4196 return false;
4197
4198 unsigned NumElts = VT.getVectorNumElements();
4199 WhichResult = (M[0] == 0 ? 0 : 1);
4200 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004201 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4202 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004203 return false;
4204 }
4205 return true;
4206}
4207
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004208static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004209 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4210 if (EltSz == 64)
4211 return false;
4212
Bob Wilsonc692cb72009-08-21 20:54:19 +00004213 unsigned NumElts = VT.getVectorNumElements();
4214 WhichResult = (M[0] == 0 ? 0 : 1);
4215 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004216 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004217 if ((unsigned) M[i] != 2 * i + WhichResult)
4218 return false;
4219 }
4220
4221 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004222 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004223 return false;
4224
4225 return true;
4226}
4227
Bob Wilson324f4f12009-12-03 06:40:55 +00004228/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4229/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4230/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004231static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004232 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4233 if (EltSz == 64)
4234 return false;
4235
4236 unsigned Half = VT.getVectorNumElements() / 2;
4237 WhichResult = (M[0] == 0 ? 0 : 1);
4238 for (unsigned j = 0; j != 2; ++j) {
4239 unsigned Idx = WhichResult;
4240 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004241 int MIdx = M[i + j * Half];
4242 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004243 return false;
4244 Idx += 2;
4245 }
4246 }
4247
4248 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4249 if (VT.is64BitVector() && EltSz == 32)
4250 return false;
4251
4252 return true;
4253}
4254
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004255static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004256 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4257 if (EltSz == 64)
4258 return false;
4259
Bob Wilsonc692cb72009-08-21 20:54:19 +00004260 unsigned NumElts = VT.getVectorNumElements();
4261 WhichResult = (M[0] == 0 ? 0 : 1);
4262 unsigned Idx = WhichResult * NumElts / 2;
4263 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004264 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4265 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004266 return false;
4267 Idx += 1;
4268 }
4269
4270 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004271 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004272 return false;
4273
4274 return true;
4275}
4276
Bob Wilson324f4f12009-12-03 06:40:55 +00004277/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4278/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4279/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004280static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004281 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4282 if (EltSz == 64)
4283 return false;
4284
4285 unsigned NumElts = VT.getVectorNumElements();
4286 WhichResult = (M[0] == 0 ? 0 : 1);
4287 unsigned Idx = WhichResult * NumElts / 2;
4288 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004289 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4290 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004291 return false;
4292 Idx += 1;
4293 }
4294
4295 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4296 if (VT.is64BitVector() && EltSz == 32)
4297 return false;
4298
4299 return true;
4300}
4301
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004302/// \return true if this is a reverse operation on an vector.
4303static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4304 unsigned NumElts = VT.getVectorNumElements();
4305 // Make sure the mask has the right size.
4306 if (NumElts != M.size())
4307 return false;
4308
4309 // Look for <15, ..., 3, -1, 1, 0>.
4310 for (unsigned i = 0; i != NumElts; ++i)
4311 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4312 return false;
4313
4314 return true;
4315}
4316
Dale Johannesenf630c712010-07-29 20:10:08 +00004317// If N is an integer constant that can be moved into a register in one
4318// instruction, return an SDValue of such a constant (will become a MOV
4319// instruction). Otherwise return null.
4320static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4321 const ARMSubtarget *ST, DebugLoc dl) {
4322 uint64_t Val;
4323 if (!isa<ConstantSDNode>(N))
4324 return SDValue();
4325 Val = cast<ConstantSDNode>(N)->getZExtValue();
4326
4327 if (ST->isThumb1Only()) {
4328 if (Val <= 255 || ~Val <= 255)
4329 return DAG.getConstant(Val, MVT::i32);
4330 } else {
4331 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4332 return DAG.getConstant(Val, MVT::i32);
4333 }
4334 return SDValue();
4335}
4336
Bob Wilson5bafff32009-06-22 23:27:02 +00004337// If this is a case we can't handle, return null and let the default
4338// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004339SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4340 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004341 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004342 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004343 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004344
4345 APInt SplatBits, SplatUndef;
4346 unsigned SplatBitSize;
4347 bool HasAnyUndefs;
4348 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004349 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004350 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004351 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004352 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004353 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004354 DAG, VmovVT, VT.is128BitVector(),
4355 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004356 if (Val.getNode()) {
4357 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004358 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004359 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004360
4361 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004362 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004363 Val = isNEONModifiedImm(NegatedImm,
4364 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004365 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004366 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004367 if (Val.getNode()) {
4368 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004369 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004370 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004371
4372 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004373 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004374 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004375 if (ImmVal != -1) {
4376 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4377 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4378 }
4379 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004380 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004381 }
4382
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004383 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004384 //
4385 // As an optimisation, even if more than one value is used it may be more
4386 // profitable to splat with one value then change some lanes.
4387 //
4388 // Heuristically we decide to do this if the vector has a "dominant" value,
4389 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004390 unsigned NumElts = VT.getVectorNumElements();
4391 bool isOnlyLowElement = true;
4392 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004393 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004394 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004395
4396 // Map of the number of times a particular SDValue appears in the
4397 // element list.
James Molloy95154342012-09-06 10:32:08 +00004398 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004399 SDValue Value;
4400 for (unsigned i = 0; i < NumElts; ++i) {
4401 SDValue V = Op.getOperand(i);
4402 if (V.getOpcode() == ISD::UNDEF)
4403 continue;
4404 if (i > 0)
4405 isOnlyLowElement = false;
4406 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4407 isConstant = false;
4408
James Molloyba8562a2012-09-06 09:55:02 +00004409 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004410 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004411
4412 // Is this value dominant? (takes up more than half of the lanes)
4413 if (++Count > (NumElts / 2)) {
4414 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004415 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004416 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004417 }
James Molloyba8562a2012-09-06 09:55:02 +00004418 if (ValueCounts.size() != 1)
4419 usesOnlyOneValue = false;
4420 if (!Value.getNode() && ValueCounts.size() > 0)
4421 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004422
James Molloyba8562a2012-09-06 09:55:02 +00004423 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004424 return DAG.getUNDEF(VT);
4425
4426 if (isOnlyLowElement)
4427 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4428
Dale Johannesenf630c712010-07-29 20:10:08 +00004429 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4430
Dale Johannesen575cd142010-10-19 20:00:17 +00004431 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4432 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004433 if (hasDominantValue && EltSize <= 32) {
4434 if (!isConstant) {
4435 SDValue N;
4436
4437 // If we are VDUPing a value that comes directly from a vector, that will
4438 // cause an unnecessary move to and from a GPR, where instead we could
4439 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004440 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4441 // We need to create a new undef vector to use for the VDUPLANE if the
4442 // size of the vector from which we get the value is different than the
4443 // size of the vector that we need to create. We will insert the element
4444 // such that the register coalescer will remove unnecessary copies.
4445 if (VT != Value->getOperand(0).getValueType()) {
4446 ConstantSDNode *constIndex;
4447 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4448 assert(constIndex && "The index is not a constant!");
4449 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4450 VT.getVectorNumElements();
4451 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4452 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4453 Value, DAG.getConstant(index, MVT::i32)),
4454 DAG.getConstant(index, MVT::i32));
4455 } else {
4456 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004457 Value->getOperand(0), Value->getOperand(1));
Silviu Barangabb1078e2012-10-15 09:41:32 +00004458 }
4459 }
James Molloyba8562a2012-09-06 09:55:02 +00004460 else
4461 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4462
4463 if (!usesOnlyOneValue) {
4464 // The dominant value was splatted as 'N', but we now have to insert
4465 // all differing elements.
4466 for (unsigned I = 0; I < NumElts; ++I) {
4467 if (Op.getOperand(I) == Value)
4468 continue;
4469 SmallVector<SDValue, 3> Ops;
4470 Ops.push_back(N);
4471 Ops.push_back(Op.getOperand(I));
4472 Ops.push_back(DAG.getConstant(I, MVT::i32));
4473 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4474 }
4475 }
4476 return N;
4477 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004478 if (VT.getVectorElementType().isFloatingPoint()) {
4479 SmallVector<SDValue, 8> Ops;
4480 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004481 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004482 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004483 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4484 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004485 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4486 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004487 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004488 }
James Molloyba8562a2012-09-06 09:55:02 +00004489 if (usesOnlyOneValue) {
4490 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4491 if (isConstant && Val.getNode())
4492 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4493 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004494 }
4495
4496 // If all elements are constants and the case above didn't get hit, fall back
4497 // to the default expansion, which will generate a load from the constant
4498 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004499 if (isConstant)
4500 return SDValue();
4501
Bob Wilson11a1dff2011-01-07 21:37:30 +00004502 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4503 if (NumElts >= 4) {
4504 SDValue shuffle = ReconstructShuffle(Op, DAG);
4505 if (shuffle != SDValue())
4506 return shuffle;
4507 }
4508
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004509 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004510 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4511 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004512 if (EltSize >= 32) {
4513 // Do the expansion with floating-point types, since that is what the VFP
4514 // registers are defined to use, and since i64 is not legal.
4515 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4516 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004517 SmallVector<SDValue, 8> Ops;
4518 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004519 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004520 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004521 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004522 }
4523
4524 return SDValue();
4525}
4526
Bob Wilson11a1dff2011-01-07 21:37:30 +00004527// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004528// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004529SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4530 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004531 DebugLoc dl = Op.getDebugLoc();
4532 EVT VT = Op.getValueType();
4533 unsigned NumElts = VT.getVectorNumElements();
4534
4535 SmallVector<SDValue, 2> SourceVecs;
4536 SmallVector<unsigned, 2> MinElts;
4537 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004538
Bob Wilson11a1dff2011-01-07 21:37:30 +00004539 for (unsigned i = 0; i < NumElts; ++i) {
4540 SDValue V = Op.getOperand(i);
4541 if (V.getOpcode() == ISD::UNDEF)
4542 continue;
4543 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4544 // A shuffle can only come from building a vector from various
4545 // elements of other vectors.
4546 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004547 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4548 VT.getVectorElementType()) {
4549 // This code doesn't know how to handle shuffles where the vector
4550 // element types do not match (this happens because type legalization
4551 // promotes the return type of EXTRACT_VECTOR_ELT).
4552 // FIXME: It might be appropriate to extend this code to handle
4553 // mismatched types.
4554 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004555 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004556
Bob Wilson11a1dff2011-01-07 21:37:30 +00004557 // Record this extraction against the appropriate vector if possible...
4558 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004559 // If the element number isn't a constant, we can't effectively
4560 // analyze what's going on.
4561 if (!isa<ConstantSDNode>(V.getOperand(1)))
4562 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004563 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4564 bool FoundSource = false;
4565 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4566 if (SourceVecs[j] == SourceVec) {
4567 if (MinElts[j] > EltNo)
4568 MinElts[j] = EltNo;
4569 if (MaxElts[j] < EltNo)
4570 MaxElts[j] = EltNo;
4571 FoundSource = true;
4572 break;
4573 }
4574 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004575
Bob Wilson11a1dff2011-01-07 21:37:30 +00004576 // Or record a new source if not...
4577 if (!FoundSource) {
4578 SourceVecs.push_back(SourceVec);
4579 MinElts.push_back(EltNo);
4580 MaxElts.push_back(EltNo);
4581 }
4582 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004583
Bob Wilson11a1dff2011-01-07 21:37:30 +00004584 // Currently only do something sane when at most two source vectors
4585 // involved.
4586 if (SourceVecs.size() > 2)
4587 return SDValue();
4588
4589 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4590 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004591
Bob Wilson11a1dff2011-01-07 21:37:30 +00004592 // This loop extracts the usage patterns of the source vectors
4593 // and prepares appropriate SDValues for a shuffle if possible.
4594 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4595 if (SourceVecs[i].getValueType() == VT) {
4596 // No VEXT necessary
4597 ShuffleSrcs[i] = SourceVecs[i];
4598 VEXTOffsets[i] = 0;
4599 continue;
4600 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4601 // It probably isn't worth padding out a smaller vector just to
4602 // break it down again in a shuffle.
4603 return SDValue();
4604 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004605
Bob Wilson11a1dff2011-01-07 21:37:30 +00004606 // Since only 64-bit and 128-bit vectors are legal on ARM and
4607 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004608 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4609 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004610
Bob Wilson11a1dff2011-01-07 21:37:30 +00004611 if (MaxElts[i] - MinElts[i] >= NumElts) {
4612 // Span too large for a VEXT to cope
4613 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004614 }
4615
Bob Wilson11a1dff2011-01-07 21:37:30 +00004616 if (MinElts[i] >= NumElts) {
4617 // The extraction can just take the second half
4618 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004619 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4620 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004621 DAG.getIntPtrConstant(NumElts));
4622 } else if (MaxElts[i] < NumElts) {
4623 // The extraction can just take the first half
4624 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004625 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4626 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004627 DAG.getIntPtrConstant(0));
4628 } else {
4629 // An actual VEXT is needed
4630 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004631 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4632 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004633 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004634 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4635 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004636 DAG.getIntPtrConstant(NumElts));
4637 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4638 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4639 }
4640 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004641
Bob Wilson11a1dff2011-01-07 21:37:30 +00004642 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004643
Bob Wilson11a1dff2011-01-07 21:37:30 +00004644 for (unsigned i = 0; i < NumElts; ++i) {
4645 SDValue Entry = Op.getOperand(i);
4646 if (Entry.getOpcode() == ISD::UNDEF) {
4647 Mask.push_back(-1);
4648 continue;
4649 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004650
Bob Wilson11a1dff2011-01-07 21:37:30 +00004651 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004652 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4653 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004654 if (ExtractVec == SourceVecs[0]) {
4655 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4656 } else {
4657 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4658 }
4659 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004660
Bob Wilson11a1dff2011-01-07 21:37:30 +00004661 // Final check before we try to produce nonsense...
4662 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004663 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4664 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004665
Bob Wilson11a1dff2011-01-07 21:37:30 +00004666 return SDValue();
4667}
4668
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004669/// isShuffleMaskLegal - Targets can use this to indicate that they only
4670/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4671/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4672/// are assumed to be legal.
4673bool
4674ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4675 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004676 if (VT.getVectorNumElements() == 4 &&
4677 (VT.is128BitVector() || VT.is64BitVector())) {
4678 unsigned PFIndexes[4];
4679 for (unsigned i = 0; i != 4; ++i) {
4680 if (M[i] < 0)
4681 PFIndexes[i] = 8;
4682 else
4683 PFIndexes[i] = M[i];
4684 }
4685
4686 // Compute the index in the perfect shuffle table.
4687 unsigned PFTableIndex =
4688 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4689 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4690 unsigned Cost = (PFEntry >> 30);
4691
4692 if (Cost <= 4)
4693 return true;
4694 }
4695
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004696 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004697 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004698
Bob Wilson53dd2452010-06-07 23:53:38 +00004699 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4700 return (EltSize >= 32 ||
4701 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004702 isVREVMask(M, VT, 64) ||
4703 isVREVMask(M, VT, 32) ||
4704 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004705 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004706 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004707 isVTRNMask(M, VT, WhichResult) ||
4708 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004709 isVZIPMask(M, VT, WhichResult) ||
4710 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4711 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004712 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
4713 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004714}
4715
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004716/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4717/// the specified operations to build the shuffle.
4718static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4719 SDValue RHS, SelectionDAG &DAG,
4720 DebugLoc dl) {
4721 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4722 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4723 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4724
4725 enum {
4726 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4727 OP_VREV,
4728 OP_VDUP0,
4729 OP_VDUP1,
4730 OP_VDUP2,
4731 OP_VDUP3,
4732 OP_VEXT1,
4733 OP_VEXT2,
4734 OP_VEXT3,
4735 OP_VUZPL, // VUZP, left result
4736 OP_VUZPR, // VUZP, right result
4737 OP_VZIPL, // VZIP, left result
4738 OP_VZIPR, // VZIP, right result
4739 OP_VTRNL, // VTRN, left result
4740 OP_VTRNR // VTRN, right result
4741 };
4742
4743 if (OpNum == OP_COPY) {
4744 if (LHSID == (1*9+2)*9+3) return LHS;
4745 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4746 return RHS;
4747 }
4748
4749 SDValue OpLHS, OpRHS;
4750 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4751 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4752 EVT VT = OpLHS.getValueType();
4753
4754 switch (OpNum) {
4755 default: llvm_unreachable("Unknown shuffle opcode!");
4756 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004757 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004758 if (VT.getVectorElementType() == MVT::i32 ||
4759 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004760 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4761 // vrev <4 x i16> -> VREV32
4762 if (VT.getVectorElementType() == MVT::i16)
4763 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4764 // vrev <4 x i8> -> VREV16
4765 assert(VT.getVectorElementType() == MVT::i8);
4766 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004767 case OP_VDUP0:
4768 case OP_VDUP1:
4769 case OP_VDUP2:
4770 case OP_VDUP3:
4771 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004772 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004773 case OP_VEXT1:
4774 case OP_VEXT2:
4775 case OP_VEXT3:
4776 return DAG.getNode(ARMISD::VEXT, dl, VT,
4777 OpLHS, OpRHS,
4778 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4779 case OP_VUZPL:
4780 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004781 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004782 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4783 case OP_VZIPL:
4784 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004785 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004786 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4787 case OP_VTRNL:
4788 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004789 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4790 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004791 }
4792}
4793
Bill Wendling69a05a72011-03-14 23:02:38 +00004794static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004795 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004796 SelectionDAG &DAG) {
4797 // Check to see if we can use the VTBL instruction.
4798 SDValue V1 = Op.getOperand(0);
4799 SDValue V2 = Op.getOperand(1);
4800 DebugLoc DL = Op.getDebugLoc();
4801
4802 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004803 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004804 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4805 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4806
4807 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4808 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4809 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4810 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004811
Owen Anderson76706012011-04-05 21:48:57 +00004812 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004813 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4814 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004815}
4816
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004817static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
4818 SelectionDAG &DAG) {
4819 DebugLoc DL = Op.getDebugLoc();
4820 SDValue OpLHS = Op.getOperand(0);
4821 EVT VT = OpLHS.getValueType();
4822
4823 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
4824 "Expect an v8i16/v16i8 type");
4825 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
4826 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
4827 // extract the first 8 bytes into the top double word and the last 8 bytes
4828 // into the bottom double word. The v8i16 case is similar.
4829 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
4830 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
4831 DAG.getConstant(ExtractNum, MVT::i32));
4832}
4833
Bob Wilson5bafff32009-06-22 23:27:02 +00004834static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004835 SDValue V1 = Op.getOperand(0);
4836 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004837 DebugLoc dl = Op.getDebugLoc();
4838 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004839 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004840
Bob Wilson28865062009-08-13 02:13:04 +00004841 // Convert shuffles that are directly supported on NEON to target-specific
4842 // DAG nodes, instead of keeping them as shuffles and matching them again
4843 // during code selection. This is more efficient and avoids the possibility
4844 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004845 // FIXME: floating-point vectors should be canonicalized to integer vectors
4846 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004847 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004848
Bob Wilson53dd2452010-06-07 23:53:38 +00004849 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4850 if (EltSize <= 32) {
4851 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4852 int Lane = SVN->getSplatIndex();
4853 // If this is undef splat, generate it via "just" vdup, if possible.
4854 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004855
Dan Gohman65fd6562011-11-03 21:49:52 +00004856 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004857 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4858 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4859 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004860 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4861 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4862 // reaches it).
4863 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4864 !isa<ConstantSDNode>(V1.getOperand(0))) {
4865 bool IsScalarToVector = true;
4866 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4867 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4868 IsScalarToVector = false;
4869 break;
4870 }
4871 if (IsScalarToVector)
4872 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4873 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004874 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4875 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004876 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004877
4878 bool ReverseVEXT;
4879 unsigned Imm;
4880 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4881 if (ReverseVEXT)
4882 std::swap(V1, V2);
4883 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4884 DAG.getConstant(Imm, MVT::i32));
4885 }
4886
4887 if (isVREVMask(ShuffleMask, VT, 64))
4888 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4889 if (isVREVMask(ShuffleMask, VT, 32))
4890 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4891 if (isVREVMask(ShuffleMask, VT, 16))
4892 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4893
Quentin Colombet43934ae2012-11-02 21:32:17 +00004894 if (V2->getOpcode() == ISD::UNDEF &&
4895 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4896 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4897 DAG.getConstant(Imm, MVT::i32));
4898 }
4899
Bob Wilson53dd2452010-06-07 23:53:38 +00004900 // Check for Neon shuffles that modify both input vectors in place.
4901 // If both results are used, i.e., if there are two shuffles with the same
4902 // source operands and with masks corresponding to both results of one of
4903 // these operations, DAG memoization will ensure that a single node is
4904 // used for both shuffles.
4905 unsigned WhichResult;
4906 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4907 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4908 V1, V2).getValue(WhichResult);
4909 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4910 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4911 V1, V2).getValue(WhichResult);
4912 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4913 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4914 V1, V2).getValue(WhichResult);
4915
4916 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4917 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4918 V1, V1).getValue(WhichResult);
4919 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4920 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4921 V1, V1).getValue(WhichResult);
4922 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4923 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4924 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004925 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004926
Bob Wilsonc692cb72009-08-21 20:54:19 +00004927 // If the shuffle is not directly supported and it has 4 elements, use
4928 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004929 unsigned NumElts = VT.getVectorNumElements();
4930 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004931 unsigned PFIndexes[4];
4932 for (unsigned i = 0; i != 4; ++i) {
4933 if (ShuffleMask[i] < 0)
4934 PFIndexes[i] = 8;
4935 else
4936 PFIndexes[i] = ShuffleMask[i];
4937 }
4938
4939 // Compute the index in the perfect shuffle table.
4940 unsigned PFTableIndex =
4941 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004942 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4943 unsigned Cost = (PFEntry >> 30);
4944
4945 if (Cost <= 4)
4946 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4947 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004948
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004949 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004950 if (EltSize >= 32) {
4951 // Do the expansion with floating-point types, since that is what the VFP
4952 // registers are defined to use, and since i64 is not legal.
4953 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4954 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004955 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4956 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004957 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004958 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004959 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004960 Ops.push_back(DAG.getUNDEF(EltVT));
4961 else
4962 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4963 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4964 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4965 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004966 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004967 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004968 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004969 }
4970
Arnold Schwaighoferd9316da2013-02-12 01:58:32 +00004971 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
4972 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
4973
Bill Wendling69a05a72011-03-14 23:02:38 +00004974 if (VT == MVT::v8i8) {
4975 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4976 if (NewOp.getNode())
4977 return NewOp;
4978 }
4979
Bob Wilson22cac0d2009-08-14 05:16:33 +00004980 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004981}
4982
Eli Friedman5c89cb82011-10-24 23:08:52 +00004983static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4984 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4985 SDValue Lane = Op.getOperand(2);
4986 if (!isa<ConstantSDNode>(Lane))
4987 return SDValue();
4988
4989 return Op;
4990}
4991
Bob Wilson5bafff32009-06-22 23:27:02 +00004992static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004993 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004994 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004995 if (!isa<ConstantSDNode>(Lane))
4996 return SDValue();
4997
4998 SDValue Vec = Op.getOperand(0);
4999 if (Op.getValueType() == MVT::i32 &&
5000 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
5001 DebugLoc dl = Op.getDebugLoc();
5002 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5003 }
5004
5005 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00005006}
5007
Bob Wilsona6d65862009-08-03 20:36:38 +00005008static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5009 // The only time a CONCAT_VECTORS operation can have legal types is when
5010 // two 64-bit vectors are concatenated to a 128-bit vector.
5011 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5012 "unexpected CONCAT_VECTORS");
5013 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005014 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00005015 SDValue Op0 = Op.getOperand(0);
5016 SDValue Op1 = Op.getOperand(1);
5017 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005018 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005019 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00005020 DAG.getIntPtrConstant(0));
5021 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00005022 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005023 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00005024 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005025 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00005026}
5027
Bob Wilson626613d2010-11-23 19:38:38 +00005028/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5029/// element has been zero/sign-extended, depending on the isSigned parameter,
5030/// from an integer type half its size.
5031static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5032 bool isSigned) {
5033 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5034 EVT VT = N->getValueType(0);
5035 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5036 SDNode *BVN = N->getOperand(0).getNode();
5037 if (BVN->getValueType(0) != MVT::v4i32 ||
5038 BVN->getOpcode() != ISD::BUILD_VECTOR)
5039 return false;
5040 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5041 unsigned HiElt = 1 - LoElt;
5042 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5043 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5044 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5045 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5046 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5047 return false;
5048 if (isSigned) {
5049 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5050 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5051 return true;
5052 } else {
5053 if (Hi0->isNullValue() && Hi1->isNullValue())
5054 return true;
5055 }
5056 return false;
5057 }
5058
5059 if (N->getOpcode() != ISD::BUILD_VECTOR)
5060 return false;
5061
5062 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5063 SDNode *Elt = N->getOperand(i).getNode();
5064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5065 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5066 unsigned HalfSize = EltSize / 2;
5067 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00005068 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005069 return false;
5070 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00005071 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005072 return false;
5073 }
5074 continue;
5075 }
5076 return false;
5077 }
5078
5079 return true;
5080}
5081
5082/// isSignExtended - Check if a node is a vector value that is sign-extended
5083/// or a constant BUILD_VECTOR with sign-extended elements.
5084static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5085 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5086 return true;
5087 if (isExtendedBUILD_VECTOR(N, DAG, true))
5088 return true;
5089 return false;
5090}
5091
5092/// isZeroExtended - Check if a node is a vector value that is zero-extended
5093/// or a constant BUILD_VECTOR with zero-extended elements.
5094static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5095 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5096 return true;
5097 if (isExtendedBUILD_VECTOR(N, DAG, false))
5098 return true;
5099 return false;
5100}
5101
Sebastian Popcb495302012-11-30 19:08:04 +00005102/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5103/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5104/// We insert the required extension here to get the vector to fill a D register.
5105static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5106 const EVT &OrigTy,
5107 const EVT &ExtTy,
5108 unsigned ExtOpcode) {
5109 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5110 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5111 // 64-bits we need to insert a new extension so that it will be 64-bits.
5112 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5113 if (OrigTy.getSizeInBits() >= 64)
5114 return N;
5115
5116 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5117 MVT::SimpleValueType OrigSimpleTy = OrigTy.getSimpleVT().SimpleTy;
5118 EVT NewVT;
5119 switch (OrigSimpleTy) {
5120 default: llvm_unreachable("Unexpected Orig Vector Type");
5121 case MVT::v2i8:
5122 case MVT::v2i16:
5123 NewVT = MVT::v2i32;
5124 break;
5125 case MVT::v4i8:
5126 NewVT = MVT::v4i16;
5127 break;
5128 }
5129 return DAG.getNode(ExtOpcode, N->getDebugLoc(), NewVT, N);
5130}
5131
5132/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5133/// does not do any sign/zero extension. If the original vector is less
5134/// than 64 bits, an appropriate extension will be added after the load to
5135/// reach a total size of 64 bits. We have to add the extension separately
5136/// because ARM does not have a sign/zero extending load for vectors.
5137static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5138 SDValue NonExtendingLoad =
5139 DAG.getLoad(LD->getMemoryVT(), LD->getDebugLoc(), LD->getChain(),
5140 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5141 LD->isNonTemporal(), LD->isInvariant(),
5142 LD->getAlignment());
5143 unsigned ExtOp = 0;
5144 switch (LD->getExtensionType()) {
5145 default: llvm_unreachable("Unexpected LoadExtType");
5146 case ISD::EXTLOAD:
5147 case ISD::SEXTLOAD: ExtOp = ISD::SIGN_EXTEND; break;
5148 case ISD::ZEXTLOAD: ExtOp = ISD::ZERO_EXTEND; break;
5149 }
5150 MVT::SimpleValueType MemType = LD->getMemoryVT().getSimpleVT().SimpleTy;
5151 MVT::SimpleValueType ExtType = LD->getValueType(0).getSimpleVT().SimpleTy;
5152 return AddRequiredExtensionForVMULL(NonExtendingLoad, DAG,
5153 MemType, ExtType, ExtOp);
5154}
5155
5156/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5157/// extending load, or BUILD_VECTOR with extended elements, return the
5158/// unextended value. The unextended vector should be 64 bits so that it can
5159/// be used as an operand to a VMULL instruction. If the original vector size
5160/// before extension is less than 64 bits we add a an extension to resize
5161/// the vector to 64 bits.
5162static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005163 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popcb495302012-11-30 19:08:04 +00005164 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5165 N->getOperand(0)->getValueType(0),
5166 N->getValueType(0),
5167 N->getOpcode());
5168
Bob Wilson626613d2010-11-23 19:38:38 +00005169 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popcb495302012-11-30 19:08:04 +00005170 return SkipLoadExtensionForVMULL(LD, DAG);
5171
Bob Wilson626613d2010-11-23 19:38:38 +00005172 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5173 // have been legalized as a BITCAST from v4i32.
5174 if (N->getOpcode() == ISD::BITCAST) {
5175 SDNode *BVN = N->getOperand(0).getNode();
5176 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5177 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5178 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5179 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
5180 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5181 }
5182 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5183 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5184 EVT VT = N->getValueType(0);
5185 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5186 unsigned NumElts = VT.getVectorNumElements();
5187 MVT TruncVT = MVT::getIntegerVT(EltSize);
5188 SmallVector<SDValue, 8> Ops;
5189 for (unsigned i = 0; i != NumElts; ++i) {
5190 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5191 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00005192 // Element types smaller than 32 bits are not legal, so use i32 elements.
5193 // The values are implicitly truncated so sext vs. zext doesn't matter.
5194 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00005195 }
5196 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5197 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005198}
5199
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005200static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5201 unsigned Opcode = N->getOpcode();
5202 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5203 SDNode *N0 = N->getOperand(0).getNode();
5204 SDNode *N1 = N->getOperand(1).getNode();
5205 return N0->hasOneUse() && N1->hasOneUse() &&
5206 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5207 }
5208 return false;
5209}
5210
5211static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5212 unsigned Opcode = N->getOpcode();
5213 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5214 SDNode *N0 = N->getOperand(0).getNode();
5215 SDNode *N1 = N->getOperand(1).getNode();
5216 return N0->hasOneUse() && N1->hasOneUse() &&
5217 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5218 }
5219 return false;
5220}
5221
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005222static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5223 // Multiplications are only custom-lowered for 128-bit vectors so that
5224 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5225 EVT VT = Op.getValueType();
Sebastian Popcb495302012-11-30 19:08:04 +00005226 assert(VT.is128BitVector() && VT.isInteger() &&
5227 "unexpected type for custom-lowering ISD::MUL");
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005228 SDNode *N0 = Op.getOperand(0).getNode();
5229 SDNode *N1 = Op.getOperand(1).getNode();
5230 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005231 bool isMLA = false;
5232 bool isN0SExt = isSignExtended(N0, DAG);
5233 bool isN1SExt = isSignExtended(N1, DAG);
5234 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005235 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005236 else {
5237 bool isN0ZExt = isZeroExtended(N0, DAG);
5238 bool isN1ZExt = isZeroExtended(N1, DAG);
5239 if (isN0ZExt && isN1ZExt)
5240 NewOpc = ARMISD::VMULLu;
5241 else if (isN1SExt || isN1ZExt) {
5242 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5243 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5244 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5245 NewOpc = ARMISD::VMULLs;
5246 isMLA = true;
5247 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5248 NewOpc = ARMISD::VMULLu;
5249 isMLA = true;
5250 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5251 std::swap(N0, N1);
5252 NewOpc = ARMISD::VMULLu;
5253 isMLA = true;
5254 }
5255 }
5256
5257 if (!NewOpc) {
5258 if (VT == MVT::v2i64)
5259 // Fall through to expand this. It is not legal.
5260 return SDValue();
5261 else
5262 // Other vector multiplications are legal.
5263 return Op;
5264 }
5265 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005266
5267 // Legalize to a VMULL instruction.
5268 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005269 SDValue Op0;
Sebastian Popcb495302012-11-30 19:08:04 +00005270 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005271 if (!isMLA) {
Sebastian Popcb495302012-11-30 19:08:04 +00005272 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005273 assert(Op0.getValueType().is64BitVector() &&
5274 Op1.getValueType().is64BitVector() &&
5275 "unexpected types for extended operands to VMULL");
5276 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5277 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005278
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005279 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5280 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5281 // vmull q0, d4, d6
5282 // vmlal q0, d5, d6
5283 // is faster than
5284 // vaddl q0, d4, d5
5285 // vmovl q1, d6
5286 // vmul q0, q0, q1
Sebastian Popcb495302012-11-30 19:08:04 +00005287 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5288 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005289 EVT Op1VT = Op1.getValueType();
5290 return DAG.getNode(N0->getOpcode(), DL, VT,
5291 DAG.getNode(NewOpc, DL, VT,
5292 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5293 DAG.getNode(NewOpc, DL, VT,
5294 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005295}
5296
Owen Anderson76706012011-04-05 21:48:57 +00005297static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005298LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5299 // Convert to float
5300 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5301 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5302 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5303 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5304 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5305 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5306 // Get reciprocal estimate.
5307 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005308 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005309 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5310 // Because char has a smaller range than uchar, we can actually get away
5311 // without any newton steps. This requires that we use a weird bias
5312 // of 0xb000, however (again, this has been exhaustively tested).
5313 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5314 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5315 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5316 Y = DAG.getConstant(0xb000, MVT::i32);
5317 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5318 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5319 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5320 // Convert back to short.
5321 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5322 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5323 return X;
5324}
5325
Owen Anderson76706012011-04-05 21:48:57 +00005326static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005327LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5328 SDValue N2;
5329 // Convert to float.
5330 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5331 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5332 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5333 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5334 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5335 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005336
Nate Begeman7973f352011-02-11 20:53:29 +00005337 // Use reciprocal estimate and one refinement step.
5338 // float4 recip = vrecpeq_f32(yf);
5339 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005340 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005341 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005342 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005343 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5344 N1, N2);
5345 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5346 // Because short has a smaller range than ushort, we can actually get away
5347 // with only a single newton step. This requires that we use a weird bias
5348 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005349 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005350 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5351 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005352 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005353 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5354 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5355 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5356 // Convert back to integer and return.
5357 // return vmovn_s32(vcvt_s32_f32(result));
5358 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5359 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5360 return N0;
5361}
5362
5363static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5364 EVT VT = Op.getValueType();
5365 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5366 "unexpected type for custom-lowering ISD::SDIV");
5367
5368 DebugLoc dl = Op.getDebugLoc();
5369 SDValue N0 = Op.getOperand(0);
5370 SDValue N1 = Op.getOperand(1);
5371 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005372
Nate Begeman7973f352011-02-11 20:53:29 +00005373 if (VT == MVT::v8i8) {
5374 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5375 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005376
Nate Begeman7973f352011-02-11 20:53:29 +00005377 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5378 DAG.getIntPtrConstant(4));
5379 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005380 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005381 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5382 DAG.getIntPtrConstant(0));
5383 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5384 DAG.getIntPtrConstant(0));
5385
5386 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5387 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5388
5389 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5390 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005391
Nate Begeman7973f352011-02-11 20:53:29 +00005392 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5393 return N0;
5394 }
5395 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5396}
5397
5398static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5399 EVT VT = Op.getValueType();
5400 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5401 "unexpected type for custom-lowering ISD::UDIV");
5402
5403 DebugLoc dl = Op.getDebugLoc();
5404 SDValue N0 = Op.getOperand(0);
5405 SDValue N1 = Op.getOperand(1);
5406 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005407
Nate Begeman7973f352011-02-11 20:53:29 +00005408 if (VT == MVT::v8i8) {
5409 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5410 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005411
Nate Begeman7973f352011-02-11 20:53:29 +00005412 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5413 DAG.getIntPtrConstant(4));
5414 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005415 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005416 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5417 DAG.getIntPtrConstant(0));
5418 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5419 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005420
Nate Begeman7973f352011-02-11 20:53:29 +00005421 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5422 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005423
Nate Begeman7973f352011-02-11 20:53:29 +00005424 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5425 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005426
5427 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005428 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5429 N0);
5430 return N0;
5431 }
Owen Anderson76706012011-04-05 21:48:57 +00005432
Nate Begeman7973f352011-02-11 20:53:29 +00005433 // v4i16 sdiv ... Convert to float.
5434 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5435 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5436 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5437 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5438 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005439 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005440
5441 // Use reciprocal estimate and two refinement steps.
5442 // float4 recip = vrecpeq_f32(yf);
5443 // recip *= vrecpsq_f32(yf, recip);
5444 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005445 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005446 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005447 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005448 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005449 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005450 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005451 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005452 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005453 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005454 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5455 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5456 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5457 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005458 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005459 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5460 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5461 N1 = DAG.getConstant(2, MVT::i32);
5462 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5463 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5464 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5465 // Convert back to integer and return.
5466 // return vmovn_u32(vcvt_s32_f32(result));
5467 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5468 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5469 return N0;
5470}
5471
Evan Cheng342e3162011-08-30 01:34:54 +00005472static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5473 EVT VT = Op.getNode()->getValueType(0);
5474 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5475
5476 unsigned Opc;
5477 bool ExtraOp = false;
5478 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005479 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005480 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5481 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5482 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5483 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5484 }
5485
5486 if (!ExtraOp)
5487 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5488 Op.getOperand(1));
5489 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5490 Op.getOperand(1), Op.getOperand(2));
5491}
5492
Eli Friedman74bf18c2011-09-15 22:26:18 +00005493static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005494 // Monotonic load/store is legal for all targets
5495 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5496 return Op;
5497
5498 // Aquire/Release load/store is not legal for targets without a
5499 // dmb or equivalent available.
5500 return SDValue();
5501}
5502
5503
Eli Friedman2bdffe42011-08-31 00:31:29 +00005504static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005505ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5506 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005507 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005508 assert (Node->getValueType(0) == MVT::i64 &&
5509 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005510
Eli Friedman4d3f3292011-08-31 17:52:22 +00005511 SmallVector<SDValue, 6> Ops;
5512 Ops.push_back(Node->getOperand(0)); // Chain
5513 Ops.push_back(Node->getOperand(1)); // Ptr
5514 // Low part of Val1
5515 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5516 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5517 // High part of Val1
5518 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5519 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005520 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005521 // High part of Val1
5522 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5523 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5524 // High part of Val2
5525 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5526 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5527 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005528 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5529 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005530 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005531 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005532 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005533 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5534 Results.push_back(Result.getValue(2));
5535}
5536
Dan Gohmand858e902010-04-17 15:26:15 +00005537SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005538 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005539 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005540 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005541 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005542 case ISD::GlobalAddress:
5543 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5544 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005545 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005546 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005547 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5548 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005549 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005550 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005551 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005552 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005553 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005554 case ISD::SINT_TO_FP:
5555 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5556 case ISD::FP_TO_SINT:
5557 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005558 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005559 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005560 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005561 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005562 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005563 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005564 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5565 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005566 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005567 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005568 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005569 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005570 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005571 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005572 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005573 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengc8e70452012-12-04 22:41:50 +00005574 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005575 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005576 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005577 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005578 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005579 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005580 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005581 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005582 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005583 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005584 case ISD::SDIV: return LowerSDIV(Op, DAG);
5585 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005586 case ISD::ADDC:
5587 case ISD::ADDE:
5588 case ISD::SUBC:
5589 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005590 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005591 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005592 }
Evan Chenga8e29892007-01-19 07:51:42 +00005593}
5594
Duncan Sands1607f052008-12-01 11:39:25 +00005595/// ReplaceNodeResults - Replace the results of node with an illegal result
5596/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005597void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5598 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005599 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005600 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005601 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005602 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005603 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005604 case ISD::BITCAST:
5605 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005606 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005607 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005608 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005609 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005610 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005611 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005612 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005613 return;
5614 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005615 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005616 return;
5617 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005618 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005619 return;
5620 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005621 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005622 return;
5623 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005624 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005625 return;
5626 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005627 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005628 return;
5629 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005630 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005631 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005632 case ISD::ATOMIC_CMP_SWAP:
5633 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5634 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005635 case ISD::ATOMIC_LOAD_MIN:
5636 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5637 return;
5638 case ISD::ATOMIC_LOAD_UMIN:
5639 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5640 return;
5641 case ISD::ATOMIC_LOAD_MAX:
5642 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5643 return;
5644 case ISD::ATOMIC_LOAD_UMAX:
5645 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5646 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005647 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005648 if (Res.getNode())
5649 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005650}
Chris Lattner27a6c732007-11-24 07:07:01 +00005651
Evan Chenga8e29892007-01-19 07:51:42 +00005652//===----------------------------------------------------------------------===//
5653// ARM Scheduler Hooks
5654//===----------------------------------------------------------------------===//
5655
5656MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005657ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5658 MachineBasicBlock *BB,
5659 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005660 unsigned dest = MI->getOperand(0).getReg();
5661 unsigned ptr = MI->getOperand(1).getReg();
5662 unsigned oldval = MI->getOperand(2).getReg();
5663 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005664 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5665 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005666 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005667
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005668 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005669 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5670 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5671 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005672
5673 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005674 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5675 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5676 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005677 }
5678
Jim Grosbach5278eb82009-12-11 01:42:04 +00005679 unsigned ldrOpc, strOpc;
5680 switch (Size) {
5681 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005682 case 1:
5683 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005684 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005685 break;
5686 case 2:
5687 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5688 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5689 break;
5690 case 4:
5691 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5692 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5693 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005694 }
5695
5696 MachineFunction *MF = BB->getParent();
5697 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5698 MachineFunction::iterator It = BB;
5699 ++It; // insert the new blocks after the current block
5700
5701 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5702 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5703 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5704 MF->insert(It, loop1MBB);
5705 MF->insert(It, loop2MBB);
5706 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005707
5708 // Transfer the remainder of BB and its successor edges to exitMBB.
5709 exitMBB->splice(exitMBB->begin(), BB,
5710 llvm::next(MachineBasicBlock::iterator(MI)),
5711 BB->end());
5712 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005713
5714 // thisMBB:
5715 // ...
5716 // fallthrough --> loop1MBB
5717 BB->addSuccessor(loop1MBB);
5718
5719 // loop1MBB:
5720 // ldrex dest, [ptr]
5721 // cmp dest, oldval
5722 // bne exitMBB
5723 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005724 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5725 if (ldrOpc == ARM::t2LDREX)
5726 MIB.addImm(0);
5727 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005728 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005729 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005730 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5731 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005732 BB->addSuccessor(loop2MBB);
5733 BB->addSuccessor(exitMBB);
5734
5735 // loop2MBB:
5736 // strex scratch, newval, [ptr]
5737 // cmp scratch, #0
5738 // bne loop1MBB
5739 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005740 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5741 if (strOpc == ARM::t2STREX)
5742 MIB.addImm(0);
5743 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005744 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005745 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005746 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5747 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005748 BB->addSuccessor(loop1MBB);
5749 BB->addSuccessor(exitMBB);
5750
5751 // exitMBB:
5752 // ...
5753 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005754
Dan Gohman14152b42010-07-06 20:24:04 +00005755 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005756
Jim Grosbach5278eb82009-12-11 01:42:04 +00005757 return BB;
5758}
5759
5760MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005761ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5762 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005763 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5764 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5765
5766 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005767 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005768 MachineFunction::iterator It = BB;
5769 ++It;
5770
5771 unsigned dest = MI->getOperand(0).getReg();
5772 unsigned ptr = MI->getOperand(1).getReg();
5773 unsigned incr = MI->getOperand(2).getReg();
5774 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005775 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005776
5777 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5778 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005779 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5780 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005781 }
5782
Jim Grosbachc3c23542009-12-14 04:22:04 +00005783 unsigned ldrOpc, strOpc;
5784 switch (Size) {
5785 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005786 case 1:
5787 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005788 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005789 break;
5790 case 2:
5791 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5792 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5793 break;
5794 case 4:
5795 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5796 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5797 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005798 }
5799
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005800 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5801 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5802 MF->insert(It, loopMBB);
5803 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005804
5805 // Transfer the remainder of BB and its successor edges to exitMBB.
5806 exitMBB->splice(exitMBB->begin(), BB,
5807 llvm::next(MachineBasicBlock::iterator(MI)),
5808 BB->end());
5809 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005810
Craig Topper420761a2012-04-20 07:30:17 +00005811 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005812 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005813 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005814 unsigned scratch = MRI.createVirtualRegister(TRC);
5815 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005816
5817 // thisMBB:
5818 // ...
5819 // fallthrough --> loopMBB
5820 BB->addSuccessor(loopMBB);
5821
5822 // loopMBB:
5823 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005824 // <binop> scratch2, dest, incr
5825 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005826 // cmp scratch, #0
5827 // bne- loopMBB
5828 // fallthrough --> exitMBB
5829 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005830 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5831 if (ldrOpc == ARM::t2LDREX)
5832 MIB.addImm(0);
5833 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005834 if (BinOpcode) {
5835 // operand order needs to go the other way for NAND
5836 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5837 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5838 addReg(incr).addReg(dest)).addReg(0);
5839 else
5840 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5841 addReg(dest).addReg(incr)).addReg(0);
5842 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005843
Jim Grosbachb6aed502011-09-09 18:37:27 +00005844 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5845 if (strOpc == ARM::t2STREX)
5846 MIB.addImm(0);
5847 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005848 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005849 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005850 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5851 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005852
5853 BB->addSuccessor(loopMBB);
5854 BB->addSuccessor(exitMBB);
5855
5856 // exitMBB:
5857 // ...
5858 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005859
Dan Gohman14152b42010-07-06 20:24:04 +00005860 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005861
Jim Grosbachc3c23542009-12-14 04:22:04 +00005862 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005863}
5864
Jim Grosbachf7da8822011-04-26 19:44:18 +00005865MachineBasicBlock *
5866ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5867 MachineBasicBlock *BB,
5868 unsigned Size,
5869 bool signExtend,
5870 ARMCC::CondCodes Cond) const {
5871 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5872
5873 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5874 MachineFunction *MF = BB->getParent();
5875 MachineFunction::iterator It = BB;
5876 ++It;
5877
5878 unsigned dest = MI->getOperand(0).getReg();
5879 unsigned ptr = MI->getOperand(1).getReg();
5880 unsigned incr = MI->getOperand(2).getReg();
5881 unsigned oldval = dest;
5882 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005883 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005884
5885 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5886 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005887 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5888 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005889 }
5890
Jim Grosbachf7da8822011-04-26 19:44:18 +00005891 unsigned ldrOpc, strOpc, extendOpc;
5892 switch (Size) {
5893 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5894 case 1:
5895 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5896 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005897 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005898 break;
5899 case 2:
5900 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5901 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005902 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005903 break;
5904 case 4:
5905 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5906 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5907 extendOpc = 0;
5908 break;
5909 }
5910
5911 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5912 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5913 MF->insert(It, loopMBB);
5914 MF->insert(It, exitMBB);
5915
5916 // Transfer the remainder of BB and its successor edges to exitMBB.
5917 exitMBB->splice(exitMBB->begin(), BB,
5918 llvm::next(MachineBasicBlock::iterator(MI)),
5919 BB->end());
5920 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5921
Craig Topper420761a2012-04-20 07:30:17 +00005922 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005923 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005924 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005925 unsigned scratch = MRI.createVirtualRegister(TRC);
5926 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005927
5928 // thisMBB:
5929 // ...
5930 // fallthrough --> loopMBB
5931 BB->addSuccessor(loopMBB);
5932
5933 // loopMBB:
5934 // ldrex dest, ptr
5935 // (sign extend dest, if required)
5936 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005937 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005938 // strex scratch, scratch2, ptr
5939 // cmp scratch, #0
5940 // bne- loopMBB
5941 // fallthrough --> exitMBB
5942 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005943 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5944 if (ldrOpc == ARM::t2LDREX)
5945 MIB.addImm(0);
5946 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005947
5948 // Sign extend the value, if necessary.
5949 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005950 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005951 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5952 .addReg(dest)
5953 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005954 }
5955
5956 // Build compare and cmov instructions.
5957 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5958 .addReg(oldval).addReg(incr));
5959 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005960 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005961
Jim Grosbachb6aed502011-09-09 18:37:27 +00005962 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5963 if (strOpc == ARM::t2STREX)
5964 MIB.addImm(0);
5965 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005966 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5967 .addReg(scratch).addImm(0));
5968 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5969 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5970
5971 BB->addSuccessor(loopMBB);
5972 BB->addSuccessor(exitMBB);
5973
5974 // exitMBB:
5975 // ...
5976 BB = exitMBB;
5977
5978 MI->eraseFromParent(); // The instruction is gone now.
5979
5980 return BB;
5981}
5982
Eli Friedman2bdffe42011-08-31 00:31:29 +00005983MachineBasicBlock *
5984ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5985 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00005986 bool NeedsCarry, bool IsCmpxchg,
5987 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005988 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5989 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5990
5991 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5992 MachineFunction *MF = BB->getParent();
5993 MachineFunction::iterator It = BB;
5994 ++It;
5995
5996 unsigned destlo = MI->getOperand(0).getReg();
5997 unsigned desthi = MI->getOperand(1).getReg();
5998 unsigned ptr = MI->getOperand(2).getReg();
5999 unsigned vallo = MI->getOperand(3).getReg();
6000 unsigned valhi = MI->getOperand(4).getReg();
6001 DebugLoc dl = MI->getDebugLoc();
6002 bool isThumb2 = Subtarget->isThumb2();
6003
6004 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6005 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00006006 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
6007 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
6008 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006009 }
6010
Eli Friedman2bdffe42011-08-31 00:31:29 +00006011 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00006012 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006013 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006014 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006015 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00006016 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006017 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006018
Eli Friedman2bdffe42011-08-31 00:31:29 +00006019 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006020 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
6021 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006022 MF->insert(It, exitMBB);
6023
6024 // Transfer the remainder of BB and its successor edges to exitMBB.
6025 exitMBB->splice(exitMBB->begin(), BB,
6026 llvm::next(MachineBasicBlock::iterator(MI)),
6027 BB->end());
6028 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6029
Craig Topper420761a2012-04-20 07:30:17 +00006030 const TargetRegisterClass *TRC = isThumb2 ?
6031 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6032 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006033 unsigned storesuccess = MRI.createVirtualRegister(TRC);
6034
6035 // thisMBB:
6036 // ...
6037 // fallthrough --> loopMBB
6038 BB->addSuccessor(loopMBB);
6039
6040 // loopMBB:
6041 // ldrexd r2, r3, ptr
6042 // <binopa> r0, r2, incr
6043 // <binopb> r1, r3, incr
6044 // strexd storesuccess, r0, r1, ptr
6045 // cmp storesuccess, #0
6046 // bne- loopMBB
6047 // fallthrough --> exitMBB
Eli Friedman2bdffe42011-08-31 00:31:29 +00006048 BB = loopMBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006049
Eli Friedman2bdffe42011-08-31 00:31:29 +00006050 // Load
Tim Northover0adfded2013-01-29 09:06:13 +00006051 if (isThumb2) {
6052 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2LDREXD))
6053 .addReg(destlo, RegState::Define)
6054 .addReg(desthi, RegState::Define)
6055 .addReg(ptr));
6056 } else {
6057 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6058 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDREXD))
6059 .addReg(GPRPair0, RegState::Define).addReg(ptr));
6060 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
6061 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6062 .addReg(GPRPair0, 0, ARM::gsub_0);
6063 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6064 .addReg(GPRPair0, 0, ARM::gsub_1);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006065 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006066
Tim Northover0adfded2013-01-29 09:06:13 +00006067 unsigned StoreLo, StoreHi;
Eli Friedman4d3f3292011-08-31 17:52:22 +00006068 if (IsCmpxchg) {
6069 // Add early exit
6070 for (unsigned i = 0; i < 2; i++) {
6071 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6072 ARM::CMPrr))
6073 .addReg(i == 0 ? destlo : desthi)
6074 .addReg(i == 0 ? vallo : valhi));
6075 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6076 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6077 BB->addSuccessor(exitMBB);
6078 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6079 BB = (i == 0 ? contBB : cont2BB);
6080 }
6081
6082 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006083 StoreLo = MI->getOperand(5).getReg();
6084 StoreHi = MI->getOperand(6).getReg();
Eli Friedman4d3f3292011-08-31 17:52:22 +00006085 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006086 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006087 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6088 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00006089 .addReg(destlo).addReg(vallo))
6090 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006091 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6092 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006093 .addReg(desthi).addReg(valhi))
6094 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006095
Tim Northover0adfded2013-01-29 09:06:13 +00006096 StoreLo = tmpRegLo;
6097 StoreHi = tmpRegHi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006098 } else {
6099 // Copy to physregs for strexd
Tim Northover0adfded2013-01-29 09:06:13 +00006100 StoreLo = vallo;
6101 StoreHi = valhi;
Eli Friedman2bdffe42011-08-31 00:31:29 +00006102 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00006103 if (IsMinMax) {
6104 // Compare and branch to exit block.
6105 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6106 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6107 BB->addSuccessor(exitMBB);
6108 BB->addSuccessor(contBB);
6109 BB = contBB;
Tim Northover0adfded2013-01-29 09:06:13 +00006110 StoreLo = vallo;
6111 StoreHi = valhi;
Silviu Baranga35b3df62012-11-29 14:41:25 +00006112 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006113
6114 // Store
Tim Northover0adfded2013-01-29 09:06:13 +00006115 if (isThumb2) {
6116 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2STREXD), storesuccess)
6117 .addReg(StoreLo).addReg(StoreHi).addReg(ptr));
6118 } else {
6119 // Marshal a pair...
6120 unsigned StorePair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6121 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6122 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6123 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6124 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6125 .addReg(UndefPair)
6126 .addReg(StoreLo)
6127 .addImm(ARM::gsub_0);
6128 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), StorePair)
6129 .addReg(r1)
6130 .addReg(StoreHi)
6131 .addImm(ARM::gsub_1);
6132
6133 // ...and store it
6134 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::STREXD), storesuccess)
6135 .addReg(StorePair).addReg(ptr));
6136 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006137 // Cmp+jump
6138 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6139 .addReg(storesuccess).addImm(0));
6140 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6141 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6142
6143 BB->addSuccessor(loopMBB);
6144 BB->addSuccessor(exitMBB);
6145
6146 // exitMBB:
6147 // ...
6148 BB = exitMBB;
6149
6150 MI->eraseFromParent(); // The instruction is gone now.
6151
6152 return BB;
6153}
6154
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006155/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6156/// registers the function context.
6157void ARMTargetLowering::
6158SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6159 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006160 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6161 DebugLoc dl = MI->getDebugLoc();
6162 MachineFunction *MF = MBB->getParent();
6163 MachineRegisterInfo *MRI = &MF->getRegInfo();
6164 MachineConstantPool *MCP = MF->getConstantPool();
6165 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6166 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006167
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006168 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006169 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006170
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006171 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006172 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006173 ARMConstantPoolValue *CPV =
6174 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6175 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6176
Craig Topper420761a2012-04-20 07:30:17 +00006177 const TargetRegisterClass *TRC = isThumb ?
6178 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6179 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006180
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006181 // Grab constant pool and fixed stack memory operands.
6182 MachineMemOperand *CPMMO =
6183 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6184 MachineMemOperand::MOLoad, 4, 4);
6185
6186 MachineMemOperand *FIMMOSt =
6187 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6188 MachineMemOperand::MOStore, 4, 4);
6189
6190 // Load the address of the dispatch MBB into the jump buffer.
6191 if (isThumb2) {
6192 // Incoming value: jbuf
6193 // ldr.n r5, LCPI1_1
6194 // orr r5, r5, #1
6195 // add r5, pc
6196 // str r5, [$jbuf, #+4] ; &jbuf[1]
6197 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6198 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6199 .addConstantPoolIndex(CPI)
6200 .addMemOperand(CPMMO));
6201 // Set the low bit because of thumb mode.
6202 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6203 AddDefaultCC(
6204 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6205 .addReg(NewVReg1, RegState::Kill)
6206 .addImm(0x01)));
6207 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6208 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6209 .addReg(NewVReg2, RegState::Kill)
6210 .addImm(PCLabelId);
6211 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6212 .addReg(NewVReg3, RegState::Kill)
6213 .addFrameIndex(FI)
6214 .addImm(36) // &jbuf[1] :: pc
6215 .addMemOperand(FIMMOSt));
6216 } else if (isThumb) {
6217 // Incoming value: jbuf
6218 // ldr.n r1, LCPI1_4
6219 // add r1, pc
6220 // mov r2, #1
6221 // orrs r1, r2
6222 // add r2, $jbuf, #+4 ; &jbuf[1]
6223 // str r1, [r2]
6224 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6225 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6226 .addConstantPoolIndex(CPI)
6227 .addMemOperand(CPMMO));
6228 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6229 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6230 .addReg(NewVReg1, RegState::Kill)
6231 .addImm(PCLabelId);
6232 // Set the low bit because of thumb mode.
6233 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6234 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6235 .addReg(ARM::CPSR, RegState::Define)
6236 .addImm(1));
6237 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6238 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6239 .addReg(ARM::CPSR, RegState::Define)
6240 .addReg(NewVReg2, RegState::Kill)
6241 .addReg(NewVReg3, RegState::Kill));
6242 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6243 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6244 .addFrameIndex(FI)
6245 .addImm(36)); // &jbuf[1] :: pc
6246 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6247 .addReg(NewVReg4, RegState::Kill)
6248 .addReg(NewVReg5, RegState::Kill)
6249 .addImm(0)
6250 .addMemOperand(FIMMOSt));
6251 } else {
6252 // Incoming value: jbuf
6253 // ldr r1, LCPI1_1
6254 // add r1, pc, r1
6255 // str r1, [$jbuf, #+4] ; &jbuf[1]
6256 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6257 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6258 .addConstantPoolIndex(CPI)
6259 .addImm(0)
6260 .addMemOperand(CPMMO));
6261 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6262 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6263 .addReg(NewVReg1, RegState::Kill)
6264 .addImm(PCLabelId));
6265 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6266 .addReg(NewVReg2, RegState::Kill)
6267 .addFrameIndex(FI)
6268 .addImm(36) // &jbuf[1] :: pc
6269 .addMemOperand(FIMMOSt));
6270 }
6271}
6272
6273MachineBasicBlock *ARMTargetLowering::
6274EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6275 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6276 DebugLoc dl = MI->getDebugLoc();
6277 MachineFunction *MF = MBB->getParent();
6278 MachineRegisterInfo *MRI = &MF->getRegInfo();
6279 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6280 MachineFrameInfo *MFI = MF->getFrameInfo();
6281 int FI = MFI->getFunctionContextIndex();
6282
Craig Topper420761a2012-04-20 07:30:17 +00006283 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6284 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006285 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006286
Bill Wendling04f15b42011-10-06 21:29:56 +00006287 // Get a mapping of the call site numbers to all of the landing pads they're
6288 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006289 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6290 unsigned MaxCSNum = 0;
6291 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006292 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6293 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006294 if (!BB->isLandingPad()) continue;
6295
6296 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6297 // pad.
6298 for (MachineBasicBlock::iterator
6299 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6300 if (!II->isEHLabel()) continue;
6301
6302 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006303 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006304
Bill Wendling5cbef192011-10-05 23:28:57 +00006305 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6306 for (SmallVectorImpl<unsigned>::iterator
6307 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6308 CSI != CSE; ++CSI) {
6309 CallSiteNumToLPad[*CSI].push_back(BB);
6310 MaxCSNum = std::max(MaxCSNum, *CSI);
6311 }
Bill Wendling2a850152011-10-05 00:02:33 +00006312 break;
6313 }
6314 }
6315
6316 // Get an ordered list of the machine basic blocks for the jump table.
6317 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006318 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006319 LPadList.reserve(CallSiteNumToLPad.size());
6320 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6321 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6322 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006323 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006324 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006325 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6326 }
Bill Wendling2a850152011-10-05 00:02:33 +00006327 }
6328
Bill Wendling5cbef192011-10-05 23:28:57 +00006329 assert(!LPadList.empty() &&
6330 "No landing pad destinations for the dispatch jump table!");
6331
Bill Wendling04f15b42011-10-06 21:29:56 +00006332 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006333 MachineJumpTableInfo *JTI =
6334 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6335 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6336 unsigned UId = AFI->createJumpTableUId();
6337
Bill Wendling04f15b42011-10-06 21:29:56 +00006338 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006339
6340 // Shove the dispatch's address into the return slot in the function context.
6341 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6342 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006343
Bill Wendlingbb734682011-10-05 00:39:32 +00006344 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky0f156af2013-01-30 16:30:19 +00006345 unsigned trap_opcode;
Chad Rosier279706e2013-02-28 18:54:27 +00006346 if (Subtarget->isThumb())
Eli Bendersky0f156af2013-01-30 16:30:19 +00006347 trap_opcode = ARM::tTRAP;
Chad Rosier279706e2013-02-28 18:54:27 +00006348 else
6349 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6350
Eli Bendersky0f156af2013-01-30 16:30:19 +00006351 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendlingbb734682011-10-05 00:39:32 +00006352 DispatchBB->addSuccessor(TrapBB);
6353
6354 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6355 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006356
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006357 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006358 MF->insert(MF->end(), DispatchBB);
6359 MF->insert(MF->end(), DispContBB);
6360 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006361
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006362 // Insert code into the entry block that creates and registers the function
6363 // context.
6364 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6365
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006366 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006367 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006368 MachineMemOperand::MOLoad |
6369 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006370
Chad Rosiere7bd5192012-11-06 23:05:24 +00006371 MachineInstrBuilder MIB;
6372 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6373
6374 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6375 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6376
6377 // Add a register mask with no preserved registers. This results in all
6378 // registers being marked as clobbered.
6379 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006380
Bill Wendling952cb502011-10-18 22:49:07 +00006381 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006382 if (Subtarget->isThumb2()) {
6383 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6384 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6385 .addFrameIndex(FI)
6386 .addImm(4)
6387 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006388
Bill Wendling952cb502011-10-18 22:49:07 +00006389 if (NumLPads < 256) {
6390 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6391 .addReg(NewVReg1)
6392 .addImm(LPadList.size()));
6393 } else {
6394 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6395 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006396 .addImm(NumLPads & 0xFFFF));
6397
6398 unsigned VReg2 = VReg1;
6399 if ((NumLPads & 0xFFFF0000) != 0) {
6400 VReg2 = MRI->createVirtualRegister(TRC);
6401 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6402 .addReg(VReg1)
6403 .addImm(NumLPads >> 16));
6404 }
6405
Bill Wendling952cb502011-10-18 22:49:07 +00006406 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6407 .addReg(NewVReg1)
6408 .addReg(VReg2));
6409 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006410
Bill Wendling95ce2e92011-10-06 22:53:00 +00006411 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6412 .addMBB(TrapBB)
6413 .addImm(ARMCC::HI)
6414 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006415
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006416 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6417 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006418 .addJumpTableIndex(MJTI)
6419 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006420
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006421 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006422 AddDefaultCC(
6423 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006424 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6425 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006426 .addReg(NewVReg1)
6427 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6428
6429 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006430 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006431 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006432 .addJumpTableIndex(MJTI)
6433 .addImm(UId);
6434 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006435 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6436 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6437 .addFrameIndex(FI)
6438 .addImm(1)
6439 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006440
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006441 if (NumLPads < 256) {
6442 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6443 .addReg(NewVReg1)
6444 .addImm(NumLPads));
6445 } else {
6446 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006447 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6448 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6449
6450 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006451 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006452 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006453 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006454 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006455
6456 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6457 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6458 .addReg(VReg1, RegState::Define)
6459 .addConstantPoolIndex(Idx));
6460 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6461 .addReg(NewVReg1)
6462 .addReg(VReg1));
6463 }
6464
Bill Wendling083a8eb2011-10-06 23:37:36 +00006465 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6466 .addMBB(TrapBB)
6467 .addImm(ARMCC::HI)
6468 .addReg(ARM::CPSR);
6469
6470 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6471 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6472 .addReg(ARM::CPSR, RegState::Define)
6473 .addReg(NewVReg1)
6474 .addImm(2));
6475
6476 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006477 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006478 .addJumpTableIndex(MJTI)
6479 .addImm(UId));
6480
6481 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6482 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6483 .addReg(ARM::CPSR, RegState::Define)
6484 .addReg(NewVReg2, RegState::Kill)
6485 .addReg(NewVReg3));
6486
6487 MachineMemOperand *JTMMOLd =
6488 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6489 MachineMemOperand::MOLoad, 4, 4);
6490
6491 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6492 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6493 .addReg(NewVReg4, RegState::Kill)
6494 .addImm(0)
6495 .addMemOperand(JTMMOLd));
6496
6497 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6498 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6499 .addReg(ARM::CPSR, RegState::Define)
6500 .addReg(NewVReg5, RegState::Kill)
6501 .addReg(NewVReg3));
6502
6503 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6504 .addReg(NewVReg6, RegState::Kill)
6505 .addJumpTableIndex(MJTI)
6506 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006507 } else {
6508 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6509 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6510 .addFrameIndex(FI)
6511 .addImm(4)
6512 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006513
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006514 if (NumLPads < 256) {
6515 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6516 .addReg(NewVReg1)
6517 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006518 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006519 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6520 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006521 .addImm(NumLPads & 0xFFFF));
6522
6523 unsigned VReg2 = VReg1;
6524 if ((NumLPads & 0xFFFF0000) != 0) {
6525 VReg2 = MRI->createVirtualRegister(TRC);
6526 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6527 .addReg(VReg1)
6528 .addImm(NumLPads >> 16));
6529 }
6530
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006531 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6532 .addReg(NewVReg1)
6533 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006534 } else {
6535 MachineConstantPool *ConstantPool = MF->getConstantPool();
6536 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6537 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6538
6539 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006540 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006541 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006542 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006543 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6544
6545 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6546 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6547 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006548 .addConstantPoolIndex(Idx)
6549 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006550 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6551 .addReg(NewVReg1)
6552 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006553 }
6554
Bill Wendling95ce2e92011-10-06 22:53:00 +00006555 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6556 .addMBB(TrapBB)
6557 .addImm(ARMCC::HI)
6558 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006559
Bill Wendling564392b2011-10-18 22:11:18 +00006560 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006561 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006562 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006563 .addReg(NewVReg1)
6564 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006565 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6566 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006567 .addJumpTableIndex(MJTI)
6568 .addImm(UId));
6569
6570 MachineMemOperand *JTMMOLd =
6571 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6572 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006573 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006574 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006575 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6576 .addReg(NewVReg3, RegState::Kill)
6577 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006578 .addImm(0)
6579 .addMemOperand(JTMMOLd));
6580
6581 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006582 .addReg(NewVReg5, RegState::Kill)
6583 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006584 .addJumpTableIndex(MJTI)
6585 .addImm(UId);
6586 }
Bill Wendling2a850152011-10-05 00:02:33 +00006587
Bill Wendlingbb734682011-10-05 00:39:32 +00006588 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006589 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006590 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006591 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6592 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006593 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006594 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006595 }
6596
Bill Wendling24bb9252011-10-17 05:25:09 +00006597 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006598 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006599 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006600 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6601 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6602 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006603
6604 // Remove the landing pad successor from the invoke block and replace it
6605 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006606 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6607 BB->succ_end());
6608 while (!Successors.empty()) {
6609 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006610 if (SMBB->isLandingPad()) {
6611 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006612 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006613 }
6614 }
6615
6616 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006617
6618 // Find the invoke call and mark all of the callee-saved registers as
6619 // 'implicit defined' so that they're spilled. This prevents code from
6620 // moving instructions to before the EH block, where they will never be
6621 // executed.
6622 for (MachineBasicBlock::reverse_iterator
6623 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006624 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006625
6626 DenseMap<unsigned, bool> DefRegs;
6627 for (MachineInstr::mop_iterator
6628 OI = II->operands_begin(), OE = II->operands_end();
6629 OI != OE; ++OI) {
6630 if (!OI->isReg()) continue;
6631 DefRegs[OI->getReg()] = true;
6632 }
6633
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00006634 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006635
Bill Wendling5d798592011-10-14 23:55:44 +00006636 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006637 unsigned Reg = SavedRegs[i];
6638 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006639 !ARM::tGPRRegClass.contains(Reg) &&
6640 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006641 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006642 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006643 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006644 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006645 continue;
6646 if (!DefRegs[Reg])
6647 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006648 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006649
6650 break;
6651 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006652 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006653
Bill Wendlingf7b02072011-10-18 18:30:49 +00006654 // Mark all former landing pads as non-landing pads. The dispatch is the only
6655 // landing pad now.
6656 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6657 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6658 (*I)->setIsLandingPad(false);
6659
Bill Wendlingbb734682011-10-05 00:39:32 +00006660 // The instruction is gone now.
6661 MI->eraseFromParent();
6662
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006663 return MBB;
6664}
6665
Evan Cheng218977b2010-07-13 19:27:42 +00006666static
6667MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6668 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6669 E = MBB->succ_end(); I != E; ++I)
6670 if (*I != Succ)
6671 return *I;
6672 llvm_unreachable("Expecting a BB with two successors!");
6673}
6674
Manman Ren68f25572012-06-01 19:33:18 +00006675MachineBasicBlock *ARMTargetLowering::
6676EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6677 // This pseudo instruction has 3 operands: dst, src, size
6678 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6679 // Otherwise, we will generate unrolled scalar copies.
6680 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6681 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6682 MachineFunction::iterator It = BB;
6683 ++It;
6684
6685 unsigned dest = MI->getOperand(0).getReg();
6686 unsigned src = MI->getOperand(1).getReg();
6687 unsigned SizeVal = MI->getOperand(2).getImm();
6688 unsigned Align = MI->getOperand(3).getImm();
6689 DebugLoc dl = MI->getDebugLoc();
6690
6691 bool isThumb2 = Subtarget->isThumb2();
6692 MachineFunction *MF = BB->getParent();
6693 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006694 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006695
6696 const TargetRegisterClass *TRC = isThumb2 ?
6697 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6698 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006699 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006700
6701 if (Align & 1) {
6702 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6703 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6704 UnitSize = 1;
6705 } else if (Align & 2) {
6706 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6707 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6708 UnitSize = 2;
6709 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006710 // Check whether we can use NEON instructions.
Bill Wendling831737d2012-12-30 10:32:01 +00006711 if (!MF->getFunction()->getAttributes().
6712 hasAttribute(AttributeSet::FunctionIndex,
6713 Attribute::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006714 Subtarget->hasNEON()) {
6715 if ((Align % 16 == 0) && SizeVal >= 16) {
6716 ldrOpc = ARM::VLD1q32wb_fixed;
6717 strOpc = ARM::VST1q32wb_fixed;
6718 UnitSize = 16;
6719 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6720 }
6721 else if ((Align % 8 == 0) && SizeVal >= 8) {
6722 ldrOpc = ARM::VLD1d32wb_fixed;
6723 strOpc = ARM::VST1d32wb_fixed;
6724 UnitSize = 8;
6725 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6726 }
6727 }
6728 // Can't use NEON instructions.
6729 if (UnitSize == 0) {
6730 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6731 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6732 UnitSize = 4;
6733 }
Manman Ren68f25572012-06-01 19:33:18 +00006734 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006735
Manman Ren68f25572012-06-01 19:33:18 +00006736 unsigned BytesLeft = SizeVal % UnitSize;
6737 unsigned LoopSize = SizeVal - BytesLeft;
6738
6739 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6740 // Use LDR and STR to copy.
6741 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6742 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6743 unsigned srcIn = src;
6744 unsigned destIn = dest;
6745 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006746 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006747 unsigned srcOut = MRI.createVirtualRegister(TRC);
6748 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006749 if (UnitSize >= 8) {
6750 AddDefaultPred(BuildMI(*BB, MI, dl,
6751 TII->get(ldrOpc), scratch)
6752 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6753
6754 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6755 .addReg(destIn).addImm(0).addReg(scratch));
6756 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006757 AddDefaultPred(BuildMI(*BB, MI, dl,
6758 TII->get(ldrOpc), scratch)
6759 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6760
6761 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6762 .addReg(scratch).addReg(destIn)
6763 .addImm(UnitSize));
6764 } else {
6765 AddDefaultPred(BuildMI(*BB, MI, dl,
6766 TII->get(ldrOpc), scratch)
6767 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6768 .addImm(UnitSize));
6769
6770 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6771 .addReg(scratch).addReg(destIn)
6772 .addReg(0).addImm(UnitSize));
6773 }
6774 srcIn = srcOut;
6775 destIn = destOut;
6776 }
6777
6778 // Handle the leftover bytes with LDRB and STRB.
6779 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6780 // [destOut] = STRB_POST(scratch, destIn, 1)
6781 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6782 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6783 for (unsigned i = 0; i < BytesLeft; i++) {
6784 unsigned scratch = MRI.createVirtualRegister(TRC);
6785 unsigned srcOut = MRI.createVirtualRegister(TRC);
6786 unsigned destOut = MRI.createVirtualRegister(TRC);
6787 if (isThumb2) {
6788 AddDefaultPred(BuildMI(*BB, MI, dl,
6789 TII->get(ldrOpc),scratch)
6790 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6791
6792 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6793 .addReg(scratch).addReg(destIn)
6794 .addReg(0).addImm(1));
6795 } else {
6796 AddDefaultPred(BuildMI(*BB, MI, dl,
6797 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006798 .addReg(srcOut, RegState::Define).addReg(srcIn)
6799 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006800
6801 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6802 .addReg(scratch).addReg(destIn)
6803 .addReg(0).addImm(1));
6804 }
6805 srcIn = srcOut;
6806 destIn = destOut;
6807 }
6808 MI->eraseFromParent(); // The instruction is gone now.
6809 return BB;
6810 }
6811
6812 // Expand the pseudo op to a loop.
6813 // thisMBB:
6814 // ...
6815 // movw varEnd, # --> with thumb2
6816 // movt varEnd, #
6817 // ldrcp varEnd, idx --> without thumb2
6818 // fallthrough --> loopMBB
6819 // loopMBB:
6820 // PHI varPhi, varEnd, varLoop
6821 // PHI srcPhi, src, srcLoop
6822 // PHI destPhi, dst, destLoop
6823 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6824 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6825 // subs varLoop, varPhi, #UnitSize
6826 // bne loopMBB
6827 // fallthrough --> exitMBB
6828 // exitMBB:
6829 // epilogue to handle left-over bytes
6830 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6831 // [destOut] = STRB_POST(scratch, destLoop, 1)
6832 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6833 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6834 MF->insert(It, loopMBB);
6835 MF->insert(It, exitMBB);
6836
6837 // Transfer the remainder of BB and its successor edges to exitMBB.
6838 exitMBB->splice(exitMBB->begin(), BB,
6839 llvm::next(MachineBasicBlock::iterator(MI)),
6840 BB->end());
6841 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6842
6843 // Load an immediate to varEnd.
6844 unsigned varEnd = MRI.createVirtualRegister(TRC);
6845 if (isThumb2) {
6846 unsigned VReg1 = varEnd;
6847 if ((LoopSize & 0xFFFF0000) != 0)
6848 VReg1 = MRI.createVirtualRegister(TRC);
6849 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6850 .addImm(LoopSize & 0xFFFF));
6851
6852 if ((LoopSize & 0xFFFF0000) != 0)
6853 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6854 .addReg(VReg1)
6855 .addImm(LoopSize >> 16));
6856 } else {
6857 MachineConstantPool *ConstantPool = MF->getConstantPool();
6858 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6859 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6860
6861 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006862 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006863 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006864 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006865 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6866
6867 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6868 .addReg(varEnd, RegState::Define)
6869 .addConstantPoolIndex(Idx)
6870 .addImm(0));
6871 }
6872 BB->addSuccessor(loopMBB);
6873
6874 // Generate the loop body:
6875 // varPhi = PHI(varLoop, varEnd)
6876 // srcPhi = PHI(srcLoop, src)
6877 // destPhi = PHI(destLoop, dst)
6878 MachineBasicBlock *entryBB = BB;
6879 BB = loopMBB;
6880 unsigned varLoop = MRI.createVirtualRegister(TRC);
6881 unsigned varPhi = MRI.createVirtualRegister(TRC);
6882 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6883 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6884 unsigned destLoop = MRI.createVirtualRegister(TRC);
6885 unsigned destPhi = MRI.createVirtualRegister(TRC);
6886
6887 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6888 .addReg(varLoop).addMBB(loopMBB)
6889 .addReg(varEnd).addMBB(entryBB);
6890 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6891 .addReg(srcLoop).addMBB(loopMBB)
6892 .addReg(src).addMBB(entryBB);
6893 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6894 .addReg(destLoop).addMBB(loopMBB)
6895 .addReg(dest).addMBB(entryBB);
6896
6897 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6898 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006899 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6900 if (UnitSize >= 8) {
6901 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6902 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6903
6904 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6905 .addReg(destPhi).addImm(0).addReg(scratch));
6906 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006907 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6908 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6909
6910 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6911 .addReg(scratch).addReg(destPhi)
6912 .addImm(UnitSize));
6913 } else {
6914 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6915 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6916 .addImm(UnitSize));
6917
6918 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6919 .addReg(scratch).addReg(destPhi)
6920 .addReg(0).addImm(UnitSize));
6921 }
6922
6923 // Decrement loop variable by UnitSize.
6924 MachineInstrBuilder MIB = BuildMI(BB, dl,
6925 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6926 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6927 MIB->getOperand(5).setReg(ARM::CPSR);
6928 MIB->getOperand(5).setIsDef(true);
6929
6930 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6931 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6932
6933 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6934 BB->addSuccessor(loopMBB);
6935 BB->addSuccessor(exitMBB);
6936
6937 // Add epilogue to handle BytesLeft.
6938 BB = exitMBB;
6939 MachineInstr *StartOfExit = exitMBB->begin();
6940 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6941 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6942
6943 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6944 // [destOut] = STRB_POST(scratch, destLoop, 1)
6945 unsigned srcIn = srcLoop;
6946 unsigned destIn = destLoop;
6947 for (unsigned i = 0; i < BytesLeft; i++) {
6948 unsigned scratch = MRI.createVirtualRegister(TRC);
6949 unsigned srcOut = MRI.createVirtualRegister(TRC);
6950 unsigned destOut = MRI.createVirtualRegister(TRC);
6951 if (isThumb2) {
6952 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6953 TII->get(ldrOpc),scratch)
6954 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6955
6956 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6957 .addReg(scratch).addReg(destIn)
6958 .addImm(1));
6959 } else {
6960 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6961 TII->get(ldrOpc),scratch)
6962 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6963
6964 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6965 .addReg(scratch).addReg(destIn)
6966 .addReg(0).addImm(1));
6967 }
6968 srcIn = srcOut;
6969 destIn = destOut;
6970 }
6971
6972 MI->eraseFromParent(); // The instruction is gone now.
6973 return BB;
6974}
6975
Jim Grosbache801dc42009-12-12 01:40:06 +00006976MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006977ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006978 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006979 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006980 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006981 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006982 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006983 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006984 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006985 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006986 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006987 // The Thumb2 pre-indexed stores have the same MI operands, they just
6988 // define them differently in the .td files from the isel patterns, so
6989 // they need pseudos.
6990 case ARM::t2STR_preidx:
6991 MI->setDesc(TII->get(ARM::t2STR_PRE));
6992 return BB;
6993 case ARM::t2STRB_preidx:
6994 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6995 return BB;
6996 case ARM::t2STRH_preidx:
6997 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6998 return BB;
6999
Jim Grosbach19dec202011-08-05 20:35:44 +00007000 case ARM::STRi_preidx:
7001 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00007002 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00007003 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7004 // Decode the offset.
7005 unsigned Offset = MI->getOperand(4).getImm();
7006 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7007 Offset = ARM_AM::getAM2Offset(Offset);
7008 if (isSub)
7009 Offset = -Offset;
7010
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007011 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00007012 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00007013 .addOperand(MI->getOperand(0)) // Rn_wb
7014 .addOperand(MI->getOperand(1)) // Rt
7015 .addOperand(MI->getOperand(2)) // Rn
7016 .addImm(Offset) // offset (skip GPR==zero_reg)
7017 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007018 .addOperand(MI->getOperand(6))
7019 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00007020 MI->eraseFromParent();
7021 return BB;
7022 }
7023 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00007024 case ARM::STRBr_preidx:
7025 case ARM::STRH_preidx: {
7026 unsigned NewOpc;
7027 switch (MI->getOpcode()) {
7028 default: llvm_unreachable("unexpected opcode!");
7029 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7030 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7031 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7032 }
Jim Grosbach19dec202011-08-05 20:35:44 +00007033 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7034 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7035 MIB.addOperand(MI->getOperand(i));
7036 MI->eraseFromParent();
7037 return BB;
7038 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007039 case ARM::ATOMIC_LOAD_ADD_I8:
7040 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7041 case ARM::ATOMIC_LOAD_ADD_I16:
7042 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7043 case ARM::ATOMIC_LOAD_ADD_I32:
7044 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007045
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007046 case ARM::ATOMIC_LOAD_AND_I8:
7047 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7048 case ARM::ATOMIC_LOAD_AND_I16:
7049 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7050 case ARM::ATOMIC_LOAD_AND_I32:
7051 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007052
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007053 case ARM::ATOMIC_LOAD_OR_I8:
7054 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7055 case ARM::ATOMIC_LOAD_OR_I16:
7056 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7057 case ARM::ATOMIC_LOAD_OR_I32:
7058 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007059
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007060 case ARM::ATOMIC_LOAD_XOR_I8:
7061 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7062 case ARM::ATOMIC_LOAD_XOR_I16:
7063 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7064 case ARM::ATOMIC_LOAD_XOR_I32:
7065 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007066
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007067 case ARM::ATOMIC_LOAD_NAND_I8:
7068 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7069 case ARM::ATOMIC_LOAD_NAND_I16:
7070 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7071 case ARM::ATOMIC_LOAD_NAND_I32:
7072 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007073
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007074 case ARM::ATOMIC_LOAD_SUB_I8:
7075 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7076 case ARM::ATOMIC_LOAD_SUB_I16:
7077 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7078 case ARM::ATOMIC_LOAD_SUB_I32:
7079 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007080
Jim Grosbachf7da8822011-04-26 19:44:18 +00007081 case ARM::ATOMIC_LOAD_MIN_I8:
7082 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7083 case ARM::ATOMIC_LOAD_MIN_I16:
7084 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7085 case ARM::ATOMIC_LOAD_MIN_I32:
7086 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7087
7088 case ARM::ATOMIC_LOAD_MAX_I8:
7089 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7090 case ARM::ATOMIC_LOAD_MAX_I16:
7091 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7092 case ARM::ATOMIC_LOAD_MAX_I32:
7093 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7094
7095 case ARM::ATOMIC_LOAD_UMIN_I8:
7096 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7097 case ARM::ATOMIC_LOAD_UMIN_I16:
7098 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7099 case ARM::ATOMIC_LOAD_UMIN_I32:
7100 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7101
7102 case ARM::ATOMIC_LOAD_UMAX_I8:
7103 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7104 case ARM::ATOMIC_LOAD_UMAX_I16:
7105 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7106 case ARM::ATOMIC_LOAD_UMAX_I32:
7107 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7108
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007109 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7110 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7111 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00007112
7113 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7114 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7115 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007116
Eli Friedman2bdffe42011-08-31 00:31:29 +00007117
7118 case ARM::ATOMADD6432:
7119 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007120 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7121 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007122 case ARM::ATOMSUB6432:
7123 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007124 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7125 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007126 case ARM::ATOMOR6432:
7127 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007128 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007129 case ARM::ATOMXOR6432:
7130 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007131 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007132 case ARM::ATOMAND6432:
7133 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007134 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007135 case ARM::ATOMSWAP6432:
7136 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00007137 case ARM::ATOMCMPXCHG6432:
7138 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7139 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7140 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007141 case ARM::ATOMMIN6432:
7142 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7143 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7144 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007145 /*IsMinMax*/ true, ARMCC::LT);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007146 case ARM::ATOMMAX6432:
7147 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7148 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7149 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7150 /*IsMinMax*/ true, ARMCC::GE);
7151 case ARM::ATOMUMIN6432:
7152 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7153 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7154 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007155 /*IsMinMax*/ true, ARMCC::LO);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007156 case ARM::ATOMUMAX6432:
7157 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7158 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7159 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7160 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007161
Evan Cheng007ea272009-08-12 05:17:19 +00007162 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00007163 // To "insert" a SELECT_CC instruction, we actually have to insert the
7164 // diamond control-flow pattern. The incoming instruction knows the
7165 // destination vreg to set, the condition code register to branch on, the
7166 // true/false values to select between, and a branch opcode to use.
7167 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007168 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00007169 ++It;
7170
7171 // thisMBB:
7172 // ...
7173 // TrueVal = ...
7174 // cmpTY ccX, r1, r2
7175 // bCC copy1MBB
7176 // fallthrough --> copy0MBB
7177 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007178 MachineFunction *F = BB->getParent();
7179 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7180 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00007181 F->insert(It, copy0MBB);
7182 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00007183
7184 // Transfer the remainder of BB and its successor edges to sinkMBB.
7185 sinkMBB->splice(sinkMBB->begin(), BB,
7186 llvm::next(MachineBasicBlock::iterator(MI)),
7187 BB->end());
7188 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7189
Dan Gohman258c58c2010-07-06 15:49:48 +00007190 BB->addSuccessor(copy0MBB);
7191 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00007192
Dan Gohman14152b42010-07-06 20:24:04 +00007193 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7194 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7195
Evan Chenga8e29892007-01-19 07:51:42 +00007196 // copy0MBB:
7197 // %FalseValue = ...
7198 // # fallthrough to sinkMBB
7199 BB = copy0MBB;
7200
7201 // Update machine-CFG edges
7202 BB->addSuccessor(sinkMBB);
7203
7204 // sinkMBB:
7205 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7206 // ...
7207 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007208 BuildMI(*BB, BB->begin(), dl,
7209 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007210 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7211 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7212
Dan Gohman14152b42010-07-06 20:24:04 +00007213 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007214 return BB;
7215 }
Evan Cheng86198642009-08-07 00:34:42 +00007216
Evan Cheng218977b2010-07-13 19:27:42 +00007217 case ARM::BCCi64:
7218 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007219 // If there is an unconditional branch to the other successor, remove it.
7220 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007221
Evan Cheng218977b2010-07-13 19:27:42 +00007222 // Compare both parts that make up the double comparison separately for
7223 // equality.
7224 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7225
7226 unsigned LHS1 = MI->getOperand(1).getReg();
7227 unsigned LHS2 = MI->getOperand(2).getReg();
7228 if (RHSisZero) {
7229 AddDefaultPred(BuildMI(BB, dl,
7230 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7231 .addReg(LHS1).addImm(0));
7232 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7233 .addReg(LHS2).addImm(0)
7234 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7235 } else {
7236 unsigned RHS1 = MI->getOperand(3).getReg();
7237 unsigned RHS2 = MI->getOperand(4).getReg();
7238 AddDefaultPred(BuildMI(BB, dl,
7239 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7240 .addReg(LHS1).addReg(RHS1));
7241 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7242 .addReg(LHS2).addReg(RHS2)
7243 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7244 }
7245
7246 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7247 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7248 if (MI->getOperand(0).getImm() == ARMCC::NE)
7249 std::swap(destMBB, exitMBB);
7250
7251 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7252 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007253 if (isThumb2)
7254 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7255 else
7256 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007257
7258 MI->eraseFromParent(); // The pseudo instruction is gone now.
7259 return BB;
7260 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007261
Bill Wendling5bc85282011-10-17 20:37:20 +00007262 case ARM::Int_eh_sjlj_setjmp:
7263 case ARM::Int_eh_sjlj_setjmp_nofp:
7264 case ARM::tInt_eh_sjlj_setjmp:
7265 case ARM::t2Int_eh_sjlj_setjmp:
7266 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7267 EmitSjLjDispatchBlock(MI, BB);
7268 return BB;
7269
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007270 case ARM::ABS:
7271 case ARM::t2ABS: {
7272 // To insert an ABS instruction, we have to insert the
7273 // diamond control-flow pattern. The incoming instruction knows the
7274 // source vreg to test against 0, the destination vreg to set,
7275 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007276 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007277 // It transforms
7278 // V1 = ABS V0
7279 // into
7280 // V2 = MOVS V0
7281 // BCC (branch to SinkBB if V0 >= 0)
7282 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007283 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007284 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7285 MachineFunction::iterator BBI = BB;
7286 ++BBI;
7287 MachineFunction *Fn = BB->getParent();
7288 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7289 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7290 Fn->insert(BBI, RSBBB);
7291 Fn->insert(BBI, SinkBB);
7292
7293 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7294 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7295 bool isThumb2 = Subtarget->isThumb2();
7296 MachineRegisterInfo &MRI = Fn->getRegInfo();
7297 // In Thumb mode S must not be specified if source register is the SP or
7298 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007299 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7300 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7301 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007302
7303 // Transfer the remainder of BB and its successor edges to sinkMBB.
7304 SinkBB->splice(SinkBB->begin(), BB,
7305 llvm::next(MachineBasicBlock::iterator(MI)),
7306 BB->end());
7307 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7308
7309 BB->addSuccessor(RSBBB);
7310 BB->addSuccessor(SinkBB);
7311
7312 // fall through to SinkMBB
7313 RSBBB->addSuccessor(SinkBB);
7314
Manman Ren307473d2012-06-15 21:32:12 +00007315 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007316 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007317 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7318 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007319
7320 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007321 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007322 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7323 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7324
7325 // insert rsbri in RSBBB
7326 // Note: BCC and rsbri will be converted into predicated rsbmi
7327 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007328 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007329 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007330 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007331 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7332
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007333 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007334 // reuse ABSDstReg to not change uses of ABS instruction
7335 BuildMI(*SinkBB, SinkBB->begin(), dl,
7336 TII->get(ARM::PHI), ABSDstReg)
7337 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007338 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007339
7340 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007341 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007342
7343 // return last added BB
7344 return SinkBB;
7345 }
Manman Ren68f25572012-06-01 19:33:18 +00007346 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007347 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007348 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007349 }
7350}
7351
Evan Cheng37fefc22011-08-30 19:09:48 +00007352void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7353 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007354 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007355 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7356 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7357 return;
7358 }
7359
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007360 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007361 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7362 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7363 // operand is still set to noreg. If needed, set the optional operand's
7364 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007365 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007366 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007367
Andrew Trick3be654f2011-09-21 02:20:46 +00007368 // Rename pseudo opcodes.
7369 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7370 if (NewOpc) {
7371 const ARMBaseInstrInfo *TII =
7372 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007373 MCID = &TII->get(NewOpc);
7374
7375 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7376 "converted opcode should be the same except for cc_out");
7377
7378 MI->setDesc(*MCID);
7379
7380 // Add the optional cc_out operand
7381 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007382 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007383 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007384
7385 // Any ARM instruction that sets the 's' bit should specify an optional
7386 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007387 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007388 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007389 return;
7390 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007391 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7392 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007393 bool definesCPSR = false;
7394 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007395 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007396 i != e; ++i) {
7397 const MachineOperand &MO = MI->getOperand(i);
7398 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7399 definesCPSR = true;
7400 if (MO.isDead())
7401 deadCPSR = true;
7402 MI->RemoveOperand(i);
7403 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007404 }
7405 }
Andrew Trick4815d562011-09-20 03:17:40 +00007406 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007407 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007408 return;
7409 }
7410 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007411 if (deadCPSR) {
7412 assert(!MI->getOperand(ccOutIdx).getReg() &&
7413 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007414 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007415 }
Andrew Trick4815d562011-09-20 03:17:40 +00007416
Andrew Trick3be654f2011-09-21 02:20:46 +00007417 // If this instruction was defined with an optional CPSR def and its dag node
7418 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007419 MachineOperand &MO = MI->getOperand(ccOutIdx);
7420 MO.setReg(ARM::CPSR);
7421 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007422}
7423
Evan Chenga8e29892007-01-19 07:51:42 +00007424//===----------------------------------------------------------------------===//
7425// ARM Optimization Hooks
7426//===----------------------------------------------------------------------===//
7427
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007428// Helper function that checks if N is a null or all ones constant.
7429static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7430 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7431 if (!C)
7432 return false;
7433 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7434}
7435
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007436// Return true if N is conditionally 0 or all ones.
7437// Detects these expressions where cc is an i1 value:
7438//
7439// (select cc 0, y) [AllOnes=0]
7440// (select cc y, 0) [AllOnes=0]
7441// (zext cc) [AllOnes=0]
7442// (sext cc) [AllOnes=0/1]
7443// (select cc -1, y) [AllOnes=1]
7444// (select cc y, -1) [AllOnes=1]
7445//
7446// Invert is set when N is the null/all ones constant when CC is false.
7447// OtherOp is set to the alternative value of N.
7448static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7449 SDValue &CC, bool &Invert,
7450 SDValue &OtherOp,
7451 SelectionDAG &DAG) {
7452 switch (N->getOpcode()) {
7453 default: return false;
7454 case ISD::SELECT: {
7455 CC = N->getOperand(0);
7456 SDValue N1 = N->getOperand(1);
7457 SDValue N2 = N->getOperand(2);
7458 if (isZeroOrAllOnes(N1, AllOnes)) {
7459 Invert = false;
7460 OtherOp = N2;
7461 return true;
7462 }
7463 if (isZeroOrAllOnes(N2, AllOnes)) {
7464 Invert = true;
7465 OtherOp = N1;
7466 return true;
7467 }
7468 return false;
7469 }
7470 case ISD::ZERO_EXTEND:
7471 // (zext cc) can never be the all ones value.
7472 if (AllOnes)
7473 return false;
7474 // Fall through.
7475 case ISD::SIGN_EXTEND: {
7476 EVT VT = N->getValueType(0);
7477 CC = N->getOperand(0);
7478 if (CC.getValueType() != MVT::i1)
7479 return false;
7480 Invert = !AllOnes;
7481 if (AllOnes)
7482 // When looking for an AllOnes constant, N is an sext, and the 'other'
7483 // value is 0.
7484 OtherOp = DAG.getConstant(0, VT);
7485 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7486 // When looking for a 0 constant, N can be zext or sext.
7487 OtherOp = DAG.getConstant(1, VT);
7488 else
7489 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7490 return true;
7491 }
7492 }
7493}
7494
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007495// Combine a constant select operand into its use:
7496//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007497// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7498// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7499// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7500// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7501// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007502//
7503// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007504// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007505//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007506// Also recognize sext/zext from i1:
7507//
7508// (add (zext cc), x) -> (select cc (add x, 1), x)
7509// (add (sext cc), x) -> (select cc (add x, -1), x)
7510//
7511// These transformations eventually create predicated instructions.
7512//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007513// @param N The node to transform.
7514// @param Slct The N operand that is a select.
7515// @param OtherOp The other N operand (x above).
7516// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007517// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007518// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007519static
7520SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007521 TargetLowering::DAGCombinerInfo &DCI,
7522 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007523 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007524 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007525 SDValue NonConstantVal;
7526 SDValue CCOp;
7527 bool SwapSelectOps;
7528 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7529 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007530 return SDValue();
7531
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007532 // Slct is now know to be the desired identity constant when CC is true.
7533 SDValue TrueVal = OtherOp;
7534 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7535 OtherOp, NonConstantVal);
7536 // Unless SwapSelectOps says CC should be false.
7537 if (SwapSelectOps)
7538 std::swap(TrueVal, FalseVal);
7539
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007540 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007541 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007542}
7543
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007544// Attempt combineSelectAndUse on each operand of a commutative operator N.
7545static
7546SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7547 TargetLowering::DAGCombinerInfo &DCI) {
7548 SDValue N0 = N->getOperand(0);
7549 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007550 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007551 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7552 if (Result.getNode())
7553 return Result;
7554 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007555 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007556 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7557 if (Result.getNode())
7558 return Result;
7559 }
7560 return SDValue();
7561}
7562
Eric Christopherfa6f5912011-06-29 21:10:36 +00007563// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007564// (only after legalization).
7565static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7566 TargetLowering::DAGCombinerInfo &DCI,
7567 const ARMSubtarget *Subtarget) {
7568
7569 // Only perform optimization if after legalize, and if NEON is available. We
7570 // also expected both operands to be BUILD_VECTORs.
7571 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7572 || N0.getOpcode() != ISD::BUILD_VECTOR
7573 || N1.getOpcode() != ISD::BUILD_VECTOR)
7574 return SDValue();
7575
7576 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7577 EVT VT = N->getValueType(0);
7578 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7579 return SDValue();
7580
7581 // Check that the vector operands are of the right form.
7582 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7583 // operands, where N is the size of the formed vector.
7584 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7585 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007586
7587 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007588 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007589 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007590 SDValue Vec = N0->getOperand(0)->getOperand(0);
7591 SDNode *V = Vec.getNode();
7592 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007593
Eric Christopherfa6f5912011-06-29 21:10:36 +00007594 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007595 // check to see if each of their operands are an EXTRACT_VECTOR with
7596 // the same vector and appropriate index.
7597 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7598 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7599 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007600
Tanya Lattner189531f2011-06-14 23:48:48 +00007601 SDValue ExtVec0 = N0->getOperand(i);
7602 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007603
Tanya Lattner189531f2011-06-14 23:48:48 +00007604 // First operand is the vector, verify its the same.
7605 if (V != ExtVec0->getOperand(0).getNode() ||
7606 V != ExtVec1->getOperand(0).getNode())
7607 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007608
Tanya Lattner189531f2011-06-14 23:48:48 +00007609 // Second is the constant, verify its correct.
7610 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7611 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007612
Tanya Lattner189531f2011-06-14 23:48:48 +00007613 // For the constant, we want to see all the even or all the odd.
7614 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7615 || C1->getZExtValue() != nextIndex+1)
7616 return SDValue();
7617
7618 // Increment index.
7619 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007620 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007621 return SDValue();
7622 }
7623
7624 // Create VPADDL node.
7625 SelectionDAG &DAG = DCI.DAG;
7626 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007627
7628 // Build operand list.
7629 SmallVector<SDValue, 8> Ops;
7630 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7631 TLI.getPointerTy()));
7632
7633 // Input is the vector.
7634 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007635
Tanya Lattner189531f2011-06-14 23:48:48 +00007636 // Get widened type and narrowed type.
7637 MVT widenType;
7638 unsigned numElem = VT.getVectorNumElements();
7639 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7640 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7641 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7642 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7643 default:
Craig Topperbc219812012-02-07 02:50:20 +00007644 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007645 }
7646
7647 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7648 widenType, &Ops[0], Ops.size());
7649 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7650}
7651
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007652static SDValue findMUL_LOHI(SDValue V) {
7653 if (V->getOpcode() == ISD::UMUL_LOHI ||
7654 V->getOpcode() == ISD::SMUL_LOHI)
7655 return V;
7656 return SDValue();
7657}
7658
7659static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7660 TargetLowering::DAGCombinerInfo &DCI,
7661 const ARMSubtarget *Subtarget) {
7662
7663 if (Subtarget->isThumb1Only()) return SDValue();
7664
7665 // Only perform the checks after legalize when the pattern is available.
7666 if (DCI.isBeforeLegalize()) return SDValue();
7667
7668 // Look for multiply add opportunities.
7669 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7670 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7671 // a glue link from the first add to the second add.
7672 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7673 // a S/UMLAL instruction.
7674 // loAdd UMUL_LOHI
7675 // \ / :lo \ :hi
7676 // \ / \ [no multiline comment]
7677 // ADDC | hiAdd
7678 // \ :glue / /
7679 // \ / /
7680 // ADDE
7681 //
7682 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7683 SDValue AddcOp0 = AddcNode->getOperand(0);
7684 SDValue AddcOp1 = AddcNode->getOperand(1);
7685
7686 // Check if the two operands are from the same mul_lohi node.
7687 if (AddcOp0.getNode() == AddcOp1.getNode())
7688 return SDValue();
7689
7690 assert(AddcNode->getNumValues() == 2 &&
7691 AddcNode->getValueType(0) == MVT::i32 &&
7692 AddcNode->getValueType(1) == MVT::Glue &&
7693 "Expect ADDC with two result values: i32, glue");
7694
7695 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7696 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7697 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7698 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7699 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7700 return SDValue();
7701
7702 // Look for the glued ADDE.
7703 SDNode* AddeNode = AddcNode->getGluedUser();
7704 if (AddeNode == NULL)
7705 return SDValue();
7706
7707 // Make sure it is really an ADDE.
7708 if (AddeNode->getOpcode() != ISD::ADDE)
7709 return SDValue();
7710
7711 assert(AddeNode->getNumOperands() == 3 &&
7712 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7713 "ADDE node has the wrong inputs");
7714
7715 // Check for the triangle shape.
7716 SDValue AddeOp0 = AddeNode->getOperand(0);
7717 SDValue AddeOp1 = AddeNode->getOperand(1);
7718
7719 // Make sure that the ADDE operands are not coming from the same node.
7720 if (AddeOp0.getNode() == AddeOp1.getNode())
7721 return SDValue();
7722
7723 // Find the MUL_LOHI node walking up ADDE's operands.
7724 bool IsLeftOperandMUL = false;
7725 SDValue MULOp = findMUL_LOHI(AddeOp0);
7726 if (MULOp == SDValue())
7727 MULOp = findMUL_LOHI(AddeOp1);
7728 else
7729 IsLeftOperandMUL = true;
7730 if (MULOp == SDValue())
7731 return SDValue();
7732
7733 // Figure out the right opcode.
7734 unsigned Opc = MULOp->getOpcode();
7735 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7736
7737 // Figure out the high and low input values to the MLAL node.
7738 SDValue* HiMul = &MULOp;
7739 SDValue* HiAdd = NULL;
7740 SDValue* LoMul = NULL;
7741 SDValue* LowAdd = NULL;
7742
7743 if (IsLeftOperandMUL)
7744 HiAdd = &AddeOp1;
7745 else
7746 HiAdd = &AddeOp0;
7747
7748
7749 if (AddcOp0->getOpcode() == Opc) {
7750 LoMul = &AddcOp0;
7751 LowAdd = &AddcOp1;
7752 }
7753 if (AddcOp1->getOpcode() == Opc) {
7754 LoMul = &AddcOp1;
7755 LowAdd = &AddcOp0;
7756 }
7757
7758 if (LoMul == NULL)
7759 return SDValue();
7760
7761 if (LoMul->getNode() != HiMul->getNode())
7762 return SDValue();
7763
7764 // Create the merged node.
7765 SelectionDAG &DAG = DCI.DAG;
7766
7767 // Build operand list.
7768 SmallVector<SDValue, 8> Ops;
7769 Ops.push_back(LoMul->getOperand(0));
7770 Ops.push_back(LoMul->getOperand(1));
7771 Ops.push_back(*LowAdd);
7772 Ops.push_back(*HiAdd);
7773
7774 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7775 DAG.getVTList(MVT::i32, MVT::i32),
7776 &Ops[0], Ops.size());
7777
7778 // Replace the ADDs' nodes uses by the MLA node's values.
7779 SDValue HiMLALResult(MLALNode.getNode(), 1);
7780 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7781
7782 SDValue LoMLALResult(MLALNode.getNode(), 0);
7783 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7784
7785 // Return original node to notify the driver to stop replacing.
7786 SDValue resNode(AddcNode, 0);
7787 return resNode;
7788}
7789
7790/// PerformADDCCombine - Target-specific dag combine transform from
7791/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7792static SDValue PerformADDCCombine(SDNode *N,
7793 TargetLowering::DAGCombinerInfo &DCI,
7794 const ARMSubtarget *Subtarget) {
7795
7796 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7797
7798}
7799
Bob Wilson3d5792a2010-07-29 20:34:14 +00007800/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7801/// operands N0 and N1. This is a helper for PerformADDCombine that is
7802/// called with the default operands, and if that fails, with commuted
7803/// operands.
7804static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007805 TargetLowering::DAGCombinerInfo &DCI,
7806 const ARMSubtarget *Subtarget){
7807
7808 // Attempt to create vpaddl for this add.
7809 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7810 if (Result.getNode())
7811 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007812
Chris Lattnerd1980a52009-03-12 06:52:53 +00007813 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007814 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007815 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7816 if (Result.getNode()) return Result;
7817 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007818 return SDValue();
7819}
7820
Bob Wilson3d5792a2010-07-29 20:34:14 +00007821/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7822///
7823static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007824 TargetLowering::DAGCombinerInfo &DCI,
7825 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007826 SDValue N0 = N->getOperand(0);
7827 SDValue N1 = N->getOperand(1);
7828
7829 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007830 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007831 if (Result.getNode())
7832 return Result;
7833
7834 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007835 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007836}
7837
Chris Lattnerd1980a52009-03-12 06:52:53 +00007838/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007839///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007840static SDValue PerformSUBCombine(SDNode *N,
7841 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007842 SDValue N0 = N->getOperand(0);
7843 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007844
Chris Lattnerd1980a52009-03-12 06:52:53 +00007845 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007846 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007847 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7848 if (Result.getNode()) return Result;
7849 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007850
Chris Lattnerd1980a52009-03-12 06:52:53 +00007851 return SDValue();
7852}
7853
Evan Cheng463d3582011-03-31 19:38:48 +00007854/// PerformVMULCombine
7855/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7856/// special multiplier accumulator forwarding.
7857/// vmul d3, d0, d2
7858/// vmla d3, d1, d2
7859/// is faster than
7860/// vadd d3, d0, d1
7861/// vmul d3, d3, d2
7862static SDValue PerformVMULCombine(SDNode *N,
7863 TargetLowering::DAGCombinerInfo &DCI,
7864 const ARMSubtarget *Subtarget) {
7865 if (!Subtarget->hasVMLxForwarding())
7866 return SDValue();
7867
7868 SelectionDAG &DAG = DCI.DAG;
7869 SDValue N0 = N->getOperand(0);
7870 SDValue N1 = N->getOperand(1);
7871 unsigned Opcode = N0.getOpcode();
7872 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7873 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007874 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007875 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7876 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7877 return SDValue();
7878 std::swap(N0, N1);
7879 }
7880
7881 EVT VT = N->getValueType(0);
7882 DebugLoc DL = N->getDebugLoc();
7883 SDValue N00 = N0->getOperand(0);
7884 SDValue N01 = N0->getOperand(1);
7885 return DAG.getNode(Opcode, DL, VT,
7886 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7887 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7888}
7889
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007890static SDValue PerformMULCombine(SDNode *N,
7891 TargetLowering::DAGCombinerInfo &DCI,
7892 const ARMSubtarget *Subtarget) {
7893 SelectionDAG &DAG = DCI.DAG;
7894
7895 if (Subtarget->isThumb1Only())
7896 return SDValue();
7897
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007898 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7899 return SDValue();
7900
7901 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007902 if (VT.is64BitVector() || VT.is128BitVector())
7903 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007904 if (VT != MVT::i32)
7905 return SDValue();
7906
7907 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7908 if (!C)
7909 return SDValue();
7910
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007911 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007912 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007913
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007914 ShiftAmt = ShiftAmt & (32 - 1);
7915 SDValue V = N->getOperand(0);
7916 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007917
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007918 SDValue Res;
7919 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007920
7921 if (MulAmt >= 0) {
7922 if (isPowerOf2_32(MulAmt - 1)) {
7923 // (mul x, 2^N + 1) => (add (shl x, N), x)
7924 Res = DAG.getNode(ISD::ADD, DL, VT,
7925 V,
7926 DAG.getNode(ISD::SHL, DL, VT,
7927 V,
7928 DAG.getConstant(Log2_32(MulAmt - 1),
7929 MVT::i32)));
7930 } else if (isPowerOf2_32(MulAmt + 1)) {
7931 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7932 Res = DAG.getNode(ISD::SUB, DL, VT,
7933 DAG.getNode(ISD::SHL, DL, VT,
7934 V,
7935 DAG.getConstant(Log2_32(MulAmt + 1),
7936 MVT::i32)),
7937 V);
7938 } else
7939 return SDValue();
7940 } else {
7941 uint64_t MulAmtAbs = -MulAmt;
7942 if (isPowerOf2_32(MulAmtAbs + 1)) {
7943 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7944 Res = DAG.getNode(ISD::SUB, DL, VT,
7945 V,
7946 DAG.getNode(ISD::SHL, DL, VT,
7947 V,
7948 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7949 MVT::i32)));
7950 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7951 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7952 Res = DAG.getNode(ISD::ADD, DL, VT,
7953 V,
7954 DAG.getNode(ISD::SHL, DL, VT,
7955 V,
7956 DAG.getConstant(Log2_32(MulAmtAbs-1),
7957 MVT::i32)));
7958 Res = DAG.getNode(ISD::SUB, DL, VT,
7959 DAG.getConstant(0, MVT::i32),Res);
7960
7961 } else
7962 return SDValue();
7963 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007964
7965 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007966 Res = DAG.getNode(ISD::SHL, DL, VT,
7967 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007968
7969 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007970 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007971 return SDValue();
7972}
7973
Owen Anderson080c0922010-11-05 19:27:46 +00007974static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007975 TargetLowering::DAGCombinerInfo &DCI,
7976 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007977
Owen Anderson080c0922010-11-05 19:27:46 +00007978 // Attempt to use immediate-form VBIC
7979 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7980 DebugLoc dl = N->getDebugLoc();
7981 EVT VT = N->getValueType(0);
7982 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007983
Tanya Lattner0433b212011-04-07 15:24:20 +00007984 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7985 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007986
Owen Anderson080c0922010-11-05 19:27:46 +00007987 APInt SplatBits, SplatUndef;
7988 unsigned SplatBitSize;
7989 bool HasAnyUndefs;
7990 if (BVN &&
7991 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7992 if (SplatBitSize <= 64) {
7993 EVT VbicVT;
7994 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7995 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007996 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007997 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007998 if (Val.getNode()) {
7999 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008000 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00008001 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008002 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00008003 }
8004 }
8005 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008006
Evan Chengc892aeb2012-02-23 01:19:06 +00008007 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008008 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8009 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8010 if (Result.getNode())
8011 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008012 }
8013
Owen Anderson080c0922010-11-05 19:27:46 +00008014 return SDValue();
8015}
8016
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008017/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8018static SDValue PerformORCombine(SDNode *N,
8019 TargetLowering::DAGCombinerInfo &DCI,
8020 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00008021 // Attempt to use immediate-form VORR
8022 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8023 DebugLoc dl = N->getDebugLoc();
8024 EVT VT = N->getValueType(0);
8025 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008026
Tanya Lattner0433b212011-04-07 15:24:20 +00008027 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8028 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008029
Owen Anderson60f48702010-11-03 23:15:26 +00008030 APInt SplatBits, SplatUndef;
8031 unsigned SplatBitSize;
8032 bool HasAnyUndefs;
8033 if (BVN && Subtarget->hasNEON() &&
8034 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8035 if (SplatBitSize <= 64) {
8036 EVT VorrVT;
8037 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8038 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008039 DAG, VorrVT, VT.is128BitVector(),
8040 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00008041 if (Val.getNode()) {
8042 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008043 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00008044 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008045 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00008046 }
8047 }
8048 }
8049
Evan Chengc892aeb2012-02-23 01:19:06 +00008050 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008051 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8052 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8053 if (Result.getNode())
8054 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008055 }
8056
Nadav Rotemdf832032012-08-13 18:52:44 +00008057 // The code below optimizes (or (and X, Y), Z).
8058 // The AND operand needs to have a single user to make these optimizations
8059 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008060 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00008061 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008062 return SDValue();
8063 SDValue N1 = N->getOperand(1);
8064
8065 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8066 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8067 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8068 APInt SplatUndef;
8069 unsigned SplatBitSize;
8070 bool HasAnyUndefs;
8071
8072 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8073 APInt SplatBits0;
8074 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8075 HasAnyUndefs) && !HasAnyUndefs) {
8076 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8077 APInt SplatBits1;
8078 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8079 HasAnyUndefs) && !HasAnyUndefs &&
8080 SplatBits0 == ~SplatBits1) {
8081 // Canonicalize the vector type to make instruction selection simpler.
8082 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8083 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8084 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00008085 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008086 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8087 }
8088 }
8089 }
8090
Jim Grosbach54238562010-07-17 03:30:54 +00008091 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8092 // reasonable.
8093
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008094 // BFI is only available on V6T2+
8095 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8096 return SDValue();
8097
Jim Grosbach54238562010-07-17 03:30:54 +00008098 DebugLoc DL = N->getDebugLoc();
8099 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008100 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00008101 //
8102 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008103 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008104 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008105 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008106 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00008107 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008108
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008109 if (VT != MVT::i32)
8110 return SDValue();
8111
Evan Cheng30fb13f2010-12-13 20:32:54 +00008112 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00008113
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008114 // The value and the mask need to be constants so we can verify this is
8115 // actually a bitfield set. If the mask is 0xffff, we can do better
8116 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00008117 SDValue MaskOp = N0.getOperand(1);
8118 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8119 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008120 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008121 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008122 if (Mask == 0xffff)
8123 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008124 SDValue Res;
8125 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008126 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8127 if (N1C) {
8128 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008129 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00008130 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008131
Evan Chenga9688c42010-12-11 04:11:38 +00008132 if (ARM::isBitFieldInvertedMask(Mask)) {
8133 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008134
Evan Cheng30fb13f2010-12-13 20:32:54 +00008135 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00008136 DAG.getConstant(Val, MVT::i32),
8137 DAG.getConstant(Mask, MVT::i32));
8138
8139 // Do not add new nodes to DAG combiner worklist.
8140 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008141 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008142 }
Jim Grosbach54238562010-07-17 03:30:54 +00008143 } else if (N1.getOpcode() == ISD::AND) {
8144 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008145 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8146 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00008147 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008148 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008149
Eric Christopher29aeed12011-03-26 01:21:03 +00008150 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8151 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00008152 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008153 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008154 // The pack halfword instruction works better for masks that fit it,
8155 // so use that when it's available.
8156 if (Subtarget->hasT2ExtractPack() &&
8157 (Mask == 0xffff || Mask == 0xffff0000))
8158 return SDValue();
8159 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00008160 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00008161 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00008162 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00008163 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00008164 DAG.getConstant(Mask, MVT::i32));
8165 // Do not add new nodes to DAG combiner worklist.
8166 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008167 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008168 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008169 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008170 // The pack halfword instruction works better for masks that fit it,
8171 // so use that when it's available.
8172 if (Subtarget->hasT2ExtractPack() &&
8173 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8174 return SDValue();
8175 // 2b
8176 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008177 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00008178 DAG.getConstant(lsb, MVT::i32));
8179 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00008180 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00008181 // Do not add new nodes to DAG combiner worklist.
8182 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008183 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008184 }
8185 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008186
Evan Cheng30fb13f2010-12-13 20:32:54 +00008187 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8188 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8189 ARM::isBitFieldInvertedMask(~Mask)) {
8190 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8191 // where lsb(mask) == #shamt and masked bits of B are known zero.
8192 SDValue ShAmt = N00.getOperand(1);
8193 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8194 unsigned LSB = CountTrailingZeros_32(Mask);
8195 if (ShAmtC != LSB)
8196 return SDValue();
8197
8198 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8199 DAG.getConstant(~Mask, MVT::i32));
8200
8201 // Do not add new nodes to DAG combiner worklist.
8202 DCI.CombineTo(N, Res, false);
8203 }
8204
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008205 return SDValue();
8206}
8207
Evan Chengc892aeb2012-02-23 01:19:06 +00008208static SDValue PerformXORCombine(SDNode *N,
8209 TargetLowering::DAGCombinerInfo &DCI,
8210 const ARMSubtarget *Subtarget) {
8211 EVT VT = N->getValueType(0);
8212 SelectionDAG &DAG = DCI.DAG;
8213
8214 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8215 return SDValue();
8216
8217 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008218 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8219 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8220 if (Result.getNode())
8221 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008222 }
8223
8224 return SDValue();
8225}
8226
Evan Chengbf188ae2011-06-15 01:12:31 +00008227/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8228/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008229static SDValue PerformBFICombine(SDNode *N,
8230 TargetLowering::DAGCombinerInfo &DCI) {
8231 SDValue N1 = N->getOperand(1);
8232 if (N1.getOpcode() == ISD::AND) {
8233 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8234 if (!N11C)
8235 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008236 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8237 unsigned LSB = CountTrailingZeros_32(~InvMask);
8238 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8239 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008240 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008241 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008242 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8243 N->getOperand(0), N1.getOperand(0),
8244 N->getOperand(2));
8245 }
8246 return SDValue();
8247}
8248
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008249/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8250/// ARMISD::VMOVRRD.
8251static SDValue PerformVMOVRRDCombine(SDNode *N,
8252 TargetLowering::DAGCombinerInfo &DCI) {
8253 // vmovrrd(vmovdrr x, y) -> x,y
8254 SDValue InDouble = N->getOperand(0);
8255 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8256 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008257
8258 // vmovrrd(load f64) -> (load i32), (load i32)
8259 SDNode *InNode = InDouble.getNode();
8260 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8261 InNode->getValueType(0) == MVT::f64 &&
8262 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8263 !cast<LoadSDNode>(InNode)->isVolatile()) {
8264 // TODO: Should this be done for non-FrameIndex operands?
8265 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8266
8267 SelectionDAG &DAG = DCI.DAG;
8268 DebugLoc DL = LD->getDebugLoc();
8269 SDValue BasePtr = LD->getBasePtr();
8270 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8271 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008272 LD->isNonTemporal(), LD->isInvariant(),
8273 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008274
8275 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8276 DAG.getConstant(4, MVT::i32));
8277 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8278 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008279 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008280 std::min(4U, LD->getAlignment() / 2));
8281
8282 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8283 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8284 DCI.RemoveFromWorklist(LD);
8285 DAG.DeleteNode(LD);
8286 return Result;
8287 }
8288
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008289 return SDValue();
8290}
8291
8292/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8293/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8294static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8295 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8296 SDValue Op0 = N->getOperand(0);
8297 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008298 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008299 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008300 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008301 Op1 = Op1.getOperand(0);
8302 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8303 Op0.getNode() == Op1.getNode() &&
8304 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008305 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008306 N->getValueType(0), Op0.getOperand(0));
8307 return SDValue();
8308}
8309
Bob Wilson31600902010-12-21 06:43:19 +00008310/// PerformSTORECombine - Target-specific dag combine xforms for
8311/// ISD::STORE.
8312static SDValue PerformSTORECombine(SDNode *N,
8313 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008314 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008315 if (St->isVolatile())
8316 return SDValue();
8317
Andrew Trick49b446f2012-07-18 18:34:24 +00008318 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008319 // pack all of the elements in one place. Next, store to memory in fewer
8320 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008321 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008322 EVT VT = StVal.getValueType();
8323 if (St->isTruncatingStore() && VT.isVector()) {
8324 SelectionDAG &DAG = DCI.DAG;
8325 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8326 EVT StVT = St->getMemoryVT();
8327 unsigned NumElems = VT.getVectorNumElements();
8328 assert(StVT != VT && "Cannot truncate to the same type");
8329 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8330 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8331
8332 // From, To sizes and ElemCount must be pow of two
8333 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8334
8335 // We are going to use the original vector elt for storing.
8336 // Accumulated smaller vector elements must be a multiple of the store size.
8337 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8338
8339 unsigned SizeRatio = FromEltSz / ToEltSz;
8340 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8341
8342 // Create a type on which we perform the shuffle.
8343 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8344 NumElems*SizeRatio);
8345 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8346
8347 DebugLoc DL = St->getDebugLoc();
8348 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8349 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8350 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8351
8352 // Can't shuffle using an illegal type.
8353 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8354
8355 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8356 DAG.getUNDEF(WideVec.getValueType()),
8357 ShuffleVec.data());
8358 // At this point all of the data is stored at the bottom of the
8359 // register. We now need to save it to mem.
8360
8361 // Find the largest store unit
8362 MVT StoreType = MVT::i8;
8363 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8364 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8365 MVT Tp = (MVT::SimpleValueType)tp;
8366 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8367 StoreType = Tp;
8368 }
8369 // Didn't find a legal store type.
8370 if (!TLI.isTypeLegal(StoreType))
8371 return SDValue();
8372
8373 // Bitcast the original vector into a vector of store-size units
8374 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8375 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8376 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8377 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8378 SmallVector<SDValue, 8> Chains;
8379 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8380 TLI.getPointerTy());
8381 SDValue BasePtr = St->getBasePtr();
8382
8383 // Perform one or more big stores into memory.
8384 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8385 for (unsigned I = 0; I < E; I++) {
8386 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8387 StoreType, ShuffWide,
8388 DAG.getIntPtrConstant(I));
8389 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8390 St->getPointerInfo(), St->isVolatile(),
8391 St->isNonTemporal(), St->getAlignment());
8392 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8393 Increment);
8394 Chains.push_back(Ch);
8395 }
8396 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8397 Chains.size());
8398 }
8399
8400 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008401 return SDValue();
8402
Chad Rosier96b66d62012-04-09 19:38:15 +00008403 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8404 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008405 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008406 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008407 SelectionDAG &DAG = DCI.DAG;
8408 DebugLoc DL = St->getDebugLoc();
8409 SDValue BasePtr = St->getBasePtr();
8410 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8411 StVal.getNode()->getOperand(0), BasePtr,
8412 St->getPointerInfo(), St->isVolatile(),
8413 St->isNonTemporal(), St->getAlignment());
8414
8415 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8416 DAG.getConstant(4, MVT::i32));
8417 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8418 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8419 St->isNonTemporal(),
8420 std::min(4U, St->getAlignment() / 2));
8421 }
8422
8423 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008424 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8425 return SDValue();
8426
Chad Rosier96b66d62012-04-09 19:38:15 +00008427 // Bitcast an i64 store extracted from a vector to f64.
8428 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008429 SelectionDAG &DAG = DCI.DAG;
8430 DebugLoc dl = StVal.getDebugLoc();
8431 SDValue IntVec = StVal.getOperand(0);
8432 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8433 IntVec.getValueType().getVectorNumElements());
8434 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8435 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8436 Vec, StVal.getOperand(1));
8437 dl = N->getDebugLoc();
8438 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8439 // Make the DAGCombiner fold the bitcasts.
8440 DCI.AddToWorklist(Vec.getNode());
8441 DCI.AddToWorklist(ExtElt.getNode());
8442 DCI.AddToWorklist(V.getNode());
8443 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8444 St->getPointerInfo(), St->isVolatile(),
8445 St->isNonTemporal(), St->getAlignment(),
8446 St->getTBAAInfo());
8447}
8448
8449/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8450/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8451/// i64 vector to have f64 elements, since the value can then be loaded
8452/// directly into a VFP register.
8453static bool hasNormalLoadOperand(SDNode *N) {
8454 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8455 for (unsigned i = 0; i < NumElts; ++i) {
8456 SDNode *Elt = N->getOperand(i).getNode();
8457 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8458 return true;
8459 }
8460 return false;
8461}
8462
Bob Wilson75f02882010-09-17 22:59:05 +00008463/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8464/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008465static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8466 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008467 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8468 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8469 // into a pair of GPRs, which is fine when the value is used as a scalar,
8470 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008471 SelectionDAG &DAG = DCI.DAG;
8472 if (N->getNumOperands() == 2) {
8473 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8474 if (RV.getNode())
8475 return RV;
8476 }
Bob Wilson75f02882010-09-17 22:59:05 +00008477
Bob Wilson31600902010-12-21 06:43:19 +00008478 // Load i64 elements as f64 values so that type legalization does not split
8479 // them up into i32 values.
8480 EVT VT = N->getValueType(0);
8481 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8482 return SDValue();
8483 DebugLoc dl = N->getDebugLoc();
8484 SmallVector<SDValue, 8> Ops;
8485 unsigned NumElts = VT.getVectorNumElements();
8486 for (unsigned i = 0; i < NumElts; ++i) {
8487 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8488 Ops.push_back(V);
8489 // Make the DAGCombiner fold the bitcast.
8490 DCI.AddToWorklist(V.getNode());
8491 }
8492 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8493 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8494 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8495}
8496
8497/// PerformInsertEltCombine - Target-specific dag combine xforms for
8498/// ISD::INSERT_VECTOR_ELT.
8499static SDValue PerformInsertEltCombine(SDNode *N,
8500 TargetLowering::DAGCombinerInfo &DCI) {
8501 // Bitcast an i64 load inserted into a vector to f64.
8502 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8503 EVT VT = N->getValueType(0);
8504 SDNode *Elt = N->getOperand(1).getNode();
8505 if (VT.getVectorElementType() != MVT::i64 ||
8506 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8507 return SDValue();
8508
8509 SelectionDAG &DAG = DCI.DAG;
8510 DebugLoc dl = N->getDebugLoc();
8511 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8512 VT.getVectorNumElements());
8513 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8514 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8515 // Make the DAGCombiner fold the bitcasts.
8516 DCI.AddToWorklist(Vec.getNode());
8517 DCI.AddToWorklist(V.getNode());
8518 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8519 Vec, V, N->getOperand(2));
8520 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008521}
8522
Bob Wilsonf20700c2010-10-27 20:38:28 +00008523/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8524/// ISD::VECTOR_SHUFFLE.
8525static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8526 // The LLVM shufflevector instruction does not require the shuffle mask
8527 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8528 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8529 // operands do not match the mask length, they are extended by concatenating
8530 // them with undef vectors. That is probably the right thing for other
8531 // targets, but for NEON it is better to concatenate two double-register
8532 // size vector operands into a single quad-register size vector. Do that
8533 // transformation here:
8534 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8535 // shuffle(concat(v1, v2), undef)
8536 SDValue Op0 = N->getOperand(0);
8537 SDValue Op1 = N->getOperand(1);
8538 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8539 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8540 Op0.getNumOperands() != 2 ||
8541 Op1.getNumOperands() != 2)
8542 return SDValue();
8543 SDValue Concat0Op1 = Op0.getOperand(1);
8544 SDValue Concat1Op1 = Op1.getOperand(1);
8545 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8546 Concat1Op1.getOpcode() != ISD::UNDEF)
8547 return SDValue();
8548 // Skip the transformation if any of the types are illegal.
8549 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8550 EVT VT = N->getValueType(0);
8551 if (!TLI.isTypeLegal(VT) ||
8552 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8553 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8554 return SDValue();
8555
8556 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8557 Op0.getOperand(0), Op1.getOperand(0));
8558 // Translate the shuffle mask.
8559 SmallVector<int, 16> NewMask;
8560 unsigned NumElts = VT.getVectorNumElements();
8561 unsigned HalfElts = NumElts/2;
8562 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8563 for (unsigned n = 0; n < NumElts; ++n) {
8564 int MaskElt = SVN->getMaskElt(n);
8565 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008566 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008567 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008568 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008569 NewElt = HalfElts + MaskElt - NumElts;
8570 NewMask.push_back(NewElt);
8571 }
8572 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8573 DAG.getUNDEF(VT), NewMask.data());
8574}
8575
Bob Wilson1c3ef902011-02-07 17:43:21 +00008576/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8577/// NEON load/store intrinsics to merge base address updates.
8578static SDValue CombineBaseUpdate(SDNode *N,
8579 TargetLowering::DAGCombinerInfo &DCI) {
8580 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8581 return SDValue();
8582
8583 SelectionDAG &DAG = DCI.DAG;
8584 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8585 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8586 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8587 SDValue Addr = N->getOperand(AddrOpIdx);
8588
8589 // Search for a use of the address operand that is an increment.
8590 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8591 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8592 SDNode *User = *UI;
8593 if (User->getOpcode() != ISD::ADD ||
8594 UI.getUse().getResNo() != Addr.getResNo())
8595 continue;
8596
8597 // Check that the add is independent of the load/store. Otherwise, folding
8598 // it would create a cycle.
8599 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8600 continue;
8601
8602 // Find the new opcode for the updating load/store.
8603 bool isLoad = true;
8604 bool isLaneOp = false;
8605 unsigned NewOpc = 0;
8606 unsigned NumVecs = 0;
8607 if (isIntrinsic) {
8608 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8609 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008610 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008611 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8612 NumVecs = 1; break;
8613 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8614 NumVecs = 2; break;
8615 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8616 NumVecs = 3; break;
8617 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8618 NumVecs = 4; break;
8619 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8620 NumVecs = 2; isLaneOp = true; break;
8621 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8622 NumVecs = 3; isLaneOp = true; break;
8623 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8624 NumVecs = 4; isLaneOp = true; break;
8625 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8626 NumVecs = 1; isLoad = false; break;
8627 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8628 NumVecs = 2; isLoad = false; break;
8629 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8630 NumVecs = 3; isLoad = false; break;
8631 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8632 NumVecs = 4; isLoad = false; break;
8633 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8634 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8635 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8636 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8637 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8638 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8639 }
8640 } else {
8641 isLaneOp = true;
8642 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008643 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008644 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8645 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8646 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8647 }
8648 }
8649
8650 // Find the size of memory referenced by the load/store.
8651 EVT VecTy;
8652 if (isLoad)
8653 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008654 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008655 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8656 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8657 if (isLaneOp)
8658 NumBytes /= VecTy.getVectorNumElements();
8659
8660 // If the increment is a constant, it must match the memory ref size.
8661 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8662 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8663 uint64_t IncVal = CInc->getZExtValue();
8664 if (IncVal != NumBytes)
8665 continue;
8666 } else if (NumBytes >= 3 * 16) {
8667 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8668 // separate instructions that make it harder to use a non-constant update.
8669 continue;
8670 }
8671
8672 // Create the new updating load/store node.
8673 EVT Tys[6];
8674 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8675 unsigned n;
8676 for (n = 0; n < NumResultVecs; ++n)
8677 Tys[n] = VecTy;
8678 Tys[n++] = MVT::i32;
8679 Tys[n] = MVT::Other;
8680 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8681 SmallVector<SDValue, 8> Ops;
8682 Ops.push_back(N->getOperand(0)); // incoming chain
8683 Ops.push_back(N->getOperand(AddrOpIdx));
8684 Ops.push_back(Inc);
8685 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8686 Ops.push_back(N->getOperand(i));
8687 }
8688 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8689 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8690 Ops.data(), Ops.size(),
8691 MemInt->getMemoryVT(),
8692 MemInt->getMemOperand());
8693
8694 // Update the uses.
8695 std::vector<SDValue> NewResults;
8696 for (unsigned i = 0; i < NumResultVecs; ++i) {
8697 NewResults.push_back(SDValue(UpdN.getNode(), i));
8698 }
8699 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8700 DCI.CombineTo(N, NewResults);
8701 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8702
8703 break;
Owen Anderson76706012011-04-05 21:48:57 +00008704 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008705 return SDValue();
8706}
8707
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008708/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8709/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8710/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8711/// return true.
8712static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8713 SelectionDAG &DAG = DCI.DAG;
8714 EVT VT = N->getValueType(0);
8715 // vldN-dup instructions only support 64-bit vectors for N > 1.
8716 if (!VT.is64BitVector())
8717 return false;
8718
8719 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8720 SDNode *VLD = N->getOperand(0).getNode();
8721 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8722 return false;
8723 unsigned NumVecs = 0;
8724 unsigned NewOpc = 0;
8725 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8726 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8727 NumVecs = 2;
8728 NewOpc = ARMISD::VLD2DUP;
8729 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8730 NumVecs = 3;
8731 NewOpc = ARMISD::VLD3DUP;
8732 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8733 NumVecs = 4;
8734 NewOpc = ARMISD::VLD4DUP;
8735 } else {
8736 return false;
8737 }
8738
8739 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8740 // numbers match the load.
8741 unsigned VLDLaneNo =
8742 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8743 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8744 UI != UE; ++UI) {
8745 // Ignore uses of the chain result.
8746 if (UI.getUse().getResNo() == NumVecs)
8747 continue;
8748 SDNode *User = *UI;
8749 if (User->getOpcode() != ARMISD::VDUPLANE ||
8750 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8751 return false;
8752 }
8753
8754 // Create the vldN-dup node.
8755 EVT Tys[5];
8756 unsigned n;
8757 for (n = 0; n < NumVecs; ++n)
8758 Tys[n] = VT;
8759 Tys[n] = MVT::Other;
8760 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8761 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8762 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8763 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8764 Ops, 2, VLDMemInt->getMemoryVT(),
8765 VLDMemInt->getMemOperand());
8766
8767 // Update the uses.
8768 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8769 UI != UE; ++UI) {
8770 unsigned ResNo = UI.getUse().getResNo();
8771 // Ignore uses of the chain result.
8772 if (ResNo == NumVecs)
8773 continue;
8774 SDNode *User = *UI;
8775 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8776 }
8777
8778 // Now the vldN-lane intrinsic is dead except for its chain result.
8779 // Update uses of the chain.
8780 std::vector<SDValue> VLDDupResults;
8781 for (unsigned n = 0; n < NumVecs; ++n)
8782 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8783 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8784 DCI.CombineTo(VLD, VLDDupResults);
8785
8786 return true;
8787}
8788
Bob Wilson9e82bf12010-07-14 01:22:12 +00008789/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8790/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008791static SDValue PerformVDUPLANECombine(SDNode *N,
8792 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008793 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008794
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008795 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8796 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8797 if (CombineVLDDUP(N, DCI))
8798 return SDValue(N, 0);
8799
8800 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8801 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008802 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008803 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008804 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008805 return SDValue();
8806
8807 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8808 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8809 // The canonical VMOV for a zero vector uses a 32-bit element size.
8810 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8811 unsigned EltBits;
8812 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8813 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008814 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008815 if (EltSize > VT.getVectorElementType().getSizeInBits())
8816 return SDValue();
8817
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008818 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008819}
8820
Eric Christopherfa6f5912011-06-29 21:10:36 +00008821// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008822// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8823static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8824{
Chad Rosier118c9a02011-06-28 17:26:57 +00008825 integerPart cN;
8826 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008827 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8828 I != E; I++) {
8829 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8830 if (!C)
8831 return false;
8832
Eric Christopherfa6f5912011-06-29 21:10:36 +00008833 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008834 APFloat APF = C->getValueAPF();
8835 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8836 != APFloat::opOK || !isExact)
8837 return false;
8838
8839 c0 = (I == 0) ? cN : c0;
8840 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8841 return false;
8842 }
8843 C = c0;
8844 return true;
8845}
8846
8847/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8848/// can replace combinations of VMUL and VCVT (floating-point to integer)
8849/// when the VMUL has a constant operand that is a power of 2.
8850///
8851/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8852/// vmul.f32 d16, d17, d16
8853/// vcvt.s32.f32 d16, d16
8854/// becomes:
8855/// vcvt.s32.f32 d16, d16, #3
8856static SDValue PerformVCVTCombine(SDNode *N,
8857 TargetLowering::DAGCombinerInfo &DCI,
8858 const ARMSubtarget *Subtarget) {
8859 SelectionDAG &DAG = DCI.DAG;
8860 SDValue Op = N->getOperand(0);
8861
8862 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8863 Op.getOpcode() != ISD::FMUL)
8864 return SDValue();
8865
8866 uint64_t C;
8867 SDValue N0 = Op->getOperand(0);
8868 SDValue ConstVec = Op->getOperand(1);
8869 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8870
Eric Christopherfa6f5912011-06-29 21:10:36 +00008871 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008872 !isConstVecPow2(ConstVec, isSigned, C))
8873 return SDValue();
8874
8875 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8876 Intrinsic::arm_neon_vcvtfp2fxu;
8877 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8878 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008879 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008880 DAG.getConstant(Log2_64(C), MVT::i32));
8881}
8882
8883/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8884/// can replace combinations of VCVT (integer to floating-point) and VDIV
8885/// when the VDIV has a constant operand that is a power of 2.
8886///
8887/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8888/// vcvt.f32.s32 d16, d16
8889/// vdiv.f32 d16, d17, d16
8890/// becomes:
8891/// vcvt.f32.s32 d16, d16, #3
8892static SDValue PerformVDIVCombine(SDNode *N,
8893 TargetLowering::DAGCombinerInfo &DCI,
8894 const ARMSubtarget *Subtarget) {
8895 SelectionDAG &DAG = DCI.DAG;
8896 SDValue Op = N->getOperand(0);
8897 unsigned OpOpcode = Op.getNode()->getOpcode();
8898
8899 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8900 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8901 return SDValue();
8902
8903 uint64_t C;
8904 SDValue ConstVec = N->getOperand(1);
8905 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8906
8907 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8908 !isConstVecPow2(ConstVec, isSigned, C))
8909 return SDValue();
8910
Eric Christopherfa6f5912011-06-29 21:10:36 +00008911 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008912 Intrinsic::arm_neon_vcvtfxu2fp;
8913 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8914 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008915 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008916 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8917}
8918
8919/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008920/// operand of a vector shift operation, where all the elements of the
8921/// build_vector must have the same constant integer value.
8922static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8923 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008924 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008925 Op = Op.getOperand(0);
8926 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8927 APInt SplatBits, SplatUndef;
8928 unsigned SplatBitSize;
8929 bool HasAnyUndefs;
8930 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8931 HasAnyUndefs, ElementBits) ||
8932 SplatBitSize > ElementBits)
8933 return false;
8934 Cnt = SplatBits.getSExtValue();
8935 return true;
8936}
8937
8938/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8939/// operand of a vector shift left operation. That value must be in the range:
8940/// 0 <= Value < ElementBits for a left shift; or
8941/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008942static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008943 assert(VT.isVector() && "vector shift count is not a vector type");
8944 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8945 if (! getVShiftImm(Op, ElementBits, Cnt))
8946 return false;
8947 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8948}
8949
8950/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8951/// operand of a vector shift right operation. For a shift opcode, the value
8952/// is positive, but for an intrinsic the value count must be negative. The
8953/// absolute value must be in the range:
8954/// 1 <= |Value| <= ElementBits for a right shift; or
8955/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008956static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008957 int64_t &Cnt) {
8958 assert(VT.isVector() && "vector shift count is not a vector type");
8959 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8960 if (! getVShiftImm(Op, ElementBits, Cnt))
8961 return false;
8962 if (isIntrinsic)
8963 Cnt = -Cnt;
8964 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8965}
8966
8967/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8968static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8969 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8970 switch (IntNo) {
8971 default:
8972 // Don't do anything for most intrinsics.
8973 break;
8974
8975 // Vector shifts: check for immediate versions and lower them.
8976 // Note: This is done during DAG combining instead of DAG legalizing because
8977 // the build_vectors for 64-bit vector element shift counts are generally
8978 // not legal, and it is hard to see their values after they get legalized to
8979 // loads from a constant pool.
8980 case Intrinsic::arm_neon_vshifts:
8981 case Intrinsic::arm_neon_vshiftu:
8982 case Intrinsic::arm_neon_vshiftls:
8983 case Intrinsic::arm_neon_vshiftlu:
8984 case Intrinsic::arm_neon_vshiftn:
8985 case Intrinsic::arm_neon_vrshifts:
8986 case Intrinsic::arm_neon_vrshiftu:
8987 case Intrinsic::arm_neon_vrshiftn:
8988 case Intrinsic::arm_neon_vqshifts:
8989 case Intrinsic::arm_neon_vqshiftu:
8990 case Intrinsic::arm_neon_vqshiftsu:
8991 case Intrinsic::arm_neon_vqshiftns:
8992 case Intrinsic::arm_neon_vqshiftnu:
8993 case Intrinsic::arm_neon_vqshiftnsu:
8994 case Intrinsic::arm_neon_vqrshiftns:
8995 case Intrinsic::arm_neon_vqrshiftnu:
8996 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008997 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008998 int64_t Cnt;
8999 unsigned VShiftOpc = 0;
9000
9001 switch (IntNo) {
9002 case Intrinsic::arm_neon_vshifts:
9003 case Intrinsic::arm_neon_vshiftu:
9004 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9005 VShiftOpc = ARMISD::VSHL;
9006 break;
9007 }
9008 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9009 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9010 ARMISD::VSHRs : ARMISD::VSHRu);
9011 break;
9012 }
9013 return SDValue();
9014
9015 case Intrinsic::arm_neon_vshiftls:
9016 case Intrinsic::arm_neon_vshiftlu:
9017 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
9018 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009019 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009020
9021 case Intrinsic::arm_neon_vrshifts:
9022 case Intrinsic::arm_neon_vrshiftu:
9023 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9024 break;
9025 return SDValue();
9026
9027 case Intrinsic::arm_neon_vqshifts:
9028 case Intrinsic::arm_neon_vqshiftu:
9029 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9030 break;
9031 return SDValue();
9032
9033 case Intrinsic::arm_neon_vqshiftsu:
9034 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9035 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009036 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009037
9038 case Intrinsic::arm_neon_vshiftn:
9039 case Intrinsic::arm_neon_vrshiftn:
9040 case Intrinsic::arm_neon_vqshiftns:
9041 case Intrinsic::arm_neon_vqshiftnu:
9042 case Intrinsic::arm_neon_vqshiftnsu:
9043 case Intrinsic::arm_neon_vqrshiftns:
9044 case Intrinsic::arm_neon_vqrshiftnu:
9045 case Intrinsic::arm_neon_vqrshiftnsu:
9046 // Narrowing shifts require an immediate right shift.
9047 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9048 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00009049 llvm_unreachable("invalid shift count for narrowing vector shift "
9050 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009051
9052 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009053 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00009054 }
9055
9056 switch (IntNo) {
9057 case Intrinsic::arm_neon_vshifts:
9058 case Intrinsic::arm_neon_vshiftu:
9059 // Opcode already set above.
9060 break;
9061 case Intrinsic::arm_neon_vshiftls:
9062 case Intrinsic::arm_neon_vshiftlu:
9063 if (Cnt == VT.getVectorElementType().getSizeInBits())
9064 VShiftOpc = ARMISD::VSHLLi;
9065 else
9066 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9067 ARMISD::VSHLLs : ARMISD::VSHLLu);
9068 break;
9069 case Intrinsic::arm_neon_vshiftn:
9070 VShiftOpc = ARMISD::VSHRN; break;
9071 case Intrinsic::arm_neon_vrshifts:
9072 VShiftOpc = ARMISD::VRSHRs; break;
9073 case Intrinsic::arm_neon_vrshiftu:
9074 VShiftOpc = ARMISD::VRSHRu; break;
9075 case Intrinsic::arm_neon_vrshiftn:
9076 VShiftOpc = ARMISD::VRSHRN; break;
9077 case Intrinsic::arm_neon_vqshifts:
9078 VShiftOpc = ARMISD::VQSHLs; break;
9079 case Intrinsic::arm_neon_vqshiftu:
9080 VShiftOpc = ARMISD::VQSHLu; break;
9081 case Intrinsic::arm_neon_vqshiftsu:
9082 VShiftOpc = ARMISD::VQSHLsu; break;
9083 case Intrinsic::arm_neon_vqshiftns:
9084 VShiftOpc = ARMISD::VQSHRNs; break;
9085 case Intrinsic::arm_neon_vqshiftnu:
9086 VShiftOpc = ARMISD::VQSHRNu; break;
9087 case Intrinsic::arm_neon_vqshiftnsu:
9088 VShiftOpc = ARMISD::VQSHRNsu; break;
9089 case Intrinsic::arm_neon_vqrshiftns:
9090 VShiftOpc = ARMISD::VQRSHRNs; break;
9091 case Intrinsic::arm_neon_vqrshiftnu:
9092 VShiftOpc = ARMISD::VQRSHRNu; break;
9093 case Intrinsic::arm_neon_vqrshiftnsu:
9094 VShiftOpc = ARMISD::VQRSHRNsu; break;
9095 }
9096
9097 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009098 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009099 }
9100
9101 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00009102 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009103 int64_t Cnt;
9104 unsigned VShiftOpc = 0;
9105
9106 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9107 VShiftOpc = ARMISD::VSLI;
9108 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9109 VShiftOpc = ARMISD::VSRI;
9110 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00009111 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009112 }
9113
9114 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
9115 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009116 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009117 }
9118
9119 case Intrinsic::arm_neon_vqrshifts:
9120 case Intrinsic::arm_neon_vqrshiftu:
9121 // No immediate versions of these to check for.
9122 break;
9123 }
9124
9125 return SDValue();
9126}
9127
9128/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9129/// lowers them. As with the vector shift intrinsics, this is done during DAG
9130/// combining instead of DAG legalizing because the build_vectors for 64-bit
9131/// vector element shift counts are generally not legal, and it is hard to see
9132/// their values after they get legalized to loads from a constant pool.
9133static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9134 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00009135 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00009136 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9137 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9138 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9139 SDValue N1 = N->getOperand(1);
9140 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9141 SDValue N0 = N->getOperand(0);
9142 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9143 DAG.MaskedValueIsZero(N0.getOperand(0),
9144 APInt::getHighBitsSet(32, 16)))
9145 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
9146 }
9147 }
Bob Wilson5bafff32009-06-22 23:27:02 +00009148
9149 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00009150 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9151 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00009152 return SDValue();
9153
9154 assert(ST->hasNEON() && "unexpected vector shift");
9155 int64_t Cnt;
9156
9157 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009158 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009159
9160 case ISD::SHL:
9161 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9162 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009163 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009164 break;
9165
9166 case ISD::SRA:
9167 case ISD::SRL:
9168 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9169 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9170 ARMISD::VSHRs : ARMISD::VSHRu);
9171 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009173 }
9174 }
9175 return SDValue();
9176}
9177
9178/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9179/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9180static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9181 const ARMSubtarget *ST) {
9182 SDValue N0 = N->getOperand(0);
9183
9184 // Check for sign- and zero-extensions of vector extract operations of 8-
9185 // and 16-bit vector elements. NEON supports these directly. They are
9186 // handled during DAG combining because type legalization will promote them
9187 // to 32-bit types and it is messy to recognize the operations after that.
9188 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9189 SDValue Vec = N0.getOperand(0);
9190 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009191 EVT VT = N->getValueType(0);
9192 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009193 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9194
Owen Anderson825b72b2009-08-11 20:47:22 +00009195 if (VT == MVT::i32 &&
9196 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00009197 TLI.isTypeLegal(Vec.getValueType()) &&
9198 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009199
9200 unsigned Opc = 0;
9201 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009202 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009203 case ISD::SIGN_EXTEND:
9204 Opc = ARMISD::VGETLANEs;
9205 break;
9206 case ISD::ZERO_EXTEND:
9207 case ISD::ANY_EXTEND:
9208 Opc = ARMISD::VGETLANEu;
9209 break;
9210 }
9211 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9212 }
9213 }
9214
9215 return SDValue();
9216}
9217
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009218/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9219/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9220static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9221 const ARMSubtarget *ST) {
9222 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009223 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009224 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9225 // a NaN; only do the transformation when it matches that behavior.
9226
9227 // For now only do this when using NEON for FP operations; if using VFP, it
9228 // is not obvious that the benefit outweighs the cost of switching to the
9229 // NEON pipeline.
9230 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9231 N->getValueType(0) != MVT::f32)
9232 return SDValue();
9233
9234 SDValue CondLHS = N->getOperand(0);
9235 SDValue CondRHS = N->getOperand(1);
9236 SDValue LHS = N->getOperand(2);
9237 SDValue RHS = N->getOperand(3);
9238 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9239
9240 unsigned Opcode = 0;
9241 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009242 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009243 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009244 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009245 IsReversed = true ; // x CC y ? y : x
9246 } else {
9247 return SDValue();
9248 }
9249
Bob Wilsone742bb52010-02-24 22:15:53 +00009250 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009251 switch (CC) {
9252 default: break;
9253 case ISD::SETOLT:
9254 case ISD::SETOLE:
9255 case ISD::SETLT:
9256 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009257 case ISD::SETULT:
9258 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009259 // If LHS is NaN, an ordered comparison will be false and the result will
9260 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9261 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9262 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9263 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9264 break;
9265 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9266 // will return -0, so vmin can only be used for unsafe math or if one of
9267 // the operands is known to be nonzero.
9268 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009269 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009270 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9271 break;
9272 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009273 break;
9274
9275 case ISD::SETOGT:
9276 case ISD::SETOGE:
9277 case ISD::SETGT:
9278 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009279 case ISD::SETUGT:
9280 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009281 // If LHS is NaN, an ordered comparison will be false and the result will
9282 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9283 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9284 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9285 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9286 break;
9287 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9288 // will return +0, so vmax can only be used for unsafe math or if one of
9289 // the operands is known to be nonzero.
9290 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009291 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009292 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9293 break;
9294 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009295 break;
9296 }
9297
9298 if (!Opcode)
9299 return SDValue();
9300 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9301}
9302
Evan Chenge721f5c2011-07-13 00:42:17 +00009303/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9304SDValue
9305ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9306 SDValue Cmp = N->getOperand(4);
9307 if (Cmp.getOpcode() != ARMISD::CMPZ)
9308 // Only looking at EQ and NE cases.
9309 return SDValue();
9310
9311 EVT VT = N->getValueType(0);
9312 DebugLoc dl = N->getDebugLoc();
9313 SDValue LHS = Cmp.getOperand(0);
9314 SDValue RHS = Cmp.getOperand(1);
9315 SDValue FalseVal = N->getOperand(0);
9316 SDValue TrueVal = N->getOperand(1);
9317 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009318 ARMCC::CondCodes CC =
9319 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009320
9321 // Simplify
9322 // mov r1, r0
9323 // cmp r1, x
9324 // mov r0, y
9325 // moveq r0, x
9326 // to
9327 // cmp r0, x
9328 // movne r0, y
9329 //
9330 // mov r1, r0
9331 // cmp r1, x
9332 // mov r0, x
9333 // movne r0, y
9334 // to
9335 // cmp r0, x
9336 // movne r0, y
9337 /// FIXME: Turn this into a target neutral optimization?
9338 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009339 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009340 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9341 N->getOperand(3), Cmp);
9342 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9343 SDValue ARMcc;
9344 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9345 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9346 N->getOperand(3), NewCmp);
9347 }
9348
9349 if (Res.getNode()) {
9350 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009351 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009352 // Capture demanded bits information that would be otherwise lost.
9353 if (KnownZero == 0xfffffffe)
9354 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9355 DAG.getValueType(MVT::i1));
9356 else if (KnownZero == 0xffffff00)
9357 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9358 DAG.getValueType(MVT::i8));
9359 else if (KnownZero == 0xffff0000)
9360 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9361 DAG.getValueType(MVT::i16));
9362 }
9363
9364 return Res;
9365}
9366
Dan Gohman475871a2008-07-27 21:46:04 +00009367SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009368 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009369 switch (N->getOpcode()) {
9370 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009371 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009372 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009373 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009374 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009375 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009376 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9377 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009378 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009379 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009380 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009381 case ISD::STORE: return PerformSTORECombine(N, DCI);
9382 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9383 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009384 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009385 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009386 case ISD::FP_TO_SINT:
9387 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9388 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009389 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009390 case ISD::SHL:
9391 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009392 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009393 case ISD::SIGN_EXTEND:
9394 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009395 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9396 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009397 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009398 case ARMISD::VLD2DUP:
9399 case ARMISD::VLD3DUP:
9400 case ARMISD::VLD4DUP:
9401 return CombineBaseUpdate(N, DCI);
9402 case ISD::INTRINSIC_VOID:
9403 case ISD::INTRINSIC_W_CHAIN:
9404 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9405 case Intrinsic::arm_neon_vld1:
9406 case Intrinsic::arm_neon_vld2:
9407 case Intrinsic::arm_neon_vld3:
9408 case Intrinsic::arm_neon_vld4:
9409 case Intrinsic::arm_neon_vld2lane:
9410 case Intrinsic::arm_neon_vld3lane:
9411 case Intrinsic::arm_neon_vld4lane:
9412 case Intrinsic::arm_neon_vst1:
9413 case Intrinsic::arm_neon_vst2:
9414 case Intrinsic::arm_neon_vst3:
9415 case Intrinsic::arm_neon_vst4:
9416 case Intrinsic::arm_neon_vst2lane:
9417 case Intrinsic::arm_neon_vst3lane:
9418 case Intrinsic::arm_neon_vst4lane:
9419 return CombineBaseUpdate(N, DCI);
9420 default: break;
9421 }
9422 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009423 }
Dan Gohman475871a2008-07-27 21:46:04 +00009424 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009425}
9426
Evan Cheng31959b12011-02-02 01:06:55 +00009427bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9428 EVT VT) const {
9429 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9430}
9431
Evan Cheng376642e2012-12-10 23:21:26 +00009432bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009433 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009434 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009435
9436 switch (VT.getSimpleVT().SimpleTy) {
9437 default:
9438 return false;
9439 case MVT::i8:
9440 case MVT::i16:
Evan Cheng376642e2012-12-10 23:21:26 +00009441 case MVT::i32: {
Evan Chengd10eab02012-09-18 01:42:45 +00009442 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng376642e2012-12-10 23:21:26 +00009443 if (AllowsUnaligned) {
9444 if (Fast)
9445 *Fast = Subtarget->hasV7Ops();
9446 return true;
9447 }
9448 return false;
9449 }
Evan Chenga99c5082012-08-15 17:44:53 +00009450 case MVT::f64:
Evan Cheng376642e2012-12-10 23:21:26 +00009451 case MVT::v2f64: {
Evan Chengd10eab02012-09-18 01:42:45 +00009452 // For any little-endian targets with neon, we can support unaligned ld/st
9453 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9454 // A big-endian target may also explictly support unaligned accesses
Evan Cheng376642e2012-12-10 23:21:26 +00009455 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9456 if (Fast)
9457 *Fast = true;
9458 return true;
9459 }
9460 return false;
9461 }
Bill Wendlingaf566342009-08-15 21:21:19 +00009462 }
9463}
9464
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009465static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9466 unsigned AlignCheck) {
9467 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9468 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9469}
9470
9471EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9472 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00009473 bool IsMemset, bool ZeroMemset,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009474 bool MemcpyStrSrc,
9475 MachineFunction &MF) const {
9476 const Function *F = MF.getFunction();
9477
9478 // See if we can use NEON instructions for this...
Evan Cheng946a3a92012-12-12 02:34:41 +00009479 if ((!IsMemset || ZeroMemset) &&
Evan Cheng376642e2012-12-10 23:21:26 +00009480 Subtarget->hasNEON() &&
Bill Wendling831737d2012-12-30 10:32:01 +00009481 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9482 Attribute::NoImplicitFloat)) {
Evan Cheng376642e2012-12-10 23:21:26 +00009483 bool Fast;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009484 if (Size >= 16 &&
9485 (memOpAlign(SrcAlign, DstAlign, 16) ||
9486 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009487 return MVT::v2f64;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009488 } else if (Size >= 8 &&
9489 (memOpAlign(SrcAlign, DstAlign, 8) ||
9490 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009491 return MVT::f64;
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009492 }
9493 }
9494
Lang Hames5207bf22011-11-08 18:56:23 +00009495 // Lowering to i32/i16 if the size permits.
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009496 if (Size >= 4)
Lang Hames5207bf22011-11-08 18:56:23 +00009497 return MVT::i32;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009498 else if (Size >= 2)
Lang Hames5207bf22011-11-08 18:56:23 +00009499 return MVT::i16;
Lang Hames5207bf22011-11-08 18:56:23 +00009500
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009501 // Let the target-independent logic figure it out.
9502 return MVT::Other;
9503}
9504
Evan Cheng2766a472012-12-06 19:13:27 +00009505bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9506 if (Val.getOpcode() != ISD::LOAD)
9507 return false;
9508
9509 EVT VT1 = Val.getValueType();
9510 if (!VT1.isSimple() || !VT1.isInteger() ||
9511 !VT2.isSimple() || !VT2.isInteger())
9512 return false;
9513
9514 switch (VT1.getSimpleVT().SimpleTy) {
9515 default: break;
9516 case MVT::i1:
9517 case MVT::i8:
9518 case MVT::i16:
9519 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9520 return true;
9521 }
9522
9523 return false;
9524}
9525
Evan Chenge6c835f2009-08-14 20:09:37 +00009526static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9527 if (V < 0)
9528 return false;
9529
9530 unsigned Scale = 1;
9531 switch (VT.getSimpleVT().SimpleTy) {
9532 default: return false;
9533 case MVT::i1:
9534 case MVT::i8:
9535 // Scale == 1;
9536 break;
9537 case MVT::i16:
9538 // Scale == 2;
9539 Scale = 2;
9540 break;
9541 case MVT::i32:
9542 // Scale == 4;
9543 Scale = 4;
9544 break;
9545 }
9546
9547 if ((V & (Scale - 1)) != 0)
9548 return false;
9549 V /= Scale;
9550 return V == (V & ((1LL << 5) - 1));
9551}
9552
9553static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9554 const ARMSubtarget *Subtarget) {
9555 bool isNeg = false;
9556 if (V < 0) {
9557 isNeg = true;
9558 V = - V;
9559 }
9560
9561 switch (VT.getSimpleVT().SimpleTy) {
9562 default: return false;
9563 case MVT::i1:
9564 case MVT::i8:
9565 case MVT::i16:
9566 case MVT::i32:
9567 // + imm12 or - imm8
9568 if (isNeg)
9569 return V == (V & ((1LL << 8) - 1));
9570 return V == (V & ((1LL << 12) - 1));
9571 case MVT::f32:
9572 case MVT::f64:
9573 // Same as ARM mode. FIXME: NEON?
9574 if (!Subtarget->hasVFP2())
9575 return false;
9576 if ((V & 3) != 0)
9577 return false;
9578 V >>= 2;
9579 return V == (V & ((1LL << 8) - 1));
9580 }
9581}
9582
Evan Chengb01fad62007-03-12 23:30:29 +00009583/// isLegalAddressImmediate - Return true if the integer value can be used
9584/// as the offset of the target addressing mode for load / store of the
9585/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009586static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009587 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009588 if (V == 0)
9589 return true;
9590
Evan Cheng65011532009-03-09 19:15:00 +00009591 if (!VT.isSimple())
9592 return false;
9593
Evan Chenge6c835f2009-08-14 20:09:37 +00009594 if (Subtarget->isThumb1Only())
9595 return isLegalT1AddressImmediate(V, VT);
9596 else if (Subtarget->isThumb2())
9597 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009598
Evan Chenge6c835f2009-08-14 20:09:37 +00009599 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009600 if (V < 0)
9601 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009602 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009603 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009604 case MVT::i1:
9605 case MVT::i8:
9606 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009607 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009608 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009610 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009611 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009612 case MVT::f32:
9613 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009614 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009615 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009616 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009617 return false;
9618 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009619 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009620 }
Evan Chenga8e29892007-01-19 07:51:42 +00009621}
9622
Evan Chenge6c835f2009-08-14 20:09:37 +00009623bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9624 EVT VT) const {
9625 int Scale = AM.Scale;
9626 if (Scale < 0)
9627 return false;
9628
9629 switch (VT.getSimpleVT().SimpleTy) {
9630 default: return false;
9631 case MVT::i1:
9632 case MVT::i8:
9633 case MVT::i16:
9634 case MVT::i32:
9635 if (Scale == 1)
9636 return true;
9637 // r + r << imm
9638 Scale = Scale & ~1;
9639 return Scale == 2 || Scale == 4 || Scale == 8;
9640 case MVT::i64:
9641 // r + r
9642 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9643 return true;
9644 return false;
9645 case MVT::isVoid:
9646 // Note, we allow "void" uses (basically, uses that aren't loads or
9647 // stores), because arm allows folding a scale into many arithmetic
9648 // operations. This should be made more precise and revisited later.
9649
9650 // Allow r << imm, but the imm has to be a multiple of two.
9651 if (Scale & 1) return false;
9652 return isPowerOf2_32(Scale);
9653 }
9654}
9655
Chris Lattner37caf8c2007-04-09 23:33:39 +00009656/// isLegalAddressingMode - Return true if the addressing mode represented
9657/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009658bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009659 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009660 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009661 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009662 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009663
Chris Lattner37caf8c2007-04-09 23:33:39 +00009664 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009665 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009666 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009667
Chris Lattner37caf8c2007-04-09 23:33:39 +00009668 switch (AM.Scale) {
9669 case 0: // no scale reg, must be "r+i" or "r", or "i".
9670 break;
9671 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009672 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009673 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009674 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009675 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009676 // ARM doesn't support any R+R*scale+imm addr modes.
9677 if (AM.BaseOffs)
9678 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009679
Bob Wilson2c7dab12009-04-08 17:55:28 +00009680 if (!VT.isSimple())
9681 return false;
9682
Evan Chenge6c835f2009-08-14 20:09:37 +00009683 if (Subtarget->isThumb2())
9684 return isLegalT2ScaledAddressingMode(AM, VT);
9685
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009686 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009688 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009689 case MVT::i1:
9690 case MVT::i8:
9691 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009692 if (Scale < 0) Scale = -Scale;
9693 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009694 return true;
9695 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009696 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009697 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009698 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009699 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009700 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009701 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009702 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009703
Owen Anderson825b72b2009-08-11 20:47:22 +00009704 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009705 // Note, we allow "void" uses (basically, uses that aren't loads or
9706 // stores), because arm allows folding a scale into many arithmetic
9707 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009708
Chris Lattner37caf8c2007-04-09 23:33:39 +00009709 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009710 if (Scale & 1) return false;
9711 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009712 }
Evan Chengb01fad62007-03-12 23:30:29 +00009713 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009714 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009715}
9716
Evan Cheng77e47512009-11-11 19:05:52 +00009717/// isLegalICmpImmediate - Return true if the specified immediate is legal
9718/// icmp immediate, that is the target has icmp instructions which can compare
9719/// a register against the immediate without having to materialize the
9720/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009721bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009722 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009723 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009724 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009725 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009726 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009727 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009728 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009729}
9730
Andrew Trick8d8d9612012-07-18 18:34:27 +00009731/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9732/// *or sub* immediate, that is the target has add or sub instructions which can
9733/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009734/// immediate into a register.
9735bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009736 // Same encoding for add/sub, just flip the sign.
9737 int64_t AbsImm = llvm::abs64(Imm);
9738 if (!Subtarget->isThumb())
9739 return ARM_AM::getSOImmVal(AbsImm) != -1;
9740 if (Subtarget->isThumb2())
9741 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9742 // Thumb1 only has 8-bit unsigned immediate.
9743 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009744}
9745
Owen Andersone50ed302009-08-10 22:56:29 +00009746static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009747 bool isSEXTLoad, SDValue &Base,
9748 SDValue &Offset, bool &isInc,
9749 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009750 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9751 return false;
9752
Owen Anderson825b72b2009-08-11 20:47:22 +00009753 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009754 // AddressingMode 3
9755 Base = Ptr->getOperand(0);
9756 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009757 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009758 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009759 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009760 isInc = false;
9761 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9762 return true;
9763 }
9764 }
9765 isInc = (Ptr->getOpcode() == ISD::ADD);
9766 Offset = Ptr->getOperand(1);
9767 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009768 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009769 // AddressingMode 2
9770 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009771 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009772 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009773 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009774 isInc = false;
9775 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9776 Base = Ptr->getOperand(0);
9777 return true;
9778 }
9779 }
9780
9781 if (Ptr->getOpcode() == ISD::ADD) {
9782 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009783 ARM_AM::ShiftOpc ShOpcVal=
9784 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009785 if (ShOpcVal != ARM_AM::no_shift) {
9786 Base = Ptr->getOperand(1);
9787 Offset = Ptr->getOperand(0);
9788 } else {
9789 Base = Ptr->getOperand(0);
9790 Offset = Ptr->getOperand(1);
9791 }
9792 return true;
9793 }
9794
9795 isInc = (Ptr->getOpcode() == ISD::ADD);
9796 Base = Ptr->getOperand(0);
9797 Offset = Ptr->getOperand(1);
9798 return true;
9799 }
9800
Jim Grosbache5165492009-11-09 00:11:35 +00009801 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009802 return false;
9803}
9804
Owen Andersone50ed302009-08-10 22:56:29 +00009805static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009806 bool isSEXTLoad, SDValue &Base,
9807 SDValue &Offset, bool &isInc,
9808 SelectionDAG &DAG) {
9809 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9810 return false;
9811
9812 Base = Ptr->getOperand(0);
9813 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9814 int RHSC = (int)RHS->getZExtValue();
9815 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9816 assert(Ptr->getOpcode() == ISD::ADD);
9817 isInc = false;
9818 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9819 return true;
9820 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9821 isInc = Ptr->getOpcode() == ISD::ADD;
9822 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9823 return true;
9824 }
9825 }
9826
9827 return false;
9828}
9829
Evan Chenga8e29892007-01-19 07:51:42 +00009830/// getPreIndexedAddressParts - returns true by value, base pointer and
9831/// offset pointer and addressing mode by reference if the node's address
9832/// can be legally represented as pre-indexed load / store address.
9833bool
Dan Gohman475871a2008-07-27 21:46:04 +00009834ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9835 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009836 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009837 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009838 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009839 return false;
9840
Owen Andersone50ed302009-08-10 22:56:29 +00009841 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009842 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009843 bool isSEXTLoad = false;
9844 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9845 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009846 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009847 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9848 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9849 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009850 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009851 } else
9852 return false;
9853
9854 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009855 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009856 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009857 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9858 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009859 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009860 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009861 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009862 if (!isLegal)
9863 return false;
9864
9865 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9866 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009867}
9868
9869/// getPostIndexedAddressParts - returns true by value, base pointer and
9870/// offset pointer and addressing mode by reference if this node can be
9871/// combined with a load / store to form a post-indexed load / store.
9872bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009873 SDValue &Base,
9874 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009875 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009876 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009877 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009878 return false;
9879
Owen Andersone50ed302009-08-10 22:56:29 +00009880 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009881 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009882 bool isSEXTLoad = false;
9883 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009884 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009885 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009886 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9887 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009888 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009889 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009890 } else
9891 return false;
9892
9893 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009894 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009895 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009896 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009897 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009898 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009899 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9900 isInc, DAG);
9901 if (!isLegal)
9902 return false;
9903
Evan Cheng28dad2a2010-05-18 21:31:17 +00009904 if (Ptr != Base) {
9905 // Swap base ptr and offset to catch more post-index load / store when
9906 // it's legal. In Thumb2 mode, offset must be an immediate.
9907 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9908 !Subtarget->isThumb2())
9909 std::swap(Base, Offset);
9910
9911 // Post-indexed load / store update the base pointer.
9912 if (Ptr != Base)
9913 return false;
9914 }
9915
Evan Chenge88d5ce2009-07-02 07:28:31 +00009916 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9917 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009918}
9919
Dan Gohman475871a2008-07-27 21:46:04 +00009920void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009921 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009922 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009923 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009924 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009925 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009926 switch (Op.getOpcode()) {
9927 default: break;
9928 case ARMISD::CMOV: {
9929 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009930 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009931 if (KnownZero == 0 && KnownOne == 0) return;
9932
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009933 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009934 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009935 KnownZero &= KnownZeroRHS;
9936 KnownOne &= KnownOneRHS;
9937 return;
9938 }
9939 }
9940}
9941
9942//===----------------------------------------------------------------------===//
9943// ARM Inline Assembly Support
9944//===----------------------------------------------------------------------===//
9945
Evan Cheng55d42002011-01-08 01:24:27 +00009946bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9947 // Looking for "rev" which is V6+.
9948 if (!Subtarget->hasV6Ops())
9949 return false;
9950
9951 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9952 std::string AsmStr = IA->getAsmString();
9953 SmallVector<StringRef, 4> AsmPieces;
9954 SplitString(AsmStr, AsmPieces, ";\n");
9955
9956 switch (AsmPieces.size()) {
9957 default: return false;
9958 case 1:
9959 AsmStr = AsmPieces[0];
9960 AsmPieces.clear();
9961 SplitString(AsmStr, AsmPieces, " \t,");
9962
9963 // rev $0, $1
9964 if (AsmPieces.size() == 3 &&
9965 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9966 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009967 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009968 if (Ty && Ty->getBitWidth() == 32)
9969 return IntrinsicLowering::LowerToByteSwap(CI);
9970 }
9971 break;
9972 }
9973
9974 return false;
9975}
9976
Evan Chenga8e29892007-01-19 07:51:42 +00009977/// getConstraintType - Given a constraint letter, return the type of
9978/// constraint it is for this target.
9979ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009980ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9981 if (Constraint.size() == 1) {
9982 switch (Constraint[0]) {
9983 default: break;
9984 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009985 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009986 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009987 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009988 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009989 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009990 // An address with a single base register. Due to the way we
9991 // currently handle addresses it is the same as an 'r' memory constraint.
9992 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009993 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009994 } else if (Constraint.size() == 2) {
9995 switch (Constraint[0]) {
9996 default: break;
9997 // All 'U+' constraints are addresses.
9998 case 'U': return C_Memory;
9999 }
Evan Chenga8e29892007-01-19 07:51:42 +000010000 }
Chris Lattner4234f572007-03-25 02:14:49 +000010001 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +000010002}
10003
John Thompson44ab89e2010-10-29 17:29:13 +000010004/// Examine constraint type and operand type and determine a weight value.
10005/// This object must already have been set up with the operand type
10006/// and the current alternative constraint selected.
10007TargetLowering::ConstraintWeight
10008ARMTargetLowering::getSingleConstraintMatchWeight(
10009 AsmOperandInfo &info, const char *constraint) const {
10010 ConstraintWeight weight = CW_Invalid;
10011 Value *CallOperandVal = info.CallOperandVal;
10012 // If we don't have a value, we can't do a match,
10013 // but allow it at the lowest weight.
10014 if (CallOperandVal == NULL)
10015 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010016 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +000010017 // Look at the constraint type.
10018 switch (*constraint) {
10019 default:
10020 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10021 break;
10022 case 'l':
10023 if (type->isIntegerTy()) {
10024 if (Subtarget->isThumb())
10025 weight = CW_SpecificReg;
10026 else
10027 weight = CW_Register;
10028 }
10029 break;
10030 case 'w':
10031 if (type->isFloatingPointTy())
10032 weight = CW_Register;
10033 break;
10034 }
10035 return weight;
10036}
10037
Eric Christopher35e6d4d2011-06-30 23:50:52 +000010038typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10039RCPair
Evan Chenga8e29892007-01-19 07:51:42 +000010040ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010041 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +000010042 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010043 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +000010044 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +000010045 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010046 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010047 return RCPair(0U, &ARM::tGPRRegClass);
10048 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +000010049 case 'h': // High regs or no regs.
10050 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010051 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +000010052 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010053 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +000010054 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010055 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +000010056 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010057 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +000010058 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010059 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +000010060 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010061 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010062 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010063 case 'x':
10064 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010065 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010066 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010067 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010068 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010069 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010070 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010071 case 't':
10072 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010073 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010074 break;
Evan Chenga8e29892007-01-19 07:51:42 +000010075 }
10076 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010077 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +000010078 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010079
Evan Chenga8e29892007-01-19 07:51:42 +000010080 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10081}
10082
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010083/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10084/// vector. If it is invalid, don't add anything to Ops.
10085void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000010086 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010087 std::vector<SDValue>&Ops,
10088 SelectionDAG &DAG) const {
10089 SDValue Result(0, 0);
10090
Eric Christopher100c8332011-06-02 23:16:42 +000010091 // Currently only support length 1 constraints.
10092 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000010093
Eric Christopher100c8332011-06-02 23:16:42 +000010094 char ConstraintLetter = Constraint[0];
10095 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010096 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +000010097 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010098 case 'I': case 'J': case 'K': case 'L':
10099 case 'M': case 'N': case 'O':
10100 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10101 if (!C)
10102 return;
10103
10104 int64_t CVal64 = C->getSExtValue();
10105 int CVal = (int) CVal64;
10106 // None of these constraints allow values larger than 32 bits. Check
10107 // that the value fits in an int.
10108 if (CVal != CVal64)
10109 return;
10110
Eric Christopher100c8332011-06-02 23:16:42 +000010111 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +000010112 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +000010113 // Constant suitable for movw, must be between 0 and
10114 // 65535.
10115 if (Subtarget->hasV6T2Ops())
10116 if (CVal >= 0 && CVal <= 65535)
10117 break;
10118 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010119 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010120 if (Subtarget->isThumb1Only()) {
10121 // This must be a constant between 0 and 255, for ADD
10122 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010123 if (CVal >= 0 && CVal <= 255)
10124 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010125 } else if (Subtarget->isThumb2()) {
10126 // A constant that can be used as an immediate value in a
10127 // data-processing instruction.
10128 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10129 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010130 } else {
10131 // A constant that can be used as an immediate value in a
10132 // data-processing instruction.
10133 if (ARM_AM::getSOImmVal(CVal) != -1)
10134 break;
10135 }
10136 return;
10137
10138 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010139 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010140 // This must be a constant between -255 and -1, for negated ADD
10141 // immediates. This can be used in GCC with an "n" modifier that
10142 // prints the negated value, for use with SUB instructions. It is
10143 // not useful otherwise but is implemented for compatibility.
10144 if (CVal >= -255 && CVal <= -1)
10145 break;
10146 } else {
10147 // This must be a constant between -4095 and 4095. It is not clear
10148 // what this constraint is intended for. Implemented for
10149 // compatibility with GCC.
10150 if (CVal >= -4095 && CVal <= 4095)
10151 break;
10152 }
10153 return;
10154
10155 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010156 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010157 // A 32-bit value where only one byte has a nonzero value. Exclude
10158 // zero to match GCC. This constraint is used by GCC internally for
10159 // constants that can be loaded with a move/shift combination.
10160 // It is not useful otherwise but is implemented for compatibility.
10161 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10162 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010163 } else if (Subtarget->isThumb2()) {
10164 // A constant whose bitwise inverse can be used as an immediate
10165 // value in a data-processing instruction. This can be used in GCC
10166 // with a "B" modifier that prints the inverted value, for use with
10167 // BIC and MVN instructions. It is not useful otherwise but is
10168 // implemented for compatibility.
10169 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10170 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010171 } else {
10172 // A constant whose bitwise inverse can be used as an immediate
10173 // value in a data-processing instruction. This can be used in GCC
10174 // with a "B" modifier that prints the inverted value, for use with
10175 // BIC and MVN instructions. It is not useful otherwise but is
10176 // implemented for compatibility.
10177 if (ARM_AM::getSOImmVal(~CVal) != -1)
10178 break;
10179 }
10180 return;
10181
10182 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010183 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010184 // This must be a constant between -7 and 7,
10185 // for 3-operand ADD/SUB immediate instructions.
10186 if (CVal >= -7 && CVal < 7)
10187 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010188 } else if (Subtarget->isThumb2()) {
10189 // A constant whose negation can be used as an immediate value in a
10190 // data-processing instruction. This can be used in GCC with an "n"
10191 // modifier that prints the negated value, for use with SUB
10192 // instructions. It is not useful otherwise but is implemented for
10193 // compatibility.
10194 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10195 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010196 } else {
10197 // A constant whose negation can be used as an immediate value in a
10198 // data-processing instruction. This can be used in GCC with an "n"
10199 // modifier that prints the negated value, for use with SUB
10200 // instructions. It is not useful otherwise but is implemented for
10201 // compatibility.
10202 if (ARM_AM::getSOImmVal(-CVal) != -1)
10203 break;
10204 }
10205 return;
10206
10207 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010208 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010209 // This must be a multiple of 4 between 0 and 1020, for
10210 // ADD sp + immediate.
10211 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10212 break;
10213 } else {
10214 // A power of two or a constant between 0 and 32. This is used in
10215 // GCC for the shift amount on shifted register operands, but it is
10216 // useful in general for any shift amounts.
10217 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10218 break;
10219 }
10220 return;
10221
10222 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010223 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010224 // This must be a constant between 0 and 31, for shift amounts.
10225 if (CVal >= 0 && CVal <= 31)
10226 break;
10227 }
10228 return;
10229
10230 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010231 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010232 // This must be a multiple of 4 between -508 and 508, for
10233 // ADD/SUB sp = sp + immediate.
10234 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10235 break;
10236 }
10237 return;
10238 }
10239 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10240 break;
10241 }
10242
10243 if (Result.getNode()) {
10244 Ops.push_back(Result);
10245 return;
10246 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010247 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010248}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010249
10250bool
10251ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10252 // The ARM target isn't yet aware of offsets.
10253 return false;
10254}
Evan Cheng39382422009-10-28 01:44:26 +000010255
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010256bool ARM::isBitFieldInvertedMask(unsigned v) {
10257 if (v == 0xffffffff)
10258 return 0;
10259 // there can be 1's on either or both "outsides", all the "inside"
10260 // bits must be 0's
10261 unsigned int lsb = 0, msb = 31;
10262 while (v & (1 << msb)) --msb;
10263 while (v & (1 << lsb)) ++lsb;
10264 for (unsigned int i = lsb; i <= msb; ++i) {
10265 if (v & (1 << i))
10266 return 0;
10267 }
10268 return 1;
10269}
10270
Evan Cheng39382422009-10-28 01:44:26 +000010271/// isFPImmLegal - Returns true if the target can instruction select the
10272/// specified FP immediate natively. If false, the legalizer will
10273/// materialize the FP immediate as a load from a constant pool.
10274bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10275 if (!Subtarget->hasVFP3())
10276 return false;
10277 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010278 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010279 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010280 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010281 return false;
10282}
Bob Wilson65ffec42010-09-21 17:56:22 +000010283
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010284/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010285/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10286/// specified in the intrinsic calls.
10287bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10288 const CallInst &I,
10289 unsigned Intrinsic) const {
10290 switch (Intrinsic) {
10291 case Intrinsic::arm_neon_vld1:
10292 case Intrinsic::arm_neon_vld2:
10293 case Intrinsic::arm_neon_vld3:
10294 case Intrinsic::arm_neon_vld4:
10295 case Intrinsic::arm_neon_vld2lane:
10296 case Intrinsic::arm_neon_vld3lane:
10297 case Intrinsic::arm_neon_vld4lane: {
10298 Info.opc = ISD::INTRINSIC_W_CHAIN;
10299 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010300 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010301 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10302 Info.ptrVal = I.getArgOperand(0);
10303 Info.offset = 0;
10304 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10305 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10306 Info.vol = false; // volatile loads with NEON intrinsics not supported
10307 Info.readMem = true;
10308 Info.writeMem = false;
10309 return true;
10310 }
10311 case Intrinsic::arm_neon_vst1:
10312 case Intrinsic::arm_neon_vst2:
10313 case Intrinsic::arm_neon_vst3:
10314 case Intrinsic::arm_neon_vst4:
10315 case Intrinsic::arm_neon_vst2lane:
10316 case Intrinsic::arm_neon_vst3lane:
10317 case Intrinsic::arm_neon_vst4lane: {
10318 Info.opc = ISD::INTRINSIC_VOID;
10319 // Conservatively set memVT to the entire set of vectors stored.
10320 unsigned NumElts = 0;
10321 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010322 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010323 if (!ArgTy->isVectorTy())
10324 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010325 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010326 }
10327 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10328 Info.ptrVal = I.getArgOperand(0);
10329 Info.offset = 0;
10330 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10331 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10332 Info.vol = false; // volatile stores with NEON intrinsics not supported
10333 Info.readMem = false;
10334 Info.writeMem = true;
10335 return true;
10336 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010337 case Intrinsic::arm_strexd: {
10338 Info.opc = ISD::INTRINSIC_W_CHAIN;
10339 Info.memVT = MVT::i64;
10340 Info.ptrVal = I.getArgOperand(2);
10341 Info.offset = 0;
10342 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010343 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010344 Info.readMem = false;
10345 Info.writeMem = true;
10346 return true;
10347 }
10348 case Intrinsic::arm_ldrexd: {
10349 Info.opc = ISD::INTRINSIC_W_CHAIN;
10350 Info.memVT = MVT::i64;
10351 Info.ptrVal = I.getArgOperand(0);
10352 Info.offset = 0;
10353 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010354 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010355 Info.readMem = true;
10356 Info.writeMem = false;
10357 return true;
10358 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010359 default:
10360 break;
10361 }
10362
10363 return false;
10364}