blob: 3d283a5ac9504b03d37b78afb3f6c78127c68f3f [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
Bob Wilson1c3ef902011-02-07 17:43:21 +0000557 setTargetDAGCombine(ISD::INTRINSIC_VOID);
558 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000559 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
560 setTargetDAGCombine(ISD::SHL);
561 setTargetDAGCombine(ISD::SRL);
562 setTargetDAGCombine(ISD::SRA);
563 setTargetDAGCombine(ISD::SIGN_EXTEND);
564 setTargetDAGCombine(ISD::ZERO_EXTEND);
565 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000566 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000567 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000568 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000569 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
570 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000571 setTargetDAGCombine(ISD::FP_TO_SINT);
572 setTargetDAGCombine(ISD::FP_TO_UINT);
573 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000574
James Molloy873fd5f2012-02-20 09:24:05 +0000575 // It is legal to extload from v4i8 to v4i16 or v4i32.
576 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
577 MVT::v4i16, MVT::v2i16,
578 MVT::v2i32};
579 for (unsigned i = 0; i < 6; ++i) {
580 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
581 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
582 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
583 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000584 }
585
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000586 // ARM and Thumb2 support UMLAL/SMLAL.
587 if (!Subtarget->isThumb1Only())
588 setTargetDAGCombine(ISD::ADDC);
589
590
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000591 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000592
593 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000595
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000596 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000598
Evan Chenga8e29892007-01-19 07:51:42 +0000599 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000600 if (!Subtarget->isThumb1Only()) {
601 for (unsigned im = (unsigned)ISD::PRE_INC;
602 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 setIndexedLoadAction(im, MVT::i1, Legal);
604 setIndexedLoadAction(im, MVT::i8, Legal);
605 setIndexedLoadAction(im, MVT::i16, Legal);
606 setIndexedLoadAction(im, MVT::i32, Legal);
607 setIndexedStoreAction(im, MVT::i1, Legal);
608 setIndexedStoreAction(im, MVT::i8, Legal);
609 setIndexedStoreAction(im, MVT::i16, Legal);
610 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000611 }
Evan Chenga8e29892007-01-19 07:51:42 +0000612 }
613
614 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000615 setOperationAction(ISD::MUL, MVT::i64, Expand);
616 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000617 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
619 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000620 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000621 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
622 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000623 setOperationAction(ISD::MULHS, MVT::i32, Expand);
624
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000625 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000626 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000627 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000628 setOperationAction(ISD::SRL, MVT::i64, Custom);
629 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000630
Evan Cheng342e3162011-08-30 01:34:54 +0000631 if (!Subtarget->isThumb1Only()) {
632 // FIXME: We should do this for Thumb1 as well.
633 setOperationAction(ISD::ADDC, MVT::i32, Custom);
634 setOperationAction(ISD::ADDE, MVT::i32, Custom);
635 setOperationAction(ISD::SUBC, MVT::i32, Custom);
636 setOperationAction(ISD::SUBE, MVT::i32, Custom);
637 }
638
Evan Chenga8e29892007-01-19 07:51:42 +0000639 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000641 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000643 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000645
Chandler Carruth63974b22011-12-13 01:56:10 +0000646 // These just redirect to CTTZ and CTLZ on ARM.
647 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
648 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
649
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000650 // Only ARMv6 has BSWAP.
651 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000653
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000654 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
655 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
656 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000657 setOperationAction(ISD::SDIV, MVT::i32, Expand);
658 setOperationAction(ISD::UDIV, MVT::i32, Expand);
659 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000660 setOperationAction(ISD::SREM, MVT::i32, Expand);
661 setOperationAction(ISD::UREM, MVT::i32, Expand);
662 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
663 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
666 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
667 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
668 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000669 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000670
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000671 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000672
Evan Chenga8e29892007-01-19 07:51:42 +0000673 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::VASTART, MVT::Other, Custom);
675 setOperationAction(ISD::VAARG, MVT::Other, Expand);
676 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
677 setOperationAction(ISD::VAEND, MVT::Other, Expand);
678 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
679 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000680
681 if (!Subtarget->isTargetDarwin()) {
682 // Non-Darwin platforms may return values in these registers via the
683 // personality function.
684 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
685 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
686 setExceptionPointerRegister(ARM::R0);
687 setExceptionSelectorRegister(ARM::R1);
688 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000689
Evan Cheng3a1588a2010-04-15 22:20:34 +0000690 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000691 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
692 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000693 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000694 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000695 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000696 // membarrier needs custom lowering; the rest are legal and handled
697 // normally.
698 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000699 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000700 // Custom lowering for 64-bit ops
701 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
702 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
703 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
704 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
705 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
Silviu Baranga35b3df62012-11-29 14:41:25 +0000706 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
707 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
708 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
709 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
710 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000711 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000712 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
713 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000714 } else {
715 // Set them all for expansion, which will force libcalls.
716 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000717 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000718 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000719 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000720 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000721 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000722 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000723 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000724 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000725 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000726 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000727 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000728 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000729 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000730 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
731 // Unordered/Monotonic case.
732 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
733 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000734 // Since the libcalls include locking, fold in the fences
735 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000736 }
Evan Chenga8e29892007-01-19 07:51:42 +0000737
Evan Cheng416941d2010-11-04 05:19:35 +0000738 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000739
Eli Friedmana2c6f452010-06-26 04:36:50 +0000740 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
741 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
743 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000744 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000746
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000747 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
748 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000749 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000750 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000751 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000752 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
753 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000754
755 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000757 if (Subtarget->isTargetDarwin()) {
758 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
759 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000760 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000761 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000762
Owen Anderson825b72b2009-08-11 20:47:22 +0000763 setOperationAction(ISD::SETCC, MVT::i32, Expand);
764 setOperationAction(ISD::SETCC, MVT::f32, Expand);
765 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000766 setOperationAction(ISD::SELECT, MVT::i32, Custom);
767 setOperationAction(ISD::SELECT, MVT::f32, Custom);
768 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
770 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
771 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000772
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
774 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
775 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
776 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
777 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000778
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000779 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 setOperationAction(ISD::FSIN, MVT::f64, Expand);
781 setOperationAction(ISD::FSIN, MVT::f32, Expand);
782 setOperationAction(ISD::FCOS, MVT::f32, Expand);
783 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000784 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
785 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::FREM, MVT::f64, Expand);
787 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000788 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
789 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
791 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000792 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 setOperationAction(ISD::FPOW, MVT::f64, Expand);
794 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000795
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000796 if (!Subtarget->hasVFP4()) {
797 setOperationAction(ISD::FMA, MVT::f64, Expand);
798 setOperationAction(ISD::FMA, MVT::f32, Expand);
799 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000800
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000801 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000802 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000803 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
804 if (Subtarget->hasVFP2()) {
805 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
806 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
807 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
808 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
809 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000810 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000811 if (!Subtarget->hasFP16()) {
812 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
813 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000814 }
Evan Cheng110cf482008-04-01 01:50:16 +0000815 }
Evan Chenga8e29892007-01-19 07:51:42 +0000816
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000817 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000818 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000819 setTargetDAGCombine(ISD::ADD);
820 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000821 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000822 setTargetDAGCombine(ISD::AND);
823 setTargetDAGCombine(ISD::OR);
824 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000825
Evan Cheng5fb468a2012-02-23 02:58:19 +0000826 if (Subtarget->hasV6Ops())
827 setTargetDAGCombine(ISD::SRL);
828
Evan Chenga8e29892007-01-19 07:51:42 +0000829 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000830
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000831 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
832 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000833 setSchedulingPreference(Sched::RegPressure);
834 else
835 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000836
Evan Cheng05219282011-01-06 06:52:41 +0000837 //// temporary - rewrite interface to use type
Evan Cheng376642e2012-12-10 23:21:26 +0000838 maxStoresPerMemset = 8;
Lang Hames75757f92011-10-26 20:56:52 +0000839 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Cheng376642e2012-12-10 23:21:26 +0000840 maxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
841 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
842 maxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
843 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengf6799392010-06-26 01:52:05 +0000844
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000845 // On ARM arguments smaller than 4 bytes are extended, so all arguments
846 // are at least 4 bytes aligned.
847 setMinStackArgumentAlignment(4);
848
Evan Chengfff606d2010-09-24 19:07:23 +0000849 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000850
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000851 // Prefer likely predicted branches to selects on out-of-order cores.
Silviu Baranga616471d2012-09-13 15:05:10 +0000852 predictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000853
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000854 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000855}
856
Andrew Trick32cec0a2011-01-19 02:35:27 +0000857// FIXME: It might make sense to define the representative register class as the
858// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
859// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
860// SPR's representative would be DPR_VFP2. This should work well if register
861// pressure tracking were modified such that a register use would increment the
862// pressure of the register class's representative and all of it's super
863// classes' representatives transitively. We have not implemented this because
864// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000865// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000866// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000867std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +0000868ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Cheng4f6b4672010-07-21 06:09:07 +0000869 const TargetRegisterClass *RRC = 0;
870 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +0000871 switch (VT.SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000872 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000874 // Use DPR as representative register class for all floating point
875 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
876 // the cost is 1 for both f32 and f64.
877 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000879 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000880 // When NEON is used for SP, only half of the register file is available
881 // because operations that define both SP and DP results will be constrained
882 // to the VFP2 class (D0-D15). We currently model this constraint prior to
883 // coalescing by double-counting the SP regs. See the FIXME above.
884 if (Subtarget->useNEONForSinglePrecisionFP())
885 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000886 break;
887 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
888 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000889 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000890 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000891 break;
892 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000893 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000894 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000895 break;
896 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000897 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000898 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000899 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000900 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000901 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000902}
903
Evan Chenga8e29892007-01-19 07:51:42 +0000904const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
905 switch (Opcode) {
906 default: return 0;
907 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000908 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000909 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000910 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
911 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000912 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000913 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
914 case ARMISD::tCALL: return "ARMISD::tCALL";
915 case ARMISD::BRCOND: return "ARMISD::BRCOND";
916 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000917 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000918 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
919 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
920 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000921 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000922 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000923 case ARMISD::CMPFP: return "ARMISD::CMPFP";
924 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000925 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000926 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000927
Evan Chenga8e29892007-01-19 07:51:42 +0000928 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000929
Jim Grosbach3482c802010-01-18 19:58:49 +0000930 case ARMISD::RBIT: return "ARMISD::RBIT";
931
Bob Wilson76a312b2010-03-19 22:51:32 +0000932 case ARMISD::FTOSI: return "ARMISD::FTOSI";
933 case ARMISD::FTOUI: return "ARMISD::FTOUI";
934 case ARMISD::SITOF: return "ARMISD::SITOF";
935 case ARMISD::UITOF: return "ARMISD::UITOF";
936
Evan Chenga8e29892007-01-19 07:51:42 +0000937 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
938 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
939 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000940
Evan Cheng342e3162011-08-30 01:34:54 +0000941 case ARMISD::ADDC: return "ARMISD::ADDC";
942 case ARMISD::ADDE: return "ARMISD::ADDE";
943 case ARMISD::SUBC: return "ARMISD::SUBC";
944 case ARMISD::SUBE: return "ARMISD::SUBE";
945
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000946 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
947 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000948
Evan Chengc5942082009-10-28 06:55:03 +0000949 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
950 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
951
Dale Johannesen51e28e62010-06-03 21:09:53 +0000952 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000953
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000954 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000955
Evan Cheng86198642009-08-07 00:34:42 +0000956 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
957
Jim Grosbach3728e962009-12-10 00:11:09 +0000958 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000959 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000960
Evan Chengdfed19f2010-11-03 06:34:55 +0000961 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
962
Bob Wilson5bafff32009-06-22 23:27:02 +0000963 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000964 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000965 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000966 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
967 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000968 case ARMISD::VCGEU: return "ARMISD::VCGEU";
969 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000970 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
971 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000972 case ARMISD::VCGTU: return "ARMISD::VCGTU";
973 case ARMISD::VTST: return "ARMISD::VTST";
974
975 case ARMISD::VSHL: return "ARMISD::VSHL";
976 case ARMISD::VSHRs: return "ARMISD::VSHRs";
977 case ARMISD::VSHRu: return "ARMISD::VSHRu";
978 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
979 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
980 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
981 case ARMISD::VSHRN: return "ARMISD::VSHRN";
982 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
983 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
984 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
985 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
986 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
987 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
988 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
989 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
990 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
991 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
992 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
993 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
994 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
995 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000996 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000997 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000998 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000999 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +00001000 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +00001001 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +00001002 case ARMISD::VREV64: return "ARMISD::VREV64";
1003 case ARMISD::VREV32: return "ARMISD::VREV32";
1004 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001005 case ARMISD::VZIP: return "ARMISD::VZIP";
1006 case ARMISD::VUZP: return "ARMISD::VUZP";
1007 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +00001008 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1009 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +00001010 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1011 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00001012 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1013 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001014 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001015 case ARMISD::FMAX: return "ARMISD::FMAX";
1016 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001017 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001018 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1019 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001020 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001021 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1022 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1023 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001024 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1025 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1026 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1027 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1028 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1029 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1030 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1031 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1032 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1033 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1034 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1035 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1036 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1037 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1038 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1039 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1040 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001041 }
1042}
1043
Duncan Sands28b77e92011-09-06 19:07:46 +00001044EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1045 if (!VT.isVector()) return getPointerTy();
1046 return VT.changeVectorElementTypeToInteger();
1047}
1048
Evan Cheng06b666c2010-05-15 02:18:07 +00001049/// getRegClassFor - Return the register class that should be used for the
1050/// specified value type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001051const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001052 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1053 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1054 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001055 if (Subtarget->hasNEON()) {
1056 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001057 return &ARM::QQPRRegClass;
1058 if (VT == MVT::v8i64)
1059 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001060 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001061 return TargetLowering::getRegClassFor(VT);
1062}
1063
Eric Christopherab695882010-07-21 22:26:11 +00001064// Create a fast isel object.
1065FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001066ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1067 const TargetLibraryInfo *libInfo) const {
1068 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001069}
1070
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001071/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1072/// be used for loads / stores from the global.
1073unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1074 return (Subtarget->isThumb1Only() ? 127 : 4095);
1075}
1076
Evan Cheng1cc39842010-05-20 23:26:43 +00001077Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001078 unsigned NumVals = N->getNumValues();
1079 if (!NumVals)
1080 return Sched::RegPressure;
1081
1082 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001083 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001084 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001085 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001086 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001087 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001088 }
Evan Chengc10f5432010-05-28 23:25:23 +00001089
1090 if (!N->isMachineOpcode())
1091 return Sched::RegPressure;
1092
1093 // Load are scheduled for latency even if there instruction itinerary
1094 // is not available.
1095 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001096 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001097
Evan Chenge837dea2011-06-28 19:10:37 +00001098 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001099 return Sched::RegPressure;
1100 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001101 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001102 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001103
Evan Cheng1cc39842010-05-20 23:26:43 +00001104 return Sched::RegPressure;
1105}
1106
Evan Chenga8e29892007-01-19 07:51:42 +00001107//===----------------------------------------------------------------------===//
1108// Lowering Code
1109//===----------------------------------------------------------------------===//
1110
Evan Chenga8e29892007-01-19 07:51:42 +00001111/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1112static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1113 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001114 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001115 case ISD::SETNE: return ARMCC::NE;
1116 case ISD::SETEQ: return ARMCC::EQ;
1117 case ISD::SETGT: return ARMCC::GT;
1118 case ISD::SETGE: return ARMCC::GE;
1119 case ISD::SETLT: return ARMCC::LT;
1120 case ISD::SETLE: return ARMCC::LE;
1121 case ISD::SETUGT: return ARMCC::HI;
1122 case ISD::SETUGE: return ARMCC::HS;
1123 case ISD::SETULT: return ARMCC::LO;
1124 case ISD::SETULE: return ARMCC::LS;
1125 }
1126}
1127
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001128/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1129static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001130 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001131 CondCode2 = ARMCC::AL;
1132 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001133 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001134 case ISD::SETEQ:
1135 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1136 case ISD::SETGT:
1137 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1138 case ISD::SETGE:
1139 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1140 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001141 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001142 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1143 case ISD::SETO: CondCode = ARMCC::VC; break;
1144 case ISD::SETUO: CondCode = ARMCC::VS; break;
1145 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1146 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1147 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1148 case ISD::SETLT:
1149 case ISD::SETULT: CondCode = ARMCC::LT; break;
1150 case ISD::SETLE:
1151 case ISD::SETULE: CondCode = ARMCC::LE; break;
1152 case ISD::SETNE:
1153 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1154 }
Evan Chenga8e29892007-01-19 07:51:42 +00001155}
1156
Bob Wilson1f595bb2009-04-17 19:07:39 +00001157//===----------------------------------------------------------------------===//
1158// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001159//===----------------------------------------------------------------------===//
1160
1161#include "ARMGenCallingConv.inc"
1162
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001163/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1164/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001165CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001166 bool Return,
1167 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001168 switch (CC) {
1169 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001170 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001171 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001172 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001173 if (!Subtarget->isAAPCS_ABI())
1174 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1175 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1176 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1177 }
1178 // Fallthrough
1179 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001180 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001181 if (!Subtarget->isAAPCS_ABI())
1182 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1183 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001184 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1185 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001186 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1187 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1188 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001189 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001190 if (!isVarArg)
1191 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1192 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001193 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001194 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001195 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001196 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001197 case CallingConv::GHC:
1198 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001199 }
1200}
1201
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202/// LowerCallResult - Lower the result values of a call into the
1203/// appropriate copies out of appropriate physical registers.
1204SDValue
1205ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001206 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207 const SmallVectorImpl<ISD::InputArg> &Ins,
1208 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001209 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210
Bob Wilson1f595bb2009-04-17 19:07:39 +00001211 // Assign locations to each value returned by this call.
1212 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001213 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1214 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001215 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001216 CCAssignFnForNode(CallConv, /* Return*/ true,
1217 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001218
1219 // Copy all of the result registers out of their specified physreg.
1220 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1221 CCValAssign VA = RVLocs[i];
1222
Bob Wilson80915242009-04-25 00:33:20 +00001223 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001224 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001225 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001228 Chain = Lo.getValue(1);
1229 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001230 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001231 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001232 InFlag);
1233 Chain = Hi.getValue(1);
1234 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001235 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001236
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (VA.getLocVT() == MVT::v2f64) {
1238 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1239 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1240 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001241
1242 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001244 Chain = Lo.getValue(1);
1245 InFlag = Lo.getValue(2);
1246 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001248 Chain = Hi.getValue(1);
1249 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001250 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1252 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001253 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001254 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001255 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1256 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001257 Chain = Val.getValue(1);
1258 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259 }
Bob Wilson80915242009-04-25 00:33:20 +00001260
1261 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001262 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001263 case CCValAssign::Full: break;
1264 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001265 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001266 break;
1267 }
1268
Dan Gohman98ca4f22009-08-05 01:29:28 +00001269 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001270 }
1271
Dan Gohman98ca4f22009-08-05 01:29:28 +00001272 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001273}
1274
Bob Wilsondee46d72009-04-17 20:35:10 +00001275/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001276SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1278 SDValue StackPtr, SDValue Arg,
1279 DebugLoc dl, SelectionDAG &DAG,
1280 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001281 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001282 unsigned LocMemOffset = VA.getLocMemOffset();
1283 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1284 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001285 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001286 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001287 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001288}
1289
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001291 SDValue Chain, SDValue &Arg,
1292 RegsToPassVector &RegsToPass,
1293 CCValAssign &VA, CCValAssign &NextVA,
1294 SDValue &StackPtr,
1295 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001296 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001297
Jim Grosbache5165492009-11-09 00:11:35 +00001298 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001300 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1301
1302 if (NextVA.isRegLoc())
1303 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1304 else {
1305 assert(NextVA.isMemLoc());
1306 if (StackPtr.getNode() == 0)
1307 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1308
Dan Gohman98ca4f22009-08-05 01:29:28 +00001309 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1310 dl, DAG, NextVA,
1311 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001312 }
1313}
1314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001316/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1317/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001319ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001320 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001321 SelectionDAG &DAG = CLI.DAG;
1322 DebugLoc &dl = CLI.DL;
1323 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1324 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1325 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1326 SDValue Chain = CLI.Chain;
1327 SDValue Callee = CLI.Callee;
1328 bool &isTailCall = CLI.IsTailCall;
1329 CallingConv::ID CallConv = CLI.CallConv;
1330 bool doesNotRet = CLI.DoesNotReturn;
1331 bool isVarArg = CLI.IsVarArg;
1332
Dale Johannesen51e28e62010-06-03 21:09:53 +00001333 MachineFunction &MF = DAG.getMachineFunction();
1334 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1335 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001336 // Disable tail calls if they're not supported.
1337 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001338 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001339 if (isTailCall) {
1340 // Check if it's really possible to do a tail call.
1341 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1342 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001343 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001344 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1345 // detected sibcalls.
1346 if (isTailCall) {
1347 ++NumTailCalls;
1348 IsSibCall = true;
1349 }
1350 }
Evan Chenga8e29892007-01-19 07:51:42 +00001351
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352 // Analyze operands of the call, assigning locations to each operand.
1353 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001354 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1355 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001357 CCAssignFnForNode(CallConv, /* Return*/ false,
1358 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001359
Bob Wilson1f595bb2009-04-17 19:07:39 +00001360 // Get a count of how many bytes are to be pushed on the stack.
1361 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001362
Dale Johannesen51e28e62010-06-03 21:09:53 +00001363 // For tail calls, memory operands are available in our caller's stack.
1364 if (IsSibCall)
1365 NumBytes = 0;
1366
Evan Chenga8e29892007-01-19 07:51:42 +00001367 // Adjust the stack pointer for the new arguments...
1368 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001369 if (!IsSibCall)
1370 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001371
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001372 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001373
Bob Wilson5bafff32009-06-22 23:27:02 +00001374 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001376
Bob Wilson1f595bb2009-04-17 19:07:39 +00001377 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001378 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001379 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1380 i != e;
1381 ++i, ++realArgIdx) {
1382 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001383 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001384 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001385 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001386
Bob Wilson1f595bb2009-04-17 19:07:39 +00001387 // Promote the value if needed.
1388 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001389 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001390 case CCValAssign::Full: break;
1391 case CCValAssign::SExt:
1392 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1393 break;
1394 case CCValAssign::ZExt:
1395 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1396 break;
1397 case CCValAssign::AExt:
1398 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1399 break;
1400 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001401 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001402 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001403 }
1404
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001405 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001406 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001407 if (VA.getLocVT() == MVT::v2f64) {
1408 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1409 DAG.getConstant(0, MVT::i32));
1410 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1411 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001412
Dan Gohman98ca4f22009-08-05 01:29:28 +00001413 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001414 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1415
1416 VA = ArgLocs[++i]; // skip ahead to next loc
1417 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001418 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001419 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1420 } else {
1421 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001422
Dan Gohman98ca4f22009-08-05 01:29:28 +00001423 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1424 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001425 }
1426 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001427 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001428 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001429 }
1430 } else if (VA.isRegLoc()) {
1431 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001432 } else if (isByVal) {
1433 assert(VA.isMemLoc());
1434 unsigned offset = 0;
1435
1436 // True if this byval aggregate will be split between registers
1437 // and memory.
1438 if (CCInfo.isFirstByValRegValid()) {
1439 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1440 unsigned int i, j;
1441 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1442 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1443 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1444 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1445 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001446 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001447 MemOpChains.push_back(Load.getValue(1));
1448 RegsToPass.push_back(std::make_pair(j, Load));
1449 }
1450 offset = ARM::R4 - CCInfo.getFirstByValReg();
1451 CCInfo.clearFirstByValReg();
1452 }
1453
Manman Ren763a75d2012-06-01 02:44:42 +00001454 if (Flags.getByValSize() - 4*offset > 0) {
1455 unsigned LocMemOffset = VA.getLocMemOffset();
1456 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1457 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1458 StkPtrOff);
1459 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1460 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1461 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1462 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001463 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001464
Manman Ren763a75d2012-06-01 02:44:42 +00001465 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001466 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001467 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1468 Ops, array_lengthof(Ops)));
1469 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001470 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001471 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001472
Dan Gohman98ca4f22009-08-05 01:29:28 +00001473 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1474 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001475 }
Evan Chenga8e29892007-01-19 07:51:42 +00001476 }
1477
1478 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001479 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001480 &MemOpChains[0], MemOpChains.size());
1481
1482 // Build a sequence of copy-to-reg nodes chained together with token chain
1483 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001485 // Tail call byval lowering might overwrite argument registers so in case of
1486 // tail call optimization the copies to registers are lowered later.
1487 if (!isTailCall)
1488 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1489 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1490 RegsToPass[i].second, InFlag);
1491 InFlag = Chain.getValue(1);
1492 }
Evan Chenga8e29892007-01-19 07:51:42 +00001493
Dale Johannesen51e28e62010-06-03 21:09:53 +00001494 // For tail calls lower the arguments to the 'real' stack slot.
1495 if (isTailCall) {
1496 // Force all the incoming stack arguments to be loaded from the stack
1497 // before any new outgoing arguments are stored to the stack, because the
1498 // outgoing stack slots may alias the incoming argument stack slots, and
1499 // the alias isn't otherwise explicit. This is slightly more conservative
1500 // than necessary, because it means that each store effectively depends
1501 // on every argument instead of just those arguments it would clobber.
1502
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001503 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001504 InFlag = SDValue();
1505 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1506 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1507 RegsToPass[i].second, InFlag);
1508 InFlag = Chain.getValue(1);
1509 }
1510 InFlag =SDValue();
1511 }
1512
Bill Wendling056292f2008-09-16 21:48:12 +00001513 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1514 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1515 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001516 bool isDirect = false;
1517 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001518 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001519 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001520
1521 if (EnableARMLongCalls) {
1522 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1523 && "long-calls with non-static relocation model!");
1524 // Handle a global address or an external symbol. If it's not one of
1525 // those, the target's already in a register, so we don't need to do
1526 // anything extra.
1527 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001528 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001529 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001530 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001531 ARMConstantPoolValue *CPV =
1532 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1533
Jim Grosbache7b52522010-04-14 22:28:31 +00001534 // Get the address of the callee into a register
1535 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1536 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1537 Callee = DAG.getLoad(getPointerTy(), dl,
1538 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001539 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001540 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001541 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1542 const char *Sym = S->getSymbol();
1543
1544 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001545 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001546 ARMConstantPoolValue *CPV =
1547 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1548 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001549 // Get the address of the callee into a register
1550 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1551 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1552 Callee = DAG.getLoad(getPointerTy(), dl,
1553 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001554 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001555 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001556 }
1557 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001558 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001559 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001560 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001561 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001562 getTargetMachine().getRelocationModel() != Reloc::Static;
1563 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001564 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001565 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001566 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001567 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001568 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001569 ARMConstantPoolValue *CPV =
1570 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001571 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001572 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001573 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001574 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001575 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001576 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001577 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001578 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001579 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001580 } else {
1581 // On ELF targets for PIC code, direct calls should go through the PLT
1582 unsigned OpFlags = 0;
1583 if (Subtarget->isTargetELF() &&
1584 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1585 OpFlags = ARMII::MO_PLT;
1586 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1587 }
Bill Wendling056292f2008-09-16 21:48:12 +00001588 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001589 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001590 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001591 getTargetMachine().getRelocationModel() != Reloc::Static;
1592 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001593 // tBX takes a register source operand.
1594 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001595 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001596 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001597 ARMConstantPoolValue *CPV =
1598 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1599 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001600 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001602 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001603 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001604 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001605 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001606 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001607 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001608 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001609 } else {
1610 unsigned OpFlags = 0;
1611 // On ELF targets for PIC code, direct calls should go through the PLT
1612 if (Subtarget->isTargetELF() &&
1613 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1614 OpFlags = ARMII::MO_PLT;
1615 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1616 }
Evan Chenga8e29892007-01-19 07:51:42 +00001617 }
1618
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001619 // FIXME: handle tail calls differently.
1620 unsigned CallOpc;
Bill Wendling831737d2012-12-30 10:32:01 +00001621 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1622 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001623 if (Subtarget->isThumb()) {
1624 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001625 CallOpc = ARMISD::CALL_NOLINK;
1626 else
1627 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1628 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001629 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001630 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001631 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001632 // Emit regular call when code size is the priority
1633 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001634 // "mov lr, pc; b _foo" to avoid confusing the RSP
1635 CallOpc = ARMISD::CALL_NOLINK;
1636 else
1637 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001638 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001639
Dan Gohman475871a2008-07-27 21:46:04 +00001640 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001641 Ops.push_back(Chain);
1642 Ops.push_back(Callee);
1643
1644 // Add argument registers to the end of the list so that they are known live
1645 // into the call.
1646 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1647 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1648 RegsToPass[i].second.getValueType()));
1649
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001650 // Add a register mask operand representing the call-preserved registers.
1651 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1652 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1653 assert(Mask && "Missing call preserved mask for calling convention");
1654 Ops.push_back(DAG.getRegisterMask(Mask));
1655
Gabor Greifba36cb52008-08-28 21:40:38 +00001656 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001657 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001658
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001659 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001660 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001661 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001662
Duncan Sands4bdcb612008-07-02 17:40:58 +00001663 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001664 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001665 InFlag = Chain.getValue(1);
1666
Chris Lattnere563bbc2008-10-11 22:08:30 +00001667 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1668 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001670 InFlag = Chain.getValue(1);
1671
Bob Wilson1f595bb2009-04-17 19:07:39 +00001672 // Handle result values, copying them out of physregs into vregs that we
1673 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001674 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1675 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001676}
1677
Stuart Hastingsf222e592011-02-28 17:17:53 +00001678/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001679/// on the stack. Remember the next parameter register to allocate,
1680/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001681/// this.
1682void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001683ARMTargetLowering::HandleByVal(
1684 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001685 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1686 assert((State->getCallOrPrologue() == Prologue ||
1687 State->getCallOrPrologue() == Call) &&
1688 "unhandled ParmContext");
1689 if ((!State->isFirstByValRegValid()) &&
1690 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001691 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1692 unsigned AlignInRegs = Align / 4;
1693 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1694 for (unsigned i = 0; i < Waste; ++i)
1695 reg = State->AllocateReg(GPRArgRegs, 4);
1696 }
1697 if (reg != 0) {
1698 State->setFirstByValReg(reg);
1699 // At a call site, a byval parameter that is split between
1700 // registers and memory needs its size truncated here. In a
1701 // function prologue, such byval parameters are reassembled in
1702 // memory, and are not truncated.
1703 if (State->getCallOrPrologue() == Call) {
1704 unsigned excess = 4 * (ARM::R4 - reg);
1705 assert(size >= excess && "expected larger existing stack allocation");
1706 size -= excess;
1707 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001708 }
1709 }
1710 // Confiscate any remaining parameter registers to preclude their
1711 // assignment to subsequent parameters.
1712 while (State->AllocateReg(GPRArgRegs, 4))
1713 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001714}
1715
Dale Johannesen51e28e62010-06-03 21:09:53 +00001716/// MatchingStackOffset - Return true if the given stack call argument is
1717/// already available in the same position (relatively) of the caller's
1718/// incoming argument stack.
1719static
1720bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1721 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001722 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001723 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1724 int FI = INT_MAX;
1725 if (Arg.getOpcode() == ISD::CopyFromReg) {
1726 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001727 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001728 return false;
1729 MachineInstr *Def = MRI->getVRegDef(VR);
1730 if (!Def)
1731 return false;
1732 if (!Flags.isByVal()) {
1733 if (!TII->isLoadFromStackSlot(Def, FI))
1734 return false;
1735 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001736 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001737 }
1738 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1739 if (Flags.isByVal())
1740 // ByVal argument is passed in as a pointer but it's now being
1741 // dereferenced. e.g.
1742 // define @foo(%struct.X* %A) {
1743 // tail call @bar(%struct.X* byval %A)
1744 // }
1745 return false;
1746 SDValue Ptr = Ld->getBasePtr();
1747 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1748 if (!FINode)
1749 return false;
1750 FI = FINode->getIndex();
1751 } else
1752 return false;
1753
1754 assert(FI != INT_MAX);
1755 if (!MFI->isFixedObjectIndex(FI))
1756 return false;
1757 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1758}
1759
1760/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1761/// for tail call optimization. Targets which want to do tail call
1762/// optimization should implement this function.
1763bool
1764ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1765 CallingConv::ID CalleeCC,
1766 bool isVarArg,
1767 bool isCalleeStructRet,
1768 bool isCallerStructRet,
1769 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001770 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001771 const SmallVectorImpl<ISD::InputArg> &Ins,
1772 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001773 const Function *CallerF = DAG.getMachineFunction().getFunction();
1774 CallingConv::ID CallerCC = CallerF->getCallingConv();
1775 bool CCMatch = CallerCC == CalleeCC;
1776
1777 // Look for obvious safe cases to perform tail call optimization that do not
1778 // require ABI changes. This is what gcc calls sibcall.
1779
Jim Grosbach7616b642010-06-16 23:45:49 +00001780 // Do not sibcall optimize vararg calls unless the call site is not passing
1781 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001782 if (isVarArg && !Outs.empty())
1783 return false;
1784
1785 // Also avoid sibcall optimization if either caller or callee uses struct
1786 // return semantics.
1787 if (isCalleeStructRet || isCallerStructRet)
1788 return false;
1789
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001790 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001791 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1792 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1793 // support in the assembler and linker to be used. This would need to be
1794 // fixed to fully support tail calls in Thumb1.
1795 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001796 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1797 // LR. This means if we need to reload LR, it takes an extra instructions,
1798 // which outweighs the value of the tail call; but here we don't know yet
1799 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001800 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001801 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001802
1803 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1804 // but we need to make sure there are enough registers; the only valid
1805 // registers are the 4 used for parameters. We don't currently do this
1806 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001807 if (Subtarget->isThumb1Only())
1808 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001809
Dale Johannesen51e28e62010-06-03 21:09:53 +00001810 // If the calling conventions do not match, then we'd better make sure the
1811 // results are returned in the same way as what the caller expects.
1812 if (!CCMatch) {
1813 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001814 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1815 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001816 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1817
1818 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001819 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1820 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001821 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1822
1823 if (RVLocs1.size() != RVLocs2.size())
1824 return false;
1825 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1826 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1827 return false;
1828 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1829 return false;
1830 if (RVLocs1[i].isRegLoc()) {
1831 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1832 return false;
1833 } else {
1834 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1835 return false;
1836 }
1837 }
1838 }
1839
Manman Rene6c3cc82012-10-12 23:39:43 +00001840 // If Caller's vararg or byval argument has been split between registers and
1841 // stack, do not perform tail call, since part of the argument is in caller's
1842 // local frame.
1843 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1844 getInfo<ARMFunctionInfo>();
1845 if (AFI_Caller->getVarArgsRegSaveSize())
1846 return false;
1847
Dale Johannesen51e28e62010-06-03 21:09:53 +00001848 // If the callee takes no arguments then go on to check the results of the
1849 // call.
1850 if (!Outs.empty()) {
1851 // Check if stack adjustment is needed. For now, do not do this if any
1852 // argument is passed on the stack.
1853 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001854 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1855 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001856 CCInfo.AnalyzeCallOperands(Outs,
1857 CCAssignFnForNode(CalleeCC, false, isVarArg));
1858 if (CCInfo.getNextStackOffset()) {
1859 MachineFunction &MF = DAG.getMachineFunction();
1860
1861 // Check if the arguments are already laid out in the right way as
1862 // the caller's fixed stack objects.
1863 MachineFrameInfo *MFI = MF.getFrameInfo();
1864 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001865 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001866 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1867 i != e;
1868 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001869 CCValAssign &VA = ArgLocs[i];
1870 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001871 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001872 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001873 if (VA.getLocInfo() == CCValAssign::Indirect)
1874 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001875 if (VA.needsCustom()) {
1876 // f64 and vector types are split into multiple registers or
1877 // register/stack-slot combinations. The types will not match
1878 // the registers; give up on memory f64 refs until we figure
1879 // out what to do about this.
1880 if (!VA.isRegLoc())
1881 return false;
1882 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001883 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001884 if (RegVT == MVT::v2f64) {
1885 if (!ArgLocs[++i].isRegLoc())
1886 return false;
1887 if (!ArgLocs[++i].isRegLoc())
1888 return false;
1889 }
1890 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001891 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1892 MFI, MRI, TII))
1893 return false;
1894 }
1895 }
1896 }
1897 }
1898
1899 return true;
1900}
1901
Benjamin Kramer350c0082012-11-28 20:55:10 +00001902bool
1903ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1904 MachineFunction &MF, bool isVarArg,
1905 const SmallVectorImpl<ISD::OutputArg> &Outs,
1906 LLVMContext &Context) const {
1907 SmallVector<CCValAssign, 16> RVLocs;
1908 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1909 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1910 isVarArg));
1911}
1912
Dan Gohman98ca4f22009-08-05 01:29:28 +00001913SDValue
1914ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001915 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001917 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001918 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001919
Bob Wilsondee46d72009-04-17 20:35:10 +00001920 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001921 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001922
Bob Wilsondee46d72009-04-17 20:35:10 +00001923 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001924 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1925 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001926
Dan Gohman98ca4f22009-08-05 01:29:28 +00001927 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001928 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1929 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001930
1931 // If this is the first return lowered for this function, add
1932 // the regs to the liveout set for the function.
1933 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1934 for (unsigned i = 0; i != RVLocs.size(); ++i)
1935 if (RVLocs[i].isRegLoc())
1936 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001937 }
1938
Bob Wilson1f595bb2009-04-17 19:07:39 +00001939 SDValue Flag;
1940
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);
1968 VA = RVLocs[++i]; // skip ahead to next loc
1969 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1970 HalfGPRs.getValue(1), Flag);
1971 Flag = Chain.getValue(1);
1972 VA = RVLocs[++i]; // skip ahead to next loc
1973
1974 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1976 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001977 }
1978 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1979 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001980 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001981 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001982 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001983 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001984 VA = RVLocs[++i]; // skip ahead to next loc
1985 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1986 Flag);
1987 } else
1988 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1989
Bob Wilsondee46d72009-04-17 20:35:10 +00001990 // Guarantee that all emitted copies are
1991 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001992 Flag = Chain.getValue(1);
1993 }
1994
1995 SDValue result;
1996 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001998 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001999 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002000
2001 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00002002}
2003
Evan Chengbf010eb2012-04-10 01:51:00 +00002004bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002005 if (N->getNumValues() != 1)
2006 return false;
2007 if (!N->hasNUsesOfValue(1, 0))
2008 return false;
2009
Evan Chengbf010eb2012-04-10 01:51:00 +00002010 SDValue TCChain = Chain;
2011 SDNode *Copy = *N->use_begin();
2012 if (Copy->getOpcode() == ISD::CopyToReg) {
2013 // If the copy has a glue operand, we conservatively assume it isn't safe to
2014 // perform a tail call.
2015 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2016 return false;
2017 TCChain = Copy->getOperand(0);
2018 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2019 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002020 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002021 SmallPtrSet<SDNode*, 2> Copies;
2022 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002023 UI != UE; ++UI) {
2024 if (UI->getOpcode() != ISD::CopyToReg)
2025 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002026 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002027 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002028 if (Copies.size() > 2)
2029 return false;
2030
2031 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2032 UI != UE; ++UI) {
2033 SDValue UseChain = UI->getOperand(0);
2034 if (Copies.count(UseChain.getNode()))
2035 // Second CopyToReg
2036 Copy = *UI;
2037 else
2038 // First CopyToReg
2039 TCChain = UseChain;
2040 }
2041 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002042 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002043 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002044 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002045 Copy = *Copy->use_begin();
2046 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002047 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002048 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002049 } else {
2050 return false;
2051 }
2052
Evan Cheng1bf891a2010-12-01 22:59:46 +00002053 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002054 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2055 UI != UE; ++UI) {
2056 if (UI->getOpcode() != ARMISD::RET_FLAG)
2057 return false;
2058 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002059 }
2060
Evan Chengbf010eb2012-04-10 01:51:00 +00002061 if (!HasRet)
2062 return false;
2063
2064 Chain = TCChain;
2065 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002066}
2067
Evan Cheng485fafc2011-03-21 01:19:09 +00002068bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002069 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002070 return false;
2071
2072 if (!CI->isTailCall())
2073 return false;
2074
2075 return !Subtarget->isThumb1Only();
2076}
2077
Bob Wilsonb62d2572009-11-03 00:02:05 +00002078// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2079// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2080// one of the above mentioned nodes. It has to be wrapped because otherwise
2081// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2082// be used to form addressing mode. These wrapped nodes will be selected
2083// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002084static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002085 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002086 // FIXME there is no actual debug info here
2087 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002088 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002089 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002090 if (CP->isMachineConstantPoolEntry())
2091 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2092 CP->getAlignment());
2093 else
2094 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2095 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002096 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002097}
2098
Jim Grosbache1102ca2010-07-19 17:20:38 +00002099unsigned ARMTargetLowering::getJumpTableEncoding() const {
2100 return MachineJumpTableInfo::EK_Inline;
2101}
2102
Dan Gohmand858e902010-04-17 15:26:15 +00002103SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2104 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002105 MachineFunction &MF = DAG.getMachineFunction();
2106 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2107 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002108 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002109 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002110 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002111 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2112 SDValue CPAddr;
2113 if (RelocM == Reloc::Static) {
2114 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2115 } else {
2116 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002117 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002118 ARMConstantPoolValue *CPV =
2119 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2120 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002121 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2122 }
2123 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2124 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002125 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002126 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002127 if (RelocM == Reloc::Static)
2128 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002129 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002130 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002131}
2132
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002134SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002135ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002136 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002137 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002138 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002139 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002140 MachineFunction &MF = DAG.getMachineFunction();
2141 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002142 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002143 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002144 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2145 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002146 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002148 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002149 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002150 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002151 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002152
Evan Chenge7e0d622009-11-06 22:24:13 +00002153 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002154 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002155
2156 // call __tls_get_addr.
2157 ArgListTy Args;
2158 ArgListEntry Entry;
2159 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002160 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002161 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002162 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002163 TargetLowering::CallLoweringInfo CLI(Chain,
2164 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002165 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002166 0, CallingConv::C, /*isTailCall=*/false,
2167 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002168 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002169 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002170 return CallResult.first;
2171}
2172
2173// Lower ISD::GlobalTLSAddress using the "initial exec" or
2174// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002175SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002176ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002177 SelectionDAG &DAG,
2178 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002179 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002180 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002181 SDValue Offset;
2182 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002183 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002184 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002185 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002186
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002187 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002188 MachineFunction &MF = DAG.getMachineFunction();
2189 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002190 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002191 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002192 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2193 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002194 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2195 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2196 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002197 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002198 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002199 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002200 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002201 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002202 Chain = Offset.getValue(1);
2203
Evan Chenge7e0d622009-11-06 22:24:13 +00002204 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002205 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002206
Evan Cheng9eda6892009-10-31 03:39:36 +00002207 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002208 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002209 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002210 } else {
2211 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002212 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002213 ARMConstantPoolValue *CPV =
2214 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002215 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002217 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002218 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002219 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002220 }
2221
2222 // The address of the thread local variable is the add of the thread
2223 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002224 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002225}
2226
Dan Gohman475871a2008-07-27 21:46:04 +00002227SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002228ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002229 // TODO: implement the "local dynamic" model
2230 assert(Subtarget->isTargetELF() &&
2231 "TLS not implemented for non-ELF targets");
2232 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002233
2234 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2235
2236 switch (model) {
2237 case TLSModel::GeneralDynamic:
2238 case TLSModel::LocalDynamic:
2239 return LowerToTLSGeneralDynamicModel(GA, DAG);
2240 case TLSModel::InitialExec:
2241 case TLSModel::LocalExec:
2242 return LowerToTLSExecModels(GA, DAG, model);
2243 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002244 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002245}
2246
Dan Gohman475871a2008-07-27 21:46:04 +00002247SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002248 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002249 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002250 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002251 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002252 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2253 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002254 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002255 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002256 ARMConstantPoolConstant::Create(GV,
2257 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002258 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002259 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002260 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002261 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002262 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002263 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002264 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002265 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002266 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002267 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002268 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002269 MachinePointerInfo::getGOT(),
2270 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002271 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002272 }
2273
2274 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002275 // pair. This is always cheaper.
2276 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002277 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002278 // FIXME: Once remat is capable of dealing with instructions with register
2279 // operands, expand this into two nodes.
2280 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2281 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002282 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002283 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2284 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2285 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2286 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002287 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002288 }
2289}
2290
Dan Gohman475871a2008-07-27 21:46:04 +00002291SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002292 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002293 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002294 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002295 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002296 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002297 MachineFunction &MF = DAG.getMachineFunction();
2298 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2299
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002300 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2301 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002302 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002303 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002304 // FIXME: Once remat is capable of dealing with instructions with register
2305 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002306 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002307 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2308 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2309
Evan Cheng53519f02011-01-21 18:55:51 +00002310 unsigned Wrapper = (RelocM == Reloc::PIC_)
2311 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2312 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002313 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002314 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2315 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002316 MachinePointerInfo::getGOT(),
2317 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002318 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002319 }
2320
2321 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002322 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002323 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002324 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002325 } else {
2326 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002327 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2328 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002329 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2330 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002331 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002332 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002334
Evan Cheng9eda6892009-10-31 03:39:36 +00002335 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002336 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002337 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002338 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002339
2340 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002341 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002342 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002343 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002344
Evan Cheng63476a82009-09-03 07:04:02 +00002345 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002346 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002347 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002348
2349 return Result;
2350}
2351
Dan Gohman475871a2008-07-27 21:46:04 +00002352SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002353 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002354 assert(Subtarget->isTargetELF() &&
2355 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002356 MachineFunction &MF = DAG.getMachineFunction();
2357 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002358 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002359 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002360 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002361 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002362 ARMConstantPoolValue *CPV =
2363 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2364 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002365 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002366 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002367 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002368 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002369 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002370 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002371 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002372}
2373
Jim Grosbach0e0da732009-05-12 23:59:14 +00002374SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002375ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2376 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002377 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002378 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2379 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002380 Op.getOperand(1), Val);
2381}
2382
2383SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002384ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2385 DebugLoc dl = Op.getDebugLoc();
2386 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2387 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2388}
2389
2390SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002391ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002392 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002393 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002394 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002395 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002396 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002397 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002398 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002399 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2400 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002401 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002402 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002403 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002404 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002405 EVT PtrVT = getPointerTy();
2406 DebugLoc dl = Op.getDebugLoc();
2407 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2408 SDValue CPAddr;
2409 unsigned PCAdj = (RelocM != Reloc::PIC_)
2410 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002411 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002412 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2413 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002414 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002415 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002416 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002417 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002418 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002419 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002420
2421 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002422 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002423 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2424 }
2425 return Result;
2426 }
Evan Cheng92e39162011-03-29 23:06:19 +00002427 case Intrinsic::arm_neon_vmulls:
2428 case Intrinsic::arm_neon_vmullu: {
2429 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2430 ? ARMISD::VMULLs : ARMISD::VMULLu;
2431 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2432 Op.getOperand(1), Op.getOperand(2));
2433 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002434 }
2435}
2436
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002437static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002438 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002439 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002440 if (!Subtarget->hasDataBarrier()) {
2441 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2442 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2443 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002444 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002445 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002446 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002447 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002448 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002449
2450 SDValue Op5 = Op.getOperand(5);
2451 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2452 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2453 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2454 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2455
2456 ARM_MB::MemBOpt DMBOpt;
2457 if (isDeviceBarrier)
2458 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2459 else
2460 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2461 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2462 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002463}
2464
Eli Friedman26689ac2011-08-03 21:06:02 +00002465
2466static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2467 const ARMSubtarget *Subtarget) {
2468 // FIXME: handle "fence singlethread" more efficiently.
2469 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002470 if (!Subtarget->hasDataBarrier()) {
2471 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2472 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2473 // here.
2474 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2475 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002476 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002477 DAG.getConstant(0, MVT::i32));
2478 }
2479
Eli Friedman26689ac2011-08-03 21:06:02 +00002480 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002481 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002482}
2483
Evan Chengdfed19f2010-11-03 06:34:55 +00002484static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2485 const ARMSubtarget *Subtarget) {
2486 // ARM pre v5TE and Thumb1 does not have preload instructions.
2487 if (!(Subtarget->isThumb2() ||
2488 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2489 // Just preserve the chain.
2490 return Op.getOperand(0);
2491
2492 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002493 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2494 if (!isRead &&
2495 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2496 // ARMv7 with MP extension has PLDW.
2497 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002498
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002499 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2500 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002501 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002502 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002503 isData = ~isData & 1;
2504 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002505
2506 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002507 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2508 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002509}
2510
Dan Gohman1e93df62010-04-17 14:41:14 +00002511static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2512 MachineFunction &MF = DAG.getMachineFunction();
2513 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2514
Evan Chenga8e29892007-01-19 07:51:42 +00002515 // vastart just stores the address of the VarArgsFrameIndex slot into the
2516 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002517 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002518 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002519 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002520 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002521 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2522 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002523}
2524
Dan Gohman475871a2008-07-27 21:46:04 +00002525SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002526ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2527 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002528 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002529 MachineFunction &MF = DAG.getMachineFunction();
2530 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2531
Craig Topper44d23822012-02-22 05:59:10 +00002532 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002533 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002534 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002535 else
Craig Topper420761a2012-04-20 07:30:17 +00002536 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002537
2538 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002539 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002540 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002541
2542 SDValue ArgValue2;
2543 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002544 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002545 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002546
2547 // Create load node to retrieve arguments from the stack.
2548 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002549 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002550 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002551 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002552 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002553 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002554 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002555 }
2556
Jim Grosbache5165492009-11-09 00:11:35 +00002557 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002558}
2559
Stuart Hastingsc7315872011-04-20 16:47:52 +00002560void
2561ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2562 unsigned &VARegSize, unsigned &VARegSaveSize)
2563 const {
2564 unsigned NumGPRs;
2565 if (CCInfo.isFirstByValRegValid())
2566 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2567 else {
2568 unsigned int firstUnalloced;
2569 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2570 sizeof(GPRArgRegs) /
2571 sizeof(GPRArgRegs[0]));
2572 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2573 }
2574
2575 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2576 VARegSize = NumGPRs * 4;
2577 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2578}
2579
2580// The remaining GPRs hold either the beginning of variable-argument
2581// data, or the beginning of an aggregate passed by value (usuall
2582// byval). Either way, we allocate stack slots adjacent to the data
2583// provided by our caller, and store the unallocated registers there.
2584// If this is a variadic function, the va_list pointer will begin with
2585// these values; otherwise, this reassembles a (byval) structure that
2586// was split between registers and memory.
2587void
2588ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2589 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002590 const Value *OrigArg,
2591 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002592 unsigned ArgOffset,
2593 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002594 MachineFunction &MF = DAG.getMachineFunction();
2595 MachineFrameInfo *MFI = MF.getFrameInfo();
2596 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2597 unsigned firstRegToSaveIndex;
2598 if (CCInfo.isFirstByValRegValid())
2599 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2600 else {
2601 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2602 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2603 }
2604
2605 unsigned VARegSize, VARegSaveSize;
2606 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2607 if (VARegSaveSize) {
2608 // If this function is vararg, store any remaining integer argument regs
2609 // to their spots on the stack so that they may be loaded by deferencing
2610 // the result of va_next.
2611 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002612 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2613 ArgOffset + VARegSaveSize
2614 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002615 false));
2616 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2617 getPointerTy());
2618
2619 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002620 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002621 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002622 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002623 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002624 else
Craig Topper420761a2012-04-20 07:30:17 +00002625 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002626
2627 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2628 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2629 SDValue Store =
2630 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002631 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002632 false, false, 0);
2633 MemOps.push_back(Store);
2634 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2635 DAG.getConstant(4, getPointerTy()));
2636 }
2637 if (!MemOps.empty())
2638 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2639 &MemOps[0], MemOps.size());
2640 } else
2641 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002642 AFI->setVarArgsFrameIndex(
2643 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002644}
2645
Bob Wilson5bafff32009-06-22 23:27:02 +00002646SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002647ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002648 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002649 const SmallVectorImpl<ISD::InputArg>
2650 &Ins,
2651 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002652 SmallVectorImpl<SDValue> &InVals)
2653 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002654 MachineFunction &MF = DAG.getMachineFunction();
2655 MachineFrameInfo *MFI = MF.getFrameInfo();
2656
Bob Wilson1f595bb2009-04-17 19:07:39 +00002657 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2658
2659 // Assign locations to all of the incoming arguments.
2660 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002661 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2662 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002663 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002664 CCAssignFnForNode(CallConv, /* Return*/ false,
2665 isVarArg));
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002666
Bob Wilson1f595bb2009-04-17 19:07:39 +00002667 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002668 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002669 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002670 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2671 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002672 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2673 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002674 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2675 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002676 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002677 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002678 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002679
Bob Wilson1f595bb2009-04-17 19:07:39 +00002680 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002681 // f64 and vector types are split up into multiple registers or
2682 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002683 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002684 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002685 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002686 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002687 SDValue ArgValue2;
2688 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002689 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002690 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2691 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002692 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002693 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002694 } else {
2695 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2696 Chain, DAG, dl);
2697 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002698 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2699 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002700 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002701 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002702 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2703 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002704 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002705
Bob Wilson5bafff32009-06-22 23:27:02 +00002706 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002707 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002708
Owen Anderson825b72b2009-08-11 20:47:22 +00002709 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002710 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002711 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002712 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002714 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002715 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002716 RC = AFI->isThumb1OnlyFunction() ?
2717 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2718 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002719 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002720 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002721
2722 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002723 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002724 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002725 }
2726
2727 // If this is an 8 or 16-bit value, it is really passed promoted
2728 // to 32 bits. Insert an assert[sz]ext to capture this, then
2729 // truncate to the right size.
2730 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002731 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002732 case CCValAssign::Full: break;
2733 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002734 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002735 break;
2736 case CCValAssign::SExt:
2737 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2738 DAG.getValueType(VA.getValVT()));
2739 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2740 break;
2741 case CCValAssign::ZExt:
2742 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2743 DAG.getValueType(VA.getValVT()));
2744 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2745 break;
2746 }
2747
Dan Gohman98ca4f22009-08-05 01:29:28 +00002748 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002749
2750 } else { // VA.isRegLoc()
2751
2752 // sanity check
2753 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002754 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002755
Stuart Hastingsf222e592011-02-28 17:17:53 +00002756 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002757
Stuart Hastingsf222e592011-02-28 17:17:53 +00002758 // Some Ins[] entries become multiple ArgLoc[] entries.
2759 // Process them only once.
2760 if (index != lastInsIndex)
2761 {
2762 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002763 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002764 // This can be changed with more analysis.
2765 // In case of tail call optimization mark all arguments mutable.
2766 // Since they could be overwritten by lowering of arguments in case of
2767 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002768 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002769 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2770 if (!AFI->getVarArgsFrameIndex()) {
2771 VarArgStyleRegisters(CCInfo, DAG,
2772 dl, Chain, CurOrigArg,
2773 Ins[VA.getValNo()].PartOffset,
2774 VA.getLocMemOffset(),
2775 true /*force mutable frames*/);
2776 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2777 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2778 } else {
2779 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2780 VA.getLocMemOffset(), false);
2781 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2782 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002783 } else {
2784 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2785 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002786
Stuart Hastingsf222e592011-02-28 17:17:53 +00002787 // Create load nodes to retrieve arguments from the stack.
2788 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2789 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2790 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002791 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002792 }
2793 lastInsIndex = index;
2794 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002795 }
2796 }
2797
2798 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002799 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002800 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2801 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002802
Dan Gohman98ca4f22009-08-05 01:29:28 +00002803 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002804}
2805
2806/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002807static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002808 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002809 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002810 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002811 // Maybe this has already been legalized into the constant pool?
2812 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002813 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002814 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002815 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002816 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002817 }
2818 }
2819 return false;
2820}
2821
Evan Chenga8e29892007-01-19 07:51:42 +00002822/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2823/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002824SDValue
2825ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002826 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002827 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002828 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002829 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002830 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002831 // Constant does not fit, try adjusting it by one?
2832 switch (CC) {
2833 default: break;
2834 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002835 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002836 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002837 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002838 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002839 }
2840 break;
2841 case ISD::SETULT:
2842 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002843 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002844 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002845 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002846 }
2847 break;
2848 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002849 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002850 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002851 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002852 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002853 }
2854 break;
2855 case ISD::SETULE:
2856 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002857 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002858 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002860 }
2861 break;
2862 }
2863 }
2864 }
2865
2866 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002867 ARMISD::NodeType CompareType;
2868 switch (CondCode) {
2869 default:
2870 CompareType = ARMISD::CMP;
2871 break;
2872 case ARMCC::EQ:
2873 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002874 // Uses only Z Flag
2875 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002876 break;
2877 }
Evan Cheng218977b2010-07-13 19:27:42 +00002878 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002879 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002880}
2881
2882/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002883SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002884ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002885 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002886 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002887 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002888 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002889 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002890 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2891 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002892}
2893
Bob Wilson79f56c92011-03-08 01:17:20 +00002894/// duplicateCmp - Glue values can have only one use, so this function
2895/// duplicates a comparison node.
2896SDValue
2897ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2898 unsigned Opc = Cmp.getOpcode();
2899 DebugLoc DL = Cmp.getDebugLoc();
2900 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2901 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2902
2903 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2904 Cmp = Cmp.getOperand(0);
2905 Opc = Cmp.getOpcode();
2906 if (Opc == ARMISD::CMPFP)
2907 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2908 else {
2909 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2910 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2911 }
2912 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2913}
2914
Bill Wendlingde2b1512010-08-11 08:43:16 +00002915SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2916 SDValue Cond = Op.getOperand(0);
2917 SDValue SelectTrue = Op.getOperand(1);
2918 SDValue SelectFalse = Op.getOperand(2);
2919 DebugLoc dl = Op.getDebugLoc();
2920
2921 // Convert:
2922 //
2923 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2924 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2925 //
2926 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2927 const ConstantSDNode *CMOVTrue =
2928 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2929 const ConstantSDNode *CMOVFalse =
2930 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2931
2932 if (CMOVTrue && CMOVFalse) {
2933 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2934 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2935
2936 SDValue True;
2937 SDValue False;
2938 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2939 True = SelectTrue;
2940 False = SelectFalse;
2941 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2942 True = SelectFalse;
2943 False = SelectTrue;
2944 }
2945
2946 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002947 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002948 SDValue ARMcc = Cond.getOperand(2);
2949 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002950 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002951 assert(True.getValueType() == VT);
2952 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002953 }
2954 }
2955 }
2956
Dan Gohmandb953892012-02-24 00:09:36 +00002957 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2958 // undefined bits before doing a full-word comparison with zero.
2959 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2960 DAG.getConstant(1, Cond.getValueType()));
2961
Bill Wendlingde2b1512010-08-11 08:43:16 +00002962 return DAG.getSelectCC(dl, Cond,
2963 DAG.getConstant(0, Cond.getValueType()),
2964 SelectTrue, SelectFalse, ISD::SETNE);
2965}
2966
Dan Gohmand858e902010-04-17 15:26:15 +00002967SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002968 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002969 SDValue LHS = Op.getOperand(0);
2970 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002971 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002972 SDValue TrueVal = Op.getOperand(2);
2973 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002974 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002975
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002977 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002979 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002980 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002981 }
2982
2983 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002984 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002985
Evan Cheng218977b2010-07-13 19:27:42 +00002986 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2987 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002988 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002989 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002990 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002991 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002992 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002993 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002994 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002995 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002996 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002997 }
2998 return Result;
2999}
3000
Evan Cheng218977b2010-07-13 19:27:42 +00003001/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3002/// to morph to an integer compare sequence.
3003static bool canChangeToInt(SDValue Op, bool &SeenZero,
3004 const ARMSubtarget *Subtarget) {
3005 SDNode *N = Op.getNode();
3006 if (!N->hasOneUse())
3007 // Otherwise it requires moving the value from fp to integer registers.
3008 return false;
3009 if (!N->getNumValues())
3010 return false;
3011 EVT VT = Op.getValueType();
3012 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3013 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3014 // vmrs are very slow, e.g. cortex-a8.
3015 return false;
3016
3017 if (isFloatingPointZero(Op)) {
3018 SeenZero = true;
3019 return true;
3020 }
3021 return ISD::isNormalLoad(N);
3022}
3023
3024static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3025 if (isFloatingPointZero(Op))
3026 return DAG.getConstant(0, MVT::i32);
3027
3028 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3029 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003030 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003031 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003032 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003033
3034 llvm_unreachable("Unknown VFP cmp argument!");
3035}
3036
3037static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3038 SDValue &RetVal1, SDValue &RetVal2) {
3039 if (isFloatingPointZero(Op)) {
3040 RetVal1 = DAG.getConstant(0, MVT::i32);
3041 RetVal2 = DAG.getConstant(0, MVT::i32);
3042 return;
3043 }
3044
3045 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3046 SDValue Ptr = Ld->getBasePtr();
3047 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3048 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003049 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003050 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003051 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003052
3053 EVT PtrType = Ptr.getValueType();
3054 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3055 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3056 PtrType, Ptr, DAG.getConstant(4, PtrType));
3057 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3058 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003059 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003060 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003061 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003062 return;
3063 }
3064
3065 llvm_unreachable("Unknown VFP cmp argument!");
3066}
3067
3068/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3069/// f32 and even f64 comparisons to integer ones.
3070SDValue
3071ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3072 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003073 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003074 SDValue LHS = Op.getOperand(2);
3075 SDValue RHS = Op.getOperand(3);
3076 SDValue Dest = Op.getOperand(4);
3077 DebugLoc dl = Op.getDebugLoc();
3078
Evan Chengfc501a32012-03-01 23:27:13 +00003079 bool LHSSeenZero = false;
3080 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3081 bool RHSSeenZero = false;
3082 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3083 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003084 // If unsafe fp math optimization is enabled and there are no other uses of
3085 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003086 // to an integer comparison.
3087 if (CC == ISD::SETOEQ)
3088 CC = ISD::SETEQ;
3089 else if (CC == ISD::SETUNE)
3090 CC = ISD::SETNE;
3091
Evan Chengfc501a32012-03-01 23:27:13 +00003092 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003093 SDValue ARMcc;
3094 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003095 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3096 bitcastf32Toi32(LHS, DAG), Mask);
3097 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3098 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003099 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3100 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3101 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3102 Chain, Dest, ARMcc, CCR, Cmp);
3103 }
3104
3105 SDValue LHS1, LHS2;
3106 SDValue RHS1, RHS2;
3107 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3108 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003109 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3110 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003111 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3112 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003113 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003114 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3115 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3116 }
3117
3118 return SDValue();
3119}
3120
3121SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3122 SDValue Chain = Op.getOperand(0);
3123 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3124 SDValue LHS = Op.getOperand(2);
3125 SDValue RHS = Op.getOperand(3);
3126 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003127 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003128
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003130 SDValue ARMcc;
3131 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003132 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003134 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003135 }
3136
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003138
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003139 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003140 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3141 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3142 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3143 if (Result.getNode())
3144 return Result;
3145 }
3146
Evan Chenga8e29892007-01-19 07:51:42 +00003147 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003148 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003149
Evan Cheng218977b2010-07-13 19:27:42 +00003150 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3151 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003153 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003154 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003155 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003156 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003157 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3158 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003159 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003160 }
3161 return Res;
3162}
3163
Dan Gohmand858e902010-04-17 15:26:15 +00003164SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003165 SDValue Chain = Op.getOperand(0);
3166 SDValue Table = Op.getOperand(1);
3167 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003168 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003169
Owen Andersone50ed302009-08-10 22:56:29 +00003170 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003171 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3172 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003173 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003174 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003176 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3177 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003178 if (Subtarget->isThumb2()) {
3179 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3180 // which does another jump to the destination. This also makes it easier
3181 // to translate it to TBB / TBH later.
3182 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003184 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003185 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003186 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003187 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003188 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003189 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003190 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003191 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003193 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003194 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003195 MachinePointerInfo::getJumpTable(),
3196 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003197 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003199 }
Evan Chenga8e29892007-01-19 07:51:42 +00003200}
3201
Eli Friedman14e809c2011-11-09 23:36:02 +00003202static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003203 EVT VT = Op.getValueType();
3204 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003205
James Molloy873fd5f2012-02-20 09:24:05 +00003206 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3207 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3208 return Op;
3209 return DAG.UnrollVectorOp(Op.getNode());
3210 }
3211
3212 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3213 "Invalid type for custom lowering!");
3214 if (VT != MVT::v4i16)
3215 return DAG.UnrollVectorOp(Op.getNode());
3216
3217 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3218 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003219}
3220
Bob Wilson76a312b2010-03-19 22:51:32 +00003221static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003222 EVT VT = Op.getValueType();
3223 if (VT.isVector())
3224 return LowerVectorFP_TO_INT(Op, DAG);
3225
Bob Wilson76a312b2010-03-19 22:51:32 +00003226 DebugLoc dl = Op.getDebugLoc();
3227 unsigned Opc;
3228
3229 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003230 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003231 case ISD::FP_TO_SINT:
3232 Opc = ARMISD::FTOSI;
3233 break;
3234 case ISD::FP_TO_UINT:
3235 Opc = ARMISD::FTOUI;
3236 break;
3237 }
3238 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003239 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003240}
3241
Cameron Zwarich3007d332011-03-29 21:41:55 +00003242static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3243 EVT VT = Op.getValueType();
3244 DebugLoc dl = Op.getDebugLoc();
3245
Eli Friedman14e809c2011-11-09 23:36:02 +00003246 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3247 if (VT.getVectorElementType() == MVT::f32)
3248 return Op;
3249 return DAG.UnrollVectorOp(Op.getNode());
3250 }
3251
Duncan Sands1f6a3292011-08-12 14:54:45 +00003252 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3253 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003254 if (VT != MVT::v4f32)
3255 return DAG.UnrollVectorOp(Op.getNode());
3256
3257 unsigned CastOpc;
3258 unsigned Opc;
3259 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003260 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003261 case ISD::SINT_TO_FP:
3262 CastOpc = ISD::SIGN_EXTEND;
3263 Opc = ISD::SINT_TO_FP;
3264 break;
3265 case ISD::UINT_TO_FP:
3266 CastOpc = ISD::ZERO_EXTEND;
3267 Opc = ISD::UINT_TO_FP;
3268 break;
3269 }
3270
3271 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3272 return DAG.getNode(Opc, dl, VT, Op);
3273}
3274
Bob Wilson76a312b2010-03-19 22:51:32 +00003275static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3276 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003277 if (VT.isVector())
3278 return LowerVectorINT_TO_FP(Op, DAG);
3279
Bob Wilson76a312b2010-03-19 22:51:32 +00003280 DebugLoc dl = Op.getDebugLoc();
3281 unsigned Opc;
3282
3283 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003284 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003285 case ISD::SINT_TO_FP:
3286 Opc = ARMISD::SITOF;
3287 break;
3288 case ISD::UINT_TO_FP:
3289 Opc = ARMISD::UITOF;
3290 break;
3291 }
3292
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003293 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003294 return DAG.getNode(Opc, dl, VT, Op);
3295}
3296
Evan Cheng515fe3a2010-07-08 02:08:50 +00003297SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003298 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003299 SDValue Tmp0 = Op.getOperand(0);
3300 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003301 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003302 EVT VT = Op.getValueType();
3303 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003304 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3305 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3306 bool UseNEON = !InGPR && Subtarget->hasNEON();
3307
3308 if (UseNEON) {
3309 // Use VBSL to copy the sign bit.
3310 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3311 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3312 DAG.getTargetConstant(EncodedVal, MVT::i32));
3313 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3314 if (VT == MVT::f64)
3315 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3316 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3317 DAG.getConstant(32, MVT::i32));
3318 else /*if (VT == MVT::f32)*/
3319 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3320 if (SrcVT == MVT::f32) {
3321 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3322 if (VT == MVT::f64)
3323 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3324 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3325 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003326 } else if (VT == MVT::f32)
3327 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3328 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3329 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003330 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3331 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3332
3333 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3334 MVT::i32);
3335 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3336 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3337 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003338
Evan Chenge573fb32011-02-23 02:24:55 +00003339 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3340 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3341 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003342 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003343 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3344 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3345 DAG.getConstant(0, MVT::i32));
3346 } else {
3347 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3348 }
3349
3350 return Res;
3351 }
Evan Chengc143dd42011-02-11 02:28:55 +00003352
3353 // Bitcast operand 1 to i32.
3354 if (SrcVT == MVT::f64)
3355 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3356 &Tmp1, 1).getValue(1);
3357 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3358
Evan Chenge573fb32011-02-23 02:24:55 +00003359 // Or in the signbit with integer operations.
3360 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3361 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3362 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3363 if (VT == MVT::f32) {
3364 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3365 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3366 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3367 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003368 }
3369
Evan Chenge573fb32011-02-23 02:24:55 +00003370 // f64: Or the high part with signbit and then combine two parts.
3371 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3372 &Tmp0, 1);
3373 SDValue Lo = Tmp0.getValue(0);
3374 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3375 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3376 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003377}
3378
Evan Cheng2457f2c2010-05-22 01:47:14 +00003379SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3380 MachineFunction &MF = DAG.getMachineFunction();
3381 MachineFrameInfo *MFI = MF.getFrameInfo();
3382 MFI->setReturnAddressIsTaken(true);
3383
3384 EVT VT = Op.getValueType();
3385 DebugLoc dl = Op.getDebugLoc();
3386 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3387 if (Depth) {
3388 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3389 SDValue Offset = DAG.getConstant(4, MVT::i32);
3390 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3391 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003392 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003393 }
3394
3395 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003396 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003397 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3398}
3399
Dan Gohmand858e902010-04-17 15:26:15 +00003400SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003401 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3402 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003403
Owen Andersone50ed302009-08-10 22:56:29 +00003404 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003405 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3406 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003407 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003408 ? ARM::R7 : ARM::R11;
3409 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3410 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003411 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3412 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003413 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003414 return FrameAddr;
3415}
3416
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003417/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003418/// expand a bit convert where either the source or destination type is i64 to
3419/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3420/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3421/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003422static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003423 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3424 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003425 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003426
Bob Wilson9f3f0612010-04-17 05:30:19 +00003427 // This function is only supposed to be called for i64 types, either as the
3428 // source or destination of the bit convert.
3429 EVT SrcVT = Op.getValueType();
3430 EVT DstVT = N->getValueType(0);
3431 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003432 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003433
Bob Wilson9f3f0612010-04-17 05:30:19 +00003434 // Turn i64->f64 into VMOVDRR.
3435 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3437 DAG.getConstant(0, MVT::i32));
3438 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3439 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003440 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003441 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003442 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003443
Jim Grosbache5165492009-11-09 00:11:35 +00003444 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003445 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3446 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3447 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3448 // Merge the pieces into a single i64 value.
3449 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3450 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003451
Bob Wilson9f3f0612010-04-17 05:30:19 +00003452 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003453}
3454
Bob Wilson5bafff32009-06-22 23:27:02 +00003455/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003456/// Zero vectors are used to represent vector negation and in those cases
3457/// will be implemented with the NEON VNEG instruction. However, VNEG does
3458/// not support i64 elements, so sometimes the zero vectors will need to be
3459/// explicitly constructed. Regardless, use a canonical VMOV to create the
3460/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003461static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003462 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003463 // The canonical modified immediate encoding of a zero vector is....0!
3464 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3465 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3466 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003467 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003468}
3469
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003470/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3471/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003472SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3473 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003474 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3475 EVT VT = Op.getValueType();
3476 unsigned VTBits = VT.getSizeInBits();
3477 DebugLoc dl = Op.getDebugLoc();
3478 SDValue ShOpLo = Op.getOperand(0);
3479 SDValue ShOpHi = Op.getOperand(1);
3480 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003481 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003482 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003483
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003484 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3485
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003486 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3487 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3488 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3489 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3490 DAG.getConstant(VTBits, MVT::i32));
3491 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3492 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003493 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003494
3495 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3496 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003497 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003498 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003499 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003500 CCR, Cmp);
3501
3502 SDValue Ops[2] = { Lo, Hi };
3503 return DAG.getMergeValues(Ops, 2, dl);
3504}
3505
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003506/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3507/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003508SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3509 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003510 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3511 EVT VT = Op.getValueType();
3512 unsigned VTBits = VT.getSizeInBits();
3513 DebugLoc dl = Op.getDebugLoc();
3514 SDValue ShOpLo = Op.getOperand(0);
3515 SDValue ShOpHi = Op.getOperand(1);
3516 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003517 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003518
3519 assert(Op.getOpcode() == ISD::SHL_PARTS);
3520 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3521 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3522 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3523 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3524 DAG.getConstant(VTBits, MVT::i32));
3525 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3526 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3527
3528 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3529 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3530 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003531 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003532 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003533 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003534 CCR, Cmp);
3535
3536 SDValue Ops[2] = { Lo, Hi };
3537 return DAG.getMergeValues(Ops, 2, dl);
3538}
3539
Jim Grosbach4725ca72010-09-08 03:54:02 +00003540SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003541 SelectionDAG &DAG) const {
3542 // The rounding mode is in bits 23:22 of the FPSCR.
3543 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3544 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3545 // so that the shift + and get folded into a bitfield extract.
3546 DebugLoc dl = Op.getDebugLoc();
3547 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3548 DAG.getConstant(Intrinsic::arm_get_fpscr,
3549 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003550 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003551 DAG.getConstant(1U << 22, MVT::i32));
3552 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3553 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003554 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003555 DAG.getConstant(3, MVT::i32));
3556}
3557
Jim Grosbach3482c802010-01-18 19:58:49 +00003558static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3559 const ARMSubtarget *ST) {
3560 EVT VT = N->getValueType(0);
3561 DebugLoc dl = N->getDebugLoc();
3562
3563 if (!ST->hasV6T2Ops())
3564 return SDValue();
3565
3566 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3567 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3568}
3569
Evan Chengc8e70452012-12-04 22:41:50 +00003570/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3571/// for each 16-bit element from operand, repeated. The basic idea is to
3572/// leverage vcnt to get the 8-bit counts, gather and add the results.
3573///
3574/// Trace for v4i16:
3575/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3576/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3577/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
3578/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
3579/// [b0 b1 b2 b3 b4 b5 b6 b7]
3580/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3581/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3582/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3583static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3584 EVT VT = N->getValueType(0);
3585 DebugLoc DL = N->getDebugLoc();
3586
3587 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3588 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3589 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3590 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3591 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3592 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3593}
3594
3595/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3596/// bit-count for each 16-bit element from the operand. We need slightly
3597/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3598/// 64/128-bit registers.
3599///
3600/// Trace for v4i16:
3601/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3602/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3603/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3604/// v4i16:Extracted = [k0 k1 k2 k3 ]
3605static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3606 EVT VT = N->getValueType(0);
3607 DebugLoc DL = N->getDebugLoc();
3608
3609 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3610 if (VT.is64BitVector()) {
3611 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3612 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3613 DAG.getIntPtrConstant(0));
3614 } else {
3615 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3616 BitCounts, DAG.getIntPtrConstant(0));
3617 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3618 }
3619}
3620
3621/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3622/// bit-count for each 32-bit element from the operand. The idea here is
3623/// to split the vector into 16-bit elements, leverage the 16-bit count
3624/// routine, and then combine the results.
3625///
3626/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3627/// input = [v0 v1 ] (vi: 32-bit elements)
3628/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3629/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
3630/// vrev: N0 = [k1 k0 k3 k2 ]
3631/// [k0 k1 k2 k3 ]
3632/// N1 =+[k1 k0 k3 k2 ]
3633/// [k0 k2 k1 k3 ]
3634/// N2 =+[k1 k3 k0 k2 ]
3635/// [k0 k2 k1 k3 ]
3636/// Extended =+[k1 k3 k0 k2 ]
3637/// [k0 k2 ]
3638/// Extracted=+[k1 k3 ]
3639///
3640static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3641 EVT VT = N->getValueType(0);
3642 DebugLoc DL = N->getDebugLoc();
3643
3644 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3645
3646 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3647 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3648 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3649 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3650 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3651
3652 if (VT.is64BitVector()) {
3653 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3654 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3655 DAG.getIntPtrConstant(0));
3656 } else {
3657 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3658 DAG.getIntPtrConstant(0));
3659 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3660 }
3661}
3662
3663static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3664 const ARMSubtarget *ST) {
3665 EVT VT = N->getValueType(0);
3666
3667 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay105ab4f2012-12-04 23:54:02 +00003668 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3669 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengc8e70452012-12-04 22:41:50 +00003670 "Unexpected type for custom ctpop lowering");
3671
3672 if (VT.getVectorElementType() == MVT::i32)
3673 return lowerCTPOP32BitElements(N, DAG);
3674 else
3675 return lowerCTPOP16BitElements(N, DAG);
3676}
3677
Bob Wilson5bafff32009-06-22 23:27:02 +00003678static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3679 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003680 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003681 DebugLoc dl = N->getDebugLoc();
3682
Bob Wilsond5448bb2010-11-18 21:16:28 +00003683 if (!VT.isVector())
3684 return SDValue();
3685
Bob Wilson5bafff32009-06-22 23:27:02 +00003686 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003687 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003688
Bob Wilsond5448bb2010-11-18 21:16:28 +00003689 // Left shifts translate directly to the vshiftu intrinsic.
3690 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003691 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003692 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3693 N->getOperand(0), N->getOperand(1));
3694
3695 assert((N->getOpcode() == ISD::SRA ||
3696 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3697
3698 // NEON uses the same intrinsics for both left and right shifts. For
3699 // right shifts, the shift amounts are negative, so negate the vector of
3700 // shift amounts.
3701 EVT ShiftVT = N->getOperand(1).getValueType();
3702 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3703 getZeroVector(ShiftVT, DAG, dl),
3704 N->getOperand(1));
3705 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3706 Intrinsic::arm_neon_vshifts :
3707 Intrinsic::arm_neon_vshiftu);
3708 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3709 DAG.getConstant(vshiftInt, MVT::i32),
3710 N->getOperand(0), NegatedCount);
3711}
3712
3713static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3714 const ARMSubtarget *ST) {
3715 EVT VT = N->getValueType(0);
3716 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003717
Eli Friedmance392eb2009-08-22 03:13:10 +00003718 // We can get here for a node like i32 = ISD::SHL i32, i64
3719 if (VT != MVT::i64)
3720 return SDValue();
3721
3722 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003723 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003724
Chris Lattner27a6c732007-11-24 07:07:01 +00003725 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3726 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003727 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003728 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003729
Chris Lattner27a6c732007-11-24 07:07:01 +00003730 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003731 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003732
Chris Lattner27a6c732007-11-24 07:07:01 +00003733 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003735 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003736 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003737 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003738
Chris Lattner27a6c732007-11-24 07:07:01 +00003739 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3740 // captures the result into a carry flag.
3741 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003742 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003743
Chris Lattner27a6c732007-11-24 07:07:01 +00003744 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003745 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003746
Chris Lattner27a6c732007-11-24 07:07:01 +00003747 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003749}
3750
Bob Wilson5bafff32009-06-22 23:27:02 +00003751static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3752 SDValue TmpOp0, TmpOp1;
3753 bool Invert = false;
3754 bool Swap = false;
3755 unsigned Opc = 0;
3756
3757 SDValue Op0 = Op.getOperand(0);
3758 SDValue Op1 = Op.getOperand(1);
3759 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003760 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003761 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3762 DebugLoc dl = Op.getDebugLoc();
3763
3764 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3765 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003766 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003767 case ISD::SETUNE:
3768 case ISD::SETNE: Invert = true; // Fallthrough
3769 case ISD::SETOEQ:
3770 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3771 case ISD::SETOLT:
3772 case ISD::SETLT: Swap = true; // Fallthrough
3773 case ISD::SETOGT:
3774 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3775 case ISD::SETOLE:
3776 case ISD::SETLE: Swap = true; // Fallthrough
3777 case ISD::SETOGE:
3778 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3779 case ISD::SETUGE: Swap = true; // Fallthrough
3780 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3781 case ISD::SETUGT: Swap = true; // Fallthrough
3782 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3783 case ISD::SETUEQ: Invert = true; // Fallthrough
3784 case ISD::SETONE:
3785 // Expand this to (OLT | OGT).
3786 TmpOp0 = Op0;
3787 TmpOp1 = Op1;
3788 Opc = ISD::OR;
3789 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3790 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3791 break;
3792 case ISD::SETUO: Invert = true; // Fallthrough
3793 case ISD::SETO:
3794 // Expand this to (OLT | OGE).
3795 TmpOp0 = Op0;
3796 TmpOp1 = Op1;
3797 Opc = ISD::OR;
3798 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3799 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3800 break;
3801 }
3802 } else {
3803 // Integer comparisons.
3804 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003805 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003806 case ISD::SETNE: Invert = true;
3807 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3808 case ISD::SETLT: Swap = true;
3809 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3810 case ISD::SETLE: Swap = true;
3811 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3812 case ISD::SETULT: Swap = true;
3813 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3814 case ISD::SETULE: Swap = true;
3815 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3816 }
3817
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003818 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 if (Opc == ARMISD::VCEQ) {
3820
3821 SDValue AndOp;
3822 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3823 AndOp = Op0;
3824 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3825 AndOp = Op1;
3826
3827 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003828 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003829 AndOp = AndOp.getOperand(0);
3830
3831 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3832 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003833 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3834 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003835 Invert = !Invert;
3836 }
3837 }
3838 }
3839
3840 if (Swap)
3841 std::swap(Op0, Op1);
3842
Owen Andersonc24cb352010-11-08 23:21:22 +00003843 // If one of the operands is a constant vector zero, attempt to fold the
3844 // comparison to a specialized compare-against-zero form.
3845 SDValue SingleOp;
3846 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3847 SingleOp = Op0;
3848 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3849 if (Opc == ARMISD::VCGE)
3850 Opc = ARMISD::VCLEZ;
3851 else if (Opc == ARMISD::VCGT)
3852 Opc = ARMISD::VCLTZ;
3853 SingleOp = Op1;
3854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003855
Owen Andersonc24cb352010-11-08 23:21:22 +00003856 SDValue Result;
3857 if (SingleOp.getNode()) {
3858 switch (Opc) {
3859 case ARMISD::VCEQ:
3860 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3861 case ARMISD::VCGE:
3862 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3863 case ARMISD::VCLEZ:
3864 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3865 case ARMISD::VCGT:
3866 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3867 case ARMISD::VCLTZ:
3868 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3869 default:
3870 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3871 }
3872 } else {
3873 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3874 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003875
3876 if (Invert)
3877 Result = DAG.getNOT(dl, Result, VT);
3878
3879 return Result;
3880}
3881
Bob Wilsond3c42842010-06-14 22:19:57 +00003882/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3883/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003884/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003885static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3886 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003887 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003888 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003889
Bob Wilson827b2102010-06-15 19:05:35 +00003890 // SplatBitSize is set to the smallest size that splats the vector, so a
3891 // zero vector will always have SplatBitSize == 8. However, NEON modified
3892 // immediate instructions others than VMOV do not support the 8-bit encoding
3893 // of a zero vector, and the default encoding of zero is supposed to be the
3894 // 32-bit version.
3895 if (SplatBits == 0)
3896 SplatBitSize = 32;
3897
Bob Wilson5bafff32009-06-22 23:27:02 +00003898 switch (SplatBitSize) {
3899 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003900 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003901 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003902 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003903 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003904 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003905 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003906 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003907 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003908
3909 case 16:
3910 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003911 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003912 if ((SplatBits & ~0xff) == 0) {
3913 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003914 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003915 Imm = SplatBits;
3916 break;
3917 }
3918 if ((SplatBits & ~0xff00) == 0) {
3919 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003920 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003921 Imm = SplatBits >> 8;
3922 break;
3923 }
3924 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003925
3926 case 32:
3927 // NEON's 32-bit VMOV supports splat values where:
3928 // * only one byte is nonzero, or
3929 // * the least significant byte is 0xff and the second byte is nonzero, or
3930 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003931 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003932 if ((SplatBits & ~0xff) == 0) {
3933 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003934 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003935 Imm = SplatBits;
3936 break;
3937 }
3938 if ((SplatBits & ~0xff00) == 0) {
3939 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003940 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003941 Imm = SplatBits >> 8;
3942 break;
3943 }
3944 if ((SplatBits & ~0xff0000) == 0) {
3945 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003946 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003947 Imm = SplatBits >> 16;
3948 break;
3949 }
3950 if ((SplatBits & ~0xff000000) == 0) {
3951 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003952 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003953 Imm = SplatBits >> 24;
3954 break;
3955 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003956
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003957 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3958 if (type == OtherModImm) return SDValue();
3959
Bob Wilson5bafff32009-06-22 23:27:02 +00003960 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003961 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3962 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003963 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003964 Imm = SplatBits >> 8;
3965 SplatBits |= 0xff;
3966 break;
3967 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003968
3969 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003970 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3971 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003972 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003973 Imm = SplatBits >> 16;
3974 SplatBits |= 0xffff;
3975 break;
3976 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003977
3978 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3979 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3980 // VMOV.I32. A (very) minor optimization would be to replicate the value
3981 // and fall through here to test for a valid 64-bit splat. But, then the
3982 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003983 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003984
3985 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003986 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003987 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003988 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003989 uint64_t BitMask = 0xff;
3990 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003991 unsigned ImmMask = 1;
3992 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003993 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003994 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003995 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003996 Imm |= ImmMask;
3997 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003998 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003999 }
Bob Wilson5bafff32009-06-22 23:27:02 +00004000 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004001 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00004002 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00004003 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004004 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004005 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00004006 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00004007 break;
4008 }
4009
Bob Wilson1a913ed2010-06-11 21:34:50 +00004010 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00004011 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00004012 }
4013
Bob Wilsoncba270d2010-07-13 21:16:48 +00004014 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4015 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00004016}
4017
Lang Hamesc0a9f822012-03-29 21:56:11 +00004018SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4019 const ARMSubtarget *ST) const {
4020 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
4021 return SDValue();
4022
4023 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4024 assert(Op.getValueType() == MVT::f32 &&
4025 "ConstantFP custom lowering should only occur for f32.");
4026
4027 // Try splatting with a VMOV.f32...
4028 APFloat FPVal = CFP->getValueAPF();
4029 int ImmVal = ARM_AM::getFP32Imm(FPVal);
4030 if (ImmVal != -1) {
4031 DebugLoc DL = Op.getDebugLoc();
4032 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4033 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4034 NewVal);
4035 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4036 DAG.getConstant(0, MVT::i32));
4037 }
4038
4039 // If that fails, try a VMOV.i32
4040 EVT VMovVT;
4041 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
4042 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
4043 VMOVModImm);
4044 if (NewVal != SDValue()) {
4045 DebugLoc DL = Op.getDebugLoc();
4046 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4047 NewVal);
4048 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4049 VecConstant);
4050 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4051 DAG.getConstant(0, MVT::i32));
4052 }
4053
4054 // Finally, try a VMVN.i32
4055 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
4056 VMVNModImm);
4057 if (NewVal != SDValue()) {
4058 DebugLoc DL = Op.getDebugLoc();
4059 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4060 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4061 VecConstant);
4062 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4063 DAG.getConstant(0, MVT::i32));
4064 }
4065
4066 return SDValue();
4067}
4068
Quentin Colombet43934ae2012-11-02 21:32:17 +00004069// check if an VEXT instruction can handle the shuffle mask when the
4070// vector sources of the shuffle are the same.
4071static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4072 unsigned NumElts = VT.getVectorNumElements();
4073
4074 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4075 if (M[0] < 0)
4076 return false;
4077
4078 Imm = M[0];
4079
4080 // If this is a VEXT shuffle, the immediate value is the index of the first
4081 // element. The other shuffle indices must be the successive elements after
4082 // the first one.
4083 unsigned ExpectedElt = Imm;
4084 for (unsigned i = 1; i < NumElts; ++i) {
4085 // Increment the expected index. If it wraps around, just follow it
4086 // back to index zero and keep going.
4087 ++ExpectedElt;
4088 if (ExpectedElt == NumElts)
4089 ExpectedElt = 0;
4090
4091 if (M[i] < 0) continue; // ignore UNDEF indices
4092 if (ExpectedElt != static_cast<unsigned>(M[i]))
4093 return false;
4094 }
4095
4096 return true;
4097}
4098
Lang Hamesc0a9f822012-03-29 21:56:11 +00004099
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004100static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004101 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004102 unsigned NumElts = VT.getVectorNumElements();
4103 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004104
4105 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4106 if (M[0] < 0)
4107 return false;
4108
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004109 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004110
4111 // If this is a VEXT shuffle, the immediate value is the index of the first
4112 // element. The other shuffle indices must be the successive elements after
4113 // the first one.
4114 unsigned ExpectedElt = Imm;
4115 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004116 // Increment the expected index. If it wraps around, it may still be
4117 // a VEXT but the source vectors must be swapped.
4118 ExpectedElt += 1;
4119 if (ExpectedElt == NumElts * 2) {
4120 ExpectedElt = 0;
4121 ReverseVEXT = true;
4122 }
4123
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004124 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004125 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004126 return false;
4127 }
4128
4129 // Adjust the index value if the source operands will be swapped.
4130 if (ReverseVEXT)
4131 Imm -= NumElts;
4132
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004133 return true;
4134}
4135
Bob Wilson8bb9e482009-07-26 00:39:34 +00004136/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4137/// instruction with the specified blocksize. (The order of the elements
4138/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004139static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004140 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4141 "Only possible block sizes for VREV are: 16, 32, 64");
4142
Bob Wilson8bb9e482009-07-26 00:39:34 +00004143 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004144 if (EltSz == 64)
4145 return false;
4146
4147 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004148 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004149 // If the first shuffle index is UNDEF, be optimistic.
4150 if (M[0] < 0)
4151 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004152
4153 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4154 return false;
4155
4156 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004157 if (M[i] < 0) continue; // ignore UNDEF indices
4158 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004159 return false;
4160 }
4161
4162 return true;
4163}
4164
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004165static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004166 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4167 // range, then 0 is placed into the resulting vector. So pretty much any mask
4168 // of 8 elements can work here.
4169 return VT == MVT::v8i8 && M.size() == 8;
4170}
4171
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004172static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004173 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4174 if (EltSz == 64)
4175 return false;
4176
Bob Wilsonc692cb72009-08-21 20:54:19 +00004177 unsigned NumElts = VT.getVectorNumElements();
4178 WhichResult = (M[0] == 0 ? 0 : 1);
4179 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004180 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4181 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004182 return false;
4183 }
4184 return true;
4185}
4186
Bob Wilson324f4f12009-12-03 06:40:55 +00004187/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4188/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4189/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004190static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004191 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4192 if (EltSz == 64)
4193 return false;
4194
4195 unsigned NumElts = VT.getVectorNumElements();
4196 WhichResult = (M[0] == 0 ? 0 : 1);
4197 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004198 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4199 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004200 return false;
4201 }
4202 return true;
4203}
4204
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004205static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004206 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4207 if (EltSz == 64)
4208 return false;
4209
Bob Wilsonc692cb72009-08-21 20:54:19 +00004210 unsigned NumElts = VT.getVectorNumElements();
4211 WhichResult = (M[0] == 0 ? 0 : 1);
4212 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004213 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004214 if ((unsigned) M[i] != 2 * i + WhichResult)
4215 return false;
4216 }
4217
4218 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004219 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004220 return false;
4221
4222 return true;
4223}
4224
Bob Wilson324f4f12009-12-03 06:40:55 +00004225/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4226/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4227/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004228static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004229 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4230 if (EltSz == 64)
4231 return false;
4232
4233 unsigned Half = VT.getVectorNumElements() / 2;
4234 WhichResult = (M[0] == 0 ? 0 : 1);
4235 for (unsigned j = 0; j != 2; ++j) {
4236 unsigned Idx = WhichResult;
4237 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004238 int MIdx = M[i + j * Half];
4239 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004240 return false;
4241 Idx += 2;
4242 }
4243 }
4244
4245 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4246 if (VT.is64BitVector() && EltSz == 32)
4247 return false;
4248
4249 return true;
4250}
4251
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004252static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004253 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4254 if (EltSz == 64)
4255 return false;
4256
Bob Wilsonc692cb72009-08-21 20:54:19 +00004257 unsigned NumElts = VT.getVectorNumElements();
4258 WhichResult = (M[0] == 0 ? 0 : 1);
4259 unsigned Idx = WhichResult * NumElts / 2;
4260 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004261 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4262 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004263 return false;
4264 Idx += 1;
4265 }
4266
4267 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004268 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004269 return false;
4270
4271 return true;
4272}
4273
Bob Wilson324f4f12009-12-03 06:40:55 +00004274/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4275/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4276/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004277static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004278 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4279 if (EltSz == 64)
4280 return false;
4281
4282 unsigned NumElts = VT.getVectorNumElements();
4283 WhichResult = (M[0] == 0 ? 0 : 1);
4284 unsigned Idx = WhichResult * NumElts / 2;
4285 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004286 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4287 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004288 return false;
4289 Idx += 1;
4290 }
4291
4292 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4293 if (VT.is64BitVector() && EltSz == 32)
4294 return false;
4295
4296 return true;
4297}
4298
Dale Johannesenf630c712010-07-29 20:10:08 +00004299// If N is an integer constant that can be moved into a register in one
4300// instruction, return an SDValue of such a constant (will become a MOV
4301// instruction). Otherwise return null.
4302static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4303 const ARMSubtarget *ST, DebugLoc dl) {
4304 uint64_t Val;
4305 if (!isa<ConstantSDNode>(N))
4306 return SDValue();
4307 Val = cast<ConstantSDNode>(N)->getZExtValue();
4308
4309 if (ST->isThumb1Only()) {
4310 if (Val <= 255 || ~Val <= 255)
4311 return DAG.getConstant(Val, MVT::i32);
4312 } else {
4313 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4314 return DAG.getConstant(Val, MVT::i32);
4315 }
4316 return SDValue();
4317}
4318
Bob Wilson5bafff32009-06-22 23:27:02 +00004319// If this is a case we can't handle, return null and let the default
4320// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004321SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4322 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004323 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004324 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004325 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004326
4327 APInt SplatBits, SplatUndef;
4328 unsigned SplatBitSize;
4329 bool HasAnyUndefs;
4330 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004331 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004332 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004333 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004334 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004335 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004336 DAG, VmovVT, VT.is128BitVector(),
4337 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004338 if (Val.getNode()) {
4339 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004340 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004341 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004342
4343 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004344 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004345 Val = isNEONModifiedImm(NegatedImm,
4346 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004347 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004348 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004349 if (Val.getNode()) {
4350 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004351 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004352 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004353
4354 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004355 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004356 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004357 if (ImmVal != -1) {
4358 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4359 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4360 }
4361 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004362 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004363 }
4364
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004365 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004366 //
4367 // As an optimisation, even if more than one value is used it may be more
4368 // profitable to splat with one value then change some lanes.
4369 //
4370 // Heuristically we decide to do this if the vector has a "dominant" value,
4371 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004372 unsigned NumElts = VT.getVectorNumElements();
4373 bool isOnlyLowElement = true;
4374 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004375 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004376 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004377
4378 // Map of the number of times a particular SDValue appears in the
4379 // element list.
James Molloy95154342012-09-06 10:32:08 +00004380 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004381 SDValue Value;
4382 for (unsigned i = 0; i < NumElts; ++i) {
4383 SDValue V = Op.getOperand(i);
4384 if (V.getOpcode() == ISD::UNDEF)
4385 continue;
4386 if (i > 0)
4387 isOnlyLowElement = false;
4388 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4389 isConstant = false;
4390
James Molloyba8562a2012-09-06 09:55:02 +00004391 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004392 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004393
4394 // Is this value dominant? (takes up more than half of the lanes)
4395 if (++Count > (NumElts / 2)) {
4396 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004397 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004398 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004399 }
James Molloyba8562a2012-09-06 09:55:02 +00004400 if (ValueCounts.size() != 1)
4401 usesOnlyOneValue = false;
4402 if (!Value.getNode() && ValueCounts.size() > 0)
4403 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004404
James Molloyba8562a2012-09-06 09:55:02 +00004405 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004406 return DAG.getUNDEF(VT);
4407
4408 if (isOnlyLowElement)
4409 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4410
Dale Johannesenf630c712010-07-29 20:10:08 +00004411 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4412
Dale Johannesen575cd142010-10-19 20:00:17 +00004413 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4414 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004415 if (hasDominantValue && EltSize <= 32) {
4416 if (!isConstant) {
4417 SDValue N;
4418
4419 // If we are VDUPing a value that comes directly from a vector, that will
4420 // cause an unnecessary move to and from a GPR, where instead we could
4421 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004422 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4423 // We need to create a new undef vector to use for the VDUPLANE if the
4424 // size of the vector from which we get the value is different than the
4425 // size of the vector that we need to create. We will insert the element
4426 // such that the register coalescer will remove unnecessary copies.
4427 if (VT != Value->getOperand(0).getValueType()) {
4428 ConstantSDNode *constIndex;
4429 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4430 assert(constIndex && "The index is not a constant!");
4431 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4432 VT.getVectorNumElements();
4433 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4434 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4435 Value, DAG.getConstant(index, MVT::i32)),
4436 DAG.getConstant(index, MVT::i32));
4437 } else {
4438 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004439 Value->getOperand(0), Value->getOperand(1));
Silviu Barangabb1078e2012-10-15 09:41:32 +00004440 }
4441 }
James Molloyba8562a2012-09-06 09:55:02 +00004442 else
4443 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4444
4445 if (!usesOnlyOneValue) {
4446 // The dominant value was splatted as 'N', but we now have to insert
4447 // all differing elements.
4448 for (unsigned I = 0; I < NumElts; ++I) {
4449 if (Op.getOperand(I) == Value)
4450 continue;
4451 SmallVector<SDValue, 3> Ops;
4452 Ops.push_back(N);
4453 Ops.push_back(Op.getOperand(I));
4454 Ops.push_back(DAG.getConstant(I, MVT::i32));
4455 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4456 }
4457 }
4458 return N;
4459 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004460 if (VT.getVectorElementType().isFloatingPoint()) {
4461 SmallVector<SDValue, 8> Ops;
4462 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004463 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004464 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004465 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4466 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004467 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4468 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004469 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004470 }
James Molloyba8562a2012-09-06 09:55:02 +00004471 if (usesOnlyOneValue) {
4472 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4473 if (isConstant && Val.getNode())
4474 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4475 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004476 }
4477
4478 // If all elements are constants and the case above didn't get hit, fall back
4479 // to the default expansion, which will generate a load from the constant
4480 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004481 if (isConstant)
4482 return SDValue();
4483
Bob Wilson11a1dff2011-01-07 21:37:30 +00004484 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4485 if (NumElts >= 4) {
4486 SDValue shuffle = ReconstructShuffle(Op, DAG);
4487 if (shuffle != SDValue())
4488 return shuffle;
4489 }
4490
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004491 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004492 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4493 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004494 if (EltSize >= 32) {
4495 // Do the expansion with floating-point types, since that is what the VFP
4496 // registers are defined to use, and since i64 is not legal.
4497 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4498 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004499 SmallVector<SDValue, 8> Ops;
4500 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004501 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004502 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004503 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004504 }
4505
4506 return SDValue();
4507}
4508
Bob Wilson11a1dff2011-01-07 21:37:30 +00004509// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004510// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004511SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4512 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004513 DebugLoc dl = Op.getDebugLoc();
4514 EVT VT = Op.getValueType();
4515 unsigned NumElts = VT.getVectorNumElements();
4516
4517 SmallVector<SDValue, 2> SourceVecs;
4518 SmallVector<unsigned, 2> MinElts;
4519 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004520
Bob Wilson11a1dff2011-01-07 21:37:30 +00004521 for (unsigned i = 0; i < NumElts; ++i) {
4522 SDValue V = Op.getOperand(i);
4523 if (V.getOpcode() == ISD::UNDEF)
4524 continue;
4525 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4526 // A shuffle can only come from building a vector from various
4527 // elements of other vectors.
4528 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004529 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4530 VT.getVectorElementType()) {
4531 // This code doesn't know how to handle shuffles where the vector
4532 // element types do not match (this happens because type legalization
4533 // promotes the return type of EXTRACT_VECTOR_ELT).
4534 // FIXME: It might be appropriate to extend this code to handle
4535 // mismatched types.
4536 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004537 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004538
Bob Wilson11a1dff2011-01-07 21:37:30 +00004539 // Record this extraction against the appropriate vector if possible...
4540 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004541 // If the element number isn't a constant, we can't effectively
4542 // analyze what's going on.
4543 if (!isa<ConstantSDNode>(V.getOperand(1)))
4544 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004545 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4546 bool FoundSource = false;
4547 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4548 if (SourceVecs[j] == SourceVec) {
4549 if (MinElts[j] > EltNo)
4550 MinElts[j] = EltNo;
4551 if (MaxElts[j] < EltNo)
4552 MaxElts[j] = EltNo;
4553 FoundSource = true;
4554 break;
4555 }
4556 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004557
Bob Wilson11a1dff2011-01-07 21:37:30 +00004558 // Or record a new source if not...
4559 if (!FoundSource) {
4560 SourceVecs.push_back(SourceVec);
4561 MinElts.push_back(EltNo);
4562 MaxElts.push_back(EltNo);
4563 }
4564 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004565
Bob Wilson11a1dff2011-01-07 21:37:30 +00004566 // Currently only do something sane when at most two source vectors
4567 // involved.
4568 if (SourceVecs.size() > 2)
4569 return SDValue();
4570
4571 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4572 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004573
Bob Wilson11a1dff2011-01-07 21:37:30 +00004574 // This loop extracts the usage patterns of the source vectors
4575 // and prepares appropriate SDValues for a shuffle if possible.
4576 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4577 if (SourceVecs[i].getValueType() == VT) {
4578 // No VEXT necessary
4579 ShuffleSrcs[i] = SourceVecs[i];
4580 VEXTOffsets[i] = 0;
4581 continue;
4582 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4583 // It probably isn't worth padding out a smaller vector just to
4584 // break it down again in a shuffle.
4585 return SDValue();
4586 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004587
Bob Wilson11a1dff2011-01-07 21:37:30 +00004588 // Since only 64-bit and 128-bit vectors are legal on ARM and
4589 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004590 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4591 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004592
Bob Wilson11a1dff2011-01-07 21:37:30 +00004593 if (MaxElts[i] - MinElts[i] >= NumElts) {
4594 // Span too large for a VEXT to cope
4595 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004596 }
4597
Bob Wilson11a1dff2011-01-07 21:37:30 +00004598 if (MinElts[i] >= NumElts) {
4599 // The extraction can just take the second half
4600 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004601 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4602 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004603 DAG.getIntPtrConstant(NumElts));
4604 } else if (MaxElts[i] < NumElts) {
4605 // The extraction can just take the first half
4606 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004607 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4608 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004609 DAG.getIntPtrConstant(0));
4610 } else {
4611 // An actual VEXT is needed
4612 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004613 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4614 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004615 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004616 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4617 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004618 DAG.getIntPtrConstant(NumElts));
4619 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4620 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4621 }
4622 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004623
Bob Wilson11a1dff2011-01-07 21:37:30 +00004624 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004625
Bob Wilson11a1dff2011-01-07 21:37:30 +00004626 for (unsigned i = 0; i < NumElts; ++i) {
4627 SDValue Entry = Op.getOperand(i);
4628 if (Entry.getOpcode() == ISD::UNDEF) {
4629 Mask.push_back(-1);
4630 continue;
4631 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004632
Bob Wilson11a1dff2011-01-07 21:37:30 +00004633 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004634 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4635 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004636 if (ExtractVec == SourceVecs[0]) {
4637 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4638 } else {
4639 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4640 }
4641 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004642
Bob Wilson11a1dff2011-01-07 21:37:30 +00004643 // Final check before we try to produce nonsense...
4644 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004645 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4646 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004647
Bob Wilson11a1dff2011-01-07 21:37:30 +00004648 return SDValue();
4649}
4650
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004651/// isShuffleMaskLegal - Targets can use this to indicate that they only
4652/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4653/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4654/// are assumed to be legal.
4655bool
4656ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4657 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004658 if (VT.getVectorNumElements() == 4 &&
4659 (VT.is128BitVector() || VT.is64BitVector())) {
4660 unsigned PFIndexes[4];
4661 for (unsigned i = 0; i != 4; ++i) {
4662 if (M[i] < 0)
4663 PFIndexes[i] = 8;
4664 else
4665 PFIndexes[i] = M[i];
4666 }
4667
4668 // Compute the index in the perfect shuffle table.
4669 unsigned PFTableIndex =
4670 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4671 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4672 unsigned Cost = (PFEntry >> 30);
4673
4674 if (Cost <= 4)
4675 return true;
4676 }
4677
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004678 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004679 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004680
Bob Wilson53dd2452010-06-07 23:53:38 +00004681 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4682 return (EltSize >= 32 ||
4683 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004684 isVREVMask(M, VT, 64) ||
4685 isVREVMask(M, VT, 32) ||
4686 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004687 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004688 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004689 isVTRNMask(M, VT, WhichResult) ||
4690 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004691 isVZIPMask(M, VT, WhichResult) ||
4692 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4693 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4694 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004695}
4696
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004697/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4698/// the specified operations to build the shuffle.
4699static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4700 SDValue RHS, SelectionDAG &DAG,
4701 DebugLoc dl) {
4702 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4703 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4704 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4705
4706 enum {
4707 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4708 OP_VREV,
4709 OP_VDUP0,
4710 OP_VDUP1,
4711 OP_VDUP2,
4712 OP_VDUP3,
4713 OP_VEXT1,
4714 OP_VEXT2,
4715 OP_VEXT3,
4716 OP_VUZPL, // VUZP, left result
4717 OP_VUZPR, // VUZP, right result
4718 OP_VZIPL, // VZIP, left result
4719 OP_VZIPR, // VZIP, right result
4720 OP_VTRNL, // VTRN, left result
4721 OP_VTRNR // VTRN, right result
4722 };
4723
4724 if (OpNum == OP_COPY) {
4725 if (LHSID == (1*9+2)*9+3) return LHS;
4726 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4727 return RHS;
4728 }
4729
4730 SDValue OpLHS, OpRHS;
4731 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4732 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4733 EVT VT = OpLHS.getValueType();
4734
4735 switch (OpNum) {
4736 default: llvm_unreachable("Unknown shuffle opcode!");
4737 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004738 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004739 if (VT.getVectorElementType() == MVT::i32 ||
4740 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004741 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4742 // vrev <4 x i16> -> VREV32
4743 if (VT.getVectorElementType() == MVT::i16)
4744 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4745 // vrev <4 x i8> -> VREV16
4746 assert(VT.getVectorElementType() == MVT::i8);
4747 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004748 case OP_VDUP0:
4749 case OP_VDUP1:
4750 case OP_VDUP2:
4751 case OP_VDUP3:
4752 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004753 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004754 case OP_VEXT1:
4755 case OP_VEXT2:
4756 case OP_VEXT3:
4757 return DAG.getNode(ARMISD::VEXT, dl, VT,
4758 OpLHS, OpRHS,
4759 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4760 case OP_VUZPL:
4761 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004762 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004763 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4764 case OP_VZIPL:
4765 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004766 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004767 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4768 case OP_VTRNL:
4769 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004770 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4771 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004772 }
4773}
4774
Bill Wendling69a05a72011-03-14 23:02:38 +00004775static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004776 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004777 SelectionDAG &DAG) {
4778 // Check to see if we can use the VTBL instruction.
4779 SDValue V1 = Op.getOperand(0);
4780 SDValue V2 = Op.getOperand(1);
4781 DebugLoc DL = Op.getDebugLoc();
4782
4783 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004784 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004785 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4786 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4787
4788 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4789 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4790 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4791 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004792
Owen Anderson76706012011-04-05 21:48:57 +00004793 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004794 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4795 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004796}
4797
Bob Wilson5bafff32009-06-22 23:27:02 +00004798static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004799 SDValue V1 = Op.getOperand(0);
4800 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004801 DebugLoc dl = Op.getDebugLoc();
4802 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004803 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004804
Bob Wilson28865062009-08-13 02:13:04 +00004805 // Convert shuffles that are directly supported on NEON to target-specific
4806 // DAG nodes, instead of keeping them as shuffles and matching them again
4807 // during code selection. This is more efficient and avoids the possibility
4808 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004809 // FIXME: floating-point vectors should be canonicalized to integer vectors
4810 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004811 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004812
Bob Wilson53dd2452010-06-07 23:53:38 +00004813 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4814 if (EltSize <= 32) {
4815 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4816 int Lane = SVN->getSplatIndex();
4817 // If this is undef splat, generate it via "just" vdup, if possible.
4818 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004819
Dan Gohman65fd6562011-11-03 21:49:52 +00004820 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004821 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4822 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4823 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004824 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4825 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4826 // reaches it).
4827 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4828 !isa<ConstantSDNode>(V1.getOperand(0))) {
4829 bool IsScalarToVector = true;
4830 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4831 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4832 IsScalarToVector = false;
4833 break;
4834 }
4835 if (IsScalarToVector)
4836 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4837 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004838 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4839 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004840 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004841
4842 bool ReverseVEXT;
4843 unsigned Imm;
4844 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4845 if (ReverseVEXT)
4846 std::swap(V1, V2);
4847 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4848 DAG.getConstant(Imm, MVT::i32));
4849 }
4850
4851 if (isVREVMask(ShuffleMask, VT, 64))
4852 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4853 if (isVREVMask(ShuffleMask, VT, 32))
4854 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4855 if (isVREVMask(ShuffleMask, VT, 16))
4856 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4857
Quentin Colombet43934ae2012-11-02 21:32:17 +00004858 if (V2->getOpcode() == ISD::UNDEF &&
4859 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4860 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4861 DAG.getConstant(Imm, MVT::i32));
4862 }
4863
Bob Wilson53dd2452010-06-07 23:53:38 +00004864 // Check for Neon shuffles that modify both input vectors in place.
4865 // If both results are used, i.e., if there are two shuffles with the same
4866 // source operands and with masks corresponding to both results of one of
4867 // these operations, DAG memoization will ensure that a single node is
4868 // used for both shuffles.
4869 unsigned WhichResult;
4870 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4871 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4872 V1, V2).getValue(WhichResult);
4873 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4874 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4875 V1, V2).getValue(WhichResult);
4876 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4877 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4878 V1, V2).getValue(WhichResult);
4879
4880 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4881 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4882 V1, V1).getValue(WhichResult);
4883 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4884 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4885 V1, V1).getValue(WhichResult);
4886 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4887 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4888 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004889 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004890
Bob Wilsonc692cb72009-08-21 20:54:19 +00004891 // If the shuffle is not directly supported and it has 4 elements, use
4892 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004893 unsigned NumElts = VT.getVectorNumElements();
4894 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004895 unsigned PFIndexes[4];
4896 for (unsigned i = 0; i != 4; ++i) {
4897 if (ShuffleMask[i] < 0)
4898 PFIndexes[i] = 8;
4899 else
4900 PFIndexes[i] = ShuffleMask[i];
4901 }
4902
4903 // Compute the index in the perfect shuffle table.
4904 unsigned PFTableIndex =
4905 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004906 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4907 unsigned Cost = (PFEntry >> 30);
4908
4909 if (Cost <= 4)
4910 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4911 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004912
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004913 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004914 if (EltSize >= 32) {
4915 // Do the expansion with floating-point types, since that is what the VFP
4916 // registers are defined to use, and since i64 is not legal.
4917 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4918 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004919 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4920 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004921 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004922 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004923 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004924 Ops.push_back(DAG.getUNDEF(EltVT));
4925 else
4926 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4927 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4928 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4929 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004930 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004931 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004932 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004933 }
4934
Bill Wendling69a05a72011-03-14 23:02:38 +00004935 if (VT == MVT::v8i8) {
4936 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4937 if (NewOp.getNode())
4938 return NewOp;
4939 }
4940
Bob Wilson22cac0d2009-08-14 05:16:33 +00004941 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004942}
4943
Eli Friedman5c89cb82011-10-24 23:08:52 +00004944static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4945 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4946 SDValue Lane = Op.getOperand(2);
4947 if (!isa<ConstantSDNode>(Lane))
4948 return SDValue();
4949
4950 return Op;
4951}
4952
Bob Wilson5bafff32009-06-22 23:27:02 +00004953static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004954 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004955 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004956 if (!isa<ConstantSDNode>(Lane))
4957 return SDValue();
4958
4959 SDValue Vec = Op.getOperand(0);
4960 if (Op.getValueType() == MVT::i32 &&
4961 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4962 DebugLoc dl = Op.getDebugLoc();
4963 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4964 }
4965
4966 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004967}
4968
Bob Wilsona6d65862009-08-03 20:36:38 +00004969static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4970 // The only time a CONCAT_VECTORS operation can have legal types is when
4971 // two 64-bit vectors are concatenated to a 128-bit vector.
4972 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4973 "unexpected CONCAT_VECTORS");
4974 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004975 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004976 SDValue Op0 = Op.getOperand(0);
4977 SDValue Op1 = Op.getOperand(1);
4978 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004979 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004980 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004981 DAG.getIntPtrConstant(0));
4982 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004983 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004984 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004985 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004986 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004987}
4988
Bob Wilson626613d2010-11-23 19:38:38 +00004989/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4990/// element has been zero/sign-extended, depending on the isSigned parameter,
4991/// from an integer type half its size.
4992static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4993 bool isSigned) {
4994 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4995 EVT VT = N->getValueType(0);
4996 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4997 SDNode *BVN = N->getOperand(0).getNode();
4998 if (BVN->getValueType(0) != MVT::v4i32 ||
4999 BVN->getOpcode() != ISD::BUILD_VECTOR)
5000 return false;
5001 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5002 unsigned HiElt = 1 - LoElt;
5003 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5004 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5005 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5006 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5007 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5008 return false;
5009 if (isSigned) {
5010 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5011 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5012 return true;
5013 } else {
5014 if (Hi0->isNullValue() && Hi1->isNullValue())
5015 return true;
5016 }
5017 return false;
5018 }
5019
5020 if (N->getOpcode() != ISD::BUILD_VECTOR)
5021 return false;
5022
5023 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5024 SDNode *Elt = N->getOperand(i).getNode();
5025 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5026 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5027 unsigned HalfSize = EltSize / 2;
5028 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00005029 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005030 return false;
5031 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00005032 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005033 return false;
5034 }
5035 continue;
5036 }
5037 return false;
5038 }
5039
5040 return true;
5041}
5042
5043/// isSignExtended - Check if a node is a vector value that is sign-extended
5044/// or a constant BUILD_VECTOR with sign-extended elements.
5045static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5046 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5047 return true;
5048 if (isExtendedBUILD_VECTOR(N, DAG, true))
5049 return true;
5050 return false;
5051}
5052
5053/// isZeroExtended - Check if a node is a vector value that is zero-extended
5054/// or a constant BUILD_VECTOR with zero-extended elements.
5055static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5056 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5057 return true;
5058 if (isExtendedBUILD_VECTOR(N, DAG, false))
5059 return true;
5060 return false;
5061}
5062
Sebastian Popcb495302012-11-30 19:08:04 +00005063/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5064/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5065/// We insert the required extension here to get the vector to fill a D register.
5066static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5067 const EVT &OrigTy,
5068 const EVT &ExtTy,
5069 unsigned ExtOpcode) {
5070 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5071 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5072 // 64-bits we need to insert a new extension so that it will be 64-bits.
5073 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5074 if (OrigTy.getSizeInBits() >= 64)
5075 return N;
5076
5077 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5078 MVT::SimpleValueType OrigSimpleTy = OrigTy.getSimpleVT().SimpleTy;
5079 EVT NewVT;
5080 switch (OrigSimpleTy) {
5081 default: llvm_unreachable("Unexpected Orig Vector Type");
5082 case MVT::v2i8:
5083 case MVT::v2i16:
5084 NewVT = MVT::v2i32;
5085 break;
5086 case MVT::v4i8:
5087 NewVT = MVT::v4i16;
5088 break;
5089 }
5090 return DAG.getNode(ExtOpcode, N->getDebugLoc(), NewVT, N);
5091}
5092
5093/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5094/// does not do any sign/zero extension. If the original vector is less
5095/// than 64 bits, an appropriate extension will be added after the load to
5096/// reach a total size of 64 bits. We have to add the extension separately
5097/// because ARM does not have a sign/zero extending load for vectors.
5098static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5099 SDValue NonExtendingLoad =
5100 DAG.getLoad(LD->getMemoryVT(), LD->getDebugLoc(), LD->getChain(),
5101 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5102 LD->isNonTemporal(), LD->isInvariant(),
5103 LD->getAlignment());
5104 unsigned ExtOp = 0;
5105 switch (LD->getExtensionType()) {
5106 default: llvm_unreachable("Unexpected LoadExtType");
5107 case ISD::EXTLOAD:
5108 case ISD::SEXTLOAD: ExtOp = ISD::SIGN_EXTEND; break;
5109 case ISD::ZEXTLOAD: ExtOp = ISD::ZERO_EXTEND; break;
5110 }
5111 MVT::SimpleValueType MemType = LD->getMemoryVT().getSimpleVT().SimpleTy;
5112 MVT::SimpleValueType ExtType = LD->getValueType(0).getSimpleVT().SimpleTy;
5113 return AddRequiredExtensionForVMULL(NonExtendingLoad, DAG,
5114 MemType, ExtType, ExtOp);
5115}
5116
5117/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5118/// extending load, or BUILD_VECTOR with extended elements, return the
5119/// unextended value. The unextended vector should be 64 bits so that it can
5120/// be used as an operand to a VMULL instruction. If the original vector size
5121/// before extension is less than 64 bits we add a an extension to resize
5122/// the vector to 64 bits.
5123static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005124 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popcb495302012-11-30 19:08:04 +00005125 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5126 N->getOperand(0)->getValueType(0),
5127 N->getValueType(0),
5128 N->getOpcode());
5129
Bob Wilson626613d2010-11-23 19:38:38 +00005130 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popcb495302012-11-30 19:08:04 +00005131 return SkipLoadExtensionForVMULL(LD, DAG);
5132
Bob Wilson626613d2010-11-23 19:38:38 +00005133 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5134 // have been legalized as a BITCAST from v4i32.
5135 if (N->getOpcode() == ISD::BITCAST) {
5136 SDNode *BVN = N->getOperand(0).getNode();
5137 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5138 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5139 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5140 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
5141 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5142 }
5143 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5144 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5145 EVT VT = N->getValueType(0);
5146 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5147 unsigned NumElts = VT.getVectorNumElements();
5148 MVT TruncVT = MVT::getIntegerVT(EltSize);
5149 SmallVector<SDValue, 8> Ops;
5150 for (unsigned i = 0; i != NumElts; ++i) {
5151 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5152 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00005153 // Element types smaller than 32 bits are not legal, so use i32 elements.
5154 // The values are implicitly truncated so sext vs. zext doesn't matter.
5155 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00005156 }
5157 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5158 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005159}
5160
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005161static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5162 unsigned Opcode = N->getOpcode();
5163 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5164 SDNode *N0 = N->getOperand(0).getNode();
5165 SDNode *N1 = N->getOperand(1).getNode();
5166 return N0->hasOneUse() && N1->hasOneUse() &&
5167 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5168 }
5169 return false;
5170}
5171
5172static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5173 unsigned Opcode = N->getOpcode();
5174 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5175 SDNode *N0 = N->getOperand(0).getNode();
5176 SDNode *N1 = N->getOperand(1).getNode();
5177 return N0->hasOneUse() && N1->hasOneUse() &&
5178 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5179 }
5180 return false;
5181}
5182
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005183static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5184 // Multiplications are only custom-lowered for 128-bit vectors so that
5185 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5186 EVT VT = Op.getValueType();
Sebastian Popcb495302012-11-30 19:08:04 +00005187 assert(VT.is128BitVector() && VT.isInteger() &&
5188 "unexpected type for custom-lowering ISD::MUL");
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005189 SDNode *N0 = Op.getOperand(0).getNode();
5190 SDNode *N1 = Op.getOperand(1).getNode();
5191 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005192 bool isMLA = false;
5193 bool isN0SExt = isSignExtended(N0, DAG);
5194 bool isN1SExt = isSignExtended(N1, DAG);
5195 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005196 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005197 else {
5198 bool isN0ZExt = isZeroExtended(N0, DAG);
5199 bool isN1ZExt = isZeroExtended(N1, DAG);
5200 if (isN0ZExt && isN1ZExt)
5201 NewOpc = ARMISD::VMULLu;
5202 else if (isN1SExt || isN1ZExt) {
5203 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5204 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5205 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5206 NewOpc = ARMISD::VMULLs;
5207 isMLA = true;
5208 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5209 NewOpc = ARMISD::VMULLu;
5210 isMLA = true;
5211 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5212 std::swap(N0, N1);
5213 NewOpc = ARMISD::VMULLu;
5214 isMLA = true;
5215 }
5216 }
5217
5218 if (!NewOpc) {
5219 if (VT == MVT::v2i64)
5220 // Fall through to expand this. It is not legal.
5221 return SDValue();
5222 else
5223 // Other vector multiplications are legal.
5224 return Op;
5225 }
5226 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005227
5228 // Legalize to a VMULL instruction.
5229 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005230 SDValue Op0;
Sebastian Popcb495302012-11-30 19:08:04 +00005231 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005232 if (!isMLA) {
Sebastian Popcb495302012-11-30 19:08:04 +00005233 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005234 assert(Op0.getValueType().is64BitVector() &&
5235 Op1.getValueType().is64BitVector() &&
5236 "unexpected types for extended operands to VMULL");
5237 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5238 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005239
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005240 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5241 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5242 // vmull q0, d4, d6
5243 // vmlal q0, d5, d6
5244 // is faster than
5245 // vaddl q0, d4, d5
5246 // vmovl q1, d6
5247 // vmul q0, q0, q1
Sebastian Popcb495302012-11-30 19:08:04 +00005248 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5249 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005250 EVT Op1VT = Op1.getValueType();
5251 return DAG.getNode(N0->getOpcode(), DL, VT,
5252 DAG.getNode(NewOpc, DL, VT,
5253 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5254 DAG.getNode(NewOpc, DL, VT,
5255 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005256}
5257
Owen Anderson76706012011-04-05 21:48:57 +00005258static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005259LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5260 // Convert to float
5261 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5262 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5263 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5264 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5265 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5266 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5267 // Get reciprocal estimate.
5268 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005269 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005270 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5271 // Because char has a smaller range than uchar, we can actually get away
5272 // without any newton steps. This requires that we use a weird bias
5273 // of 0xb000, however (again, this has been exhaustively tested).
5274 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5275 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5276 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5277 Y = DAG.getConstant(0xb000, MVT::i32);
5278 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5279 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5280 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5281 // Convert back to short.
5282 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5283 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5284 return X;
5285}
5286
Owen Anderson76706012011-04-05 21:48:57 +00005287static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005288LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5289 SDValue N2;
5290 // Convert to float.
5291 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5292 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5293 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5294 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5295 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5296 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005297
Nate Begeman7973f352011-02-11 20:53:29 +00005298 // Use reciprocal estimate and one refinement step.
5299 // float4 recip = vrecpeq_f32(yf);
5300 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005301 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005302 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005303 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005304 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5305 N1, N2);
5306 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5307 // Because short has a smaller range than ushort, we can actually get away
5308 // with only a single newton step. This requires that we use a weird bias
5309 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005310 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005311 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5312 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005313 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005314 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5315 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5316 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5317 // Convert back to integer and return.
5318 // return vmovn_s32(vcvt_s32_f32(result));
5319 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5320 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5321 return N0;
5322}
5323
5324static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5325 EVT VT = Op.getValueType();
5326 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5327 "unexpected type for custom-lowering ISD::SDIV");
5328
5329 DebugLoc dl = Op.getDebugLoc();
5330 SDValue N0 = Op.getOperand(0);
5331 SDValue N1 = Op.getOperand(1);
5332 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005333
Nate Begeman7973f352011-02-11 20:53:29 +00005334 if (VT == MVT::v8i8) {
5335 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5336 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005337
Nate Begeman7973f352011-02-11 20:53:29 +00005338 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5339 DAG.getIntPtrConstant(4));
5340 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005341 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005342 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5343 DAG.getIntPtrConstant(0));
5344 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5345 DAG.getIntPtrConstant(0));
5346
5347 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5348 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5349
5350 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5351 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005352
Nate Begeman7973f352011-02-11 20:53:29 +00005353 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5354 return N0;
5355 }
5356 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5357}
5358
5359static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5360 EVT VT = Op.getValueType();
5361 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5362 "unexpected type for custom-lowering ISD::UDIV");
5363
5364 DebugLoc dl = Op.getDebugLoc();
5365 SDValue N0 = Op.getOperand(0);
5366 SDValue N1 = Op.getOperand(1);
5367 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005368
Nate Begeman7973f352011-02-11 20:53:29 +00005369 if (VT == MVT::v8i8) {
5370 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5371 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005372
Nate Begeman7973f352011-02-11 20:53:29 +00005373 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5374 DAG.getIntPtrConstant(4));
5375 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005376 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005377 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5378 DAG.getIntPtrConstant(0));
5379 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5380 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005381
Nate Begeman7973f352011-02-11 20:53:29 +00005382 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5383 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005384
Nate Begeman7973f352011-02-11 20:53:29 +00005385 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5386 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005387
5388 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005389 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5390 N0);
5391 return N0;
5392 }
Owen Anderson76706012011-04-05 21:48:57 +00005393
Nate Begeman7973f352011-02-11 20:53:29 +00005394 // v4i16 sdiv ... Convert to float.
5395 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5396 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5397 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5398 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5399 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005400 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005401
5402 // Use reciprocal estimate and two refinement steps.
5403 // float4 recip = vrecpeq_f32(yf);
5404 // recip *= vrecpsq_f32(yf, recip);
5405 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005406 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005407 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005408 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005409 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005410 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005411 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005412 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005413 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005414 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005415 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5416 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5417 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5418 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005419 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005420 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5421 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5422 N1 = DAG.getConstant(2, MVT::i32);
5423 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5424 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5425 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5426 // Convert back to integer and return.
5427 // return vmovn_u32(vcvt_s32_f32(result));
5428 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5429 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5430 return N0;
5431}
5432
Evan Cheng342e3162011-08-30 01:34:54 +00005433static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5434 EVT VT = Op.getNode()->getValueType(0);
5435 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5436
5437 unsigned Opc;
5438 bool ExtraOp = false;
5439 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005440 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005441 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5442 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5443 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5444 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5445 }
5446
5447 if (!ExtraOp)
5448 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5449 Op.getOperand(1));
5450 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5451 Op.getOperand(1), Op.getOperand(2));
5452}
5453
Eli Friedman74bf18c2011-09-15 22:26:18 +00005454static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005455 // Monotonic load/store is legal for all targets
5456 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5457 return Op;
5458
5459 // Aquire/Release load/store is not legal for targets without a
5460 // dmb or equivalent available.
5461 return SDValue();
5462}
5463
5464
Eli Friedman2bdffe42011-08-31 00:31:29 +00005465static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005466ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5467 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005468 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005469 assert (Node->getValueType(0) == MVT::i64 &&
5470 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005471
Eli Friedman4d3f3292011-08-31 17:52:22 +00005472 SmallVector<SDValue, 6> Ops;
5473 Ops.push_back(Node->getOperand(0)); // Chain
5474 Ops.push_back(Node->getOperand(1)); // Ptr
5475 // Low part of Val1
5476 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5477 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5478 // High part of Val1
5479 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5480 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005481 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005482 // High part of Val1
5483 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5484 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5485 // High part of Val2
5486 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5487 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5488 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005489 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5490 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005491 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005492 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005493 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005494 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5495 Results.push_back(Result.getValue(2));
5496}
5497
Dan Gohmand858e902010-04-17 15:26:15 +00005498SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005499 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005500 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005501 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005502 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005503 case ISD::GlobalAddress:
5504 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5505 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005506 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005507 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005508 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5509 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005510 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005511 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005512 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005513 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005514 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005515 case ISD::SINT_TO_FP:
5516 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5517 case ISD::FP_TO_SINT:
5518 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005519 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005520 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005521 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005522 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005523 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005524 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005525 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5526 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005527 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005528 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005529 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005530 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005531 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005532 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005533 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005534 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengc8e70452012-12-04 22:41:50 +00005535 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005536 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005537 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005538 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005539 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005540 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005541 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005542 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005543 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005544 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005545 case ISD::SDIV: return LowerSDIV(Op, DAG);
5546 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005547 case ISD::ADDC:
5548 case ISD::ADDE:
5549 case ISD::SUBC:
5550 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005551 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005552 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005553 }
Evan Chenga8e29892007-01-19 07:51:42 +00005554}
5555
Duncan Sands1607f052008-12-01 11:39:25 +00005556/// ReplaceNodeResults - Replace the results of node with an illegal result
5557/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005558void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5559 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005560 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005561 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005562 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005563 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005564 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005565 case ISD::BITCAST:
5566 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005567 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005568 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005569 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005570 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005571 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005572 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005573 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005574 return;
5575 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005576 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005577 return;
5578 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005579 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005580 return;
5581 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005582 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005583 return;
5584 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005585 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005586 return;
5587 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005588 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005589 return;
5590 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005591 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005592 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005593 case ISD::ATOMIC_CMP_SWAP:
5594 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5595 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005596 case ISD::ATOMIC_LOAD_MIN:
5597 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5598 return;
5599 case ISD::ATOMIC_LOAD_UMIN:
5600 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5601 return;
5602 case ISD::ATOMIC_LOAD_MAX:
5603 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5604 return;
5605 case ISD::ATOMIC_LOAD_UMAX:
5606 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5607 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005608 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005609 if (Res.getNode())
5610 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005611}
Chris Lattner27a6c732007-11-24 07:07:01 +00005612
Evan Chenga8e29892007-01-19 07:51:42 +00005613//===----------------------------------------------------------------------===//
5614// ARM Scheduler Hooks
5615//===----------------------------------------------------------------------===//
5616
5617MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005618ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5619 MachineBasicBlock *BB,
5620 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005621 unsigned dest = MI->getOperand(0).getReg();
5622 unsigned ptr = MI->getOperand(1).getReg();
5623 unsigned oldval = MI->getOperand(2).getReg();
5624 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005625 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5626 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005627 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005628
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005629 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005630 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5631 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5632 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005633
5634 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005635 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5636 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5637 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005638 }
5639
Jim Grosbach5278eb82009-12-11 01:42:04 +00005640 unsigned ldrOpc, strOpc;
5641 switch (Size) {
5642 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005643 case 1:
5644 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005645 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005646 break;
5647 case 2:
5648 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5649 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5650 break;
5651 case 4:
5652 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5653 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5654 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005655 }
5656
5657 MachineFunction *MF = BB->getParent();
5658 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5659 MachineFunction::iterator It = BB;
5660 ++It; // insert the new blocks after the current block
5661
5662 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5663 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5664 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5665 MF->insert(It, loop1MBB);
5666 MF->insert(It, loop2MBB);
5667 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005668
5669 // Transfer the remainder of BB and its successor edges to exitMBB.
5670 exitMBB->splice(exitMBB->begin(), BB,
5671 llvm::next(MachineBasicBlock::iterator(MI)),
5672 BB->end());
5673 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005674
5675 // thisMBB:
5676 // ...
5677 // fallthrough --> loop1MBB
5678 BB->addSuccessor(loop1MBB);
5679
5680 // loop1MBB:
5681 // ldrex dest, [ptr]
5682 // cmp dest, oldval
5683 // bne exitMBB
5684 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005685 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5686 if (ldrOpc == ARM::t2LDREX)
5687 MIB.addImm(0);
5688 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005689 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005690 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005691 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5692 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005693 BB->addSuccessor(loop2MBB);
5694 BB->addSuccessor(exitMBB);
5695
5696 // loop2MBB:
5697 // strex scratch, newval, [ptr]
5698 // cmp scratch, #0
5699 // bne loop1MBB
5700 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005701 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5702 if (strOpc == ARM::t2STREX)
5703 MIB.addImm(0);
5704 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005705 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005706 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005707 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5708 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005709 BB->addSuccessor(loop1MBB);
5710 BB->addSuccessor(exitMBB);
5711
5712 // exitMBB:
5713 // ...
5714 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005715
Dan Gohman14152b42010-07-06 20:24:04 +00005716 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005717
Jim Grosbach5278eb82009-12-11 01:42:04 +00005718 return BB;
5719}
5720
5721MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005722ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5723 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005724 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5725 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5726
5727 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005728 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005729 MachineFunction::iterator It = BB;
5730 ++It;
5731
5732 unsigned dest = MI->getOperand(0).getReg();
5733 unsigned ptr = MI->getOperand(1).getReg();
5734 unsigned incr = MI->getOperand(2).getReg();
5735 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005736 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005737
5738 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5739 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005740 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5741 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005742 }
5743
Jim Grosbachc3c23542009-12-14 04:22:04 +00005744 unsigned ldrOpc, strOpc;
5745 switch (Size) {
5746 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005747 case 1:
5748 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005749 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005750 break;
5751 case 2:
5752 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5753 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5754 break;
5755 case 4:
5756 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5757 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5758 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005759 }
5760
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005761 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5762 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5763 MF->insert(It, loopMBB);
5764 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005765
5766 // Transfer the remainder of BB and its successor edges to exitMBB.
5767 exitMBB->splice(exitMBB->begin(), BB,
5768 llvm::next(MachineBasicBlock::iterator(MI)),
5769 BB->end());
5770 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005771
Craig Topper420761a2012-04-20 07:30:17 +00005772 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005773 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005774 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005775 unsigned scratch = MRI.createVirtualRegister(TRC);
5776 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005777
5778 // thisMBB:
5779 // ...
5780 // fallthrough --> loopMBB
5781 BB->addSuccessor(loopMBB);
5782
5783 // loopMBB:
5784 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005785 // <binop> scratch2, dest, incr
5786 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005787 // cmp scratch, #0
5788 // bne- loopMBB
5789 // fallthrough --> exitMBB
5790 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005791 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5792 if (ldrOpc == ARM::t2LDREX)
5793 MIB.addImm(0);
5794 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005795 if (BinOpcode) {
5796 // operand order needs to go the other way for NAND
5797 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5798 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5799 addReg(incr).addReg(dest)).addReg(0);
5800 else
5801 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5802 addReg(dest).addReg(incr)).addReg(0);
5803 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005804
Jim Grosbachb6aed502011-09-09 18:37:27 +00005805 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5806 if (strOpc == ARM::t2STREX)
5807 MIB.addImm(0);
5808 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005809 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005810 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005811 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5812 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005813
5814 BB->addSuccessor(loopMBB);
5815 BB->addSuccessor(exitMBB);
5816
5817 // exitMBB:
5818 // ...
5819 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005820
Dan Gohman14152b42010-07-06 20:24:04 +00005821 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005822
Jim Grosbachc3c23542009-12-14 04:22:04 +00005823 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005824}
5825
Jim Grosbachf7da8822011-04-26 19:44:18 +00005826MachineBasicBlock *
5827ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5828 MachineBasicBlock *BB,
5829 unsigned Size,
5830 bool signExtend,
5831 ARMCC::CondCodes Cond) const {
5832 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5833
5834 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5835 MachineFunction *MF = BB->getParent();
5836 MachineFunction::iterator It = BB;
5837 ++It;
5838
5839 unsigned dest = MI->getOperand(0).getReg();
5840 unsigned ptr = MI->getOperand(1).getReg();
5841 unsigned incr = MI->getOperand(2).getReg();
5842 unsigned oldval = dest;
5843 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005844 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005845
5846 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5847 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005848 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5849 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005850 }
5851
Jim Grosbachf7da8822011-04-26 19:44:18 +00005852 unsigned ldrOpc, strOpc, extendOpc;
5853 switch (Size) {
5854 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5855 case 1:
5856 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5857 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005858 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005859 break;
5860 case 2:
5861 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5862 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005863 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005864 break;
5865 case 4:
5866 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5867 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5868 extendOpc = 0;
5869 break;
5870 }
5871
5872 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5873 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5874 MF->insert(It, loopMBB);
5875 MF->insert(It, exitMBB);
5876
5877 // Transfer the remainder of BB and its successor edges to exitMBB.
5878 exitMBB->splice(exitMBB->begin(), BB,
5879 llvm::next(MachineBasicBlock::iterator(MI)),
5880 BB->end());
5881 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5882
Craig Topper420761a2012-04-20 07:30:17 +00005883 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005884 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005885 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005886 unsigned scratch = MRI.createVirtualRegister(TRC);
5887 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005888
5889 // thisMBB:
5890 // ...
5891 // fallthrough --> loopMBB
5892 BB->addSuccessor(loopMBB);
5893
5894 // loopMBB:
5895 // ldrex dest, ptr
5896 // (sign extend dest, if required)
5897 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005898 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005899 // strex scratch, scratch2, ptr
5900 // cmp scratch, #0
5901 // bne- loopMBB
5902 // fallthrough --> exitMBB
5903 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005904 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5905 if (ldrOpc == ARM::t2LDREX)
5906 MIB.addImm(0);
5907 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005908
5909 // Sign extend the value, if necessary.
5910 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005911 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005912 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5913 .addReg(dest)
5914 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005915 }
5916
5917 // Build compare and cmov instructions.
5918 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5919 .addReg(oldval).addReg(incr));
5920 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005921 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005922
Jim Grosbachb6aed502011-09-09 18:37:27 +00005923 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5924 if (strOpc == ARM::t2STREX)
5925 MIB.addImm(0);
5926 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005927 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5928 .addReg(scratch).addImm(0));
5929 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5930 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5931
5932 BB->addSuccessor(loopMBB);
5933 BB->addSuccessor(exitMBB);
5934
5935 // exitMBB:
5936 // ...
5937 BB = exitMBB;
5938
5939 MI->eraseFromParent(); // The instruction is gone now.
5940
5941 return BB;
5942}
5943
Eli Friedman2bdffe42011-08-31 00:31:29 +00005944MachineBasicBlock *
5945ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5946 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00005947 bool NeedsCarry, bool IsCmpxchg,
5948 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005949 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5950 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5951
5952 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5953 MachineFunction *MF = BB->getParent();
5954 MachineFunction::iterator It = BB;
5955 ++It;
5956
5957 unsigned destlo = MI->getOperand(0).getReg();
5958 unsigned desthi = MI->getOperand(1).getReg();
5959 unsigned ptr = MI->getOperand(2).getReg();
5960 unsigned vallo = MI->getOperand(3).getReg();
5961 unsigned valhi = MI->getOperand(4).getReg();
5962 DebugLoc dl = MI->getDebugLoc();
5963 bool isThumb2 = Subtarget->isThumb2();
5964
5965 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5966 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005967 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5968 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5969 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005970 }
5971
5972 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5973 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5974
5975 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005976 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005977 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00005978 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005979 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00005980 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005981 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005982
Eli Friedman2bdffe42011-08-31 00:31:29 +00005983 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005984 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
5985 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005986 MF->insert(It, exitMBB);
5987
5988 // Transfer the remainder of BB and its successor edges to exitMBB.
5989 exitMBB->splice(exitMBB->begin(), BB,
5990 llvm::next(MachineBasicBlock::iterator(MI)),
5991 BB->end());
5992 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5993
Craig Topper420761a2012-04-20 07:30:17 +00005994 const TargetRegisterClass *TRC = isThumb2 ?
5995 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5996 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005997 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5998
5999 // thisMBB:
6000 // ...
6001 // fallthrough --> loopMBB
6002 BB->addSuccessor(loopMBB);
6003
6004 // loopMBB:
6005 // ldrexd r2, r3, ptr
6006 // <binopa> r0, r2, incr
6007 // <binopb> r1, r3, incr
6008 // strexd storesuccess, r0, r1, ptr
6009 // cmp storesuccess, #0
6010 // bne- loopMBB
6011 // fallthrough --> exitMBB
6012 //
6013 // Note that the registers are explicitly specified because there is not any
6014 // way to force the register allocator to allocate a register pair.
6015 //
Andrew Trick3af7a672011-09-20 03:06:13 +00006016 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00006017 // need to properly enforce the restriction that the two output registers
6018 // for ldrexd must be different.
6019 BB = loopMBB;
6020 // Load
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006021 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6022 unsigned GPRPair1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006023 unsigned GPRPair2;
6024 if (IsMinMax) {
6025 //We need an extra double register for doing min/max.
6026 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6027 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6028 GPRPair2 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6029 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
6030 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6031 .addReg(undef)
6032 .addReg(vallo)
6033 .addImm(ARM::gsub_0);
6034 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair2)
6035 .addReg(r1)
6036 .addReg(valhi)
6037 .addImm(ARM::gsub_1);
6038 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006039
Eli Friedman2bdffe42011-08-31 00:31:29 +00006040 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006041 .addReg(GPRPair0, RegState::Define).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00006042 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006043 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6044 .addReg(GPRPair0, 0, ARM::gsub_0);
6045 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6046 .addReg(GPRPair0, 0, ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006047
6048 if (IsCmpxchg) {
6049 // Add early exit
6050 for (unsigned i = 0; i < 2; i++) {
6051 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6052 ARM::CMPrr))
6053 .addReg(i == 0 ? destlo : desthi)
6054 .addReg(i == 0 ? vallo : valhi));
6055 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6056 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6057 BB->addSuccessor(exitMBB);
6058 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6059 BB = (i == 0 ? contBB : cont2BB);
6060 }
6061
6062 // Copy to physregs for strexd
6063 unsigned setlo = MI->getOperand(5).getReg();
6064 unsigned sethi = MI->getOperand(6).getReg();
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006065 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6066 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6067 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
6068 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6069 .addReg(undef)
6070 .addReg(setlo)
6071 .addImm(ARM::gsub_0);
6072 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
6073 .addReg(r1)
6074 .addReg(sethi)
6075 .addImm(ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006076 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006077 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006078 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6079 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00006080 .addReg(destlo).addReg(vallo))
6081 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006082 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6083 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006084 .addReg(desthi).addReg(valhi))
6085 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006086
6087 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6088 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6089 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6090 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6091 .addReg(UndefPair)
6092 .addReg(tmpRegLo)
6093 .addImm(ARM::gsub_0);
6094 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
6095 .addReg(r1)
6096 .addReg(tmpRegHi)
6097 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006098 } else {
6099 // Copy to physregs for strexd
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006100 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6101 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6102 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6103 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6104 .addReg(UndefPair)
6105 .addReg(vallo)
6106 .addImm(ARM::gsub_0);
6107 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
6108 .addReg(r1)
6109 .addReg(valhi)
6110 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006111 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00006112 unsigned GPRPairStore = GPRPair1;
6113 if (IsMinMax) {
6114 // Compare and branch to exit block.
6115 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6116 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6117 BB->addSuccessor(exitMBB);
6118 BB->addSuccessor(contBB);
6119 BB = contBB;
6120 GPRPairStore = GPRPair2;
6121 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006122
6123 // Store
6124 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006125 .addReg(GPRPairStore).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00006126 // Cmp+jump
6127 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6128 .addReg(storesuccess).addImm(0));
6129 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6130 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6131
6132 BB->addSuccessor(loopMBB);
6133 BB->addSuccessor(exitMBB);
6134
6135 // exitMBB:
6136 // ...
6137 BB = exitMBB;
6138
6139 MI->eraseFromParent(); // The instruction is gone now.
6140
6141 return BB;
6142}
6143
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006144/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6145/// registers the function context.
6146void ARMTargetLowering::
6147SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6148 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006149 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6150 DebugLoc dl = MI->getDebugLoc();
6151 MachineFunction *MF = MBB->getParent();
6152 MachineRegisterInfo *MRI = &MF->getRegInfo();
6153 MachineConstantPool *MCP = MF->getConstantPool();
6154 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6155 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006156
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006157 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006158 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006159
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006160 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006161 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006162 ARMConstantPoolValue *CPV =
6163 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6164 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6165
Craig Topper420761a2012-04-20 07:30:17 +00006166 const TargetRegisterClass *TRC = isThumb ?
6167 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6168 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006169
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006170 // Grab constant pool and fixed stack memory operands.
6171 MachineMemOperand *CPMMO =
6172 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6173 MachineMemOperand::MOLoad, 4, 4);
6174
6175 MachineMemOperand *FIMMOSt =
6176 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6177 MachineMemOperand::MOStore, 4, 4);
6178
6179 // Load the address of the dispatch MBB into the jump buffer.
6180 if (isThumb2) {
6181 // Incoming value: jbuf
6182 // ldr.n r5, LCPI1_1
6183 // orr r5, r5, #1
6184 // add r5, pc
6185 // str r5, [$jbuf, #+4] ; &jbuf[1]
6186 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6187 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6188 .addConstantPoolIndex(CPI)
6189 .addMemOperand(CPMMO));
6190 // Set the low bit because of thumb mode.
6191 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6192 AddDefaultCC(
6193 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6194 .addReg(NewVReg1, RegState::Kill)
6195 .addImm(0x01)));
6196 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6197 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6198 .addReg(NewVReg2, RegState::Kill)
6199 .addImm(PCLabelId);
6200 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6201 .addReg(NewVReg3, RegState::Kill)
6202 .addFrameIndex(FI)
6203 .addImm(36) // &jbuf[1] :: pc
6204 .addMemOperand(FIMMOSt));
6205 } else if (isThumb) {
6206 // Incoming value: jbuf
6207 // ldr.n r1, LCPI1_4
6208 // add r1, pc
6209 // mov r2, #1
6210 // orrs r1, r2
6211 // add r2, $jbuf, #+4 ; &jbuf[1]
6212 // str r1, [r2]
6213 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6214 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6215 .addConstantPoolIndex(CPI)
6216 .addMemOperand(CPMMO));
6217 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6218 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6219 .addReg(NewVReg1, RegState::Kill)
6220 .addImm(PCLabelId);
6221 // Set the low bit because of thumb mode.
6222 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6223 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6224 .addReg(ARM::CPSR, RegState::Define)
6225 .addImm(1));
6226 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6227 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6228 .addReg(ARM::CPSR, RegState::Define)
6229 .addReg(NewVReg2, RegState::Kill)
6230 .addReg(NewVReg3, RegState::Kill));
6231 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6232 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6233 .addFrameIndex(FI)
6234 .addImm(36)); // &jbuf[1] :: pc
6235 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6236 .addReg(NewVReg4, RegState::Kill)
6237 .addReg(NewVReg5, RegState::Kill)
6238 .addImm(0)
6239 .addMemOperand(FIMMOSt));
6240 } else {
6241 // Incoming value: jbuf
6242 // ldr r1, LCPI1_1
6243 // add r1, pc, r1
6244 // str r1, [$jbuf, #+4] ; &jbuf[1]
6245 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6246 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6247 .addConstantPoolIndex(CPI)
6248 .addImm(0)
6249 .addMemOperand(CPMMO));
6250 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6251 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6252 .addReg(NewVReg1, RegState::Kill)
6253 .addImm(PCLabelId));
6254 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6255 .addReg(NewVReg2, RegState::Kill)
6256 .addFrameIndex(FI)
6257 .addImm(36) // &jbuf[1] :: pc
6258 .addMemOperand(FIMMOSt));
6259 }
6260}
6261
6262MachineBasicBlock *ARMTargetLowering::
6263EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6264 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6265 DebugLoc dl = MI->getDebugLoc();
6266 MachineFunction *MF = MBB->getParent();
6267 MachineRegisterInfo *MRI = &MF->getRegInfo();
6268 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6269 MachineFrameInfo *MFI = MF->getFrameInfo();
6270 int FI = MFI->getFunctionContextIndex();
6271
Craig Topper420761a2012-04-20 07:30:17 +00006272 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6273 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006274 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006275
Bill Wendling04f15b42011-10-06 21:29:56 +00006276 // Get a mapping of the call site numbers to all of the landing pads they're
6277 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006278 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6279 unsigned MaxCSNum = 0;
6280 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006281 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6282 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006283 if (!BB->isLandingPad()) continue;
6284
6285 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6286 // pad.
6287 for (MachineBasicBlock::iterator
6288 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6289 if (!II->isEHLabel()) continue;
6290
6291 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006292 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006293
Bill Wendling5cbef192011-10-05 23:28:57 +00006294 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6295 for (SmallVectorImpl<unsigned>::iterator
6296 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6297 CSI != CSE; ++CSI) {
6298 CallSiteNumToLPad[*CSI].push_back(BB);
6299 MaxCSNum = std::max(MaxCSNum, *CSI);
6300 }
Bill Wendling2a850152011-10-05 00:02:33 +00006301 break;
6302 }
6303 }
6304
6305 // Get an ordered list of the machine basic blocks for the jump table.
6306 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006307 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006308 LPadList.reserve(CallSiteNumToLPad.size());
6309 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6310 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6311 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006312 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006313 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006314 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6315 }
Bill Wendling2a850152011-10-05 00:02:33 +00006316 }
6317
Bill Wendling5cbef192011-10-05 23:28:57 +00006318 assert(!LPadList.empty() &&
6319 "No landing pad destinations for the dispatch jump table!");
6320
Bill Wendling04f15b42011-10-06 21:29:56 +00006321 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006322 MachineJumpTableInfo *JTI =
6323 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6324 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6325 unsigned UId = AFI->createJumpTableUId();
6326
Bill Wendling04f15b42011-10-06 21:29:56 +00006327 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006328
6329 // Shove the dispatch's address into the return slot in the function context.
6330 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6331 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006332
Bill Wendlingbb734682011-10-05 00:39:32 +00006333 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00006334 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00006335 DispatchBB->addSuccessor(TrapBB);
6336
6337 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6338 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006339
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006340 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006341 MF->insert(MF->end(), DispatchBB);
6342 MF->insert(MF->end(), DispContBB);
6343 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006344
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006345 // Insert code into the entry block that creates and registers the function
6346 // context.
6347 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6348
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006349 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006350 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006351 MachineMemOperand::MOLoad |
6352 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006353
Chad Rosiere7bd5192012-11-06 23:05:24 +00006354 MachineInstrBuilder MIB;
6355 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6356
6357 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6358 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6359
6360 // Add a register mask with no preserved registers. This results in all
6361 // registers being marked as clobbered.
6362 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006363
Bill Wendling952cb502011-10-18 22:49:07 +00006364 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006365 if (Subtarget->isThumb2()) {
6366 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6367 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6368 .addFrameIndex(FI)
6369 .addImm(4)
6370 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006371
Bill Wendling952cb502011-10-18 22:49:07 +00006372 if (NumLPads < 256) {
6373 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6374 .addReg(NewVReg1)
6375 .addImm(LPadList.size()));
6376 } else {
6377 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6378 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006379 .addImm(NumLPads & 0xFFFF));
6380
6381 unsigned VReg2 = VReg1;
6382 if ((NumLPads & 0xFFFF0000) != 0) {
6383 VReg2 = MRI->createVirtualRegister(TRC);
6384 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6385 .addReg(VReg1)
6386 .addImm(NumLPads >> 16));
6387 }
6388
Bill Wendling952cb502011-10-18 22:49:07 +00006389 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6390 .addReg(NewVReg1)
6391 .addReg(VReg2));
6392 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006393
Bill Wendling95ce2e92011-10-06 22:53:00 +00006394 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6395 .addMBB(TrapBB)
6396 .addImm(ARMCC::HI)
6397 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006398
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006399 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6400 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006401 .addJumpTableIndex(MJTI)
6402 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006403
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006404 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006405 AddDefaultCC(
6406 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006407 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6408 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006409 .addReg(NewVReg1)
6410 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6411
6412 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006413 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006414 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006415 .addJumpTableIndex(MJTI)
6416 .addImm(UId);
6417 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006418 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6419 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6420 .addFrameIndex(FI)
6421 .addImm(1)
6422 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006423
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006424 if (NumLPads < 256) {
6425 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6426 .addReg(NewVReg1)
6427 .addImm(NumLPads));
6428 } else {
6429 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006430 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6431 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6432
6433 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006434 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006435 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006436 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006437 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006438
6439 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6440 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6441 .addReg(VReg1, RegState::Define)
6442 .addConstantPoolIndex(Idx));
6443 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6444 .addReg(NewVReg1)
6445 .addReg(VReg1));
6446 }
6447
Bill Wendling083a8eb2011-10-06 23:37:36 +00006448 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6449 .addMBB(TrapBB)
6450 .addImm(ARMCC::HI)
6451 .addReg(ARM::CPSR);
6452
6453 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6454 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6455 .addReg(ARM::CPSR, RegState::Define)
6456 .addReg(NewVReg1)
6457 .addImm(2));
6458
6459 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006460 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006461 .addJumpTableIndex(MJTI)
6462 .addImm(UId));
6463
6464 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6465 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6466 .addReg(ARM::CPSR, RegState::Define)
6467 .addReg(NewVReg2, RegState::Kill)
6468 .addReg(NewVReg3));
6469
6470 MachineMemOperand *JTMMOLd =
6471 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6472 MachineMemOperand::MOLoad, 4, 4);
6473
6474 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6475 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6476 .addReg(NewVReg4, RegState::Kill)
6477 .addImm(0)
6478 .addMemOperand(JTMMOLd));
6479
6480 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6481 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6482 .addReg(ARM::CPSR, RegState::Define)
6483 .addReg(NewVReg5, RegState::Kill)
6484 .addReg(NewVReg3));
6485
6486 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6487 .addReg(NewVReg6, RegState::Kill)
6488 .addJumpTableIndex(MJTI)
6489 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006490 } else {
6491 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6492 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6493 .addFrameIndex(FI)
6494 .addImm(4)
6495 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006496
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006497 if (NumLPads < 256) {
6498 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6499 .addReg(NewVReg1)
6500 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006501 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006502 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6503 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006504 .addImm(NumLPads & 0xFFFF));
6505
6506 unsigned VReg2 = VReg1;
6507 if ((NumLPads & 0xFFFF0000) != 0) {
6508 VReg2 = MRI->createVirtualRegister(TRC);
6509 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6510 .addReg(VReg1)
6511 .addImm(NumLPads >> 16));
6512 }
6513
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006514 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6515 .addReg(NewVReg1)
6516 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006517 } else {
6518 MachineConstantPool *ConstantPool = MF->getConstantPool();
6519 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6520 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6521
6522 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006523 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006524 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006525 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006526 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6527
6528 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6529 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6530 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006531 .addConstantPoolIndex(Idx)
6532 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006533 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6534 .addReg(NewVReg1)
6535 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006536 }
6537
Bill Wendling95ce2e92011-10-06 22:53:00 +00006538 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6539 .addMBB(TrapBB)
6540 .addImm(ARMCC::HI)
6541 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006542
Bill Wendling564392b2011-10-18 22:11:18 +00006543 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006544 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006545 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006546 .addReg(NewVReg1)
6547 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006548 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6549 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006550 .addJumpTableIndex(MJTI)
6551 .addImm(UId));
6552
6553 MachineMemOperand *JTMMOLd =
6554 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6555 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006556 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006557 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006558 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6559 .addReg(NewVReg3, RegState::Kill)
6560 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006561 .addImm(0)
6562 .addMemOperand(JTMMOLd));
6563
6564 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006565 .addReg(NewVReg5, RegState::Kill)
6566 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006567 .addJumpTableIndex(MJTI)
6568 .addImm(UId);
6569 }
Bill Wendling2a850152011-10-05 00:02:33 +00006570
Bill Wendlingbb734682011-10-05 00:39:32 +00006571 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006572 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006573 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006574 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6575 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006576 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006577 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006578 }
6579
Bill Wendling24bb9252011-10-17 05:25:09 +00006580 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006581 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006582 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006583 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6584 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6585 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006586
6587 // Remove the landing pad successor from the invoke block and replace it
6588 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006589 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6590 BB->succ_end());
6591 while (!Successors.empty()) {
6592 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006593 if (SMBB->isLandingPad()) {
6594 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006595 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006596 }
6597 }
6598
6599 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006600
6601 // Find the invoke call and mark all of the callee-saved registers as
6602 // 'implicit defined' so that they're spilled. This prevents code from
6603 // moving instructions to before the EH block, where they will never be
6604 // executed.
6605 for (MachineBasicBlock::reverse_iterator
6606 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006607 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006608
6609 DenseMap<unsigned, bool> DefRegs;
6610 for (MachineInstr::mop_iterator
6611 OI = II->operands_begin(), OE = II->operands_end();
6612 OI != OE; ++OI) {
6613 if (!OI->isReg()) continue;
6614 DefRegs[OI->getReg()] = true;
6615 }
6616
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00006617 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006618
Bill Wendling5d798592011-10-14 23:55:44 +00006619 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006620 unsigned Reg = SavedRegs[i];
6621 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006622 !ARM::tGPRRegClass.contains(Reg) &&
6623 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006624 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006625 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006626 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006627 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006628 continue;
6629 if (!DefRegs[Reg])
6630 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006631 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006632
6633 break;
6634 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006635 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006636
Bill Wendlingf7b02072011-10-18 18:30:49 +00006637 // Mark all former landing pads as non-landing pads. The dispatch is the only
6638 // landing pad now.
6639 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6640 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6641 (*I)->setIsLandingPad(false);
6642
Bill Wendlingbb734682011-10-05 00:39:32 +00006643 // The instruction is gone now.
6644 MI->eraseFromParent();
6645
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006646 return MBB;
6647}
6648
Evan Cheng218977b2010-07-13 19:27:42 +00006649static
6650MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6651 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6652 E = MBB->succ_end(); I != E; ++I)
6653 if (*I != Succ)
6654 return *I;
6655 llvm_unreachable("Expecting a BB with two successors!");
6656}
6657
Manman Ren68f25572012-06-01 19:33:18 +00006658MachineBasicBlock *ARMTargetLowering::
6659EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6660 // This pseudo instruction has 3 operands: dst, src, size
6661 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6662 // Otherwise, we will generate unrolled scalar copies.
6663 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6664 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6665 MachineFunction::iterator It = BB;
6666 ++It;
6667
6668 unsigned dest = MI->getOperand(0).getReg();
6669 unsigned src = MI->getOperand(1).getReg();
6670 unsigned SizeVal = MI->getOperand(2).getImm();
6671 unsigned Align = MI->getOperand(3).getImm();
6672 DebugLoc dl = MI->getDebugLoc();
6673
6674 bool isThumb2 = Subtarget->isThumb2();
6675 MachineFunction *MF = BB->getParent();
6676 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006677 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006678
6679 const TargetRegisterClass *TRC = isThumb2 ?
6680 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6681 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006682 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006683
6684 if (Align & 1) {
6685 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6686 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6687 UnitSize = 1;
6688 } else if (Align & 2) {
6689 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6690 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6691 UnitSize = 2;
6692 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006693 // Check whether we can use NEON instructions.
Bill Wendling831737d2012-12-30 10:32:01 +00006694 if (!MF->getFunction()->getAttributes().
6695 hasAttribute(AttributeSet::FunctionIndex,
6696 Attribute::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006697 Subtarget->hasNEON()) {
6698 if ((Align % 16 == 0) && SizeVal >= 16) {
6699 ldrOpc = ARM::VLD1q32wb_fixed;
6700 strOpc = ARM::VST1q32wb_fixed;
6701 UnitSize = 16;
6702 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6703 }
6704 else if ((Align % 8 == 0) && SizeVal >= 8) {
6705 ldrOpc = ARM::VLD1d32wb_fixed;
6706 strOpc = ARM::VST1d32wb_fixed;
6707 UnitSize = 8;
6708 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6709 }
6710 }
6711 // Can't use NEON instructions.
6712 if (UnitSize == 0) {
6713 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6714 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6715 UnitSize = 4;
6716 }
Manman Ren68f25572012-06-01 19:33:18 +00006717 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006718
Manman Ren68f25572012-06-01 19:33:18 +00006719 unsigned BytesLeft = SizeVal % UnitSize;
6720 unsigned LoopSize = SizeVal - BytesLeft;
6721
6722 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6723 // Use LDR and STR to copy.
6724 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6725 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6726 unsigned srcIn = src;
6727 unsigned destIn = dest;
6728 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006729 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006730 unsigned srcOut = MRI.createVirtualRegister(TRC);
6731 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006732 if (UnitSize >= 8) {
6733 AddDefaultPred(BuildMI(*BB, MI, dl,
6734 TII->get(ldrOpc), scratch)
6735 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6736
6737 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6738 .addReg(destIn).addImm(0).addReg(scratch));
6739 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006740 AddDefaultPred(BuildMI(*BB, MI, dl,
6741 TII->get(ldrOpc), scratch)
6742 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6743
6744 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6745 .addReg(scratch).addReg(destIn)
6746 .addImm(UnitSize));
6747 } else {
6748 AddDefaultPred(BuildMI(*BB, MI, dl,
6749 TII->get(ldrOpc), scratch)
6750 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6751 .addImm(UnitSize));
6752
6753 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6754 .addReg(scratch).addReg(destIn)
6755 .addReg(0).addImm(UnitSize));
6756 }
6757 srcIn = srcOut;
6758 destIn = destOut;
6759 }
6760
6761 // Handle the leftover bytes with LDRB and STRB.
6762 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6763 // [destOut] = STRB_POST(scratch, destIn, 1)
6764 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6765 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6766 for (unsigned i = 0; i < BytesLeft; i++) {
6767 unsigned scratch = MRI.createVirtualRegister(TRC);
6768 unsigned srcOut = MRI.createVirtualRegister(TRC);
6769 unsigned destOut = MRI.createVirtualRegister(TRC);
6770 if (isThumb2) {
6771 AddDefaultPred(BuildMI(*BB, MI, dl,
6772 TII->get(ldrOpc),scratch)
6773 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6774
6775 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6776 .addReg(scratch).addReg(destIn)
6777 .addReg(0).addImm(1));
6778 } else {
6779 AddDefaultPred(BuildMI(*BB, MI, dl,
6780 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006781 .addReg(srcOut, RegState::Define).addReg(srcIn)
6782 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006783
6784 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6785 .addReg(scratch).addReg(destIn)
6786 .addReg(0).addImm(1));
6787 }
6788 srcIn = srcOut;
6789 destIn = destOut;
6790 }
6791 MI->eraseFromParent(); // The instruction is gone now.
6792 return BB;
6793 }
6794
6795 // Expand the pseudo op to a loop.
6796 // thisMBB:
6797 // ...
6798 // movw varEnd, # --> with thumb2
6799 // movt varEnd, #
6800 // ldrcp varEnd, idx --> without thumb2
6801 // fallthrough --> loopMBB
6802 // loopMBB:
6803 // PHI varPhi, varEnd, varLoop
6804 // PHI srcPhi, src, srcLoop
6805 // PHI destPhi, dst, destLoop
6806 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6807 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6808 // subs varLoop, varPhi, #UnitSize
6809 // bne loopMBB
6810 // fallthrough --> exitMBB
6811 // exitMBB:
6812 // epilogue to handle left-over bytes
6813 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6814 // [destOut] = STRB_POST(scratch, destLoop, 1)
6815 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6816 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6817 MF->insert(It, loopMBB);
6818 MF->insert(It, exitMBB);
6819
6820 // Transfer the remainder of BB and its successor edges to exitMBB.
6821 exitMBB->splice(exitMBB->begin(), BB,
6822 llvm::next(MachineBasicBlock::iterator(MI)),
6823 BB->end());
6824 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6825
6826 // Load an immediate to varEnd.
6827 unsigned varEnd = MRI.createVirtualRegister(TRC);
6828 if (isThumb2) {
6829 unsigned VReg1 = varEnd;
6830 if ((LoopSize & 0xFFFF0000) != 0)
6831 VReg1 = MRI.createVirtualRegister(TRC);
6832 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6833 .addImm(LoopSize & 0xFFFF));
6834
6835 if ((LoopSize & 0xFFFF0000) != 0)
6836 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6837 .addReg(VReg1)
6838 .addImm(LoopSize >> 16));
6839 } else {
6840 MachineConstantPool *ConstantPool = MF->getConstantPool();
6841 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6842 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6843
6844 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006845 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006846 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006847 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006848 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6849
6850 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6851 .addReg(varEnd, RegState::Define)
6852 .addConstantPoolIndex(Idx)
6853 .addImm(0));
6854 }
6855 BB->addSuccessor(loopMBB);
6856
6857 // Generate the loop body:
6858 // varPhi = PHI(varLoop, varEnd)
6859 // srcPhi = PHI(srcLoop, src)
6860 // destPhi = PHI(destLoop, dst)
6861 MachineBasicBlock *entryBB = BB;
6862 BB = loopMBB;
6863 unsigned varLoop = MRI.createVirtualRegister(TRC);
6864 unsigned varPhi = MRI.createVirtualRegister(TRC);
6865 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6866 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6867 unsigned destLoop = MRI.createVirtualRegister(TRC);
6868 unsigned destPhi = MRI.createVirtualRegister(TRC);
6869
6870 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6871 .addReg(varLoop).addMBB(loopMBB)
6872 .addReg(varEnd).addMBB(entryBB);
6873 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6874 .addReg(srcLoop).addMBB(loopMBB)
6875 .addReg(src).addMBB(entryBB);
6876 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6877 .addReg(destLoop).addMBB(loopMBB)
6878 .addReg(dest).addMBB(entryBB);
6879
6880 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6881 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006882 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6883 if (UnitSize >= 8) {
6884 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6885 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6886
6887 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6888 .addReg(destPhi).addImm(0).addReg(scratch));
6889 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006890 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6891 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6892
6893 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6894 .addReg(scratch).addReg(destPhi)
6895 .addImm(UnitSize));
6896 } else {
6897 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6898 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6899 .addImm(UnitSize));
6900
6901 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6902 .addReg(scratch).addReg(destPhi)
6903 .addReg(0).addImm(UnitSize));
6904 }
6905
6906 // Decrement loop variable by UnitSize.
6907 MachineInstrBuilder MIB = BuildMI(BB, dl,
6908 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6909 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6910 MIB->getOperand(5).setReg(ARM::CPSR);
6911 MIB->getOperand(5).setIsDef(true);
6912
6913 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6914 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6915
6916 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6917 BB->addSuccessor(loopMBB);
6918 BB->addSuccessor(exitMBB);
6919
6920 // Add epilogue to handle BytesLeft.
6921 BB = exitMBB;
6922 MachineInstr *StartOfExit = exitMBB->begin();
6923 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6924 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6925
6926 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6927 // [destOut] = STRB_POST(scratch, destLoop, 1)
6928 unsigned srcIn = srcLoop;
6929 unsigned destIn = destLoop;
6930 for (unsigned i = 0; i < BytesLeft; i++) {
6931 unsigned scratch = MRI.createVirtualRegister(TRC);
6932 unsigned srcOut = MRI.createVirtualRegister(TRC);
6933 unsigned destOut = MRI.createVirtualRegister(TRC);
6934 if (isThumb2) {
6935 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6936 TII->get(ldrOpc),scratch)
6937 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6938
6939 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6940 .addReg(scratch).addReg(destIn)
6941 .addImm(1));
6942 } else {
6943 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6944 TII->get(ldrOpc),scratch)
6945 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6946
6947 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6948 .addReg(scratch).addReg(destIn)
6949 .addReg(0).addImm(1));
6950 }
6951 srcIn = srcOut;
6952 destIn = destOut;
6953 }
6954
6955 MI->eraseFromParent(); // The instruction is gone now.
6956 return BB;
6957}
6958
Jim Grosbache801dc42009-12-12 01:40:06 +00006959MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006960ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006961 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006962 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006963 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006964 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006965 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006966 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006967 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006968 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006969 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006970 // The Thumb2 pre-indexed stores have the same MI operands, they just
6971 // define them differently in the .td files from the isel patterns, so
6972 // they need pseudos.
6973 case ARM::t2STR_preidx:
6974 MI->setDesc(TII->get(ARM::t2STR_PRE));
6975 return BB;
6976 case ARM::t2STRB_preidx:
6977 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6978 return BB;
6979 case ARM::t2STRH_preidx:
6980 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6981 return BB;
6982
Jim Grosbach19dec202011-08-05 20:35:44 +00006983 case ARM::STRi_preidx:
6984 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006985 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006986 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6987 // Decode the offset.
6988 unsigned Offset = MI->getOperand(4).getImm();
6989 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6990 Offset = ARM_AM::getAM2Offset(Offset);
6991 if (isSub)
6992 Offset = -Offset;
6993
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006994 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006995 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006996 .addOperand(MI->getOperand(0)) // Rn_wb
6997 .addOperand(MI->getOperand(1)) // Rt
6998 .addOperand(MI->getOperand(2)) // Rn
6999 .addImm(Offset) // offset (skip GPR==zero_reg)
7000 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00007001 .addOperand(MI->getOperand(6))
7002 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00007003 MI->eraseFromParent();
7004 return BB;
7005 }
7006 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00007007 case ARM::STRBr_preidx:
7008 case ARM::STRH_preidx: {
7009 unsigned NewOpc;
7010 switch (MI->getOpcode()) {
7011 default: llvm_unreachable("unexpected opcode!");
7012 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7013 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7014 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7015 }
Jim Grosbach19dec202011-08-05 20:35:44 +00007016 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7017 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7018 MIB.addOperand(MI->getOperand(i));
7019 MI->eraseFromParent();
7020 return BB;
7021 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007022 case ARM::ATOMIC_LOAD_ADD_I8:
7023 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7024 case ARM::ATOMIC_LOAD_ADD_I16:
7025 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7026 case ARM::ATOMIC_LOAD_ADD_I32:
7027 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007028
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007029 case ARM::ATOMIC_LOAD_AND_I8:
7030 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7031 case ARM::ATOMIC_LOAD_AND_I16:
7032 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7033 case ARM::ATOMIC_LOAD_AND_I32:
7034 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007035
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007036 case ARM::ATOMIC_LOAD_OR_I8:
7037 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7038 case ARM::ATOMIC_LOAD_OR_I16:
7039 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7040 case ARM::ATOMIC_LOAD_OR_I32:
7041 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007042
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007043 case ARM::ATOMIC_LOAD_XOR_I8:
7044 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7045 case ARM::ATOMIC_LOAD_XOR_I16:
7046 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7047 case ARM::ATOMIC_LOAD_XOR_I32:
7048 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007049
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007050 case ARM::ATOMIC_LOAD_NAND_I8:
7051 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7052 case ARM::ATOMIC_LOAD_NAND_I16:
7053 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7054 case ARM::ATOMIC_LOAD_NAND_I32:
7055 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007056
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007057 case ARM::ATOMIC_LOAD_SUB_I8:
7058 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7059 case ARM::ATOMIC_LOAD_SUB_I16:
7060 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7061 case ARM::ATOMIC_LOAD_SUB_I32:
7062 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007063
Jim Grosbachf7da8822011-04-26 19:44:18 +00007064 case ARM::ATOMIC_LOAD_MIN_I8:
7065 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7066 case ARM::ATOMIC_LOAD_MIN_I16:
7067 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7068 case ARM::ATOMIC_LOAD_MIN_I32:
7069 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7070
7071 case ARM::ATOMIC_LOAD_MAX_I8:
7072 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7073 case ARM::ATOMIC_LOAD_MAX_I16:
7074 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7075 case ARM::ATOMIC_LOAD_MAX_I32:
7076 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7077
7078 case ARM::ATOMIC_LOAD_UMIN_I8:
7079 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7080 case ARM::ATOMIC_LOAD_UMIN_I16:
7081 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7082 case ARM::ATOMIC_LOAD_UMIN_I32:
7083 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7084
7085 case ARM::ATOMIC_LOAD_UMAX_I8:
7086 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7087 case ARM::ATOMIC_LOAD_UMAX_I16:
7088 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7089 case ARM::ATOMIC_LOAD_UMAX_I32:
7090 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7091
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007092 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7093 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7094 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00007095
7096 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7097 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7098 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007099
Eli Friedman2bdffe42011-08-31 00:31:29 +00007100
7101 case ARM::ATOMADD6432:
7102 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007103 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7104 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007105 case ARM::ATOMSUB6432:
7106 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007107 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7108 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007109 case ARM::ATOMOR6432:
7110 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007111 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007112 case ARM::ATOMXOR6432:
7113 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007114 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007115 case ARM::ATOMAND6432:
7116 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007117 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007118 case ARM::ATOMSWAP6432:
7119 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00007120 case ARM::ATOMCMPXCHG6432:
7121 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7122 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7123 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007124 case ARM::ATOMMIN6432:
7125 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7126 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7127 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007128 /*IsMinMax*/ true, ARMCC::LT);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007129 case ARM::ATOMMAX6432:
7130 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7131 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7132 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7133 /*IsMinMax*/ true, ARMCC::GE);
7134 case ARM::ATOMUMIN6432:
7135 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7136 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7137 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007138 /*IsMinMax*/ true, ARMCC::LO);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007139 case ARM::ATOMUMAX6432:
7140 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7141 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7142 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7143 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007144
Evan Cheng007ea272009-08-12 05:17:19 +00007145 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00007146 // To "insert" a SELECT_CC instruction, we actually have to insert the
7147 // diamond control-flow pattern. The incoming instruction knows the
7148 // destination vreg to set, the condition code register to branch on, the
7149 // true/false values to select between, and a branch opcode to use.
7150 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007151 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00007152 ++It;
7153
7154 // thisMBB:
7155 // ...
7156 // TrueVal = ...
7157 // cmpTY ccX, r1, r2
7158 // bCC copy1MBB
7159 // fallthrough --> copy0MBB
7160 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007161 MachineFunction *F = BB->getParent();
7162 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7163 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00007164 F->insert(It, copy0MBB);
7165 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00007166
7167 // Transfer the remainder of BB and its successor edges to sinkMBB.
7168 sinkMBB->splice(sinkMBB->begin(), BB,
7169 llvm::next(MachineBasicBlock::iterator(MI)),
7170 BB->end());
7171 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7172
Dan Gohman258c58c2010-07-06 15:49:48 +00007173 BB->addSuccessor(copy0MBB);
7174 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00007175
Dan Gohman14152b42010-07-06 20:24:04 +00007176 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7177 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7178
Evan Chenga8e29892007-01-19 07:51:42 +00007179 // copy0MBB:
7180 // %FalseValue = ...
7181 // # fallthrough to sinkMBB
7182 BB = copy0MBB;
7183
7184 // Update machine-CFG edges
7185 BB->addSuccessor(sinkMBB);
7186
7187 // sinkMBB:
7188 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7189 // ...
7190 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007191 BuildMI(*BB, BB->begin(), dl,
7192 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007193 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7194 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7195
Dan Gohman14152b42010-07-06 20:24:04 +00007196 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007197 return BB;
7198 }
Evan Cheng86198642009-08-07 00:34:42 +00007199
Evan Cheng218977b2010-07-13 19:27:42 +00007200 case ARM::BCCi64:
7201 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007202 // If there is an unconditional branch to the other successor, remove it.
7203 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007204
Evan Cheng218977b2010-07-13 19:27:42 +00007205 // Compare both parts that make up the double comparison separately for
7206 // equality.
7207 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7208
7209 unsigned LHS1 = MI->getOperand(1).getReg();
7210 unsigned LHS2 = MI->getOperand(2).getReg();
7211 if (RHSisZero) {
7212 AddDefaultPred(BuildMI(BB, dl,
7213 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7214 .addReg(LHS1).addImm(0));
7215 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7216 .addReg(LHS2).addImm(0)
7217 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7218 } else {
7219 unsigned RHS1 = MI->getOperand(3).getReg();
7220 unsigned RHS2 = MI->getOperand(4).getReg();
7221 AddDefaultPred(BuildMI(BB, dl,
7222 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7223 .addReg(LHS1).addReg(RHS1));
7224 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7225 .addReg(LHS2).addReg(RHS2)
7226 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7227 }
7228
7229 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7230 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7231 if (MI->getOperand(0).getImm() == ARMCC::NE)
7232 std::swap(destMBB, exitMBB);
7233
7234 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7235 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007236 if (isThumb2)
7237 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7238 else
7239 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007240
7241 MI->eraseFromParent(); // The pseudo instruction is gone now.
7242 return BB;
7243 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007244
Bill Wendling5bc85282011-10-17 20:37:20 +00007245 case ARM::Int_eh_sjlj_setjmp:
7246 case ARM::Int_eh_sjlj_setjmp_nofp:
7247 case ARM::tInt_eh_sjlj_setjmp:
7248 case ARM::t2Int_eh_sjlj_setjmp:
7249 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7250 EmitSjLjDispatchBlock(MI, BB);
7251 return BB;
7252
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007253 case ARM::ABS:
7254 case ARM::t2ABS: {
7255 // To insert an ABS instruction, we have to insert the
7256 // diamond control-flow pattern. The incoming instruction knows the
7257 // source vreg to test against 0, the destination vreg to set,
7258 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007259 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007260 // It transforms
7261 // V1 = ABS V0
7262 // into
7263 // V2 = MOVS V0
7264 // BCC (branch to SinkBB if V0 >= 0)
7265 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007266 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007267 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7268 MachineFunction::iterator BBI = BB;
7269 ++BBI;
7270 MachineFunction *Fn = BB->getParent();
7271 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7272 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7273 Fn->insert(BBI, RSBBB);
7274 Fn->insert(BBI, SinkBB);
7275
7276 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7277 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7278 bool isThumb2 = Subtarget->isThumb2();
7279 MachineRegisterInfo &MRI = Fn->getRegInfo();
7280 // In Thumb mode S must not be specified if source register is the SP or
7281 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007282 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7283 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7284 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007285
7286 // Transfer the remainder of BB and its successor edges to sinkMBB.
7287 SinkBB->splice(SinkBB->begin(), BB,
7288 llvm::next(MachineBasicBlock::iterator(MI)),
7289 BB->end());
7290 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7291
7292 BB->addSuccessor(RSBBB);
7293 BB->addSuccessor(SinkBB);
7294
7295 // fall through to SinkMBB
7296 RSBBB->addSuccessor(SinkBB);
7297
Manman Ren307473d2012-06-15 21:32:12 +00007298 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007299 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007300 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7301 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007302
7303 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007304 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007305 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7306 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7307
7308 // insert rsbri in RSBBB
7309 // Note: BCC and rsbri will be converted into predicated rsbmi
7310 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007311 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007312 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007313 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007314 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7315
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007316 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007317 // reuse ABSDstReg to not change uses of ABS instruction
7318 BuildMI(*SinkBB, SinkBB->begin(), dl,
7319 TII->get(ARM::PHI), ABSDstReg)
7320 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007321 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007322
7323 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007324 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007325
7326 // return last added BB
7327 return SinkBB;
7328 }
Manman Ren68f25572012-06-01 19:33:18 +00007329 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007330 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007331 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007332 }
7333}
7334
Evan Cheng37fefc22011-08-30 19:09:48 +00007335void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7336 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007337 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007338 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7339 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7340 return;
7341 }
7342
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007343 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007344 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7345 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7346 // operand is still set to noreg. If needed, set the optional operand's
7347 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007348 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007349 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007350
Andrew Trick3be654f2011-09-21 02:20:46 +00007351 // Rename pseudo opcodes.
7352 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7353 if (NewOpc) {
7354 const ARMBaseInstrInfo *TII =
7355 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007356 MCID = &TII->get(NewOpc);
7357
7358 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7359 "converted opcode should be the same except for cc_out");
7360
7361 MI->setDesc(*MCID);
7362
7363 // Add the optional cc_out operand
7364 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007365 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007366 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007367
7368 // Any ARM instruction that sets the 's' bit should specify an optional
7369 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007370 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007371 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007372 return;
7373 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007374 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7375 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007376 bool definesCPSR = false;
7377 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007378 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007379 i != e; ++i) {
7380 const MachineOperand &MO = MI->getOperand(i);
7381 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7382 definesCPSR = true;
7383 if (MO.isDead())
7384 deadCPSR = true;
7385 MI->RemoveOperand(i);
7386 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007387 }
7388 }
Andrew Trick4815d562011-09-20 03:17:40 +00007389 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007390 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007391 return;
7392 }
7393 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007394 if (deadCPSR) {
7395 assert(!MI->getOperand(ccOutIdx).getReg() &&
7396 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007397 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007398 }
Andrew Trick4815d562011-09-20 03:17:40 +00007399
Andrew Trick3be654f2011-09-21 02:20:46 +00007400 // If this instruction was defined with an optional CPSR def and its dag node
7401 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007402 MachineOperand &MO = MI->getOperand(ccOutIdx);
7403 MO.setReg(ARM::CPSR);
7404 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007405}
7406
Evan Chenga8e29892007-01-19 07:51:42 +00007407//===----------------------------------------------------------------------===//
7408// ARM Optimization Hooks
7409//===----------------------------------------------------------------------===//
7410
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007411// Helper function that checks if N is a null or all ones constant.
7412static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7413 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7414 if (!C)
7415 return false;
7416 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7417}
7418
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007419// Return true if N is conditionally 0 or all ones.
7420// Detects these expressions where cc is an i1 value:
7421//
7422// (select cc 0, y) [AllOnes=0]
7423// (select cc y, 0) [AllOnes=0]
7424// (zext cc) [AllOnes=0]
7425// (sext cc) [AllOnes=0/1]
7426// (select cc -1, y) [AllOnes=1]
7427// (select cc y, -1) [AllOnes=1]
7428//
7429// Invert is set when N is the null/all ones constant when CC is false.
7430// OtherOp is set to the alternative value of N.
7431static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7432 SDValue &CC, bool &Invert,
7433 SDValue &OtherOp,
7434 SelectionDAG &DAG) {
7435 switch (N->getOpcode()) {
7436 default: return false;
7437 case ISD::SELECT: {
7438 CC = N->getOperand(0);
7439 SDValue N1 = N->getOperand(1);
7440 SDValue N2 = N->getOperand(2);
7441 if (isZeroOrAllOnes(N1, AllOnes)) {
7442 Invert = false;
7443 OtherOp = N2;
7444 return true;
7445 }
7446 if (isZeroOrAllOnes(N2, AllOnes)) {
7447 Invert = true;
7448 OtherOp = N1;
7449 return true;
7450 }
7451 return false;
7452 }
7453 case ISD::ZERO_EXTEND:
7454 // (zext cc) can never be the all ones value.
7455 if (AllOnes)
7456 return false;
7457 // Fall through.
7458 case ISD::SIGN_EXTEND: {
7459 EVT VT = N->getValueType(0);
7460 CC = N->getOperand(0);
7461 if (CC.getValueType() != MVT::i1)
7462 return false;
7463 Invert = !AllOnes;
7464 if (AllOnes)
7465 // When looking for an AllOnes constant, N is an sext, and the 'other'
7466 // value is 0.
7467 OtherOp = DAG.getConstant(0, VT);
7468 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7469 // When looking for a 0 constant, N can be zext or sext.
7470 OtherOp = DAG.getConstant(1, VT);
7471 else
7472 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7473 return true;
7474 }
7475 }
7476}
7477
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007478// Combine a constant select operand into its use:
7479//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007480// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7481// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7482// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7483// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7484// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007485//
7486// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007487// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007488//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007489// Also recognize sext/zext from i1:
7490//
7491// (add (zext cc), x) -> (select cc (add x, 1), x)
7492// (add (sext cc), x) -> (select cc (add x, -1), x)
7493//
7494// These transformations eventually create predicated instructions.
7495//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007496// @param N The node to transform.
7497// @param Slct The N operand that is a select.
7498// @param OtherOp The other N operand (x above).
7499// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007500// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007501// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007502static
7503SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007504 TargetLowering::DAGCombinerInfo &DCI,
7505 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007506 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007507 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007508 SDValue NonConstantVal;
7509 SDValue CCOp;
7510 bool SwapSelectOps;
7511 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7512 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007513 return SDValue();
7514
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007515 // Slct is now know to be the desired identity constant when CC is true.
7516 SDValue TrueVal = OtherOp;
7517 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7518 OtherOp, NonConstantVal);
7519 // Unless SwapSelectOps says CC should be false.
7520 if (SwapSelectOps)
7521 std::swap(TrueVal, FalseVal);
7522
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007523 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007524 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007525}
7526
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007527// Attempt combineSelectAndUse on each operand of a commutative operator N.
7528static
7529SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7530 TargetLowering::DAGCombinerInfo &DCI) {
7531 SDValue N0 = N->getOperand(0);
7532 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007533 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007534 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7535 if (Result.getNode())
7536 return Result;
7537 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007538 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007539 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7540 if (Result.getNode())
7541 return Result;
7542 }
7543 return SDValue();
7544}
7545
Eric Christopherfa6f5912011-06-29 21:10:36 +00007546// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007547// (only after legalization).
7548static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7549 TargetLowering::DAGCombinerInfo &DCI,
7550 const ARMSubtarget *Subtarget) {
7551
7552 // Only perform optimization if after legalize, and if NEON is available. We
7553 // also expected both operands to be BUILD_VECTORs.
7554 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7555 || N0.getOpcode() != ISD::BUILD_VECTOR
7556 || N1.getOpcode() != ISD::BUILD_VECTOR)
7557 return SDValue();
7558
7559 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7560 EVT VT = N->getValueType(0);
7561 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7562 return SDValue();
7563
7564 // Check that the vector operands are of the right form.
7565 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7566 // operands, where N is the size of the formed vector.
7567 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7568 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007569
7570 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007571 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007572 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007573 SDValue Vec = N0->getOperand(0)->getOperand(0);
7574 SDNode *V = Vec.getNode();
7575 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007576
Eric Christopherfa6f5912011-06-29 21:10:36 +00007577 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007578 // check to see if each of their operands are an EXTRACT_VECTOR with
7579 // the same vector and appropriate index.
7580 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7581 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7582 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007583
Tanya Lattner189531f2011-06-14 23:48:48 +00007584 SDValue ExtVec0 = N0->getOperand(i);
7585 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007586
Tanya Lattner189531f2011-06-14 23:48:48 +00007587 // First operand is the vector, verify its the same.
7588 if (V != ExtVec0->getOperand(0).getNode() ||
7589 V != ExtVec1->getOperand(0).getNode())
7590 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007591
Tanya Lattner189531f2011-06-14 23:48:48 +00007592 // Second is the constant, verify its correct.
7593 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7594 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007595
Tanya Lattner189531f2011-06-14 23:48:48 +00007596 // For the constant, we want to see all the even or all the odd.
7597 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7598 || C1->getZExtValue() != nextIndex+1)
7599 return SDValue();
7600
7601 // Increment index.
7602 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007603 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007604 return SDValue();
7605 }
7606
7607 // Create VPADDL node.
7608 SelectionDAG &DAG = DCI.DAG;
7609 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007610
7611 // Build operand list.
7612 SmallVector<SDValue, 8> Ops;
7613 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7614 TLI.getPointerTy()));
7615
7616 // Input is the vector.
7617 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007618
Tanya Lattner189531f2011-06-14 23:48:48 +00007619 // Get widened type and narrowed type.
7620 MVT widenType;
7621 unsigned numElem = VT.getVectorNumElements();
7622 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7623 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7624 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7625 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7626 default:
Craig Topperbc219812012-02-07 02:50:20 +00007627 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007628 }
7629
7630 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7631 widenType, &Ops[0], Ops.size());
7632 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7633}
7634
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007635static SDValue findMUL_LOHI(SDValue V) {
7636 if (V->getOpcode() == ISD::UMUL_LOHI ||
7637 V->getOpcode() == ISD::SMUL_LOHI)
7638 return V;
7639 return SDValue();
7640}
7641
7642static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7643 TargetLowering::DAGCombinerInfo &DCI,
7644 const ARMSubtarget *Subtarget) {
7645
7646 if (Subtarget->isThumb1Only()) return SDValue();
7647
7648 // Only perform the checks after legalize when the pattern is available.
7649 if (DCI.isBeforeLegalize()) return SDValue();
7650
7651 // Look for multiply add opportunities.
7652 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7653 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7654 // a glue link from the first add to the second add.
7655 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7656 // a S/UMLAL instruction.
7657 // loAdd UMUL_LOHI
7658 // \ / :lo \ :hi
7659 // \ / \ [no multiline comment]
7660 // ADDC | hiAdd
7661 // \ :glue / /
7662 // \ / /
7663 // ADDE
7664 //
7665 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7666 SDValue AddcOp0 = AddcNode->getOperand(0);
7667 SDValue AddcOp1 = AddcNode->getOperand(1);
7668
7669 // Check if the two operands are from the same mul_lohi node.
7670 if (AddcOp0.getNode() == AddcOp1.getNode())
7671 return SDValue();
7672
7673 assert(AddcNode->getNumValues() == 2 &&
7674 AddcNode->getValueType(0) == MVT::i32 &&
7675 AddcNode->getValueType(1) == MVT::Glue &&
7676 "Expect ADDC with two result values: i32, glue");
7677
7678 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7679 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7680 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7681 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7682 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7683 return SDValue();
7684
7685 // Look for the glued ADDE.
7686 SDNode* AddeNode = AddcNode->getGluedUser();
7687 if (AddeNode == NULL)
7688 return SDValue();
7689
7690 // Make sure it is really an ADDE.
7691 if (AddeNode->getOpcode() != ISD::ADDE)
7692 return SDValue();
7693
7694 assert(AddeNode->getNumOperands() == 3 &&
7695 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7696 "ADDE node has the wrong inputs");
7697
7698 // Check for the triangle shape.
7699 SDValue AddeOp0 = AddeNode->getOperand(0);
7700 SDValue AddeOp1 = AddeNode->getOperand(1);
7701
7702 // Make sure that the ADDE operands are not coming from the same node.
7703 if (AddeOp0.getNode() == AddeOp1.getNode())
7704 return SDValue();
7705
7706 // Find the MUL_LOHI node walking up ADDE's operands.
7707 bool IsLeftOperandMUL = false;
7708 SDValue MULOp = findMUL_LOHI(AddeOp0);
7709 if (MULOp == SDValue())
7710 MULOp = findMUL_LOHI(AddeOp1);
7711 else
7712 IsLeftOperandMUL = true;
7713 if (MULOp == SDValue())
7714 return SDValue();
7715
7716 // Figure out the right opcode.
7717 unsigned Opc = MULOp->getOpcode();
7718 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7719
7720 // Figure out the high and low input values to the MLAL node.
7721 SDValue* HiMul = &MULOp;
7722 SDValue* HiAdd = NULL;
7723 SDValue* LoMul = NULL;
7724 SDValue* LowAdd = NULL;
7725
7726 if (IsLeftOperandMUL)
7727 HiAdd = &AddeOp1;
7728 else
7729 HiAdd = &AddeOp0;
7730
7731
7732 if (AddcOp0->getOpcode() == Opc) {
7733 LoMul = &AddcOp0;
7734 LowAdd = &AddcOp1;
7735 }
7736 if (AddcOp1->getOpcode() == Opc) {
7737 LoMul = &AddcOp1;
7738 LowAdd = &AddcOp0;
7739 }
7740
7741 if (LoMul == NULL)
7742 return SDValue();
7743
7744 if (LoMul->getNode() != HiMul->getNode())
7745 return SDValue();
7746
7747 // Create the merged node.
7748 SelectionDAG &DAG = DCI.DAG;
7749
7750 // Build operand list.
7751 SmallVector<SDValue, 8> Ops;
7752 Ops.push_back(LoMul->getOperand(0));
7753 Ops.push_back(LoMul->getOperand(1));
7754 Ops.push_back(*LowAdd);
7755 Ops.push_back(*HiAdd);
7756
7757 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7758 DAG.getVTList(MVT::i32, MVT::i32),
7759 &Ops[0], Ops.size());
7760
7761 // Replace the ADDs' nodes uses by the MLA node's values.
7762 SDValue HiMLALResult(MLALNode.getNode(), 1);
7763 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7764
7765 SDValue LoMLALResult(MLALNode.getNode(), 0);
7766 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7767
7768 // Return original node to notify the driver to stop replacing.
7769 SDValue resNode(AddcNode, 0);
7770 return resNode;
7771}
7772
7773/// PerformADDCCombine - Target-specific dag combine transform from
7774/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7775static SDValue PerformADDCCombine(SDNode *N,
7776 TargetLowering::DAGCombinerInfo &DCI,
7777 const ARMSubtarget *Subtarget) {
7778
7779 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7780
7781}
7782
Bob Wilson3d5792a2010-07-29 20:34:14 +00007783/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7784/// operands N0 and N1. This is a helper for PerformADDCombine that is
7785/// called with the default operands, and if that fails, with commuted
7786/// operands.
7787static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007788 TargetLowering::DAGCombinerInfo &DCI,
7789 const ARMSubtarget *Subtarget){
7790
7791 // Attempt to create vpaddl for this add.
7792 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7793 if (Result.getNode())
7794 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007795
Chris Lattnerd1980a52009-03-12 06:52:53 +00007796 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007797 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007798 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7799 if (Result.getNode()) return Result;
7800 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007801 return SDValue();
7802}
7803
Bob Wilson3d5792a2010-07-29 20:34:14 +00007804/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7805///
7806static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007807 TargetLowering::DAGCombinerInfo &DCI,
7808 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007809 SDValue N0 = N->getOperand(0);
7810 SDValue N1 = N->getOperand(1);
7811
7812 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007813 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007814 if (Result.getNode())
7815 return Result;
7816
7817 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007818 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007819}
7820
Chris Lattnerd1980a52009-03-12 06:52:53 +00007821/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007822///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007823static SDValue PerformSUBCombine(SDNode *N,
7824 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007825 SDValue N0 = N->getOperand(0);
7826 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007827
Chris Lattnerd1980a52009-03-12 06:52:53 +00007828 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007829 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007830 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7831 if (Result.getNode()) return Result;
7832 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007833
Chris Lattnerd1980a52009-03-12 06:52:53 +00007834 return SDValue();
7835}
7836
Evan Cheng463d3582011-03-31 19:38:48 +00007837/// PerformVMULCombine
7838/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7839/// special multiplier accumulator forwarding.
7840/// vmul d3, d0, d2
7841/// vmla d3, d1, d2
7842/// is faster than
7843/// vadd d3, d0, d1
7844/// vmul d3, d3, d2
7845static SDValue PerformVMULCombine(SDNode *N,
7846 TargetLowering::DAGCombinerInfo &DCI,
7847 const ARMSubtarget *Subtarget) {
7848 if (!Subtarget->hasVMLxForwarding())
7849 return SDValue();
7850
7851 SelectionDAG &DAG = DCI.DAG;
7852 SDValue N0 = N->getOperand(0);
7853 SDValue N1 = N->getOperand(1);
7854 unsigned Opcode = N0.getOpcode();
7855 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7856 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007857 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007858 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7859 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7860 return SDValue();
7861 std::swap(N0, N1);
7862 }
7863
7864 EVT VT = N->getValueType(0);
7865 DebugLoc DL = N->getDebugLoc();
7866 SDValue N00 = N0->getOperand(0);
7867 SDValue N01 = N0->getOperand(1);
7868 return DAG.getNode(Opcode, DL, VT,
7869 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7870 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7871}
7872
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007873static SDValue PerformMULCombine(SDNode *N,
7874 TargetLowering::DAGCombinerInfo &DCI,
7875 const ARMSubtarget *Subtarget) {
7876 SelectionDAG &DAG = DCI.DAG;
7877
7878 if (Subtarget->isThumb1Only())
7879 return SDValue();
7880
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007881 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7882 return SDValue();
7883
7884 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007885 if (VT.is64BitVector() || VT.is128BitVector())
7886 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007887 if (VT != MVT::i32)
7888 return SDValue();
7889
7890 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7891 if (!C)
7892 return SDValue();
7893
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007894 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007895 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007896
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007897 ShiftAmt = ShiftAmt & (32 - 1);
7898 SDValue V = N->getOperand(0);
7899 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007900
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007901 SDValue Res;
7902 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007903
7904 if (MulAmt >= 0) {
7905 if (isPowerOf2_32(MulAmt - 1)) {
7906 // (mul x, 2^N + 1) => (add (shl x, N), x)
7907 Res = DAG.getNode(ISD::ADD, DL, VT,
7908 V,
7909 DAG.getNode(ISD::SHL, DL, VT,
7910 V,
7911 DAG.getConstant(Log2_32(MulAmt - 1),
7912 MVT::i32)));
7913 } else if (isPowerOf2_32(MulAmt + 1)) {
7914 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7915 Res = DAG.getNode(ISD::SUB, DL, VT,
7916 DAG.getNode(ISD::SHL, DL, VT,
7917 V,
7918 DAG.getConstant(Log2_32(MulAmt + 1),
7919 MVT::i32)),
7920 V);
7921 } else
7922 return SDValue();
7923 } else {
7924 uint64_t MulAmtAbs = -MulAmt;
7925 if (isPowerOf2_32(MulAmtAbs + 1)) {
7926 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7927 Res = DAG.getNode(ISD::SUB, DL, VT,
7928 V,
7929 DAG.getNode(ISD::SHL, DL, VT,
7930 V,
7931 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7932 MVT::i32)));
7933 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7934 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7935 Res = DAG.getNode(ISD::ADD, DL, VT,
7936 V,
7937 DAG.getNode(ISD::SHL, DL, VT,
7938 V,
7939 DAG.getConstant(Log2_32(MulAmtAbs-1),
7940 MVT::i32)));
7941 Res = DAG.getNode(ISD::SUB, DL, VT,
7942 DAG.getConstant(0, MVT::i32),Res);
7943
7944 } else
7945 return SDValue();
7946 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007947
7948 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007949 Res = DAG.getNode(ISD::SHL, DL, VT,
7950 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007951
7952 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007953 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007954 return SDValue();
7955}
7956
Owen Anderson080c0922010-11-05 19:27:46 +00007957static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007958 TargetLowering::DAGCombinerInfo &DCI,
7959 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007960
Owen Anderson080c0922010-11-05 19:27:46 +00007961 // Attempt to use immediate-form VBIC
7962 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7963 DebugLoc dl = N->getDebugLoc();
7964 EVT VT = N->getValueType(0);
7965 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007966
Tanya Lattner0433b212011-04-07 15:24:20 +00007967 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7968 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007969
Owen Anderson080c0922010-11-05 19:27:46 +00007970 APInt SplatBits, SplatUndef;
7971 unsigned SplatBitSize;
7972 bool HasAnyUndefs;
7973 if (BVN &&
7974 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7975 if (SplatBitSize <= 64) {
7976 EVT VbicVT;
7977 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7978 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007979 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007980 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007981 if (Val.getNode()) {
7982 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007983 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007984 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007985 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007986 }
7987 }
7988 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007989
Evan Chengc892aeb2012-02-23 01:19:06 +00007990 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007991 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7992 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7993 if (Result.getNode())
7994 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007995 }
7996
Owen Anderson080c0922010-11-05 19:27:46 +00007997 return SDValue();
7998}
7999
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008000/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8001static SDValue PerformORCombine(SDNode *N,
8002 TargetLowering::DAGCombinerInfo &DCI,
8003 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00008004 // Attempt to use immediate-form VORR
8005 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8006 DebugLoc dl = N->getDebugLoc();
8007 EVT VT = N->getValueType(0);
8008 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008009
Tanya Lattner0433b212011-04-07 15:24:20 +00008010 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8011 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008012
Owen Anderson60f48702010-11-03 23:15:26 +00008013 APInt SplatBits, SplatUndef;
8014 unsigned SplatBitSize;
8015 bool HasAnyUndefs;
8016 if (BVN && Subtarget->hasNEON() &&
8017 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8018 if (SplatBitSize <= 64) {
8019 EVT VorrVT;
8020 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8021 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008022 DAG, VorrVT, VT.is128BitVector(),
8023 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00008024 if (Val.getNode()) {
8025 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008026 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00008027 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008028 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00008029 }
8030 }
8031 }
8032
Evan Chengc892aeb2012-02-23 01:19:06 +00008033 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008034 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8035 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8036 if (Result.getNode())
8037 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008038 }
8039
Nadav Rotemdf832032012-08-13 18:52:44 +00008040 // The code below optimizes (or (and X, Y), Z).
8041 // The AND operand needs to have a single user to make these optimizations
8042 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008043 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00008044 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008045 return SDValue();
8046 SDValue N1 = N->getOperand(1);
8047
8048 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8049 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8050 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8051 APInt SplatUndef;
8052 unsigned SplatBitSize;
8053 bool HasAnyUndefs;
8054
8055 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8056 APInt SplatBits0;
8057 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8058 HasAnyUndefs) && !HasAnyUndefs) {
8059 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8060 APInt SplatBits1;
8061 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8062 HasAnyUndefs) && !HasAnyUndefs &&
8063 SplatBits0 == ~SplatBits1) {
8064 // Canonicalize the vector type to make instruction selection simpler.
8065 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8066 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8067 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00008068 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008069 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8070 }
8071 }
8072 }
8073
Jim Grosbach54238562010-07-17 03:30:54 +00008074 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8075 // reasonable.
8076
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008077 // BFI is only available on V6T2+
8078 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8079 return SDValue();
8080
Jim Grosbach54238562010-07-17 03:30:54 +00008081 DebugLoc DL = N->getDebugLoc();
8082 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008083 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00008084 //
8085 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008086 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008087 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008088 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008089 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00008090 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008091
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008092 if (VT != MVT::i32)
8093 return SDValue();
8094
Evan Cheng30fb13f2010-12-13 20:32:54 +00008095 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00008096
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008097 // The value and the mask need to be constants so we can verify this is
8098 // actually a bitfield set. If the mask is 0xffff, we can do better
8099 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00008100 SDValue MaskOp = N0.getOperand(1);
8101 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8102 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008103 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008104 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008105 if (Mask == 0xffff)
8106 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008107 SDValue Res;
8108 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008109 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8110 if (N1C) {
8111 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008112 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00008113 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008114
Evan Chenga9688c42010-12-11 04:11:38 +00008115 if (ARM::isBitFieldInvertedMask(Mask)) {
8116 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008117
Evan Cheng30fb13f2010-12-13 20:32:54 +00008118 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00008119 DAG.getConstant(Val, MVT::i32),
8120 DAG.getConstant(Mask, MVT::i32));
8121
8122 // Do not add new nodes to DAG combiner worklist.
8123 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008124 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008125 }
Jim Grosbach54238562010-07-17 03:30:54 +00008126 } else if (N1.getOpcode() == ISD::AND) {
8127 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008128 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8129 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00008130 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008131 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008132
Eric Christopher29aeed12011-03-26 01:21:03 +00008133 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8134 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00008135 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008136 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008137 // The pack halfword instruction works better for masks that fit it,
8138 // so use that when it's available.
8139 if (Subtarget->hasT2ExtractPack() &&
8140 (Mask == 0xffff || Mask == 0xffff0000))
8141 return SDValue();
8142 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00008143 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00008144 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00008145 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00008146 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00008147 DAG.getConstant(Mask, MVT::i32));
8148 // Do not add new nodes to DAG combiner worklist.
8149 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008150 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008151 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008152 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008153 // The pack halfword instruction works better for masks that fit it,
8154 // so use that when it's available.
8155 if (Subtarget->hasT2ExtractPack() &&
8156 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8157 return SDValue();
8158 // 2b
8159 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008160 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00008161 DAG.getConstant(lsb, MVT::i32));
8162 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00008163 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00008164 // Do not add new nodes to DAG combiner worklist.
8165 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008166 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008167 }
8168 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008169
Evan Cheng30fb13f2010-12-13 20:32:54 +00008170 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8171 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8172 ARM::isBitFieldInvertedMask(~Mask)) {
8173 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8174 // where lsb(mask) == #shamt and masked bits of B are known zero.
8175 SDValue ShAmt = N00.getOperand(1);
8176 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8177 unsigned LSB = CountTrailingZeros_32(Mask);
8178 if (ShAmtC != LSB)
8179 return SDValue();
8180
8181 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8182 DAG.getConstant(~Mask, MVT::i32));
8183
8184 // Do not add new nodes to DAG combiner worklist.
8185 DCI.CombineTo(N, Res, false);
8186 }
8187
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008188 return SDValue();
8189}
8190
Evan Chengc892aeb2012-02-23 01:19:06 +00008191static SDValue PerformXORCombine(SDNode *N,
8192 TargetLowering::DAGCombinerInfo &DCI,
8193 const ARMSubtarget *Subtarget) {
8194 EVT VT = N->getValueType(0);
8195 SelectionDAG &DAG = DCI.DAG;
8196
8197 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8198 return SDValue();
8199
8200 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008201 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8202 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8203 if (Result.getNode())
8204 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008205 }
8206
8207 return SDValue();
8208}
8209
Evan Chengbf188ae2011-06-15 01:12:31 +00008210/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8211/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008212static SDValue PerformBFICombine(SDNode *N,
8213 TargetLowering::DAGCombinerInfo &DCI) {
8214 SDValue N1 = N->getOperand(1);
8215 if (N1.getOpcode() == ISD::AND) {
8216 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8217 if (!N11C)
8218 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008219 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8220 unsigned LSB = CountTrailingZeros_32(~InvMask);
8221 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8222 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008223 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008224 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008225 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8226 N->getOperand(0), N1.getOperand(0),
8227 N->getOperand(2));
8228 }
8229 return SDValue();
8230}
8231
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008232/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8233/// ARMISD::VMOVRRD.
8234static SDValue PerformVMOVRRDCombine(SDNode *N,
8235 TargetLowering::DAGCombinerInfo &DCI) {
8236 // vmovrrd(vmovdrr x, y) -> x,y
8237 SDValue InDouble = N->getOperand(0);
8238 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8239 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008240
8241 // vmovrrd(load f64) -> (load i32), (load i32)
8242 SDNode *InNode = InDouble.getNode();
8243 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8244 InNode->getValueType(0) == MVT::f64 &&
8245 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8246 !cast<LoadSDNode>(InNode)->isVolatile()) {
8247 // TODO: Should this be done for non-FrameIndex operands?
8248 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8249
8250 SelectionDAG &DAG = DCI.DAG;
8251 DebugLoc DL = LD->getDebugLoc();
8252 SDValue BasePtr = LD->getBasePtr();
8253 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8254 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008255 LD->isNonTemporal(), LD->isInvariant(),
8256 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008257
8258 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8259 DAG.getConstant(4, MVT::i32));
8260 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8261 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008262 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008263 std::min(4U, LD->getAlignment() / 2));
8264
8265 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8266 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8267 DCI.RemoveFromWorklist(LD);
8268 DAG.DeleteNode(LD);
8269 return Result;
8270 }
8271
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008272 return SDValue();
8273}
8274
8275/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8276/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8277static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8278 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8279 SDValue Op0 = N->getOperand(0);
8280 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008281 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008282 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008283 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008284 Op1 = Op1.getOperand(0);
8285 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8286 Op0.getNode() == Op1.getNode() &&
8287 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008288 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008289 N->getValueType(0), Op0.getOperand(0));
8290 return SDValue();
8291}
8292
Bob Wilson31600902010-12-21 06:43:19 +00008293/// PerformSTORECombine - Target-specific dag combine xforms for
8294/// ISD::STORE.
8295static SDValue PerformSTORECombine(SDNode *N,
8296 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008297 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008298 if (St->isVolatile())
8299 return SDValue();
8300
Andrew Trick49b446f2012-07-18 18:34:24 +00008301 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008302 // pack all of the elements in one place. Next, store to memory in fewer
8303 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008304 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008305 EVT VT = StVal.getValueType();
8306 if (St->isTruncatingStore() && VT.isVector()) {
8307 SelectionDAG &DAG = DCI.DAG;
8308 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8309 EVT StVT = St->getMemoryVT();
8310 unsigned NumElems = VT.getVectorNumElements();
8311 assert(StVT != VT && "Cannot truncate to the same type");
8312 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8313 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8314
8315 // From, To sizes and ElemCount must be pow of two
8316 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8317
8318 // We are going to use the original vector elt for storing.
8319 // Accumulated smaller vector elements must be a multiple of the store size.
8320 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8321
8322 unsigned SizeRatio = FromEltSz / ToEltSz;
8323 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8324
8325 // Create a type on which we perform the shuffle.
8326 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8327 NumElems*SizeRatio);
8328 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8329
8330 DebugLoc DL = St->getDebugLoc();
8331 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8332 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8333 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8334
8335 // Can't shuffle using an illegal type.
8336 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8337
8338 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8339 DAG.getUNDEF(WideVec.getValueType()),
8340 ShuffleVec.data());
8341 // At this point all of the data is stored at the bottom of the
8342 // register. We now need to save it to mem.
8343
8344 // Find the largest store unit
8345 MVT StoreType = MVT::i8;
8346 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8347 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8348 MVT Tp = (MVT::SimpleValueType)tp;
8349 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8350 StoreType = Tp;
8351 }
8352 // Didn't find a legal store type.
8353 if (!TLI.isTypeLegal(StoreType))
8354 return SDValue();
8355
8356 // Bitcast the original vector into a vector of store-size units
8357 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8358 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8359 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8360 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8361 SmallVector<SDValue, 8> Chains;
8362 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8363 TLI.getPointerTy());
8364 SDValue BasePtr = St->getBasePtr();
8365
8366 // Perform one or more big stores into memory.
8367 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8368 for (unsigned I = 0; I < E; I++) {
8369 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8370 StoreType, ShuffWide,
8371 DAG.getIntPtrConstant(I));
8372 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8373 St->getPointerInfo(), St->isVolatile(),
8374 St->isNonTemporal(), St->getAlignment());
8375 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8376 Increment);
8377 Chains.push_back(Ch);
8378 }
8379 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8380 Chains.size());
8381 }
8382
8383 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008384 return SDValue();
8385
Chad Rosier96b66d62012-04-09 19:38:15 +00008386 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8387 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008388 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008389 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008390 SelectionDAG &DAG = DCI.DAG;
8391 DebugLoc DL = St->getDebugLoc();
8392 SDValue BasePtr = St->getBasePtr();
8393 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8394 StVal.getNode()->getOperand(0), BasePtr,
8395 St->getPointerInfo(), St->isVolatile(),
8396 St->isNonTemporal(), St->getAlignment());
8397
8398 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8399 DAG.getConstant(4, MVT::i32));
8400 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8401 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8402 St->isNonTemporal(),
8403 std::min(4U, St->getAlignment() / 2));
8404 }
8405
8406 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008407 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8408 return SDValue();
8409
Chad Rosier96b66d62012-04-09 19:38:15 +00008410 // Bitcast an i64 store extracted from a vector to f64.
8411 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008412 SelectionDAG &DAG = DCI.DAG;
8413 DebugLoc dl = StVal.getDebugLoc();
8414 SDValue IntVec = StVal.getOperand(0);
8415 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8416 IntVec.getValueType().getVectorNumElements());
8417 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8418 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8419 Vec, StVal.getOperand(1));
8420 dl = N->getDebugLoc();
8421 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8422 // Make the DAGCombiner fold the bitcasts.
8423 DCI.AddToWorklist(Vec.getNode());
8424 DCI.AddToWorklist(ExtElt.getNode());
8425 DCI.AddToWorklist(V.getNode());
8426 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8427 St->getPointerInfo(), St->isVolatile(),
8428 St->isNonTemporal(), St->getAlignment(),
8429 St->getTBAAInfo());
8430}
8431
8432/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8433/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8434/// i64 vector to have f64 elements, since the value can then be loaded
8435/// directly into a VFP register.
8436static bool hasNormalLoadOperand(SDNode *N) {
8437 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8438 for (unsigned i = 0; i < NumElts; ++i) {
8439 SDNode *Elt = N->getOperand(i).getNode();
8440 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8441 return true;
8442 }
8443 return false;
8444}
8445
Bob Wilson75f02882010-09-17 22:59:05 +00008446/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8447/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008448static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8449 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008450 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8451 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8452 // into a pair of GPRs, which is fine when the value is used as a scalar,
8453 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008454 SelectionDAG &DAG = DCI.DAG;
8455 if (N->getNumOperands() == 2) {
8456 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8457 if (RV.getNode())
8458 return RV;
8459 }
Bob Wilson75f02882010-09-17 22:59:05 +00008460
Bob Wilson31600902010-12-21 06:43:19 +00008461 // Load i64 elements as f64 values so that type legalization does not split
8462 // them up into i32 values.
8463 EVT VT = N->getValueType(0);
8464 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8465 return SDValue();
8466 DebugLoc dl = N->getDebugLoc();
8467 SmallVector<SDValue, 8> Ops;
8468 unsigned NumElts = VT.getVectorNumElements();
8469 for (unsigned i = 0; i < NumElts; ++i) {
8470 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8471 Ops.push_back(V);
8472 // Make the DAGCombiner fold the bitcast.
8473 DCI.AddToWorklist(V.getNode());
8474 }
8475 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8476 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8477 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8478}
8479
8480/// PerformInsertEltCombine - Target-specific dag combine xforms for
8481/// ISD::INSERT_VECTOR_ELT.
8482static SDValue PerformInsertEltCombine(SDNode *N,
8483 TargetLowering::DAGCombinerInfo &DCI) {
8484 // Bitcast an i64 load inserted into a vector to f64.
8485 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8486 EVT VT = N->getValueType(0);
8487 SDNode *Elt = N->getOperand(1).getNode();
8488 if (VT.getVectorElementType() != MVT::i64 ||
8489 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8490 return SDValue();
8491
8492 SelectionDAG &DAG = DCI.DAG;
8493 DebugLoc dl = N->getDebugLoc();
8494 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8495 VT.getVectorNumElements());
8496 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8497 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8498 // Make the DAGCombiner fold the bitcasts.
8499 DCI.AddToWorklist(Vec.getNode());
8500 DCI.AddToWorklist(V.getNode());
8501 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8502 Vec, V, N->getOperand(2));
8503 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008504}
8505
Bob Wilsonf20700c2010-10-27 20:38:28 +00008506/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8507/// ISD::VECTOR_SHUFFLE.
8508static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8509 // The LLVM shufflevector instruction does not require the shuffle mask
8510 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8511 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8512 // operands do not match the mask length, they are extended by concatenating
8513 // them with undef vectors. That is probably the right thing for other
8514 // targets, but for NEON it is better to concatenate two double-register
8515 // size vector operands into a single quad-register size vector. Do that
8516 // transformation here:
8517 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8518 // shuffle(concat(v1, v2), undef)
8519 SDValue Op0 = N->getOperand(0);
8520 SDValue Op1 = N->getOperand(1);
8521 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8522 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8523 Op0.getNumOperands() != 2 ||
8524 Op1.getNumOperands() != 2)
8525 return SDValue();
8526 SDValue Concat0Op1 = Op0.getOperand(1);
8527 SDValue Concat1Op1 = Op1.getOperand(1);
8528 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8529 Concat1Op1.getOpcode() != ISD::UNDEF)
8530 return SDValue();
8531 // Skip the transformation if any of the types are illegal.
8532 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8533 EVT VT = N->getValueType(0);
8534 if (!TLI.isTypeLegal(VT) ||
8535 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8536 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8537 return SDValue();
8538
8539 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8540 Op0.getOperand(0), Op1.getOperand(0));
8541 // Translate the shuffle mask.
8542 SmallVector<int, 16> NewMask;
8543 unsigned NumElts = VT.getVectorNumElements();
8544 unsigned HalfElts = NumElts/2;
8545 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8546 for (unsigned n = 0; n < NumElts; ++n) {
8547 int MaskElt = SVN->getMaskElt(n);
8548 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008549 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008550 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008551 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008552 NewElt = HalfElts + MaskElt - NumElts;
8553 NewMask.push_back(NewElt);
8554 }
8555 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8556 DAG.getUNDEF(VT), NewMask.data());
8557}
8558
Bob Wilson1c3ef902011-02-07 17:43:21 +00008559/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8560/// NEON load/store intrinsics to merge base address updates.
8561static SDValue CombineBaseUpdate(SDNode *N,
8562 TargetLowering::DAGCombinerInfo &DCI) {
8563 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8564 return SDValue();
8565
8566 SelectionDAG &DAG = DCI.DAG;
8567 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8568 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8569 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8570 SDValue Addr = N->getOperand(AddrOpIdx);
8571
8572 // Search for a use of the address operand that is an increment.
8573 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8574 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8575 SDNode *User = *UI;
8576 if (User->getOpcode() != ISD::ADD ||
8577 UI.getUse().getResNo() != Addr.getResNo())
8578 continue;
8579
8580 // Check that the add is independent of the load/store. Otherwise, folding
8581 // it would create a cycle.
8582 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8583 continue;
8584
8585 // Find the new opcode for the updating load/store.
8586 bool isLoad = true;
8587 bool isLaneOp = false;
8588 unsigned NewOpc = 0;
8589 unsigned NumVecs = 0;
8590 if (isIntrinsic) {
8591 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8592 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008593 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008594 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8595 NumVecs = 1; break;
8596 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8597 NumVecs = 2; break;
8598 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8599 NumVecs = 3; break;
8600 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8601 NumVecs = 4; break;
8602 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8603 NumVecs = 2; isLaneOp = true; break;
8604 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8605 NumVecs = 3; isLaneOp = true; break;
8606 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8607 NumVecs = 4; isLaneOp = true; break;
8608 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8609 NumVecs = 1; isLoad = false; break;
8610 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8611 NumVecs = 2; isLoad = false; break;
8612 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8613 NumVecs = 3; isLoad = false; break;
8614 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8615 NumVecs = 4; isLoad = false; break;
8616 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8617 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8618 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8619 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8620 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8621 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8622 }
8623 } else {
8624 isLaneOp = true;
8625 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008626 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008627 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8628 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8629 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8630 }
8631 }
8632
8633 // Find the size of memory referenced by the load/store.
8634 EVT VecTy;
8635 if (isLoad)
8636 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008637 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008638 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8639 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8640 if (isLaneOp)
8641 NumBytes /= VecTy.getVectorNumElements();
8642
8643 // If the increment is a constant, it must match the memory ref size.
8644 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8645 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8646 uint64_t IncVal = CInc->getZExtValue();
8647 if (IncVal != NumBytes)
8648 continue;
8649 } else if (NumBytes >= 3 * 16) {
8650 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8651 // separate instructions that make it harder to use a non-constant update.
8652 continue;
8653 }
8654
8655 // Create the new updating load/store node.
8656 EVT Tys[6];
8657 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8658 unsigned n;
8659 for (n = 0; n < NumResultVecs; ++n)
8660 Tys[n] = VecTy;
8661 Tys[n++] = MVT::i32;
8662 Tys[n] = MVT::Other;
8663 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8664 SmallVector<SDValue, 8> Ops;
8665 Ops.push_back(N->getOperand(0)); // incoming chain
8666 Ops.push_back(N->getOperand(AddrOpIdx));
8667 Ops.push_back(Inc);
8668 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8669 Ops.push_back(N->getOperand(i));
8670 }
8671 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8672 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8673 Ops.data(), Ops.size(),
8674 MemInt->getMemoryVT(),
8675 MemInt->getMemOperand());
8676
8677 // Update the uses.
8678 std::vector<SDValue> NewResults;
8679 for (unsigned i = 0; i < NumResultVecs; ++i) {
8680 NewResults.push_back(SDValue(UpdN.getNode(), i));
8681 }
8682 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8683 DCI.CombineTo(N, NewResults);
8684 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8685
8686 break;
Owen Anderson76706012011-04-05 21:48:57 +00008687 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008688 return SDValue();
8689}
8690
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008691/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8692/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8693/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8694/// return true.
8695static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8696 SelectionDAG &DAG = DCI.DAG;
8697 EVT VT = N->getValueType(0);
8698 // vldN-dup instructions only support 64-bit vectors for N > 1.
8699 if (!VT.is64BitVector())
8700 return false;
8701
8702 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8703 SDNode *VLD = N->getOperand(0).getNode();
8704 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8705 return false;
8706 unsigned NumVecs = 0;
8707 unsigned NewOpc = 0;
8708 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8709 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8710 NumVecs = 2;
8711 NewOpc = ARMISD::VLD2DUP;
8712 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8713 NumVecs = 3;
8714 NewOpc = ARMISD::VLD3DUP;
8715 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8716 NumVecs = 4;
8717 NewOpc = ARMISD::VLD4DUP;
8718 } else {
8719 return false;
8720 }
8721
8722 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8723 // numbers match the load.
8724 unsigned VLDLaneNo =
8725 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8726 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8727 UI != UE; ++UI) {
8728 // Ignore uses of the chain result.
8729 if (UI.getUse().getResNo() == NumVecs)
8730 continue;
8731 SDNode *User = *UI;
8732 if (User->getOpcode() != ARMISD::VDUPLANE ||
8733 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8734 return false;
8735 }
8736
8737 // Create the vldN-dup node.
8738 EVT Tys[5];
8739 unsigned n;
8740 for (n = 0; n < NumVecs; ++n)
8741 Tys[n] = VT;
8742 Tys[n] = MVT::Other;
8743 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8744 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8745 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8746 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8747 Ops, 2, VLDMemInt->getMemoryVT(),
8748 VLDMemInt->getMemOperand());
8749
8750 // Update the uses.
8751 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8752 UI != UE; ++UI) {
8753 unsigned ResNo = UI.getUse().getResNo();
8754 // Ignore uses of the chain result.
8755 if (ResNo == NumVecs)
8756 continue;
8757 SDNode *User = *UI;
8758 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8759 }
8760
8761 // Now the vldN-lane intrinsic is dead except for its chain result.
8762 // Update uses of the chain.
8763 std::vector<SDValue> VLDDupResults;
8764 for (unsigned n = 0; n < NumVecs; ++n)
8765 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8766 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8767 DCI.CombineTo(VLD, VLDDupResults);
8768
8769 return true;
8770}
8771
Bob Wilson9e82bf12010-07-14 01:22:12 +00008772/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8773/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008774static SDValue PerformVDUPLANECombine(SDNode *N,
8775 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008776 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008777
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008778 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8779 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8780 if (CombineVLDDUP(N, DCI))
8781 return SDValue(N, 0);
8782
8783 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8784 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008785 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008786 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008787 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008788 return SDValue();
8789
8790 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8791 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8792 // The canonical VMOV for a zero vector uses a 32-bit element size.
8793 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8794 unsigned EltBits;
8795 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8796 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008797 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008798 if (EltSize > VT.getVectorElementType().getSizeInBits())
8799 return SDValue();
8800
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008801 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008802}
8803
Eric Christopherfa6f5912011-06-29 21:10:36 +00008804// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008805// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8806static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8807{
Chad Rosier118c9a02011-06-28 17:26:57 +00008808 integerPart cN;
8809 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008810 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8811 I != E; I++) {
8812 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8813 if (!C)
8814 return false;
8815
Eric Christopherfa6f5912011-06-29 21:10:36 +00008816 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008817 APFloat APF = C->getValueAPF();
8818 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8819 != APFloat::opOK || !isExact)
8820 return false;
8821
8822 c0 = (I == 0) ? cN : c0;
8823 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8824 return false;
8825 }
8826 C = c0;
8827 return true;
8828}
8829
8830/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8831/// can replace combinations of VMUL and VCVT (floating-point to integer)
8832/// when the VMUL has a constant operand that is a power of 2.
8833///
8834/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8835/// vmul.f32 d16, d17, d16
8836/// vcvt.s32.f32 d16, d16
8837/// becomes:
8838/// vcvt.s32.f32 d16, d16, #3
8839static SDValue PerformVCVTCombine(SDNode *N,
8840 TargetLowering::DAGCombinerInfo &DCI,
8841 const ARMSubtarget *Subtarget) {
8842 SelectionDAG &DAG = DCI.DAG;
8843 SDValue Op = N->getOperand(0);
8844
8845 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8846 Op.getOpcode() != ISD::FMUL)
8847 return SDValue();
8848
8849 uint64_t C;
8850 SDValue N0 = Op->getOperand(0);
8851 SDValue ConstVec = Op->getOperand(1);
8852 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8853
Eric Christopherfa6f5912011-06-29 21:10:36 +00008854 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008855 !isConstVecPow2(ConstVec, isSigned, C))
8856 return SDValue();
8857
8858 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8859 Intrinsic::arm_neon_vcvtfp2fxu;
8860 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8861 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008862 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008863 DAG.getConstant(Log2_64(C), MVT::i32));
8864}
8865
8866/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8867/// can replace combinations of VCVT (integer to floating-point) and VDIV
8868/// when the VDIV has a constant operand that is a power of 2.
8869///
8870/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8871/// vcvt.f32.s32 d16, d16
8872/// vdiv.f32 d16, d17, d16
8873/// becomes:
8874/// vcvt.f32.s32 d16, d16, #3
8875static SDValue PerformVDIVCombine(SDNode *N,
8876 TargetLowering::DAGCombinerInfo &DCI,
8877 const ARMSubtarget *Subtarget) {
8878 SelectionDAG &DAG = DCI.DAG;
8879 SDValue Op = N->getOperand(0);
8880 unsigned OpOpcode = Op.getNode()->getOpcode();
8881
8882 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8883 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8884 return SDValue();
8885
8886 uint64_t C;
8887 SDValue ConstVec = N->getOperand(1);
8888 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8889
8890 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8891 !isConstVecPow2(ConstVec, isSigned, C))
8892 return SDValue();
8893
Eric Christopherfa6f5912011-06-29 21:10:36 +00008894 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008895 Intrinsic::arm_neon_vcvtfxu2fp;
8896 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8897 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008898 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008899 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8900}
8901
8902/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008903/// operand of a vector shift operation, where all the elements of the
8904/// build_vector must have the same constant integer value.
8905static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8906 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008907 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008908 Op = Op.getOperand(0);
8909 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8910 APInt SplatBits, SplatUndef;
8911 unsigned SplatBitSize;
8912 bool HasAnyUndefs;
8913 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8914 HasAnyUndefs, ElementBits) ||
8915 SplatBitSize > ElementBits)
8916 return false;
8917 Cnt = SplatBits.getSExtValue();
8918 return true;
8919}
8920
8921/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8922/// operand of a vector shift left operation. That value must be in the range:
8923/// 0 <= Value < ElementBits for a left shift; or
8924/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008925static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008926 assert(VT.isVector() && "vector shift count is not a vector type");
8927 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8928 if (! getVShiftImm(Op, ElementBits, Cnt))
8929 return false;
8930 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8931}
8932
8933/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8934/// operand of a vector shift right operation. For a shift opcode, the value
8935/// is positive, but for an intrinsic the value count must be negative. The
8936/// absolute value must be in the range:
8937/// 1 <= |Value| <= ElementBits for a right shift; or
8938/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008939static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008940 int64_t &Cnt) {
8941 assert(VT.isVector() && "vector shift count is not a vector type");
8942 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8943 if (! getVShiftImm(Op, ElementBits, Cnt))
8944 return false;
8945 if (isIntrinsic)
8946 Cnt = -Cnt;
8947 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8948}
8949
8950/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8951static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8952 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8953 switch (IntNo) {
8954 default:
8955 // Don't do anything for most intrinsics.
8956 break;
8957
8958 // Vector shifts: check for immediate versions and lower them.
8959 // Note: This is done during DAG combining instead of DAG legalizing because
8960 // the build_vectors for 64-bit vector element shift counts are generally
8961 // not legal, and it is hard to see their values after they get legalized to
8962 // loads from a constant pool.
8963 case Intrinsic::arm_neon_vshifts:
8964 case Intrinsic::arm_neon_vshiftu:
8965 case Intrinsic::arm_neon_vshiftls:
8966 case Intrinsic::arm_neon_vshiftlu:
8967 case Intrinsic::arm_neon_vshiftn:
8968 case Intrinsic::arm_neon_vrshifts:
8969 case Intrinsic::arm_neon_vrshiftu:
8970 case Intrinsic::arm_neon_vrshiftn:
8971 case Intrinsic::arm_neon_vqshifts:
8972 case Intrinsic::arm_neon_vqshiftu:
8973 case Intrinsic::arm_neon_vqshiftsu:
8974 case Intrinsic::arm_neon_vqshiftns:
8975 case Intrinsic::arm_neon_vqshiftnu:
8976 case Intrinsic::arm_neon_vqshiftnsu:
8977 case Intrinsic::arm_neon_vqrshiftns:
8978 case Intrinsic::arm_neon_vqrshiftnu:
8979 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008980 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008981 int64_t Cnt;
8982 unsigned VShiftOpc = 0;
8983
8984 switch (IntNo) {
8985 case Intrinsic::arm_neon_vshifts:
8986 case Intrinsic::arm_neon_vshiftu:
8987 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8988 VShiftOpc = ARMISD::VSHL;
8989 break;
8990 }
8991 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8992 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8993 ARMISD::VSHRs : ARMISD::VSHRu);
8994 break;
8995 }
8996 return SDValue();
8997
8998 case Intrinsic::arm_neon_vshiftls:
8999 case Intrinsic::arm_neon_vshiftlu:
9000 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
9001 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009002 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009003
9004 case Intrinsic::arm_neon_vrshifts:
9005 case Intrinsic::arm_neon_vrshiftu:
9006 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9007 break;
9008 return SDValue();
9009
9010 case Intrinsic::arm_neon_vqshifts:
9011 case Intrinsic::arm_neon_vqshiftu:
9012 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9013 break;
9014 return SDValue();
9015
9016 case Intrinsic::arm_neon_vqshiftsu:
9017 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9018 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009019 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009020
9021 case Intrinsic::arm_neon_vshiftn:
9022 case Intrinsic::arm_neon_vrshiftn:
9023 case Intrinsic::arm_neon_vqshiftns:
9024 case Intrinsic::arm_neon_vqshiftnu:
9025 case Intrinsic::arm_neon_vqshiftnsu:
9026 case Intrinsic::arm_neon_vqrshiftns:
9027 case Intrinsic::arm_neon_vqrshiftnu:
9028 case Intrinsic::arm_neon_vqrshiftnsu:
9029 // Narrowing shifts require an immediate right shift.
9030 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9031 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00009032 llvm_unreachable("invalid shift count for narrowing vector shift "
9033 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009034
9035 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009036 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00009037 }
9038
9039 switch (IntNo) {
9040 case Intrinsic::arm_neon_vshifts:
9041 case Intrinsic::arm_neon_vshiftu:
9042 // Opcode already set above.
9043 break;
9044 case Intrinsic::arm_neon_vshiftls:
9045 case Intrinsic::arm_neon_vshiftlu:
9046 if (Cnt == VT.getVectorElementType().getSizeInBits())
9047 VShiftOpc = ARMISD::VSHLLi;
9048 else
9049 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9050 ARMISD::VSHLLs : ARMISD::VSHLLu);
9051 break;
9052 case Intrinsic::arm_neon_vshiftn:
9053 VShiftOpc = ARMISD::VSHRN; break;
9054 case Intrinsic::arm_neon_vrshifts:
9055 VShiftOpc = ARMISD::VRSHRs; break;
9056 case Intrinsic::arm_neon_vrshiftu:
9057 VShiftOpc = ARMISD::VRSHRu; break;
9058 case Intrinsic::arm_neon_vrshiftn:
9059 VShiftOpc = ARMISD::VRSHRN; break;
9060 case Intrinsic::arm_neon_vqshifts:
9061 VShiftOpc = ARMISD::VQSHLs; break;
9062 case Intrinsic::arm_neon_vqshiftu:
9063 VShiftOpc = ARMISD::VQSHLu; break;
9064 case Intrinsic::arm_neon_vqshiftsu:
9065 VShiftOpc = ARMISD::VQSHLsu; break;
9066 case Intrinsic::arm_neon_vqshiftns:
9067 VShiftOpc = ARMISD::VQSHRNs; break;
9068 case Intrinsic::arm_neon_vqshiftnu:
9069 VShiftOpc = ARMISD::VQSHRNu; break;
9070 case Intrinsic::arm_neon_vqshiftnsu:
9071 VShiftOpc = ARMISD::VQSHRNsu; break;
9072 case Intrinsic::arm_neon_vqrshiftns:
9073 VShiftOpc = ARMISD::VQRSHRNs; break;
9074 case Intrinsic::arm_neon_vqrshiftnu:
9075 VShiftOpc = ARMISD::VQRSHRNu; break;
9076 case Intrinsic::arm_neon_vqrshiftnsu:
9077 VShiftOpc = ARMISD::VQRSHRNsu; break;
9078 }
9079
9080 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009081 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009082 }
9083
9084 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00009085 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009086 int64_t Cnt;
9087 unsigned VShiftOpc = 0;
9088
9089 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9090 VShiftOpc = ARMISD::VSLI;
9091 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9092 VShiftOpc = ARMISD::VSRI;
9093 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00009094 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009095 }
9096
9097 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
9098 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009099 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009100 }
9101
9102 case Intrinsic::arm_neon_vqrshifts:
9103 case Intrinsic::arm_neon_vqrshiftu:
9104 // No immediate versions of these to check for.
9105 break;
9106 }
9107
9108 return SDValue();
9109}
9110
9111/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9112/// lowers them. As with the vector shift intrinsics, this is done during DAG
9113/// combining instead of DAG legalizing because the build_vectors for 64-bit
9114/// vector element shift counts are generally not legal, and it is hard to see
9115/// their values after they get legalized to loads from a constant pool.
9116static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9117 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00009118 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00009119 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9120 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9121 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9122 SDValue N1 = N->getOperand(1);
9123 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9124 SDValue N0 = N->getOperand(0);
9125 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9126 DAG.MaskedValueIsZero(N0.getOperand(0),
9127 APInt::getHighBitsSet(32, 16)))
9128 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
9129 }
9130 }
Bob Wilson5bafff32009-06-22 23:27:02 +00009131
9132 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00009133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9134 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00009135 return SDValue();
9136
9137 assert(ST->hasNEON() && "unexpected vector shift");
9138 int64_t Cnt;
9139
9140 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009141 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009142
9143 case ISD::SHL:
9144 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9145 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009146 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009147 break;
9148
9149 case ISD::SRA:
9150 case ISD::SRL:
9151 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9152 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9153 ARMISD::VSHRs : ARMISD::VSHRu);
9154 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009155 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009156 }
9157 }
9158 return SDValue();
9159}
9160
9161/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9162/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9163static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9164 const ARMSubtarget *ST) {
9165 SDValue N0 = N->getOperand(0);
9166
9167 // Check for sign- and zero-extensions of vector extract operations of 8-
9168 // and 16-bit vector elements. NEON supports these directly. They are
9169 // handled during DAG combining because type legalization will promote them
9170 // to 32-bit types and it is messy to recognize the operations after that.
9171 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9172 SDValue Vec = N0.getOperand(0);
9173 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009174 EVT VT = N->getValueType(0);
9175 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009176 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9177
Owen Anderson825b72b2009-08-11 20:47:22 +00009178 if (VT == MVT::i32 &&
9179 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00009180 TLI.isTypeLegal(Vec.getValueType()) &&
9181 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009182
9183 unsigned Opc = 0;
9184 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009185 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009186 case ISD::SIGN_EXTEND:
9187 Opc = ARMISD::VGETLANEs;
9188 break;
9189 case ISD::ZERO_EXTEND:
9190 case ISD::ANY_EXTEND:
9191 Opc = ARMISD::VGETLANEu;
9192 break;
9193 }
9194 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9195 }
9196 }
9197
9198 return SDValue();
9199}
9200
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009201/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9202/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9203static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9204 const ARMSubtarget *ST) {
9205 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009206 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009207 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9208 // a NaN; only do the transformation when it matches that behavior.
9209
9210 // For now only do this when using NEON for FP operations; if using VFP, it
9211 // is not obvious that the benefit outweighs the cost of switching to the
9212 // NEON pipeline.
9213 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9214 N->getValueType(0) != MVT::f32)
9215 return SDValue();
9216
9217 SDValue CondLHS = N->getOperand(0);
9218 SDValue CondRHS = N->getOperand(1);
9219 SDValue LHS = N->getOperand(2);
9220 SDValue RHS = N->getOperand(3);
9221 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9222
9223 unsigned Opcode = 0;
9224 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009225 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009226 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009227 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009228 IsReversed = true ; // x CC y ? y : x
9229 } else {
9230 return SDValue();
9231 }
9232
Bob Wilsone742bb52010-02-24 22:15:53 +00009233 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009234 switch (CC) {
9235 default: break;
9236 case ISD::SETOLT:
9237 case ISD::SETOLE:
9238 case ISD::SETLT:
9239 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009240 case ISD::SETULT:
9241 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009242 // If LHS is NaN, an ordered comparison will be false and the result will
9243 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9244 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9245 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9246 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9247 break;
9248 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9249 // will return -0, so vmin can only be used for unsafe math or if one of
9250 // the operands is known to be nonzero.
9251 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009252 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009253 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9254 break;
9255 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009256 break;
9257
9258 case ISD::SETOGT:
9259 case ISD::SETOGE:
9260 case ISD::SETGT:
9261 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009262 case ISD::SETUGT:
9263 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009264 // If LHS is NaN, an ordered comparison will be false and the result will
9265 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9266 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9267 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9268 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9269 break;
9270 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9271 // will return +0, so vmax can only be used for unsafe math or if one of
9272 // the operands is known to be nonzero.
9273 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009274 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009275 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9276 break;
9277 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009278 break;
9279 }
9280
9281 if (!Opcode)
9282 return SDValue();
9283 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9284}
9285
Evan Chenge721f5c2011-07-13 00:42:17 +00009286/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9287SDValue
9288ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9289 SDValue Cmp = N->getOperand(4);
9290 if (Cmp.getOpcode() != ARMISD::CMPZ)
9291 // Only looking at EQ and NE cases.
9292 return SDValue();
9293
9294 EVT VT = N->getValueType(0);
9295 DebugLoc dl = N->getDebugLoc();
9296 SDValue LHS = Cmp.getOperand(0);
9297 SDValue RHS = Cmp.getOperand(1);
9298 SDValue FalseVal = N->getOperand(0);
9299 SDValue TrueVal = N->getOperand(1);
9300 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009301 ARMCC::CondCodes CC =
9302 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009303
9304 // Simplify
9305 // mov r1, r0
9306 // cmp r1, x
9307 // mov r0, y
9308 // moveq r0, x
9309 // to
9310 // cmp r0, x
9311 // movne r0, y
9312 //
9313 // mov r1, r0
9314 // cmp r1, x
9315 // mov r0, x
9316 // movne r0, y
9317 // to
9318 // cmp r0, x
9319 // movne r0, y
9320 /// FIXME: Turn this into a target neutral optimization?
9321 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009322 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009323 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9324 N->getOperand(3), Cmp);
9325 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9326 SDValue ARMcc;
9327 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9328 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9329 N->getOperand(3), NewCmp);
9330 }
9331
9332 if (Res.getNode()) {
9333 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009334 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009335 // Capture demanded bits information that would be otherwise lost.
9336 if (KnownZero == 0xfffffffe)
9337 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9338 DAG.getValueType(MVT::i1));
9339 else if (KnownZero == 0xffffff00)
9340 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9341 DAG.getValueType(MVT::i8));
9342 else if (KnownZero == 0xffff0000)
9343 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9344 DAG.getValueType(MVT::i16));
9345 }
9346
9347 return Res;
9348}
9349
Dan Gohman475871a2008-07-27 21:46:04 +00009350SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009351 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009352 switch (N->getOpcode()) {
9353 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009354 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009355 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009356 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009357 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009358 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009359 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9360 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009361 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009362 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009363 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009364 case ISD::STORE: return PerformSTORECombine(N, DCI);
9365 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9366 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009367 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009368 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009369 case ISD::FP_TO_SINT:
9370 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9371 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009372 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009373 case ISD::SHL:
9374 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009375 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009376 case ISD::SIGN_EXTEND:
9377 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009378 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9379 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009380 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009381 case ARMISD::VLD2DUP:
9382 case ARMISD::VLD3DUP:
9383 case ARMISD::VLD4DUP:
9384 return CombineBaseUpdate(N, DCI);
9385 case ISD::INTRINSIC_VOID:
9386 case ISD::INTRINSIC_W_CHAIN:
9387 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9388 case Intrinsic::arm_neon_vld1:
9389 case Intrinsic::arm_neon_vld2:
9390 case Intrinsic::arm_neon_vld3:
9391 case Intrinsic::arm_neon_vld4:
9392 case Intrinsic::arm_neon_vld2lane:
9393 case Intrinsic::arm_neon_vld3lane:
9394 case Intrinsic::arm_neon_vld4lane:
9395 case Intrinsic::arm_neon_vst1:
9396 case Intrinsic::arm_neon_vst2:
9397 case Intrinsic::arm_neon_vst3:
9398 case Intrinsic::arm_neon_vst4:
9399 case Intrinsic::arm_neon_vst2lane:
9400 case Intrinsic::arm_neon_vst3lane:
9401 case Intrinsic::arm_neon_vst4lane:
9402 return CombineBaseUpdate(N, DCI);
9403 default: break;
9404 }
9405 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009406 }
Dan Gohman475871a2008-07-27 21:46:04 +00009407 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009408}
9409
Evan Cheng31959b12011-02-02 01:06:55 +00009410bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9411 EVT VT) const {
9412 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9413}
9414
Evan Cheng376642e2012-12-10 23:21:26 +00009415bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009416 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009417 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009418
9419 switch (VT.getSimpleVT().SimpleTy) {
9420 default:
9421 return false;
9422 case MVT::i8:
9423 case MVT::i16:
Evan Cheng376642e2012-12-10 23:21:26 +00009424 case MVT::i32: {
Evan Chengd10eab02012-09-18 01:42:45 +00009425 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng376642e2012-12-10 23:21:26 +00009426 if (AllowsUnaligned) {
9427 if (Fast)
9428 *Fast = Subtarget->hasV7Ops();
9429 return true;
9430 }
9431 return false;
9432 }
Evan Chenga99c5082012-08-15 17:44:53 +00009433 case MVT::f64:
Evan Cheng376642e2012-12-10 23:21:26 +00009434 case MVT::v2f64: {
Evan Chengd10eab02012-09-18 01:42:45 +00009435 // For any little-endian targets with neon, we can support unaligned ld/st
9436 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9437 // A big-endian target may also explictly support unaligned accesses
Evan Cheng376642e2012-12-10 23:21:26 +00009438 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9439 if (Fast)
9440 *Fast = true;
9441 return true;
9442 }
9443 return false;
9444 }
Bill Wendlingaf566342009-08-15 21:21:19 +00009445 }
9446}
9447
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009448static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9449 unsigned AlignCheck) {
9450 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9451 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9452}
9453
9454EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9455 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00009456 bool IsMemset, bool ZeroMemset,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009457 bool MemcpyStrSrc,
9458 MachineFunction &MF) const {
9459 const Function *F = MF.getFunction();
9460
9461 // See if we can use NEON instructions for this...
Evan Cheng946a3a92012-12-12 02:34:41 +00009462 if ((!IsMemset || ZeroMemset) &&
Evan Cheng376642e2012-12-10 23:21:26 +00009463 Subtarget->hasNEON() &&
Bill Wendling831737d2012-12-30 10:32:01 +00009464 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9465 Attribute::NoImplicitFloat)) {
Evan Cheng376642e2012-12-10 23:21:26 +00009466 bool Fast;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009467 if (Size >= 16 &&
9468 (memOpAlign(SrcAlign, DstAlign, 16) ||
9469 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009470 return MVT::v2f64;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009471 } else if (Size >= 8 &&
9472 (memOpAlign(SrcAlign, DstAlign, 8) ||
9473 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009474 return MVT::f64;
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009475 }
9476 }
9477
Lang Hames5207bf22011-11-08 18:56:23 +00009478 // Lowering to i32/i16 if the size permits.
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009479 if (Size >= 4)
Lang Hames5207bf22011-11-08 18:56:23 +00009480 return MVT::i32;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009481 else if (Size >= 2)
Lang Hames5207bf22011-11-08 18:56:23 +00009482 return MVT::i16;
Lang Hames5207bf22011-11-08 18:56:23 +00009483
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009484 // Let the target-independent logic figure it out.
9485 return MVT::Other;
9486}
9487
Evan Cheng2766a472012-12-06 19:13:27 +00009488bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9489 if (Val.getOpcode() != ISD::LOAD)
9490 return false;
9491
9492 EVT VT1 = Val.getValueType();
9493 if (!VT1.isSimple() || !VT1.isInteger() ||
9494 !VT2.isSimple() || !VT2.isInteger())
9495 return false;
9496
9497 switch (VT1.getSimpleVT().SimpleTy) {
9498 default: break;
9499 case MVT::i1:
9500 case MVT::i8:
9501 case MVT::i16:
9502 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9503 return true;
9504 }
9505
9506 return false;
9507}
9508
Evan Chenge6c835f2009-08-14 20:09:37 +00009509static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9510 if (V < 0)
9511 return false;
9512
9513 unsigned Scale = 1;
9514 switch (VT.getSimpleVT().SimpleTy) {
9515 default: return false;
9516 case MVT::i1:
9517 case MVT::i8:
9518 // Scale == 1;
9519 break;
9520 case MVT::i16:
9521 // Scale == 2;
9522 Scale = 2;
9523 break;
9524 case MVT::i32:
9525 // Scale == 4;
9526 Scale = 4;
9527 break;
9528 }
9529
9530 if ((V & (Scale - 1)) != 0)
9531 return false;
9532 V /= Scale;
9533 return V == (V & ((1LL << 5) - 1));
9534}
9535
9536static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9537 const ARMSubtarget *Subtarget) {
9538 bool isNeg = false;
9539 if (V < 0) {
9540 isNeg = true;
9541 V = - V;
9542 }
9543
9544 switch (VT.getSimpleVT().SimpleTy) {
9545 default: return false;
9546 case MVT::i1:
9547 case MVT::i8:
9548 case MVT::i16:
9549 case MVT::i32:
9550 // + imm12 or - imm8
9551 if (isNeg)
9552 return V == (V & ((1LL << 8) - 1));
9553 return V == (V & ((1LL << 12) - 1));
9554 case MVT::f32:
9555 case MVT::f64:
9556 // Same as ARM mode. FIXME: NEON?
9557 if (!Subtarget->hasVFP2())
9558 return false;
9559 if ((V & 3) != 0)
9560 return false;
9561 V >>= 2;
9562 return V == (V & ((1LL << 8) - 1));
9563 }
9564}
9565
Evan Chengb01fad62007-03-12 23:30:29 +00009566/// isLegalAddressImmediate - Return true if the integer value can be used
9567/// as the offset of the target addressing mode for load / store of the
9568/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009569static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009570 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009571 if (V == 0)
9572 return true;
9573
Evan Cheng65011532009-03-09 19:15:00 +00009574 if (!VT.isSimple())
9575 return false;
9576
Evan Chenge6c835f2009-08-14 20:09:37 +00009577 if (Subtarget->isThumb1Only())
9578 return isLegalT1AddressImmediate(V, VT);
9579 else if (Subtarget->isThumb2())
9580 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009581
Evan Chenge6c835f2009-08-14 20:09:37 +00009582 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009583 if (V < 0)
9584 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009585 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009586 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009587 case MVT::i1:
9588 case MVT::i8:
9589 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009590 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009591 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009593 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009594 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009595 case MVT::f32:
9596 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009597 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009598 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009599 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009600 return false;
9601 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009602 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009603 }
Evan Chenga8e29892007-01-19 07:51:42 +00009604}
9605
Evan Chenge6c835f2009-08-14 20:09:37 +00009606bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9607 EVT VT) const {
9608 int Scale = AM.Scale;
9609 if (Scale < 0)
9610 return false;
9611
9612 switch (VT.getSimpleVT().SimpleTy) {
9613 default: return false;
9614 case MVT::i1:
9615 case MVT::i8:
9616 case MVT::i16:
9617 case MVT::i32:
9618 if (Scale == 1)
9619 return true;
9620 // r + r << imm
9621 Scale = Scale & ~1;
9622 return Scale == 2 || Scale == 4 || Scale == 8;
9623 case MVT::i64:
9624 // r + r
9625 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9626 return true;
9627 return false;
9628 case MVT::isVoid:
9629 // Note, we allow "void" uses (basically, uses that aren't loads or
9630 // stores), because arm allows folding a scale into many arithmetic
9631 // operations. This should be made more precise and revisited later.
9632
9633 // Allow r << imm, but the imm has to be a multiple of two.
9634 if (Scale & 1) return false;
9635 return isPowerOf2_32(Scale);
9636 }
9637}
9638
Chris Lattner37caf8c2007-04-09 23:33:39 +00009639/// isLegalAddressingMode - Return true if the addressing mode represented
9640/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009641bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009642 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009643 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009644 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009645 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009646
Chris Lattner37caf8c2007-04-09 23:33:39 +00009647 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009648 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009649 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009650
Chris Lattner37caf8c2007-04-09 23:33:39 +00009651 switch (AM.Scale) {
9652 case 0: // no scale reg, must be "r+i" or "r", or "i".
9653 break;
9654 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009655 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009656 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009657 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009658 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009659 // ARM doesn't support any R+R*scale+imm addr modes.
9660 if (AM.BaseOffs)
9661 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009662
Bob Wilson2c7dab12009-04-08 17:55:28 +00009663 if (!VT.isSimple())
9664 return false;
9665
Evan Chenge6c835f2009-08-14 20:09:37 +00009666 if (Subtarget->isThumb2())
9667 return isLegalT2ScaledAddressingMode(AM, VT);
9668
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009669 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009670 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009671 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009672 case MVT::i1:
9673 case MVT::i8:
9674 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009675 if (Scale < 0) Scale = -Scale;
9676 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009677 return true;
9678 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009679 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009680 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009681 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009682 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009683 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009684 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009685 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009686
Owen Anderson825b72b2009-08-11 20:47:22 +00009687 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009688 // Note, we allow "void" uses (basically, uses that aren't loads or
9689 // stores), because arm allows folding a scale into many arithmetic
9690 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009691
Chris Lattner37caf8c2007-04-09 23:33:39 +00009692 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009693 if (Scale & 1) return false;
9694 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009695 }
Evan Chengb01fad62007-03-12 23:30:29 +00009696 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009697 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009698}
9699
Evan Cheng77e47512009-11-11 19:05:52 +00009700/// isLegalICmpImmediate - Return true if the specified immediate is legal
9701/// icmp immediate, that is the target has icmp instructions which can compare
9702/// a register against the immediate without having to materialize the
9703/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009704bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009705 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009706 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009707 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009708 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009709 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009710 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009711 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009712}
9713
Andrew Trick8d8d9612012-07-18 18:34:27 +00009714/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9715/// *or sub* immediate, that is the target has add or sub instructions which can
9716/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009717/// immediate into a register.
9718bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009719 // Same encoding for add/sub, just flip the sign.
9720 int64_t AbsImm = llvm::abs64(Imm);
9721 if (!Subtarget->isThumb())
9722 return ARM_AM::getSOImmVal(AbsImm) != -1;
9723 if (Subtarget->isThumb2())
9724 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9725 // Thumb1 only has 8-bit unsigned immediate.
9726 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009727}
9728
Owen Andersone50ed302009-08-10 22:56:29 +00009729static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009730 bool isSEXTLoad, SDValue &Base,
9731 SDValue &Offset, bool &isInc,
9732 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009733 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9734 return false;
9735
Owen Anderson825b72b2009-08-11 20:47:22 +00009736 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009737 // AddressingMode 3
9738 Base = Ptr->getOperand(0);
9739 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009740 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009741 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009742 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009743 isInc = false;
9744 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9745 return true;
9746 }
9747 }
9748 isInc = (Ptr->getOpcode() == ISD::ADD);
9749 Offset = Ptr->getOperand(1);
9750 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009751 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009752 // AddressingMode 2
9753 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009754 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009755 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009756 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009757 isInc = false;
9758 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9759 Base = Ptr->getOperand(0);
9760 return true;
9761 }
9762 }
9763
9764 if (Ptr->getOpcode() == ISD::ADD) {
9765 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009766 ARM_AM::ShiftOpc ShOpcVal=
9767 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009768 if (ShOpcVal != ARM_AM::no_shift) {
9769 Base = Ptr->getOperand(1);
9770 Offset = Ptr->getOperand(0);
9771 } else {
9772 Base = Ptr->getOperand(0);
9773 Offset = Ptr->getOperand(1);
9774 }
9775 return true;
9776 }
9777
9778 isInc = (Ptr->getOpcode() == ISD::ADD);
9779 Base = Ptr->getOperand(0);
9780 Offset = Ptr->getOperand(1);
9781 return true;
9782 }
9783
Jim Grosbache5165492009-11-09 00:11:35 +00009784 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009785 return false;
9786}
9787
Owen Andersone50ed302009-08-10 22:56:29 +00009788static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009789 bool isSEXTLoad, SDValue &Base,
9790 SDValue &Offset, bool &isInc,
9791 SelectionDAG &DAG) {
9792 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9793 return false;
9794
9795 Base = Ptr->getOperand(0);
9796 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9797 int RHSC = (int)RHS->getZExtValue();
9798 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9799 assert(Ptr->getOpcode() == ISD::ADD);
9800 isInc = false;
9801 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9802 return true;
9803 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9804 isInc = Ptr->getOpcode() == ISD::ADD;
9805 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9806 return true;
9807 }
9808 }
9809
9810 return false;
9811}
9812
Evan Chenga8e29892007-01-19 07:51:42 +00009813/// getPreIndexedAddressParts - returns true by value, base pointer and
9814/// offset pointer and addressing mode by reference if the node's address
9815/// can be legally represented as pre-indexed load / store address.
9816bool
Dan Gohman475871a2008-07-27 21:46:04 +00009817ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9818 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009819 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009820 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009821 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009822 return false;
9823
Owen Andersone50ed302009-08-10 22:56:29 +00009824 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009825 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009826 bool isSEXTLoad = false;
9827 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9828 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009829 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009830 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9831 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9832 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009833 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009834 } else
9835 return false;
9836
9837 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009838 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009839 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009840 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9841 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009842 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009843 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009844 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009845 if (!isLegal)
9846 return false;
9847
9848 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9849 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009850}
9851
9852/// getPostIndexedAddressParts - returns true by value, base pointer and
9853/// offset pointer and addressing mode by reference if this node can be
9854/// combined with a load / store to form a post-indexed load / store.
9855bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009856 SDValue &Base,
9857 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009858 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009859 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009860 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009861 return false;
9862
Owen Andersone50ed302009-08-10 22:56:29 +00009863 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009864 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009865 bool isSEXTLoad = false;
9866 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009867 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009868 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009869 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9870 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009871 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009872 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009873 } else
9874 return false;
9875
9876 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009877 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009878 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009879 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009880 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009881 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009882 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9883 isInc, DAG);
9884 if (!isLegal)
9885 return false;
9886
Evan Cheng28dad2a2010-05-18 21:31:17 +00009887 if (Ptr != Base) {
9888 // Swap base ptr and offset to catch more post-index load / store when
9889 // it's legal. In Thumb2 mode, offset must be an immediate.
9890 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9891 !Subtarget->isThumb2())
9892 std::swap(Base, Offset);
9893
9894 // Post-indexed load / store update the base pointer.
9895 if (Ptr != Base)
9896 return false;
9897 }
9898
Evan Chenge88d5ce2009-07-02 07:28:31 +00009899 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9900 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009901}
9902
Dan Gohman475871a2008-07-27 21:46:04 +00009903void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009904 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009905 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009906 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009907 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009908 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009909 switch (Op.getOpcode()) {
9910 default: break;
9911 case ARMISD::CMOV: {
9912 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009913 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009914 if (KnownZero == 0 && KnownOne == 0) return;
9915
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009916 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009917 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009918 KnownZero &= KnownZeroRHS;
9919 KnownOne &= KnownOneRHS;
9920 return;
9921 }
9922 }
9923}
9924
9925//===----------------------------------------------------------------------===//
9926// ARM Inline Assembly Support
9927//===----------------------------------------------------------------------===//
9928
Evan Cheng55d42002011-01-08 01:24:27 +00009929bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9930 // Looking for "rev" which is V6+.
9931 if (!Subtarget->hasV6Ops())
9932 return false;
9933
9934 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9935 std::string AsmStr = IA->getAsmString();
9936 SmallVector<StringRef, 4> AsmPieces;
9937 SplitString(AsmStr, AsmPieces, ";\n");
9938
9939 switch (AsmPieces.size()) {
9940 default: return false;
9941 case 1:
9942 AsmStr = AsmPieces[0];
9943 AsmPieces.clear();
9944 SplitString(AsmStr, AsmPieces, " \t,");
9945
9946 // rev $0, $1
9947 if (AsmPieces.size() == 3 &&
9948 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9949 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009950 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009951 if (Ty && Ty->getBitWidth() == 32)
9952 return IntrinsicLowering::LowerToByteSwap(CI);
9953 }
9954 break;
9955 }
9956
9957 return false;
9958}
9959
Evan Chenga8e29892007-01-19 07:51:42 +00009960/// getConstraintType - Given a constraint letter, return the type of
9961/// constraint it is for this target.
9962ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009963ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9964 if (Constraint.size() == 1) {
9965 switch (Constraint[0]) {
9966 default: break;
9967 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009968 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009969 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009970 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009971 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009972 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009973 // An address with a single base register. Due to the way we
9974 // currently handle addresses it is the same as an 'r' memory constraint.
9975 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009976 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009977 } else if (Constraint.size() == 2) {
9978 switch (Constraint[0]) {
9979 default: break;
9980 // All 'U+' constraints are addresses.
9981 case 'U': return C_Memory;
9982 }
Evan Chenga8e29892007-01-19 07:51:42 +00009983 }
Chris Lattner4234f572007-03-25 02:14:49 +00009984 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009985}
9986
John Thompson44ab89e2010-10-29 17:29:13 +00009987/// Examine constraint type and operand type and determine a weight value.
9988/// This object must already have been set up with the operand type
9989/// and the current alternative constraint selected.
9990TargetLowering::ConstraintWeight
9991ARMTargetLowering::getSingleConstraintMatchWeight(
9992 AsmOperandInfo &info, const char *constraint) const {
9993 ConstraintWeight weight = CW_Invalid;
9994 Value *CallOperandVal = info.CallOperandVal;
9995 // If we don't have a value, we can't do a match,
9996 // but allow it at the lowest weight.
9997 if (CallOperandVal == NULL)
9998 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009999 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +000010000 // Look at the constraint type.
10001 switch (*constraint) {
10002 default:
10003 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10004 break;
10005 case 'l':
10006 if (type->isIntegerTy()) {
10007 if (Subtarget->isThumb())
10008 weight = CW_SpecificReg;
10009 else
10010 weight = CW_Register;
10011 }
10012 break;
10013 case 'w':
10014 if (type->isFloatingPointTy())
10015 weight = CW_Register;
10016 break;
10017 }
10018 return weight;
10019}
10020
Eric Christopher35e6d4d2011-06-30 23:50:52 +000010021typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10022RCPair
Evan Chenga8e29892007-01-19 07:51:42 +000010023ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010024 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +000010025 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010026 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +000010027 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +000010028 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010029 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010030 return RCPair(0U, &ARM::tGPRRegClass);
10031 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +000010032 case 'h': // High regs or no regs.
10033 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010034 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +000010035 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010036 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +000010037 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010038 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +000010039 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010040 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +000010041 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010042 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +000010043 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010044 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010045 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010046 case 'x':
10047 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010048 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010049 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010050 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010051 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010052 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010053 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010054 case 't':
10055 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010056 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010057 break;
Evan Chenga8e29892007-01-19 07:51:42 +000010058 }
10059 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010060 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +000010061 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010062
Evan Chenga8e29892007-01-19 07:51:42 +000010063 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10064}
10065
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010066/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10067/// vector. If it is invalid, don't add anything to Ops.
10068void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000010069 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010070 std::vector<SDValue>&Ops,
10071 SelectionDAG &DAG) const {
10072 SDValue Result(0, 0);
10073
Eric Christopher100c8332011-06-02 23:16:42 +000010074 // Currently only support length 1 constraints.
10075 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000010076
Eric Christopher100c8332011-06-02 23:16:42 +000010077 char ConstraintLetter = Constraint[0];
10078 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010079 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +000010080 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010081 case 'I': case 'J': case 'K': case 'L':
10082 case 'M': case 'N': case 'O':
10083 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10084 if (!C)
10085 return;
10086
10087 int64_t CVal64 = C->getSExtValue();
10088 int CVal = (int) CVal64;
10089 // None of these constraints allow values larger than 32 bits. Check
10090 // that the value fits in an int.
10091 if (CVal != CVal64)
10092 return;
10093
Eric Christopher100c8332011-06-02 23:16:42 +000010094 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +000010095 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +000010096 // Constant suitable for movw, must be between 0 and
10097 // 65535.
10098 if (Subtarget->hasV6T2Ops())
10099 if (CVal >= 0 && CVal <= 65535)
10100 break;
10101 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010102 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010103 if (Subtarget->isThumb1Only()) {
10104 // This must be a constant between 0 and 255, for ADD
10105 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010106 if (CVal >= 0 && CVal <= 255)
10107 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010108 } else if (Subtarget->isThumb2()) {
10109 // A constant that can be used as an immediate value in a
10110 // data-processing instruction.
10111 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10112 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010113 } else {
10114 // A constant that can be used as an immediate value in a
10115 // data-processing instruction.
10116 if (ARM_AM::getSOImmVal(CVal) != -1)
10117 break;
10118 }
10119 return;
10120
10121 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010122 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010123 // This must be a constant between -255 and -1, for negated ADD
10124 // immediates. This can be used in GCC with an "n" modifier that
10125 // prints the negated value, for use with SUB instructions. It is
10126 // not useful otherwise but is implemented for compatibility.
10127 if (CVal >= -255 && CVal <= -1)
10128 break;
10129 } else {
10130 // This must be a constant between -4095 and 4095. It is not clear
10131 // what this constraint is intended for. Implemented for
10132 // compatibility with GCC.
10133 if (CVal >= -4095 && CVal <= 4095)
10134 break;
10135 }
10136 return;
10137
10138 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010139 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010140 // A 32-bit value where only one byte has a nonzero value. Exclude
10141 // zero to match GCC. This constraint is used by GCC internally for
10142 // constants that can be loaded with a move/shift combination.
10143 // It is not useful otherwise but is implemented for compatibility.
10144 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10145 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010146 } else if (Subtarget->isThumb2()) {
10147 // A constant whose bitwise inverse can be used as an immediate
10148 // value in a data-processing instruction. This can be used in GCC
10149 // with a "B" modifier that prints the inverted value, for use with
10150 // BIC and MVN instructions. It is not useful otherwise but is
10151 // implemented for compatibility.
10152 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10153 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010154 } else {
10155 // A constant whose bitwise inverse can be used as an immediate
10156 // value in a data-processing instruction. This can be used in GCC
10157 // with a "B" modifier that prints the inverted value, for use with
10158 // BIC and MVN instructions. It is not useful otherwise but is
10159 // implemented for compatibility.
10160 if (ARM_AM::getSOImmVal(~CVal) != -1)
10161 break;
10162 }
10163 return;
10164
10165 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010166 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010167 // This must be a constant between -7 and 7,
10168 // for 3-operand ADD/SUB immediate instructions.
10169 if (CVal >= -7 && CVal < 7)
10170 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010171 } else if (Subtarget->isThumb2()) {
10172 // A constant whose negation can be used as an immediate value in a
10173 // data-processing instruction. This can be used in GCC with an "n"
10174 // modifier that prints the negated value, for use with SUB
10175 // instructions. It is not useful otherwise but is implemented for
10176 // compatibility.
10177 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10178 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010179 } else {
10180 // A constant whose negation can be used as an immediate value in a
10181 // data-processing instruction. This can be used in GCC with an "n"
10182 // modifier that prints the negated value, for use with SUB
10183 // instructions. It is not useful otherwise but is implemented for
10184 // compatibility.
10185 if (ARM_AM::getSOImmVal(-CVal) != -1)
10186 break;
10187 }
10188 return;
10189
10190 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010191 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010192 // This must be a multiple of 4 between 0 and 1020, for
10193 // ADD sp + immediate.
10194 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10195 break;
10196 } else {
10197 // A power of two or a constant between 0 and 32. This is used in
10198 // GCC for the shift amount on shifted register operands, but it is
10199 // useful in general for any shift amounts.
10200 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10201 break;
10202 }
10203 return;
10204
10205 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010206 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010207 // This must be a constant between 0 and 31, for shift amounts.
10208 if (CVal >= 0 && CVal <= 31)
10209 break;
10210 }
10211 return;
10212
10213 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010214 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010215 // This must be a multiple of 4 between -508 and 508, for
10216 // ADD/SUB sp = sp + immediate.
10217 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10218 break;
10219 }
10220 return;
10221 }
10222 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10223 break;
10224 }
10225
10226 if (Result.getNode()) {
10227 Ops.push_back(Result);
10228 return;
10229 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010230 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010231}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010232
10233bool
10234ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10235 // The ARM target isn't yet aware of offsets.
10236 return false;
10237}
Evan Cheng39382422009-10-28 01:44:26 +000010238
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010239bool ARM::isBitFieldInvertedMask(unsigned v) {
10240 if (v == 0xffffffff)
10241 return 0;
10242 // there can be 1's on either or both "outsides", all the "inside"
10243 // bits must be 0's
10244 unsigned int lsb = 0, msb = 31;
10245 while (v & (1 << msb)) --msb;
10246 while (v & (1 << lsb)) ++lsb;
10247 for (unsigned int i = lsb; i <= msb; ++i) {
10248 if (v & (1 << i))
10249 return 0;
10250 }
10251 return 1;
10252}
10253
Evan Cheng39382422009-10-28 01:44:26 +000010254/// isFPImmLegal - Returns true if the target can instruction select the
10255/// specified FP immediate natively. If false, the legalizer will
10256/// materialize the FP immediate as a load from a constant pool.
10257bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10258 if (!Subtarget->hasVFP3())
10259 return false;
10260 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010261 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010262 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010263 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010264 return false;
10265}
Bob Wilson65ffec42010-09-21 17:56:22 +000010266
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010267/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010268/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10269/// specified in the intrinsic calls.
10270bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10271 const CallInst &I,
10272 unsigned Intrinsic) const {
10273 switch (Intrinsic) {
10274 case Intrinsic::arm_neon_vld1:
10275 case Intrinsic::arm_neon_vld2:
10276 case Intrinsic::arm_neon_vld3:
10277 case Intrinsic::arm_neon_vld4:
10278 case Intrinsic::arm_neon_vld2lane:
10279 case Intrinsic::arm_neon_vld3lane:
10280 case Intrinsic::arm_neon_vld4lane: {
10281 Info.opc = ISD::INTRINSIC_W_CHAIN;
10282 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010283 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010284 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10285 Info.ptrVal = I.getArgOperand(0);
10286 Info.offset = 0;
10287 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10288 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10289 Info.vol = false; // volatile loads with NEON intrinsics not supported
10290 Info.readMem = true;
10291 Info.writeMem = false;
10292 return true;
10293 }
10294 case Intrinsic::arm_neon_vst1:
10295 case Intrinsic::arm_neon_vst2:
10296 case Intrinsic::arm_neon_vst3:
10297 case Intrinsic::arm_neon_vst4:
10298 case Intrinsic::arm_neon_vst2lane:
10299 case Intrinsic::arm_neon_vst3lane:
10300 case Intrinsic::arm_neon_vst4lane: {
10301 Info.opc = ISD::INTRINSIC_VOID;
10302 // Conservatively set memVT to the entire set of vectors stored.
10303 unsigned NumElts = 0;
10304 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010305 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010306 if (!ArgTy->isVectorTy())
10307 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010308 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010309 }
10310 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10311 Info.ptrVal = I.getArgOperand(0);
10312 Info.offset = 0;
10313 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10314 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10315 Info.vol = false; // volatile stores with NEON intrinsics not supported
10316 Info.readMem = false;
10317 Info.writeMem = true;
10318 return true;
10319 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010320 case Intrinsic::arm_strexd: {
10321 Info.opc = ISD::INTRINSIC_W_CHAIN;
10322 Info.memVT = MVT::i64;
10323 Info.ptrVal = I.getArgOperand(2);
10324 Info.offset = 0;
10325 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010326 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010327 Info.readMem = false;
10328 Info.writeMem = true;
10329 return true;
10330 }
10331 case Intrinsic::arm_ldrexd: {
10332 Info.opc = ISD::INTRINSIC_W_CHAIN;
10333 Info.memVT = MVT::i64;
10334 Info.ptrVal = I.getArgOperand(0);
10335 Info.offset = 0;
10336 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010337 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010338 Info.readMem = true;
10339 Info.writeMem = false;
10340 return true;
10341 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010342 default:
10343 break;
10344 }
10345
10346 return false;
10347}
Evan Chenge07f85e2012-12-11 23:26:14 +000010348