blob: bdbf45ca04c7eb1c47e822920855edd35ce17efa [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);
784 setOperationAction(ISD::FREM, MVT::f64, Expand);
785 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000786 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
787 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
789 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000790 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 setOperationAction(ISD::FPOW, MVT::f64, Expand);
792 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000793
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000794 if (!Subtarget->hasVFP4()) {
795 setOperationAction(ISD::FMA, MVT::f64, Expand);
796 setOperationAction(ISD::FMA, MVT::f32, Expand);
797 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000798
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000799 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000800 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000801 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
802 if (Subtarget->hasVFP2()) {
803 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
804 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
805 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
806 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
807 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000808 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000809 if (!Subtarget->hasFP16()) {
810 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
811 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000812 }
Evan Cheng110cf482008-04-01 01:50:16 +0000813 }
Evan Chenga8e29892007-01-19 07:51:42 +0000814
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000815 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000816 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000817 setTargetDAGCombine(ISD::ADD);
818 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000819 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000820 setTargetDAGCombine(ISD::AND);
821 setTargetDAGCombine(ISD::OR);
822 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000823
Evan Cheng5fb468a2012-02-23 02:58:19 +0000824 if (Subtarget->hasV6Ops())
825 setTargetDAGCombine(ISD::SRL);
826
Evan Chenga8e29892007-01-19 07:51:42 +0000827 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000828
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000829 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
830 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000831 setSchedulingPreference(Sched::RegPressure);
832 else
833 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000834
Evan Cheng05219282011-01-06 06:52:41 +0000835 //// temporary - rewrite interface to use type
Evan Cheng376642e2012-12-10 23:21:26 +0000836 maxStoresPerMemset = 8;
Lang Hames75757f92011-10-26 20:56:52 +0000837 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Cheng376642e2012-12-10 23:21:26 +0000838 maxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
839 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
840 maxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
841 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengf6799392010-06-26 01:52:05 +0000842
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000843 // On ARM arguments smaller than 4 bytes are extended, so all arguments
844 // are at least 4 bytes aligned.
845 setMinStackArgumentAlignment(4);
846
Evan Chengfff606d2010-09-24 19:07:23 +0000847 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000848
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000849 // Prefer likely predicted branches to selects on out-of-order cores.
Silviu Baranga616471d2012-09-13 15:05:10 +0000850 predictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000851
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000852 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000853}
854
Andrew Trick32cec0a2011-01-19 02:35:27 +0000855// FIXME: It might make sense to define the representative register class as the
856// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
857// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
858// SPR's representative would be DPR_VFP2. This should work well if register
859// pressure tracking were modified such that a register use would increment the
860// pressure of the register class's representative and all of it's super
861// classes' representatives transitively. We have not implemented this because
862// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000863// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000864// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000865std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +0000866ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Evan Cheng4f6b4672010-07-21 06:09:07 +0000867 const TargetRegisterClass *RRC = 0;
868 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +0000869 switch (VT.SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000870 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000871 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000872 // Use DPR as representative register class for all floating point
873 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
874 // the cost is 1 for both f32 and f64.
875 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000876 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000877 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000878 // When NEON is used for SP, only half of the register file is available
879 // because operations that define both SP and DP results will be constrained
880 // to the VFP2 class (D0-D15). We currently model this constraint prior to
881 // coalescing by double-counting the SP regs. See the FIXME above.
882 if (Subtarget->useNEONForSinglePrecisionFP())
883 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000884 break;
885 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
886 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000887 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000888 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000889 break;
890 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000891 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000892 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000893 break;
894 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000895 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000896 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000897 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000898 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000899 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000900}
901
Evan Chenga8e29892007-01-19 07:51:42 +0000902const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
903 switch (Opcode) {
904 default: return 0;
905 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000906 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000907 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000908 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
909 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000910 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000911 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
912 case ARMISD::tCALL: return "ARMISD::tCALL";
913 case ARMISD::BRCOND: return "ARMISD::BRCOND";
914 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000915 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000916 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
917 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
918 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000919 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000920 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000921 case ARMISD::CMPFP: return "ARMISD::CMPFP";
922 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000923 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000924 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000925
Evan Chenga8e29892007-01-19 07:51:42 +0000926 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000927
Jim Grosbach3482c802010-01-18 19:58:49 +0000928 case ARMISD::RBIT: return "ARMISD::RBIT";
929
Bob Wilson76a312b2010-03-19 22:51:32 +0000930 case ARMISD::FTOSI: return "ARMISD::FTOSI";
931 case ARMISD::FTOUI: return "ARMISD::FTOUI";
932 case ARMISD::SITOF: return "ARMISD::SITOF";
933 case ARMISD::UITOF: return "ARMISD::UITOF";
934
Evan Chenga8e29892007-01-19 07:51:42 +0000935 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
936 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
937 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000938
Evan Cheng342e3162011-08-30 01:34:54 +0000939 case ARMISD::ADDC: return "ARMISD::ADDC";
940 case ARMISD::ADDE: return "ARMISD::ADDE";
941 case ARMISD::SUBC: return "ARMISD::SUBC";
942 case ARMISD::SUBE: return "ARMISD::SUBE";
943
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000944 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
945 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000946
Evan Chengc5942082009-10-28 06:55:03 +0000947 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
948 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
949
Dale Johannesen51e28e62010-06-03 21:09:53 +0000950 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000951
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000952 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000953
Evan Cheng86198642009-08-07 00:34:42 +0000954 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
955
Jim Grosbach3728e962009-12-10 00:11:09 +0000956 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000957 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000958
Evan Chengdfed19f2010-11-03 06:34:55 +0000959 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
960
Bob Wilson5bafff32009-06-22 23:27:02 +0000961 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000962 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000963 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000964 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
965 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000966 case ARMISD::VCGEU: return "ARMISD::VCGEU";
967 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000968 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
969 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000970 case ARMISD::VCGTU: return "ARMISD::VCGTU";
971 case ARMISD::VTST: return "ARMISD::VTST";
972
973 case ARMISD::VSHL: return "ARMISD::VSHL";
974 case ARMISD::VSHRs: return "ARMISD::VSHRs";
975 case ARMISD::VSHRu: return "ARMISD::VSHRu";
976 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
977 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
978 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
979 case ARMISD::VSHRN: return "ARMISD::VSHRN";
980 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
981 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
982 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
983 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
984 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
985 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
986 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
987 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
988 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
989 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
990 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
991 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
992 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
993 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000994 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000995 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000996 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000997 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000998 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000999 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +00001000 case ARMISD::VREV64: return "ARMISD::VREV64";
1001 case ARMISD::VREV32: return "ARMISD::VREV32";
1002 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00001003 case ARMISD::VZIP: return "ARMISD::VZIP";
1004 case ARMISD::VUZP: return "ARMISD::VUZP";
1005 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +00001006 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1007 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +00001008 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1009 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00001010 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1011 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +00001012 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +00001013 case ARMISD::FMAX: return "ARMISD::FMAX";
1014 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001015 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001016 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1017 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001018 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001019 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1020 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1021 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001022 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1023 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1024 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1025 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1026 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1027 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1028 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1029 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1030 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1031 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1032 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1033 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1034 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1035 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1036 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1037 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1038 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001039 }
1040}
1041
Duncan Sands28b77e92011-09-06 19:07:46 +00001042EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1043 if (!VT.isVector()) return getPointerTy();
1044 return VT.changeVectorElementTypeToInteger();
1045}
1046
Evan Cheng06b666c2010-05-15 02:18:07 +00001047/// getRegClassFor - Return the register class that should be used for the
1048/// specified value type.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001049const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001050 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1051 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1052 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001053 if (Subtarget->hasNEON()) {
1054 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001055 return &ARM::QQPRRegClass;
1056 if (VT == MVT::v8i64)
1057 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001058 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001059 return TargetLowering::getRegClassFor(VT);
1060}
1061
Eric Christopherab695882010-07-21 22:26:11 +00001062// Create a fast isel object.
1063FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001064ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1065 const TargetLibraryInfo *libInfo) const {
1066 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001067}
1068
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001069/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1070/// be used for loads / stores from the global.
1071unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1072 return (Subtarget->isThumb1Only() ? 127 : 4095);
1073}
1074
Evan Cheng1cc39842010-05-20 23:26:43 +00001075Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001076 unsigned NumVals = N->getNumValues();
1077 if (!NumVals)
1078 return Sched::RegPressure;
1079
1080 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001081 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001082 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001083 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001084 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001085 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001086 }
Evan Chengc10f5432010-05-28 23:25:23 +00001087
1088 if (!N->isMachineOpcode())
1089 return Sched::RegPressure;
1090
1091 // Load are scheduled for latency even if there instruction itinerary
1092 // is not available.
1093 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001094 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001095
Evan Chenge837dea2011-06-28 19:10:37 +00001096 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001097 return Sched::RegPressure;
1098 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001099 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001100 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001101
Evan Cheng1cc39842010-05-20 23:26:43 +00001102 return Sched::RegPressure;
1103}
1104
Evan Chenga8e29892007-01-19 07:51:42 +00001105//===----------------------------------------------------------------------===//
1106// Lowering Code
1107//===----------------------------------------------------------------------===//
1108
Evan Chenga8e29892007-01-19 07:51:42 +00001109/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1110static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1111 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001112 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001113 case ISD::SETNE: return ARMCC::NE;
1114 case ISD::SETEQ: return ARMCC::EQ;
1115 case ISD::SETGT: return ARMCC::GT;
1116 case ISD::SETGE: return ARMCC::GE;
1117 case ISD::SETLT: return ARMCC::LT;
1118 case ISD::SETLE: return ARMCC::LE;
1119 case ISD::SETUGT: return ARMCC::HI;
1120 case ISD::SETUGE: return ARMCC::HS;
1121 case ISD::SETULT: return ARMCC::LO;
1122 case ISD::SETULE: return ARMCC::LS;
1123 }
1124}
1125
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001126/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1127static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001128 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001129 CondCode2 = ARMCC::AL;
1130 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001131 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001132 case ISD::SETEQ:
1133 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1134 case ISD::SETGT:
1135 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1136 case ISD::SETGE:
1137 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1138 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001139 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001140 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1141 case ISD::SETO: CondCode = ARMCC::VC; break;
1142 case ISD::SETUO: CondCode = ARMCC::VS; break;
1143 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1144 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1145 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1146 case ISD::SETLT:
1147 case ISD::SETULT: CondCode = ARMCC::LT; break;
1148 case ISD::SETLE:
1149 case ISD::SETULE: CondCode = ARMCC::LE; break;
1150 case ISD::SETNE:
1151 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1152 }
Evan Chenga8e29892007-01-19 07:51:42 +00001153}
1154
Bob Wilson1f595bb2009-04-17 19:07:39 +00001155//===----------------------------------------------------------------------===//
1156// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001157//===----------------------------------------------------------------------===//
1158
1159#include "ARMGenCallingConv.inc"
1160
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001161/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1162/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001163CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001164 bool Return,
1165 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001166 switch (CC) {
1167 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001168 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001169 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001170 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001171 if (!Subtarget->isAAPCS_ABI())
1172 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1173 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1174 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1175 }
1176 // Fallthrough
1177 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001178 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001179 if (!Subtarget->isAAPCS_ABI())
1180 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1181 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001182 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1183 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001184 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1185 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1186 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001187 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001188 if (!isVarArg)
1189 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1190 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001191 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001192 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001193 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001194 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001195 case CallingConv::GHC:
1196 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001197 }
1198}
1199
Dan Gohman98ca4f22009-08-05 01:29:28 +00001200/// LowerCallResult - Lower the result values of a call into the
1201/// appropriate copies out of appropriate physical registers.
1202SDValue
1203ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001204 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205 const SmallVectorImpl<ISD::InputArg> &Ins,
1206 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001207 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001208
Bob Wilson1f595bb2009-04-17 19:07:39 +00001209 // Assign locations to each value returned by this call.
1210 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001211 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1212 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001214 CCAssignFnForNode(CallConv, /* Return*/ true,
1215 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001216
1217 // Copy all of the result registers out of their specified physreg.
1218 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1219 CCValAssign VA = RVLocs[i];
1220
Bob Wilson80915242009-04-25 00:33:20 +00001221 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001222 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001223 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001224 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001225 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001226 Chain = Lo.getValue(1);
1227 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001228 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001230 InFlag);
1231 Chain = Hi.getValue(1);
1232 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001233 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001234
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 if (VA.getLocVT() == MVT::v2f64) {
1236 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1237 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1238 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001239
1240 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001242 Chain = Lo.getValue(1);
1243 InFlag = Lo.getValue(2);
1244 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001245 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001246 Chain = Hi.getValue(1);
1247 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001248 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1250 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001251 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001252 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001253 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1254 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001255 Chain = Val.getValue(1);
1256 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001257 }
Bob Wilson80915242009-04-25 00:33:20 +00001258
1259 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001260 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001261 case CCValAssign::Full: break;
1262 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001263 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001264 break;
1265 }
1266
Dan Gohman98ca4f22009-08-05 01:29:28 +00001267 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 }
1269
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001271}
1272
Bob Wilsondee46d72009-04-17 20:35:10 +00001273/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001274SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1276 SDValue StackPtr, SDValue Arg,
1277 DebugLoc dl, SelectionDAG &DAG,
1278 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001279 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001280 unsigned LocMemOffset = VA.getLocMemOffset();
1281 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1282 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001283 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001284 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001285 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001286}
1287
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001289 SDValue Chain, SDValue &Arg,
1290 RegsToPassVector &RegsToPass,
1291 CCValAssign &VA, CCValAssign &NextVA,
1292 SDValue &StackPtr,
1293 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001294 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001295
Jim Grosbache5165492009-11-09 00:11:35 +00001296 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001298 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1299
1300 if (NextVA.isRegLoc())
1301 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1302 else {
1303 assert(NextVA.isMemLoc());
1304 if (StackPtr.getNode() == 0)
1305 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1306
Dan Gohman98ca4f22009-08-05 01:29:28 +00001307 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1308 dl, DAG, NextVA,
1309 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001310 }
1311}
1312
Dan Gohman98ca4f22009-08-05 01:29:28 +00001313/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001314/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1315/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001316SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001317ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001318 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001319 SelectionDAG &DAG = CLI.DAG;
1320 DebugLoc &dl = CLI.DL;
1321 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1322 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1323 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1324 SDValue Chain = CLI.Chain;
1325 SDValue Callee = CLI.Callee;
1326 bool &isTailCall = CLI.IsTailCall;
1327 CallingConv::ID CallConv = CLI.CallConv;
1328 bool doesNotRet = CLI.DoesNotReturn;
1329 bool isVarArg = CLI.IsVarArg;
1330
Dale Johannesen51e28e62010-06-03 21:09:53 +00001331 MachineFunction &MF = DAG.getMachineFunction();
1332 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1333 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001334 // Disable tail calls if they're not supported.
1335 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001336 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001337 if (isTailCall) {
1338 // Check if it's really possible to do a tail call.
1339 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1340 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001341 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001342 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1343 // detected sibcalls.
1344 if (isTailCall) {
1345 ++NumTailCalls;
1346 IsSibCall = true;
1347 }
1348 }
Evan Chenga8e29892007-01-19 07:51:42 +00001349
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350 // Analyze operands of the call, assigning locations to each operand.
1351 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001352 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1353 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001355 CCAssignFnForNode(CallConv, /* Return*/ false,
1356 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001357
Bob Wilson1f595bb2009-04-17 19:07:39 +00001358 // Get a count of how many bytes are to be pushed on the stack.
1359 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001360
Dale Johannesen51e28e62010-06-03 21:09:53 +00001361 // For tail calls, memory operands are available in our caller's stack.
1362 if (IsSibCall)
1363 NumBytes = 0;
1364
Evan Chenga8e29892007-01-19 07:51:42 +00001365 // Adjust the stack pointer for the new arguments...
1366 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001367 if (!IsSibCall)
1368 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001369
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001370 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001371
Bob Wilson5bafff32009-06-22 23:27:02 +00001372 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001373 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001374
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001376 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001377 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1378 i != e;
1379 ++i, ++realArgIdx) {
1380 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001381 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001383 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001384
Bob Wilson1f595bb2009-04-17 19:07:39 +00001385 // Promote the value if needed.
1386 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001387 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001388 case CCValAssign::Full: break;
1389 case CCValAssign::SExt:
1390 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1391 break;
1392 case CCValAssign::ZExt:
1393 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1394 break;
1395 case CCValAssign::AExt:
1396 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1397 break;
1398 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001399 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001400 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001401 }
1402
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001403 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001404 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001405 if (VA.getLocVT() == MVT::v2f64) {
1406 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1407 DAG.getConstant(0, MVT::i32));
1408 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1409 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001410
Dan Gohman98ca4f22009-08-05 01:29:28 +00001411 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001412 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1413
1414 VA = ArgLocs[++i]; // skip ahead to next loc
1415 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001416 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001417 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1418 } else {
1419 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001420
Dan Gohman98ca4f22009-08-05 01:29:28 +00001421 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1422 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001423 }
1424 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001425 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001426 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001427 }
1428 } else if (VA.isRegLoc()) {
1429 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001430 } else if (isByVal) {
1431 assert(VA.isMemLoc());
1432 unsigned offset = 0;
1433
1434 // True if this byval aggregate will be split between registers
1435 // and memory.
1436 if (CCInfo.isFirstByValRegValid()) {
1437 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1438 unsigned int i, j;
1439 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1440 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1441 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1442 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1443 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001444 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001445 MemOpChains.push_back(Load.getValue(1));
1446 RegsToPass.push_back(std::make_pair(j, Load));
1447 }
1448 offset = ARM::R4 - CCInfo.getFirstByValReg();
1449 CCInfo.clearFirstByValReg();
1450 }
1451
Manman Ren763a75d2012-06-01 02:44:42 +00001452 if (Flags.getByValSize() - 4*offset > 0) {
1453 unsigned LocMemOffset = VA.getLocMemOffset();
1454 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1455 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1456 StkPtrOff);
1457 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1458 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1459 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1460 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001461 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001462
Manman Ren763a75d2012-06-01 02:44:42 +00001463 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001464 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001465 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1466 Ops, array_lengthof(Ops)));
1467 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001468 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001469 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001470
Dan Gohman98ca4f22009-08-05 01:29:28 +00001471 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1472 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001473 }
Evan Chenga8e29892007-01-19 07:51:42 +00001474 }
1475
1476 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001477 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001478 &MemOpChains[0], MemOpChains.size());
1479
1480 // Build a sequence of copy-to-reg nodes chained together with token chain
1481 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001482 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001483 // Tail call byval lowering might overwrite argument registers so in case of
1484 // tail call optimization the copies to registers are lowered later.
1485 if (!isTailCall)
1486 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1487 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1488 RegsToPass[i].second, InFlag);
1489 InFlag = Chain.getValue(1);
1490 }
Evan Chenga8e29892007-01-19 07:51:42 +00001491
Dale Johannesen51e28e62010-06-03 21:09:53 +00001492 // For tail calls lower the arguments to the 'real' stack slot.
1493 if (isTailCall) {
1494 // Force all the incoming stack arguments to be loaded from the stack
1495 // before any new outgoing arguments are stored to the stack, because the
1496 // outgoing stack slots may alias the incoming argument stack slots, and
1497 // the alias isn't otherwise explicit. This is slightly more conservative
1498 // than necessary, because it means that each store effectively depends
1499 // on every argument instead of just those arguments it would clobber.
1500
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001501 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001502 InFlag = SDValue();
1503 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1504 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1505 RegsToPass[i].second, InFlag);
1506 InFlag = Chain.getValue(1);
1507 }
1508 InFlag =SDValue();
1509 }
1510
Bill Wendling056292f2008-09-16 21:48:12 +00001511 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1512 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1513 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001514 bool isDirect = false;
1515 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001516 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001517 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001518
1519 if (EnableARMLongCalls) {
1520 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1521 && "long-calls with non-static relocation model!");
1522 // Handle a global address or an external symbol. If it's not one of
1523 // those, the target's already in a register, so we don't need to do
1524 // anything extra.
1525 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001526 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001527 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001528 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001529 ARMConstantPoolValue *CPV =
1530 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1531
Jim Grosbache7b52522010-04-14 22:28:31 +00001532 // Get the address of the callee into a register
1533 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1534 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1535 Callee = DAG.getLoad(getPointerTy(), dl,
1536 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001537 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001538 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001539 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1540 const char *Sym = S->getSymbol();
1541
1542 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001543 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001544 ARMConstantPoolValue *CPV =
1545 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1546 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001547 // Get the address of the callee into a register
1548 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1549 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1550 Callee = DAG.getLoad(getPointerTy(), dl,
1551 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001552 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001553 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001554 }
1555 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001556 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001557 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001558 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001559 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001560 getTargetMachine().getRelocationModel() != Reloc::Static;
1561 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001562 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001563 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001564 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001565 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001566 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001567 ARMConstantPoolValue *CPV =
1568 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001569 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001571 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001572 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001573 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001574 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001575 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001576 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001577 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001578 } else {
1579 // On ELF targets for PIC code, direct calls should go through the PLT
1580 unsigned OpFlags = 0;
1581 if (Subtarget->isTargetELF() &&
1582 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1583 OpFlags = ARMII::MO_PLT;
1584 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1585 }
Bill Wendling056292f2008-09-16 21:48:12 +00001586 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001587 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001588 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001589 getTargetMachine().getRelocationModel() != Reloc::Static;
1590 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001591 // tBX takes a register source operand.
1592 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001593 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001594 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001595 ARMConstantPoolValue *CPV =
1596 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1597 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001598 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001599 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001600 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001601 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001602 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001603 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001604 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001605 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001606 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001607 } else {
1608 unsigned OpFlags = 0;
1609 // On ELF targets for PIC code, direct calls should go through the PLT
1610 if (Subtarget->isTargetELF() &&
1611 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1612 OpFlags = ARMII::MO_PLT;
1613 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1614 }
Evan Chenga8e29892007-01-19 07:51:42 +00001615 }
1616
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001617 // FIXME: handle tail calls differently.
1618 unsigned CallOpc;
Bill Wendling831737d2012-12-30 10:32:01 +00001619 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1620 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001621 if (Subtarget->isThumb()) {
1622 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001623 CallOpc = ARMISD::CALL_NOLINK;
1624 else
1625 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1626 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001627 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001628 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001629 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001630 // Emit regular call when code size is the priority
1631 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001632 // "mov lr, pc; b _foo" to avoid confusing the RSP
1633 CallOpc = ARMISD::CALL_NOLINK;
1634 else
1635 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001636 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001637
Dan Gohman475871a2008-07-27 21:46:04 +00001638 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001639 Ops.push_back(Chain);
1640 Ops.push_back(Callee);
1641
1642 // Add argument registers to the end of the list so that they are known live
1643 // into the call.
1644 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1645 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1646 RegsToPass[i].second.getValueType()));
1647
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001648 // Add a register mask operand representing the call-preserved registers.
1649 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1650 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1651 assert(Mask && "Missing call preserved mask for calling convention");
1652 Ops.push_back(DAG.getRegisterMask(Mask));
1653
Gabor Greifba36cb52008-08-28 21:40:38 +00001654 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001655 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001656
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001657 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001658 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001659 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001660
Duncan Sands4bdcb612008-07-02 17:40:58 +00001661 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001662 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001663 InFlag = Chain.getValue(1);
1664
Chris Lattnere563bbc2008-10-11 22:08:30 +00001665 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1666 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001667 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001668 InFlag = Chain.getValue(1);
1669
Bob Wilson1f595bb2009-04-17 19:07:39 +00001670 // Handle result values, copying them out of physregs into vregs that we
1671 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001672 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1673 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001674}
1675
Stuart Hastingsf222e592011-02-28 17:17:53 +00001676/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001677/// on the stack. Remember the next parameter register to allocate,
1678/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001679/// this.
1680void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001681ARMTargetLowering::HandleByVal(
1682 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001683 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1684 assert((State->getCallOrPrologue() == Prologue ||
1685 State->getCallOrPrologue() == Call) &&
1686 "unhandled ParmContext");
1687 if ((!State->isFirstByValRegValid()) &&
1688 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001689 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1690 unsigned AlignInRegs = Align / 4;
1691 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1692 for (unsigned i = 0; i < Waste; ++i)
1693 reg = State->AllocateReg(GPRArgRegs, 4);
1694 }
1695 if (reg != 0) {
1696 State->setFirstByValReg(reg);
1697 // At a call site, a byval parameter that is split between
1698 // registers and memory needs its size truncated here. In a
1699 // function prologue, such byval parameters are reassembled in
1700 // memory, and are not truncated.
1701 if (State->getCallOrPrologue() == Call) {
1702 unsigned excess = 4 * (ARM::R4 - reg);
1703 assert(size >= excess && "expected larger existing stack allocation");
1704 size -= excess;
1705 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001706 }
1707 }
1708 // Confiscate any remaining parameter registers to preclude their
1709 // assignment to subsequent parameters.
1710 while (State->AllocateReg(GPRArgRegs, 4))
1711 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001712}
1713
Dale Johannesen51e28e62010-06-03 21:09:53 +00001714/// MatchingStackOffset - Return true if the given stack call argument is
1715/// already available in the same position (relatively) of the caller's
1716/// incoming argument stack.
1717static
1718bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1719 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001720 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001721 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1722 int FI = INT_MAX;
1723 if (Arg.getOpcode() == ISD::CopyFromReg) {
1724 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001725 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001726 return false;
1727 MachineInstr *Def = MRI->getVRegDef(VR);
1728 if (!Def)
1729 return false;
1730 if (!Flags.isByVal()) {
1731 if (!TII->isLoadFromStackSlot(Def, FI))
1732 return false;
1733 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001734 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001735 }
1736 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1737 if (Flags.isByVal())
1738 // ByVal argument is passed in as a pointer but it's now being
1739 // dereferenced. e.g.
1740 // define @foo(%struct.X* %A) {
1741 // tail call @bar(%struct.X* byval %A)
1742 // }
1743 return false;
1744 SDValue Ptr = Ld->getBasePtr();
1745 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1746 if (!FINode)
1747 return false;
1748 FI = FINode->getIndex();
1749 } else
1750 return false;
1751
1752 assert(FI != INT_MAX);
1753 if (!MFI->isFixedObjectIndex(FI))
1754 return false;
1755 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1756}
1757
1758/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1759/// for tail call optimization. Targets which want to do tail call
1760/// optimization should implement this function.
1761bool
1762ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1763 CallingConv::ID CalleeCC,
1764 bool isVarArg,
1765 bool isCalleeStructRet,
1766 bool isCallerStructRet,
1767 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001768 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001769 const SmallVectorImpl<ISD::InputArg> &Ins,
1770 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001771 const Function *CallerF = DAG.getMachineFunction().getFunction();
1772 CallingConv::ID CallerCC = CallerF->getCallingConv();
1773 bool CCMatch = CallerCC == CalleeCC;
1774
1775 // Look for obvious safe cases to perform tail call optimization that do not
1776 // require ABI changes. This is what gcc calls sibcall.
1777
Jim Grosbach7616b642010-06-16 23:45:49 +00001778 // Do not sibcall optimize vararg calls unless the call site is not passing
1779 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001780 if (isVarArg && !Outs.empty())
1781 return false;
1782
1783 // Also avoid sibcall optimization if either caller or callee uses struct
1784 // return semantics.
1785 if (isCalleeStructRet || isCallerStructRet)
1786 return false;
1787
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001788 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001789 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1790 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1791 // support in the assembler and linker to be used. This would need to be
1792 // fixed to fully support tail calls in Thumb1.
1793 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001794 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1795 // LR. This means if we need to reload LR, it takes an extra instructions,
1796 // which outweighs the value of the tail call; but here we don't know yet
1797 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001798 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001799 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001800
1801 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1802 // but we need to make sure there are enough registers; the only valid
1803 // registers are the 4 used for parameters. We don't currently do this
1804 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001805 if (Subtarget->isThumb1Only())
1806 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001807
Dale Johannesen51e28e62010-06-03 21:09:53 +00001808 // If the calling conventions do not match, then we'd better make sure the
1809 // results are returned in the same way as what the caller expects.
1810 if (!CCMatch) {
1811 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001812 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1813 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001814 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1815
1816 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001817 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1818 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001819 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1820
1821 if (RVLocs1.size() != RVLocs2.size())
1822 return false;
1823 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1824 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1825 return false;
1826 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1827 return false;
1828 if (RVLocs1[i].isRegLoc()) {
1829 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1830 return false;
1831 } else {
1832 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1833 return false;
1834 }
1835 }
1836 }
1837
Manman Rene6c3cc82012-10-12 23:39:43 +00001838 // If Caller's vararg or byval argument has been split between registers and
1839 // stack, do not perform tail call, since part of the argument is in caller's
1840 // local frame.
1841 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1842 getInfo<ARMFunctionInfo>();
1843 if (AFI_Caller->getVarArgsRegSaveSize())
1844 return false;
1845
Dale Johannesen51e28e62010-06-03 21:09:53 +00001846 // If the callee takes no arguments then go on to check the results of the
1847 // call.
1848 if (!Outs.empty()) {
1849 // Check if stack adjustment is needed. For now, do not do this if any
1850 // argument is passed on the stack.
1851 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001852 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1853 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001854 CCInfo.AnalyzeCallOperands(Outs,
1855 CCAssignFnForNode(CalleeCC, false, isVarArg));
1856 if (CCInfo.getNextStackOffset()) {
1857 MachineFunction &MF = DAG.getMachineFunction();
1858
1859 // Check if the arguments are already laid out in the right way as
1860 // the caller's fixed stack objects.
1861 MachineFrameInfo *MFI = MF.getFrameInfo();
1862 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001863 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001864 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1865 i != e;
1866 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001867 CCValAssign &VA = ArgLocs[i];
1868 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001869 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001870 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001871 if (VA.getLocInfo() == CCValAssign::Indirect)
1872 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001873 if (VA.needsCustom()) {
1874 // f64 and vector types are split into multiple registers or
1875 // register/stack-slot combinations. The types will not match
1876 // the registers; give up on memory f64 refs until we figure
1877 // out what to do about this.
1878 if (!VA.isRegLoc())
1879 return false;
1880 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001881 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001882 if (RegVT == MVT::v2f64) {
1883 if (!ArgLocs[++i].isRegLoc())
1884 return false;
1885 if (!ArgLocs[++i].isRegLoc())
1886 return false;
1887 }
1888 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001889 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1890 MFI, MRI, TII))
1891 return false;
1892 }
1893 }
1894 }
1895 }
1896
1897 return true;
1898}
1899
Benjamin Kramer350c0082012-11-28 20:55:10 +00001900bool
1901ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
1902 MachineFunction &MF, bool isVarArg,
1903 const SmallVectorImpl<ISD::OutputArg> &Outs,
1904 LLVMContext &Context) const {
1905 SmallVector<CCValAssign, 16> RVLocs;
1906 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
1907 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
1908 isVarArg));
1909}
1910
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911SDValue
1912ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001913 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001915 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001916 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001917
Bob Wilsondee46d72009-04-17 20:35:10 +00001918 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001919 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001920
Bob Wilsondee46d72009-04-17 20:35:10 +00001921 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001922 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1923 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001924
Dan Gohman98ca4f22009-08-05 01:29:28 +00001925 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001926 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1927 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001928
1929 // If this is the first return lowered for this function, add
1930 // the regs to the liveout set for the function.
1931 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1932 for (unsigned i = 0; i != RVLocs.size(); ++i)
1933 if (RVLocs[i].isRegLoc())
1934 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001935 }
1936
Bob Wilson1f595bb2009-04-17 19:07:39 +00001937 SDValue Flag;
1938
1939 // Copy the result values into the output registers.
1940 for (unsigned i = 0, realRVLocIdx = 0;
1941 i != RVLocs.size();
1942 ++i, ++realRVLocIdx) {
1943 CCValAssign &VA = RVLocs[i];
1944 assert(VA.isRegLoc() && "Can only return in registers!");
1945
Dan Gohmanc9403652010-07-07 15:54:55 +00001946 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001947
1948 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001949 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001950 case CCValAssign::Full: break;
1951 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001952 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001953 break;
1954 }
1955
Bob Wilson1f595bb2009-04-17 19:07:39 +00001956 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001957 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001958 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1960 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001961 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001963
1964 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1965 Flag = Chain.getValue(1);
1966 VA = RVLocs[++i]; // skip ahead to next loc
1967 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1968 HalfGPRs.getValue(1), Flag);
1969 Flag = Chain.getValue(1);
1970 VA = RVLocs[++i]; // skip ahead to next loc
1971
1972 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001973 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1974 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001975 }
1976 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1977 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001978 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001980 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001981 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001982 VA = RVLocs[++i]; // skip ahead to next loc
1983 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1984 Flag);
1985 } else
1986 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1987
Bob Wilsondee46d72009-04-17 20:35:10 +00001988 // Guarantee that all emitted copies are
1989 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001990 Flag = Chain.getValue(1);
1991 }
1992
1993 SDValue result;
1994 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001996 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001998
1999 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00002000}
2001
Evan Chengbf010eb2012-04-10 01:51:00 +00002002bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002003 if (N->getNumValues() != 1)
2004 return false;
2005 if (!N->hasNUsesOfValue(1, 0))
2006 return false;
2007
Evan Chengbf010eb2012-04-10 01:51:00 +00002008 SDValue TCChain = Chain;
2009 SDNode *Copy = *N->use_begin();
2010 if (Copy->getOpcode() == ISD::CopyToReg) {
2011 // If the copy has a glue operand, we conservatively assume it isn't safe to
2012 // perform a tail call.
2013 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2014 return false;
2015 TCChain = Copy->getOperand(0);
2016 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2017 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002018 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00002019 SmallPtrSet<SDNode*, 2> Copies;
2020 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00002021 UI != UE; ++UI) {
2022 if (UI->getOpcode() != ISD::CopyToReg)
2023 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002024 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002025 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002026 if (Copies.size() > 2)
2027 return false;
2028
2029 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2030 UI != UE; ++UI) {
2031 SDValue UseChain = UI->getOperand(0);
2032 if (Copies.count(UseChain.getNode()))
2033 // Second CopyToReg
2034 Copy = *UI;
2035 else
2036 // First CopyToReg
2037 TCChain = UseChain;
2038 }
2039 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002040 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002041 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002042 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002043 Copy = *Copy->use_begin();
2044 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002045 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002046 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002047 } else {
2048 return false;
2049 }
2050
Evan Cheng1bf891a2010-12-01 22:59:46 +00002051 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002052 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2053 UI != UE; ++UI) {
2054 if (UI->getOpcode() != ARMISD::RET_FLAG)
2055 return false;
2056 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002057 }
2058
Evan Chengbf010eb2012-04-10 01:51:00 +00002059 if (!HasRet)
2060 return false;
2061
2062 Chain = TCChain;
2063 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002064}
2065
Evan Cheng485fafc2011-03-21 01:19:09 +00002066bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002067 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002068 return false;
2069
2070 if (!CI->isTailCall())
2071 return false;
2072
2073 return !Subtarget->isThumb1Only();
2074}
2075
Bob Wilsonb62d2572009-11-03 00:02:05 +00002076// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2077// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2078// one of the above mentioned nodes. It has to be wrapped because otherwise
2079// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2080// be used to form addressing mode. These wrapped nodes will be selected
2081// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002082static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002083 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002084 // FIXME there is no actual debug info here
2085 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002086 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002087 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002088 if (CP->isMachineConstantPoolEntry())
2089 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2090 CP->getAlignment());
2091 else
2092 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2093 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002095}
2096
Jim Grosbache1102ca2010-07-19 17:20:38 +00002097unsigned ARMTargetLowering::getJumpTableEncoding() const {
2098 return MachineJumpTableInfo::EK_Inline;
2099}
2100
Dan Gohmand858e902010-04-17 15:26:15 +00002101SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2102 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002103 MachineFunction &MF = DAG.getMachineFunction();
2104 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2105 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002106 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002107 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002108 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002109 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2110 SDValue CPAddr;
2111 if (RelocM == Reloc::Static) {
2112 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2113 } else {
2114 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002115 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002116 ARMConstantPoolValue *CPV =
2117 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2118 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002119 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2120 }
2121 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2122 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002123 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002124 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002125 if (RelocM == Reloc::Static)
2126 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002127 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002128 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002129}
2130
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002131// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002132SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002134 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002135 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002136 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002137 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 MachineFunction &MF = DAG.getMachineFunction();
2139 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002140 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002141 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002142 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2143 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002144 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002145 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002146 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002147 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002148 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002150
Evan Chenge7e0d622009-11-06 22:24:13 +00002151 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002153
2154 // call __tls_get_addr.
2155 ArgListTy Args;
2156 ArgListEntry Entry;
2157 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002158 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002160 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002161 TargetLowering::CallLoweringInfo CLI(Chain,
2162 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002163 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002164 0, CallingConv::C, /*isTailCall=*/false,
2165 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002166 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002167 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002168 return CallResult.first;
2169}
2170
2171// Lower ISD::GlobalTLSAddress using the "initial exec" or
2172// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002173SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002175 SelectionDAG &DAG,
2176 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002177 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002178 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002179 SDValue Offset;
2180 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002181 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002182 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002183 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002184
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002185 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002186 MachineFunction &MF = DAG.getMachineFunction();
2187 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002188 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002189 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002190 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2191 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002192 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2193 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2194 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002195 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002196 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002197 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002198 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002199 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002200 Chain = Offset.getValue(1);
2201
Evan Chenge7e0d622009-11-06 22:24:13 +00002202 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002203 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002204
Evan Cheng9eda6892009-10-31 03:39:36 +00002205 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002206 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002207 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002208 } else {
2209 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002210 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002211 ARMConstantPoolValue *CPV =
2212 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002213 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002214 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002215 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002216 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002217 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002218 }
2219
2220 // The address of the thread local variable is the add of the thread
2221 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002222 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002223}
2224
Dan Gohman475871a2008-07-27 21:46:04 +00002225SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002226ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002227 // TODO: implement the "local dynamic" model
2228 assert(Subtarget->isTargetELF() &&
2229 "TLS not implemented for non-ELF targets");
2230 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002231
2232 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2233
2234 switch (model) {
2235 case TLSModel::GeneralDynamic:
2236 case TLSModel::LocalDynamic:
2237 return LowerToTLSGeneralDynamicModel(GA, DAG);
2238 case TLSModel::InitialExec:
2239 case TLSModel::LocalExec:
2240 return LowerToTLSExecModels(GA, DAG, model);
2241 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002242 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002243}
2244
Dan Gohman475871a2008-07-27 21:46:04 +00002245SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002246 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002247 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002248 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002249 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002250 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2251 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002252 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002253 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002254 ARMConstantPoolConstant::Create(GV,
2255 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002256 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002257 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002258 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002259 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002260 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002261 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002262 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002263 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002264 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002265 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002266 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002267 MachinePointerInfo::getGOT(),
2268 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002269 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002270 }
2271
2272 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002273 // pair. This is always cheaper.
2274 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002275 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002276 // FIXME: Once remat is capable of dealing with instructions with register
2277 // operands, expand this into two nodes.
2278 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2279 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002280 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002281 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2282 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2283 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2284 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002285 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002286 }
2287}
2288
Dan Gohman475871a2008-07-27 21:46:04 +00002289SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002290 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002291 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002292 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002293 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002294 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002295 MachineFunction &MF = DAG.getMachineFunction();
2296 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2297
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002298 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2299 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002300 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002301 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002302 // FIXME: Once remat is capable of dealing with instructions with register
2303 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002304 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002305 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2306 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2307
Evan Cheng53519f02011-01-21 18:55:51 +00002308 unsigned Wrapper = (RelocM == Reloc::PIC_)
2309 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2310 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002311 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002312 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2313 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002314 MachinePointerInfo::getGOT(),
2315 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002316 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002317 }
2318
2319 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002320 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002321 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002322 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002323 } else {
2324 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002325 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2326 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002327 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2328 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002329 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002330 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002331 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002332
Evan Cheng9eda6892009-10-31 03:39:36 +00002333 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002334 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002335 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002336 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002337
2338 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002339 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002340 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002341 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002342
Evan Cheng63476a82009-09-03 07:04:02 +00002343 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002344 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002345 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002346
2347 return Result;
2348}
2349
Dan Gohman475871a2008-07-27 21:46:04 +00002350SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002351 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002352 assert(Subtarget->isTargetELF() &&
2353 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002354 MachineFunction &MF = DAG.getMachineFunction();
2355 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002356 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002357 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002358 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002359 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002360 ARMConstantPoolValue *CPV =
2361 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2362 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002363 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002364 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002365 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002366 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002367 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002368 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002369 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002370}
2371
Jim Grosbach0e0da732009-05-12 23:59:14 +00002372SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002373ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2374 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002375 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002376 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2377 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002378 Op.getOperand(1), Val);
2379}
2380
2381SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002382ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2383 DebugLoc dl = Op.getDebugLoc();
2384 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2385 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2386}
2387
2388SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002389ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002390 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002391 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002392 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002393 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002394 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002395 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002396 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002397 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2398 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002399 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002400 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002401 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002402 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002403 EVT PtrVT = getPointerTy();
2404 DebugLoc dl = Op.getDebugLoc();
2405 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2406 SDValue CPAddr;
2407 unsigned PCAdj = (RelocM != Reloc::PIC_)
2408 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002409 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002410 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2411 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002412 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002413 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002414 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002415 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002416 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002417 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002418
2419 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002420 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002421 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2422 }
2423 return Result;
2424 }
Evan Cheng92e39162011-03-29 23:06:19 +00002425 case Intrinsic::arm_neon_vmulls:
2426 case Intrinsic::arm_neon_vmullu: {
2427 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2428 ? ARMISD::VMULLs : ARMISD::VMULLu;
2429 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2430 Op.getOperand(1), Op.getOperand(2));
2431 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002432 }
2433}
2434
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002435static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002436 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002437 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002438 if (!Subtarget->hasDataBarrier()) {
2439 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2440 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2441 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002442 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002443 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002444 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002445 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002446 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002447
2448 SDValue Op5 = Op.getOperand(5);
2449 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2450 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2451 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2452 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2453
2454 ARM_MB::MemBOpt DMBOpt;
2455 if (isDeviceBarrier)
2456 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2457 else
2458 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2459 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2460 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002461}
2462
Eli Friedman26689ac2011-08-03 21:06:02 +00002463
2464static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2465 const ARMSubtarget *Subtarget) {
2466 // FIXME: handle "fence singlethread" more efficiently.
2467 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002468 if (!Subtarget->hasDataBarrier()) {
2469 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2470 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2471 // here.
2472 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2473 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002474 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002475 DAG.getConstant(0, MVT::i32));
2476 }
2477
Eli Friedman26689ac2011-08-03 21:06:02 +00002478 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002479 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002480}
2481
Evan Chengdfed19f2010-11-03 06:34:55 +00002482static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2483 const ARMSubtarget *Subtarget) {
2484 // ARM pre v5TE and Thumb1 does not have preload instructions.
2485 if (!(Subtarget->isThumb2() ||
2486 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2487 // Just preserve the chain.
2488 return Op.getOperand(0);
2489
2490 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002491 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2492 if (!isRead &&
2493 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2494 // ARMv7 with MP extension has PLDW.
2495 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002496
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002497 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2498 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002499 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002500 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002501 isData = ~isData & 1;
2502 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002503
2504 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002505 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2506 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002507}
2508
Dan Gohman1e93df62010-04-17 14:41:14 +00002509static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2510 MachineFunction &MF = DAG.getMachineFunction();
2511 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2512
Evan Chenga8e29892007-01-19 07:51:42 +00002513 // vastart just stores the address of the VarArgsFrameIndex slot into the
2514 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002515 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002516 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002517 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002518 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002519 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2520 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002521}
2522
Dan Gohman475871a2008-07-27 21:46:04 +00002523SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002524ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2525 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002526 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002527 MachineFunction &MF = DAG.getMachineFunction();
2528 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2529
Craig Topper44d23822012-02-22 05:59:10 +00002530 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002531 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002532 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002533 else
Craig Topper420761a2012-04-20 07:30:17 +00002534 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002535
2536 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002537 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002538 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002539
2540 SDValue ArgValue2;
2541 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002542 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002543 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002544
2545 // Create load node to retrieve arguments from the stack.
2546 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002547 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002548 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002549 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002550 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002551 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002552 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002553 }
2554
Jim Grosbache5165492009-11-09 00:11:35 +00002555 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002556}
2557
Stuart Hastingsc7315872011-04-20 16:47:52 +00002558void
2559ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2560 unsigned &VARegSize, unsigned &VARegSaveSize)
2561 const {
2562 unsigned NumGPRs;
2563 if (CCInfo.isFirstByValRegValid())
2564 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2565 else {
2566 unsigned int firstUnalloced;
2567 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2568 sizeof(GPRArgRegs) /
2569 sizeof(GPRArgRegs[0]));
2570 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2571 }
2572
2573 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2574 VARegSize = NumGPRs * 4;
2575 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2576}
2577
2578// The remaining GPRs hold either the beginning of variable-argument
2579// data, or the beginning of an aggregate passed by value (usuall
2580// byval). Either way, we allocate stack slots adjacent to the data
2581// provided by our caller, and store the unallocated registers there.
2582// If this is a variadic function, the va_list pointer will begin with
2583// these values; otherwise, this reassembles a (byval) structure that
2584// was split between registers and memory.
2585void
2586ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2587 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002588 const Value *OrigArg,
2589 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002590 unsigned ArgOffset,
2591 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002592 MachineFunction &MF = DAG.getMachineFunction();
2593 MachineFrameInfo *MFI = MF.getFrameInfo();
2594 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2595 unsigned firstRegToSaveIndex;
2596 if (CCInfo.isFirstByValRegValid())
2597 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2598 else {
2599 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2600 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2601 }
2602
2603 unsigned VARegSize, VARegSaveSize;
2604 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2605 if (VARegSaveSize) {
2606 // If this function is vararg, store any remaining integer argument regs
2607 // to their spots on the stack so that they may be loaded by deferencing
2608 // the result of va_next.
2609 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002610 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2611 ArgOffset + VARegSaveSize
2612 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002613 false));
2614 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2615 getPointerTy());
2616
2617 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002618 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002619 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002620 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002621 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002622 else
Craig Topper420761a2012-04-20 07:30:17 +00002623 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002624
2625 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2626 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2627 SDValue Store =
2628 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002629 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002630 false, false, 0);
2631 MemOps.push_back(Store);
2632 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2633 DAG.getConstant(4, getPointerTy()));
2634 }
2635 if (!MemOps.empty())
2636 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2637 &MemOps[0], MemOps.size());
2638 } else
2639 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002640 AFI->setVarArgsFrameIndex(
2641 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002642}
2643
Bob Wilson5bafff32009-06-22 23:27:02 +00002644SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002645ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002646 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002647 const SmallVectorImpl<ISD::InputArg>
2648 &Ins,
2649 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002650 SmallVectorImpl<SDValue> &InVals)
2651 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002652 MachineFunction &MF = DAG.getMachineFunction();
2653 MachineFrameInfo *MFI = MF.getFrameInfo();
2654
Bob Wilson1f595bb2009-04-17 19:07:39 +00002655 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2656
2657 // Assign locations to all of the incoming arguments.
2658 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002659 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2660 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002661 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002662 CCAssignFnForNode(CallConv, /* Return*/ false,
2663 isVarArg));
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002664
Bob Wilson1f595bb2009-04-17 19:07:39 +00002665 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002666 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002667 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002668 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2669 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002670 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2671 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002672 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2673 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002674 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002675 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002676 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002677
Bob Wilson1f595bb2009-04-17 19:07:39 +00002678 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002679 // f64 and vector types are split up into multiple registers or
2680 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002682 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002683 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002684 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002685 SDValue ArgValue2;
2686 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002687 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002688 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2689 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002690 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002691 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002692 } else {
2693 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2694 Chain, DAG, dl);
2695 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002696 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2697 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002698 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002699 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002700 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2701 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002702 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002703
Bob Wilson5bafff32009-06-22 23:27:02 +00002704 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002705 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002706
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002708 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002709 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002710 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002711 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002712 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002713 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002714 RC = AFI->isThumb1OnlyFunction() ?
2715 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2716 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002717 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002718 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002719
2720 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002721 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002722 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002723 }
2724
2725 // If this is an 8 or 16-bit value, it is really passed promoted
2726 // to 32 bits. Insert an assert[sz]ext to capture this, then
2727 // truncate to the right size.
2728 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002729 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002730 case CCValAssign::Full: break;
2731 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002732 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002733 break;
2734 case CCValAssign::SExt:
2735 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2736 DAG.getValueType(VA.getValVT()));
2737 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2738 break;
2739 case CCValAssign::ZExt:
2740 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2741 DAG.getValueType(VA.getValVT()));
2742 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2743 break;
2744 }
2745
Dan Gohman98ca4f22009-08-05 01:29:28 +00002746 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002747
2748 } else { // VA.isRegLoc()
2749
2750 // sanity check
2751 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002752 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002753
Stuart Hastingsf222e592011-02-28 17:17:53 +00002754 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002755
Stuart Hastingsf222e592011-02-28 17:17:53 +00002756 // Some Ins[] entries become multiple ArgLoc[] entries.
2757 // Process them only once.
2758 if (index != lastInsIndex)
2759 {
2760 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002761 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002762 // This can be changed with more analysis.
2763 // In case of tail call optimization mark all arguments mutable.
2764 // Since they could be overwritten by lowering of arguments in case of
2765 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002766 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002767 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2768 if (!AFI->getVarArgsFrameIndex()) {
2769 VarArgStyleRegisters(CCInfo, DAG,
2770 dl, Chain, CurOrigArg,
2771 Ins[VA.getValNo()].PartOffset,
2772 VA.getLocMemOffset(),
2773 true /*force mutable frames*/);
2774 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2775 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2776 } else {
2777 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2778 VA.getLocMemOffset(), false);
2779 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2780 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002781 } else {
2782 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2783 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002784
Stuart Hastingsf222e592011-02-28 17:17:53 +00002785 // Create load nodes to retrieve arguments from the stack.
2786 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2787 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2788 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002789 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002790 }
2791 lastInsIndex = index;
2792 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002793 }
2794 }
2795
2796 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002797 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002798 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2799 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002800
Dan Gohman98ca4f22009-08-05 01:29:28 +00002801 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002802}
2803
2804/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002805static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002806 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002807 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002808 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002809 // Maybe this has already been legalized into the constant pool?
2810 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002811 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002812 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002813 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002814 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002815 }
2816 }
2817 return false;
2818}
2819
Evan Chenga8e29892007-01-19 07:51:42 +00002820/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2821/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002822SDValue
2823ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002824 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002825 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002826 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002827 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002828 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002829 // Constant does not fit, try adjusting it by one?
2830 switch (CC) {
2831 default: break;
2832 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002833 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002834 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002835 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002836 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002837 }
2838 break;
2839 case ISD::SETULT:
2840 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002841 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002842 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002843 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002844 }
2845 break;
2846 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002847 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002848 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002849 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002850 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002851 }
2852 break;
2853 case ISD::SETULE:
2854 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002855 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002856 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002857 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002858 }
2859 break;
2860 }
2861 }
2862 }
2863
2864 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002865 ARMISD::NodeType CompareType;
2866 switch (CondCode) {
2867 default:
2868 CompareType = ARMISD::CMP;
2869 break;
2870 case ARMCC::EQ:
2871 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002872 // Uses only Z Flag
2873 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002874 break;
2875 }
Evan Cheng218977b2010-07-13 19:27:42 +00002876 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002877 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002878}
2879
2880/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002881SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002882ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002883 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002884 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002885 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002886 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002887 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002888 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2889 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002890}
2891
Bob Wilson79f56c92011-03-08 01:17:20 +00002892/// duplicateCmp - Glue values can have only one use, so this function
2893/// duplicates a comparison node.
2894SDValue
2895ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2896 unsigned Opc = Cmp.getOpcode();
2897 DebugLoc DL = Cmp.getDebugLoc();
2898 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2899 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2900
2901 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2902 Cmp = Cmp.getOperand(0);
2903 Opc = Cmp.getOpcode();
2904 if (Opc == ARMISD::CMPFP)
2905 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2906 else {
2907 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2908 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2909 }
2910 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2911}
2912
Bill Wendlingde2b1512010-08-11 08:43:16 +00002913SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2914 SDValue Cond = Op.getOperand(0);
2915 SDValue SelectTrue = Op.getOperand(1);
2916 SDValue SelectFalse = Op.getOperand(2);
2917 DebugLoc dl = Op.getDebugLoc();
2918
2919 // Convert:
2920 //
2921 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2922 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2923 //
2924 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2925 const ConstantSDNode *CMOVTrue =
2926 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2927 const ConstantSDNode *CMOVFalse =
2928 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2929
2930 if (CMOVTrue && CMOVFalse) {
2931 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2932 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2933
2934 SDValue True;
2935 SDValue False;
2936 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2937 True = SelectTrue;
2938 False = SelectFalse;
2939 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2940 True = SelectFalse;
2941 False = SelectTrue;
2942 }
2943
2944 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002945 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002946 SDValue ARMcc = Cond.getOperand(2);
2947 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002948 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002949 assert(True.getValueType() == VT);
2950 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002951 }
2952 }
2953 }
2954
Dan Gohmandb953892012-02-24 00:09:36 +00002955 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2956 // undefined bits before doing a full-word comparison with zero.
2957 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2958 DAG.getConstant(1, Cond.getValueType()));
2959
Bill Wendlingde2b1512010-08-11 08:43:16 +00002960 return DAG.getSelectCC(dl, Cond,
2961 DAG.getConstant(0, Cond.getValueType()),
2962 SelectTrue, SelectFalse, ISD::SETNE);
2963}
2964
Dan Gohmand858e902010-04-17 15:26:15 +00002965SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002966 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002967 SDValue LHS = Op.getOperand(0);
2968 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002969 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002970 SDValue TrueVal = Op.getOperand(2);
2971 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002972 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002973
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002975 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002977 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002978 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002979 }
2980
2981 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002982 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002983
Evan Cheng218977b2010-07-13 19:27:42 +00002984 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2985 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002986 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002987 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002988 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002989 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002990 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002991 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002992 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002993 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002994 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002995 }
2996 return Result;
2997}
2998
Evan Cheng218977b2010-07-13 19:27:42 +00002999/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3000/// to morph to an integer compare sequence.
3001static bool canChangeToInt(SDValue Op, bool &SeenZero,
3002 const ARMSubtarget *Subtarget) {
3003 SDNode *N = Op.getNode();
3004 if (!N->hasOneUse())
3005 // Otherwise it requires moving the value from fp to integer registers.
3006 return false;
3007 if (!N->getNumValues())
3008 return false;
3009 EVT VT = Op.getValueType();
3010 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3011 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3012 // vmrs are very slow, e.g. cortex-a8.
3013 return false;
3014
3015 if (isFloatingPointZero(Op)) {
3016 SeenZero = true;
3017 return true;
3018 }
3019 return ISD::isNormalLoad(N);
3020}
3021
3022static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3023 if (isFloatingPointZero(Op))
3024 return DAG.getConstant(0, MVT::i32);
3025
3026 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3027 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003028 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003029 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003030 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003031
3032 llvm_unreachable("Unknown VFP cmp argument!");
3033}
3034
3035static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3036 SDValue &RetVal1, SDValue &RetVal2) {
3037 if (isFloatingPointZero(Op)) {
3038 RetVal1 = DAG.getConstant(0, MVT::i32);
3039 RetVal2 = DAG.getConstant(0, MVT::i32);
3040 return;
3041 }
3042
3043 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3044 SDValue Ptr = Ld->getBasePtr();
3045 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3046 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003047 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003048 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003049 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003050
3051 EVT PtrType = Ptr.getValueType();
3052 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3053 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3054 PtrType, Ptr, DAG.getConstant(4, PtrType));
3055 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3056 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003057 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003058 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003059 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003060 return;
3061 }
3062
3063 llvm_unreachable("Unknown VFP cmp argument!");
3064}
3065
3066/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3067/// f32 and even f64 comparisons to integer ones.
3068SDValue
3069ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3070 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003071 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003072 SDValue LHS = Op.getOperand(2);
3073 SDValue RHS = Op.getOperand(3);
3074 SDValue Dest = Op.getOperand(4);
3075 DebugLoc dl = Op.getDebugLoc();
3076
Evan Chengfc501a32012-03-01 23:27:13 +00003077 bool LHSSeenZero = false;
3078 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3079 bool RHSSeenZero = false;
3080 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3081 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003082 // If unsafe fp math optimization is enabled and there are no other uses of
3083 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003084 // to an integer comparison.
3085 if (CC == ISD::SETOEQ)
3086 CC = ISD::SETEQ;
3087 else if (CC == ISD::SETUNE)
3088 CC = ISD::SETNE;
3089
Evan Chengfc501a32012-03-01 23:27:13 +00003090 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003091 SDValue ARMcc;
3092 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003093 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3094 bitcastf32Toi32(LHS, DAG), Mask);
3095 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3096 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003097 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3098 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3099 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3100 Chain, Dest, ARMcc, CCR, Cmp);
3101 }
3102
3103 SDValue LHS1, LHS2;
3104 SDValue RHS1, RHS2;
3105 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3106 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003107 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3108 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003109 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3110 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003111 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003112 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3113 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3114 }
3115
3116 return SDValue();
3117}
3118
3119SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3120 SDValue Chain = Op.getOperand(0);
3121 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3122 SDValue LHS = Op.getOperand(2);
3123 SDValue RHS = Op.getOperand(3);
3124 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003125 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003126
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003128 SDValue ARMcc;
3129 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003132 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003133 }
3134
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003136
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003137 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003138 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3139 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3140 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3141 if (Result.getNode())
3142 return Result;
3143 }
3144
Evan Chenga8e29892007-01-19 07:51:42 +00003145 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003146 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003147
Evan Cheng218977b2010-07-13 19:27:42 +00003148 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3149 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003151 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003152 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003153 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003154 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003155 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3156 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003157 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003158 }
3159 return Res;
3160}
3161
Dan Gohmand858e902010-04-17 15:26:15 +00003162SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003163 SDValue Chain = Op.getOperand(0);
3164 SDValue Table = Op.getOperand(1);
3165 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003166 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003167
Owen Andersone50ed302009-08-10 22:56:29 +00003168 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003169 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3170 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003171 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003172 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003173 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003174 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3175 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003176 if (Subtarget->isThumb2()) {
3177 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3178 // which does another jump to the destination. This also makes it easier
3179 // to translate it to TBB / TBH later.
3180 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003182 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003183 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003184 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003185 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003186 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003187 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003188 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003189 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003190 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003191 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003192 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003193 MachinePointerInfo::getJumpTable(),
3194 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003195 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003197 }
Evan Chenga8e29892007-01-19 07:51:42 +00003198}
3199
Eli Friedman14e809c2011-11-09 23:36:02 +00003200static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003201 EVT VT = Op.getValueType();
3202 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003203
James Molloy873fd5f2012-02-20 09:24:05 +00003204 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3205 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3206 return Op;
3207 return DAG.UnrollVectorOp(Op.getNode());
3208 }
3209
3210 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3211 "Invalid type for custom lowering!");
3212 if (VT != MVT::v4i16)
3213 return DAG.UnrollVectorOp(Op.getNode());
3214
3215 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3216 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003217}
3218
Bob Wilson76a312b2010-03-19 22:51:32 +00003219static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003220 EVT VT = Op.getValueType();
3221 if (VT.isVector())
3222 return LowerVectorFP_TO_INT(Op, DAG);
3223
Bob Wilson76a312b2010-03-19 22:51:32 +00003224 DebugLoc dl = Op.getDebugLoc();
3225 unsigned Opc;
3226
3227 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003228 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003229 case ISD::FP_TO_SINT:
3230 Opc = ARMISD::FTOSI;
3231 break;
3232 case ISD::FP_TO_UINT:
3233 Opc = ARMISD::FTOUI;
3234 break;
3235 }
3236 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003237 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003238}
3239
Cameron Zwarich3007d332011-03-29 21:41:55 +00003240static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3241 EVT VT = Op.getValueType();
3242 DebugLoc dl = Op.getDebugLoc();
3243
Eli Friedman14e809c2011-11-09 23:36:02 +00003244 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3245 if (VT.getVectorElementType() == MVT::f32)
3246 return Op;
3247 return DAG.UnrollVectorOp(Op.getNode());
3248 }
3249
Duncan Sands1f6a3292011-08-12 14:54:45 +00003250 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3251 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003252 if (VT != MVT::v4f32)
3253 return DAG.UnrollVectorOp(Op.getNode());
3254
3255 unsigned CastOpc;
3256 unsigned Opc;
3257 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003258 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003259 case ISD::SINT_TO_FP:
3260 CastOpc = ISD::SIGN_EXTEND;
3261 Opc = ISD::SINT_TO_FP;
3262 break;
3263 case ISD::UINT_TO_FP:
3264 CastOpc = ISD::ZERO_EXTEND;
3265 Opc = ISD::UINT_TO_FP;
3266 break;
3267 }
3268
3269 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3270 return DAG.getNode(Opc, dl, VT, Op);
3271}
3272
Bob Wilson76a312b2010-03-19 22:51:32 +00003273static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3274 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003275 if (VT.isVector())
3276 return LowerVectorINT_TO_FP(Op, DAG);
3277
Bob Wilson76a312b2010-03-19 22:51:32 +00003278 DebugLoc dl = Op.getDebugLoc();
3279 unsigned Opc;
3280
3281 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003282 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003283 case ISD::SINT_TO_FP:
3284 Opc = ARMISD::SITOF;
3285 break;
3286 case ISD::UINT_TO_FP:
3287 Opc = ARMISD::UITOF;
3288 break;
3289 }
3290
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003291 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003292 return DAG.getNode(Opc, dl, VT, Op);
3293}
3294
Evan Cheng515fe3a2010-07-08 02:08:50 +00003295SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003296 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003297 SDValue Tmp0 = Op.getOperand(0);
3298 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003299 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003300 EVT VT = Op.getValueType();
3301 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003302 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3303 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3304 bool UseNEON = !InGPR && Subtarget->hasNEON();
3305
3306 if (UseNEON) {
3307 // Use VBSL to copy the sign bit.
3308 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3309 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3310 DAG.getTargetConstant(EncodedVal, MVT::i32));
3311 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3312 if (VT == MVT::f64)
3313 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3314 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3315 DAG.getConstant(32, MVT::i32));
3316 else /*if (VT == MVT::f32)*/
3317 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3318 if (SrcVT == MVT::f32) {
3319 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3320 if (VT == MVT::f64)
3321 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3322 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3323 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003324 } else if (VT == MVT::f32)
3325 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3326 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3327 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003328 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3329 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3330
3331 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3332 MVT::i32);
3333 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3334 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3335 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003336
Evan Chenge573fb32011-02-23 02:24:55 +00003337 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3338 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3339 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003340 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003341 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3342 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3343 DAG.getConstant(0, MVT::i32));
3344 } else {
3345 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3346 }
3347
3348 return Res;
3349 }
Evan Chengc143dd42011-02-11 02:28:55 +00003350
3351 // Bitcast operand 1 to i32.
3352 if (SrcVT == MVT::f64)
3353 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3354 &Tmp1, 1).getValue(1);
3355 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3356
Evan Chenge573fb32011-02-23 02:24:55 +00003357 // Or in the signbit with integer operations.
3358 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3359 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3360 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3361 if (VT == MVT::f32) {
3362 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3363 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3364 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3365 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003366 }
3367
Evan Chenge573fb32011-02-23 02:24:55 +00003368 // f64: Or the high part with signbit and then combine two parts.
3369 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3370 &Tmp0, 1);
3371 SDValue Lo = Tmp0.getValue(0);
3372 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3373 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3374 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003375}
3376
Evan Cheng2457f2c2010-05-22 01:47:14 +00003377SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3378 MachineFunction &MF = DAG.getMachineFunction();
3379 MachineFrameInfo *MFI = MF.getFrameInfo();
3380 MFI->setReturnAddressIsTaken(true);
3381
3382 EVT VT = Op.getValueType();
3383 DebugLoc dl = Op.getDebugLoc();
3384 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3385 if (Depth) {
3386 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3387 SDValue Offset = DAG.getConstant(4, MVT::i32);
3388 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3389 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003390 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003391 }
3392
3393 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003394 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003395 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3396}
3397
Dan Gohmand858e902010-04-17 15:26:15 +00003398SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003399 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3400 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003401
Owen Andersone50ed302009-08-10 22:56:29 +00003402 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003403 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3404 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003405 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003406 ? ARM::R7 : ARM::R11;
3407 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3408 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003409 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3410 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003411 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003412 return FrameAddr;
3413}
3414
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003415/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003416/// expand a bit convert where either the source or destination type is i64 to
3417/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3418/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3419/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003420static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003421 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3422 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003423 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003424
Bob Wilson9f3f0612010-04-17 05:30:19 +00003425 // This function is only supposed to be called for i64 types, either as the
3426 // source or destination of the bit convert.
3427 EVT SrcVT = Op.getValueType();
3428 EVT DstVT = N->getValueType(0);
3429 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003430 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003431
Bob Wilson9f3f0612010-04-17 05:30:19 +00003432 // Turn i64->f64 into VMOVDRR.
3433 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3435 DAG.getConstant(0, MVT::i32));
3436 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3437 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003438 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003439 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003440 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003441
Jim Grosbache5165492009-11-09 00:11:35 +00003442 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003443 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3444 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3445 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3446 // Merge the pieces into a single i64 value.
3447 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3448 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003449
Bob Wilson9f3f0612010-04-17 05:30:19 +00003450 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003451}
3452
Bob Wilson5bafff32009-06-22 23:27:02 +00003453/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003454/// Zero vectors are used to represent vector negation and in those cases
3455/// will be implemented with the NEON VNEG instruction. However, VNEG does
3456/// not support i64 elements, so sometimes the zero vectors will need to be
3457/// explicitly constructed. Regardless, use a canonical VMOV to create the
3458/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003459static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003460 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003461 // The canonical modified immediate encoding of a zero vector is....0!
3462 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3463 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3464 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003465 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003466}
3467
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003468/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3469/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003470SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3471 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003472 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3473 EVT VT = Op.getValueType();
3474 unsigned VTBits = VT.getSizeInBits();
3475 DebugLoc dl = Op.getDebugLoc();
3476 SDValue ShOpLo = Op.getOperand(0);
3477 SDValue ShOpHi = Op.getOperand(1);
3478 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003479 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003480 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003481
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003482 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3483
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003484 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3485 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3486 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3487 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3488 DAG.getConstant(VTBits, MVT::i32));
3489 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3490 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003491 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003492
3493 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3494 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003495 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003496 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003497 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003498 CCR, Cmp);
3499
3500 SDValue Ops[2] = { Lo, Hi };
3501 return DAG.getMergeValues(Ops, 2, dl);
3502}
3503
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003504/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3505/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003506SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3507 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003508 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3509 EVT VT = Op.getValueType();
3510 unsigned VTBits = VT.getSizeInBits();
3511 DebugLoc dl = Op.getDebugLoc();
3512 SDValue ShOpLo = Op.getOperand(0);
3513 SDValue ShOpHi = Op.getOperand(1);
3514 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003515 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003516
3517 assert(Op.getOpcode() == ISD::SHL_PARTS);
3518 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3519 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3520 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3521 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3522 DAG.getConstant(VTBits, MVT::i32));
3523 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3524 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3525
3526 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3527 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3528 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003529 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003530 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003531 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003532 CCR, Cmp);
3533
3534 SDValue Ops[2] = { Lo, Hi };
3535 return DAG.getMergeValues(Ops, 2, dl);
3536}
3537
Jim Grosbach4725ca72010-09-08 03:54:02 +00003538SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003539 SelectionDAG &DAG) const {
3540 // The rounding mode is in bits 23:22 of the FPSCR.
3541 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3542 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3543 // so that the shift + and get folded into a bitfield extract.
3544 DebugLoc dl = Op.getDebugLoc();
3545 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3546 DAG.getConstant(Intrinsic::arm_get_fpscr,
3547 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003548 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003549 DAG.getConstant(1U << 22, MVT::i32));
3550 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3551 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003552 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003553 DAG.getConstant(3, MVT::i32));
3554}
3555
Jim Grosbach3482c802010-01-18 19:58:49 +00003556static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3557 const ARMSubtarget *ST) {
3558 EVT VT = N->getValueType(0);
3559 DebugLoc dl = N->getDebugLoc();
3560
3561 if (!ST->hasV6T2Ops())
3562 return SDValue();
3563
3564 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3565 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3566}
3567
Evan Chengc8e70452012-12-04 22:41:50 +00003568/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3569/// for each 16-bit element from operand, repeated. The basic idea is to
3570/// leverage vcnt to get the 8-bit counts, gather and add the results.
3571///
3572/// Trace for v4i16:
3573/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3574/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3575/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
3576/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
3577/// [b0 b1 b2 b3 b4 b5 b6 b7]
3578/// +[b1 b0 b3 b2 b5 b4 b7 b6]
3579/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3580/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3581static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3582 EVT VT = N->getValueType(0);
3583 DebugLoc DL = N->getDebugLoc();
3584
3585 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3586 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3587 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3588 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3589 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3590 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3591}
3592
3593/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3594/// bit-count for each 16-bit element from the operand. We need slightly
3595/// different sequencing for v4i16 and v8i16 to stay within NEON's available
3596/// 64/128-bit registers.
3597///
3598/// Trace for v4i16:
3599/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3600/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3601/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3602/// v4i16:Extracted = [k0 k1 k2 k3 ]
3603static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3604 EVT VT = N->getValueType(0);
3605 DebugLoc DL = N->getDebugLoc();
3606
3607 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3608 if (VT.is64BitVector()) {
3609 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3610 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3611 DAG.getIntPtrConstant(0));
3612 } else {
3613 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3614 BitCounts, DAG.getIntPtrConstant(0));
3615 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3616 }
3617}
3618
3619/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3620/// bit-count for each 32-bit element from the operand. The idea here is
3621/// to split the vector into 16-bit elements, leverage the 16-bit count
3622/// routine, and then combine the results.
3623///
3624/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3625/// input = [v0 v1 ] (vi: 32-bit elements)
3626/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3627/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
3628/// vrev: N0 = [k1 k0 k3 k2 ]
3629/// [k0 k1 k2 k3 ]
3630/// N1 =+[k1 k0 k3 k2 ]
3631/// [k0 k2 k1 k3 ]
3632/// N2 =+[k1 k3 k0 k2 ]
3633/// [k0 k2 k1 k3 ]
3634/// Extended =+[k1 k3 k0 k2 ]
3635/// [k0 k2 ]
3636/// Extracted=+[k1 k3 ]
3637///
3638static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3639 EVT VT = N->getValueType(0);
3640 DebugLoc DL = N->getDebugLoc();
3641
3642 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3643
3644 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3645 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3646 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3647 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3648 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3649
3650 if (VT.is64BitVector()) {
3651 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3652 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3653 DAG.getIntPtrConstant(0));
3654 } else {
3655 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3656 DAG.getIntPtrConstant(0));
3657 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3658 }
3659}
3660
3661static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3662 const ARMSubtarget *ST) {
3663 EVT VT = N->getValueType(0);
3664
3665 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay105ab4f2012-12-04 23:54:02 +00003666 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3667 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengc8e70452012-12-04 22:41:50 +00003668 "Unexpected type for custom ctpop lowering");
3669
3670 if (VT.getVectorElementType() == MVT::i32)
3671 return lowerCTPOP32BitElements(N, DAG);
3672 else
3673 return lowerCTPOP16BitElements(N, DAG);
3674}
3675
Bob Wilson5bafff32009-06-22 23:27:02 +00003676static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3677 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003678 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003679 DebugLoc dl = N->getDebugLoc();
3680
Bob Wilsond5448bb2010-11-18 21:16:28 +00003681 if (!VT.isVector())
3682 return SDValue();
3683
Bob Wilson5bafff32009-06-22 23:27:02 +00003684 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003685 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003686
Bob Wilsond5448bb2010-11-18 21:16:28 +00003687 // Left shifts translate directly to the vshiftu intrinsic.
3688 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003689 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003690 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3691 N->getOperand(0), N->getOperand(1));
3692
3693 assert((N->getOpcode() == ISD::SRA ||
3694 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3695
3696 // NEON uses the same intrinsics for both left and right shifts. For
3697 // right shifts, the shift amounts are negative, so negate the vector of
3698 // shift amounts.
3699 EVT ShiftVT = N->getOperand(1).getValueType();
3700 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3701 getZeroVector(ShiftVT, DAG, dl),
3702 N->getOperand(1));
3703 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3704 Intrinsic::arm_neon_vshifts :
3705 Intrinsic::arm_neon_vshiftu);
3706 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3707 DAG.getConstant(vshiftInt, MVT::i32),
3708 N->getOperand(0), NegatedCount);
3709}
3710
3711static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3712 const ARMSubtarget *ST) {
3713 EVT VT = N->getValueType(0);
3714 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003715
Eli Friedmance392eb2009-08-22 03:13:10 +00003716 // We can get here for a node like i32 = ISD::SHL i32, i64
3717 if (VT != MVT::i64)
3718 return SDValue();
3719
3720 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003721 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003722
Chris Lattner27a6c732007-11-24 07:07:01 +00003723 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3724 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003725 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003726 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003727
Chris Lattner27a6c732007-11-24 07:07:01 +00003728 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003729 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003730
Chris Lattner27a6c732007-11-24 07:07:01 +00003731 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003733 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003735 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003736
Chris Lattner27a6c732007-11-24 07:07:01 +00003737 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3738 // captures the result into a carry flag.
3739 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003740 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003741
Chris Lattner27a6c732007-11-24 07:07:01 +00003742 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003743 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003744
Chris Lattner27a6c732007-11-24 07:07:01 +00003745 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003747}
3748
Bob Wilson5bafff32009-06-22 23:27:02 +00003749static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3750 SDValue TmpOp0, TmpOp1;
3751 bool Invert = false;
3752 bool Swap = false;
3753 unsigned Opc = 0;
3754
3755 SDValue Op0 = Op.getOperand(0);
3756 SDValue Op1 = Op.getOperand(1);
3757 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003758 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003759 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3760 DebugLoc dl = Op.getDebugLoc();
3761
3762 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3763 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003764 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003765 case ISD::SETUNE:
3766 case ISD::SETNE: Invert = true; // Fallthrough
3767 case ISD::SETOEQ:
3768 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3769 case ISD::SETOLT:
3770 case ISD::SETLT: Swap = true; // Fallthrough
3771 case ISD::SETOGT:
3772 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3773 case ISD::SETOLE:
3774 case ISD::SETLE: Swap = true; // Fallthrough
3775 case ISD::SETOGE:
3776 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3777 case ISD::SETUGE: Swap = true; // Fallthrough
3778 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3779 case ISD::SETUGT: Swap = true; // Fallthrough
3780 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3781 case ISD::SETUEQ: Invert = true; // Fallthrough
3782 case ISD::SETONE:
3783 // Expand this to (OLT | OGT).
3784 TmpOp0 = Op0;
3785 TmpOp1 = Op1;
3786 Opc = ISD::OR;
3787 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3788 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3789 break;
3790 case ISD::SETUO: Invert = true; // Fallthrough
3791 case ISD::SETO:
3792 // Expand this to (OLT | OGE).
3793 TmpOp0 = Op0;
3794 TmpOp1 = Op1;
3795 Opc = ISD::OR;
3796 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3797 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3798 break;
3799 }
3800 } else {
3801 // Integer comparisons.
3802 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003803 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003804 case ISD::SETNE: Invert = true;
3805 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3806 case ISD::SETLT: Swap = true;
3807 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3808 case ISD::SETLE: Swap = true;
3809 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3810 case ISD::SETULT: Swap = true;
3811 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3812 case ISD::SETULE: Swap = true;
3813 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3814 }
3815
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003816 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003817 if (Opc == ARMISD::VCEQ) {
3818
3819 SDValue AndOp;
3820 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3821 AndOp = Op0;
3822 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3823 AndOp = Op1;
3824
3825 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003826 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003827 AndOp = AndOp.getOperand(0);
3828
3829 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3830 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003831 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3832 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003833 Invert = !Invert;
3834 }
3835 }
3836 }
3837
3838 if (Swap)
3839 std::swap(Op0, Op1);
3840
Owen Andersonc24cb352010-11-08 23:21:22 +00003841 // If one of the operands is a constant vector zero, attempt to fold the
3842 // comparison to a specialized compare-against-zero form.
3843 SDValue SingleOp;
3844 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3845 SingleOp = Op0;
3846 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3847 if (Opc == ARMISD::VCGE)
3848 Opc = ARMISD::VCLEZ;
3849 else if (Opc == ARMISD::VCGT)
3850 Opc = ARMISD::VCLTZ;
3851 SingleOp = Op1;
3852 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003853
Owen Andersonc24cb352010-11-08 23:21:22 +00003854 SDValue Result;
3855 if (SingleOp.getNode()) {
3856 switch (Opc) {
3857 case ARMISD::VCEQ:
3858 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3859 case ARMISD::VCGE:
3860 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3861 case ARMISD::VCLEZ:
3862 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3863 case ARMISD::VCGT:
3864 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3865 case ARMISD::VCLTZ:
3866 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3867 default:
3868 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3869 }
3870 } else {
3871 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3872 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003873
3874 if (Invert)
3875 Result = DAG.getNOT(dl, Result, VT);
3876
3877 return Result;
3878}
3879
Bob Wilsond3c42842010-06-14 22:19:57 +00003880/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3881/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003882/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003883static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3884 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003885 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003886 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003887
Bob Wilson827b2102010-06-15 19:05:35 +00003888 // SplatBitSize is set to the smallest size that splats the vector, so a
3889 // zero vector will always have SplatBitSize == 8. However, NEON modified
3890 // immediate instructions others than VMOV do not support the 8-bit encoding
3891 // of a zero vector, and the default encoding of zero is supposed to be the
3892 // 32-bit version.
3893 if (SplatBits == 0)
3894 SplatBitSize = 32;
3895
Bob Wilson5bafff32009-06-22 23:27:02 +00003896 switch (SplatBitSize) {
3897 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003898 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003899 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003900 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003901 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003902 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003903 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003904 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003905 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003906
3907 case 16:
3908 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003909 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003910 if ((SplatBits & ~0xff) == 0) {
3911 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003912 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003913 Imm = SplatBits;
3914 break;
3915 }
3916 if ((SplatBits & ~0xff00) == 0) {
3917 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003918 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003919 Imm = SplatBits >> 8;
3920 break;
3921 }
3922 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003923
3924 case 32:
3925 // NEON's 32-bit VMOV supports splat values where:
3926 // * only one byte is nonzero, or
3927 // * the least significant byte is 0xff and the second byte is nonzero, or
3928 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003929 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003930 if ((SplatBits & ~0xff) == 0) {
3931 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003932 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003933 Imm = SplatBits;
3934 break;
3935 }
3936 if ((SplatBits & ~0xff00) == 0) {
3937 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003938 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003939 Imm = SplatBits >> 8;
3940 break;
3941 }
3942 if ((SplatBits & ~0xff0000) == 0) {
3943 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003944 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003945 Imm = SplatBits >> 16;
3946 break;
3947 }
3948 if ((SplatBits & ~0xff000000) == 0) {
3949 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003950 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003951 Imm = SplatBits >> 24;
3952 break;
3953 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003954
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003955 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3956 if (type == OtherModImm) return SDValue();
3957
Bob Wilson5bafff32009-06-22 23:27:02 +00003958 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003959 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3960 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003961 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003962 Imm = SplatBits >> 8;
3963 SplatBits |= 0xff;
3964 break;
3965 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003966
3967 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003968 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3969 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003970 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003971 Imm = SplatBits >> 16;
3972 SplatBits |= 0xffff;
3973 break;
3974 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003975
3976 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3977 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3978 // VMOV.I32. A (very) minor optimization would be to replicate the value
3979 // and fall through here to test for a valid 64-bit splat. But, then the
3980 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003981 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003982
3983 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003984 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003985 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003986 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003987 uint64_t BitMask = 0xff;
3988 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003989 unsigned ImmMask = 1;
3990 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003991 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003992 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003993 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003994 Imm |= ImmMask;
3995 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003996 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003997 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003998 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003999 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00004000 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00004001 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00004002 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00004003 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00004004 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00004005 break;
4006 }
4007
Bob Wilson1a913ed2010-06-11 21:34:50 +00004008 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00004009 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00004010 }
4011
Bob Wilsoncba270d2010-07-13 21:16:48 +00004012 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4013 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00004014}
4015
Lang Hamesc0a9f822012-03-29 21:56:11 +00004016SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4017 const ARMSubtarget *ST) const {
4018 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
4019 return SDValue();
4020
4021 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4022 assert(Op.getValueType() == MVT::f32 &&
4023 "ConstantFP custom lowering should only occur for f32.");
4024
4025 // Try splatting with a VMOV.f32...
4026 APFloat FPVal = CFP->getValueAPF();
4027 int ImmVal = ARM_AM::getFP32Imm(FPVal);
4028 if (ImmVal != -1) {
4029 DebugLoc DL = Op.getDebugLoc();
4030 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4031 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4032 NewVal);
4033 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4034 DAG.getConstant(0, MVT::i32));
4035 }
4036
4037 // If that fails, try a VMOV.i32
4038 EVT VMovVT;
4039 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
4040 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
4041 VMOVModImm);
4042 if (NewVal != SDValue()) {
4043 DebugLoc DL = Op.getDebugLoc();
4044 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4045 NewVal);
4046 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4047 VecConstant);
4048 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4049 DAG.getConstant(0, MVT::i32));
4050 }
4051
4052 // Finally, try a VMVN.i32
4053 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
4054 VMVNModImm);
4055 if (NewVal != SDValue()) {
4056 DebugLoc DL = Op.getDebugLoc();
4057 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4058 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4059 VecConstant);
4060 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4061 DAG.getConstant(0, MVT::i32));
4062 }
4063
4064 return SDValue();
4065}
4066
Quentin Colombet43934ae2012-11-02 21:32:17 +00004067// check if an VEXT instruction can handle the shuffle mask when the
4068// vector sources of the shuffle are the same.
4069static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4070 unsigned NumElts = VT.getVectorNumElements();
4071
4072 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4073 if (M[0] < 0)
4074 return false;
4075
4076 Imm = M[0];
4077
4078 // If this is a VEXT shuffle, the immediate value is the index of the first
4079 // element. The other shuffle indices must be the successive elements after
4080 // the first one.
4081 unsigned ExpectedElt = Imm;
4082 for (unsigned i = 1; i < NumElts; ++i) {
4083 // Increment the expected index. If it wraps around, just follow it
4084 // back to index zero and keep going.
4085 ++ExpectedElt;
4086 if (ExpectedElt == NumElts)
4087 ExpectedElt = 0;
4088
4089 if (M[i] < 0) continue; // ignore UNDEF indices
4090 if (ExpectedElt != static_cast<unsigned>(M[i]))
4091 return false;
4092 }
4093
4094 return true;
4095}
4096
Lang Hamesc0a9f822012-03-29 21:56:11 +00004097
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004098static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004099 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004100 unsigned NumElts = VT.getVectorNumElements();
4101 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004102
4103 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4104 if (M[0] < 0)
4105 return false;
4106
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004107 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004108
4109 // If this is a VEXT shuffle, the immediate value is the index of the first
4110 // element. The other shuffle indices must be the successive elements after
4111 // the first one.
4112 unsigned ExpectedElt = Imm;
4113 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004114 // Increment the expected index. If it wraps around, it may still be
4115 // a VEXT but the source vectors must be swapped.
4116 ExpectedElt += 1;
4117 if (ExpectedElt == NumElts * 2) {
4118 ExpectedElt = 0;
4119 ReverseVEXT = true;
4120 }
4121
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004122 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004123 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004124 return false;
4125 }
4126
4127 // Adjust the index value if the source operands will be swapped.
4128 if (ReverseVEXT)
4129 Imm -= NumElts;
4130
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004131 return true;
4132}
4133
Bob Wilson8bb9e482009-07-26 00:39:34 +00004134/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4135/// instruction with the specified blocksize. (The order of the elements
4136/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004137static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004138 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4139 "Only possible block sizes for VREV are: 16, 32, 64");
4140
Bob Wilson8bb9e482009-07-26 00:39:34 +00004141 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004142 if (EltSz == 64)
4143 return false;
4144
4145 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004146 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004147 // If the first shuffle index is UNDEF, be optimistic.
4148 if (M[0] < 0)
4149 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004150
4151 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4152 return false;
4153
4154 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004155 if (M[i] < 0) continue; // ignore UNDEF indices
4156 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004157 return false;
4158 }
4159
4160 return true;
4161}
4162
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004163static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004164 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4165 // range, then 0 is placed into the resulting vector. So pretty much any mask
4166 // of 8 elements can work here.
4167 return VT == MVT::v8i8 && M.size() == 8;
4168}
4169
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004170static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004171 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4172 if (EltSz == 64)
4173 return false;
4174
Bob Wilsonc692cb72009-08-21 20:54:19 +00004175 unsigned NumElts = VT.getVectorNumElements();
4176 WhichResult = (M[0] == 0 ? 0 : 1);
4177 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004178 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4179 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004180 return false;
4181 }
4182 return true;
4183}
4184
Bob Wilson324f4f12009-12-03 06:40:55 +00004185/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4186/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4187/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004188static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004189 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4190 if (EltSz == 64)
4191 return false;
4192
4193 unsigned NumElts = VT.getVectorNumElements();
4194 WhichResult = (M[0] == 0 ? 0 : 1);
4195 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004196 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4197 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004198 return false;
4199 }
4200 return true;
4201}
4202
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004203static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004204 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4205 if (EltSz == 64)
4206 return false;
4207
Bob Wilsonc692cb72009-08-21 20:54:19 +00004208 unsigned NumElts = VT.getVectorNumElements();
4209 WhichResult = (M[0] == 0 ? 0 : 1);
4210 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004211 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004212 if ((unsigned) M[i] != 2 * i + WhichResult)
4213 return false;
4214 }
4215
4216 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004217 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004218 return false;
4219
4220 return true;
4221}
4222
Bob Wilson324f4f12009-12-03 06:40:55 +00004223/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4224/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4225/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004226static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004227 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4228 if (EltSz == 64)
4229 return false;
4230
4231 unsigned Half = VT.getVectorNumElements() / 2;
4232 WhichResult = (M[0] == 0 ? 0 : 1);
4233 for (unsigned j = 0; j != 2; ++j) {
4234 unsigned Idx = WhichResult;
4235 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004236 int MIdx = M[i + j * Half];
4237 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004238 return false;
4239 Idx += 2;
4240 }
4241 }
4242
4243 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4244 if (VT.is64BitVector() && EltSz == 32)
4245 return false;
4246
4247 return true;
4248}
4249
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004250static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004251 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4252 if (EltSz == 64)
4253 return false;
4254
Bob Wilsonc692cb72009-08-21 20:54:19 +00004255 unsigned NumElts = VT.getVectorNumElements();
4256 WhichResult = (M[0] == 0 ? 0 : 1);
4257 unsigned Idx = WhichResult * NumElts / 2;
4258 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004259 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4260 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004261 return false;
4262 Idx += 1;
4263 }
4264
4265 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004266 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004267 return false;
4268
4269 return true;
4270}
4271
Bob Wilson324f4f12009-12-03 06:40:55 +00004272/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4273/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4274/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004275static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004276 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4277 if (EltSz == 64)
4278 return false;
4279
4280 unsigned NumElts = VT.getVectorNumElements();
4281 WhichResult = (M[0] == 0 ? 0 : 1);
4282 unsigned Idx = WhichResult * NumElts / 2;
4283 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004284 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4285 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004286 return false;
4287 Idx += 1;
4288 }
4289
4290 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4291 if (VT.is64BitVector() && EltSz == 32)
4292 return false;
4293
4294 return true;
4295}
4296
Dale Johannesenf630c712010-07-29 20:10:08 +00004297// If N is an integer constant that can be moved into a register in one
4298// instruction, return an SDValue of such a constant (will become a MOV
4299// instruction). Otherwise return null.
4300static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4301 const ARMSubtarget *ST, DebugLoc dl) {
4302 uint64_t Val;
4303 if (!isa<ConstantSDNode>(N))
4304 return SDValue();
4305 Val = cast<ConstantSDNode>(N)->getZExtValue();
4306
4307 if (ST->isThumb1Only()) {
4308 if (Val <= 255 || ~Val <= 255)
4309 return DAG.getConstant(Val, MVT::i32);
4310 } else {
4311 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4312 return DAG.getConstant(Val, MVT::i32);
4313 }
4314 return SDValue();
4315}
4316
Bob Wilson5bafff32009-06-22 23:27:02 +00004317// If this is a case we can't handle, return null and let the default
4318// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004319SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4320 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004321 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004322 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004323 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004324
4325 APInt SplatBits, SplatUndef;
4326 unsigned SplatBitSize;
4327 bool HasAnyUndefs;
4328 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004329 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004330 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004331 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004332 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004333 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004334 DAG, VmovVT, VT.is128BitVector(),
4335 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004336 if (Val.getNode()) {
4337 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004338 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004339 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004340
4341 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004342 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004343 Val = isNEONModifiedImm(NegatedImm,
4344 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004345 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004346 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004347 if (Val.getNode()) {
4348 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004349 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004350 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004351
4352 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004353 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004354 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004355 if (ImmVal != -1) {
4356 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4357 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4358 }
4359 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004360 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004361 }
4362
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004363 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004364 //
4365 // As an optimisation, even if more than one value is used it may be more
4366 // profitable to splat with one value then change some lanes.
4367 //
4368 // Heuristically we decide to do this if the vector has a "dominant" value,
4369 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004370 unsigned NumElts = VT.getVectorNumElements();
4371 bool isOnlyLowElement = true;
4372 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004373 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004374 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004375
4376 // Map of the number of times a particular SDValue appears in the
4377 // element list.
James Molloy95154342012-09-06 10:32:08 +00004378 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004379 SDValue Value;
4380 for (unsigned i = 0; i < NumElts; ++i) {
4381 SDValue V = Op.getOperand(i);
4382 if (V.getOpcode() == ISD::UNDEF)
4383 continue;
4384 if (i > 0)
4385 isOnlyLowElement = false;
4386 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4387 isConstant = false;
4388
James Molloyba8562a2012-09-06 09:55:02 +00004389 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004390 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004391
4392 // Is this value dominant? (takes up more than half of the lanes)
4393 if (++Count > (NumElts / 2)) {
4394 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004395 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004396 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004397 }
James Molloyba8562a2012-09-06 09:55:02 +00004398 if (ValueCounts.size() != 1)
4399 usesOnlyOneValue = false;
4400 if (!Value.getNode() && ValueCounts.size() > 0)
4401 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004402
James Molloyba8562a2012-09-06 09:55:02 +00004403 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004404 return DAG.getUNDEF(VT);
4405
4406 if (isOnlyLowElement)
4407 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4408
Dale Johannesenf630c712010-07-29 20:10:08 +00004409 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4410
Dale Johannesen575cd142010-10-19 20:00:17 +00004411 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4412 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004413 if (hasDominantValue && EltSize <= 32) {
4414 if (!isConstant) {
4415 SDValue N;
4416
4417 // If we are VDUPing a value that comes directly from a vector, that will
4418 // cause an unnecessary move to and from a GPR, where instead we could
4419 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004420 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4421 // We need to create a new undef vector to use for the VDUPLANE if the
4422 // size of the vector from which we get the value is different than the
4423 // size of the vector that we need to create. We will insert the element
4424 // such that the register coalescer will remove unnecessary copies.
4425 if (VT != Value->getOperand(0).getValueType()) {
4426 ConstantSDNode *constIndex;
4427 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4428 assert(constIndex && "The index is not a constant!");
4429 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4430 VT.getVectorNumElements();
4431 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4432 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4433 Value, DAG.getConstant(index, MVT::i32)),
4434 DAG.getConstant(index, MVT::i32));
4435 } else {
4436 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004437 Value->getOperand(0), Value->getOperand(1));
Silviu Barangabb1078e2012-10-15 09:41:32 +00004438 }
4439 }
James Molloyba8562a2012-09-06 09:55:02 +00004440 else
4441 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4442
4443 if (!usesOnlyOneValue) {
4444 // The dominant value was splatted as 'N', but we now have to insert
4445 // all differing elements.
4446 for (unsigned I = 0; I < NumElts; ++I) {
4447 if (Op.getOperand(I) == Value)
4448 continue;
4449 SmallVector<SDValue, 3> Ops;
4450 Ops.push_back(N);
4451 Ops.push_back(Op.getOperand(I));
4452 Ops.push_back(DAG.getConstant(I, MVT::i32));
4453 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4454 }
4455 }
4456 return N;
4457 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004458 if (VT.getVectorElementType().isFloatingPoint()) {
4459 SmallVector<SDValue, 8> Ops;
4460 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004461 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004462 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004463 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4464 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004465 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4466 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004467 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004468 }
James Molloyba8562a2012-09-06 09:55:02 +00004469 if (usesOnlyOneValue) {
4470 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4471 if (isConstant && Val.getNode())
4472 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4473 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004474 }
4475
4476 // If all elements are constants and the case above didn't get hit, fall back
4477 // to the default expansion, which will generate a load from the constant
4478 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004479 if (isConstant)
4480 return SDValue();
4481
Bob Wilson11a1dff2011-01-07 21:37:30 +00004482 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4483 if (NumElts >= 4) {
4484 SDValue shuffle = ReconstructShuffle(Op, DAG);
4485 if (shuffle != SDValue())
4486 return shuffle;
4487 }
4488
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004489 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004490 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4491 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004492 if (EltSize >= 32) {
4493 // Do the expansion with floating-point types, since that is what the VFP
4494 // registers are defined to use, and since i64 is not legal.
4495 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4496 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004497 SmallVector<SDValue, 8> Ops;
4498 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004499 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004500 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004501 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004502 }
4503
4504 return SDValue();
4505}
4506
Bob Wilson11a1dff2011-01-07 21:37:30 +00004507// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004508// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004509SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4510 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004511 DebugLoc dl = Op.getDebugLoc();
4512 EVT VT = Op.getValueType();
4513 unsigned NumElts = VT.getVectorNumElements();
4514
4515 SmallVector<SDValue, 2> SourceVecs;
4516 SmallVector<unsigned, 2> MinElts;
4517 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004518
Bob Wilson11a1dff2011-01-07 21:37:30 +00004519 for (unsigned i = 0; i < NumElts; ++i) {
4520 SDValue V = Op.getOperand(i);
4521 if (V.getOpcode() == ISD::UNDEF)
4522 continue;
4523 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4524 // A shuffle can only come from building a vector from various
4525 // elements of other vectors.
4526 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004527 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4528 VT.getVectorElementType()) {
4529 // This code doesn't know how to handle shuffles where the vector
4530 // element types do not match (this happens because type legalization
4531 // promotes the return type of EXTRACT_VECTOR_ELT).
4532 // FIXME: It might be appropriate to extend this code to handle
4533 // mismatched types.
4534 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004535 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004536
Bob Wilson11a1dff2011-01-07 21:37:30 +00004537 // Record this extraction against the appropriate vector if possible...
4538 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004539 // If the element number isn't a constant, we can't effectively
4540 // analyze what's going on.
4541 if (!isa<ConstantSDNode>(V.getOperand(1)))
4542 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004543 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4544 bool FoundSource = false;
4545 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4546 if (SourceVecs[j] == SourceVec) {
4547 if (MinElts[j] > EltNo)
4548 MinElts[j] = EltNo;
4549 if (MaxElts[j] < EltNo)
4550 MaxElts[j] = EltNo;
4551 FoundSource = true;
4552 break;
4553 }
4554 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004555
Bob Wilson11a1dff2011-01-07 21:37:30 +00004556 // Or record a new source if not...
4557 if (!FoundSource) {
4558 SourceVecs.push_back(SourceVec);
4559 MinElts.push_back(EltNo);
4560 MaxElts.push_back(EltNo);
4561 }
4562 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004563
Bob Wilson11a1dff2011-01-07 21:37:30 +00004564 // Currently only do something sane when at most two source vectors
4565 // involved.
4566 if (SourceVecs.size() > 2)
4567 return SDValue();
4568
4569 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4570 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004571
Bob Wilson11a1dff2011-01-07 21:37:30 +00004572 // This loop extracts the usage patterns of the source vectors
4573 // and prepares appropriate SDValues for a shuffle if possible.
4574 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4575 if (SourceVecs[i].getValueType() == VT) {
4576 // No VEXT necessary
4577 ShuffleSrcs[i] = SourceVecs[i];
4578 VEXTOffsets[i] = 0;
4579 continue;
4580 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4581 // It probably isn't worth padding out a smaller vector just to
4582 // break it down again in a shuffle.
4583 return SDValue();
4584 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004585
Bob Wilson11a1dff2011-01-07 21:37:30 +00004586 // Since only 64-bit and 128-bit vectors are legal on ARM and
4587 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004588 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4589 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004590
Bob Wilson11a1dff2011-01-07 21:37:30 +00004591 if (MaxElts[i] - MinElts[i] >= NumElts) {
4592 // Span too large for a VEXT to cope
4593 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004594 }
4595
Bob Wilson11a1dff2011-01-07 21:37:30 +00004596 if (MinElts[i] >= NumElts) {
4597 // The extraction can just take the second half
4598 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004599 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4600 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004601 DAG.getIntPtrConstant(NumElts));
4602 } else if (MaxElts[i] < NumElts) {
4603 // The extraction can just take the first half
4604 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004605 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4606 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004607 DAG.getIntPtrConstant(0));
4608 } else {
4609 // An actual VEXT is needed
4610 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004611 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4612 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004613 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004614 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4615 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004616 DAG.getIntPtrConstant(NumElts));
4617 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4618 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4619 }
4620 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004621
Bob Wilson11a1dff2011-01-07 21:37:30 +00004622 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004623
Bob Wilson11a1dff2011-01-07 21:37:30 +00004624 for (unsigned i = 0; i < NumElts; ++i) {
4625 SDValue Entry = Op.getOperand(i);
4626 if (Entry.getOpcode() == ISD::UNDEF) {
4627 Mask.push_back(-1);
4628 continue;
4629 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004630
Bob Wilson11a1dff2011-01-07 21:37:30 +00004631 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004632 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4633 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004634 if (ExtractVec == SourceVecs[0]) {
4635 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4636 } else {
4637 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4638 }
4639 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004640
Bob Wilson11a1dff2011-01-07 21:37:30 +00004641 // Final check before we try to produce nonsense...
4642 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004643 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4644 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004645
Bob Wilson11a1dff2011-01-07 21:37:30 +00004646 return SDValue();
4647}
4648
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004649/// isShuffleMaskLegal - Targets can use this to indicate that they only
4650/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4651/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4652/// are assumed to be legal.
4653bool
4654ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4655 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004656 if (VT.getVectorNumElements() == 4 &&
4657 (VT.is128BitVector() || VT.is64BitVector())) {
4658 unsigned PFIndexes[4];
4659 for (unsigned i = 0; i != 4; ++i) {
4660 if (M[i] < 0)
4661 PFIndexes[i] = 8;
4662 else
4663 PFIndexes[i] = M[i];
4664 }
4665
4666 // Compute the index in the perfect shuffle table.
4667 unsigned PFTableIndex =
4668 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4669 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4670 unsigned Cost = (PFEntry >> 30);
4671
4672 if (Cost <= 4)
4673 return true;
4674 }
4675
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004676 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004677 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004678
Bob Wilson53dd2452010-06-07 23:53:38 +00004679 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4680 return (EltSize >= 32 ||
4681 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004682 isVREVMask(M, VT, 64) ||
4683 isVREVMask(M, VT, 32) ||
4684 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004685 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004686 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004687 isVTRNMask(M, VT, WhichResult) ||
4688 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004689 isVZIPMask(M, VT, WhichResult) ||
4690 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4691 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4692 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004693}
4694
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004695/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4696/// the specified operations to build the shuffle.
4697static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4698 SDValue RHS, SelectionDAG &DAG,
4699 DebugLoc dl) {
4700 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4701 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4702 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4703
4704 enum {
4705 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4706 OP_VREV,
4707 OP_VDUP0,
4708 OP_VDUP1,
4709 OP_VDUP2,
4710 OP_VDUP3,
4711 OP_VEXT1,
4712 OP_VEXT2,
4713 OP_VEXT3,
4714 OP_VUZPL, // VUZP, left result
4715 OP_VUZPR, // VUZP, right result
4716 OP_VZIPL, // VZIP, left result
4717 OP_VZIPR, // VZIP, right result
4718 OP_VTRNL, // VTRN, left result
4719 OP_VTRNR // VTRN, right result
4720 };
4721
4722 if (OpNum == OP_COPY) {
4723 if (LHSID == (1*9+2)*9+3) return LHS;
4724 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4725 return RHS;
4726 }
4727
4728 SDValue OpLHS, OpRHS;
4729 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4730 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4731 EVT VT = OpLHS.getValueType();
4732
4733 switch (OpNum) {
4734 default: llvm_unreachable("Unknown shuffle opcode!");
4735 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004736 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004737 if (VT.getVectorElementType() == MVT::i32 ||
4738 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004739 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4740 // vrev <4 x i16> -> VREV32
4741 if (VT.getVectorElementType() == MVT::i16)
4742 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4743 // vrev <4 x i8> -> VREV16
4744 assert(VT.getVectorElementType() == MVT::i8);
4745 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004746 case OP_VDUP0:
4747 case OP_VDUP1:
4748 case OP_VDUP2:
4749 case OP_VDUP3:
4750 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004751 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004752 case OP_VEXT1:
4753 case OP_VEXT2:
4754 case OP_VEXT3:
4755 return DAG.getNode(ARMISD::VEXT, dl, VT,
4756 OpLHS, OpRHS,
4757 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4758 case OP_VUZPL:
4759 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004760 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004761 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4762 case OP_VZIPL:
4763 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004764 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004765 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4766 case OP_VTRNL:
4767 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004768 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4769 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004770 }
4771}
4772
Bill Wendling69a05a72011-03-14 23:02:38 +00004773static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004774 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004775 SelectionDAG &DAG) {
4776 // Check to see if we can use the VTBL instruction.
4777 SDValue V1 = Op.getOperand(0);
4778 SDValue V2 = Op.getOperand(1);
4779 DebugLoc DL = Op.getDebugLoc();
4780
4781 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004782 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004783 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4784 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4785
4786 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4787 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4788 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4789 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004790
Owen Anderson76706012011-04-05 21:48:57 +00004791 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004792 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4793 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004794}
4795
Bob Wilson5bafff32009-06-22 23:27:02 +00004796static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004797 SDValue V1 = Op.getOperand(0);
4798 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004799 DebugLoc dl = Op.getDebugLoc();
4800 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004801 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004802
Bob Wilson28865062009-08-13 02:13:04 +00004803 // Convert shuffles that are directly supported on NEON to target-specific
4804 // DAG nodes, instead of keeping them as shuffles and matching them again
4805 // during code selection. This is more efficient and avoids the possibility
4806 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004807 // FIXME: floating-point vectors should be canonicalized to integer vectors
4808 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004809 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004810
Bob Wilson53dd2452010-06-07 23:53:38 +00004811 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4812 if (EltSize <= 32) {
4813 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4814 int Lane = SVN->getSplatIndex();
4815 // If this is undef splat, generate it via "just" vdup, if possible.
4816 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004817
Dan Gohman65fd6562011-11-03 21:49:52 +00004818 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004819 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4820 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4821 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004822 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4823 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4824 // reaches it).
4825 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4826 !isa<ConstantSDNode>(V1.getOperand(0))) {
4827 bool IsScalarToVector = true;
4828 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4829 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4830 IsScalarToVector = false;
4831 break;
4832 }
4833 if (IsScalarToVector)
4834 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4835 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004836 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4837 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004838 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004839
4840 bool ReverseVEXT;
4841 unsigned Imm;
4842 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4843 if (ReverseVEXT)
4844 std::swap(V1, V2);
4845 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4846 DAG.getConstant(Imm, MVT::i32));
4847 }
4848
4849 if (isVREVMask(ShuffleMask, VT, 64))
4850 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4851 if (isVREVMask(ShuffleMask, VT, 32))
4852 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4853 if (isVREVMask(ShuffleMask, VT, 16))
4854 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4855
Quentin Colombet43934ae2012-11-02 21:32:17 +00004856 if (V2->getOpcode() == ISD::UNDEF &&
4857 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4858 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4859 DAG.getConstant(Imm, MVT::i32));
4860 }
4861
Bob Wilson53dd2452010-06-07 23:53:38 +00004862 // Check for Neon shuffles that modify both input vectors in place.
4863 // If both results are used, i.e., if there are two shuffles with the same
4864 // source operands and with masks corresponding to both results of one of
4865 // these operations, DAG memoization will ensure that a single node is
4866 // used for both shuffles.
4867 unsigned WhichResult;
4868 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4869 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4870 V1, V2).getValue(WhichResult);
4871 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4872 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4873 V1, V2).getValue(WhichResult);
4874 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4875 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4876 V1, V2).getValue(WhichResult);
4877
4878 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4879 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4880 V1, V1).getValue(WhichResult);
4881 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4882 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4883 V1, V1).getValue(WhichResult);
4884 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4885 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4886 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004887 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004888
Bob Wilsonc692cb72009-08-21 20:54:19 +00004889 // If the shuffle is not directly supported and it has 4 elements, use
4890 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004891 unsigned NumElts = VT.getVectorNumElements();
4892 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004893 unsigned PFIndexes[4];
4894 for (unsigned i = 0; i != 4; ++i) {
4895 if (ShuffleMask[i] < 0)
4896 PFIndexes[i] = 8;
4897 else
4898 PFIndexes[i] = ShuffleMask[i];
4899 }
4900
4901 // Compute the index in the perfect shuffle table.
4902 unsigned PFTableIndex =
4903 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004904 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4905 unsigned Cost = (PFEntry >> 30);
4906
4907 if (Cost <= 4)
4908 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4909 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004910
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004911 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004912 if (EltSize >= 32) {
4913 // Do the expansion with floating-point types, since that is what the VFP
4914 // registers are defined to use, and since i64 is not legal.
4915 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4916 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4918 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004919 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004920 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004921 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004922 Ops.push_back(DAG.getUNDEF(EltVT));
4923 else
4924 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4925 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4926 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4927 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004928 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004929 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004930 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004931 }
4932
Bill Wendling69a05a72011-03-14 23:02:38 +00004933 if (VT == MVT::v8i8) {
4934 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4935 if (NewOp.getNode())
4936 return NewOp;
4937 }
4938
Bob Wilson22cac0d2009-08-14 05:16:33 +00004939 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004940}
4941
Eli Friedman5c89cb82011-10-24 23:08:52 +00004942static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4943 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4944 SDValue Lane = Op.getOperand(2);
4945 if (!isa<ConstantSDNode>(Lane))
4946 return SDValue();
4947
4948 return Op;
4949}
4950
Bob Wilson5bafff32009-06-22 23:27:02 +00004951static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004952 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004953 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004954 if (!isa<ConstantSDNode>(Lane))
4955 return SDValue();
4956
4957 SDValue Vec = Op.getOperand(0);
4958 if (Op.getValueType() == MVT::i32 &&
4959 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4960 DebugLoc dl = Op.getDebugLoc();
4961 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4962 }
4963
4964 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004965}
4966
Bob Wilsona6d65862009-08-03 20:36:38 +00004967static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4968 // The only time a CONCAT_VECTORS operation can have legal types is when
4969 // two 64-bit vectors are concatenated to a 128-bit vector.
4970 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4971 "unexpected CONCAT_VECTORS");
4972 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004973 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004974 SDValue Op0 = Op.getOperand(0);
4975 SDValue Op1 = Op.getOperand(1);
4976 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004977 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004978 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004979 DAG.getIntPtrConstant(0));
4980 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004981 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004982 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004983 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004984 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004985}
4986
Bob Wilson626613d2010-11-23 19:38:38 +00004987/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4988/// element has been zero/sign-extended, depending on the isSigned parameter,
4989/// from an integer type half its size.
4990static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4991 bool isSigned) {
4992 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4993 EVT VT = N->getValueType(0);
4994 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4995 SDNode *BVN = N->getOperand(0).getNode();
4996 if (BVN->getValueType(0) != MVT::v4i32 ||
4997 BVN->getOpcode() != ISD::BUILD_VECTOR)
4998 return false;
4999 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5000 unsigned HiElt = 1 - LoElt;
5001 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5002 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5003 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5004 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5005 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5006 return false;
5007 if (isSigned) {
5008 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5009 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5010 return true;
5011 } else {
5012 if (Hi0->isNullValue() && Hi1->isNullValue())
5013 return true;
5014 }
5015 return false;
5016 }
5017
5018 if (N->getOpcode() != ISD::BUILD_VECTOR)
5019 return false;
5020
5021 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5022 SDNode *Elt = N->getOperand(i).getNode();
5023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5024 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5025 unsigned HalfSize = EltSize / 2;
5026 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00005027 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005028 return false;
5029 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00005030 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00005031 return false;
5032 }
5033 continue;
5034 }
5035 return false;
5036 }
5037
5038 return true;
5039}
5040
5041/// isSignExtended - Check if a node is a vector value that is sign-extended
5042/// or a constant BUILD_VECTOR with sign-extended elements.
5043static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5044 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5045 return true;
5046 if (isExtendedBUILD_VECTOR(N, DAG, true))
5047 return true;
5048 return false;
5049}
5050
5051/// isZeroExtended - Check if a node is a vector value that is zero-extended
5052/// or a constant BUILD_VECTOR with zero-extended elements.
5053static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5054 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5055 return true;
5056 if (isExtendedBUILD_VECTOR(N, DAG, false))
5057 return true;
5058 return false;
5059}
5060
Sebastian Popcb495302012-11-30 19:08:04 +00005061/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5062/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5063/// We insert the required extension here to get the vector to fill a D register.
5064static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5065 const EVT &OrigTy,
5066 const EVT &ExtTy,
5067 unsigned ExtOpcode) {
5068 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5069 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5070 // 64-bits we need to insert a new extension so that it will be 64-bits.
5071 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5072 if (OrigTy.getSizeInBits() >= 64)
5073 return N;
5074
5075 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5076 MVT::SimpleValueType OrigSimpleTy = OrigTy.getSimpleVT().SimpleTy;
5077 EVT NewVT;
5078 switch (OrigSimpleTy) {
5079 default: llvm_unreachable("Unexpected Orig Vector Type");
5080 case MVT::v2i8:
5081 case MVT::v2i16:
5082 NewVT = MVT::v2i32;
5083 break;
5084 case MVT::v4i8:
5085 NewVT = MVT::v4i16;
5086 break;
5087 }
5088 return DAG.getNode(ExtOpcode, N->getDebugLoc(), NewVT, N);
5089}
5090
5091/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5092/// does not do any sign/zero extension. If the original vector is less
5093/// than 64 bits, an appropriate extension will be added after the load to
5094/// reach a total size of 64 bits. We have to add the extension separately
5095/// because ARM does not have a sign/zero extending load for vectors.
5096static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5097 SDValue NonExtendingLoad =
5098 DAG.getLoad(LD->getMemoryVT(), LD->getDebugLoc(), LD->getChain(),
5099 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5100 LD->isNonTemporal(), LD->isInvariant(),
5101 LD->getAlignment());
5102 unsigned ExtOp = 0;
5103 switch (LD->getExtensionType()) {
5104 default: llvm_unreachable("Unexpected LoadExtType");
5105 case ISD::EXTLOAD:
5106 case ISD::SEXTLOAD: ExtOp = ISD::SIGN_EXTEND; break;
5107 case ISD::ZEXTLOAD: ExtOp = ISD::ZERO_EXTEND; break;
5108 }
5109 MVT::SimpleValueType MemType = LD->getMemoryVT().getSimpleVT().SimpleTy;
5110 MVT::SimpleValueType ExtType = LD->getValueType(0).getSimpleVT().SimpleTy;
5111 return AddRequiredExtensionForVMULL(NonExtendingLoad, DAG,
5112 MemType, ExtType, ExtOp);
5113}
5114
5115/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5116/// extending load, or BUILD_VECTOR with extended elements, return the
5117/// unextended value. The unextended vector should be 64 bits so that it can
5118/// be used as an operand to a VMULL instruction. If the original vector size
5119/// before extension is less than 64 bits we add a an extension to resize
5120/// the vector to 64 bits.
5121static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005122 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popcb495302012-11-30 19:08:04 +00005123 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5124 N->getOperand(0)->getValueType(0),
5125 N->getValueType(0),
5126 N->getOpcode());
5127
Bob Wilson626613d2010-11-23 19:38:38 +00005128 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popcb495302012-11-30 19:08:04 +00005129 return SkipLoadExtensionForVMULL(LD, DAG);
5130
Bob Wilson626613d2010-11-23 19:38:38 +00005131 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5132 // have been legalized as a BITCAST from v4i32.
5133 if (N->getOpcode() == ISD::BITCAST) {
5134 SDNode *BVN = N->getOperand(0).getNode();
5135 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5136 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5137 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5138 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
5139 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5140 }
5141 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5142 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5143 EVT VT = N->getValueType(0);
5144 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5145 unsigned NumElts = VT.getVectorNumElements();
5146 MVT TruncVT = MVT::getIntegerVT(EltSize);
5147 SmallVector<SDValue, 8> Ops;
5148 for (unsigned i = 0; i != NumElts; ++i) {
5149 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5150 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00005151 // Element types smaller than 32 bits are not legal, so use i32 elements.
5152 // The values are implicitly truncated so sext vs. zext doesn't matter.
5153 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00005154 }
5155 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5156 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005157}
5158
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005159static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5160 unsigned Opcode = N->getOpcode();
5161 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5162 SDNode *N0 = N->getOperand(0).getNode();
5163 SDNode *N1 = N->getOperand(1).getNode();
5164 return N0->hasOneUse() && N1->hasOneUse() &&
5165 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5166 }
5167 return false;
5168}
5169
5170static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5171 unsigned Opcode = N->getOpcode();
5172 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5173 SDNode *N0 = N->getOperand(0).getNode();
5174 SDNode *N1 = N->getOperand(1).getNode();
5175 return N0->hasOneUse() && N1->hasOneUse() &&
5176 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5177 }
5178 return false;
5179}
5180
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005181static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5182 // Multiplications are only custom-lowered for 128-bit vectors so that
5183 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5184 EVT VT = Op.getValueType();
Sebastian Popcb495302012-11-30 19:08:04 +00005185 assert(VT.is128BitVector() && VT.isInteger() &&
5186 "unexpected type for custom-lowering ISD::MUL");
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005187 SDNode *N0 = Op.getOperand(0).getNode();
5188 SDNode *N1 = Op.getOperand(1).getNode();
5189 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005190 bool isMLA = false;
5191 bool isN0SExt = isSignExtended(N0, DAG);
5192 bool isN1SExt = isSignExtended(N1, DAG);
5193 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005194 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005195 else {
5196 bool isN0ZExt = isZeroExtended(N0, DAG);
5197 bool isN1ZExt = isZeroExtended(N1, DAG);
5198 if (isN0ZExt && isN1ZExt)
5199 NewOpc = ARMISD::VMULLu;
5200 else if (isN1SExt || isN1ZExt) {
5201 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5202 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5203 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5204 NewOpc = ARMISD::VMULLs;
5205 isMLA = true;
5206 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5207 NewOpc = ARMISD::VMULLu;
5208 isMLA = true;
5209 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5210 std::swap(N0, N1);
5211 NewOpc = ARMISD::VMULLu;
5212 isMLA = true;
5213 }
5214 }
5215
5216 if (!NewOpc) {
5217 if (VT == MVT::v2i64)
5218 // Fall through to expand this. It is not legal.
5219 return SDValue();
5220 else
5221 // Other vector multiplications are legal.
5222 return Op;
5223 }
5224 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005225
5226 // Legalize to a VMULL instruction.
5227 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005228 SDValue Op0;
Sebastian Popcb495302012-11-30 19:08:04 +00005229 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005230 if (!isMLA) {
Sebastian Popcb495302012-11-30 19:08:04 +00005231 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005232 assert(Op0.getValueType().is64BitVector() &&
5233 Op1.getValueType().is64BitVector() &&
5234 "unexpected types for extended operands to VMULL");
5235 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5236 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005237
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005238 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5239 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5240 // vmull q0, d4, d6
5241 // vmlal q0, d5, d6
5242 // is faster than
5243 // vaddl q0, d4, d5
5244 // vmovl q1, d6
5245 // vmul q0, q0, q1
Sebastian Popcb495302012-11-30 19:08:04 +00005246 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5247 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005248 EVT Op1VT = Op1.getValueType();
5249 return DAG.getNode(N0->getOpcode(), DL, VT,
5250 DAG.getNode(NewOpc, DL, VT,
5251 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5252 DAG.getNode(NewOpc, DL, VT,
5253 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005254}
5255
Owen Anderson76706012011-04-05 21:48:57 +00005256static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005257LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5258 // Convert to float
5259 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5260 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5261 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5262 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5263 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5264 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5265 // Get reciprocal estimate.
5266 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005267 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005268 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5269 // Because char has a smaller range than uchar, we can actually get away
5270 // without any newton steps. This requires that we use a weird bias
5271 // of 0xb000, however (again, this has been exhaustively tested).
5272 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5273 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5274 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5275 Y = DAG.getConstant(0xb000, MVT::i32);
5276 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5277 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5278 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5279 // Convert back to short.
5280 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5281 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5282 return X;
5283}
5284
Owen Anderson76706012011-04-05 21:48:57 +00005285static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005286LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5287 SDValue N2;
5288 // Convert to float.
5289 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5290 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5291 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5292 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5293 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5294 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005295
Nate Begeman7973f352011-02-11 20:53:29 +00005296 // Use reciprocal estimate and one refinement step.
5297 // float4 recip = vrecpeq_f32(yf);
5298 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005299 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005300 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005301 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005302 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5303 N1, N2);
5304 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5305 // Because short has a smaller range than ushort, we can actually get away
5306 // with only a single newton step. This requires that we use a weird bias
5307 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005308 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005309 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5310 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005311 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005312 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5313 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5314 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5315 // Convert back to integer and return.
5316 // return vmovn_s32(vcvt_s32_f32(result));
5317 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5318 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5319 return N0;
5320}
5321
5322static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5323 EVT VT = Op.getValueType();
5324 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5325 "unexpected type for custom-lowering ISD::SDIV");
5326
5327 DebugLoc dl = Op.getDebugLoc();
5328 SDValue N0 = Op.getOperand(0);
5329 SDValue N1 = Op.getOperand(1);
5330 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005331
Nate Begeman7973f352011-02-11 20:53:29 +00005332 if (VT == MVT::v8i8) {
5333 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5334 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005335
Nate Begeman7973f352011-02-11 20:53:29 +00005336 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5337 DAG.getIntPtrConstant(4));
5338 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005339 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005340 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5341 DAG.getIntPtrConstant(0));
5342 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5343 DAG.getIntPtrConstant(0));
5344
5345 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5346 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5347
5348 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5349 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005350
Nate Begeman7973f352011-02-11 20:53:29 +00005351 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5352 return N0;
5353 }
5354 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5355}
5356
5357static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5358 EVT VT = Op.getValueType();
5359 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5360 "unexpected type for custom-lowering ISD::UDIV");
5361
5362 DebugLoc dl = Op.getDebugLoc();
5363 SDValue N0 = Op.getOperand(0);
5364 SDValue N1 = Op.getOperand(1);
5365 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005366
Nate Begeman7973f352011-02-11 20:53:29 +00005367 if (VT == MVT::v8i8) {
5368 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5369 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005370
Nate Begeman7973f352011-02-11 20:53:29 +00005371 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5372 DAG.getIntPtrConstant(4));
5373 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005374 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005375 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5376 DAG.getIntPtrConstant(0));
5377 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5378 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005379
Nate Begeman7973f352011-02-11 20:53:29 +00005380 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5381 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005382
Nate Begeman7973f352011-02-11 20:53:29 +00005383 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5384 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005385
5386 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005387 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5388 N0);
5389 return N0;
5390 }
Owen Anderson76706012011-04-05 21:48:57 +00005391
Nate Begeman7973f352011-02-11 20:53:29 +00005392 // v4i16 sdiv ... Convert to float.
5393 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5394 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5395 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5396 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5397 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005398 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005399
5400 // Use reciprocal estimate and two refinement steps.
5401 // float4 recip = vrecpeq_f32(yf);
5402 // recip *= vrecpsq_f32(yf, recip);
5403 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005404 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005405 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005406 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005407 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005408 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005409 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005410 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005411 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005412 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005413 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5414 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5415 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5416 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005417 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005418 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5419 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5420 N1 = DAG.getConstant(2, MVT::i32);
5421 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5422 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5423 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5424 // Convert back to integer and return.
5425 // return vmovn_u32(vcvt_s32_f32(result));
5426 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5427 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5428 return N0;
5429}
5430
Evan Cheng342e3162011-08-30 01:34:54 +00005431static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5432 EVT VT = Op.getNode()->getValueType(0);
5433 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5434
5435 unsigned Opc;
5436 bool ExtraOp = false;
5437 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005438 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005439 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5440 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5441 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5442 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5443 }
5444
5445 if (!ExtraOp)
5446 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5447 Op.getOperand(1));
5448 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5449 Op.getOperand(1), Op.getOperand(2));
5450}
5451
Eli Friedman74bf18c2011-09-15 22:26:18 +00005452static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005453 // Monotonic load/store is legal for all targets
5454 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5455 return Op;
5456
5457 // Aquire/Release load/store is not legal for targets without a
5458 // dmb or equivalent available.
5459 return SDValue();
5460}
5461
5462
Eli Friedman2bdffe42011-08-31 00:31:29 +00005463static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005464ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5465 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005466 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005467 assert (Node->getValueType(0) == MVT::i64 &&
5468 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005469
Eli Friedman4d3f3292011-08-31 17:52:22 +00005470 SmallVector<SDValue, 6> Ops;
5471 Ops.push_back(Node->getOperand(0)); // Chain
5472 Ops.push_back(Node->getOperand(1)); // Ptr
5473 // Low part of Val1
5474 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5475 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5476 // High part of Val1
5477 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5478 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005479 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005480 // High part of Val1
5481 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5482 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5483 // High part of Val2
5484 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5485 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5486 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005487 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5488 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005489 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005490 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005491 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005492 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5493 Results.push_back(Result.getValue(2));
5494}
5495
Dan Gohmand858e902010-04-17 15:26:15 +00005496SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005497 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005498 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005499 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005500 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005501 case ISD::GlobalAddress:
5502 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5503 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005504 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005505 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005506 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5507 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005508 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005509 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005510 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005511 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005512 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005513 case ISD::SINT_TO_FP:
5514 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5515 case ISD::FP_TO_SINT:
5516 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005517 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005518 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005519 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005520 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005521 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005522 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005523 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5524 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005525 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005526 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005527 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005528 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005529 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005530 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005531 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005532 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengc8e70452012-12-04 22:41:50 +00005533 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005534 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005535 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005536 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005537 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005538 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005539 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005540 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005541 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005542 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005543 case ISD::SDIV: return LowerSDIV(Op, DAG);
5544 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005545 case ISD::ADDC:
5546 case ISD::ADDE:
5547 case ISD::SUBC:
5548 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005549 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005550 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005551 }
Evan Chenga8e29892007-01-19 07:51:42 +00005552}
5553
Duncan Sands1607f052008-12-01 11:39:25 +00005554/// ReplaceNodeResults - Replace the results of node with an illegal result
5555/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005556void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5557 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005558 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005559 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005560 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005561 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005562 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005563 case ISD::BITCAST:
5564 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005565 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005566 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005567 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005568 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005569 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005570 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005571 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005572 return;
5573 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005574 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005575 return;
5576 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005577 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005578 return;
5579 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005580 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005581 return;
5582 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005583 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005584 return;
5585 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005586 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005587 return;
5588 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005589 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005590 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005591 case ISD::ATOMIC_CMP_SWAP:
5592 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5593 return;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005594 case ISD::ATOMIC_LOAD_MIN:
5595 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5596 return;
5597 case ISD::ATOMIC_LOAD_UMIN:
5598 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5599 return;
5600 case ISD::ATOMIC_LOAD_MAX:
5601 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5602 return;
5603 case ISD::ATOMIC_LOAD_UMAX:
5604 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
5605 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005606 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005607 if (Res.getNode())
5608 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005609}
Chris Lattner27a6c732007-11-24 07:07:01 +00005610
Evan Chenga8e29892007-01-19 07:51:42 +00005611//===----------------------------------------------------------------------===//
5612// ARM Scheduler Hooks
5613//===----------------------------------------------------------------------===//
5614
5615MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005616ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5617 MachineBasicBlock *BB,
5618 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005619 unsigned dest = MI->getOperand(0).getReg();
5620 unsigned ptr = MI->getOperand(1).getReg();
5621 unsigned oldval = MI->getOperand(2).getReg();
5622 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5624 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005625 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005626
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005627 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005628 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5629 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5630 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005631
5632 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005633 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5634 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5635 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005636 }
5637
Jim Grosbach5278eb82009-12-11 01:42:04 +00005638 unsigned ldrOpc, strOpc;
5639 switch (Size) {
5640 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005641 case 1:
5642 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005643 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005644 break;
5645 case 2:
5646 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5647 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5648 break;
5649 case 4:
5650 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5651 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5652 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005653 }
5654
5655 MachineFunction *MF = BB->getParent();
5656 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5657 MachineFunction::iterator It = BB;
5658 ++It; // insert the new blocks after the current block
5659
5660 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5661 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5662 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5663 MF->insert(It, loop1MBB);
5664 MF->insert(It, loop2MBB);
5665 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005666
5667 // Transfer the remainder of BB and its successor edges to exitMBB.
5668 exitMBB->splice(exitMBB->begin(), BB,
5669 llvm::next(MachineBasicBlock::iterator(MI)),
5670 BB->end());
5671 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005672
5673 // thisMBB:
5674 // ...
5675 // fallthrough --> loop1MBB
5676 BB->addSuccessor(loop1MBB);
5677
5678 // loop1MBB:
5679 // ldrex dest, [ptr]
5680 // cmp dest, oldval
5681 // bne exitMBB
5682 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005683 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5684 if (ldrOpc == ARM::t2LDREX)
5685 MIB.addImm(0);
5686 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005687 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005688 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005689 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5690 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005691 BB->addSuccessor(loop2MBB);
5692 BB->addSuccessor(exitMBB);
5693
5694 // loop2MBB:
5695 // strex scratch, newval, [ptr]
5696 // cmp scratch, #0
5697 // bne loop1MBB
5698 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005699 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5700 if (strOpc == ARM::t2STREX)
5701 MIB.addImm(0);
5702 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005703 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005704 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005705 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5706 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005707 BB->addSuccessor(loop1MBB);
5708 BB->addSuccessor(exitMBB);
5709
5710 // exitMBB:
5711 // ...
5712 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005713
Dan Gohman14152b42010-07-06 20:24:04 +00005714 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005715
Jim Grosbach5278eb82009-12-11 01:42:04 +00005716 return BB;
5717}
5718
5719MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005720ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5721 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005722 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5723 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5724
5725 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005726 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005727 MachineFunction::iterator It = BB;
5728 ++It;
5729
5730 unsigned dest = MI->getOperand(0).getReg();
5731 unsigned ptr = MI->getOperand(1).getReg();
5732 unsigned incr = MI->getOperand(2).getReg();
5733 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005734 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005735
5736 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5737 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005738 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5739 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005740 }
5741
Jim Grosbachc3c23542009-12-14 04:22:04 +00005742 unsigned ldrOpc, strOpc;
5743 switch (Size) {
5744 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005745 case 1:
5746 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005747 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005748 break;
5749 case 2:
5750 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5751 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5752 break;
5753 case 4:
5754 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5755 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5756 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005757 }
5758
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005759 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5760 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5761 MF->insert(It, loopMBB);
5762 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005763
5764 // Transfer the remainder of BB and its successor edges to exitMBB.
5765 exitMBB->splice(exitMBB->begin(), BB,
5766 llvm::next(MachineBasicBlock::iterator(MI)),
5767 BB->end());
5768 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005769
Craig Topper420761a2012-04-20 07:30:17 +00005770 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005771 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005772 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005773 unsigned scratch = MRI.createVirtualRegister(TRC);
5774 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005775
5776 // thisMBB:
5777 // ...
5778 // fallthrough --> loopMBB
5779 BB->addSuccessor(loopMBB);
5780
5781 // loopMBB:
5782 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005783 // <binop> scratch2, dest, incr
5784 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005785 // cmp scratch, #0
5786 // bne- loopMBB
5787 // fallthrough --> exitMBB
5788 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005789 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5790 if (ldrOpc == ARM::t2LDREX)
5791 MIB.addImm(0);
5792 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005793 if (BinOpcode) {
5794 // operand order needs to go the other way for NAND
5795 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5796 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5797 addReg(incr).addReg(dest)).addReg(0);
5798 else
5799 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5800 addReg(dest).addReg(incr)).addReg(0);
5801 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005802
Jim Grosbachb6aed502011-09-09 18:37:27 +00005803 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5804 if (strOpc == ARM::t2STREX)
5805 MIB.addImm(0);
5806 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005807 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005808 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005809 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5810 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005811
5812 BB->addSuccessor(loopMBB);
5813 BB->addSuccessor(exitMBB);
5814
5815 // exitMBB:
5816 // ...
5817 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005818
Dan Gohman14152b42010-07-06 20:24:04 +00005819 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005820
Jim Grosbachc3c23542009-12-14 04:22:04 +00005821 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005822}
5823
Jim Grosbachf7da8822011-04-26 19:44:18 +00005824MachineBasicBlock *
5825ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5826 MachineBasicBlock *BB,
5827 unsigned Size,
5828 bool signExtend,
5829 ARMCC::CondCodes Cond) const {
5830 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5831
5832 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5833 MachineFunction *MF = BB->getParent();
5834 MachineFunction::iterator It = BB;
5835 ++It;
5836
5837 unsigned dest = MI->getOperand(0).getReg();
5838 unsigned ptr = MI->getOperand(1).getReg();
5839 unsigned incr = MI->getOperand(2).getReg();
5840 unsigned oldval = dest;
5841 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005842 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005843
5844 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5845 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005846 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5847 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005848 }
5849
Jim Grosbachf7da8822011-04-26 19:44:18 +00005850 unsigned ldrOpc, strOpc, extendOpc;
5851 switch (Size) {
5852 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5853 case 1:
5854 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5855 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005856 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005857 break;
5858 case 2:
5859 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5860 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005861 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005862 break;
5863 case 4:
5864 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5865 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5866 extendOpc = 0;
5867 break;
5868 }
5869
5870 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5871 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5872 MF->insert(It, loopMBB);
5873 MF->insert(It, exitMBB);
5874
5875 // Transfer the remainder of BB and its successor edges to exitMBB.
5876 exitMBB->splice(exitMBB->begin(), BB,
5877 llvm::next(MachineBasicBlock::iterator(MI)),
5878 BB->end());
5879 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5880
Craig Topper420761a2012-04-20 07:30:17 +00005881 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005882 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005883 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005884 unsigned scratch = MRI.createVirtualRegister(TRC);
5885 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005886
5887 // thisMBB:
5888 // ...
5889 // fallthrough --> loopMBB
5890 BB->addSuccessor(loopMBB);
5891
5892 // loopMBB:
5893 // ldrex dest, ptr
5894 // (sign extend dest, if required)
5895 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005896 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005897 // strex scratch, scratch2, ptr
5898 // cmp scratch, #0
5899 // bne- loopMBB
5900 // fallthrough --> exitMBB
5901 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005902 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5903 if (ldrOpc == ARM::t2LDREX)
5904 MIB.addImm(0);
5905 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005906
5907 // Sign extend the value, if necessary.
5908 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005909 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005910 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5911 .addReg(dest)
5912 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005913 }
5914
5915 // Build compare and cmov instructions.
5916 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5917 .addReg(oldval).addReg(incr));
5918 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005919 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005920
Jim Grosbachb6aed502011-09-09 18:37:27 +00005921 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5922 if (strOpc == ARM::t2STREX)
5923 MIB.addImm(0);
5924 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005925 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5926 .addReg(scratch).addImm(0));
5927 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5928 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5929
5930 BB->addSuccessor(loopMBB);
5931 BB->addSuccessor(exitMBB);
5932
5933 // exitMBB:
5934 // ...
5935 BB = exitMBB;
5936
5937 MI->eraseFromParent(); // The instruction is gone now.
5938
5939 return BB;
5940}
5941
Eli Friedman2bdffe42011-08-31 00:31:29 +00005942MachineBasicBlock *
5943ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5944 unsigned Op1, unsigned Op2,
Silviu Baranga35b3df62012-11-29 14:41:25 +00005945 bool NeedsCarry, bool IsCmpxchg,
5946 bool IsMinMax, ARMCC::CondCodes CC) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005947 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5948 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5949
5950 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5951 MachineFunction *MF = BB->getParent();
5952 MachineFunction::iterator It = BB;
5953 ++It;
5954
5955 unsigned destlo = MI->getOperand(0).getReg();
5956 unsigned desthi = MI->getOperand(1).getReg();
5957 unsigned ptr = MI->getOperand(2).getReg();
5958 unsigned vallo = MI->getOperand(3).getReg();
5959 unsigned valhi = MI->getOperand(4).getReg();
5960 DebugLoc dl = MI->getDebugLoc();
5961 bool isThumb2 = Subtarget->isThumb2();
5962
5963 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5964 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005965 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5966 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5967 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005968 }
5969
5970 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5971 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5972
5973 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005974 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Silviu Baranga35b3df62012-11-29 14:41:25 +00005975 if (IsCmpxchg || IsMinMax)
Eli Friedman4d3f3292011-08-31 17:52:22 +00005976 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005977 if (IsCmpxchg)
Eli Friedman4d3f3292011-08-31 17:52:22 +00005978 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005979 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005980
Eli Friedman2bdffe42011-08-31 00:31:29 +00005981 MF->insert(It, loopMBB);
Silviu Baranga35b3df62012-11-29 14:41:25 +00005982 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
5983 if (IsCmpxchg) MF->insert(It, cont2BB);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005984 MF->insert(It, exitMBB);
5985
5986 // Transfer the remainder of BB and its successor edges to exitMBB.
5987 exitMBB->splice(exitMBB->begin(), BB,
5988 llvm::next(MachineBasicBlock::iterator(MI)),
5989 BB->end());
5990 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5991
Craig Topper420761a2012-04-20 07:30:17 +00005992 const TargetRegisterClass *TRC = isThumb2 ?
5993 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5994 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005995 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5996
5997 // thisMBB:
5998 // ...
5999 // fallthrough --> loopMBB
6000 BB->addSuccessor(loopMBB);
6001
6002 // loopMBB:
6003 // ldrexd r2, r3, ptr
6004 // <binopa> r0, r2, incr
6005 // <binopb> r1, r3, incr
6006 // strexd storesuccess, r0, r1, ptr
6007 // cmp storesuccess, #0
6008 // bne- loopMBB
6009 // fallthrough --> exitMBB
6010 //
6011 // Note that the registers are explicitly specified because there is not any
6012 // way to force the register allocator to allocate a register pair.
6013 //
Andrew Trick3af7a672011-09-20 03:06:13 +00006014 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00006015 // need to properly enforce the restriction that the two output registers
6016 // for ldrexd must be different.
6017 BB = loopMBB;
6018 // Load
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006019 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6020 unsigned GPRPair1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
Silviu Baranga35b3df62012-11-29 14:41:25 +00006021 unsigned GPRPair2;
6022 if (IsMinMax) {
6023 //We need an extra double register for doing min/max.
6024 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6025 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6026 GPRPair2 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6027 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
6028 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6029 .addReg(undef)
6030 .addReg(vallo)
6031 .addImm(ARM::gsub_0);
6032 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair2)
6033 .addReg(r1)
6034 .addReg(valhi)
6035 .addImm(ARM::gsub_1);
6036 }
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006037
Eli Friedman2bdffe42011-08-31 00:31:29 +00006038 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006039 .addReg(GPRPair0, RegState::Define).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00006040 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006041 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6042 .addReg(GPRPair0, 0, ARM::gsub_0);
6043 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6044 .addReg(GPRPair0, 0, ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006045
6046 if (IsCmpxchg) {
6047 // Add early exit
6048 for (unsigned i = 0; i < 2; i++) {
6049 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6050 ARM::CMPrr))
6051 .addReg(i == 0 ? destlo : desthi)
6052 .addReg(i == 0 ? vallo : valhi));
6053 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6054 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6055 BB->addSuccessor(exitMBB);
6056 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6057 BB = (i == 0 ? contBB : cont2BB);
6058 }
6059
6060 // Copy to physregs for strexd
6061 unsigned setlo = MI->getOperand(5).getReg();
6062 unsigned sethi = MI->getOperand(6).getReg();
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006063 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6064 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6065 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
6066 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6067 .addReg(undef)
6068 .addReg(setlo)
6069 .addImm(ARM::gsub_0);
6070 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
6071 .addReg(r1)
6072 .addReg(sethi)
6073 .addImm(ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006074 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00006075 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006076 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6077 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00006078 .addReg(destlo).addReg(vallo))
6079 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006080 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6081 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006082 .addReg(desthi).addReg(valhi))
6083 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006084
6085 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6086 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6087 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6088 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6089 .addReg(UndefPair)
6090 .addReg(tmpRegLo)
6091 .addImm(ARM::gsub_0);
6092 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
6093 .addReg(r1)
6094 .addReg(tmpRegHi)
6095 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006096 } else {
6097 // Copy to physregs for strexd
Weiming Zhaoe56764b2012-11-16 21:55:34 +00006098 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6099 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6100 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6101 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6102 .addReg(UndefPair)
6103 .addReg(vallo)
6104 .addImm(ARM::gsub_0);
6105 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
6106 .addReg(r1)
6107 .addReg(valhi)
6108 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006109 }
Silviu Baranga35b3df62012-11-29 14:41:25 +00006110 unsigned GPRPairStore = GPRPair1;
6111 if (IsMinMax) {
6112 // Compare and branch to exit block.
6113 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6114 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6115 BB->addSuccessor(exitMBB);
6116 BB->addSuccessor(contBB);
6117 BB = contBB;
6118 GPRPairStore = GPRPair2;
6119 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00006120
6121 // Store
6122 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
Silviu Baranga35b3df62012-11-29 14:41:25 +00006123 .addReg(GPRPairStore).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00006124 // Cmp+jump
6125 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6126 .addReg(storesuccess).addImm(0));
6127 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6128 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6129
6130 BB->addSuccessor(loopMBB);
6131 BB->addSuccessor(exitMBB);
6132
6133 // exitMBB:
6134 // ...
6135 BB = exitMBB;
6136
6137 MI->eraseFromParent(); // The instruction is gone now.
6138
6139 return BB;
6140}
6141
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006142/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6143/// registers the function context.
6144void ARMTargetLowering::
6145SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6146 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006147 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6148 DebugLoc dl = MI->getDebugLoc();
6149 MachineFunction *MF = MBB->getParent();
6150 MachineRegisterInfo *MRI = &MF->getRegInfo();
6151 MachineConstantPool *MCP = MF->getConstantPool();
6152 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6153 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006154
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006155 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006156 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006157
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006158 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00006159 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006160 ARMConstantPoolValue *CPV =
6161 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6162 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6163
Craig Topper420761a2012-04-20 07:30:17 +00006164 const TargetRegisterClass *TRC = isThumb ?
6165 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6166 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006167
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006168 // Grab constant pool and fixed stack memory operands.
6169 MachineMemOperand *CPMMO =
6170 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6171 MachineMemOperand::MOLoad, 4, 4);
6172
6173 MachineMemOperand *FIMMOSt =
6174 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6175 MachineMemOperand::MOStore, 4, 4);
6176
6177 // Load the address of the dispatch MBB into the jump buffer.
6178 if (isThumb2) {
6179 // Incoming value: jbuf
6180 // ldr.n r5, LCPI1_1
6181 // orr r5, r5, #1
6182 // add r5, pc
6183 // str r5, [$jbuf, #+4] ; &jbuf[1]
6184 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6185 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6186 .addConstantPoolIndex(CPI)
6187 .addMemOperand(CPMMO));
6188 // Set the low bit because of thumb mode.
6189 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6190 AddDefaultCC(
6191 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6192 .addReg(NewVReg1, RegState::Kill)
6193 .addImm(0x01)));
6194 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6195 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6196 .addReg(NewVReg2, RegState::Kill)
6197 .addImm(PCLabelId);
6198 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6199 .addReg(NewVReg3, RegState::Kill)
6200 .addFrameIndex(FI)
6201 .addImm(36) // &jbuf[1] :: pc
6202 .addMemOperand(FIMMOSt));
6203 } else if (isThumb) {
6204 // Incoming value: jbuf
6205 // ldr.n r1, LCPI1_4
6206 // add r1, pc
6207 // mov r2, #1
6208 // orrs r1, r2
6209 // add r2, $jbuf, #+4 ; &jbuf[1]
6210 // str r1, [r2]
6211 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6212 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6213 .addConstantPoolIndex(CPI)
6214 .addMemOperand(CPMMO));
6215 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6216 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6217 .addReg(NewVReg1, RegState::Kill)
6218 .addImm(PCLabelId);
6219 // Set the low bit because of thumb mode.
6220 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6221 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6222 .addReg(ARM::CPSR, RegState::Define)
6223 .addImm(1));
6224 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6225 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6226 .addReg(ARM::CPSR, RegState::Define)
6227 .addReg(NewVReg2, RegState::Kill)
6228 .addReg(NewVReg3, RegState::Kill));
6229 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6230 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6231 .addFrameIndex(FI)
6232 .addImm(36)); // &jbuf[1] :: pc
6233 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6234 .addReg(NewVReg4, RegState::Kill)
6235 .addReg(NewVReg5, RegState::Kill)
6236 .addImm(0)
6237 .addMemOperand(FIMMOSt));
6238 } else {
6239 // Incoming value: jbuf
6240 // ldr r1, LCPI1_1
6241 // add r1, pc, r1
6242 // str r1, [$jbuf, #+4] ; &jbuf[1]
6243 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6244 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6245 .addConstantPoolIndex(CPI)
6246 .addImm(0)
6247 .addMemOperand(CPMMO));
6248 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6249 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6250 .addReg(NewVReg1, RegState::Kill)
6251 .addImm(PCLabelId));
6252 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6253 .addReg(NewVReg2, RegState::Kill)
6254 .addFrameIndex(FI)
6255 .addImm(36) // &jbuf[1] :: pc
6256 .addMemOperand(FIMMOSt));
6257 }
6258}
6259
6260MachineBasicBlock *ARMTargetLowering::
6261EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6262 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6263 DebugLoc dl = MI->getDebugLoc();
6264 MachineFunction *MF = MBB->getParent();
6265 MachineRegisterInfo *MRI = &MF->getRegInfo();
6266 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6267 MachineFrameInfo *MFI = MF->getFrameInfo();
6268 int FI = MFI->getFunctionContextIndex();
6269
Craig Topper420761a2012-04-20 07:30:17 +00006270 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6271 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006272 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006273
Bill Wendling04f15b42011-10-06 21:29:56 +00006274 // Get a mapping of the call site numbers to all of the landing pads they're
6275 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006276 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6277 unsigned MaxCSNum = 0;
6278 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006279 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6280 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006281 if (!BB->isLandingPad()) continue;
6282
6283 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6284 // pad.
6285 for (MachineBasicBlock::iterator
6286 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6287 if (!II->isEHLabel()) continue;
6288
6289 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006290 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006291
Bill Wendling5cbef192011-10-05 23:28:57 +00006292 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6293 for (SmallVectorImpl<unsigned>::iterator
6294 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6295 CSI != CSE; ++CSI) {
6296 CallSiteNumToLPad[*CSI].push_back(BB);
6297 MaxCSNum = std::max(MaxCSNum, *CSI);
6298 }
Bill Wendling2a850152011-10-05 00:02:33 +00006299 break;
6300 }
6301 }
6302
6303 // Get an ordered list of the machine basic blocks for the jump table.
6304 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006305 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006306 LPadList.reserve(CallSiteNumToLPad.size());
6307 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6308 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6309 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006310 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006311 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006312 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6313 }
Bill Wendling2a850152011-10-05 00:02:33 +00006314 }
6315
Bill Wendling5cbef192011-10-05 23:28:57 +00006316 assert(!LPadList.empty() &&
6317 "No landing pad destinations for the dispatch jump table!");
6318
Bill Wendling04f15b42011-10-06 21:29:56 +00006319 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006320 MachineJumpTableInfo *JTI =
6321 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6322 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6323 unsigned UId = AFI->createJumpTableUId();
6324
Bill Wendling04f15b42011-10-06 21:29:56 +00006325 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006326
6327 // Shove the dispatch's address into the return slot in the function context.
6328 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6329 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006330
Bill Wendlingbb734682011-10-05 00:39:32 +00006331 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00006332 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00006333 DispatchBB->addSuccessor(TrapBB);
6334
6335 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6336 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006337
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006338 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006339 MF->insert(MF->end(), DispatchBB);
6340 MF->insert(MF->end(), DispContBB);
6341 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006342
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006343 // Insert code into the entry block that creates and registers the function
6344 // context.
6345 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6346
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006347 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006348 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006349 MachineMemOperand::MOLoad |
6350 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006351
Chad Rosiere7bd5192012-11-06 23:05:24 +00006352 MachineInstrBuilder MIB;
6353 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6354
6355 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6356 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6357
6358 // Add a register mask with no preserved registers. This results in all
6359 // registers being marked as clobbered.
6360 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006361
Bill Wendling952cb502011-10-18 22:49:07 +00006362 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006363 if (Subtarget->isThumb2()) {
6364 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6365 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6366 .addFrameIndex(FI)
6367 .addImm(4)
6368 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006369
Bill Wendling952cb502011-10-18 22:49:07 +00006370 if (NumLPads < 256) {
6371 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6372 .addReg(NewVReg1)
6373 .addImm(LPadList.size()));
6374 } else {
6375 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6376 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006377 .addImm(NumLPads & 0xFFFF));
6378
6379 unsigned VReg2 = VReg1;
6380 if ((NumLPads & 0xFFFF0000) != 0) {
6381 VReg2 = MRI->createVirtualRegister(TRC);
6382 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6383 .addReg(VReg1)
6384 .addImm(NumLPads >> 16));
6385 }
6386
Bill Wendling952cb502011-10-18 22:49:07 +00006387 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6388 .addReg(NewVReg1)
6389 .addReg(VReg2));
6390 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006391
Bill Wendling95ce2e92011-10-06 22:53:00 +00006392 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6393 .addMBB(TrapBB)
6394 .addImm(ARMCC::HI)
6395 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006396
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006397 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6398 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006399 .addJumpTableIndex(MJTI)
6400 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006401
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006402 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006403 AddDefaultCC(
6404 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006405 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6406 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006407 .addReg(NewVReg1)
6408 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6409
6410 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006411 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006412 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006413 .addJumpTableIndex(MJTI)
6414 .addImm(UId);
6415 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006416 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6417 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6418 .addFrameIndex(FI)
6419 .addImm(1)
6420 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006421
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006422 if (NumLPads < 256) {
6423 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6424 .addReg(NewVReg1)
6425 .addImm(NumLPads));
6426 } else {
6427 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006428 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6429 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6430
6431 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006432 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006433 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006434 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006435 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006436
6437 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6438 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6439 .addReg(VReg1, RegState::Define)
6440 .addConstantPoolIndex(Idx));
6441 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6442 .addReg(NewVReg1)
6443 .addReg(VReg1));
6444 }
6445
Bill Wendling083a8eb2011-10-06 23:37:36 +00006446 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6447 .addMBB(TrapBB)
6448 .addImm(ARMCC::HI)
6449 .addReg(ARM::CPSR);
6450
6451 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6452 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6453 .addReg(ARM::CPSR, RegState::Define)
6454 .addReg(NewVReg1)
6455 .addImm(2));
6456
6457 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006458 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006459 .addJumpTableIndex(MJTI)
6460 .addImm(UId));
6461
6462 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6463 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6464 .addReg(ARM::CPSR, RegState::Define)
6465 .addReg(NewVReg2, RegState::Kill)
6466 .addReg(NewVReg3));
6467
6468 MachineMemOperand *JTMMOLd =
6469 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6470 MachineMemOperand::MOLoad, 4, 4);
6471
6472 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6473 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6474 .addReg(NewVReg4, RegState::Kill)
6475 .addImm(0)
6476 .addMemOperand(JTMMOLd));
6477
6478 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6479 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6480 .addReg(ARM::CPSR, RegState::Define)
6481 .addReg(NewVReg5, RegState::Kill)
6482 .addReg(NewVReg3));
6483
6484 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6485 .addReg(NewVReg6, RegState::Kill)
6486 .addJumpTableIndex(MJTI)
6487 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006488 } else {
6489 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6490 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6491 .addFrameIndex(FI)
6492 .addImm(4)
6493 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006494
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006495 if (NumLPads < 256) {
6496 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6497 .addReg(NewVReg1)
6498 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006499 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006500 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6501 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006502 .addImm(NumLPads & 0xFFFF));
6503
6504 unsigned VReg2 = VReg1;
6505 if ((NumLPads & 0xFFFF0000) != 0) {
6506 VReg2 = MRI->createVirtualRegister(TRC);
6507 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6508 .addReg(VReg1)
6509 .addImm(NumLPads >> 16));
6510 }
6511
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006512 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6513 .addReg(NewVReg1)
6514 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006515 } else {
6516 MachineConstantPool *ConstantPool = MF->getConstantPool();
6517 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6518 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6519
6520 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006521 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006522 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006523 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006524 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6525
6526 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6527 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6528 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006529 .addConstantPoolIndex(Idx)
6530 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006531 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6532 .addReg(NewVReg1)
6533 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006534 }
6535
Bill Wendling95ce2e92011-10-06 22:53:00 +00006536 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6537 .addMBB(TrapBB)
6538 .addImm(ARMCC::HI)
6539 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006540
Bill Wendling564392b2011-10-18 22:11:18 +00006541 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006542 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006543 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006544 .addReg(NewVReg1)
6545 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006546 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6547 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006548 .addJumpTableIndex(MJTI)
6549 .addImm(UId));
6550
6551 MachineMemOperand *JTMMOLd =
6552 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6553 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006554 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006555 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006556 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6557 .addReg(NewVReg3, RegState::Kill)
6558 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006559 .addImm(0)
6560 .addMemOperand(JTMMOLd));
6561
6562 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006563 .addReg(NewVReg5, RegState::Kill)
6564 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006565 .addJumpTableIndex(MJTI)
6566 .addImm(UId);
6567 }
Bill Wendling2a850152011-10-05 00:02:33 +00006568
Bill Wendlingbb734682011-10-05 00:39:32 +00006569 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006570 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006571 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006572 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6573 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006574 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006575 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006576 }
6577
Bill Wendling24bb9252011-10-17 05:25:09 +00006578 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006579 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006580 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006581 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6582 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6583 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006584
6585 // Remove the landing pad successor from the invoke block and replace it
6586 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006587 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6588 BB->succ_end());
6589 while (!Successors.empty()) {
6590 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006591 if (SMBB->isLandingPad()) {
6592 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006593 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006594 }
6595 }
6596
6597 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006598
6599 // Find the invoke call and mark all of the callee-saved registers as
6600 // 'implicit defined' so that they're spilled. This prevents code from
6601 // moving instructions to before the EH block, where they will never be
6602 // executed.
6603 for (MachineBasicBlock::reverse_iterator
6604 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006605 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006606
6607 DenseMap<unsigned, bool> DefRegs;
6608 for (MachineInstr::mop_iterator
6609 OI = II->operands_begin(), OE = II->operands_end();
6610 OI != OE; ++OI) {
6611 if (!OI->isReg()) continue;
6612 DefRegs[OI->getReg()] = true;
6613 }
6614
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00006615 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006616
Bill Wendling5d798592011-10-14 23:55:44 +00006617 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006618 unsigned Reg = SavedRegs[i];
6619 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006620 !ARM::tGPRRegClass.contains(Reg) &&
6621 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006622 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006623 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006624 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006625 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006626 continue;
6627 if (!DefRegs[Reg])
6628 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006629 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006630
6631 break;
6632 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006633 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006634
Bill Wendlingf7b02072011-10-18 18:30:49 +00006635 // Mark all former landing pads as non-landing pads. The dispatch is the only
6636 // landing pad now.
6637 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6638 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6639 (*I)->setIsLandingPad(false);
6640
Bill Wendlingbb734682011-10-05 00:39:32 +00006641 // The instruction is gone now.
6642 MI->eraseFromParent();
6643
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006644 return MBB;
6645}
6646
Evan Cheng218977b2010-07-13 19:27:42 +00006647static
6648MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6649 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6650 E = MBB->succ_end(); I != E; ++I)
6651 if (*I != Succ)
6652 return *I;
6653 llvm_unreachable("Expecting a BB with two successors!");
6654}
6655
Manman Ren68f25572012-06-01 19:33:18 +00006656MachineBasicBlock *ARMTargetLowering::
6657EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6658 // This pseudo instruction has 3 operands: dst, src, size
6659 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6660 // Otherwise, we will generate unrolled scalar copies.
6661 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6663 MachineFunction::iterator It = BB;
6664 ++It;
6665
6666 unsigned dest = MI->getOperand(0).getReg();
6667 unsigned src = MI->getOperand(1).getReg();
6668 unsigned SizeVal = MI->getOperand(2).getImm();
6669 unsigned Align = MI->getOperand(3).getImm();
6670 DebugLoc dl = MI->getDebugLoc();
6671
6672 bool isThumb2 = Subtarget->isThumb2();
6673 MachineFunction *MF = BB->getParent();
6674 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006675 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006676
6677 const TargetRegisterClass *TRC = isThumb2 ?
6678 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6679 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006680 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006681
6682 if (Align & 1) {
6683 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6684 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6685 UnitSize = 1;
6686 } else if (Align & 2) {
6687 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6688 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6689 UnitSize = 2;
6690 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006691 // Check whether we can use NEON instructions.
Bill Wendling831737d2012-12-30 10:32:01 +00006692 if (!MF->getFunction()->getAttributes().
6693 hasAttribute(AttributeSet::FunctionIndex,
6694 Attribute::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006695 Subtarget->hasNEON()) {
6696 if ((Align % 16 == 0) && SizeVal >= 16) {
6697 ldrOpc = ARM::VLD1q32wb_fixed;
6698 strOpc = ARM::VST1q32wb_fixed;
6699 UnitSize = 16;
6700 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6701 }
6702 else if ((Align % 8 == 0) && SizeVal >= 8) {
6703 ldrOpc = ARM::VLD1d32wb_fixed;
6704 strOpc = ARM::VST1d32wb_fixed;
6705 UnitSize = 8;
6706 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6707 }
6708 }
6709 // Can't use NEON instructions.
6710 if (UnitSize == 0) {
6711 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6712 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6713 UnitSize = 4;
6714 }
Manman Ren68f25572012-06-01 19:33:18 +00006715 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006716
Manman Ren68f25572012-06-01 19:33:18 +00006717 unsigned BytesLeft = SizeVal % UnitSize;
6718 unsigned LoopSize = SizeVal - BytesLeft;
6719
6720 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6721 // Use LDR and STR to copy.
6722 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6723 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6724 unsigned srcIn = src;
6725 unsigned destIn = dest;
6726 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006727 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006728 unsigned srcOut = MRI.createVirtualRegister(TRC);
6729 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006730 if (UnitSize >= 8) {
6731 AddDefaultPred(BuildMI(*BB, MI, dl,
6732 TII->get(ldrOpc), scratch)
6733 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6734
6735 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6736 .addReg(destIn).addImm(0).addReg(scratch));
6737 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006738 AddDefaultPred(BuildMI(*BB, MI, dl,
6739 TII->get(ldrOpc), scratch)
6740 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6741
6742 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6743 .addReg(scratch).addReg(destIn)
6744 .addImm(UnitSize));
6745 } else {
6746 AddDefaultPred(BuildMI(*BB, MI, dl,
6747 TII->get(ldrOpc), scratch)
6748 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6749 .addImm(UnitSize));
6750
6751 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6752 .addReg(scratch).addReg(destIn)
6753 .addReg(0).addImm(UnitSize));
6754 }
6755 srcIn = srcOut;
6756 destIn = destOut;
6757 }
6758
6759 // Handle the leftover bytes with LDRB and STRB.
6760 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6761 // [destOut] = STRB_POST(scratch, destIn, 1)
6762 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6763 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6764 for (unsigned i = 0; i < BytesLeft; i++) {
6765 unsigned scratch = MRI.createVirtualRegister(TRC);
6766 unsigned srcOut = MRI.createVirtualRegister(TRC);
6767 unsigned destOut = MRI.createVirtualRegister(TRC);
6768 if (isThumb2) {
6769 AddDefaultPred(BuildMI(*BB, MI, dl,
6770 TII->get(ldrOpc),scratch)
6771 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6772
6773 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6774 .addReg(scratch).addReg(destIn)
6775 .addReg(0).addImm(1));
6776 } else {
6777 AddDefaultPred(BuildMI(*BB, MI, dl,
6778 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006779 .addReg(srcOut, RegState::Define).addReg(srcIn)
6780 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006781
6782 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6783 .addReg(scratch).addReg(destIn)
6784 .addReg(0).addImm(1));
6785 }
6786 srcIn = srcOut;
6787 destIn = destOut;
6788 }
6789 MI->eraseFromParent(); // The instruction is gone now.
6790 return BB;
6791 }
6792
6793 // Expand the pseudo op to a loop.
6794 // thisMBB:
6795 // ...
6796 // movw varEnd, # --> with thumb2
6797 // movt varEnd, #
6798 // ldrcp varEnd, idx --> without thumb2
6799 // fallthrough --> loopMBB
6800 // loopMBB:
6801 // PHI varPhi, varEnd, varLoop
6802 // PHI srcPhi, src, srcLoop
6803 // PHI destPhi, dst, destLoop
6804 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6805 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6806 // subs varLoop, varPhi, #UnitSize
6807 // bne loopMBB
6808 // fallthrough --> exitMBB
6809 // exitMBB:
6810 // epilogue to handle left-over bytes
6811 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6812 // [destOut] = STRB_POST(scratch, destLoop, 1)
6813 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6814 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6815 MF->insert(It, loopMBB);
6816 MF->insert(It, exitMBB);
6817
6818 // Transfer the remainder of BB and its successor edges to exitMBB.
6819 exitMBB->splice(exitMBB->begin(), BB,
6820 llvm::next(MachineBasicBlock::iterator(MI)),
6821 BB->end());
6822 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6823
6824 // Load an immediate to varEnd.
6825 unsigned varEnd = MRI.createVirtualRegister(TRC);
6826 if (isThumb2) {
6827 unsigned VReg1 = varEnd;
6828 if ((LoopSize & 0xFFFF0000) != 0)
6829 VReg1 = MRI.createVirtualRegister(TRC);
6830 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6831 .addImm(LoopSize & 0xFFFF));
6832
6833 if ((LoopSize & 0xFFFF0000) != 0)
6834 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6835 .addReg(VReg1)
6836 .addImm(LoopSize >> 16));
6837 } else {
6838 MachineConstantPool *ConstantPool = MF->getConstantPool();
6839 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6840 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6841
6842 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006843 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006844 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006845 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006846 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6847
6848 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6849 .addReg(varEnd, RegState::Define)
6850 .addConstantPoolIndex(Idx)
6851 .addImm(0));
6852 }
6853 BB->addSuccessor(loopMBB);
6854
6855 // Generate the loop body:
6856 // varPhi = PHI(varLoop, varEnd)
6857 // srcPhi = PHI(srcLoop, src)
6858 // destPhi = PHI(destLoop, dst)
6859 MachineBasicBlock *entryBB = BB;
6860 BB = loopMBB;
6861 unsigned varLoop = MRI.createVirtualRegister(TRC);
6862 unsigned varPhi = MRI.createVirtualRegister(TRC);
6863 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6864 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6865 unsigned destLoop = MRI.createVirtualRegister(TRC);
6866 unsigned destPhi = MRI.createVirtualRegister(TRC);
6867
6868 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6869 .addReg(varLoop).addMBB(loopMBB)
6870 .addReg(varEnd).addMBB(entryBB);
6871 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6872 .addReg(srcLoop).addMBB(loopMBB)
6873 .addReg(src).addMBB(entryBB);
6874 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6875 .addReg(destLoop).addMBB(loopMBB)
6876 .addReg(dest).addMBB(entryBB);
6877
6878 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6879 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006880 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6881 if (UnitSize >= 8) {
6882 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6883 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6884
6885 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6886 .addReg(destPhi).addImm(0).addReg(scratch));
6887 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006888 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6889 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6890
6891 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6892 .addReg(scratch).addReg(destPhi)
6893 .addImm(UnitSize));
6894 } else {
6895 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6896 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6897 .addImm(UnitSize));
6898
6899 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6900 .addReg(scratch).addReg(destPhi)
6901 .addReg(0).addImm(UnitSize));
6902 }
6903
6904 // Decrement loop variable by UnitSize.
6905 MachineInstrBuilder MIB = BuildMI(BB, dl,
6906 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6907 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6908 MIB->getOperand(5).setReg(ARM::CPSR);
6909 MIB->getOperand(5).setIsDef(true);
6910
6911 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6912 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6913
6914 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6915 BB->addSuccessor(loopMBB);
6916 BB->addSuccessor(exitMBB);
6917
6918 // Add epilogue to handle BytesLeft.
6919 BB = exitMBB;
6920 MachineInstr *StartOfExit = exitMBB->begin();
6921 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6922 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6923
6924 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6925 // [destOut] = STRB_POST(scratch, destLoop, 1)
6926 unsigned srcIn = srcLoop;
6927 unsigned destIn = destLoop;
6928 for (unsigned i = 0; i < BytesLeft; i++) {
6929 unsigned scratch = MRI.createVirtualRegister(TRC);
6930 unsigned srcOut = MRI.createVirtualRegister(TRC);
6931 unsigned destOut = MRI.createVirtualRegister(TRC);
6932 if (isThumb2) {
6933 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6934 TII->get(ldrOpc),scratch)
6935 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6936
6937 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6938 .addReg(scratch).addReg(destIn)
6939 .addImm(1));
6940 } else {
6941 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6942 TII->get(ldrOpc),scratch)
6943 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6944
6945 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6946 .addReg(scratch).addReg(destIn)
6947 .addReg(0).addImm(1));
6948 }
6949 srcIn = srcOut;
6950 destIn = destOut;
6951 }
6952
6953 MI->eraseFromParent(); // The instruction is gone now.
6954 return BB;
6955}
6956
Jim Grosbache801dc42009-12-12 01:40:06 +00006957MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006958ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006959 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006960 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006961 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006962 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006963 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006964 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006965 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006966 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006967 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006968 // The Thumb2 pre-indexed stores have the same MI operands, they just
6969 // define them differently in the .td files from the isel patterns, so
6970 // they need pseudos.
6971 case ARM::t2STR_preidx:
6972 MI->setDesc(TII->get(ARM::t2STR_PRE));
6973 return BB;
6974 case ARM::t2STRB_preidx:
6975 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6976 return BB;
6977 case ARM::t2STRH_preidx:
6978 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6979 return BB;
6980
Jim Grosbach19dec202011-08-05 20:35:44 +00006981 case ARM::STRi_preidx:
6982 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006983 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006984 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6985 // Decode the offset.
6986 unsigned Offset = MI->getOperand(4).getImm();
6987 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6988 Offset = ARM_AM::getAM2Offset(Offset);
6989 if (isSub)
6990 Offset = -Offset;
6991
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006992 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006993 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006994 .addOperand(MI->getOperand(0)) // Rn_wb
6995 .addOperand(MI->getOperand(1)) // Rt
6996 .addOperand(MI->getOperand(2)) // Rn
6997 .addImm(Offset) // offset (skip GPR==zero_reg)
6998 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006999 .addOperand(MI->getOperand(6))
7000 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00007001 MI->eraseFromParent();
7002 return BB;
7003 }
7004 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00007005 case ARM::STRBr_preidx:
7006 case ARM::STRH_preidx: {
7007 unsigned NewOpc;
7008 switch (MI->getOpcode()) {
7009 default: llvm_unreachable("unexpected opcode!");
7010 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7011 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7012 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7013 }
Jim Grosbach19dec202011-08-05 20:35:44 +00007014 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7015 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7016 MIB.addOperand(MI->getOperand(i));
7017 MI->eraseFromParent();
7018 return BB;
7019 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007020 case ARM::ATOMIC_LOAD_ADD_I8:
7021 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7022 case ARM::ATOMIC_LOAD_ADD_I16:
7023 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7024 case ARM::ATOMIC_LOAD_ADD_I32:
7025 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007026
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007027 case ARM::ATOMIC_LOAD_AND_I8:
7028 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7029 case ARM::ATOMIC_LOAD_AND_I16:
7030 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7031 case ARM::ATOMIC_LOAD_AND_I32:
7032 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007033
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007034 case ARM::ATOMIC_LOAD_OR_I8:
7035 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7036 case ARM::ATOMIC_LOAD_OR_I16:
7037 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7038 case ARM::ATOMIC_LOAD_OR_I32:
7039 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007040
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007041 case ARM::ATOMIC_LOAD_XOR_I8:
7042 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7043 case ARM::ATOMIC_LOAD_XOR_I16:
7044 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7045 case ARM::ATOMIC_LOAD_XOR_I32:
7046 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007047
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007048 case ARM::ATOMIC_LOAD_NAND_I8:
7049 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7050 case ARM::ATOMIC_LOAD_NAND_I16:
7051 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7052 case ARM::ATOMIC_LOAD_NAND_I32:
7053 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007054
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007055 case ARM::ATOMIC_LOAD_SUB_I8:
7056 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7057 case ARM::ATOMIC_LOAD_SUB_I16:
7058 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7059 case ARM::ATOMIC_LOAD_SUB_I32:
7060 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00007061
Jim Grosbachf7da8822011-04-26 19:44:18 +00007062 case ARM::ATOMIC_LOAD_MIN_I8:
7063 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7064 case ARM::ATOMIC_LOAD_MIN_I16:
7065 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7066 case ARM::ATOMIC_LOAD_MIN_I32:
7067 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7068
7069 case ARM::ATOMIC_LOAD_MAX_I8:
7070 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7071 case ARM::ATOMIC_LOAD_MAX_I16:
7072 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7073 case ARM::ATOMIC_LOAD_MAX_I32:
7074 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7075
7076 case ARM::ATOMIC_LOAD_UMIN_I8:
7077 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7078 case ARM::ATOMIC_LOAD_UMIN_I16:
7079 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7080 case ARM::ATOMIC_LOAD_UMIN_I32:
7081 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7082
7083 case ARM::ATOMIC_LOAD_UMAX_I8:
7084 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7085 case ARM::ATOMIC_LOAD_UMAX_I16:
7086 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7087 case ARM::ATOMIC_LOAD_UMAX_I32:
7088 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7089
Jim Grosbacha36c8f22009-12-14 20:14:59 +00007090 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7091 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7092 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00007093
7094 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7095 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7096 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00007097
Eli Friedman2bdffe42011-08-31 00:31:29 +00007098
7099 case ARM::ATOMADD6432:
7100 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007101 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7102 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007103 case ARM::ATOMSUB6432:
7104 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007105 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7106 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007107 case ARM::ATOMOR6432:
7108 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007109 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007110 case ARM::ATOMXOR6432:
7111 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007112 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007113 case ARM::ATOMAND6432:
7114 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00007115 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007116 case ARM::ATOMSWAP6432:
7117 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00007118 case ARM::ATOMCMPXCHG6432:
7119 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7120 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7121 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007122 case ARM::ATOMMIN6432:
7123 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7124 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7125 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007126 /*IsMinMax*/ true, ARMCC::LT);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007127 case ARM::ATOMMAX6432:
7128 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7129 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7130 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7131 /*IsMinMax*/ true, ARMCC::GE);
7132 case ARM::ATOMUMIN6432:
7133 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7134 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7135 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
Silviu Baranga4a9256f2013-01-25 10:39:49 +00007136 /*IsMinMax*/ true, ARMCC::LO);
Silviu Baranga35b3df62012-11-29 14:41:25 +00007137 case ARM::ATOMUMAX6432:
7138 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7139 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7140 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7141 /*IsMinMax*/ true, ARMCC::HS);
Eli Friedman2bdffe42011-08-31 00:31:29 +00007142
Evan Cheng007ea272009-08-12 05:17:19 +00007143 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00007144 // To "insert" a SELECT_CC instruction, we actually have to insert the
7145 // diamond control-flow pattern. The incoming instruction knows the
7146 // destination vreg to set, the condition code register to branch on, the
7147 // true/false values to select between, and a branch opcode to use.
7148 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007149 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00007150 ++It;
7151
7152 // thisMBB:
7153 // ...
7154 // TrueVal = ...
7155 // cmpTY ccX, r1, r2
7156 // bCC copy1MBB
7157 // fallthrough --> copy0MBB
7158 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007159 MachineFunction *F = BB->getParent();
7160 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7161 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00007162 F->insert(It, copy0MBB);
7163 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00007164
7165 // Transfer the remainder of BB and its successor edges to sinkMBB.
7166 sinkMBB->splice(sinkMBB->begin(), BB,
7167 llvm::next(MachineBasicBlock::iterator(MI)),
7168 BB->end());
7169 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7170
Dan Gohman258c58c2010-07-06 15:49:48 +00007171 BB->addSuccessor(copy0MBB);
7172 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00007173
Dan Gohman14152b42010-07-06 20:24:04 +00007174 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7175 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7176
Evan Chenga8e29892007-01-19 07:51:42 +00007177 // copy0MBB:
7178 // %FalseValue = ...
7179 // # fallthrough to sinkMBB
7180 BB = copy0MBB;
7181
7182 // Update machine-CFG edges
7183 BB->addSuccessor(sinkMBB);
7184
7185 // sinkMBB:
7186 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7187 // ...
7188 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00007189 BuildMI(*BB, BB->begin(), dl,
7190 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00007191 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7192 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7193
Dan Gohman14152b42010-07-06 20:24:04 +00007194 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00007195 return BB;
7196 }
Evan Cheng86198642009-08-07 00:34:42 +00007197
Evan Cheng218977b2010-07-13 19:27:42 +00007198 case ARM::BCCi64:
7199 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00007200 // If there is an unconditional branch to the other successor, remove it.
7201 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00007202
Evan Cheng218977b2010-07-13 19:27:42 +00007203 // Compare both parts that make up the double comparison separately for
7204 // equality.
7205 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7206
7207 unsigned LHS1 = MI->getOperand(1).getReg();
7208 unsigned LHS2 = MI->getOperand(2).getReg();
7209 if (RHSisZero) {
7210 AddDefaultPred(BuildMI(BB, dl,
7211 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7212 .addReg(LHS1).addImm(0));
7213 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7214 .addReg(LHS2).addImm(0)
7215 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7216 } else {
7217 unsigned RHS1 = MI->getOperand(3).getReg();
7218 unsigned RHS2 = MI->getOperand(4).getReg();
7219 AddDefaultPred(BuildMI(BB, dl,
7220 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7221 .addReg(LHS1).addReg(RHS1));
7222 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7223 .addReg(LHS2).addReg(RHS2)
7224 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7225 }
7226
7227 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7228 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7229 if (MI->getOperand(0).getImm() == ARMCC::NE)
7230 std::swap(destMBB, exitMBB);
7231
7232 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7233 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00007234 if (isThumb2)
7235 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7236 else
7237 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00007238
7239 MI->eraseFromParent(); // The pseudo instruction is gone now.
7240 return BB;
7241 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007242
Bill Wendling5bc85282011-10-17 20:37:20 +00007243 case ARM::Int_eh_sjlj_setjmp:
7244 case ARM::Int_eh_sjlj_setjmp_nofp:
7245 case ARM::tInt_eh_sjlj_setjmp:
7246 case ARM::t2Int_eh_sjlj_setjmp:
7247 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7248 EmitSjLjDispatchBlock(MI, BB);
7249 return BB;
7250
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007251 case ARM::ABS:
7252 case ARM::t2ABS: {
7253 // To insert an ABS instruction, we have to insert the
7254 // diamond control-flow pattern. The incoming instruction knows the
7255 // source vreg to test against 0, the destination vreg to set,
7256 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007257 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007258 // It transforms
7259 // V1 = ABS V0
7260 // into
7261 // V2 = MOVS V0
7262 // BCC (branch to SinkBB if V0 >= 0)
7263 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007264 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007265 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7266 MachineFunction::iterator BBI = BB;
7267 ++BBI;
7268 MachineFunction *Fn = BB->getParent();
7269 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7270 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7271 Fn->insert(BBI, RSBBB);
7272 Fn->insert(BBI, SinkBB);
7273
7274 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7275 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7276 bool isThumb2 = Subtarget->isThumb2();
7277 MachineRegisterInfo &MRI = Fn->getRegInfo();
7278 // In Thumb mode S must not be specified if source register is the SP or
7279 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007280 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7281 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7282 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007283
7284 // Transfer the remainder of BB and its successor edges to sinkMBB.
7285 SinkBB->splice(SinkBB->begin(), BB,
7286 llvm::next(MachineBasicBlock::iterator(MI)),
7287 BB->end());
7288 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7289
7290 BB->addSuccessor(RSBBB);
7291 BB->addSuccessor(SinkBB);
7292
7293 // fall through to SinkMBB
7294 RSBBB->addSuccessor(SinkBB);
7295
Manman Ren307473d2012-06-15 21:32:12 +00007296 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007297 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007298 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7299 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007300
7301 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007302 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007303 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7304 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7305
7306 // insert rsbri in RSBBB
7307 // Note: BCC and rsbri will be converted into predicated rsbmi
7308 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007309 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007310 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007311 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007312 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7313
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007314 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007315 // reuse ABSDstReg to not change uses of ABS instruction
7316 BuildMI(*SinkBB, SinkBB->begin(), dl,
7317 TII->get(ARM::PHI), ABSDstReg)
7318 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007319 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007320
7321 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007322 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007323
7324 // return last added BB
7325 return SinkBB;
7326 }
Manman Ren68f25572012-06-01 19:33:18 +00007327 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007328 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007329 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007330 }
7331}
7332
Evan Cheng37fefc22011-08-30 19:09:48 +00007333void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7334 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007335 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007336 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7337 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7338 return;
7339 }
7340
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007341 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007342 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7343 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7344 // operand is still set to noreg. If needed, set the optional operand's
7345 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007346 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007347 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007348
Andrew Trick3be654f2011-09-21 02:20:46 +00007349 // Rename pseudo opcodes.
7350 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7351 if (NewOpc) {
7352 const ARMBaseInstrInfo *TII =
7353 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007354 MCID = &TII->get(NewOpc);
7355
7356 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7357 "converted opcode should be the same except for cc_out");
7358
7359 MI->setDesc(*MCID);
7360
7361 // Add the optional cc_out operand
7362 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007363 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007364 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007365
7366 // Any ARM instruction that sets the 's' bit should specify an optional
7367 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007368 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007369 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007370 return;
7371 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007372 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7373 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007374 bool definesCPSR = false;
7375 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007376 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007377 i != e; ++i) {
7378 const MachineOperand &MO = MI->getOperand(i);
7379 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7380 definesCPSR = true;
7381 if (MO.isDead())
7382 deadCPSR = true;
7383 MI->RemoveOperand(i);
7384 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007385 }
7386 }
Andrew Trick4815d562011-09-20 03:17:40 +00007387 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007388 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007389 return;
7390 }
7391 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007392 if (deadCPSR) {
7393 assert(!MI->getOperand(ccOutIdx).getReg() &&
7394 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007395 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007396 }
Andrew Trick4815d562011-09-20 03:17:40 +00007397
Andrew Trick3be654f2011-09-21 02:20:46 +00007398 // If this instruction was defined with an optional CPSR def and its dag node
7399 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007400 MachineOperand &MO = MI->getOperand(ccOutIdx);
7401 MO.setReg(ARM::CPSR);
7402 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007403}
7404
Evan Chenga8e29892007-01-19 07:51:42 +00007405//===----------------------------------------------------------------------===//
7406// ARM Optimization Hooks
7407//===----------------------------------------------------------------------===//
7408
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007409// Helper function that checks if N is a null or all ones constant.
7410static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7411 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7412 if (!C)
7413 return false;
7414 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7415}
7416
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007417// Return true if N is conditionally 0 or all ones.
7418// Detects these expressions where cc is an i1 value:
7419//
7420// (select cc 0, y) [AllOnes=0]
7421// (select cc y, 0) [AllOnes=0]
7422// (zext cc) [AllOnes=0]
7423// (sext cc) [AllOnes=0/1]
7424// (select cc -1, y) [AllOnes=1]
7425// (select cc y, -1) [AllOnes=1]
7426//
7427// Invert is set when N is the null/all ones constant when CC is false.
7428// OtherOp is set to the alternative value of N.
7429static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7430 SDValue &CC, bool &Invert,
7431 SDValue &OtherOp,
7432 SelectionDAG &DAG) {
7433 switch (N->getOpcode()) {
7434 default: return false;
7435 case ISD::SELECT: {
7436 CC = N->getOperand(0);
7437 SDValue N1 = N->getOperand(1);
7438 SDValue N2 = N->getOperand(2);
7439 if (isZeroOrAllOnes(N1, AllOnes)) {
7440 Invert = false;
7441 OtherOp = N2;
7442 return true;
7443 }
7444 if (isZeroOrAllOnes(N2, AllOnes)) {
7445 Invert = true;
7446 OtherOp = N1;
7447 return true;
7448 }
7449 return false;
7450 }
7451 case ISD::ZERO_EXTEND:
7452 // (zext cc) can never be the all ones value.
7453 if (AllOnes)
7454 return false;
7455 // Fall through.
7456 case ISD::SIGN_EXTEND: {
7457 EVT VT = N->getValueType(0);
7458 CC = N->getOperand(0);
7459 if (CC.getValueType() != MVT::i1)
7460 return false;
7461 Invert = !AllOnes;
7462 if (AllOnes)
7463 // When looking for an AllOnes constant, N is an sext, and the 'other'
7464 // value is 0.
7465 OtherOp = DAG.getConstant(0, VT);
7466 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7467 // When looking for a 0 constant, N can be zext or sext.
7468 OtherOp = DAG.getConstant(1, VT);
7469 else
7470 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7471 return true;
7472 }
7473 }
7474}
7475
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007476// Combine a constant select operand into its use:
7477//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007478// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7479// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7480// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7481// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7482// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007483//
7484// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007485// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007486//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007487// Also recognize sext/zext from i1:
7488//
7489// (add (zext cc), x) -> (select cc (add x, 1), x)
7490// (add (sext cc), x) -> (select cc (add x, -1), x)
7491//
7492// These transformations eventually create predicated instructions.
7493//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007494// @param N The node to transform.
7495// @param Slct The N operand that is a select.
7496// @param OtherOp The other N operand (x above).
7497// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007498// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007499// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007500static
7501SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007502 TargetLowering::DAGCombinerInfo &DCI,
7503 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007504 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007505 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007506 SDValue NonConstantVal;
7507 SDValue CCOp;
7508 bool SwapSelectOps;
7509 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7510 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007511 return SDValue();
7512
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007513 // Slct is now know to be the desired identity constant when CC is true.
7514 SDValue TrueVal = OtherOp;
7515 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7516 OtherOp, NonConstantVal);
7517 // Unless SwapSelectOps says CC should be false.
7518 if (SwapSelectOps)
7519 std::swap(TrueVal, FalseVal);
7520
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007521 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007522 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007523}
7524
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007525// Attempt combineSelectAndUse on each operand of a commutative operator N.
7526static
7527SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7528 TargetLowering::DAGCombinerInfo &DCI) {
7529 SDValue N0 = N->getOperand(0);
7530 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007531 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007532 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7533 if (Result.getNode())
7534 return Result;
7535 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007536 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007537 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7538 if (Result.getNode())
7539 return Result;
7540 }
7541 return SDValue();
7542}
7543
Eric Christopherfa6f5912011-06-29 21:10:36 +00007544// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007545// (only after legalization).
7546static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7547 TargetLowering::DAGCombinerInfo &DCI,
7548 const ARMSubtarget *Subtarget) {
7549
7550 // Only perform optimization if after legalize, and if NEON is available. We
7551 // also expected both operands to be BUILD_VECTORs.
7552 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7553 || N0.getOpcode() != ISD::BUILD_VECTOR
7554 || N1.getOpcode() != ISD::BUILD_VECTOR)
7555 return SDValue();
7556
7557 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7558 EVT VT = N->getValueType(0);
7559 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7560 return SDValue();
7561
7562 // Check that the vector operands are of the right form.
7563 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7564 // operands, where N is the size of the formed vector.
7565 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7566 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007567
7568 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007569 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007570 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007571 SDValue Vec = N0->getOperand(0)->getOperand(0);
7572 SDNode *V = Vec.getNode();
7573 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007574
Eric Christopherfa6f5912011-06-29 21:10:36 +00007575 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007576 // check to see if each of their operands are an EXTRACT_VECTOR with
7577 // the same vector and appropriate index.
7578 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7579 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7580 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007581
Tanya Lattner189531f2011-06-14 23:48:48 +00007582 SDValue ExtVec0 = N0->getOperand(i);
7583 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007584
Tanya Lattner189531f2011-06-14 23:48:48 +00007585 // First operand is the vector, verify its the same.
7586 if (V != ExtVec0->getOperand(0).getNode() ||
7587 V != ExtVec1->getOperand(0).getNode())
7588 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007589
Tanya Lattner189531f2011-06-14 23:48:48 +00007590 // Second is the constant, verify its correct.
7591 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7592 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007593
Tanya Lattner189531f2011-06-14 23:48:48 +00007594 // For the constant, we want to see all the even or all the odd.
7595 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7596 || C1->getZExtValue() != nextIndex+1)
7597 return SDValue();
7598
7599 // Increment index.
7600 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007601 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007602 return SDValue();
7603 }
7604
7605 // Create VPADDL node.
7606 SelectionDAG &DAG = DCI.DAG;
7607 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007608
7609 // Build operand list.
7610 SmallVector<SDValue, 8> Ops;
7611 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7612 TLI.getPointerTy()));
7613
7614 // Input is the vector.
7615 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007616
Tanya Lattner189531f2011-06-14 23:48:48 +00007617 // Get widened type and narrowed type.
7618 MVT widenType;
7619 unsigned numElem = VT.getVectorNumElements();
7620 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7621 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7622 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7623 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7624 default:
Craig Topperbc219812012-02-07 02:50:20 +00007625 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007626 }
7627
7628 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7629 widenType, &Ops[0], Ops.size());
7630 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7631}
7632
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007633static SDValue findMUL_LOHI(SDValue V) {
7634 if (V->getOpcode() == ISD::UMUL_LOHI ||
7635 V->getOpcode() == ISD::SMUL_LOHI)
7636 return V;
7637 return SDValue();
7638}
7639
7640static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7641 TargetLowering::DAGCombinerInfo &DCI,
7642 const ARMSubtarget *Subtarget) {
7643
7644 if (Subtarget->isThumb1Only()) return SDValue();
7645
7646 // Only perform the checks after legalize when the pattern is available.
7647 if (DCI.isBeforeLegalize()) return SDValue();
7648
7649 // Look for multiply add opportunities.
7650 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7651 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7652 // a glue link from the first add to the second add.
7653 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7654 // a S/UMLAL instruction.
7655 // loAdd UMUL_LOHI
7656 // \ / :lo \ :hi
7657 // \ / \ [no multiline comment]
7658 // ADDC | hiAdd
7659 // \ :glue / /
7660 // \ / /
7661 // ADDE
7662 //
7663 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7664 SDValue AddcOp0 = AddcNode->getOperand(0);
7665 SDValue AddcOp1 = AddcNode->getOperand(1);
7666
7667 // Check if the two operands are from the same mul_lohi node.
7668 if (AddcOp0.getNode() == AddcOp1.getNode())
7669 return SDValue();
7670
7671 assert(AddcNode->getNumValues() == 2 &&
7672 AddcNode->getValueType(0) == MVT::i32 &&
7673 AddcNode->getValueType(1) == MVT::Glue &&
7674 "Expect ADDC with two result values: i32, glue");
7675
7676 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7677 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7678 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7679 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7680 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7681 return SDValue();
7682
7683 // Look for the glued ADDE.
7684 SDNode* AddeNode = AddcNode->getGluedUser();
7685 if (AddeNode == NULL)
7686 return SDValue();
7687
7688 // Make sure it is really an ADDE.
7689 if (AddeNode->getOpcode() != ISD::ADDE)
7690 return SDValue();
7691
7692 assert(AddeNode->getNumOperands() == 3 &&
7693 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7694 "ADDE node has the wrong inputs");
7695
7696 // Check for the triangle shape.
7697 SDValue AddeOp0 = AddeNode->getOperand(0);
7698 SDValue AddeOp1 = AddeNode->getOperand(1);
7699
7700 // Make sure that the ADDE operands are not coming from the same node.
7701 if (AddeOp0.getNode() == AddeOp1.getNode())
7702 return SDValue();
7703
7704 // Find the MUL_LOHI node walking up ADDE's operands.
7705 bool IsLeftOperandMUL = false;
7706 SDValue MULOp = findMUL_LOHI(AddeOp0);
7707 if (MULOp == SDValue())
7708 MULOp = findMUL_LOHI(AddeOp1);
7709 else
7710 IsLeftOperandMUL = true;
7711 if (MULOp == SDValue())
7712 return SDValue();
7713
7714 // Figure out the right opcode.
7715 unsigned Opc = MULOp->getOpcode();
7716 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7717
7718 // Figure out the high and low input values to the MLAL node.
7719 SDValue* HiMul = &MULOp;
7720 SDValue* HiAdd = NULL;
7721 SDValue* LoMul = NULL;
7722 SDValue* LowAdd = NULL;
7723
7724 if (IsLeftOperandMUL)
7725 HiAdd = &AddeOp1;
7726 else
7727 HiAdd = &AddeOp0;
7728
7729
7730 if (AddcOp0->getOpcode() == Opc) {
7731 LoMul = &AddcOp0;
7732 LowAdd = &AddcOp1;
7733 }
7734 if (AddcOp1->getOpcode() == Opc) {
7735 LoMul = &AddcOp1;
7736 LowAdd = &AddcOp0;
7737 }
7738
7739 if (LoMul == NULL)
7740 return SDValue();
7741
7742 if (LoMul->getNode() != HiMul->getNode())
7743 return SDValue();
7744
7745 // Create the merged node.
7746 SelectionDAG &DAG = DCI.DAG;
7747
7748 // Build operand list.
7749 SmallVector<SDValue, 8> Ops;
7750 Ops.push_back(LoMul->getOperand(0));
7751 Ops.push_back(LoMul->getOperand(1));
7752 Ops.push_back(*LowAdd);
7753 Ops.push_back(*HiAdd);
7754
7755 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7756 DAG.getVTList(MVT::i32, MVT::i32),
7757 &Ops[0], Ops.size());
7758
7759 // Replace the ADDs' nodes uses by the MLA node's values.
7760 SDValue HiMLALResult(MLALNode.getNode(), 1);
7761 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7762
7763 SDValue LoMLALResult(MLALNode.getNode(), 0);
7764 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7765
7766 // Return original node to notify the driver to stop replacing.
7767 SDValue resNode(AddcNode, 0);
7768 return resNode;
7769}
7770
7771/// PerformADDCCombine - Target-specific dag combine transform from
7772/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7773static SDValue PerformADDCCombine(SDNode *N,
7774 TargetLowering::DAGCombinerInfo &DCI,
7775 const ARMSubtarget *Subtarget) {
7776
7777 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7778
7779}
7780
Bob Wilson3d5792a2010-07-29 20:34:14 +00007781/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7782/// operands N0 and N1. This is a helper for PerformADDCombine that is
7783/// called with the default operands, and if that fails, with commuted
7784/// operands.
7785static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007786 TargetLowering::DAGCombinerInfo &DCI,
7787 const ARMSubtarget *Subtarget){
7788
7789 // Attempt to create vpaddl for this add.
7790 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7791 if (Result.getNode())
7792 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007793
Chris Lattnerd1980a52009-03-12 06:52:53 +00007794 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007795 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007796 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7797 if (Result.getNode()) return Result;
7798 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007799 return SDValue();
7800}
7801
Bob Wilson3d5792a2010-07-29 20:34:14 +00007802/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7803///
7804static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007805 TargetLowering::DAGCombinerInfo &DCI,
7806 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007807 SDValue N0 = N->getOperand(0);
7808 SDValue N1 = N->getOperand(1);
7809
7810 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007811 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007812 if (Result.getNode())
7813 return Result;
7814
7815 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007816 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007817}
7818
Chris Lattnerd1980a52009-03-12 06:52:53 +00007819/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007820///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007821static SDValue PerformSUBCombine(SDNode *N,
7822 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007823 SDValue N0 = N->getOperand(0);
7824 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007825
Chris Lattnerd1980a52009-03-12 06:52:53 +00007826 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007827 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007828 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7829 if (Result.getNode()) return Result;
7830 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007831
Chris Lattnerd1980a52009-03-12 06:52:53 +00007832 return SDValue();
7833}
7834
Evan Cheng463d3582011-03-31 19:38:48 +00007835/// PerformVMULCombine
7836/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7837/// special multiplier accumulator forwarding.
7838/// vmul d3, d0, d2
7839/// vmla d3, d1, d2
7840/// is faster than
7841/// vadd d3, d0, d1
7842/// vmul d3, d3, d2
7843static SDValue PerformVMULCombine(SDNode *N,
7844 TargetLowering::DAGCombinerInfo &DCI,
7845 const ARMSubtarget *Subtarget) {
7846 if (!Subtarget->hasVMLxForwarding())
7847 return SDValue();
7848
7849 SelectionDAG &DAG = DCI.DAG;
7850 SDValue N0 = N->getOperand(0);
7851 SDValue N1 = N->getOperand(1);
7852 unsigned Opcode = N0.getOpcode();
7853 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7854 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007855 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007856 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7857 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7858 return SDValue();
7859 std::swap(N0, N1);
7860 }
7861
7862 EVT VT = N->getValueType(0);
7863 DebugLoc DL = N->getDebugLoc();
7864 SDValue N00 = N0->getOperand(0);
7865 SDValue N01 = N0->getOperand(1);
7866 return DAG.getNode(Opcode, DL, VT,
7867 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7868 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7869}
7870
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007871static SDValue PerformMULCombine(SDNode *N,
7872 TargetLowering::DAGCombinerInfo &DCI,
7873 const ARMSubtarget *Subtarget) {
7874 SelectionDAG &DAG = DCI.DAG;
7875
7876 if (Subtarget->isThumb1Only())
7877 return SDValue();
7878
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007879 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7880 return SDValue();
7881
7882 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007883 if (VT.is64BitVector() || VT.is128BitVector())
7884 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007885 if (VT != MVT::i32)
7886 return SDValue();
7887
7888 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7889 if (!C)
7890 return SDValue();
7891
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007892 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007893 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007894
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007895 ShiftAmt = ShiftAmt & (32 - 1);
7896 SDValue V = N->getOperand(0);
7897 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007898
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007899 SDValue Res;
7900 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007901
7902 if (MulAmt >= 0) {
7903 if (isPowerOf2_32(MulAmt - 1)) {
7904 // (mul x, 2^N + 1) => (add (shl x, N), x)
7905 Res = DAG.getNode(ISD::ADD, DL, VT,
7906 V,
7907 DAG.getNode(ISD::SHL, DL, VT,
7908 V,
7909 DAG.getConstant(Log2_32(MulAmt - 1),
7910 MVT::i32)));
7911 } else if (isPowerOf2_32(MulAmt + 1)) {
7912 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7913 Res = DAG.getNode(ISD::SUB, DL, VT,
7914 DAG.getNode(ISD::SHL, DL, VT,
7915 V,
7916 DAG.getConstant(Log2_32(MulAmt + 1),
7917 MVT::i32)),
7918 V);
7919 } else
7920 return SDValue();
7921 } else {
7922 uint64_t MulAmtAbs = -MulAmt;
7923 if (isPowerOf2_32(MulAmtAbs + 1)) {
7924 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7925 Res = DAG.getNode(ISD::SUB, DL, VT,
7926 V,
7927 DAG.getNode(ISD::SHL, DL, VT,
7928 V,
7929 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7930 MVT::i32)));
7931 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7932 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7933 Res = DAG.getNode(ISD::ADD, DL, VT,
7934 V,
7935 DAG.getNode(ISD::SHL, DL, VT,
7936 V,
7937 DAG.getConstant(Log2_32(MulAmtAbs-1),
7938 MVT::i32)));
7939 Res = DAG.getNode(ISD::SUB, DL, VT,
7940 DAG.getConstant(0, MVT::i32),Res);
7941
7942 } else
7943 return SDValue();
7944 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007945
7946 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007947 Res = DAG.getNode(ISD::SHL, DL, VT,
7948 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007949
7950 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007951 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007952 return SDValue();
7953}
7954
Owen Anderson080c0922010-11-05 19:27:46 +00007955static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007956 TargetLowering::DAGCombinerInfo &DCI,
7957 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007958
Owen Anderson080c0922010-11-05 19:27:46 +00007959 // Attempt to use immediate-form VBIC
7960 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7961 DebugLoc dl = N->getDebugLoc();
7962 EVT VT = N->getValueType(0);
7963 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007964
Tanya Lattner0433b212011-04-07 15:24:20 +00007965 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7966 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007967
Owen Anderson080c0922010-11-05 19:27:46 +00007968 APInt SplatBits, SplatUndef;
7969 unsigned SplatBitSize;
7970 bool HasAnyUndefs;
7971 if (BVN &&
7972 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7973 if (SplatBitSize <= 64) {
7974 EVT VbicVT;
7975 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7976 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007977 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007978 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007979 if (Val.getNode()) {
7980 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007981 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007982 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007983 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007984 }
7985 }
7986 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007987
Evan Chengc892aeb2012-02-23 01:19:06 +00007988 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007989 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7990 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7991 if (Result.getNode())
7992 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007993 }
7994
Owen Anderson080c0922010-11-05 19:27:46 +00007995 return SDValue();
7996}
7997
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007998/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7999static SDValue PerformORCombine(SDNode *N,
8000 TargetLowering::DAGCombinerInfo &DCI,
8001 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00008002 // Attempt to use immediate-form VORR
8003 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8004 DebugLoc dl = N->getDebugLoc();
8005 EVT VT = N->getValueType(0);
8006 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008007
Tanya Lattner0433b212011-04-07 15:24:20 +00008008 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8009 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00008010
Owen Anderson60f48702010-11-03 23:15:26 +00008011 APInt SplatBits, SplatUndef;
8012 unsigned SplatBitSize;
8013 bool HasAnyUndefs;
8014 if (BVN && Subtarget->hasNEON() &&
8015 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8016 if (SplatBitSize <= 64) {
8017 EVT VorrVT;
8018 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8019 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00008020 DAG, VorrVT, VT.is128BitVector(),
8021 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00008022 if (Val.getNode()) {
8023 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008024 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00008025 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008026 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00008027 }
8028 }
8029 }
8030
Evan Chengc892aeb2012-02-23 01:19:06 +00008031 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008032 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8033 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8034 if (Result.getNode())
8035 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008036 }
8037
Nadav Rotemdf832032012-08-13 18:52:44 +00008038 // The code below optimizes (or (and X, Y), Z).
8039 // The AND operand needs to have a single user to make these optimizations
8040 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008041 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00008042 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008043 return SDValue();
8044 SDValue N1 = N->getOperand(1);
8045
8046 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8047 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8048 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8049 APInt SplatUndef;
8050 unsigned SplatBitSize;
8051 bool HasAnyUndefs;
8052
8053 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8054 APInt SplatBits0;
8055 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8056 HasAnyUndefs) && !HasAnyUndefs) {
8057 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8058 APInt SplatBits1;
8059 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8060 HasAnyUndefs) && !HasAnyUndefs &&
8061 SplatBits0 == ~SplatBits1) {
8062 // Canonicalize the vector type to make instruction selection simpler.
8063 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8064 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8065 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00008066 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00008067 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8068 }
8069 }
8070 }
8071
Jim Grosbach54238562010-07-17 03:30:54 +00008072 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8073 // reasonable.
8074
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008075 // BFI is only available on V6T2+
8076 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8077 return SDValue();
8078
Jim Grosbach54238562010-07-17 03:30:54 +00008079 DebugLoc DL = N->getDebugLoc();
8080 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008081 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00008082 //
8083 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008084 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008085 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00008086 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00008087 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00008088 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008089
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008090 if (VT != MVT::i32)
8091 return SDValue();
8092
Evan Cheng30fb13f2010-12-13 20:32:54 +00008093 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00008094
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008095 // The value and the mask need to be constants so we can verify this is
8096 // actually a bitfield set. If the mask is 0xffff, we can do better
8097 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00008098 SDValue MaskOp = N0.getOperand(1);
8099 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8100 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008101 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008102 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008103 if (Mask == 0xffff)
8104 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008105 SDValue Res;
8106 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008107 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8108 if (N1C) {
8109 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008110 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00008111 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008112
Evan Chenga9688c42010-12-11 04:11:38 +00008113 if (ARM::isBitFieldInvertedMask(Mask)) {
8114 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008115
Evan Cheng30fb13f2010-12-13 20:32:54 +00008116 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00008117 DAG.getConstant(Val, MVT::i32),
8118 DAG.getConstant(Mask, MVT::i32));
8119
8120 // Do not add new nodes to DAG combiner worklist.
8121 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008122 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00008123 }
Jim Grosbach54238562010-07-17 03:30:54 +00008124 } else if (N1.getOpcode() == ISD::AND) {
8125 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00008126 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8127 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00008128 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00008129 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008130
Eric Christopher29aeed12011-03-26 01:21:03 +00008131 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8132 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00008133 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008134 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008135 // The pack halfword instruction works better for masks that fit it,
8136 // so use that when it's available.
8137 if (Subtarget->hasT2ExtractPack() &&
8138 (Mask == 0xffff || Mask == 0xffff0000))
8139 return SDValue();
8140 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00008141 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00008142 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00008143 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00008144 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00008145 DAG.getConstant(Mask, MVT::i32));
8146 // Do not add new nodes to DAG combiner worklist.
8147 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008148 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008149 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00008150 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00008151 // The pack halfword instruction works better for masks that fit it,
8152 // so use that when it's available.
8153 if (Subtarget->hasT2ExtractPack() &&
8154 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8155 return SDValue();
8156 // 2b
8157 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008158 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00008159 DAG.getConstant(lsb, MVT::i32));
8160 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00008161 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00008162 // Do not add new nodes to DAG combiner worklist.
8163 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00008164 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00008165 }
8166 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008167
Evan Cheng30fb13f2010-12-13 20:32:54 +00008168 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8169 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8170 ARM::isBitFieldInvertedMask(~Mask)) {
8171 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8172 // where lsb(mask) == #shamt and masked bits of B are known zero.
8173 SDValue ShAmt = N00.getOperand(1);
8174 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8175 unsigned LSB = CountTrailingZeros_32(Mask);
8176 if (ShAmtC != LSB)
8177 return SDValue();
8178
8179 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8180 DAG.getConstant(~Mask, MVT::i32));
8181
8182 // Do not add new nodes to DAG combiner worklist.
8183 DCI.CombineTo(N, Res, false);
8184 }
8185
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008186 return SDValue();
8187}
8188
Evan Chengc892aeb2012-02-23 01:19:06 +00008189static SDValue PerformXORCombine(SDNode *N,
8190 TargetLowering::DAGCombinerInfo &DCI,
8191 const ARMSubtarget *Subtarget) {
8192 EVT VT = N->getValueType(0);
8193 SelectionDAG &DAG = DCI.DAG;
8194
8195 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8196 return SDValue();
8197
8198 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00008199 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8200 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8201 if (Result.getNode())
8202 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00008203 }
8204
8205 return SDValue();
8206}
8207
Evan Chengbf188ae2011-06-15 01:12:31 +00008208/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8209/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00008210static SDValue PerformBFICombine(SDNode *N,
8211 TargetLowering::DAGCombinerInfo &DCI) {
8212 SDValue N1 = N->getOperand(1);
8213 if (N1.getOpcode() == ISD::AND) {
8214 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8215 if (!N11C)
8216 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008217 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8218 unsigned LSB = CountTrailingZeros_32(~InvMask);
8219 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
8220 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00008221 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00008222 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00008223 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
8224 N->getOperand(0), N1.getOperand(0),
8225 N->getOperand(2));
8226 }
8227 return SDValue();
8228}
8229
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008230/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8231/// ARMISD::VMOVRRD.
8232static SDValue PerformVMOVRRDCombine(SDNode *N,
8233 TargetLowering::DAGCombinerInfo &DCI) {
8234 // vmovrrd(vmovdrr x, y) -> x,y
8235 SDValue InDouble = N->getOperand(0);
8236 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8237 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00008238
8239 // vmovrrd(load f64) -> (load i32), (load i32)
8240 SDNode *InNode = InDouble.getNode();
8241 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8242 InNode->getValueType(0) == MVT::f64 &&
8243 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8244 !cast<LoadSDNode>(InNode)->isVolatile()) {
8245 // TODO: Should this be done for non-FrameIndex operands?
8246 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8247
8248 SelectionDAG &DAG = DCI.DAG;
8249 DebugLoc DL = LD->getDebugLoc();
8250 SDValue BasePtr = LD->getBasePtr();
8251 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8252 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008253 LD->isNonTemporal(), LD->isInvariant(),
8254 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00008255
8256 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8257 DAG.getConstant(4, MVT::i32));
8258 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8259 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008260 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008261 std::min(4U, LD->getAlignment() / 2));
8262
8263 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8264 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8265 DCI.RemoveFromWorklist(LD);
8266 DAG.DeleteNode(LD);
8267 return Result;
8268 }
8269
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008270 return SDValue();
8271}
8272
8273/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8274/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8275static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8276 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8277 SDValue Op0 = N->getOperand(0);
8278 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008279 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008280 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008281 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008282 Op1 = Op1.getOperand(0);
8283 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8284 Op0.getNode() == Op1.getNode() &&
8285 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008286 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008287 N->getValueType(0), Op0.getOperand(0));
8288 return SDValue();
8289}
8290
Bob Wilson31600902010-12-21 06:43:19 +00008291/// PerformSTORECombine - Target-specific dag combine xforms for
8292/// ISD::STORE.
8293static SDValue PerformSTORECombine(SDNode *N,
8294 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008295 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008296 if (St->isVolatile())
8297 return SDValue();
8298
Andrew Trick49b446f2012-07-18 18:34:24 +00008299 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008300 // pack all of the elements in one place. Next, store to memory in fewer
8301 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008302 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008303 EVT VT = StVal.getValueType();
8304 if (St->isTruncatingStore() && VT.isVector()) {
8305 SelectionDAG &DAG = DCI.DAG;
8306 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8307 EVT StVT = St->getMemoryVT();
8308 unsigned NumElems = VT.getVectorNumElements();
8309 assert(StVT != VT && "Cannot truncate to the same type");
8310 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8311 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8312
8313 // From, To sizes and ElemCount must be pow of two
8314 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8315
8316 // We are going to use the original vector elt for storing.
8317 // Accumulated smaller vector elements must be a multiple of the store size.
8318 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8319
8320 unsigned SizeRatio = FromEltSz / ToEltSz;
8321 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8322
8323 // Create a type on which we perform the shuffle.
8324 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8325 NumElems*SizeRatio);
8326 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8327
8328 DebugLoc DL = St->getDebugLoc();
8329 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8330 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8331 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8332
8333 // Can't shuffle using an illegal type.
8334 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8335
8336 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8337 DAG.getUNDEF(WideVec.getValueType()),
8338 ShuffleVec.data());
8339 // At this point all of the data is stored at the bottom of the
8340 // register. We now need to save it to mem.
8341
8342 // Find the largest store unit
8343 MVT StoreType = MVT::i8;
8344 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8345 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8346 MVT Tp = (MVT::SimpleValueType)tp;
8347 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8348 StoreType = Tp;
8349 }
8350 // Didn't find a legal store type.
8351 if (!TLI.isTypeLegal(StoreType))
8352 return SDValue();
8353
8354 // Bitcast the original vector into a vector of store-size units
8355 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8356 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8357 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8358 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8359 SmallVector<SDValue, 8> Chains;
8360 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8361 TLI.getPointerTy());
8362 SDValue BasePtr = St->getBasePtr();
8363
8364 // Perform one or more big stores into memory.
8365 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8366 for (unsigned I = 0; I < E; I++) {
8367 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8368 StoreType, ShuffWide,
8369 DAG.getIntPtrConstant(I));
8370 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8371 St->getPointerInfo(), St->isVolatile(),
8372 St->isNonTemporal(), St->getAlignment());
8373 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8374 Increment);
8375 Chains.push_back(Ch);
8376 }
8377 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8378 Chains.size());
8379 }
8380
8381 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008382 return SDValue();
8383
Chad Rosier96b66d62012-04-09 19:38:15 +00008384 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8385 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008386 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008387 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008388 SelectionDAG &DAG = DCI.DAG;
8389 DebugLoc DL = St->getDebugLoc();
8390 SDValue BasePtr = St->getBasePtr();
8391 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8392 StVal.getNode()->getOperand(0), BasePtr,
8393 St->getPointerInfo(), St->isVolatile(),
8394 St->isNonTemporal(), St->getAlignment());
8395
8396 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8397 DAG.getConstant(4, MVT::i32));
8398 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8399 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8400 St->isNonTemporal(),
8401 std::min(4U, St->getAlignment() / 2));
8402 }
8403
8404 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008405 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8406 return SDValue();
8407
Chad Rosier96b66d62012-04-09 19:38:15 +00008408 // Bitcast an i64 store extracted from a vector to f64.
8409 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008410 SelectionDAG &DAG = DCI.DAG;
8411 DebugLoc dl = StVal.getDebugLoc();
8412 SDValue IntVec = StVal.getOperand(0);
8413 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8414 IntVec.getValueType().getVectorNumElements());
8415 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8416 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8417 Vec, StVal.getOperand(1));
8418 dl = N->getDebugLoc();
8419 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8420 // Make the DAGCombiner fold the bitcasts.
8421 DCI.AddToWorklist(Vec.getNode());
8422 DCI.AddToWorklist(ExtElt.getNode());
8423 DCI.AddToWorklist(V.getNode());
8424 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8425 St->getPointerInfo(), St->isVolatile(),
8426 St->isNonTemporal(), St->getAlignment(),
8427 St->getTBAAInfo());
8428}
8429
8430/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8431/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8432/// i64 vector to have f64 elements, since the value can then be loaded
8433/// directly into a VFP register.
8434static bool hasNormalLoadOperand(SDNode *N) {
8435 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8436 for (unsigned i = 0; i < NumElts; ++i) {
8437 SDNode *Elt = N->getOperand(i).getNode();
8438 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8439 return true;
8440 }
8441 return false;
8442}
8443
Bob Wilson75f02882010-09-17 22:59:05 +00008444/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8445/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008446static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8447 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008448 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8449 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8450 // into a pair of GPRs, which is fine when the value is used as a scalar,
8451 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008452 SelectionDAG &DAG = DCI.DAG;
8453 if (N->getNumOperands() == 2) {
8454 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8455 if (RV.getNode())
8456 return RV;
8457 }
Bob Wilson75f02882010-09-17 22:59:05 +00008458
Bob Wilson31600902010-12-21 06:43:19 +00008459 // Load i64 elements as f64 values so that type legalization does not split
8460 // them up into i32 values.
8461 EVT VT = N->getValueType(0);
8462 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8463 return SDValue();
8464 DebugLoc dl = N->getDebugLoc();
8465 SmallVector<SDValue, 8> Ops;
8466 unsigned NumElts = VT.getVectorNumElements();
8467 for (unsigned i = 0; i < NumElts; ++i) {
8468 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8469 Ops.push_back(V);
8470 // Make the DAGCombiner fold the bitcast.
8471 DCI.AddToWorklist(V.getNode());
8472 }
8473 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8474 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8475 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8476}
8477
8478/// PerformInsertEltCombine - Target-specific dag combine xforms for
8479/// ISD::INSERT_VECTOR_ELT.
8480static SDValue PerformInsertEltCombine(SDNode *N,
8481 TargetLowering::DAGCombinerInfo &DCI) {
8482 // Bitcast an i64 load inserted into a vector to f64.
8483 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8484 EVT VT = N->getValueType(0);
8485 SDNode *Elt = N->getOperand(1).getNode();
8486 if (VT.getVectorElementType() != MVT::i64 ||
8487 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8488 return SDValue();
8489
8490 SelectionDAG &DAG = DCI.DAG;
8491 DebugLoc dl = N->getDebugLoc();
8492 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8493 VT.getVectorNumElements());
8494 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8495 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8496 // Make the DAGCombiner fold the bitcasts.
8497 DCI.AddToWorklist(Vec.getNode());
8498 DCI.AddToWorklist(V.getNode());
8499 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8500 Vec, V, N->getOperand(2));
8501 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008502}
8503
Bob Wilsonf20700c2010-10-27 20:38:28 +00008504/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8505/// ISD::VECTOR_SHUFFLE.
8506static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8507 // The LLVM shufflevector instruction does not require the shuffle mask
8508 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8509 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8510 // operands do not match the mask length, they are extended by concatenating
8511 // them with undef vectors. That is probably the right thing for other
8512 // targets, but for NEON it is better to concatenate two double-register
8513 // size vector operands into a single quad-register size vector. Do that
8514 // transformation here:
8515 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8516 // shuffle(concat(v1, v2), undef)
8517 SDValue Op0 = N->getOperand(0);
8518 SDValue Op1 = N->getOperand(1);
8519 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8520 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8521 Op0.getNumOperands() != 2 ||
8522 Op1.getNumOperands() != 2)
8523 return SDValue();
8524 SDValue Concat0Op1 = Op0.getOperand(1);
8525 SDValue Concat1Op1 = Op1.getOperand(1);
8526 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8527 Concat1Op1.getOpcode() != ISD::UNDEF)
8528 return SDValue();
8529 // Skip the transformation if any of the types are illegal.
8530 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8531 EVT VT = N->getValueType(0);
8532 if (!TLI.isTypeLegal(VT) ||
8533 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8534 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8535 return SDValue();
8536
8537 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8538 Op0.getOperand(0), Op1.getOperand(0));
8539 // Translate the shuffle mask.
8540 SmallVector<int, 16> NewMask;
8541 unsigned NumElts = VT.getVectorNumElements();
8542 unsigned HalfElts = NumElts/2;
8543 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8544 for (unsigned n = 0; n < NumElts; ++n) {
8545 int MaskElt = SVN->getMaskElt(n);
8546 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008547 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008548 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008549 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008550 NewElt = HalfElts + MaskElt - NumElts;
8551 NewMask.push_back(NewElt);
8552 }
8553 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8554 DAG.getUNDEF(VT), NewMask.data());
8555}
8556
Bob Wilson1c3ef902011-02-07 17:43:21 +00008557/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8558/// NEON load/store intrinsics to merge base address updates.
8559static SDValue CombineBaseUpdate(SDNode *N,
8560 TargetLowering::DAGCombinerInfo &DCI) {
8561 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8562 return SDValue();
8563
8564 SelectionDAG &DAG = DCI.DAG;
8565 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8566 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8567 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8568 SDValue Addr = N->getOperand(AddrOpIdx);
8569
8570 // Search for a use of the address operand that is an increment.
8571 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8572 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8573 SDNode *User = *UI;
8574 if (User->getOpcode() != ISD::ADD ||
8575 UI.getUse().getResNo() != Addr.getResNo())
8576 continue;
8577
8578 // Check that the add is independent of the load/store. Otherwise, folding
8579 // it would create a cycle.
8580 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8581 continue;
8582
8583 // Find the new opcode for the updating load/store.
8584 bool isLoad = true;
8585 bool isLaneOp = false;
8586 unsigned NewOpc = 0;
8587 unsigned NumVecs = 0;
8588 if (isIntrinsic) {
8589 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8590 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008591 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008592 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8593 NumVecs = 1; break;
8594 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8595 NumVecs = 2; break;
8596 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8597 NumVecs = 3; break;
8598 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8599 NumVecs = 4; break;
8600 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8601 NumVecs = 2; isLaneOp = true; break;
8602 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8603 NumVecs = 3; isLaneOp = true; break;
8604 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8605 NumVecs = 4; isLaneOp = true; break;
8606 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8607 NumVecs = 1; isLoad = false; break;
8608 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8609 NumVecs = 2; isLoad = false; break;
8610 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8611 NumVecs = 3; isLoad = false; break;
8612 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8613 NumVecs = 4; isLoad = false; break;
8614 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8615 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8616 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8617 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8618 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8619 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8620 }
8621 } else {
8622 isLaneOp = true;
8623 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008624 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008625 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8626 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8627 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8628 }
8629 }
8630
8631 // Find the size of memory referenced by the load/store.
8632 EVT VecTy;
8633 if (isLoad)
8634 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008635 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008636 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8637 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8638 if (isLaneOp)
8639 NumBytes /= VecTy.getVectorNumElements();
8640
8641 // If the increment is a constant, it must match the memory ref size.
8642 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8643 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8644 uint64_t IncVal = CInc->getZExtValue();
8645 if (IncVal != NumBytes)
8646 continue;
8647 } else if (NumBytes >= 3 * 16) {
8648 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8649 // separate instructions that make it harder to use a non-constant update.
8650 continue;
8651 }
8652
8653 // Create the new updating load/store node.
8654 EVT Tys[6];
8655 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8656 unsigned n;
8657 for (n = 0; n < NumResultVecs; ++n)
8658 Tys[n] = VecTy;
8659 Tys[n++] = MVT::i32;
8660 Tys[n] = MVT::Other;
8661 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8662 SmallVector<SDValue, 8> Ops;
8663 Ops.push_back(N->getOperand(0)); // incoming chain
8664 Ops.push_back(N->getOperand(AddrOpIdx));
8665 Ops.push_back(Inc);
8666 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8667 Ops.push_back(N->getOperand(i));
8668 }
8669 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8670 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8671 Ops.data(), Ops.size(),
8672 MemInt->getMemoryVT(),
8673 MemInt->getMemOperand());
8674
8675 // Update the uses.
8676 std::vector<SDValue> NewResults;
8677 for (unsigned i = 0; i < NumResultVecs; ++i) {
8678 NewResults.push_back(SDValue(UpdN.getNode(), i));
8679 }
8680 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8681 DCI.CombineTo(N, NewResults);
8682 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8683
8684 break;
Owen Anderson76706012011-04-05 21:48:57 +00008685 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008686 return SDValue();
8687}
8688
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008689/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8690/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8691/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8692/// return true.
8693static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8694 SelectionDAG &DAG = DCI.DAG;
8695 EVT VT = N->getValueType(0);
8696 // vldN-dup instructions only support 64-bit vectors for N > 1.
8697 if (!VT.is64BitVector())
8698 return false;
8699
8700 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8701 SDNode *VLD = N->getOperand(0).getNode();
8702 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8703 return false;
8704 unsigned NumVecs = 0;
8705 unsigned NewOpc = 0;
8706 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8707 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8708 NumVecs = 2;
8709 NewOpc = ARMISD::VLD2DUP;
8710 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8711 NumVecs = 3;
8712 NewOpc = ARMISD::VLD3DUP;
8713 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8714 NumVecs = 4;
8715 NewOpc = ARMISD::VLD4DUP;
8716 } else {
8717 return false;
8718 }
8719
8720 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8721 // numbers match the load.
8722 unsigned VLDLaneNo =
8723 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8724 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8725 UI != UE; ++UI) {
8726 // Ignore uses of the chain result.
8727 if (UI.getUse().getResNo() == NumVecs)
8728 continue;
8729 SDNode *User = *UI;
8730 if (User->getOpcode() != ARMISD::VDUPLANE ||
8731 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8732 return false;
8733 }
8734
8735 // Create the vldN-dup node.
8736 EVT Tys[5];
8737 unsigned n;
8738 for (n = 0; n < NumVecs; ++n)
8739 Tys[n] = VT;
8740 Tys[n] = MVT::Other;
8741 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8742 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8743 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8744 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8745 Ops, 2, VLDMemInt->getMemoryVT(),
8746 VLDMemInt->getMemOperand());
8747
8748 // Update the uses.
8749 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8750 UI != UE; ++UI) {
8751 unsigned ResNo = UI.getUse().getResNo();
8752 // Ignore uses of the chain result.
8753 if (ResNo == NumVecs)
8754 continue;
8755 SDNode *User = *UI;
8756 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8757 }
8758
8759 // Now the vldN-lane intrinsic is dead except for its chain result.
8760 // Update uses of the chain.
8761 std::vector<SDValue> VLDDupResults;
8762 for (unsigned n = 0; n < NumVecs; ++n)
8763 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8764 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8765 DCI.CombineTo(VLD, VLDDupResults);
8766
8767 return true;
8768}
8769
Bob Wilson9e82bf12010-07-14 01:22:12 +00008770/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8771/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008772static SDValue PerformVDUPLANECombine(SDNode *N,
8773 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008774 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008775
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008776 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8777 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8778 if (CombineVLDDUP(N, DCI))
8779 return SDValue(N, 0);
8780
8781 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8782 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008783 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008784 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008785 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008786 return SDValue();
8787
8788 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8789 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8790 // The canonical VMOV for a zero vector uses a 32-bit element size.
8791 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8792 unsigned EltBits;
8793 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8794 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008795 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008796 if (EltSize > VT.getVectorElementType().getSizeInBits())
8797 return SDValue();
8798
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008799 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008800}
8801
Eric Christopherfa6f5912011-06-29 21:10:36 +00008802// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008803// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8804static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8805{
Chad Rosier118c9a02011-06-28 17:26:57 +00008806 integerPart cN;
8807 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008808 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8809 I != E; I++) {
8810 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8811 if (!C)
8812 return false;
8813
Eric Christopherfa6f5912011-06-29 21:10:36 +00008814 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008815 APFloat APF = C->getValueAPF();
8816 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8817 != APFloat::opOK || !isExact)
8818 return false;
8819
8820 c0 = (I == 0) ? cN : c0;
8821 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8822 return false;
8823 }
8824 C = c0;
8825 return true;
8826}
8827
8828/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8829/// can replace combinations of VMUL and VCVT (floating-point to integer)
8830/// when the VMUL has a constant operand that is a power of 2.
8831///
8832/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8833/// vmul.f32 d16, d17, d16
8834/// vcvt.s32.f32 d16, d16
8835/// becomes:
8836/// vcvt.s32.f32 d16, d16, #3
8837static SDValue PerformVCVTCombine(SDNode *N,
8838 TargetLowering::DAGCombinerInfo &DCI,
8839 const ARMSubtarget *Subtarget) {
8840 SelectionDAG &DAG = DCI.DAG;
8841 SDValue Op = N->getOperand(0);
8842
8843 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8844 Op.getOpcode() != ISD::FMUL)
8845 return SDValue();
8846
8847 uint64_t C;
8848 SDValue N0 = Op->getOperand(0);
8849 SDValue ConstVec = Op->getOperand(1);
8850 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8851
Eric Christopherfa6f5912011-06-29 21:10:36 +00008852 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008853 !isConstVecPow2(ConstVec, isSigned, C))
8854 return SDValue();
8855
8856 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8857 Intrinsic::arm_neon_vcvtfp2fxu;
8858 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8859 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008860 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008861 DAG.getConstant(Log2_64(C), MVT::i32));
8862}
8863
8864/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8865/// can replace combinations of VCVT (integer to floating-point) and VDIV
8866/// when the VDIV has a constant operand that is a power of 2.
8867///
8868/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8869/// vcvt.f32.s32 d16, d16
8870/// vdiv.f32 d16, d17, d16
8871/// becomes:
8872/// vcvt.f32.s32 d16, d16, #3
8873static SDValue PerformVDIVCombine(SDNode *N,
8874 TargetLowering::DAGCombinerInfo &DCI,
8875 const ARMSubtarget *Subtarget) {
8876 SelectionDAG &DAG = DCI.DAG;
8877 SDValue Op = N->getOperand(0);
8878 unsigned OpOpcode = Op.getNode()->getOpcode();
8879
8880 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8881 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8882 return SDValue();
8883
8884 uint64_t C;
8885 SDValue ConstVec = N->getOperand(1);
8886 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8887
8888 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8889 !isConstVecPow2(ConstVec, isSigned, C))
8890 return SDValue();
8891
Eric Christopherfa6f5912011-06-29 21:10:36 +00008892 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008893 Intrinsic::arm_neon_vcvtfxu2fp;
8894 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8895 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008896 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008897 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8898}
8899
8900/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008901/// operand of a vector shift operation, where all the elements of the
8902/// build_vector must have the same constant integer value.
8903static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8904 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008905 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008906 Op = Op.getOperand(0);
8907 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8908 APInt SplatBits, SplatUndef;
8909 unsigned SplatBitSize;
8910 bool HasAnyUndefs;
8911 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8912 HasAnyUndefs, ElementBits) ||
8913 SplatBitSize > ElementBits)
8914 return false;
8915 Cnt = SplatBits.getSExtValue();
8916 return true;
8917}
8918
8919/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8920/// operand of a vector shift left operation. That value must be in the range:
8921/// 0 <= Value < ElementBits for a left shift; or
8922/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008923static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008924 assert(VT.isVector() && "vector shift count is not a vector type");
8925 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8926 if (! getVShiftImm(Op, ElementBits, Cnt))
8927 return false;
8928 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8929}
8930
8931/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8932/// operand of a vector shift right operation. For a shift opcode, the value
8933/// is positive, but for an intrinsic the value count must be negative. The
8934/// absolute value must be in the range:
8935/// 1 <= |Value| <= ElementBits for a right shift; or
8936/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008937static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008938 int64_t &Cnt) {
8939 assert(VT.isVector() && "vector shift count is not a vector type");
8940 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8941 if (! getVShiftImm(Op, ElementBits, Cnt))
8942 return false;
8943 if (isIntrinsic)
8944 Cnt = -Cnt;
8945 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8946}
8947
8948/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8949static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8950 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8951 switch (IntNo) {
8952 default:
8953 // Don't do anything for most intrinsics.
8954 break;
8955
8956 // Vector shifts: check for immediate versions and lower them.
8957 // Note: This is done during DAG combining instead of DAG legalizing because
8958 // the build_vectors for 64-bit vector element shift counts are generally
8959 // not legal, and it is hard to see their values after they get legalized to
8960 // loads from a constant pool.
8961 case Intrinsic::arm_neon_vshifts:
8962 case Intrinsic::arm_neon_vshiftu:
8963 case Intrinsic::arm_neon_vshiftls:
8964 case Intrinsic::arm_neon_vshiftlu:
8965 case Intrinsic::arm_neon_vshiftn:
8966 case Intrinsic::arm_neon_vrshifts:
8967 case Intrinsic::arm_neon_vrshiftu:
8968 case Intrinsic::arm_neon_vrshiftn:
8969 case Intrinsic::arm_neon_vqshifts:
8970 case Intrinsic::arm_neon_vqshiftu:
8971 case Intrinsic::arm_neon_vqshiftsu:
8972 case Intrinsic::arm_neon_vqshiftns:
8973 case Intrinsic::arm_neon_vqshiftnu:
8974 case Intrinsic::arm_neon_vqshiftnsu:
8975 case Intrinsic::arm_neon_vqrshiftns:
8976 case Intrinsic::arm_neon_vqrshiftnu:
8977 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008978 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008979 int64_t Cnt;
8980 unsigned VShiftOpc = 0;
8981
8982 switch (IntNo) {
8983 case Intrinsic::arm_neon_vshifts:
8984 case Intrinsic::arm_neon_vshiftu:
8985 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8986 VShiftOpc = ARMISD::VSHL;
8987 break;
8988 }
8989 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8990 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8991 ARMISD::VSHRs : ARMISD::VSHRu);
8992 break;
8993 }
8994 return SDValue();
8995
8996 case Intrinsic::arm_neon_vshiftls:
8997 case Intrinsic::arm_neon_vshiftlu:
8998 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8999 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009000 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009001
9002 case Intrinsic::arm_neon_vrshifts:
9003 case Intrinsic::arm_neon_vrshiftu:
9004 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9005 break;
9006 return SDValue();
9007
9008 case Intrinsic::arm_neon_vqshifts:
9009 case Intrinsic::arm_neon_vqshiftu:
9010 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9011 break;
9012 return SDValue();
9013
9014 case Intrinsic::arm_neon_vqshiftsu:
9015 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9016 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00009017 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009018
9019 case Intrinsic::arm_neon_vshiftn:
9020 case Intrinsic::arm_neon_vrshiftn:
9021 case Intrinsic::arm_neon_vqshiftns:
9022 case Intrinsic::arm_neon_vqshiftnu:
9023 case Intrinsic::arm_neon_vqshiftnsu:
9024 case Intrinsic::arm_neon_vqrshiftns:
9025 case Intrinsic::arm_neon_vqrshiftnu:
9026 case Intrinsic::arm_neon_vqrshiftnsu:
9027 // Narrowing shifts require an immediate right shift.
9028 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9029 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00009030 llvm_unreachable("invalid shift count for narrowing vector shift "
9031 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009032
9033 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009034 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00009035 }
9036
9037 switch (IntNo) {
9038 case Intrinsic::arm_neon_vshifts:
9039 case Intrinsic::arm_neon_vshiftu:
9040 // Opcode already set above.
9041 break;
9042 case Intrinsic::arm_neon_vshiftls:
9043 case Intrinsic::arm_neon_vshiftlu:
9044 if (Cnt == VT.getVectorElementType().getSizeInBits())
9045 VShiftOpc = ARMISD::VSHLLi;
9046 else
9047 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9048 ARMISD::VSHLLs : ARMISD::VSHLLu);
9049 break;
9050 case Intrinsic::arm_neon_vshiftn:
9051 VShiftOpc = ARMISD::VSHRN; break;
9052 case Intrinsic::arm_neon_vrshifts:
9053 VShiftOpc = ARMISD::VRSHRs; break;
9054 case Intrinsic::arm_neon_vrshiftu:
9055 VShiftOpc = ARMISD::VRSHRu; break;
9056 case Intrinsic::arm_neon_vrshiftn:
9057 VShiftOpc = ARMISD::VRSHRN; break;
9058 case Intrinsic::arm_neon_vqshifts:
9059 VShiftOpc = ARMISD::VQSHLs; break;
9060 case Intrinsic::arm_neon_vqshiftu:
9061 VShiftOpc = ARMISD::VQSHLu; break;
9062 case Intrinsic::arm_neon_vqshiftsu:
9063 VShiftOpc = ARMISD::VQSHLsu; break;
9064 case Intrinsic::arm_neon_vqshiftns:
9065 VShiftOpc = ARMISD::VQSHRNs; break;
9066 case Intrinsic::arm_neon_vqshiftnu:
9067 VShiftOpc = ARMISD::VQSHRNu; break;
9068 case Intrinsic::arm_neon_vqshiftnsu:
9069 VShiftOpc = ARMISD::VQSHRNsu; break;
9070 case Intrinsic::arm_neon_vqrshiftns:
9071 VShiftOpc = ARMISD::VQRSHRNs; break;
9072 case Intrinsic::arm_neon_vqrshiftnu:
9073 VShiftOpc = ARMISD::VQRSHRNu; break;
9074 case Intrinsic::arm_neon_vqrshiftnsu:
9075 VShiftOpc = ARMISD::VQRSHRNsu; break;
9076 }
9077
9078 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009079 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009080 }
9081
9082 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00009083 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009084 int64_t Cnt;
9085 unsigned VShiftOpc = 0;
9086
9087 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9088 VShiftOpc = ARMISD::VSLI;
9089 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9090 VShiftOpc = ARMISD::VSRI;
9091 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00009092 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00009093 }
9094
9095 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
9096 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00009097 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009098 }
9099
9100 case Intrinsic::arm_neon_vqrshifts:
9101 case Intrinsic::arm_neon_vqrshiftu:
9102 // No immediate versions of these to check for.
9103 break;
9104 }
9105
9106 return SDValue();
9107}
9108
9109/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9110/// lowers them. As with the vector shift intrinsics, this is done during DAG
9111/// combining instead of DAG legalizing because the build_vectors for 64-bit
9112/// vector element shift counts are generally not legal, and it is hard to see
9113/// their values after they get legalized to loads from a constant pool.
9114static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9115 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00009116 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00009117 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9118 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9119 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9120 SDValue N1 = N->getOperand(1);
9121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9122 SDValue N0 = N->getOperand(0);
9123 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9124 DAG.MaskedValueIsZero(N0.getOperand(0),
9125 APInt::getHighBitsSet(32, 16)))
9126 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
9127 }
9128 }
Bob Wilson5bafff32009-06-22 23:27:02 +00009129
9130 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00009131 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9132 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00009133 return SDValue();
9134
9135 assert(ST->hasNEON() && "unexpected vector shift");
9136 int64_t Cnt;
9137
9138 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009139 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009140
9141 case ISD::SHL:
9142 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9143 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009144 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009145 break;
9146
9147 case ISD::SRA:
9148 case ISD::SRL:
9149 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9150 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9151 ARMISD::VSHRs : ARMISD::VSHRu);
9152 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009153 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00009154 }
9155 }
9156 return SDValue();
9157}
9158
9159/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9160/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9161static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9162 const ARMSubtarget *ST) {
9163 SDValue N0 = N->getOperand(0);
9164
9165 // Check for sign- and zero-extensions of vector extract operations of 8-
9166 // and 16-bit vector elements. NEON supports these directly. They are
9167 // handled during DAG combining because type legalization will promote them
9168 // to 32-bit types and it is messy to recognize the operations after that.
9169 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9170 SDValue Vec = N0.getOperand(0);
9171 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009172 EVT VT = N->getValueType(0);
9173 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00009174 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9175
Owen Anderson825b72b2009-08-11 20:47:22 +00009176 if (VT == MVT::i32 &&
9177 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00009178 TLI.isTypeLegal(Vec.getValueType()) &&
9179 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00009180
9181 unsigned Opc = 0;
9182 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009183 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00009184 case ISD::SIGN_EXTEND:
9185 Opc = ARMISD::VGETLANEs;
9186 break;
9187 case ISD::ZERO_EXTEND:
9188 case ISD::ANY_EXTEND:
9189 Opc = ARMISD::VGETLANEu;
9190 break;
9191 }
9192 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
9193 }
9194 }
9195
9196 return SDValue();
9197}
9198
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009199/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9200/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9201static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9202 const ARMSubtarget *ST) {
9203 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00009204 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009205 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9206 // a NaN; only do the transformation when it matches that behavior.
9207
9208 // For now only do this when using NEON for FP operations; if using VFP, it
9209 // is not obvious that the benefit outweighs the cost of switching to the
9210 // NEON pipeline.
9211 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9212 N->getValueType(0) != MVT::f32)
9213 return SDValue();
9214
9215 SDValue CondLHS = N->getOperand(0);
9216 SDValue CondRHS = N->getOperand(1);
9217 SDValue LHS = N->getOperand(2);
9218 SDValue RHS = N->getOperand(3);
9219 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9220
9221 unsigned Opcode = 0;
9222 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00009223 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009224 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00009225 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009226 IsReversed = true ; // x CC y ? y : x
9227 } else {
9228 return SDValue();
9229 }
9230
Bob Wilsone742bb52010-02-24 22:15:53 +00009231 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009232 switch (CC) {
9233 default: break;
9234 case ISD::SETOLT:
9235 case ISD::SETOLE:
9236 case ISD::SETLT:
9237 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009238 case ISD::SETULT:
9239 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009240 // If LHS is NaN, an ordered comparison will be false and the result will
9241 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9242 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9243 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9244 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9245 break;
9246 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9247 // will return -0, so vmin can only be used for unsafe math or if one of
9248 // the operands is known to be nonzero.
9249 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009250 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009251 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9252 break;
9253 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009254 break;
9255
9256 case ISD::SETOGT:
9257 case ISD::SETOGE:
9258 case ISD::SETGT:
9259 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009260 case ISD::SETUGT:
9261 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009262 // If LHS is NaN, an ordered comparison will be false and the result will
9263 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9264 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9265 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9266 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9267 break;
9268 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9269 // will return +0, so vmax can only be used for unsafe math or if one of
9270 // the operands is known to be nonzero.
9271 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009272 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009273 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9274 break;
9275 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009276 break;
9277 }
9278
9279 if (!Opcode)
9280 return SDValue();
9281 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9282}
9283
Evan Chenge721f5c2011-07-13 00:42:17 +00009284/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9285SDValue
9286ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9287 SDValue Cmp = N->getOperand(4);
9288 if (Cmp.getOpcode() != ARMISD::CMPZ)
9289 // Only looking at EQ and NE cases.
9290 return SDValue();
9291
9292 EVT VT = N->getValueType(0);
9293 DebugLoc dl = N->getDebugLoc();
9294 SDValue LHS = Cmp.getOperand(0);
9295 SDValue RHS = Cmp.getOperand(1);
9296 SDValue FalseVal = N->getOperand(0);
9297 SDValue TrueVal = N->getOperand(1);
9298 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009299 ARMCC::CondCodes CC =
9300 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009301
9302 // Simplify
9303 // mov r1, r0
9304 // cmp r1, x
9305 // mov r0, y
9306 // moveq r0, x
9307 // to
9308 // cmp r0, x
9309 // movne r0, y
9310 //
9311 // mov r1, r0
9312 // cmp r1, x
9313 // mov r0, x
9314 // movne r0, y
9315 // to
9316 // cmp r0, x
9317 // movne r0, y
9318 /// FIXME: Turn this into a target neutral optimization?
9319 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009320 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009321 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9322 N->getOperand(3), Cmp);
9323 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9324 SDValue ARMcc;
9325 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9326 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9327 N->getOperand(3), NewCmp);
9328 }
9329
9330 if (Res.getNode()) {
9331 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009332 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009333 // Capture demanded bits information that would be otherwise lost.
9334 if (KnownZero == 0xfffffffe)
9335 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9336 DAG.getValueType(MVT::i1));
9337 else if (KnownZero == 0xffffff00)
9338 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9339 DAG.getValueType(MVT::i8));
9340 else if (KnownZero == 0xffff0000)
9341 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9342 DAG.getValueType(MVT::i16));
9343 }
9344
9345 return Res;
9346}
9347
Dan Gohman475871a2008-07-27 21:46:04 +00009348SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009349 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009350 switch (N->getOpcode()) {
9351 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009352 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009353 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009354 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009355 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009356 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009357 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9358 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009359 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009360 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009361 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009362 case ISD::STORE: return PerformSTORECombine(N, DCI);
9363 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9364 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009365 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009366 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009367 case ISD::FP_TO_SINT:
9368 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9369 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009370 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009371 case ISD::SHL:
9372 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009373 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009374 case ISD::SIGN_EXTEND:
9375 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009376 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9377 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009378 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009379 case ARMISD::VLD2DUP:
9380 case ARMISD::VLD3DUP:
9381 case ARMISD::VLD4DUP:
9382 return CombineBaseUpdate(N, DCI);
9383 case ISD::INTRINSIC_VOID:
9384 case ISD::INTRINSIC_W_CHAIN:
9385 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9386 case Intrinsic::arm_neon_vld1:
9387 case Intrinsic::arm_neon_vld2:
9388 case Intrinsic::arm_neon_vld3:
9389 case Intrinsic::arm_neon_vld4:
9390 case Intrinsic::arm_neon_vld2lane:
9391 case Intrinsic::arm_neon_vld3lane:
9392 case Intrinsic::arm_neon_vld4lane:
9393 case Intrinsic::arm_neon_vst1:
9394 case Intrinsic::arm_neon_vst2:
9395 case Intrinsic::arm_neon_vst3:
9396 case Intrinsic::arm_neon_vst4:
9397 case Intrinsic::arm_neon_vst2lane:
9398 case Intrinsic::arm_neon_vst3lane:
9399 case Intrinsic::arm_neon_vst4lane:
9400 return CombineBaseUpdate(N, DCI);
9401 default: break;
9402 }
9403 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009404 }
Dan Gohman475871a2008-07-27 21:46:04 +00009405 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009406}
9407
Evan Cheng31959b12011-02-02 01:06:55 +00009408bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9409 EVT VT) const {
9410 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9411}
9412
Evan Cheng376642e2012-12-10 23:21:26 +00009413bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009414 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009415 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009416
9417 switch (VT.getSimpleVT().SimpleTy) {
9418 default:
9419 return false;
9420 case MVT::i8:
9421 case MVT::i16:
Evan Cheng376642e2012-12-10 23:21:26 +00009422 case MVT::i32: {
Evan Chengd10eab02012-09-18 01:42:45 +00009423 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng376642e2012-12-10 23:21:26 +00009424 if (AllowsUnaligned) {
9425 if (Fast)
9426 *Fast = Subtarget->hasV7Ops();
9427 return true;
9428 }
9429 return false;
9430 }
Evan Chenga99c5082012-08-15 17:44:53 +00009431 case MVT::f64:
Evan Cheng376642e2012-12-10 23:21:26 +00009432 case MVT::v2f64: {
Evan Chengd10eab02012-09-18 01:42:45 +00009433 // For any little-endian targets with neon, we can support unaligned ld/st
9434 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9435 // A big-endian target may also explictly support unaligned accesses
Evan Cheng376642e2012-12-10 23:21:26 +00009436 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9437 if (Fast)
9438 *Fast = true;
9439 return true;
9440 }
9441 return false;
9442 }
Bill Wendlingaf566342009-08-15 21:21:19 +00009443 }
9444}
9445
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009446static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9447 unsigned AlignCheck) {
9448 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9449 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9450}
9451
9452EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9453 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00009454 bool IsMemset, bool ZeroMemset,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009455 bool MemcpyStrSrc,
9456 MachineFunction &MF) const {
9457 const Function *F = MF.getFunction();
9458
9459 // See if we can use NEON instructions for this...
Evan Cheng946a3a92012-12-12 02:34:41 +00009460 if ((!IsMemset || ZeroMemset) &&
Evan Cheng376642e2012-12-10 23:21:26 +00009461 Subtarget->hasNEON() &&
Bill Wendling831737d2012-12-30 10:32:01 +00009462 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9463 Attribute::NoImplicitFloat)) {
Evan Cheng376642e2012-12-10 23:21:26 +00009464 bool Fast;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009465 if (Size >= 16 &&
9466 (memOpAlign(SrcAlign, DstAlign, 16) ||
9467 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009468 return MVT::v2f64;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009469 } else if (Size >= 8 &&
9470 (memOpAlign(SrcAlign, DstAlign, 8) ||
9471 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
Evan Cheng376642e2012-12-10 23:21:26 +00009472 return MVT::f64;
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009473 }
9474 }
9475
Lang Hames5207bf22011-11-08 18:56:23 +00009476 // Lowering to i32/i16 if the size permits.
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009477 if (Size >= 4)
Lang Hames5207bf22011-11-08 18:56:23 +00009478 return MVT::i32;
Evan Cheng6a1b5cc2012-12-11 02:31:57 +00009479 else if (Size >= 2)
Lang Hames5207bf22011-11-08 18:56:23 +00009480 return MVT::i16;
Lang Hames5207bf22011-11-08 18:56:23 +00009481
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009482 // Let the target-independent logic figure it out.
9483 return MVT::Other;
9484}
9485
Evan Cheng2766a472012-12-06 19:13:27 +00009486bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9487 if (Val.getOpcode() != ISD::LOAD)
9488 return false;
9489
9490 EVT VT1 = Val.getValueType();
9491 if (!VT1.isSimple() || !VT1.isInteger() ||
9492 !VT2.isSimple() || !VT2.isInteger())
9493 return false;
9494
9495 switch (VT1.getSimpleVT().SimpleTy) {
9496 default: break;
9497 case MVT::i1:
9498 case MVT::i8:
9499 case MVT::i16:
9500 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9501 return true;
9502 }
9503
9504 return false;
9505}
9506
Evan Chenge6c835f2009-08-14 20:09:37 +00009507static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9508 if (V < 0)
9509 return false;
9510
9511 unsigned Scale = 1;
9512 switch (VT.getSimpleVT().SimpleTy) {
9513 default: return false;
9514 case MVT::i1:
9515 case MVT::i8:
9516 // Scale == 1;
9517 break;
9518 case MVT::i16:
9519 // Scale == 2;
9520 Scale = 2;
9521 break;
9522 case MVT::i32:
9523 // Scale == 4;
9524 Scale = 4;
9525 break;
9526 }
9527
9528 if ((V & (Scale - 1)) != 0)
9529 return false;
9530 V /= Scale;
9531 return V == (V & ((1LL << 5) - 1));
9532}
9533
9534static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9535 const ARMSubtarget *Subtarget) {
9536 bool isNeg = false;
9537 if (V < 0) {
9538 isNeg = true;
9539 V = - V;
9540 }
9541
9542 switch (VT.getSimpleVT().SimpleTy) {
9543 default: return false;
9544 case MVT::i1:
9545 case MVT::i8:
9546 case MVT::i16:
9547 case MVT::i32:
9548 // + imm12 or - imm8
9549 if (isNeg)
9550 return V == (V & ((1LL << 8) - 1));
9551 return V == (V & ((1LL << 12) - 1));
9552 case MVT::f32:
9553 case MVT::f64:
9554 // Same as ARM mode. FIXME: NEON?
9555 if (!Subtarget->hasVFP2())
9556 return false;
9557 if ((V & 3) != 0)
9558 return false;
9559 V >>= 2;
9560 return V == (V & ((1LL << 8) - 1));
9561 }
9562}
9563
Evan Chengb01fad62007-03-12 23:30:29 +00009564/// isLegalAddressImmediate - Return true if the integer value can be used
9565/// as the offset of the target addressing mode for load / store of the
9566/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009567static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009568 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009569 if (V == 0)
9570 return true;
9571
Evan Cheng65011532009-03-09 19:15:00 +00009572 if (!VT.isSimple())
9573 return false;
9574
Evan Chenge6c835f2009-08-14 20:09:37 +00009575 if (Subtarget->isThumb1Only())
9576 return isLegalT1AddressImmediate(V, VT);
9577 else if (Subtarget->isThumb2())
9578 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009579
Evan Chenge6c835f2009-08-14 20:09:37 +00009580 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009581 if (V < 0)
9582 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009583 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009584 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009585 case MVT::i1:
9586 case MVT::i8:
9587 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009588 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009589 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009591 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009592 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009593 case MVT::f32:
9594 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009595 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009596 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009597 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009598 return false;
9599 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009600 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009601 }
Evan Chenga8e29892007-01-19 07:51:42 +00009602}
9603
Evan Chenge6c835f2009-08-14 20:09:37 +00009604bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9605 EVT VT) const {
9606 int Scale = AM.Scale;
9607 if (Scale < 0)
9608 return false;
9609
9610 switch (VT.getSimpleVT().SimpleTy) {
9611 default: return false;
9612 case MVT::i1:
9613 case MVT::i8:
9614 case MVT::i16:
9615 case MVT::i32:
9616 if (Scale == 1)
9617 return true;
9618 // r + r << imm
9619 Scale = Scale & ~1;
9620 return Scale == 2 || Scale == 4 || Scale == 8;
9621 case MVT::i64:
9622 // r + r
9623 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9624 return true;
9625 return false;
9626 case MVT::isVoid:
9627 // Note, we allow "void" uses (basically, uses that aren't loads or
9628 // stores), because arm allows folding a scale into many arithmetic
9629 // operations. This should be made more precise and revisited later.
9630
9631 // Allow r << imm, but the imm has to be a multiple of two.
9632 if (Scale & 1) return false;
9633 return isPowerOf2_32(Scale);
9634 }
9635}
9636
Chris Lattner37caf8c2007-04-09 23:33:39 +00009637/// isLegalAddressingMode - Return true if the addressing mode represented
9638/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009639bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009640 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009641 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009642 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009643 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009644
Chris Lattner37caf8c2007-04-09 23:33:39 +00009645 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009646 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009647 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009648
Chris Lattner37caf8c2007-04-09 23:33:39 +00009649 switch (AM.Scale) {
9650 case 0: // no scale reg, must be "r+i" or "r", or "i".
9651 break;
9652 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009653 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009654 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009655 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009656 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009657 // ARM doesn't support any R+R*scale+imm addr modes.
9658 if (AM.BaseOffs)
9659 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009660
Bob Wilson2c7dab12009-04-08 17:55:28 +00009661 if (!VT.isSimple())
9662 return false;
9663
Evan Chenge6c835f2009-08-14 20:09:37 +00009664 if (Subtarget->isThumb2())
9665 return isLegalT2ScaledAddressingMode(AM, VT);
9666
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009667 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009668 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009669 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009670 case MVT::i1:
9671 case MVT::i8:
9672 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009673 if (Scale < 0) Scale = -Scale;
9674 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009675 return true;
9676 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009677 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009678 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009679 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009680 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009681 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009682 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009683 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009684
Owen Anderson825b72b2009-08-11 20:47:22 +00009685 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009686 // Note, we allow "void" uses (basically, uses that aren't loads or
9687 // stores), because arm allows folding a scale into many arithmetic
9688 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009689
Chris Lattner37caf8c2007-04-09 23:33:39 +00009690 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009691 if (Scale & 1) return false;
9692 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009693 }
Evan Chengb01fad62007-03-12 23:30:29 +00009694 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009695 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009696}
9697
Evan Cheng77e47512009-11-11 19:05:52 +00009698/// isLegalICmpImmediate - Return true if the specified immediate is legal
9699/// icmp immediate, that is the target has icmp instructions which can compare
9700/// a register against the immediate without having to materialize the
9701/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009702bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009703 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009704 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009705 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009706 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009707 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009708 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009709 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009710}
9711
Andrew Trick8d8d9612012-07-18 18:34:27 +00009712/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9713/// *or sub* immediate, that is the target has add or sub instructions which can
9714/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009715/// immediate into a register.
9716bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009717 // Same encoding for add/sub, just flip the sign.
9718 int64_t AbsImm = llvm::abs64(Imm);
9719 if (!Subtarget->isThumb())
9720 return ARM_AM::getSOImmVal(AbsImm) != -1;
9721 if (Subtarget->isThumb2())
9722 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9723 // Thumb1 only has 8-bit unsigned immediate.
9724 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009725}
9726
Owen Andersone50ed302009-08-10 22:56:29 +00009727static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009728 bool isSEXTLoad, SDValue &Base,
9729 SDValue &Offset, bool &isInc,
9730 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009731 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9732 return false;
9733
Owen Anderson825b72b2009-08-11 20:47:22 +00009734 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009735 // AddressingMode 3
9736 Base = Ptr->getOperand(0);
9737 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009738 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009739 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009740 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009741 isInc = false;
9742 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9743 return true;
9744 }
9745 }
9746 isInc = (Ptr->getOpcode() == ISD::ADD);
9747 Offset = Ptr->getOperand(1);
9748 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009749 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009750 // AddressingMode 2
9751 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009752 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009753 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009754 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009755 isInc = false;
9756 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9757 Base = Ptr->getOperand(0);
9758 return true;
9759 }
9760 }
9761
9762 if (Ptr->getOpcode() == ISD::ADD) {
9763 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009764 ARM_AM::ShiftOpc ShOpcVal=
9765 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009766 if (ShOpcVal != ARM_AM::no_shift) {
9767 Base = Ptr->getOperand(1);
9768 Offset = Ptr->getOperand(0);
9769 } else {
9770 Base = Ptr->getOperand(0);
9771 Offset = Ptr->getOperand(1);
9772 }
9773 return true;
9774 }
9775
9776 isInc = (Ptr->getOpcode() == ISD::ADD);
9777 Base = Ptr->getOperand(0);
9778 Offset = Ptr->getOperand(1);
9779 return true;
9780 }
9781
Jim Grosbache5165492009-11-09 00:11:35 +00009782 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009783 return false;
9784}
9785
Owen Andersone50ed302009-08-10 22:56:29 +00009786static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009787 bool isSEXTLoad, SDValue &Base,
9788 SDValue &Offset, bool &isInc,
9789 SelectionDAG &DAG) {
9790 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9791 return false;
9792
9793 Base = Ptr->getOperand(0);
9794 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9795 int RHSC = (int)RHS->getZExtValue();
9796 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9797 assert(Ptr->getOpcode() == ISD::ADD);
9798 isInc = false;
9799 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9800 return true;
9801 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9802 isInc = Ptr->getOpcode() == ISD::ADD;
9803 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9804 return true;
9805 }
9806 }
9807
9808 return false;
9809}
9810
Evan Chenga8e29892007-01-19 07:51:42 +00009811/// getPreIndexedAddressParts - returns true by value, base pointer and
9812/// offset pointer and addressing mode by reference if the node's address
9813/// can be legally represented as pre-indexed load / store address.
9814bool
Dan Gohman475871a2008-07-27 21:46:04 +00009815ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9816 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009817 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009818 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009819 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009820 return false;
9821
Owen Andersone50ed302009-08-10 22:56:29 +00009822 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009823 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009824 bool isSEXTLoad = false;
9825 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9826 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009827 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009828 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9829 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9830 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009831 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009832 } else
9833 return false;
9834
9835 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009836 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009837 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009838 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9839 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009840 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009841 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009842 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009843 if (!isLegal)
9844 return false;
9845
9846 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9847 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009848}
9849
9850/// getPostIndexedAddressParts - returns true by value, base pointer and
9851/// offset pointer and addressing mode by reference if this node can be
9852/// combined with a load / store to form a post-indexed load / store.
9853bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009854 SDValue &Base,
9855 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009856 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009857 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009858 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009859 return false;
9860
Owen Andersone50ed302009-08-10 22:56:29 +00009861 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009862 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009863 bool isSEXTLoad = false;
9864 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009865 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009866 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009867 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9868 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009869 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009870 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009871 } else
9872 return false;
9873
9874 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009875 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009876 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009877 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009878 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009879 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009880 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9881 isInc, DAG);
9882 if (!isLegal)
9883 return false;
9884
Evan Cheng28dad2a2010-05-18 21:31:17 +00009885 if (Ptr != Base) {
9886 // Swap base ptr and offset to catch more post-index load / store when
9887 // it's legal. In Thumb2 mode, offset must be an immediate.
9888 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9889 !Subtarget->isThumb2())
9890 std::swap(Base, Offset);
9891
9892 // Post-indexed load / store update the base pointer.
9893 if (Ptr != Base)
9894 return false;
9895 }
9896
Evan Chenge88d5ce2009-07-02 07:28:31 +00009897 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9898 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009899}
9900
Dan Gohman475871a2008-07-27 21:46:04 +00009901void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009902 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009903 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009904 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009905 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009906 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009907 switch (Op.getOpcode()) {
9908 default: break;
9909 case ARMISD::CMOV: {
9910 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009911 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009912 if (KnownZero == 0 && KnownOne == 0) return;
9913
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009914 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009915 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009916 KnownZero &= KnownZeroRHS;
9917 KnownOne &= KnownOneRHS;
9918 return;
9919 }
9920 }
9921}
9922
9923//===----------------------------------------------------------------------===//
9924// ARM Inline Assembly Support
9925//===----------------------------------------------------------------------===//
9926
Evan Cheng55d42002011-01-08 01:24:27 +00009927bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9928 // Looking for "rev" which is V6+.
9929 if (!Subtarget->hasV6Ops())
9930 return false;
9931
9932 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9933 std::string AsmStr = IA->getAsmString();
9934 SmallVector<StringRef, 4> AsmPieces;
9935 SplitString(AsmStr, AsmPieces, ";\n");
9936
9937 switch (AsmPieces.size()) {
9938 default: return false;
9939 case 1:
9940 AsmStr = AsmPieces[0];
9941 AsmPieces.clear();
9942 SplitString(AsmStr, AsmPieces, " \t,");
9943
9944 // rev $0, $1
9945 if (AsmPieces.size() == 3 &&
9946 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9947 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009948 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009949 if (Ty && Ty->getBitWidth() == 32)
9950 return IntrinsicLowering::LowerToByteSwap(CI);
9951 }
9952 break;
9953 }
9954
9955 return false;
9956}
9957
Evan Chenga8e29892007-01-19 07:51:42 +00009958/// getConstraintType - Given a constraint letter, return the type of
9959/// constraint it is for this target.
9960ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009961ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9962 if (Constraint.size() == 1) {
9963 switch (Constraint[0]) {
9964 default: break;
9965 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009966 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009967 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009968 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009969 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009970 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009971 // An address with a single base register. Due to the way we
9972 // currently handle addresses it is the same as an 'r' memory constraint.
9973 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009974 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009975 } else if (Constraint.size() == 2) {
9976 switch (Constraint[0]) {
9977 default: break;
9978 // All 'U+' constraints are addresses.
9979 case 'U': return C_Memory;
9980 }
Evan Chenga8e29892007-01-19 07:51:42 +00009981 }
Chris Lattner4234f572007-03-25 02:14:49 +00009982 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009983}
9984
John Thompson44ab89e2010-10-29 17:29:13 +00009985/// Examine constraint type and operand type and determine a weight value.
9986/// This object must already have been set up with the operand type
9987/// and the current alternative constraint selected.
9988TargetLowering::ConstraintWeight
9989ARMTargetLowering::getSingleConstraintMatchWeight(
9990 AsmOperandInfo &info, const char *constraint) const {
9991 ConstraintWeight weight = CW_Invalid;
9992 Value *CallOperandVal = info.CallOperandVal;
9993 // If we don't have a value, we can't do a match,
9994 // but allow it at the lowest weight.
9995 if (CallOperandVal == NULL)
9996 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009997 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009998 // Look at the constraint type.
9999 switch (*constraint) {
10000 default:
10001 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10002 break;
10003 case 'l':
10004 if (type->isIntegerTy()) {
10005 if (Subtarget->isThumb())
10006 weight = CW_SpecificReg;
10007 else
10008 weight = CW_Register;
10009 }
10010 break;
10011 case 'w':
10012 if (type->isFloatingPointTy())
10013 weight = CW_Register;
10014 break;
10015 }
10016 return weight;
10017}
10018
Eric Christopher35e6d4d2011-06-30 23:50:52 +000010019typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10020RCPair
Evan Chenga8e29892007-01-19 07:51:42 +000010021ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010022 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +000010023 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010024 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +000010025 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +000010026 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +000010027 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010028 return RCPair(0U, &ARM::tGPRRegClass);
10029 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +000010030 case 'h': // High regs or no regs.
10031 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +000010032 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +000010033 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010034 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +000010035 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010036 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +000010037 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010038 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +000010039 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010040 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +000010041 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010042 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +000010043 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +000010044 case 'x':
10045 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010046 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010047 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +000010048 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010049 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +000010050 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +000010051 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010052 case 't':
10053 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +000010054 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +000010055 break;
Evan Chenga8e29892007-01-19 07:51:42 +000010056 }
10057 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010058 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +000010059 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +000010060
Evan Chenga8e29892007-01-19 07:51:42 +000010061 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10062}
10063
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010064/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10065/// vector. If it is invalid, don't add anything to Ops.
10066void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000010067 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010068 std::vector<SDValue>&Ops,
10069 SelectionDAG &DAG) const {
10070 SDValue Result(0, 0);
10071
Eric Christopher100c8332011-06-02 23:16:42 +000010072 // Currently only support length 1 constraints.
10073 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000010074
Eric Christopher100c8332011-06-02 23:16:42 +000010075 char ConstraintLetter = Constraint[0];
10076 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010077 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +000010078 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010079 case 'I': case 'J': case 'K': case 'L':
10080 case 'M': case 'N': case 'O':
10081 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10082 if (!C)
10083 return;
10084
10085 int64_t CVal64 = C->getSExtValue();
10086 int CVal = (int) CVal64;
10087 // None of these constraints allow values larger than 32 bits. Check
10088 // that the value fits in an int.
10089 if (CVal != CVal64)
10090 return;
10091
Eric Christopher100c8332011-06-02 23:16:42 +000010092 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +000010093 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +000010094 // Constant suitable for movw, must be between 0 and
10095 // 65535.
10096 if (Subtarget->hasV6T2Ops())
10097 if (CVal >= 0 && CVal <= 65535)
10098 break;
10099 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010100 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010101 if (Subtarget->isThumb1Only()) {
10102 // This must be a constant between 0 and 255, for ADD
10103 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010104 if (CVal >= 0 && CVal <= 255)
10105 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010106 } else if (Subtarget->isThumb2()) {
10107 // A constant that can be used as an immediate value in a
10108 // data-processing instruction.
10109 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10110 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010111 } else {
10112 // A constant that can be used as an immediate value in a
10113 // data-processing instruction.
10114 if (ARM_AM::getSOImmVal(CVal) != -1)
10115 break;
10116 }
10117 return;
10118
10119 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010120 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010121 // This must be a constant between -255 and -1, for negated ADD
10122 // immediates. This can be used in GCC with an "n" modifier that
10123 // prints the negated value, for use with SUB instructions. It is
10124 // not useful otherwise but is implemented for compatibility.
10125 if (CVal >= -255 && CVal <= -1)
10126 break;
10127 } else {
10128 // This must be a constant between -4095 and 4095. It is not clear
10129 // what this constraint is intended for. Implemented for
10130 // compatibility with GCC.
10131 if (CVal >= -4095 && CVal <= 4095)
10132 break;
10133 }
10134 return;
10135
10136 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010137 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010138 // A 32-bit value where only one byte has a nonzero value. Exclude
10139 // zero to match GCC. This constraint is used by GCC internally for
10140 // constants that can be loaded with a move/shift combination.
10141 // It is not useful otherwise but is implemented for compatibility.
10142 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10143 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010144 } else if (Subtarget->isThumb2()) {
10145 // A constant whose bitwise inverse can be used as an immediate
10146 // value in a data-processing instruction. This can be used in GCC
10147 // with a "B" modifier that prints the inverted value, for use with
10148 // BIC and MVN instructions. It is not useful otherwise but is
10149 // implemented for compatibility.
10150 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10151 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010152 } else {
10153 // A constant whose bitwise inverse can be used as an immediate
10154 // value in a data-processing instruction. This can be used in GCC
10155 // with a "B" modifier that prints the inverted value, for use with
10156 // BIC and MVN instructions. It is not useful otherwise but is
10157 // implemented for compatibility.
10158 if (ARM_AM::getSOImmVal(~CVal) != -1)
10159 break;
10160 }
10161 return;
10162
10163 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010164 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010165 // This must be a constant between -7 and 7,
10166 // for 3-operand ADD/SUB immediate instructions.
10167 if (CVal >= -7 && CVal < 7)
10168 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +000010169 } else if (Subtarget->isThumb2()) {
10170 // A constant whose negation can be used as an immediate value in a
10171 // data-processing instruction. This can be used in GCC with an "n"
10172 // modifier that prints the negated value, for use with SUB
10173 // instructions. It is not useful otherwise but is implemented for
10174 // compatibility.
10175 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10176 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010177 } else {
10178 // A constant whose negation can be used as an immediate value in a
10179 // data-processing instruction. This can be used in GCC with an "n"
10180 // modifier that prints the negated value, for use with SUB
10181 // instructions. It is not useful otherwise but is implemented for
10182 // compatibility.
10183 if (ARM_AM::getSOImmVal(-CVal) != -1)
10184 break;
10185 }
10186 return;
10187
10188 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010189 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010190 // This must be a multiple of 4 between 0 and 1020, for
10191 // ADD sp + immediate.
10192 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10193 break;
10194 } else {
10195 // A power of two or a constant between 0 and 32. This is used in
10196 // GCC for the shift amount on shifted register operands, but it is
10197 // useful in general for any shift amounts.
10198 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10199 break;
10200 }
10201 return;
10202
10203 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010204 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010205 // This must be a constant between 0 and 31, for shift amounts.
10206 if (CVal >= 0 && CVal <= 31)
10207 break;
10208 }
10209 return;
10210
10211 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +000010212 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010213 // This must be a multiple of 4 between -508 and 508, for
10214 // ADD/SUB sp = sp + immediate.
10215 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10216 break;
10217 }
10218 return;
10219 }
10220 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10221 break;
10222 }
10223
10224 if (Result.getNode()) {
10225 Ops.push_back(Result);
10226 return;
10227 }
Dale Johannesen1784d162010-06-25 21:55:36 +000010228 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +000010229}
Anton Korobeynikov48e19352009-09-23 19:04:09 +000010230
10231bool
10232ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10233 // The ARM target isn't yet aware of offsets.
10234 return false;
10235}
Evan Cheng39382422009-10-28 01:44:26 +000010236
Jim Grosbach469bbdb2010-07-16 23:05:05 +000010237bool ARM::isBitFieldInvertedMask(unsigned v) {
10238 if (v == 0xffffffff)
10239 return 0;
10240 // there can be 1's on either or both "outsides", all the "inside"
10241 // bits must be 0's
10242 unsigned int lsb = 0, msb = 31;
10243 while (v & (1 << msb)) --msb;
10244 while (v & (1 << lsb)) ++lsb;
10245 for (unsigned int i = lsb; i <= msb; ++i) {
10246 if (v & (1 << i))
10247 return 0;
10248 }
10249 return 1;
10250}
10251
Evan Cheng39382422009-10-28 01:44:26 +000010252/// isFPImmLegal - Returns true if the target can instruction select the
10253/// specified FP immediate natively. If false, the legalizer will
10254/// materialize the FP immediate as a load from a constant pool.
10255bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10256 if (!Subtarget->hasVFP3())
10257 return false;
10258 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010259 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010260 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +000010261 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +000010262 return false;
10263}
Bob Wilson65ffec42010-09-21 17:56:22 +000010264
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010265/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +000010266/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10267/// specified in the intrinsic calls.
10268bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10269 const CallInst &I,
10270 unsigned Intrinsic) const {
10271 switch (Intrinsic) {
10272 case Intrinsic::arm_neon_vld1:
10273 case Intrinsic::arm_neon_vld2:
10274 case Intrinsic::arm_neon_vld3:
10275 case Intrinsic::arm_neon_vld4:
10276 case Intrinsic::arm_neon_vld2lane:
10277 case Intrinsic::arm_neon_vld3lane:
10278 case Intrinsic::arm_neon_vld4lane: {
10279 Info.opc = ISD::INTRINSIC_W_CHAIN;
10280 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +000010281 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010282 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10283 Info.ptrVal = I.getArgOperand(0);
10284 Info.offset = 0;
10285 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10286 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10287 Info.vol = false; // volatile loads with NEON intrinsics not supported
10288 Info.readMem = true;
10289 Info.writeMem = false;
10290 return true;
10291 }
10292 case Intrinsic::arm_neon_vst1:
10293 case Intrinsic::arm_neon_vst2:
10294 case Intrinsic::arm_neon_vst3:
10295 case Intrinsic::arm_neon_vst4:
10296 case Intrinsic::arm_neon_vst2lane:
10297 case Intrinsic::arm_neon_vst3lane:
10298 case Intrinsic::arm_neon_vst4lane: {
10299 Info.opc = ISD::INTRINSIC_VOID;
10300 // Conservatively set memVT to the entire set of vectors stored.
10301 unsigned NumElts = 0;
10302 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010303 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010304 if (!ArgTy->isVectorTy())
10305 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010306 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010307 }
10308 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10309 Info.ptrVal = I.getArgOperand(0);
10310 Info.offset = 0;
10311 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10312 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10313 Info.vol = false; // volatile stores with NEON intrinsics not supported
10314 Info.readMem = false;
10315 Info.writeMem = true;
10316 return true;
10317 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010318 case Intrinsic::arm_strexd: {
10319 Info.opc = ISD::INTRINSIC_W_CHAIN;
10320 Info.memVT = MVT::i64;
10321 Info.ptrVal = I.getArgOperand(2);
10322 Info.offset = 0;
10323 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010324 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010325 Info.readMem = false;
10326 Info.writeMem = true;
10327 return true;
10328 }
10329 case Intrinsic::arm_ldrexd: {
10330 Info.opc = ISD::INTRINSIC_W_CHAIN;
10331 Info.memVT = MVT::i64;
10332 Info.ptrVal = I.getArgOperand(0);
10333 Info.offset = 0;
10334 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010335 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010336 Info.readMem = true;
10337 Info.writeMem = false;
10338 return true;
10339 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010340 default:
10341 break;
10342 }
10343
10344 return false;
10345}
Evan Chenge07f85e2012-12-11 23:26:14 +000010346