blob: bd470eb4bb72cb54380921d3ce434697fe26829a [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"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000031#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000032#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000033#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000034#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000035#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/MachineBasicBlock.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000043#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000045#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000046#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000047#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000050#include "llvm/Support/raw_ostream.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
Bob Wilson1c3ef902011-02-07 17:43:21 +0000549 setTargetDAGCombine(ISD::INTRINSIC_VOID);
550 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000551 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
552 setTargetDAGCombine(ISD::SHL);
553 setTargetDAGCombine(ISD::SRL);
554 setTargetDAGCombine(ISD::SRA);
555 setTargetDAGCombine(ISD::SIGN_EXTEND);
556 setTargetDAGCombine(ISD::ZERO_EXTEND);
557 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000558 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000559 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000560 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000561 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
562 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000563 setTargetDAGCombine(ISD::FP_TO_SINT);
564 setTargetDAGCombine(ISD::FP_TO_UINT);
565 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000566
James Molloy873fd5f2012-02-20 09:24:05 +0000567 // It is legal to extload from v4i8 to v4i16 or v4i32.
568 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
569 MVT::v4i16, MVT::v2i16,
570 MVT::v2i32};
571 for (unsigned i = 0; i < 6; ++i) {
572 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
573 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
574 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
575 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000576 }
577
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000578 // ARM and Thumb2 support UMLAL/SMLAL.
579 if (!Subtarget->isThumb1Only())
580 setTargetDAGCombine(ISD::ADDC);
581
582
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000583 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000584
585 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000586 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000587
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000588 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000590
Evan Chenga8e29892007-01-19 07:51:42 +0000591 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000592 if (!Subtarget->isThumb1Only()) {
593 for (unsigned im = (unsigned)ISD::PRE_INC;
594 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setIndexedLoadAction(im, MVT::i1, Legal);
596 setIndexedLoadAction(im, MVT::i8, Legal);
597 setIndexedLoadAction(im, MVT::i16, Legal);
598 setIndexedLoadAction(im, MVT::i32, Legal);
599 setIndexedStoreAction(im, MVT::i1, Legal);
600 setIndexedStoreAction(im, MVT::i8, Legal);
601 setIndexedStoreAction(im, MVT::i16, Legal);
602 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000603 }
Evan Chenga8e29892007-01-19 07:51:42 +0000604 }
605
606 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000607 setOperationAction(ISD::MUL, MVT::i64, Expand);
608 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000609 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
611 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000612 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000613 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
614 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000615 setOperationAction(ISD::MULHS, MVT::i32, Expand);
616
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000617 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000618 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000619 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::SRL, MVT::i64, Custom);
621 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000622
Evan Cheng342e3162011-08-30 01:34:54 +0000623 if (!Subtarget->isThumb1Only()) {
624 // FIXME: We should do this for Thumb1 as well.
625 setOperationAction(ISD::ADDC, MVT::i32, Custom);
626 setOperationAction(ISD::ADDE, MVT::i32, Custom);
627 setOperationAction(ISD::SUBC, MVT::i32, Custom);
628 setOperationAction(ISD::SUBE, MVT::i32, Custom);
629 }
630
Evan Chenga8e29892007-01-19 07:51:42 +0000631 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000633 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000635 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000637
Chandler Carruth63974b22011-12-13 01:56:10 +0000638 // These just redirect to CTTZ and CTLZ on ARM.
639 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
640 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
641
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000642 // Only ARMv6 has BSWAP.
643 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000645
Bob Wilsoneb1641d2012-09-29 21:43:49 +0000646 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
647 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
648 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000649 setOperationAction(ISD::SDIV, MVT::i32, Expand);
650 setOperationAction(ISD::UDIV, MVT::i32, Expand);
651 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::SREM, MVT::i32, Expand);
653 setOperationAction(ISD::UREM, MVT::i32, Expand);
654 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
655 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000656
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
658 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
659 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
660 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000661 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000662
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000663 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000664
Evan Chenga8e29892007-01-19 07:51:42 +0000665 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 setOperationAction(ISD::VASTART, MVT::Other, Custom);
667 setOperationAction(ISD::VAARG, MVT::Other, Expand);
668 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
669 setOperationAction(ISD::VAEND, MVT::Other, Expand);
670 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
671 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000672
673 if (!Subtarget->isTargetDarwin()) {
674 // Non-Darwin platforms may return values in these registers via the
675 // personality function.
676 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
677 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
678 setExceptionPointerRegister(ARM::R0);
679 setExceptionSelectorRegister(ARM::R1);
680 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000681
Evan Cheng3a1588a2010-04-15 22:20:34 +0000682 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000683 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
684 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000685 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000686 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000687 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000688 // membarrier needs custom lowering; the rest are legal and handled
689 // normally.
690 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000691 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000692 // Custom lowering for 64-bit ops
693 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
694 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
695 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
696 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
697 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
698 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000699 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000700 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
701 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 } else {
703 // Set them all for expansion, which will force libcalls.
704 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000705 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000706 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000707 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000708 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000709 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000710 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000711 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000712 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000713 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000714 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000715 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000716 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000717 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000718 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
719 // Unordered/Monotonic case.
720 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
721 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000722 // Since the libcalls include locking, fold in the fences
723 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000724 }
Evan Chenga8e29892007-01-19 07:51:42 +0000725
Evan Cheng416941d2010-11-04 05:19:35 +0000726 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000727
Eli Friedmana2c6f452010-06-26 04:36:50 +0000728 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
729 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
731 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000732 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000734
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000735 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
736 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000737 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000738 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000739 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000740 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
741 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000742
743 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000745 if (Subtarget->isTargetDarwin()) {
746 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
747 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000748 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000749 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000750
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::SETCC, MVT::i32, Expand);
752 setOperationAction(ISD::SETCC, MVT::f32, Expand);
753 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000754 setOperationAction(ISD::SELECT, MVT::i32, Custom);
755 setOperationAction(ISD::SELECT, MVT::f32, Custom);
756 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
758 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
759 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000760
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
762 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
763 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
764 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
765 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000766
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000767 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 setOperationAction(ISD::FSIN, MVT::f64, Expand);
769 setOperationAction(ISD::FSIN, MVT::f32, Expand);
770 setOperationAction(ISD::FCOS, MVT::f32, Expand);
771 setOperationAction(ISD::FCOS, MVT::f64, Expand);
772 setOperationAction(ISD::FREM, MVT::f64, Expand);
773 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000774 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
775 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
777 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000778 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 setOperationAction(ISD::FPOW, MVT::f64, Expand);
780 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000781
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000782 if (!Subtarget->hasVFP4()) {
783 setOperationAction(ISD::FMA, MVT::f64, Expand);
784 setOperationAction(ISD::FMA, MVT::f32, Expand);
785 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000786
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000787 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000788 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000789 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
790 if (Subtarget->hasVFP2()) {
791 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
792 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
793 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
794 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
795 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000796 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000797 if (!Subtarget->hasFP16()) {
798 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
799 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000800 }
Evan Cheng110cf482008-04-01 01:50:16 +0000801 }
Evan Chenga8e29892007-01-19 07:51:42 +0000802
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000803 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000804 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000805 setTargetDAGCombine(ISD::ADD);
806 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000807 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesena7390fa2012-09-07 17:34:15 +0000808 setTargetDAGCombine(ISD::AND);
809 setTargetDAGCombine(ISD::OR);
810 setTargetDAGCombine(ISD::XOR);
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000811
Evan Cheng5fb468a2012-02-23 02:58:19 +0000812 if (Subtarget->hasV6Ops())
813 setTargetDAGCombine(ISD::SRL);
814
Evan Chenga8e29892007-01-19 07:51:42 +0000815 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000816
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000817 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
818 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000819 setSchedulingPreference(Sched::RegPressure);
820 else
821 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000822
Evan Cheng05219282011-01-06 06:52:41 +0000823 //// temporary - rewrite interface to use type
824 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000825 maxStoresPerMemset = 16;
826 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000827
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000828 // On ARM arguments smaller than 4 bytes are extended, so all arguments
829 // are at least 4 bytes aligned.
830 setMinStackArgumentAlignment(4);
831
Evan Chengfff606d2010-09-24 19:07:23 +0000832 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000833
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000834 // Prefer likely predicted branches to selects on out-of-order cores.
Silviu Baranga616471d2012-09-13 15:05:10 +0000835 predictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000836
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000837 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000838}
839
Andrew Trick32cec0a2011-01-19 02:35:27 +0000840// FIXME: It might make sense to define the representative register class as the
841// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
842// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
843// SPR's representative would be DPR_VFP2. This should work well if register
844// pressure tracking were modified such that a register use would increment the
845// pressure of the register class's representative and all of it's super
846// classes' representatives transitively. We have not implemented this because
847// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000848// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000849// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850std::pair<const TargetRegisterClass*, uint8_t>
851ARMTargetLowering::findRepresentativeClass(EVT VT) const{
852 const TargetRegisterClass *RRC = 0;
853 uint8_t Cost = 1;
854 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000855 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000856 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000857 // Use DPR as representative register class for all floating point
858 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
859 // the cost is 1 for both f32 and f64.
860 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000861 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000862 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000863 // When NEON is used for SP, only half of the register file is available
864 // because operations that define both SP and DP results will be constrained
865 // to the VFP2 class (D0-D15). We currently model this constraint prior to
866 // coalescing by double-counting the SP regs. See the FIXME above.
867 if (Subtarget->useNEONForSinglePrecisionFP())
868 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000869 break;
870 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
871 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000872 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000873 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000874 break;
875 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000876 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000877 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000878 break;
879 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000880 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000881 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000882 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000883 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000884 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000885}
886
Evan Chenga8e29892007-01-19 07:51:42 +0000887const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
888 switch (Opcode) {
889 default: return 0;
890 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000891 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000892 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000893 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
894 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000895 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000896 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
897 case ARMISD::tCALL: return "ARMISD::tCALL";
898 case ARMISD::BRCOND: return "ARMISD::BRCOND";
899 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000900 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000901 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
902 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
903 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000904 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwinc0309b42009-06-29 15:33:01 +0000905 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Chenga8e29892007-01-19 07:51:42 +0000906 case ARMISD::CMPFP: return "ARMISD::CMPFP";
907 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng218977b2010-07-13 19:27:42 +0000908 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Chenga8e29892007-01-19 07:51:42 +0000909 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chengc892aeb2012-02-23 01:19:06 +0000910
Evan Chenga8e29892007-01-19 07:51:42 +0000911 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000912
Jim Grosbach3482c802010-01-18 19:58:49 +0000913 case ARMISD::RBIT: return "ARMISD::RBIT";
914
Bob Wilson76a312b2010-03-19 22:51:32 +0000915 case ARMISD::FTOSI: return "ARMISD::FTOSI";
916 case ARMISD::FTOUI: return "ARMISD::FTOUI";
917 case ARMISD::SITOF: return "ARMISD::SITOF";
918 case ARMISD::UITOF: return "ARMISD::UITOF";
919
Evan Chenga8e29892007-01-19 07:51:42 +0000920 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
921 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
922 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson2dc4f542009-03-20 22:42:55 +0000923
Evan Cheng342e3162011-08-30 01:34:54 +0000924 case ARMISD::ADDC: return "ARMISD::ADDC";
925 case ARMISD::ADDE: return "ARMISD::ADDE";
926 case ARMISD::SUBC: return "ARMISD::SUBC";
927 case ARMISD::SUBE: return "ARMISD::SUBE";
928
Bob Wilson0b8ccb82010-09-22 22:09:21 +0000929 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
930 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000931
Evan Chengc5942082009-10-28 06:55:03 +0000932 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
933 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
934
Dale Johannesen51e28e62010-06-03 21:09:53 +0000935 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach4725ca72010-09-08 03:54:02 +0000936
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000937 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson5bafff32009-06-22 23:27:02 +0000938
Evan Cheng86198642009-08-07 00:34:42 +0000939 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
940
Jim Grosbach3728e962009-12-10 00:11:09 +0000941 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
Bob Wilsonf74a4292010-10-30 00:54:37 +0000942 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach3728e962009-12-10 00:11:09 +0000943
Evan Chengdfed19f2010-11-03 06:34:55 +0000944 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
945
Bob Wilson5bafff32009-06-22 23:27:02 +0000946 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000947 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000948 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000949 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
950 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000951 case ARMISD::VCGEU: return "ARMISD::VCGEU";
952 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilson3a75b9b2010-12-18 00:04:26 +0000953 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
954 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson5bafff32009-06-22 23:27:02 +0000955 case ARMISD::VCGTU: return "ARMISD::VCGTU";
956 case ARMISD::VTST: return "ARMISD::VTST";
957
958 case ARMISD::VSHL: return "ARMISD::VSHL";
959 case ARMISD::VSHRs: return "ARMISD::VSHRs";
960 case ARMISD::VSHRu: return "ARMISD::VSHRu";
961 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
962 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
963 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
964 case ARMISD::VSHRN: return "ARMISD::VSHRN";
965 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
966 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
967 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
968 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
969 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
970 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
971 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
972 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
973 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
974 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
975 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
976 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
977 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
978 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsoncba270d2010-07-13 21:16:48 +0000979 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilson7e3f0d22010-07-14 06:31:50 +0000980 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Chengeaa192a2011-11-15 02:12:34 +0000981 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsonc1d287b2009-08-14 05:13:08 +0000982 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilson0ce37102009-08-14 05:08:32 +0000983 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilsonde95c1b82009-08-19 17:03:43 +0000984 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsond8e17572009-08-12 22:31:50 +0000985 case ARMISD::VREV64: return "ARMISD::VREV64";
986 case ARMISD::VREV32: return "ARMISD::VREV32";
987 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov051cfd62009-08-21 12:41:42 +0000988 case ARMISD::VZIP: return "ARMISD::VZIP";
989 case ARMISD::VUZP: return "ARMISD::VUZP";
990 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendling69a05a72011-03-14 23:02:38 +0000991 case ARMISD::VTBL1: return "ARMISD::VTBL1";
992 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000993 case ARMISD::VMULLs: return "ARMISD::VMULLs";
994 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighofer67514e92012-09-04 14:37:49 +0000995 case ARMISD::UMLAL: return "ARMISD::UMLAL";
996 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilson40cbe7d2010-06-04 00:04:02 +0000997 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000998 case ARMISD::FMAX: return "ARMISD::FMAX";
999 case ARMISD::FMIN: return "ARMISD::FMIN";
Jim Grosbachdd7d28a2010-07-17 01:50:57 +00001000 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson364a72a2010-11-28 06:51:11 +00001001 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1002 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00001003 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00001004 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1005 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1006 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson1c3ef902011-02-07 17:43:21 +00001007 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1008 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1009 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1010 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1011 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1012 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1013 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1014 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1015 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1016 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1017 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1018 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1019 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1020 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1021 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1022 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1023 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Chenga8e29892007-01-19 07:51:42 +00001024 }
1025}
1026
Duncan Sands28b77e92011-09-06 19:07:46 +00001027EVT ARMTargetLowering::getSetCCResultType(EVT VT) const {
1028 if (!VT.isVector()) return getPointerTy();
1029 return VT.changeVectorElementTypeToInteger();
1030}
1031
Evan Cheng06b666c2010-05-15 02:18:07 +00001032/// getRegClassFor - Return the register class that should be used for the
1033/// specified value type.
Craig Topper44d23822012-02-22 05:59:10 +00001034const TargetRegisterClass *ARMTargetLowering::getRegClassFor(EVT VT) const {
Evan Cheng06b666c2010-05-15 02:18:07 +00001035 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1036 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1037 // load / store 4 to 8 consecutive D registers.
Evan Cheng4782b1e2010-05-15 02:20:21 +00001038 if (Subtarget->hasNEON()) {
1039 if (VT == MVT::v4i64)
Craig Topper420761a2012-04-20 07:30:17 +00001040 return &ARM::QQPRRegClass;
1041 if (VT == MVT::v8i64)
1042 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001043 }
Evan Cheng06b666c2010-05-15 02:18:07 +00001044 return TargetLowering::getRegClassFor(VT);
1045}
1046
Eric Christopherab695882010-07-21 22:26:11 +00001047// Create a fast isel object.
1048FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00001049ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1050 const TargetLibraryInfo *libInfo) const {
1051 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001052}
1053
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001054/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1055/// be used for loads / stores from the global.
1056unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1057 return (Subtarget->isThumb1Only() ? 127 : 4095);
1058}
1059
Evan Cheng1cc39842010-05-20 23:26:43 +00001060Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001061 unsigned NumVals = N->getNumValues();
1062 if (!NumVals)
1063 return Sched::RegPressure;
1064
1065 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001066 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001067 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001068 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001069 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001070 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001071 }
Evan Chengc10f5432010-05-28 23:25:23 +00001072
1073 if (!N->isMachineOpcode())
1074 return Sched::RegPressure;
1075
1076 // Load are scheduled for latency even if there instruction itinerary
1077 // is not available.
1078 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001079 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001080
Evan Chenge837dea2011-06-28 19:10:37 +00001081 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001082 return Sched::RegPressure;
1083 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001084 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001085 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001086
Evan Cheng1cc39842010-05-20 23:26:43 +00001087 return Sched::RegPressure;
1088}
1089
Evan Chenga8e29892007-01-19 07:51:42 +00001090//===----------------------------------------------------------------------===//
1091// Lowering Code
1092//===----------------------------------------------------------------------===//
1093
Evan Chenga8e29892007-01-19 07:51:42 +00001094/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1095static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1096 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001097 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001098 case ISD::SETNE: return ARMCC::NE;
1099 case ISD::SETEQ: return ARMCC::EQ;
1100 case ISD::SETGT: return ARMCC::GT;
1101 case ISD::SETGE: return ARMCC::GE;
1102 case ISD::SETLT: return ARMCC::LT;
1103 case ISD::SETLE: return ARMCC::LE;
1104 case ISD::SETUGT: return ARMCC::HI;
1105 case ISD::SETUGE: return ARMCC::HS;
1106 case ISD::SETULT: return ARMCC::LO;
1107 case ISD::SETULE: return ARMCC::LS;
1108 }
1109}
1110
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001111/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1112static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001113 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001114 CondCode2 = ARMCC::AL;
1115 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001116 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001117 case ISD::SETEQ:
1118 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1119 case ISD::SETGT:
1120 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1121 case ISD::SETGE:
1122 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1123 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001124 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001125 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1126 case ISD::SETO: CondCode = ARMCC::VC; break;
1127 case ISD::SETUO: CondCode = ARMCC::VS; break;
1128 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1129 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1130 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1131 case ISD::SETLT:
1132 case ISD::SETULT: CondCode = ARMCC::LT; break;
1133 case ISD::SETLE:
1134 case ISD::SETULE: CondCode = ARMCC::LE; break;
1135 case ISD::SETNE:
1136 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1137 }
Evan Chenga8e29892007-01-19 07:51:42 +00001138}
1139
Bob Wilson1f595bb2009-04-17 19:07:39 +00001140//===----------------------------------------------------------------------===//
1141// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001142//===----------------------------------------------------------------------===//
1143
1144#include "ARMGenCallingConv.inc"
1145
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001146/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1147/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001148CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001149 bool Return,
1150 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001151 switch (CC) {
1152 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001153 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001154 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001155 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001156 if (!Subtarget->isAAPCS_ABI())
1157 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1158 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1159 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1160 }
1161 // Fallthrough
1162 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001163 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001164 if (!Subtarget->isAAPCS_ABI())
1165 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1166 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001167 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1168 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001169 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1170 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1171 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001172 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001173 if (!isVarArg)
1174 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1175 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001176 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001177 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001178 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001179 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001180 case CallingConv::GHC:
1181 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001182 }
1183}
1184
Dan Gohman98ca4f22009-08-05 01:29:28 +00001185/// LowerCallResult - Lower the result values of a call into the
1186/// appropriate copies out of appropriate physical registers.
1187SDValue
1188ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001189 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 const SmallVectorImpl<ISD::InputArg> &Ins,
1191 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001192 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001193
Bob Wilson1f595bb2009-04-17 19:07:39 +00001194 // Assign locations to each value returned by this call.
1195 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001196 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1197 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001198 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001199 CCAssignFnForNode(CallConv, /* Return*/ true,
1200 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001201
1202 // Copy all of the result registers out of their specified physreg.
1203 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1204 CCValAssign VA = RVLocs[i];
1205
Bob Wilson80915242009-04-25 00:33:20 +00001206 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001207 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001208 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001209 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001210 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001211 Chain = Lo.getValue(1);
1212 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001213 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001215 InFlag);
1216 Chain = Hi.getValue(1);
1217 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001218 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001219
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 if (VA.getLocVT() == MVT::v2f64) {
1221 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1222 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1223 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001224
1225 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001227 Chain = Lo.getValue(1);
1228 InFlag = Lo.getValue(2);
1229 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001231 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);
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1235 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001236 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001237 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001238 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1239 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001240 Chain = Val.getValue(1);
1241 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001242 }
Bob Wilson80915242009-04-25 00:33:20 +00001243
1244 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001245 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001246 case CCValAssign::Full: break;
1247 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001248 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001249 break;
1250 }
1251
Dan Gohman98ca4f22009-08-05 01:29:28 +00001252 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001253 }
1254
Dan Gohman98ca4f22009-08-05 01:29:28 +00001255 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001256}
1257
Bob Wilsondee46d72009-04-17 20:35:10 +00001258/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001259SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001260ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1261 SDValue StackPtr, SDValue Arg,
1262 DebugLoc dl, SelectionDAG &DAG,
1263 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001264 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001265 unsigned LocMemOffset = VA.getLocMemOffset();
1266 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1267 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001268 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001269 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001270 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001271}
1272
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001274 SDValue Chain, SDValue &Arg,
1275 RegsToPassVector &RegsToPass,
1276 CCValAssign &VA, CCValAssign &NextVA,
1277 SDValue &StackPtr,
1278 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001279 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001280
Jim Grosbache5165492009-11-09 00:11:35 +00001281 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001283 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1284
1285 if (NextVA.isRegLoc())
1286 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1287 else {
1288 assert(NextVA.isMemLoc());
1289 if (StackPtr.getNode() == 0)
1290 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1291
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1293 dl, DAG, NextVA,
1294 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001295 }
1296}
1297
Dan Gohman98ca4f22009-08-05 01:29:28 +00001298/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001299/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1300/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001302ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001303 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001304 SelectionDAG &DAG = CLI.DAG;
1305 DebugLoc &dl = CLI.DL;
1306 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1307 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1308 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1309 SDValue Chain = CLI.Chain;
1310 SDValue Callee = CLI.Callee;
1311 bool &isTailCall = CLI.IsTailCall;
1312 CallingConv::ID CallConv = CLI.CallConv;
1313 bool doesNotRet = CLI.DoesNotReturn;
1314 bool isVarArg = CLI.IsVarArg;
1315
Dale Johannesen51e28e62010-06-03 21:09:53 +00001316 MachineFunction &MF = DAG.getMachineFunction();
1317 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1318 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001319 // Disable tail calls if they're not supported.
1320 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001321 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001322 if (isTailCall) {
1323 // Check if it's really possible to do a tail call.
1324 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1325 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001326 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001327 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1328 // detected sibcalls.
1329 if (isTailCall) {
1330 ++NumTailCalls;
1331 IsSibCall = true;
1332 }
1333 }
Evan Chenga8e29892007-01-19 07:51:42 +00001334
Bob Wilson1f595bb2009-04-17 19:07:39 +00001335 // Analyze operands of the call, assigning locations to each operand.
1336 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001337 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1338 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001339 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001340 CCAssignFnForNode(CallConv, /* Return*/ false,
1341 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001342
Bob Wilson1f595bb2009-04-17 19:07:39 +00001343 // Get a count of how many bytes are to be pushed on the stack.
1344 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001345
Dale Johannesen51e28e62010-06-03 21:09:53 +00001346 // For tail calls, memory operands are available in our caller's stack.
1347 if (IsSibCall)
1348 NumBytes = 0;
1349
Evan Chenga8e29892007-01-19 07:51:42 +00001350 // Adjust the stack pointer for the new arguments...
1351 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001352 if (!IsSibCall)
1353 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001354
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001355 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001356
Bob Wilson5bafff32009-06-22 23:27:02 +00001357 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001358 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001359
Bob Wilson1f595bb2009-04-17 19:07:39 +00001360 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001361 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001362 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1363 i != e;
1364 ++i, ++realArgIdx) {
1365 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001366 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001367 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001368 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001369
Bob Wilson1f595bb2009-04-17 19:07:39 +00001370 // Promote the value if needed.
1371 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001372 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001373 case CCValAssign::Full: break;
1374 case CCValAssign::SExt:
1375 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1376 break;
1377 case CCValAssign::ZExt:
1378 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1379 break;
1380 case CCValAssign::AExt:
1381 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1382 break;
1383 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001384 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001385 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001386 }
1387
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001388 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001389 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 if (VA.getLocVT() == MVT::v2f64) {
1391 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1392 DAG.getConstant(0, MVT::i32));
1393 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1394 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001395
Dan Gohman98ca4f22009-08-05 01:29:28 +00001396 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001397 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1398
1399 VA = ArgLocs[++i]; // skip ahead to next loc
1400 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001402 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1403 } else {
1404 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001405
Dan Gohman98ca4f22009-08-05 01:29:28 +00001406 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1407 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001408 }
1409 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001410 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001411 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001412 }
1413 } else if (VA.isRegLoc()) {
1414 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001415 } else if (isByVal) {
1416 assert(VA.isMemLoc());
1417 unsigned offset = 0;
1418
1419 // True if this byval aggregate will be split between registers
1420 // and memory.
1421 if (CCInfo.isFirstByValRegValid()) {
1422 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1423 unsigned int i, j;
1424 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1425 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1426 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1427 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1428 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001429 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001430 MemOpChains.push_back(Load.getValue(1));
1431 RegsToPass.push_back(std::make_pair(j, Load));
1432 }
1433 offset = ARM::R4 - CCInfo.getFirstByValReg();
1434 CCInfo.clearFirstByValReg();
1435 }
1436
Manman Ren763a75d2012-06-01 02:44:42 +00001437 if (Flags.getByValSize() - 4*offset > 0) {
1438 unsigned LocMemOffset = VA.getLocMemOffset();
1439 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1440 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1441 StkPtrOff);
1442 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1443 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1444 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1445 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001446 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001447
Manman Ren763a75d2012-06-01 02:44:42 +00001448 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001449 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001450 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1451 Ops, array_lengthof(Ops)));
1452 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001453 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001454 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001455
Dan Gohman98ca4f22009-08-05 01:29:28 +00001456 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1457 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001458 }
Evan Chenga8e29892007-01-19 07:51:42 +00001459 }
1460
1461 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001462 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001463 &MemOpChains[0], MemOpChains.size());
1464
1465 // Build a sequence of copy-to-reg nodes chained together with token chain
1466 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001467 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001468 // Tail call byval lowering might overwrite argument registers so in case of
1469 // tail call optimization the copies to registers are lowered later.
1470 if (!isTailCall)
1471 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1472 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1473 RegsToPass[i].second, InFlag);
1474 InFlag = Chain.getValue(1);
1475 }
Evan Chenga8e29892007-01-19 07:51:42 +00001476
Dale Johannesen51e28e62010-06-03 21:09:53 +00001477 // For tail calls lower the arguments to the 'real' stack slot.
1478 if (isTailCall) {
1479 // Force all the incoming stack arguments to be loaded from the stack
1480 // before any new outgoing arguments are stored to the stack, because the
1481 // outgoing stack slots may alias the incoming argument stack slots, and
1482 // the alias isn't otherwise explicit. This is slightly more conservative
1483 // than necessary, because it means that each store effectively depends
1484 // on every argument instead of just those arguments it would clobber.
1485
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001486 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001487 InFlag = SDValue();
1488 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1489 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1490 RegsToPass[i].second, InFlag);
1491 InFlag = Chain.getValue(1);
1492 }
1493 InFlag =SDValue();
1494 }
1495
Bill Wendling056292f2008-09-16 21:48:12 +00001496 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1497 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1498 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001499 bool isDirect = false;
1500 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001501 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001502 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001503
1504 if (EnableARMLongCalls) {
1505 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1506 && "long-calls with non-static relocation model!");
1507 // Handle a global address or an external symbol. If it's not one of
1508 // those, the target's already in a register, so we don't need to do
1509 // anything extra.
1510 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001511 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001512 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001513 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001514 ARMConstantPoolValue *CPV =
1515 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1516
Jim Grosbache7b52522010-04-14 22:28:31 +00001517 // Get the address of the callee into a register
1518 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1519 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1520 Callee = DAG.getLoad(getPointerTy(), dl,
1521 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001522 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001523 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001524 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1525 const char *Sym = S->getSymbol();
1526
1527 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001528 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001529 ARMConstantPoolValue *CPV =
1530 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1531 ARMPCLabelIndex, 0);
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 }
1540 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001541 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001542 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001543 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001544 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001545 getTargetMachine().getRelocationModel() != Reloc::Static;
1546 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001547 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001548 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001549 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001550 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001551 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001552 ARMConstantPoolValue *CPV =
1553 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001554 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001556 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001557 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001558 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001559 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001560 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001561 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001562 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001563 } else {
1564 // On ELF targets for PIC code, direct calls should go through the PLT
1565 unsigned OpFlags = 0;
1566 if (Subtarget->isTargetELF() &&
1567 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1568 OpFlags = ARMII::MO_PLT;
1569 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1570 }
Bill Wendling056292f2008-09-16 21:48:12 +00001571 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001572 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001573 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001574 getTargetMachine().getRelocationModel() != Reloc::Static;
1575 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001576 // tBX takes a register source operand.
1577 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001578 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001579 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001580 ARMConstantPoolValue *CPV =
1581 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1582 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001583 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001585 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001586 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001587 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001588 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001589 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001590 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001591 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001592 } else {
1593 unsigned OpFlags = 0;
1594 // On ELF targets for PIC code, direct calls should go through the PLT
1595 if (Subtarget->isTargetELF() &&
1596 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1597 OpFlags = ARMII::MO_PLT;
1598 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1599 }
Evan Chenga8e29892007-01-19 07:51:42 +00001600 }
1601
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001602 // FIXME: handle tail calls differently.
1603 unsigned CallOpc;
Quentin Colombet9a419f62012-10-30 16:32:52 +00001604 bool HasMinSizeAttr = MF.getFunction()->getFnAttributes().
1605 hasAttribute(Attributes::MinSize);
Evan Chengb6207242009-08-01 00:16:10 +00001606 if (Subtarget->isThumb()) {
1607 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001608 CallOpc = ARMISD::CALL_NOLINK;
1609 else
1610 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1611 } else {
Evan Chengb341fac2012-11-10 02:09:05 +00001612 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001613 CallOpc = ARMISD::CALL_NOLINK;
Evan Chengb341fac2012-11-10 02:09:05 +00001614 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet43934ae2012-11-02 21:32:17 +00001615 // Emit regular call when code size is the priority
1616 !HasMinSizeAttr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001617 // "mov lr, pc; b _foo" to avoid confusing the RSP
1618 CallOpc = ARMISD::CALL_NOLINK;
1619 else
1620 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001621 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001622
Dan Gohman475871a2008-07-27 21:46:04 +00001623 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001624 Ops.push_back(Chain);
1625 Ops.push_back(Callee);
1626
1627 // Add argument registers to the end of the list so that they are known live
1628 // into the call.
1629 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1630 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1631 RegsToPass[i].second.getValueType()));
1632
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001633 // Add a register mask operand representing the call-preserved registers.
1634 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1635 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1636 assert(Mask && "Missing call preserved mask for calling convention");
1637 Ops.push_back(DAG.getRegisterMask(Mask));
1638
Gabor Greifba36cb52008-08-28 21:40:38 +00001639 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001640 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001641
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001642 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001643 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001644 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001645
Duncan Sands4bdcb612008-07-02 17:40:58 +00001646 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001647 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001648 InFlag = Chain.getValue(1);
1649
Chris Lattnere563bbc2008-10-11 22:08:30 +00001650 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1651 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001653 InFlag = Chain.getValue(1);
1654
Bob Wilson1f595bb2009-04-17 19:07:39 +00001655 // Handle result values, copying them out of physregs into vregs that we
1656 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001657 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1658 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001659}
1660
Stuart Hastingsf222e592011-02-28 17:17:53 +00001661/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001662/// on the stack. Remember the next parameter register to allocate,
1663/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001664/// this.
1665void
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001666ARMTargetLowering::HandleByVal(
1667 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001668 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1669 assert((State->getCallOrPrologue() == Prologue ||
1670 State->getCallOrPrologue() == Call) &&
1671 "unhandled ParmContext");
1672 if ((!State->isFirstByValRegValid()) &&
1673 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiyb52ba9f2012-10-16 07:16:47 +00001674 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1675 unsigned AlignInRegs = Align / 4;
1676 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1677 for (unsigned i = 0; i < Waste; ++i)
1678 reg = State->AllocateReg(GPRArgRegs, 4);
1679 }
1680 if (reg != 0) {
1681 State->setFirstByValReg(reg);
1682 // At a call site, a byval parameter that is split between
1683 // registers and memory needs its size truncated here. In a
1684 // function prologue, such byval parameters are reassembled in
1685 // memory, and are not truncated.
1686 if (State->getCallOrPrologue() == Call) {
1687 unsigned excess = 4 * (ARM::R4 - reg);
1688 assert(size >= excess && "expected larger existing stack allocation");
1689 size -= excess;
1690 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001691 }
1692 }
1693 // Confiscate any remaining parameter registers to preclude their
1694 // assignment to subsequent parameters.
1695 while (State->AllocateReg(GPRArgRegs, 4))
1696 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001697}
1698
Dale Johannesen51e28e62010-06-03 21:09:53 +00001699/// MatchingStackOffset - Return true if the given stack call argument is
1700/// already available in the same position (relatively) of the caller's
1701/// incoming argument stack.
1702static
1703bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1704 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001705 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001706 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1707 int FI = INT_MAX;
1708 if (Arg.getOpcode() == ISD::CopyFromReg) {
1709 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001710 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001711 return false;
1712 MachineInstr *Def = MRI->getVRegDef(VR);
1713 if (!Def)
1714 return false;
1715 if (!Flags.isByVal()) {
1716 if (!TII->isLoadFromStackSlot(Def, FI))
1717 return false;
1718 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001719 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001720 }
1721 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1722 if (Flags.isByVal())
1723 // ByVal argument is passed in as a pointer but it's now being
1724 // dereferenced. e.g.
1725 // define @foo(%struct.X* %A) {
1726 // tail call @bar(%struct.X* byval %A)
1727 // }
1728 return false;
1729 SDValue Ptr = Ld->getBasePtr();
1730 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1731 if (!FINode)
1732 return false;
1733 FI = FINode->getIndex();
1734 } else
1735 return false;
1736
1737 assert(FI != INT_MAX);
1738 if (!MFI->isFixedObjectIndex(FI))
1739 return false;
1740 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1741}
1742
1743/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1744/// for tail call optimization. Targets which want to do tail call
1745/// optimization should implement this function.
1746bool
1747ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1748 CallingConv::ID CalleeCC,
1749 bool isVarArg,
1750 bool isCalleeStructRet,
1751 bool isCallerStructRet,
1752 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001753 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001754 const SmallVectorImpl<ISD::InputArg> &Ins,
1755 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001756 const Function *CallerF = DAG.getMachineFunction().getFunction();
1757 CallingConv::ID CallerCC = CallerF->getCallingConv();
1758 bool CCMatch = CallerCC == CalleeCC;
1759
1760 // Look for obvious safe cases to perform tail call optimization that do not
1761 // require ABI changes. This is what gcc calls sibcall.
1762
Jim Grosbach7616b642010-06-16 23:45:49 +00001763 // Do not sibcall optimize vararg calls unless the call site is not passing
1764 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001765 if (isVarArg && !Outs.empty())
1766 return false;
1767
1768 // Also avoid sibcall optimization if either caller or callee uses struct
1769 // return semantics.
1770 if (isCalleeStructRet || isCallerStructRet)
1771 return false;
1772
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001773 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001774 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1775 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1776 // support in the assembler and linker to be used. This would need to be
1777 // fixed to fully support tail calls in Thumb1.
1778 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001779 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1780 // LR. This means if we need to reload LR, it takes an extra instructions,
1781 // which outweighs the value of the tail call; but here we don't know yet
1782 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001783 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001784 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001785
1786 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1787 // but we need to make sure there are enough registers; the only valid
1788 // registers are the 4 used for parameters. We don't currently do this
1789 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001790 if (Subtarget->isThumb1Only())
1791 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001792
Dale Johannesen51e28e62010-06-03 21:09:53 +00001793 // If the calling conventions do not match, then we'd better make sure the
1794 // results are returned in the same way as what the caller expects.
1795 if (!CCMatch) {
1796 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001797 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1798 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001799 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1800
1801 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001802 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1803 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001804 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1805
1806 if (RVLocs1.size() != RVLocs2.size())
1807 return false;
1808 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1809 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1810 return false;
1811 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1812 return false;
1813 if (RVLocs1[i].isRegLoc()) {
1814 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1815 return false;
1816 } else {
1817 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1818 return false;
1819 }
1820 }
1821 }
1822
Manman Rene6c3cc82012-10-12 23:39:43 +00001823 // If Caller's vararg or byval argument has been split between registers and
1824 // stack, do not perform tail call, since part of the argument is in caller's
1825 // local frame.
1826 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1827 getInfo<ARMFunctionInfo>();
1828 if (AFI_Caller->getVarArgsRegSaveSize())
1829 return false;
1830
Dale Johannesen51e28e62010-06-03 21:09:53 +00001831 // If the callee takes no arguments then go on to check the results of the
1832 // call.
1833 if (!Outs.empty()) {
1834 // Check if stack adjustment is needed. For now, do not do this if any
1835 // argument is passed on the stack.
1836 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001837 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1838 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001839 CCInfo.AnalyzeCallOperands(Outs,
1840 CCAssignFnForNode(CalleeCC, false, isVarArg));
1841 if (CCInfo.getNextStackOffset()) {
1842 MachineFunction &MF = DAG.getMachineFunction();
1843
1844 // Check if the arguments are already laid out in the right way as
1845 // the caller's fixed stack objects.
1846 MachineFrameInfo *MFI = MF.getFrameInfo();
1847 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001848 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001849 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1850 i != e;
1851 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001852 CCValAssign &VA = ArgLocs[i];
1853 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001854 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001855 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001856 if (VA.getLocInfo() == CCValAssign::Indirect)
1857 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001858 if (VA.needsCustom()) {
1859 // f64 and vector types are split into multiple registers or
1860 // register/stack-slot combinations. The types will not match
1861 // the registers; give up on memory f64 refs until we figure
1862 // out what to do about this.
1863 if (!VA.isRegLoc())
1864 return false;
1865 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001866 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001867 if (RegVT == MVT::v2f64) {
1868 if (!ArgLocs[++i].isRegLoc())
1869 return false;
1870 if (!ArgLocs[++i].isRegLoc())
1871 return false;
1872 }
1873 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001874 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1875 MFI, MRI, TII))
1876 return false;
1877 }
1878 }
1879 }
1880 }
1881
1882 return true;
1883}
1884
Dan Gohman98ca4f22009-08-05 01:29:28 +00001885SDValue
1886ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001887 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001888 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001889 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001890 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001891
Bob Wilsondee46d72009-04-17 20:35:10 +00001892 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001893 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001894
Bob Wilsondee46d72009-04-17 20:35:10 +00001895 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001896 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1897 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001898
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001900 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1901 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001902
1903 // If this is the first return lowered for this function, add
1904 // the regs to the liveout set for the function.
1905 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1906 for (unsigned i = 0; i != RVLocs.size(); ++i)
1907 if (RVLocs[i].isRegLoc())
1908 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001909 }
1910
Bob Wilson1f595bb2009-04-17 19:07:39 +00001911 SDValue Flag;
1912
1913 // Copy the result values into the output registers.
1914 for (unsigned i = 0, realRVLocIdx = 0;
1915 i != RVLocs.size();
1916 ++i, ++realRVLocIdx) {
1917 CCValAssign &VA = RVLocs[i];
1918 assert(VA.isRegLoc() && "Can only return in registers!");
1919
Dan Gohmanc9403652010-07-07 15:54:55 +00001920 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001921
1922 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001923 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001924 case CCValAssign::Full: break;
1925 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001926 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 break;
1928 }
1929
Bob Wilson1f595bb2009-04-17 19:07:39 +00001930 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001932 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001933 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1934 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001935 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001937
1938 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1939 Flag = Chain.getValue(1);
1940 VA = RVLocs[++i]; // skip ahead to next loc
1941 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1942 HalfGPRs.getValue(1), Flag);
1943 Flag = Chain.getValue(1);
1944 VA = RVLocs[++i]; // skip ahead to next loc
1945
1946 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1948 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001949 }
1950 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1951 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001952 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001954 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001955 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001956 VA = RVLocs[++i]; // skip ahead to next loc
1957 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1958 Flag);
1959 } else
1960 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1961
Bob Wilsondee46d72009-04-17 20:35:10 +00001962 // Guarantee that all emitted copies are
1963 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001964 Flag = Chain.getValue(1);
1965 }
1966
1967 SDValue result;
1968 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001969 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001970 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001972
1973 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001974}
1975
Evan Chengbf010eb2012-04-10 01:51:00 +00001976bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977 if (N->getNumValues() != 1)
1978 return false;
1979 if (!N->hasNUsesOfValue(1, 0))
1980 return false;
1981
Evan Chengbf010eb2012-04-10 01:51:00 +00001982 SDValue TCChain = Chain;
1983 SDNode *Copy = *N->use_begin();
1984 if (Copy->getOpcode() == ISD::CopyToReg) {
1985 // If the copy has a glue operand, we conservatively assume it isn't safe to
1986 // perform a tail call.
1987 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1988 return false;
1989 TCChain = Copy->getOperand(0);
1990 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1991 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001993 SmallPtrSet<SDNode*, 2> Copies;
1994 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001995 UI != UE; ++UI) {
1996 if (UI->getOpcode() != ISD::CopyToReg)
1997 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001998 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001999 }
Evan Chengbf010eb2012-04-10 01:51:00 +00002000 if (Copies.size() > 2)
2001 return false;
2002
2003 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2004 UI != UE; ++UI) {
2005 SDValue UseChain = UI->getOperand(0);
2006 if (Copies.count(UseChain.getNode()))
2007 // Second CopyToReg
2008 Copy = *UI;
2009 else
2010 // First CopyToReg
2011 TCChain = UseChain;
2012 }
2013 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00002014 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00002015 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00002016 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002017 Copy = *Copy->use_begin();
2018 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00002019 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002020 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00002021 } else {
2022 return false;
2023 }
2024
Evan Cheng1bf891a2010-12-01 22:59:46 +00002025 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00002026 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2027 UI != UE; ++UI) {
2028 if (UI->getOpcode() != ARMISD::RET_FLAG)
2029 return false;
2030 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002031 }
2032
Evan Chengbf010eb2012-04-10 01:51:00 +00002033 if (!HasRet)
2034 return false;
2035
2036 Chain = TCChain;
2037 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002038}
2039
Evan Cheng485fafc2011-03-21 01:19:09 +00002040bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002041 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002042 return false;
2043
2044 if (!CI->isTailCall())
2045 return false;
2046
2047 return !Subtarget->isThumb1Only();
2048}
2049
Bob Wilsonb62d2572009-11-03 00:02:05 +00002050// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2051// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2052// one of the above mentioned nodes. It has to be wrapped because otherwise
2053// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2054// be used to form addressing mode. These wrapped nodes will be selected
2055// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002056static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002057 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002058 // FIXME there is no actual debug info here
2059 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002060 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002061 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002062 if (CP->isMachineConstantPoolEntry())
2063 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2064 CP->getAlignment());
2065 else
2066 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2067 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002068 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002069}
2070
Jim Grosbache1102ca2010-07-19 17:20:38 +00002071unsigned ARMTargetLowering::getJumpTableEncoding() const {
2072 return MachineJumpTableInfo::EK_Inline;
2073}
2074
Dan Gohmand858e902010-04-17 15:26:15 +00002075SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2076 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002077 MachineFunction &MF = DAG.getMachineFunction();
2078 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2079 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002080 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002081 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002082 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002083 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2084 SDValue CPAddr;
2085 if (RelocM == Reloc::Static) {
2086 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2087 } else {
2088 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002089 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002090 ARMConstantPoolValue *CPV =
2091 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2092 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002093 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2094 }
2095 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2096 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002097 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002098 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002099 if (RelocM == Reloc::Static)
2100 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002101 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002102 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002103}
2104
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002105// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002106SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002107ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002108 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002109 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002110 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002111 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002112 MachineFunction &MF = DAG.getMachineFunction();
2113 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002114 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002115 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002116 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2117 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002118 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002119 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002120 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002121 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002122 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002124
Evan Chenge7e0d622009-11-06 22:24:13 +00002125 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002126 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002127
2128 // call __tls_get_addr.
2129 ArgListTy Args;
2130 ArgListEntry Entry;
2131 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002132 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002133 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002134 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002135 TargetLowering::CallLoweringInfo CLI(Chain,
2136 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002137 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002138 0, CallingConv::C, /*isTailCall=*/false,
2139 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002140 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002141 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002142 return CallResult.first;
2143}
2144
2145// Lower ISD::GlobalTLSAddress using the "initial exec" or
2146// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002147SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002148ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002149 SelectionDAG &DAG,
2150 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002151 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002153 SDValue Offset;
2154 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002155 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002156 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002157 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002158
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002159 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002160 MachineFunction &MF = DAG.getMachineFunction();
2161 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002162 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002163 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002164 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2165 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002166 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2167 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2168 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002169 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002170 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002171 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002172 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002173 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174 Chain = Offset.getValue(1);
2175
Evan Chenge7e0d622009-11-06 22:24:13 +00002176 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002177 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002178
Evan Cheng9eda6892009-10-31 03:39:36 +00002179 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002180 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002181 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002182 } else {
2183 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002184 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002185 ARMConstantPoolValue *CPV =
2186 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002187 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002188 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002189 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002190 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002191 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002192 }
2193
2194 // The address of the thread local variable is the add of the thread
2195 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002196 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002197}
2198
Dan Gohman475871a2008-07-27 21:46:04 +00002199SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002200ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002201 // TODO: implement the "local dynamic" model
2202 assert(Subtarget->isTargetELF() &&
2203 "TLS not implemented for non-ELF targets");
2204 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002205
2206 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2207
2208 switch (model) {
2209 case TLSModel::GeneralDynamic:
2210 case TLSModel::LocalDynamic:
2211 return LowerToTLSGeneralDynamicModel(GA, DAG);
2212 case TLSModel::InitialExec:
2213 case TLSModel::LocalExec:
2214 return LowerToTLSExecModels(GA, DAG, model);
2215 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002216 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002217}
2218
Dan Gohman475871a2008-07-27 21:46:04 +00002219SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002220 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002221 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002222 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002223 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002224 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2225 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002226 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002227 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002228 ARMConstantPoolConstant::Create(GV,
2229 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002230 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002231 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002232 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002233 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002234 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002235 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002236 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002237 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002238 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002239 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002240 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002241 MachinePointerInfo::getGOT(),
2242 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002243 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002244 }
2245
2246 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002247 // pair. This is always cheaper.
2248 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002249 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002250 // FIXME: Once remat is capable of dealing with instructions with register
2251 // operands, expand this into two nodes.
2252 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2253 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002254 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002255 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2256 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2257 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2258 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002259 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002260 }
2261}
2262
Dan Gohman475871a2008-07-27 21:46:04 +00002263SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002264 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002265 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002266 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002267 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002268 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002269 MachineFunction &MF = DAG.getMachineFunction();
2270 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2271
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002272 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2273 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002274 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
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.
Evan Cheng53519f02011-01-21 18:55:51 +00002278 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002279 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2280 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2281
Evan Cheng53519f02011-01-21 18:55:51 +00002282 unsigned Wrapper = (RelocM == Reloc::PIC_)
2283 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2284 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002285 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002286 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2287 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002288 MachinePointerInfo::getGOT(),
2289 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002290 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002291 }
2292
2293 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002294 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002295 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002296 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002297 } else {
2298 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002299 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2300 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002301 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2302 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002303 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002304 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002305 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002306
Evan Cheng9eda6892009-10-31 03:39:36 +00002307 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002308 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002309 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002311
2312 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002313 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002314 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002315 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002316
Evan Cheng63476a82009-09-03 07:04:02 +00002317 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002318 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002319 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002320
2321 return Result;
2322}
2323
Dan Gohman475871a2008-07-27 21:46:04 +00002324SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002325 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002326 assert(Subtarget->isTargetELF() &&
2327 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002328 MachineFunction &MF = DAG.getMachineFunction();
2329 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002330 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002331 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002332 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002333 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002334 ARMConstantPoolValue *CPV =
2335 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2336 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002337 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002338 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002339 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002340 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002341 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002342 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002343 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002344}
2345
Jim Grosbach0e0da732009-05-12 23:59:14 +00002346SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002347ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2348 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002349 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002350 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2351 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002352 Op.getOperand(1), Val);
2353}
2354
2355SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002356ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2357 DebugLoc dl = Op.getDebugLoc();
2358 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2359 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2360}
2361
2362SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002363ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002364 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002365 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002366 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002367 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002368 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002369 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002370 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002371 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2372 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002373 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002374 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002375 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002376 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002377 EVT PtrVT = getPointerTy();
2378 DebugLoc dl = Op.getDebugLoc();
2379 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2380 SDValue CPAddr;
2381 unsigned PCAdj = (RelocM != Reloc::PIC_)
2382 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002383 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002384 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2385 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002386 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002387 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002388 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002389 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002390 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002391 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002392
2393 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002394 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002395 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2396 }
2397 return Result;
2398 }
Evan Cheng92e39162011-03-29 23:06:19 +00002399 case Intrinsic::arm_neon_vmulls:
2400 case Intrinsic::arm_neon_vmullu: {
2401 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2402 ? ARMISD::VMULLs : ARMISD::VMULLu;
2403 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2404 Op.getOperand(1), Op.getOperand(2));
2405 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002406 }
2407}
2408
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002409static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002410 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002411 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002412 if (!Subtarget->hasDataBarrier()) {
2413 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2414 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2415 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002416 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002417 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002418 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002419 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002420 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002421
2422 SDValue Op5 = Op.getOperand(5);
2423 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2424 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2425 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2426 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2427
2428 ARM_MB::MemBOpt DMBOpt;
2429 if (isDeviceBarrier)
2430 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2431 else
2432 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2433 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2434 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002435}
2436
Eli Friedman26689ac2011-08-03 21:06:02 +00002437
2438static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2439 const ARMSubtarget *Subtarget) {
2440 // FIXME: handle "fence singlethread" more efficiently.
2441 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002442 if (!Subtarget->hasDataBarrier()) {
2443 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2444 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2445 // here.
2446 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2447 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002448 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002449 DAG.getConstant(0, MVT::i32));
2450 }
2451
Eli Friedman26689ac2011-08-03 21:06:02 +00002452 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002453 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002454}
2455
Evan Chengdfed19f2010-11-03 06:34:55 +00002456static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2457 const ARMSubtarget *Subtarget) {
2458 // ARM pre v5TE and Thumb1 does not have preload instructions.
2459 if (!(Subtarget->isThumb2() ||
2460 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2461 // Just preserve the chain.
2462 return Op.getOperand(0);
2463
2464 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002465 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2466 if (!isRead &&
2467 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2468 // ARMv7 with MP extension has PLDW.
2469 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002470
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002471 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2472 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002473 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002474 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002475 isData = ~isData & 1;
2476 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002477
2478 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002479 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2480 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002481}
2482
Dan Gohman1e93df62010-04-17 14:41:14 +00002483static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2484 MachineFunction &MF = DAG.getMachineFunction();
2485 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2486
Evan Chenga8e29892007-01-19 07:51:42 +00002487 // vastart just stores the address of the VarArgsFrameIndex slot into the
2488 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002489 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002490 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002491 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002492 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002493 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2494 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002495}
2496
Dan Gohman475871a2008-07-27 21:46:04 +00002497SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002498ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2499 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002500 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002501 MachineFunction &MF = DAG.getMachineFunction();
2502 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2503
Craig Topper44d23822012-02-22 05:59:10 +00002504 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002505 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002506 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002507 else
Craig Topper420761a2012-04-20 07:30:17 +00002508 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002509
2510 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002511 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002512 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002513
2514 SDValue ArgValue2;
2515 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002516 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002517 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002518
2519 // Create load node to retrieve arguments from the stack.
2520 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002521 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002522 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002523 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002524 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002525 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002527 }
2528
Jim Grosbache5165492009-11-09 00:11:35 +00002529 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002530}
2531
Stuart Hastingsc7315872011-04-20 16:47:52 +00002532void
2533ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2534 unsigned &VARegSize, unsigned &VARegSaveSize)
2535 const {
2536 unsigned NumGPRs;
2537 if (CCInfo.isFirstByValRegValid())
2538 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2539 else {
2540 unsigned int firstUnalloced;
2541 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2542 sizeof(GPRArgRegs) /
2543 sizeof(GPRArgRegs[0]));
2544 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2545 }
2546
2547 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2548 VARegSize = NumGPRs * 4;
2549 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2550}
2551
2552// The remaining GPRs hold either the beginning of variable-argument
2553// data, or the beginning of an aggregate passed by value (usuall
2554// byval). Either way, we allocate stack slots adjacent to the data
2555// provided by our caller, and store the unallocated registers there.
2556// If this is a variadic function, the va_list pointer will begin with
2557// these values; otherwise, this reassembles a (byval) structure that
2558// was split between registers and memory.
2559void
2560ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2561 DebugLoc dl, SDValue &Chain,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002562 const Value *OrigArg,
2563 unsigned OffsetFromOrigArg,
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002564 unsigned ArgOffset,
2565 bool ForceMutable) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002566 MachineFunction &MF = DAG.getMachineFunction();
2567 MachineFrameInfo *MFI = MF.getFrameInfo();
2568 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2569 unsigned firstRegToSaveIndex;
2570 if (CCInfo.isFirstByValRegValid())
2571 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2572 else {
2573 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2574 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2575 }
2576
2577 unsigned VARegSize, VARegSaveSize;
2578 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2579 if (VARegSaveSize) {
2580 // If this function is vararg, store any remaining integer argument regs
2581 // to their spots on the stack so that they may be loaded by deferencing
2582 // the result of va_next.
2583 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002584 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2585 ArgOffset + VARegSaveSize
2586 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002587 false));
2588 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2589 getPointerTy());
2590
2591 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002592 for (unsigned i = 0; firstRegToSaveIndex < 4; ++firstRegToSaveIndex, ++i) {
Craig Topper44d23822012-02-22 05:59:10 +00002593 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002594 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002595 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002596 else
Craig Topper420761a2012-04-20 07:30:17 +00002597 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002598
2599 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2600 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2601 SDValue Store =
2602 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002603 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002604 false, false, 0);
2605 MemOps.push_back(Store);
2606 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2607 DAG.getConstant(4, getPointerTy()));
2608 }
2609 if (!MemOps.empty())
2610 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2611 &MemOps[0], MemOps.size());
2612 } else
2613 // This will point to the next argument passed via stack.
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002614 AFI->setVarArgsFrameIndex(
2615 MFI->CreateFixedObject(4, ArgOffset, !ForceMutable));
Stuart Hastingsc7315872011-04-20 16:47:52 +00002616}
2617
Bob Wilson5bafff32009-06-22 23:27:02 +00002618SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002619ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002620 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002621 const SmallVectorImpl<ISD::InputArg>
2622 &Ins,
2623 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002624 SmallVectorImpl<SDValue> &InVals)
2625 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002626 MachineFunction &MF = DAG.getMachineFunction();
2627 MachineFrameInfo *MFI = MF.getFrameInfo();
2628
Bob Wilson1f595bb2009-04-17 19:07:39 +00002629 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2630
2631 // Assign locations to all of the incoming arguments.
2632 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002633 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2634 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002635 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002636 CCAssignFnForNode(CallConv, /* Return*/ false,
2637 isVarArg));
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002638
Bob Wilson1f595bb2009-04-17 19:07:39 +00002639 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002640 int lastInsIndex = -1;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002641 SDValue ArgValue;
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002642 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2643 unsigned CurArgIdx = 0;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002644 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2645 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002646 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2647 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsondee46d72009-04-17 20:35:10 +00002648 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002649 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002650 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002651
Bob Wilson1f595bb2009-04-17 19:07:39 +00002652 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002653 // f64 and vector types are split up into multiple registers or
2654 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002656 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002657 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002658 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002659 SDValue ArgValue2;
2660 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002661 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002662 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2663 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002664 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002665 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002666 } else {
2667 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2668 Chain, DAG, dl);
2669 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2671 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002672 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002673 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002674 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2675 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002676 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002677
Bob Wilson5bafff32009-06-22 23:27:02 +00002678 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002679 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002680
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002682 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002683 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002684 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002685 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002686 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002687 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002688 RC = AFI->isThumb1OnlyFunction() ?
2689 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2690 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002691 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002692 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002693
2694 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002695 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002696 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002697 }
2698
2699 // If this is an 8 or 16-bit value, it is really passed promoted
2700 // to 32 bits. Insert an assert[sz]ext to capture this, then
2701 // truncate to the right size.
2702 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002703 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002704 case CCValAssign::Full: break;
2705 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002706 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002707 break;
2708 case CCValAssign::SExt:
2709 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2710 DAG.getValueType(VA.getValVT()));
2711 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2712 break;
2713 case CCValAssign::ZExt:
2714 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2715 DAG.getValueType(VA.getValVT()));
2716 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2717 break;
2718 }
2719
Dan Gohman98ca4f22009-08-05 01:29:28 +00002720 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002721
2722 } else { // VA.isRegLoc()
2723
2724 // sanity check
2725 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002726 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002727
Stuart Hastingsf222e592011-02-28 17:17:53 +00002728 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002729
Stuart Hastingsf222e592011-02-28 17:17:53 +00002730 // Some Ins[] entries become multiple ArgLoc[] entries.
2731 // Process them only once.
2732 if (index != lastInsIndex)
2733 {
2734 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002735 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002736 // This can be changed with more analysis.
2737 // In case of tail call optimization mark all arguments mutable.
2738 // Since they could be overwritten by lowering of arguments in case of
2739 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002740 if (Flags.isByVal()) {
Stepan Dyatkovskiy0d3c8d52012-10-19 08:23:06 +00002741 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2742 if (!AFI->getVarArgsFrameIndex()) {
2743 VarArgStyleRegisters(CCInfo, DAG,
2744 dl, Chain, CurOrigArg,
2745 Ins[VA.getValNo()].PartOffset,
2746 VA.getLocMemOffset(),
2747 true /*force mutable frames*/);
2748 int VAFrameIndex = AFI->getVarArgsFrameIndex();
2749 InVals.push_back(DAG.getFrameIndex(VAFrameIndex, getPointerTy()));
2750 } else {
2751 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
2752 VA.getLocMemOffset(), false);
2753 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2754 }
Stuart Hastingsf222e592011-02-28 17:17:53 +00002755 } else {
2756 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2757 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002758
Stuart Hastingsf222e592011-02-28 17:17:53 +00002759 // Create load nodes to retrieve arguments from the stack.
2760 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2761 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2762 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002763 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002764 }
2765 lastInsIndex = index;
2766 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002767 }
2768 }
2769
2770 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002771 if (isVarArg)
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00002772 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0, 0,
2773 CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002774
Dan Gohman98ca4f22009-08-05 01:29:28 +00002775 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002776}
2777
2778/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002779static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002780 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002781 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002782 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002783 // Maybe this has already been legalized into the constant pool?
2784 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002785 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002786 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002787 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002788 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002789 }
2790 }
2791 return false;
2792}
2793
Evan Chenga8e29892007-01-19 07:51:42 +00002794/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2795/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002796SDValue
2797ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002798 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002799 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002800 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002801 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002802 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002803 // Constant does not fit, try adjusting it by one?
2804 switch (CC) {
2805 default: break;
2806 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002807 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002808 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002809 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002810 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002811 }
2812 break;
2813 case ISD::SETULT:
2814 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002815 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002816 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002817 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002818 }
2819 break;
2820 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002821 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002822 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002823 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002824 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002825 }
2826 break;
2827 case ISD::SETULE:
2828 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002829 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002830 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002831 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002832 }
2833 break;
2834 }
2835 }
2836 }
2837
2838 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002839 ARMISD::NodeType CompareType;
2840 switch (CondCode) {
2841 default:
2842 CompareType = ARMISD::CMP;
2843 break;
2844 case ARMCC::EQ:
2845 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002846 // Uses only Z Flag
2847 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002848 break;
2849 }
Evan Cheng218977b2010-07-13 19:27:42 +00002850 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002851 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002852}
2853
2854/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002855SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002856ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002857 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002858 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002859 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002860 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002861 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002862 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2863 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002864}
2865
Bob Wilson79f56c92011-03-08 01:17:20 +00002866/// duplicateCmp - Glue values can have only one use, so this function
2867/// duplicates a comparison node.
2868SDValue
2869ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2870 unsigned Opc = Cmp.getOpcode();
2871 DebugLoc DL = Cmp.getDebugLoc();
2872 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2873 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2874
2875 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2876 Cmp = Cmp.getOperand(0);
2877 Opc = Cmp.getOpcode();
2878 if (Opc == ARMISD::CMPFP)
2879 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2880 else {
2881 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2882 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2883 }
2884 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2885}
2886
Bill Wendlingde2b1512010-08-11 08:43:16 +00002887SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2888 SDValue Cond = Op.getOperand(0);
2889 SDValue SelectTrue = Op.getOperand(1);
2890 SDValue SelectFalse = Op.getOperand(2);
2891 DebugLoc dl = Op.getDebugLoc();
2892
2893 // Convert:
2894 //
2895 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2896 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2897 //
2898 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2899 const ConstantSDNode *CMOVTrue =
2900 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2901 const ConstantSDNode *CMOVFalse =
2902 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2903
2904 if (CMOVTrue && CMOVFalse) {
2905 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2906 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2907
2908 SDValue True;
2909 SDValue False;
2910 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2911 True = SelectTrue;
2912 False = SelectFalse;
2913 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2914 True = SelectFalse;
2915 False = SelectTrue;
2916 }
2917
2918 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002919 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002920 SDValue ARMcc = Cond.getOperand(2);
2921 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002922 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002923 assert(True.getValueType() == VT);
2924 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002925 }
2926 }
2927 }
2928
Dan Gohmandb953892012-02-24 00:09:36 +00002929 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2930 // undefined bits before doing a full-word comparison with zero.
2931 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2932 DAG.getConstant(1, Cond.getValueType()));
2933
Bill Wendlingde2b1512010-08-11 08:43:16 +00002934 return DAG.getSelectCC(dl, Cond,
2935 DAG.getConstant(0, Cond.getValueType()),
2936 SelectTrue, SelectFalse, ISD::SETNE);
2937}
2938
Dan Gohmand858e902010-04-17 15:26:15 +00002939SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002940 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002941 SDValue LHS = Op.getOperand(0);
2942 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002943 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002944 SDValue TrueVal = Op.getOperand(2);
2945 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002946 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002947
Owen Anderson825b72b2009-08-11 20:47:22 +00002948 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002949 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002950 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002951 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002952 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002953 }
2954
2955 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002956 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002957
Evan Cheng218977b2010-07-13 19:27:42 +00002958 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2959 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002961 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002962 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002963 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002964 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002965 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002966 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002967 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002968 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002969 }
2970 return Result;
2971}
2972
Evan Cheng218977b2010-07-13 19:27:42 +00002973/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2974/// to morph to an integer compare sequence.
2975static bool canChangeToInt(SDValue Op, bool &SeenZero,
2976 const ARMSubtarget *Subtarget) {
2977 SDNode *N = Op.getNode();
2978 if (!N->hasOneUse())
2979 // Otherwise it requires moving the value from fp to integer registers.
2980 return false;
2981 if (!N->getNumValues())
2982 return false;
2983 EVT VT = Op.getValueType();
2984 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2985 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2986 // vmrs are very slow, e.g. cortex-a8.
2987 return false;
2988
2989 if (isFloatingPointZero(Op)) {
2990 SeenZero = true;
2991 return true;
2992 }
2993 return ISD::isNormalLoad(N);
2994}
2995
2996static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2997 if (isFloatingPointZero(Op))
2998 return DAG.getConstant(0, MVT::i32);
2999
3000 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3001 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003002 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003003 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003004 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003005
3006 llvm_unreachable("Unknown VFP cmp argument!");
3007}
3008
3009static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3010 SDValue &RetVal1, SDValue &RetVal2) {
3011 if (isFloatingPointZero(Op)) {
3012 RetVal1 = DAG.getConstant(0, MVT::i32);
3013 RetVal2 = DAG.getConstant(0, MVT::i32);
3014 return;
3015 }
3016
3017 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3018 SDValue Ptr = Ld->getBasePtr();
3019 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3020 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003021 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00003022 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003023 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00003024
3025 EVT PtrType = Ptr.getValueType();
3026 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3027 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
3028 PtrType, Ptr, DAG.getConstant(4, PtrType));
3029 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
3030 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003031 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00003032 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003033 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00003034 return;
3035 }
3036
3037 llvm_unreachable("Unknown VFP cmp argument!");
3038}
3039
3040/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3041/// f32 and even f64 comparisons to integer ones.
3042SDValue
3043ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3044 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003045 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003046 SDValue LHS = Op.getOperand(2);
3047 SDValue RHS = Op.getOperand(3);
3048 SDValue Dest = Op.getOperand(4);
3049 DebugLoc dl = Op.getDebugLoc();
3050
Evan Chengfc501a32012-03-01 23:27:13 +00003051 bool LHSSeenZero = false;
3052 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3053 bool RHSSeenZero = false;
3054 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3055 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003056 // If unsafe fp math optimization is enabled and there are no other uses of
3057 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003058 // to an integer comparison.
3059 if (CC == ISD::SETOEQ)
3060 CC = ISD::SETEQ;
3061 else if (CC == ISD::SETUNE)
3062 CC = ISD::SETNE;
3063
Evan Chengfc501a32012-03-01 23:27:13 +00003064 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003065 SDValue ARMcc;
3066 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003067 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3068 bitcastf32Toi32(LHS, DAG), Mask);
3069 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3070 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003071 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3072 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3073 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3074 Chain, Dest, ARMcc, CCR, Cmp);
3075 }
3076
3077 SDValue LHS1, LHS2;
3078 SDValue RHS1, RHS2;
3079 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3080 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003081 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3082 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003083 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3084 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003085 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003086 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3087 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3088 }
3089
3090 return SDValue();
3091}
3092
3093SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3094 SDValue Chain = Op.getOperand(0);
3095 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3096 SDValue LHS = Op.getOperand(2);
3097 SDValue RHS = Op.getOperand(3);
3098 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003099 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003100
Owen Anderson825b72b2009-08-11 20:47:22 +00003101 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003102 SDValue ARMcc;
3103 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003104 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003106 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003107 }
3108
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003110
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003111 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003112 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3113 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3114 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3115 if (Result.getNode())
3116 return Result;
3117 }
3118
Evan Chenga8e29892007-01-19 07:51:42 +00003119 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003120 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003121
Evan Cheng218977b2010-07-13 19:27:42 +00003122 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3123 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003125 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003126 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003127 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003128 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003129 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3130 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003131 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003132 }
3133 return Res;
3134}
3135
Dan Gohmand858e902010-04-17 15:26:15 +00003136SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SDValue Chain = Op.getOperand(0);
3138 SDValue Table = Op.getOperand(1);
3139 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003140 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003141
Owen Andersone50ed302009-08-10 22:56:29 +00003142 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003143 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3144 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003145 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003148 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3149 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003150 if (Subtarget->isThumb2()) {
3151 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3152 // which does another jump to the destination. This also makes it easier
3153 // to translate it to TBB / TBH later.
3154 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003156 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003157 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003158 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003159 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003160 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003161 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003162 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003163 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003164 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003165 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003166 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003167 MachinePointerInfo::getJumpTable(),
3168 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003169 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003171 }
Evan Chenga8e29892007-01-19 07:51:42 +00003172}
3173
Eli Friedman14e809c2011-11-09 23:36:02 +00003174static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003175 EVT VT = Op.getValueType();
3176 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003177
James Molloy873fd5f2012-02-20 09:24:05 +00003178 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3179 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3180 return Op;
3181 return DAG.UnrollVectorOp(Op.getNode());
3182 }
3183
3184 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3185 "Invalid type for custom lowering!");
3186 if (VT != MVT::v4i16)
3187 return DAG.UnrollVectorOp(Op.getNode());
3188
3189 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3190 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003191}
3192
Bob Wilson76a312b2010-03-19 22:51:32 +00003193static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003194 EVT VT = Op.getValueType();
3195 if (VT.isVector())
3196 return LowerVectorFP_TO_INT(Op, DAG);
3197
Bob Wilson76a312b2010-03-19 22:51:32 +00003198 DebugLoc dl = Op.getDebugLoc();
3199 unsigned Opc;
3200
3201 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003202 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003203 case ISD::FP_TO_SINT:
3204 Opc = ARMISD::FTOSI;
3205 break;
3206 case ISD::FP_TO_UINT:
3207 Opc = ARMISD::FTOUI;
3208 break;
3209 }
3210 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003211 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003212}
3213
Cameron Zwarich3007d332011-03-29 21:41:55 +00003214static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3215 EVT VT = Op.getValueType();
3216 DebugLoc dl = Op.getDebugLoc();
3217
Eli Friedman14e809c2011-11-09 23:36:02 +00003218 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3219 if (VT.getVectorElementType() == MVT::f32)
3220 return Op;
3221 return DAG.UnrollVectorOp(Op.getNode());
3222 }
3223
Duncan Sands1f6a3292011-08-12 14:54:45 +00003224 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3225 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003226 if (VT != MVT::v4f32)
3227 return DAG.UnrollVectorOp(Op.getNode());
3228
3229 unsigned CastOpc;
3230 unsigned Opc;
3231 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003232 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003233 case ISD::SINT_TO_FP:
3234 CastOpc = ISD::SIGN_EXTEND;
3235 Opc = ISD::SINT_TO_FP;
3236 break;
3237 case ISD::UINT_TO_FP:
3238 CastOpc = ISD::ZERO_EXTEND;
3239 Opc = ISD::UINT_TO_FP;
3240 break;
3241 }
3242
3243 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3244 return DAG.getNode(Opc, dl, VT, Op);
3245}
3246
Bob Wilson76a312b2010-03-19 22:51:32 +00003247static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3248 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003249 if (VT.isVector())
3250 return LowerVectorINT_TO_FP(Op, DAG);
3251
Bob Wilson76a312b2010-03-19 22:51:32 +00003252 DebugLoc dl = Op.getDebugLoc();
3253 unsigned Opc;
3254
3255 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003256 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003257 case ISD::SINT_TO_FP:
3258 Opc = ARMISD::SITOF;
3259 break;
3260 case ISD::UINT_TO_FP:
3261 Opc = ARMISD::UITOF;
3262 break;
3263 }
3264
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003265 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003266 return DAG.getNode(Opc, dl, VT, Op);
3267}
3268
Evan Cheng515fe3a2010-07-08 02:08:50 +00003269SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003270 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003271 SDValue Tmp0 = Op.getOperand(0);
3272 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003273 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003274 EVT VT = Op.getValueType();
3275 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003276 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3277 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3278 bool UseNEON = !InGPR && Subtarget->hasNEON();
3279
3280 if (UseNEON) {
3281 // Use VBSL to copy the sign bit.
3282 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3283 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3284 DAG.getTargetConstant(EncodedVal, MVT::i32));
3285 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3286 if (VT == MVT::f64)
3287 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3288 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3289 DAG.getConstant(32, MVT::i32));
3290 else /*if (VT == MVT::f32)*/
3291 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3292 if (SrcVT == MVT::f32) {
3293 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3294 if (VT == MVT::f64)
3295 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3296 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3297 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003298 } else if (VT == MVT::f32)
3299 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3300 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3301 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003302 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3303 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3304
3305 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3306 MVT::i32);
3307 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3308 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3309 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003310
Evan Chenge573fb32011-02-23 02:24:55 +00003311 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3312 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3313 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003314 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003315 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3316 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3317 DAG.getConstant(0, MVT::i32));
3318 } else {
3319 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3320 }
3321
3322 return Res;
3323 }
Evan Chengc143dd42011-02-11 02:28:55 +00003324
3325 // Bitcast operand 1 to i32.
3326 if (SrcVT == MVT::f64)
3327 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3328 &Tmp1, 1).getValue(1);
3329 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3330
Evan Chenge573fb32011-02-23 02:24:55 +00003331 // Or in the signbit with integer operations.
3332 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3333 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3334 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3335 if (VT == MVT::f32) {
3336 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3337 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3338 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3339 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003340 }
3341
Evan Chenge573fb32011-02-23 02:24:55 +00003342 // f64: Or the high part with signbit and then combine two parts.
3343 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3344 &Tmp0, 1);
3345 SDValue Lo = Tmp0.getValue(0);
3346 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3347 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3348 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003349}
3350
Evan Cheng2457f2c2010-05-22 01:47:14 +00003351SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3352 MachineFunction &MF = DAG.getMachineFunction();
3353 MachineFrameInfo *MFI = MF.getFrameInfo();
3354 MFI->setReturnAddressIsTaken(true);
3355
3356 EVT VT = Op.getValueType();
3357 DebugLoc dl = Op.getDebugLoc();
3358 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3359 if (Depth) {
3360 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3361 SDValue Offset = DAG.getConstant(4, MVT::i32);
3362 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3363 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003364 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003365 }
3366
3367 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003368 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003369 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3370}
3371
Dan Gohmand858e902010-04-17 15:26:15 +00003372SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003373 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3374 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003375
Owen Andersone50ed302009-08-10 22:56:29 +00003376 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003377 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3378 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003379 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003380 ? ARM::R7 : ARM::R11;
3381 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3382 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003383 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3384 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003385 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003386 return FrameAddr;
3387}
3388
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003389/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003390/// expand a bit convert where either the source or destination type is i64 to
3391/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3392/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3393/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003394static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003395 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3396 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003397 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003398
Bob Wilson9f3f0612010-04-17 05:30:19 +00003399 // This function is only supposed to be called for i64 types, either as the
3400 // source or destination of the bit convert.
3401 EVT SrcVT = Op.getValueType();
3402 EVT DstVT = N->getValueType(0);
3403 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003404 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003405
Bob Wilson9f3f0612010-04-17 05:30:19 +00003406 // Turn i64->f64 into VMOVDRR.
3407 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3409 DAG.getConstant(0, MVT::i32));
3410 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3411 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003412 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003413 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003414 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003415
Jim Grosbache5165492009-11-09 00:11:35 +00003416 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003417 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3418 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3419 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3420 // Merge the pieces into a single i64 value.
3421 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3422 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003423
Bob Wilson9f3f0612010-04-17 05:30:19 +00003424 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003425}
3426
Bob Wilson5bafff32009-06-22 23:27:02 +00003427/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003428/// Zero vectors are used to represent vector negation and in those cases
3429/// will be implemented with the NEON VNEG instruction. However, VNEG does
3430/// not support i64 elements, so sometimes the zero vectors will need to be
3431/// explicitly constructed. Regardless, use a canonical VMOV to create the
3432/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003433static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003434 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003435 // The canonical modified immediate encoding of a zero vector is....0!
3436 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3437 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3438 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003439 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003440}
3441
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003442/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3443/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003444SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3445 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003446 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3447 EVT VT = Op.getValueType();
3448 unsigned VTBits = VT.getSizeInBits();
3449 DebugLoc dl = Op.getDebugLoc();
3450 SDValue ShOpLo = Op.getOperand(0);
3451 SDValue ShOpHi = Op.getOperand(1);
3452 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003453 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003454 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003455
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003456 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3457
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003458 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3459 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3460 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3461 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3462 DAG.getConstant(VTBits, MVT::i32));
3463 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3464 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003465 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003466
3467 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3468 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003469 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003470 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003471 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003472 CCR, Cmp);
3473
3474 SDValue Ops[2] = { Lo, Hi };
3475 return DAG.getMergeValues(Ops, 2, dl);
3476}
3477
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003478/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3479/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003480SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3481 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003482 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3483 EVT VT = Op.getValueType();
3484 unsigned VTBits = VT.getSizeInBits();
3485 DebugLoc dl = Op.getDebugLoc();
3486 SDValue ShOpLo = Op.getOperand(0);
3487 SDValue ShOpHi = Op.getOperand(1);
3488 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003489 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003490
3491 assert(Op.getOpcode() == ISD::SHL_PARTS);
3492 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3493 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3494 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3495 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3496 DAG.getConstant(VTBits, MVT::i32));
3497 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3498 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3499
3500 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3501 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3502 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003503 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003504 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003505 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003506 CCR, Cmp);
3507
3508 SDValue Ops[2] = { Lo, Hi };
3509 return DAG.getMergeValues(Ops, 2, dl);
3510}
3511
Jim Grosbach4725ca72010-09-08 03:54:02 +00003512SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003513 SelectionDAG &DAG) const {
3514 // The rounding mode is in bits 23:22 of the FPSCR.
3515 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3516 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3517 // so that the shift + and get folded into a bitfield extract.
3518 DebugLoc dl = Op.getDebugLoc();
3519 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3520 DAG.getConstant(Intrinsic::arm_get_fpscr,
3521 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003522 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003523 DAG.getConstant(1U << 22, MVT::i32));
3524 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3525 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003526 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003527 DAG.getConstant(3, MVT::i32));
3528}
3529
Jim Grosbach3482c802010-01-18 19:58:49 +00003530static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3531 const ARMSubtarget *ST) {
3532 EVT VT = N->getValueType(0);
3533 DebugLoc dl = N->getDebugLoc();
3534
3535 if (!ST->hasV6T2Ops())
3536 return SDValue();
3537
3538 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3539 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3540}
3541
Bob Wilson5bafff32009-06-22 23:27:02 +00003542static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3543 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003544 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003545 DebugLoc dl = N->getDebugLoc();
3546
Bob Wilsond5448bb2010-11-18 21:16:28 +00003547 if (!VT.isVector())
3548 return SDValue();
3549
Bob Wilson5bafff32009-06-22 23:27:02 +00003550 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003551 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003552
Bob Wilsond5448bb2010-11-18 21:16:28 +00003553 // Left shifts translate directly to the vshiftu intrinsic.
3554 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003555 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003556 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3557 N->getOperand(0), N->getOperand(1));
3558
3559 assert((N->getOpcode() == ISD::SRA ||
3560 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3561
3562 // NEON uses the same intrinsics for both left and right shifts. For
3563 // right shifts, the shift amounts are negative, so negate the vector of
3564 // shift amounts.
3565 EVT ShiftVT = N->getOperand(1).getValueType();
3566 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3567 getZeroVector(ShiftVT, DAG, dl),
3568 N->getOperand(1));
3569 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3570 Intrinsic::arm_neon_vshifts :
3571 Intrinsic::arm_neon_vshiftu);
3572 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3573 DAG.getConstant(vshiftInt, MVT::i32),
3574 N->getOperand(0), NegatedCount);
3575}
3576
3577static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3578 const ARMSubtarget *ST) {
3579 EVT VT = N->getValueType(0);
3580 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003581
Eli Friedmance392eb2009-08-22 03:13:10 +00003582 // We can get here for a node like i32 = ISD::SHL i32, i64
3583 if (VT != MVT::i64)
3584 return SDValue();
3585
3586 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003587 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003588
Chris Lattner27a6c732007-11-24 07:07:01 +00003589 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3590 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003591 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003592 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003593
Chris Lattner27a6c732007-11-24 07:07:01 +00003594 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003595 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003596
Chris Lattner27a6c732007-11-24 07:07:01 +00003597 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003599 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003601 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003602
Chris Lattner27a6c732007-11-24 07:07:01 +00003603 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3604 // captures the result into a carry flag.
3605 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003606 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003607
Chris Lattner27a6c732007-11-24 07:07:01 +00003608 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003610
Chris Lattner27a6c732007-11-24 07:07:01 +00003611 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003613}
3614
Bob Wilson5bafff32009-06-22 23:27:02 +00003615static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3616 SDValue TmpOp0, TmpOp1;
3617 bool Invert = false;
3618 bool Swap = false;
3619 unsigned Opc = 0;
3620
3621 SDValue Op0 = Op.getOperand(0);
3622 SDValue Op1 = Op.getOperand(1);
3623 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003624 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003625 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3626 DebugLoc dl = Op.getDebugLoc();
3627
3628 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3629 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003630 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 case ISD::SETUNE:
3632 case ISD::SETNE: Invert = true; // Fallthrough
3633 case ISD::SETOEQ:
3634 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3635 case ISD::SETOLT:
3636 case ISD::SETLT: Swap = true; // Fallthrough
3637 case ISD::SETOGT:
3638 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3639 case ISD::SETOLE:
3640 case ISD::SETLE: Swap = true; // Fallthrough
3641 case ISD::SETOGE:
3642 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3643 case ISD::SETUGE: Swap = true; // Fallthrough
3644 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3645 case ISD::SETUGT: Swap = true; // Fallthrough
3646 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3647 case ISD::SETUEQ: Invert = true; // Fallthrough
3648 case ISD::SETONE:
3649 // Expand this to (OLT | OGT).
3650 TmpOp0 = Op0;
3651 TmpOp1 = Op1;
3652 Opc = ISD::OR;
3653 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3654 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3655 break;
3656 case ISD::SETUO: Invert = true; // Fallthrough
3657 case ISD::SETO:
3658 // Expand this to (OLT | OGE).
3659 TmpOp0 = Op0;
3660 TmpOp1 = Op1;
3661 Opc = ISD::OR;
3662 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3663 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3664 break;
3665 }
3666 } else {
3667 // Integer comparisons.
3668 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003669 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003670 case ISD::SETNE: Invert = true;
3671 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3672 case ISD::SETLT: Swap = true;
3673 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3674 case ISD::SETLE: Swap = true;
3675 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3676 case ISD::SETULT: Swap = true;
3677 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3678 case ISD::SETULE: Swap = true;
3679 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3680 }
3681
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003682 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003683 if (Opc == ARMISD::VCEQ) {
3684
3685 SDValue AndOp;
3686 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3687 AndOp = Op0;
3688 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3689 AndOp = Op1;
3690
3691 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003692 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003693 AndOp = AndOp.getOperand(0);
3694
3695 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3696 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003697 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3698 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003699 Invert = !Invert;
3700 }
3701 }
3702 }
3703
3704 if (Swap)
3705 std::swap(Op0, Op1);
3706
Owen Andersonc24cb352010-11-08 23:21:22 +00003707 // If one of the operands is a constant vector zero, attempt to fold the
3708 // comparison to a specialized compare-against-zero form.
3709 SDValue SingleOp;
3710 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3711 SingleOp = Op0;
3712 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3713 if (Opc == ARMISD::VCGE)
3714 Opc = ARMISD::VCLEZ;
3715 else if (Opc == ARMISD::VCGT)
3716 Opc = ARMISD::VCLTZ;
3717 SingleOp = Op1;
3718 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003719
Owen Andersonc24cb352010-11-08 23:21:22 +00003720 SDValue Result;
3721 if (SingleOp.getNode()) {
3722 switch (Opc) {
3723 case ARMISD::VCEQ:
3724 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3725 case ARMISD::VCGE:
3726 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3727 case ARMISD::VCLEZ:
3728 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3729 case ARMISD::VCGT:
3730 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3731 case ARMISD::VCLTZ:
3732 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3733 default:
3734 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3735 }
3736 } else {
3737 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3738 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003739
3740 if (Invert)
3741 Result = DAG.getNOT(dl, Result, VT);
3742
3743 return Result;
3744}
3745
Bob Wilsond3c42842010-06-14 22:19:57 +00003746/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3747/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003748/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003749static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3750 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003751 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003752 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003753
Bob Wilson827b2102010-06-15 19:05:35 +00003754 // SplatBitSize is set to the smallest size that splats the vector, so a
3755 // zero vector will always have SplatBitSize == 8. However, NEON modified
3756 // immediate instructions others than VMOV do not support the 8-bit encoding
3757 // of a zero vector, and the default encoding of zero is supposed to be the
3758 // 32-bit version.
3759 if (SplatBits == 0)
3760 SplatBitSize = 32;
3761
Bob Wilson5bafff32009-06-22 23:27:02 +00003762 switch (SplatBitSize) {
3763 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003764 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003765 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003766 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003767 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003768 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003769 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003770 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003771 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003772
3773 case 16:
3774 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003775 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003776 if ((SplatBits & ~0xff) == 0) {
3777 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003778 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003779 Imm = SplatBits;
3780 break;
3781 }
3782 if ((SplatBits & ~0xff00) == 0) {
3783 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003784 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003785 Imm = SplatBits >> 8;
3786 break;
3787 }
3788 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003789
3790 case 32:
3791 // NEON's 32-bit VMOV supports splat values where:
3792 // * only one byte is nonzero, or
3793 // * the least significant byte is 0xff and the second byte is nonzero, or
3794 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003795 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003796 if ((SplatBits & ~0xff) == 0) {
3797 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003798 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003799 Imm = SplatBits;
3800 break;
3801 }
3802 if ((SplatBits & ~0xff00) == 0) {
3803 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003804 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003805 Imm = SplatBits >> 8;
3806 break;
3807 }
3808 if ((SplatBits & ~0xff0000) == 0) {
3809 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003810 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003811 Imm = SplatBits >> 16;
3812 break;
3813 }
3814 if ((SplatBits & ~0xff000000) == 0) {
3815 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003816 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003817 Imm = SplatBits >> 24;
3818 break;
3819 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003820
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003821 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3822 if (type == OtherModImm) return SDValue();
3823
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3826 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003827 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003828 Imm = SplatBits >> 8;
3829 SplatBits |= 0xff;
3830 break;
3831 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003832
3833 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003834 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3835 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003836 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003837 Imm = SplatBits >> 16;
3838 SplatBits |= 0xffff;
3839 break;
3840 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003841
3842 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3843 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3844 // VMOV.I32. A (very) minor optimization would be to replicate the value
3845 // and fall through here to test for a valid 64-bit splat. But, then the
3846 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003847 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003848
3849 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003850 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003851 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003852 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003853 uint64_t BitMask = 0xff;
3854 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003855 unsigned ImmMask = 1;
3856 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003857 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003858 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003859 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003860 Imm |= ImmMask;
3861 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003862 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003863 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003864 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003865 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003866 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003867 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003868 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003869 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003870 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003871 break;
3872 }
3873
Bob Wilson1a913ed2010-06-11 21:34:50 +00003874 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003875 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003876 }
3877
Bob Wilsoncba270d2010-07-13 21:16:48 +00003878 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3879 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003880}
3881
Lang Hamesc0a9f822012-03-29 21:56:11 +00003882SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3883 const ARMSubtarget *ST) const {
3884 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3885 return SDValue();
3886
3887 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3888 assert(Op.getValueType() == MVT::f32 &&
3889 "ConstantFP custom lowering should only occur for f32.");
3890
3891 // Try splatting with a VMOV.f32...
3892 APFloat FPVal = CFP->getValueAPF();
3893 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3894 if (ImmVal != -1) {
3895 DebugLoc DL = Op.getDebugLoc();
3896 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3897 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3898 NewVal);
3899 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3900 DAG.getConstant(0, MVT::i32));
3901 }
3902
3903 // If that fails, try a VMOV.i32
3904 EVT VMovVT;
3905 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3906 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3907 VMOVModImm);
3908 if (NewVal != SDValue()) {
3909 DebugLoc DL = Op.getDebugLoc();
3910 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3911 NewVal);
3912 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3913 VecConstant);
3914 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3915 DAG.getConstant(0, MVT::i32));
3916 }
3917
3918 // Finally, try a VMVN.i32
3919 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3920 VMVNModImm);
3921 if (NewVal != SDValue()) {
3922 DebugLoc DL = Op.getDebugLoc();
3923 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3924 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3925 VecConstant);
3926 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3927 DAG.getConstant(0, MVT::i32));
3928 }
3929
3930 return SDValue();
3931}
3932
Quentin Colombet43934ae2012-11-02 21:32:17 +00003933// check if an VEXT instruction can handle the shuffle mask when the
3934// vector sources of the shuffle are the same.
3935static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
3936 unsigned NumElts = VT.getVectorNumElements();
3937
3938 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3939 if (M[0] < 0)
3940 return false;
3941
3942 Imm = M[0];
3943
3944 // If this is a VEXT shuffle, the immediate value is the index of the first
3945 // element. The other shuffle indices must be the successive elements after
3946 // the first one.
3947 unsigned ExpectedElt = Imm;
3948 for (unsigned i = 1; i < NumElts; ++i) {
3949 // Increment the expected index. If it wraps around, just follow it
3950 // back to index zero and keep going.
3951 ++ExpectedElt;
3952 if (ExpectedElt == NumElts)
3953 ExpectedElt = 0;
3954
3955 if (M[i] < 0) continue; // ignore UNDEF indices
3956 if (ExpectedElt != static_cast<unsigned>(M[i]))
3957 return false;
3958 }
3959
3960 return true;
3961}
3962
Lang Hamesc0a9f822012-03-29 21:56:11 +00003963
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003964static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003965 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003966 unsigned NumElts = VT.getVectorNumElements();
3967 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003968
3969 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3970 if (M[0] < 0)
3971 return false;
3972
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003973 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003974
3975 // If this is a VEXT shuffle, the immediate value is the index of the first
3976 // element. The other shuffle indices must be the successive elements after
3977 // the first one.
3978 unsigned ExpectedElt = Imm;
3979 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003980 // Increment the expected index. If it wraps around, it may still be
3981 // a VEXT but the source vectors must be swapped.
3982 ExpectedElt += 1;
3983 if (ExpectedElt == NumElts * 2) {
3984 ExpectedElt = 0;
3985 ReverseVEXT = true;
3986 }
3987
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003988 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003989 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003990 return false;
3991 }
3992
3993 // Adjust the index value if the source operands will be swapped.
3994 if (ReverseVEXT)
3995 Imm -= NumElts;
3996
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003997 return true;
3998}
3999
Bob Wilson8bb9e482009-07-26 00:39:34 +00004000/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4001/// instruction with the specified blocksize. (The order of the elements
4002/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004003static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00004004 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4005 "Only possible block sizes for VREV are: 16, 32, 64");
4006
Bob Wilson8bb9e482009-07-26 00:39:34 +00004007 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00004008 if (EltSz == 64)
4009 return false;
4010
4011 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004012 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004013 // If the first shuffle index is UNDEF, be optimistic.
4014 if (M[0] < 0)
4015 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00004016
4017 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4018 return false;
4019
4020 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004021 if (M[i] < 0) continue; // ignore UNDEF indices
4022 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00004023 return false;
4024 }
4025
4026 return true;
4027}
4028
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004029static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004030 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4031 // range, then 0 is placed into the resulting vector. So pretty much any mask
4032 // of 8 elements can work here.
4033 return VT == MVT::v8i8 && M.size() == 8;
4034}
4035
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004036static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004037 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4038 if (EltSz == 64)
4039 return false;
4040
Bob Wilsonc692cb72009-08-21 20:54:19 +00004041 unsigned NumElts = VT.getVectorNumElements();
4042 WhichResult = (M[0] == 0 ? 0 : 1);
4043 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004044 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4045 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004046 return false;
4047 }
4048 return true;
4049}
4050
Bob Wilson324f4f12009-12-03 06:40:55 +00004051/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4052/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4053/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004054static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004055 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4056 if (EltSz == 64)
4057 return false;
4058
4059 unsigned NumElts = VT.getVectorNumElements();
4060 WhichResult = (M[0] == 0 ? 0 : 1);
4061 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004062 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4063 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00004064 return false;
4065 }
4066 return true;
4067}
4068
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004069static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004070 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4071 if (EltSz == 64)
4072 return false;
4073
Bob Wilsonc692cb72009-08-21 20:54:19 +00004074 unsigned NumElts = VT.getVectorNumElements();
4075 WhichResult = (M[0] == 0 ? 0 : 1);
4076 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004077 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004078 if ((unsigned) M[i] != 2 * i + WhichResult)
4079 return false;
4080 }
4081
4082 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004083 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004084 return false;
4085
4086 return true;
4087}
4088
Bob Wilson324f4f12009-12-03 06:40:55 +00004089/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4090/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4091/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004092static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004093 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4094 if (EltSz == 64)
4095 return false;
4096
4097 unsigned Half = VT.getVectorNumElements() / 2;
4098 WhichResult = (M[0] == 0 ? 0 : 1);
4099 for (unsigned j = 0; j != 2; ++j) {
4100 unsigned Idx = WhichResult;
4101 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004102 int MIdx = M[i + j * Half];
4103 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004104 return false;
4105 Idx += 2;
4106 }
4107 }
4108
4109 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4110 if (VT.is64BitVector() && EltSz == 32)
4111 return false;
4112
4113 return true;
4114}
4115
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004116static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004117 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4118 if (EltSz == 64)
4119 return false;
4120
Bob Wilsonc692cb72009-08-21 20:54:19 +00004121 unsigned NumElts = VT.getVectorNumElements();
4122 WhichResult = (M[0] == 0 ? 0 : 1);
4123 unsigned Idx = WhichResult * NumElts / 2;
4124 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004125 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4126 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004127 return false;
4128 Idx += 1;
4129 }
4130
4131 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004132 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004133 return false;
4134
4135 return true;
4136}
4137
Bob Wilson324f4f12009-12-03 06:40:55 +00004138/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4139/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4140/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004141static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004142 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4143 if (EltSz == 64)
4144 return false;
4145
4146 unsigned NumElts = VT.getVectorNumElements();
4147 WhichResult = (M[0] == 0 ? 0 : 1);
4148 unsigned Idx = WhichResult * NumElts / 2;
4149 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004150 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4151 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004152 return false;
4153 Idx += 1;
4154 }
4155
4156 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4157 if (VT.is64BitVector() && EltSz == 32)
4158 return false;
4159
4160 return true;
4161}
4162
Dale Johannesenf630c712010-07-29 20:10:08 +00004163// If N is an integer constant that can be moved into a register in one
4164// instruction, return an SDValue of such a constant (will become a MOV
4165// instruction). Otherwise return null.
4166static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4167 const ARMSubtarget *ST, DebugLoc dl) {
4168 uint64_t Val;
4169 if (!isa<ConstantSDNode>(N))
4170 return SDValue();
4171 Val = cast<ConstantSDNode>(N)->getZExtValue();
4172
4173 if (ST->isThumb1Only()) {
4174 if (Val <= 255 || ~Val <= 255)
4175 return DAG.getConstant(Val, MVT::i32);
4176 } else {
4177 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4178 return DAG.getConstant(Val, MVT::i32);
4179 }
4180 return SDValue();
4181}
4182
Bob Wilson5bafff32009-06-22 23:27:02 +00004183// If this is a case we can't handle, return null and let the default
4184// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004185SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4186 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004187 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004188 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004189 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004190
4191 APInt SplatBits, SplatUndef;
4192 unsigned SplatBitSize;
4193 bool HasAnyUndefs;
4194 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004195 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004196 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004197 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004198 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004199 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004200 DAG, VmovVT, VT.is128BitVector(),
4201 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004202 if (Val.getNode()) {
4203 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004204 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004205 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004206
4207 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004208 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004209 Val = isNEONModifiedImm(NegatedImm,
4210 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004211 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004212 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004213 if (Val.getNode()) {
4214 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004215 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004216 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004217
4218 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004219 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004220 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004221 if (ImmVal != -1) {
4222 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4223 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4224 }
4225 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004226 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004227 }
4228
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004229 // Scan through the operands to see if only one value is used.
James Molloyba8562a2012-09-06 09:55:02 +00004230 //
4231 // As an optimisation, even if more than one value is used it may be more
4232 // profitable to splat with one value then change some lanes.
4233 //
4234 // Heuristically we decide to do this if the vector has a "dominant" value,
4235 // defined as splatted to more than half of the lanes.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004236 unsigned NumElts = VT.getVectorNumElements();
4237 bool isOnlyLowElement = true;
4238 bool usesOnlyOneValue = true;
James Molloyba8562a2012-09-06 09:55:02 +00004239 bool hasDominantValue = false;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004240 bool isConstant = true;
James Molloyba8562a2012-09-06 09:55:02 +00004241
4242 // Map of the number of times a particular SDValue appears in the
4243 // element list.
James Molloy95154342012-09-06 10:32:08 +00004244 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004245 SDValue Value;
4246 for (unsigned i = 0; i < NumElts; ++i) {
4247 SDValue V = Op.getOperand(i);
4248 if (V.getOpcode() == ISD::UNDEF)
4249 continue;
4250 if (i > 0)
4251 isOnlyLowElement = false;
4252 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4253 isConstant = false;
4254
James Molloyba8562a2012-09-06 09:55:02 +00004255 ValueCounts.insert(std::make_pair(V, 0));
James Molloy95154342012-09-06 10:32:08 +00004256 unsigned &Count = ValueCounts[V];
James Molloyba8562a2012-09-06 09:55:02 +00004257
4258 // Is this value dominant? (takes up more than half of the lanes)
4259 if (++Count > (NumElts / 2)) {
4260 hasDominantValue = true;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004261 Value = V;
James Molloyba8562a2012-09-06 09:55:02 +00004262 }
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004263 }
James Molloyba8562a2012-09-06 09:55:02 +00004264 if (ValueCounts.size() != 1)
4265 usesOnlyOneValue = false;
4266 if (!Value.getNode() && ValueCounts.size() > 0)
4267 Value = ValueCounts.begin()->first;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004268
James Molloyba8562a2012-09-06 09:55:02 +00004269 if (ValueCounts.size() == 0)
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004270 return DAG.getUNDEF(VT);
4271
4272 if (isOnlyLowElement)
4273 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4274
Dale Johannesenf630c712010-07-29 20:10:08 +00004275 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4276
Dale Johannesen575cd142010-10-19 20:00:17 +00004277 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4278 // i32 and try again.
James Molloyba8562a2012-09-06 09:55:02 +00004279 if (hasDominantValue && EltSize <= 32) {
4280 if (!isConstant) {
4281 SDValue N;
4282
4283 // If we are VDUPing a value that comes directly from a vector, that will
4284 // cause an unnecessary move to and from a GPR, where instead we could
4285 // just use VDUPLANE.
Silviu Barangabb1078e2012-10-15 09:41:32 +00004286 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
4287 // We need to create a new undef vector to use for the VDUPLANE if the
4288 // size of the vector from which we get the value is different than the
4289 // size of the vector that we need to create. We will insert the element
4290 // such that the register coalescer will remove unnecessary copies.
4291 if (VT != Value->getOperand(0).getValueType()) {
4292 ConstantSDNode *constIndex;
4293 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4294 assert(constIndex && "The index is not a constant!");
4295 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4296 VT.getVectorNumElements();
4297 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4298 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4299 Value, DAG.getConstant(index, MVT::i32)),
4300 DAG.getConstant(index, MVT::i32));
4301 } else {
4302 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloyba8562a2012-09-06 09:55:02 +00004303 Value->getOperand(0), Value->getOperand(1));
Silviu Barangabb1078e2012-10-15 09:41:32 +00004304 }
4305 }
James Molloyba8562a2012-09-06 09:55:02 +00004306 else
4307 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4308
4309 if (!usesOnlyOneValue) {
4310 // The dominant value was splatted as 'N', but we now have to insert
4311 // all differing elements.
4312 for (unsigned I = 0; I < NumElts; ++I) {
4313 if (Op.getOperand(I) == Value)
4314 continue;
4315 SmallVector<SDValue, 3> Ops;
4316 Ops.push_back(N);
4317 Ops.push_back(Op.getOperand(I));
4318 Ops.push_back(DAG.getConstant(I, MVT::i32));
4319 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4320 }
4321 }
4322 return N;
4323 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004324 if (VT.getVectorElementType().isFloatingPoint()) {
4325 SmallVector<SDValue, 8> Ops;
4326 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004327 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004328 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004329 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4330 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004331 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4332 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004333 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004334 }
James Molloyba8562a2012-09-06 09:55:02 +00004335 if (usesOnlyOneValue) {
4336 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4337 if (isConstant && Val.getNode())
4338 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4339 }
Dale Johannesenf630c712010-07-29 20:10:08 +00004340 }
4341
4342 // If all elements are constants and the case above didn't get hit, fall back
4343 // to the default expansion, which will generate a load from the constant
4344 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004345 if (isConstant)
4346 return SDValue();
4347
Bob Wilson11a1dff2011-01-07 21:37:30 +00004348 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4349 if (NumElts >= 4) {
4350 SDValue shuffle = ReconstructShuffle(Op, DAG);
4351 if (shuffle != SDValue())
4352 return shuffle;
4353 }
4354
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004355 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004356 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4357 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004358 if (EltSize >= 32) {
4359 // Do the expansion with floating-point types, since that is what the VFP
4360 // registers are defined to use, and since i64 is not legal.
4361 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4362 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004363 SmallVector<SDValue, 8> Ops;
4364 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004365 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004366 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004367 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004368 }
4369
4370 return SDValue();
4371}
4372
Bob Wilson11a1dff2011-01-07 21:37:30 +00004373// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004374// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004375SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4376 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004377 DebugLoc dl = Op.getDebugLoc();
4378 EVT VT = Op.getValueType();
4379 unsigned NumElts = VT.getVectorNumElements();
4380
4381 SmallVector<SDValue, 2> SourceVecs;
4382 SmallVector<unsigned, 2> MinElts;
4383 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004384
Bob Wilson11a1dff2011-01-07 21:37:30 +00004385 for (unsigned i = 0; i < NumElts; ++i) {
4386 SDValue V = Op.getOperand(i);
4387 if (V.getOpcode() == ISD::UNDEF)
4388 continue;
4389 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4390 // A shuffle can only come from building a vector from various
4391 // elements of other vectors.
4392 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004393 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4394 VT.getVectorElementType()) {
4395 // This code doesn't know how to handle shuffles where the vector
4396 // element types do not match (this happens because type legalization
4397 // promotes the return type of EXTRACT_VECTOR_ELT).
4398 // FIXME: It might be appropriate to extend this code to handle
4399 // mismatched types.
4400 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004401 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004402
Bob Wilson11a1dff2011-01-07 21:37:30 +00004403 // Record this extraction against the appropriate vector if possible...
4404 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004405 // If the element number isn't a constant, we can't effectively
4406 // analyze what's going on.
4407 if (!isa<ConstantSDNode>(V.getOperand(1)))
4408 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004409 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4410 bool FoundSource = false;
4411 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4412 if (SourceVecs[j] == SourceVec) {
4413 if (MinElts[j] > EltNo)
4414 MinElts[j] = EltNo;
4415 if (MaxElts[j] < EltNo)
4416 MaxElts[j] = EltNo;
4417 FoundSource = true;
4418 break;
4419 }
4420 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004421
Bob Wilson11a1dff2011-01-07 21:37:30 +00004422 // Or record a new source if not...
4423 if (!FoundSource) {
4424 SourceVecs.push_back(SourceVec);
4425 MinElts.push_back(EltNo);
4426 MaxElts.push_back(EltNo);
4427 }
4428 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004429
Bob Wilson11a1dff2011-01-07 21:37:30 +00004430 // Currently only do something sane when at most two source vectors
4431 // involved.
4432 if (SourceVecs.size() > 2)
4433 return SDValue();
4434
4435 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4436 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004437
Bob Wilson11a1dff2011-01-07 21:37:30 +00004438 // This loop extracts the usage patterns of the source vectors
4439 // and prepares appropriate SDValues for a shuffle if possible.
4440 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4441 if (SourceVecs[i].getValueType() == VT) {
4442 // No VEXT necessary
4443 ShuffleSrcs[i] = SourceVecs[i];
4444 VEXTOffsets[i] = 0;
4445 continue;
4446 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4447 // It probably isn't worth padding out a smaller vector just to
4448 // break it down again in a shuffle.
4449 return SDValue();
4450 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004451
Bob Wilson11a1dff2011-01-07 21:37:30 +00004452 // Since only 64-bit and 128-bit vectors are legal on ARM and
4453 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004454 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4455 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004456
Bob Wilson11a1dff2011-01-07 21:37:30 +00004457 if (MaxElts[i] - MinElts[i] >= NumElts) {
4458 // Span too large for a VEXT to cope
4459 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004460 }
4461
Bob Wilson11a1dff2011-01-07 21:37:30 +00004462 if (MinElts[i] >= NumElts) {
4463 // The extraction can just take the second half
4464 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004465 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4466 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004467 DAG.getIntPtrConstant(NumElts));
4468 } else if (MaxElts[i] < NumElts) {
4469 // The extraction can just take the first half
4470 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004471 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4472 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004473 DAG.getIntPtrConstant(0));
4474 } else {
4475 // An actual VEXT is needed
4476 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004477 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4478 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004479 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004480 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4481 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004482 DAG.getIntPtrConstant(NumElts));
4483 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4484 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4485 }
4486 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004487
Bob Wilson11a1dff2011-01-07 21:37:30 +00004488 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004489
Bob Wilson11a1dff2011-01-07 21:37:30 +00004490 for (unsigned i = 0; i < NumElts; ++i) {
4491 SDValue Entry = Op.getOperand(i);
4492 if (Entry.getOpcode() == ISD::UNDEF) {
4493 Mask.push_back(-1);
4494 continue;
4495 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004496
Bob Wilson11a1dff2011-01-07 21:37:30 +00004497 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004498 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4499 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004500 if (ExtractVec == SourceVecs[0]) {
4501 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4502 } else {
4503 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4504 }
4505 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004506
Bob Wilson11a1dff2011-01-07 21:37:30 +00004507 // Final check before we try to produce nonsense...
4508 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004509 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4510 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004511
Bob Wilson11a1dff2011-01-07 21:37:30 +00004512 return SDValue();
4513}
4514
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004515/// isShuffleMaskLegal - Targets can use this to indicate that they only
4516/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4517/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4518/// are assumed to be legal.
4519bool
4520ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4521 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004522 if (VT.getVectorNumElements() == 4 &&
4523 (VT.is128BitVector() || VT.is64BitVector())) {
4524 unsigned PFIndexes[4];
4525 for (unsigned i = 0; i != 4; ++i) {
4526 if (M[i] < 0)
4527 PFIndexes[i] = 8;
4528 else
4529 PFIndexes[i] = M[i];
4530 }
4531
4532 // Compute the index in the perfect shuffle table.
4533 unsigned PFTableIndex =
4534 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4535 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4536 unsigned Cost = (PFEntry >> 30);
4537
4538 if (Cost <= 4)
4539 return true;
4540 }
4541
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004542 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004543 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004544
Bob Wilson53dd2452010-06-07 23:53:38 +00004545 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4546 return (EltSize >= 32 ||
4547 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004548 isVREVMask(M, VT, 64) ||
4549 isVREVMask(M, VT, 32) ||
4550 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004551 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004552 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004553 isVTRNMask(M, VT, WhichResult) ||
4554 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004555 isVZIPMask(M, VT, WhichResult) ||
4556 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4557 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4558 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004559}
4560
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004561/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4562/// the specified operations to build the shuffle.
4563static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4564 SDValue RHS, SelectionDAG &DAG,
4565 DebugLoc dl) {
4566 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4567 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4568 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4569
4570 enum {
4571 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4572 OP_VREV,
4573 OP_VDUP0,
4574 OP_VDUP1,
4575 OP_VDUP2,
4576 OP_VDUP3,
4577 OP_VEXT1,
4578 OP_VEXT2,
4579 OP_VEXT3,
4580 OP_VUZPL, // VUZP, left result
4581 OP_VUZPR, // VUZP, right result
4582 OP_VZIPL, // VZIP, left result
4583 OP_VZIPR, // VZIP, right result
4584 OP_VTRNL, // VTRN, left result
4585 OP_VTRNR // VTRN, right result
4586 };
4587
4588 if (OpNum == OP_COPY) {
4589 if (LHSID == (1*9+2)*9+3) return LHS;
4590 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4591 return RHS;
4592 }
4593
4594 SDValue OpLHS, OpRHS;
4595 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4596 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4597 EVT VT = OpLHS.getValueType();
4598
4599 switch (OpNum) {
4600 default: llvm_unreachable("Unknown shuffle opcode!");
4601 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004602 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004603 if (VT.getVectorElementType() == MVT::i32 ||
4604 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004605 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4606 // vrev <4 x i16> -> VREV32
4607 if (VT.getVectorElementType() == MVT::i16)
4608 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4609 // vrev <4 x i8> -> VREV16
4610 assert(VT.getVectorElementType() == MVT::i8);
4611 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004612 case OP_VDUP0:
4613 case OP_VDUP1:
4614 case OP_VDUP2:
4615 case OP_VDUP3:
4616 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004617 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004618 case OP_VEXT1:
4619 case OP_VEXT2:
4620 case OP_VEXT3:
4621 return DAG.getNode(ARMISD::VEXT, dl, VT,
4622 OpLHS, OpRHS,
4623 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4624 case OP_VUZPL:
4625 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004626 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004627 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4628 case OP_VZIPL:
4629 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004630 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004631 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4632 case OP_VTRNL:
4633 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004634 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4635 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004636 }
4637}
4638
Bill Wendling69a05a72011-03-14 23:02:38 +00004639static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004640 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004641 SelectionDAG &DAG) {
4642 // Check to see if we can use the VTBL instruction.
4643 SDValue V1 = Op.getOperand(0);
4644 SDValue V2 = Op.getOperand(1);
4645 DebugLoc DL = Op.getDebugLoc();
4646
4647 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004648 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004649 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4650 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4651
4652 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4653 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4654 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4655 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004656
Owen Anderson76706012011-04-05 21:48:57 +00004657 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004658 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4659 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004660}
4661
Bob Wilson5bafff32009-06-22 23:27:02 +00004662static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004663 SDValue V1 = Op.getOperand(0);
4664 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004665 DebugLoc dl = Op.getDebugLoc();
4666 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004667 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004668
Bob Wilson28865062009-08-13 02:13:04 +00004669 // Convert shuffles that are directly supported on NEON to target-specific
4670 // DAG nodes, instead of keeping them as shuffles and matching them again
4671 // during code selection. This is more efficient and avoids the possibility
4672 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004673 // FIXME: floating-point vectors should be canonicalized to integer vectors
4674 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004675 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004676
Bob Wilson53dd2452010-06-07 23:53:38 +00004677 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4678 if (EltSize <= 32) {
4679 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4680 int Lane = SVN->getSplatIndex();
4681 // If this is undef splat, generate it via "just" vdup, if possible.
4682 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004683
Dan Gohman65fd6562011-11-03 21:49:52 +00004684 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004685 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4686 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4687 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004688 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4689 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4690 // reaches it).
4691 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4692 !isa<ConstantSDNode>(V1.getOperand(0))) {
4693 bool IsScalarToVector = true;
4694 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4695 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4696 IsScalarToVector = false;
4697 break;
4698 }
4699 if (IsScalarToVector)
4700 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4701 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004702 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4703 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004704 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004705
4706 bool ReverseVEXT;
4707 unsigned Imm;
4708 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4709 if (ReverseVEXT)
4710 std::swap(V1, V2);
4711 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4712 DAG.getConstant(Imm, MVT::i32));
4713 }
4714
4715 if (isVREVMask(ShuffleMask, VT, 64))
4716 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4717 if (isVREVMask(ShuffleMask, VT, 32))
4718 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4719 if (isVREVMask(ShuffleMask, VT, 16))
4720 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4721
Quentin Colombet43934ae2012-11-02 21:32:17 +00004722 if (V2->getOpcode() == ISD::UNDEF &&
4723 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
4724 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
4725 DAG.getConstant(Imm, MVT::i32));
4726 }
4727
Bob Wilson53dd2452010-06-07 23:53:38 +00004728 // Check for Neon shuffles that modify both input vectors in place.
4729 // If both results are used, i.e., if there are two shuffles with the same
4730 // source operands and with masks corresponding to both results of one of
4731 // these operations, DAG memoization will ensure that a single node is
4732 // used for both shuffles.
4733 unsigned WhichResult;
4734 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4735 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4736 V1, V2).getValue(WhichResult);
4737 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4738 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4739 V1, V2).getValue(WhichResult);
4740 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4741 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4742 V1, V2).getValue(WhichResult);
4743
4744 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4745 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4746 V1, V1).getValue(WhichResult);
4747 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4748 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4749 V1, V1).getValue(WhichResult);
4750 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4751 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4752 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004753 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004754
Bob Wilsonc692cb72009-08-21 20:54:19 +00004755 // If the shuffle is not directly supported and it has 4 elements, use
4756 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004757 unsigned NumElts = VT.getVectorNumElements();
4758 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004759 unsigned PFIndexes[4];
4760 for (unsigned i = 0; i != 4; ++i) {
4761 if (ShuffleMask[i] < 0)
4762 PFIndexes[i] = 8;
4763 else
4764 PFIndexes[i] = ShuffleMask[i];
4765 }
4766
4767 // Compute the index in the perfect shuffle table.
4768 unsigned PFTableIndex =
4769 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004770 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4771 unsigned Cost = (PFEntry >> 30);
4772
4773 if (Cost <= 4)
4774 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4775 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004776
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004777 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004778 if (EltSize >= 32) {
4779 // Do the expansion with floating-point types, since that is what the VFP
4780 // registers are defined to use, and since i64 is not legal.
4781 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4782 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004783 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4784 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004785 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004786 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004787 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004788 Ops.push_back(DAG.getUNDEF(EltVT));
4789 else
4790 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4791 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4792 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4793 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004794 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004795 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004796 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004797 }
4798
Bill Wendling69a05a72011-03-14 23:02:38 +00004799 if (VT == MVT::v8i8) {
4800 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4801 if (NewOp.getNode())
4802 return NewOp;
4803 }
4804
Bob Wilson22cac0d2009-08-14 05:16:33 +00004805 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004806}
4807
Eli Friedman5c89cb82011-10-24 23:08:52 +00004808static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4809 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4810 SDValue Lane = Op.getOperand(2);
4811 if (!isa<ConstantSDNode>(Lane))
4812 return SDValue();
4813
4814 return Op;
4815}
4816
Bob Wilson5bafff32009-06-22 23:27:02 +00004817static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004818 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004819 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004820 if (!isa<ConstantSDNode>(Lane))
4821 return SDValue();
4822
4823 SDValue Vec = Op.getOperand(0);
4824 if (Op.getValueType() == MVT::i32 &&
4825 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4826 DebugLoc dl = Op.getDebugLoc();
4827 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4828 }
4829
4830 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004831}
4832
Bob Wilsona6d65862009-08-03 20:36:38 +00004833static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4834 // The only time a CONCAT_VECTORS operation can have legal types is when
4835 // two 64-bit vectors are concatenated to a 128-bit vector.
4836 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4837 "unexpected CONCAT_VECTORS");
4838 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004839 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004840 SDValue Op0 = Op.getOperand(0);
4841 SDValue Op1 = Op.getOperand(1);
4842 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004843 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004844 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004845 DAG.getIntPtrConstant(0));
4846 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004848 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004849 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004850 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004851}
4852
Bob Wilson626613d2010-11-23 19:38:38 +00004853/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4854/// element has been zero/sign-extended, depending on the isSigned parameter,
4855/// from an integer type half its size.
4856static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4857 bool isSigned) {
4858 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4859 EVT VT = N->getValueType(0);
4860 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4861 SDNode *BVN = N->getOperand(0).getNode();
4862 if (BVN->getValueType(0) != MVT::v4i32 ||
4863 BVN->getOpcode() != ISD::BUILD_VECTOR)
4864 return false;
4865 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4866 unsigned HiElt = 1 - LoElt;
4867 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4868 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4869 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4870 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4871 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4872 return false;
4873 if (isSigned) {
4874 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4875 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4876 return true;
4877 } else {
4878 if (Hi0->isNullValue() && Hi1->isNullValue())
4879 return true;
4880 }
4881 return false;
4882 }
4883
4884 if (N->getOpcode() != ISD::BUILD_VECTOR)
4885 return false;
4886
4887 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4888 SDNode *Elt = N->getOperand(i).getNode();
4889 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4890 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4891 unsigned HalfSize = EltSize / 2;
4892 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004893 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004894 return false;
4895 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004896 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004897 return false;
4898 }
4899 continue;
4900 }
4901 return false;
4902 }
4903
4904 return true;
4905}
4906
4907/// isSignExtended - Check if a node is a vector value that is sign-extended
4908/// or a constant BUILD_VECTOR with sign-extended elements.
4909static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4910 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4911 return true;
4912 if (isExtendedBUILD_VECTOR(N, DAG, true))
4913 return true;
4914 return false;
4915}
4916
4917/// isZeroExtended - Check if a node is a vector value that is zero-extended
4918/// or a constant BUILD_VECTOR with zero-extended elements.
4919static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4920 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4921 return true;
4922 if (isExtendedBUILD_VECTOR(N, DAG, false))
4923 return true;
4924 return false;
4925}
4926
4927/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4928/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004929static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4930 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4931 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004932 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4933 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4934 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004935 LD->isNonTemporal(), LD->isInvariant(),
4936 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004937 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4938 // have been legalized as a BITCAST from v4i32.
4939 if (N->getOpcode() == ISD::BITCAST) {
4940 SDNode *BVN = N->getOperand(0).getNode();
4941 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4942 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4943 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4944 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4945 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4946 }
4947 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4948 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4949 EVT VT = N->getValueType(0);
4950 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4951 unsigned NumElts = VT.getVectorNumElements();
4952 MVT TruncVT = MVT::getIntegerVT(EltSize);
4953 SmallVector<SDValue, 8> Ops;
4954 for (unsigned i = 0; i != NumElts; ++i) {
4955 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4956 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004957 // Element types smaller than 32 bits are not legal, so use i32 elements.
4958 // The values are implicitly truncated so sext vs. zext doesn't matter.
4959 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004960 }
4961 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4962 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004963}
4964
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004965static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4966 unsigned Opcode = N->getOpcode();
4967 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4968 SDNode *N0 = N->getOperand(0).getNode();
4969 SDNode *N1 = N->getOperand(1).getNode();
4970 return N0->hasOneUse() && N1->hasOneUse() &&
4971 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4972 }
4973 return false;
4974}
4975
4976static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4977 unsigned Opcode = N->getOpcode();
4978 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4979 SDNode *N0 = N->getOperand(0).getNode();
4980 SDNode *N1 = N->getOperand(1).getNode();
4981 return N0->hasOneUse() && N1->hasOneUse() &&
4982 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4983 }
4984 return false;
4985}
4986
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004987static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4988 // Multiplications are only custom-lowered for 128-bit vectors so that
4989 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4990 EVT VT = Op.getValueType();
4991 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4992 SDNode *N0 = Op.getOperand(0).getNode();
4993 SDNode *N1 = Op.getOperand(1).getNode();
4994 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004995 bool isMLA = false;
4996 bool isN0SExt = isSignExtended(N0, DAG);
4997 bool isN1SExt = isSignExtended(N1, DAG);
4998 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004999 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005000 else {
5001 bool isN0ZExt = isZeroExtended(N0, DAG);
5002 bool isN1ZExt = isZeroExtended(N1, DAG);
5003 if (isN0ZExt && isN1ZExt)
5004 NewOpc = ARMISD::VMULLu;
5005 else if (isN1SExt || isN1ZExt) {
5006 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5007 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5008 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5009 NewOpc = ARMISD::VMULLs;
5010 isMLA = true;
5011 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5012 NewOpc = ARMISD::VMULLu;
5013 isMLA = true;
5014 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5015 std::swap(N0, N1);
5016 NewOpc = ARMISD::VMULLu;
5017 isMLA = true;
5018 }
5019 }
5020
5021 if (!NewOpc) {
5022 if (VT == MVT::v2i64)
5023 // Fall through to expand this. It is not legal.
5024 return SDValue();
5025 else
5026 // Other vector multiplications are legal.
5027 return Op;
5028 }
5029 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005030
5031 // Legalize to a VMULL instruction.
5032 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005033 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005034 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005035 if (!isMLA) {
5036 Op0 = SkipExtension(N0, DAG);
5037 assert(Op0.getValueType().is64BitVector() &&
5038 Op1.getValueType().is64BitVector() &&
5039 "unexpected types for extended operands to VMULL");
5040 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5041 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005042
Evan Cheng78fe9ab2011-03-29 01:56:09 +00005043 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5044 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5045 // vmull q0, d4, d6
5046 // vmlal q0, d5, d6
5047 // is faster than
5048 // vaddl q0, d4, d5
5049 // vmovl q1, d6
5050 // vmul q0, q0, q1
5051 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
5052 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
5053 EVT Op1VT = Op1.getValueType();
5054 return DAG.getNode(N0->getOpcode(), DL, VT,
5055 DAG.getNode(NewOpc, DL, VT,
5056 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5057 DAG.getNode(NewOpc, DL, VT,
5058 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005059}
5060
Owen Anderson76706012011-04-05 21:48:57 +00005061static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005062LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
5063 // Convert to float
5064 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5065 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5066 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5067 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5068 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5069 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5070 // Get reciprocal estimate.
5071 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00005072 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005073 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5074 // Because char has a smaller range than uchar, we can actually get away
5075 // without any newton steps. This requires that we use a weird bias
5076 // of 0xb000, however (again, this has been exhaustively tested).
5077 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5078 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5079 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5080 Y = DAG.getConstant(0xb000, MVT::i32);
5081 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5082 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5083 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5084 // Convert back to short.
5085 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5086 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5087 return X;
5088}
5089
Owen Anderson76706012011-04-05 21:48:57 +00005090static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00005091LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
5092 SDValue N2;
5093 // Convert to float.
5094 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5095 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5096 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5097 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5098 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5099 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005100
Nate Begeman7973f352011-02-11 20:53:29 +00005101 // Use reciprocal estimate and one refinement step.
5102 // float4 recip = vrecpeq_f32(yf);
5103 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005104 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005105 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00005106 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005107 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5108 N1, N2);
5109 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5110 // Because short has a smaller range than ushort, we can actually get away
5111 // with only a single newton step. This requires that we use a weird bias
5112 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005113 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00005114 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5115 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005116 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00005117 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5118 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5119 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5120 // Convert back to integer and return.
5121 // return vmovn_s32(vcvt_s32_f32(result));
5122 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5123 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5124 return N0;
5125}
5126
5127static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5128 EVT VT = Op.getValueType();
5129 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5130 "unexpected type for custom-lowering ISD::SDIV");
5131
5132 DebugLoc dl = Op.getDebugLoc();
5133 SDValue N0 = Op.getOperand(0);
5134 SDValue N1 = Op.getOperand(1);
5135 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005136
Nate Begeman7973f352011-02-11 20:53:29 +00005137 if (VT == MVT::v8i8) {
5138 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5139 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005140
Nate Begeman7973f352011-02-11 20:53:29 +00005141 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5142 DAG.getIntPtrConstant(4));
5143 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005144 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005145 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5146 DAG.getIntPtrConstant(0));
5147 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5148 DAG.getIntPtrConstant(0));
5149
5150 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5151 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5152
5153 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5154 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005155
Nate Begeman7973f352011-02-11 20:53:29 +00005156 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5157 return N0;
5158 }
5159 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5160}
5161
5162static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5163 EVT VT = Op.getValueType();
5164 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5165 "unexpected type for custom-lowering ISD::UDIV");
5166
5167 DebugLoc dl = Op.getDebugLoc();
5168 SDValue N0 = Op.getOperand(0);
5169 SDValue N1 = Op.getOperand(1);
5170 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005171
Nate Begeman7973f352011-02-11 20:53:29 +00005172 if (VT == MVT::v8i8) {
5173 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5174 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005175
Nate Begeman7973f352011-02-11 20:53:29 +00005176 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5177 DAG.getIntPtrConstant(4));
5178 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005179 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005180 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5181 DAG.getIntPtrConstant(0));
5182 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5183 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005184
Nate Begeman7973f352011-02-11 20:53:29 +00005185 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5186 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005187
Nate Begeman7973f352011-02-11 20:53:29 +00005188 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5189 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005190
5191 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005192 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5193 N0);
5194 return N0;
5195 }
Owen Anderson76706012011-04-05 21:48:57 +00005196
Nate Begeman7973f352011-02-11 20:53:29 +00005197 // v4i16 sdiv ... Convert to float.
5198 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5199 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5200 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5201 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5202 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005203 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005204
5205 // Use reciprocal estimate and two refinement steps.
5206 // float4 recip = vrecpeq_f32(yf);
5207 // recip *= vrecpsq_f32(yf, recip);
5208 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005209 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005210 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005211 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005212 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005213 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005214 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005215 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005216 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005217 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005218 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5219 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5220 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5221 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005222 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005223 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5224 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5225 N1 = DAG.getConstant(2, MVT::i32);
5226 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5227 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5228 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5229 // Convert back to integer and return.
5230 // return vmovn_u32(vcvt_s32_f32(result));
5231 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5232 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5233 return N0;
5234}
5235
Evan Cheng342e3162011-08-30 01:34:54 +00005236static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5237 EVT VT = Op.getNode()->getValueType(0);
5238 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5239
5240 unsigned Opc;
5241 bool ExtraOp = false;
5242 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005243 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005244 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5245 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5246 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5247 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5248 }
5249
5250 if (!ExtraOp)
5251 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5252 Op.getOperand(1));
5253 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5254 Op.getOperand(1), Op.getOperand(2));
5255}
5256
Eli Friedman74bf18c2011-09-15 22:26:18 +00005257static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005258 // Monotonic load/store is legal for all targets
5259 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5260 return Op;
5261
5262 // Aquire/Release load/store is not legal for targets without a
5263 // dmb or equivalent available.
5264 return SDValue();
5265}
5266
5267
Eli Friedman2bdffe42011-08-31 00:31:29 +00005268static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005269ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5270 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005271 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005272 assert (Node->getValueType(0) == MVT::i64 &&
5273 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005274
Eli Friedman4d3f3292011-08-31 17:52:22 +00005275 SmallVector<SDValue, 6> Ops;
5276 Ops.push_back(Node->getOperand(0)); // Chain
5277 Ops.push_back(Node->getOperand(1)); // Ptr
5278 // Low part of Val1
5279 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5280 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5281 // High part of Val1
5282 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5283 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005284 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005285 // High part of Val1
5286 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5287 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5288 // High part of Val2
5289 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5290 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5291 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005292 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5293 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005294 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005295 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005296 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005297 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5298 Results.push_back(Result.getValue(2));
5299}
5300
Dan Gohmand858e902010-04-17 15:26:15 +00005301SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005302 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005303 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005304 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005305 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005306 case ISD::GlobalAddress:
5307 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5308 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005309 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005310 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005311 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5312 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005313 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005314 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005315 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005316 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005317 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005318 case ISD::SINT_TO_FP:
5319 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5320 case ISD::FP_TO_SINT:
5321 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005322 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005323 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005324 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005325 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005326 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005327 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005328 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5329 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005330 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005331 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005332 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005333 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005334 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005335 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005336 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005337 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005338 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005339 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005340 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005341 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005342 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005343 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005344 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005345 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005346 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005347 case ISD::SDIV: return LowerSDIV(Op, DAG);
5348 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005349 case ISD::ADDC:
5350 case ISD::ADDE:
5351 case ISD::SUBC:
5352 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005353 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005354 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005355 }
Evan Chenga8e29892007-01-19 07:51:42 +00005356}
5357
Duncan Sands1607f052008-12-01 11:39:25 +00005358/// ReplaceNodeResults - Replace the results of node with an illegal result
5359/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005360void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5361 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005362 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005363 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005364 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005365 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005366 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005367 case ISD::BITCAST:
5368 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005369 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005370 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005371 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005372 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005373 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005374 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005375 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005376 return;
5377 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005378 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005379 return;
5380 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005381 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005382 return;
5383 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005384 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005385 return;
5386 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005387 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005388 return;
5389 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005390 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005391 return;
5392 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005393 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005394 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005395 case ISD::ATOMIC_CMP_SWAP:
5396 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5397 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005398 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005399 if (Res.getNode())
5400 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005401}
Chris Lattner27a6c732007-11-24 07:07:01 +00005402
Evan Chenga8e29892007-01-19 07:51:42 +00005403//===----------------------------------------------------------------------===//
5404// ARM Scheduler Hooks
5405//===----------------------------------------------------------------------===//
5406
5407MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005408ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5409 MachineBasicBlock *BB,
5410 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005411 unsigned dest = MI->getOperand(0).getReg();
5412 unsigned ptr = MI->getOperand(1).getReg();
5413 unsigned oldval = MI->getOperand(2).getReg();
5414 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005415 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5416 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005417 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005418
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005419 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005420 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5421 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5422 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005423
5424 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005425 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5426 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5427 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005428 }
5429
Jim Grosbach5278eb82009-12-11 01:42:04 +00005430 unsigned ldrOpc, strOpc;
5431 switch (Size) {
5432 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005433 case 1:
5434 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005435 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005436 break;
5437 case 2:
5438 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5439 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5440 break;
5441 case 4:
5442 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5443 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5444 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005445 }
5446
5447 MachineFunction *MF = BB->getParent();
5448 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5449 MachineFunction::iterator It = BB;
5450 ++It; // insert the new blocks after the current block
5451
5452 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5453 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5454 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5455 MF->insert(It, loop1MBB);
5456 MF->insert(It, loop2MBB);
5457 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005458
5459 // Transfer the remainder of BB and its successor edges to exitMBB.
5460 exitMBB->splice(exitMBB->begin(), BB,
5461 llvm::next(MachineBasicBlock::iterator(MI)),
5462 BB->end());
5463 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005464
5465 // thisMBB:
5466 // ...
5467 // fallthrough --> loop1MBB
5468 BB->addSuccessor(loop1MBB);
5469
5470 // loop1MBB:
5471 // ldrex dest, [ptr]
5472 // cmp dest, oldval
5473 // bne exitMBB
5474 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005475 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5476 if (ldrOpc == ARM::t2LDREX)
5477 MIB.addImm(0);
5478 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005479 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005480 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005481 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5482 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005483 BB->addSuccessor(loop2MBB);
5484 BB->addSuccessor(exitMBB);
5485
5486 // loop2MBB:
5487 // strex scratch, newval, [ptr]
5488 // cmp scratch, #0
5489 // bne loop1MBB
5490 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005491 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5492 if (strOpc == ARM::t2STREX)
5493 MIB.addImm(0);
5494 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005495 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005496 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005497 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5498 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005499 BB->addSuccessor(loop1MBB);
5500 BB->addSuccessor(exitMBB);
5501
5502 // exitMBB:
5503 // ...
5504 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005505
Dan Gohman14152b42010-07-06 20:24:04 +00005506 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005507
Jim Grosbach5278eb82009-12-11 01:42:04 +00005508 return BB;
5509}
5510
5511MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005512ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5513 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005514 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5515 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5516
5517 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005518 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005519 MachineFunction::iterator It = BB;
5520 ++It;
5521
5522 unsigned dest = MI->getOperand(0).getReg();
5523 unsigned ptr = MI->getOperand(1).getReg();
5524 unsigned incr = MI->getOperand(2).getReg();
5525 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005526 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005527
5528 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5529 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005530 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5531 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005532 }
5533
Jim Grosbachc3c23542009-12-14 04:22:04 +00005534 unsigned ldrOpc, strOpc;
5535 switch (Size) {
5536 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005537 case 1:
5538 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005539 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005540 break;
5541 case 2:
5542 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5543 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5544 break;
5545 case 4:
5546 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5547 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5548 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005549 }
5550
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005551 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5552 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5553 MF->insert(It, loopMBB);
5554 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005555
5556 // Transfer the remainder of BB and its successor edges to exitMBB.
5557 exitMBB->splice(exitMBB->begin(), BB,
5558 llvm::next(MachineBasicBlock::iterator(MI)),
5559 BB->end());
5560 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005561
Craig Topper420761a2012-04-20 07:30:17 +00005562 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005563 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005564 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005565 unsigned scratch = MRI.createVirtualRegister(TRC);
5566 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005567
5568 // thisMBB:
5569 // ...
5570 // fallthrough --> loopMBB
5571 BB->addSuccessor(loopMBB);
5572
5573 // loopMBB:
5574 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005575 // <binop> scratch2, dest, incr
5576 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005577 // cmp scratch, #0
5578 // bne- loopMBB
5579 // fallthrough --> exitMBB
5580 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005581 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5582 if (ldrOpc == ARM::t2LDREX)
5583 MIB.addImm(0);
5584 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005585 if (BinOpcode) {
5586 // operand order needs to go the other way for NAND
5587 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5588 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5589 addReg(incr).addReg(dest)).addReg(0);
5590 else
5591 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5592 addReg(dest).addReg(incr)).addReg(0);
5593 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005594
Jim Grosbachb6aed502011-09-09 18:37:27 +00005595 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5596 if (strOpc == ARM::t2STREX)
5597 MIB.addImm(0);
5598 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005599 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005600 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005601 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5602 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005603
5604 BB->addSuccessor(loopMBB);
5605 BB->addSuccessor(exitMBB);
5606
5607 // exitMBB:
5608 // ...
5609 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005610
Dan Gohman14152b42010-07-06 20:24:04 +00005611 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005612
Jim Grosbachc3c23542009-12-14 04:22:04 +00005613 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005614}
5615
Jim Grosbachf7da8822011-04-26 19:44:18 +00005616MachineBasicBlock *
5617ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5618 MachineBasicBlock *BB,
5619 unsigned Size,
5620 bool signExtend,
5621 ARMCC::CondCodes Cond) const {
5622 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5623
5624 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5625 MachineFunction *MF = BB->getParent();
5626 MachineFunction::iterator It = BB;
5627 ++It;
5628
5629 unsigned dest = MI->getOperand(0).getReg();
5630 unsigned ptr = MI->getOperand(1).getReg();
5631 unsigned incr = MI->getOperand(2).getReg();
5632 unsigned oldval = dest;
5633 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005634 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005635
5636 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5637 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005638 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5639 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005640 }
5641
Jim Grosbachf7da8822011-04-26 19:44:18 +00005642 unsigned ldrOpc, strOpc, extendOpc;
5643 switch (Size) {
5644 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5645 case 1:
5646 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5647 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005648 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005649 break;
5650 case 2:
5651 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5652 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005653 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005654 break;
5655 case 4:
5656 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5657 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5658 extendOpc = 0;
5659 break;
5660 }
5661
5662 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5663 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5664 MF->insert(It, loopMBB);
5665 MF->insert(It, exitMBB);
5666
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);
5672
Craig Topper420761a2012-04-20 07:30:17 +00005673 const TargetRegisterClass *TRC = isThumb2 ?
Jakob Stoklund Olesen05e80f22012-08-31 02:08:34 +00005674 (const TargetRegisterClass*)&ARM::rGPRRegClass :
Craig Topper420761a2012-04-20 07:30:17 +00005675 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005676 unsigned scratch = MRI.createVirtualRegister(TRC);
5677 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005678
5679 // thisMBB:
5680 // ...
5681 // fallthrough --> loopMBB
5682 BB->addSuccessor(loopMBB);
5683
5684 // loopMBB:
5685 // ldrex dest, ptr
5686 // (sign extend dest, if required)
5687 // cmp dest, incr
James Molloyd6d10ae2012-09-26 09:48:32 +00005688 // cmov.cond scratch2, incr, dest
Jim Grosbachf7da8822011-04-26 19:44:18 +00005689 // strex scratch, scratch2, ptr
5690 // cmp scratch, #0
5691 // bne- loopMBB
5692 // fallthrough --> exitMBB
5693 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005694 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5695 if (ldrOpc == ARM::t2LDREX)
5696 MIB.addImm(0);
5697 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005698
5699 // Sign extend the value, if necessary.
5700 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005701 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005702 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5703 .addReg(dest)
5704 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005705 }
5706
5707 // Build compare and cmov instructions.
5708 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5709 .addReg(oldval).addReg(incr));
5710 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
James Molloyd6d10ae2012-09-26 09:48:32 +00005711 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005712
Jim Grosbachb6aed502011-09-09 18:37:27 +00005713 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5714 if (strOpc == ARM::t2STREX)
5715 MIB.addImm(0);
5716 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005717 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5718 .addReg(scratch).addImm(0));
5719 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5720 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5721
5722 BB->addSuccessor(loopMBB);
5723 BB->addSuccessor(exitMBB);
5724
5725 // exitMBB:
5726 // ...
5727 BB = exitMBB;
5728
5729 MI->eraseFromParent(); // The instruction is gone now.
5730
5731 return BB;
5732}
5733
Eli Friedman2bdffe42011-08-31 00:31:29 +00005734MachineBasicBlock *
5735ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5736 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005737 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005738 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5739 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5740
5741 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5742 MachineFunction *MF = BB->getParent();
5743 MachineFunction::iterator It = BB;
5744 ++It;
5745
5746 unsigned destlo = MI->getOperand(0).getReg();
5747 unsigned desthi = MI->getOperand(1).getReg();
5748 unsigned ptr = MI->getOperand(2).getReg();
5749 unsigned vallo = MI->getOperand(3).getReg();
5750 unsigned valhi = MI->getOperand(4).getReg();
5751 DebugLoc dl = MI->getDebugLoc();
5752 bool isThumb2 = Subtarget->isThumb2();
5753
5754 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5755 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005756 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5757 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5758 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005759 }
5760
5761 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5762 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5763
5764 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005765 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005766 if (IsCmpxchg) {
5767 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5768 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5769 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005770 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5771 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005772 if (IsCmpxchg) {
5773 MF->insert(It, contBB);
5774 MF->insert(It, cont2BB);
5775 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005776 MF->insert(It, exitMBB);
5777
5778 // Transfer the remainder of BB and its successor edges to exitMBB.
5779 exitMBB->splice(exitMBB->begin(), BB,
5780 llvm::next(MachineBasicBlock::iterator(MI)),
5781 BB->end());
5782 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5783
Craig Topper420761a2012-04-20 07:30:17 +00005784 const TargetRegisterClass *TRC = isThumb2 ?
5785 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5786 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005787 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5788
5789 // thisMBB:
5790 // ...
5791 // fallthrough --> loopMBB
5792 BB->addSuccessor(loopMBB);
5793
5794 // loopMBB:
5795 // ldrexd r2, r3, ptr
5796 // <binopa> r0, r2, incr
5797 // <binopb> r1, r3, incr
5798 // strexd storesuccess, r0, r1, ptr
5799 // cmp storesuccess, #0
5800 // bne- loopMBB
5801 // fallthrough --> exitMBB
5802 //
5803 // Note that the registers are explicitly specified because there is not any
5804 // way to force the register allocator to allocate a register pair.
5805 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005806 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005807 // need to properly enforce the restriction that the two output registers
5808 // for ldrexd must be different.
5809 BB = loopMBB;
5810 // Load
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005811 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5812 unsigned GPRPair1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5813
Eli Friedman2bdffe42011-08-31 00:31:29 +00005814 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005815 .addReg(GPRPair0, RegState::Define).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00005816 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005817 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
5818 .addReg(GPRPair0, 0, ARM::gsub_0);
5819 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
5820 .addReg(GPRPair0, 0, ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005821
5822 if (IsCmpxchg) {
5823 // Add early exit
5824 for (unsigned i = 0; i < 2; i++) {
5825 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5826 ARM::CMPrr))
5827 .addReg(i == 0 ? destlo : desthi)
5828 .addReg(i == 0 ? vallo : valhi));
5829 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5830 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5831 BB->addSuccessor(exitMBB);
5832 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5833 BB = (i == 0 ? contBB : cont2BB);
5834 }
5835
5836 // Copy to physregs for strexd
5837 unsigned setlo = MI->getOperand(5).getReg();
5838 unsigned sethi = MI->getOperand(6).getReg();
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005839 unsigned undef = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5840 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5841 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), undef);
5842 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5843 .addReg(undef)
5844 .addReg(setlo)
5845 .addImm(ARM::gsub_0);
5846 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
5847 .addReg(r1)
5848 .addReg(sethi)
5849 .addImm(ARM::gsub_1);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005850 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005851 // Perform binary operation
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005852 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
5853 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
Eli Friedman2bdffe42011-08-31 00:31:29 +00005854 .addReg(destlo).addReg(vallo))
5855 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005856 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
5857 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
Eli Friedman2bdffe42011-08-31 00:31:29 +00005858 .addReg(desthi).addReg(valhi)).addReg(0);
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005859
5860 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5861 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
5862 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5863 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5864 .addReg(UndefPair)
5865 .addReg(tmpRegLo)
5866 .addImm(ARM::gsub_0);
5867 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
5868 .addReg(r1)
5869 .addReg(tmpRegHi)
5870 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005871 } else {
5872 // Copy to physregs for strexd
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005873 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5874 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5875 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
5876 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
5877 .addReg(UndefPair)
5878 .addReg(vallo)
5879 .addImm(ARM::gsub_0);
5880 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), GPRPair1)
5881 .addReg(r1)
5882 .addReg(valhi)
5883 .addImm(ARM::gsub_1);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005884 }
5885
5886 // Store
5887 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
Weiming Zhaoe56764b2012-11-16 21:55:34 +00005888 .addReg(GPRPair1).addReg(ptr));
Eli Friedman2bdffe42011-08-31 00:31:29 +00005889 // Cmp+jump
5890 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5891 .addReg(storesuccess).addImm(0));
5892 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5893 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5894
5895 BB->addSuccessor(loopMBB);
5896 BB->addSuccessor(exitMBB);
5897
5898 // exitMBB:
5899 // ...
5900 BB = exitMBB;
5901
5902 MI->eraseFromParent(); // The instruction is gone now.
5903
5904 return BB;
5905}
5906
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005907/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5908/// registers the function context.
5909void ARMTargetLowering::
5910SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5911 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005912 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5913 DebugLoc dl = MI->getDebugLoc();
5914 MachineFunction *MF = MBB->getParent();
5915 MachineRegisterInfo *MRI = &MF->getRegInfo();
5916 MachineConstantPool *MCP = MF->getConstantPool();
5917 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5918 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005919
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005920 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005921 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005922
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005923 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005924 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005925 ARMConstantPoolValue *CPV =
5926 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5927 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5928
Craig Topper420761a2012-04-20 07:30:17 +00005929 const TargetRegisterClass *TRC = isThumb ?
5930 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5931 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005932
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005933 // Grab constant pool and fixed stack memory operands.
5934 MachineMemOperand *CPMMO =
5935 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5936 MachineMemOperand::MOLoad, 4, 4);
5937
5938 MachineMemOperand *FIMMOSt =
5939 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5940 MachineMemOperand::MOStore, 4, 4);
5941
5942 // Load the address of the dispatch MBB into the jump buffer.
5943 if (isThumb2) {
5944 // Incoming value: jbuf
5945 // ldr.n r5, LCPI1_1
5946 // orr r5, r5, #1
5947 // add r5, pc
5948 // str r5, [$jbuf, #+4] ; &jbuf[1]
5949 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5950 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5951 .addConstantPoolIndex(CPI)
5952 .addMemOperand(CPMMO));
5953 // Set the low bit because of thumb mode.
5954 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5955 AddDefaultCC(
5956 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5957 .addReg(NewVReg1, RegState::Kill)
5958 .addImm(0x01)));
5959 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5960 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5961 .addReg(NewVReg2, RegState::Kill)
5962 .addImm(PCLabelId);
5963 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5964 .addReg(NewVReg3, RegState::Kill)
5965 .addFrameIndex(FI)
5966 .addImm(36) // &jbuf[1] :: pc
5967 .addMemOperand(FIMMOSt));
5968 } else if (isThumb) {
5969 // Incoming value: jbuf
5970 // ldr.n r1, LCPI1_4
5971 // add r1, pc
5972 // mov r2, #1
5973 // orrs r1, r2
5974 // add r2, $jbuf, #+4 ; &jbuf[1]
5975 // str r1, [r2]
5976 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5977 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5978 .addConstantPoolIndex(CPI)
5979 .addMemOperand(CPMMO));
5980 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5981 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5982 .addReg(NewVReg1, RegState::Kill)
5983 .addImm(PCLabelId);
5984 // Set the low bit because of thumb mode.
5985 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5986 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5987 .addReg(ARM::CPSR, RegState::Define)
5988 .addImm(1));
5989 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5990 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5991 .addReg(ARM::CPSR, RegState::Define)
5992 .addReg(NewVReg2, RegState::Kill)
5993 .addReg(NewVReg3, RegState::Kill));
5994 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5995 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5996 .addFrameIndex(FI)
5997 .addImm(36)); // &jbuf[1] :: pc
5998 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5999 .addReg(NewVReg4, RegState::Kill)
6000 .addReg(NewVReg5, RegState::Kill)
6001 .addImm(0)
6002 .addMemOperand(FIMMOSt));
6003 } else {
6004 // Incoming value: jbuf
6005 // ldr r1, LCPI1_1
6006 // add r1, pc, r1
6007 // str r1, [$jbuf, #+4] ; &jbuf[1]
6008 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6009 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6010 .addConstantPoolIndex(CPI)
6011 .addImm(0)
6012 .addMemOperand(CPMMO));
6013 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6014 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6015 .addReg(NewVReg1, RegState::Kill)
6016 .addImm(PCLabelId));
6017 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6018 .addReg(NewVReg2, RegState::Kill)
6019 .addFrameIndex(FI)
6020 .addImm(36) // &jbuf[1] :: pc
6021 .addMemOperand(FIMMOSt));
6022 }
6023}
6024
6025MachineBasicBlock *ARMTargetLowering::
6026EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6027 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6028 DebugLoc dl = MI->getDebugLoc();
6029 MachineFunction *MF = MBB->getParent();
6030 MachineRegisterInfo *MRI = &MF->getRegInfo();
6031 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6032 MachineFrameInfo *MFI = MF->getFrameInfo();
6033 int FI = MFI->getFunctionContextIndex();
6034
Craig Topper420761a2012-04-20 07:30:17 +00006035 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6036 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00006037 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006038
Bill Wendling04f15b42011-10-06 21:29:56 +00006039 // Get a mapping of the call site numbers to all of the landing pads they're
6040 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00006041 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6042 unsigned MaxCSNum = 0;
6043 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00006044 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6045 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00006046 if (!BB->isLandingPad()) continue;
6047
6048 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6049 // pad.
6050 for (MachineBasicBlock::iterator
6051 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6052 if (!II->isEHLabel()) continue;
6053
6054 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00006055 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00006056
Bill Wendling5cbef192011-10-05 23:28:57 +00006057 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6058 for (SmallVectorImpl<unsigned>::iterator
6059 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6060 CSI != CSE; ++CSI) {
6061 CallSiteNumToLPad[*CSI].push_back(BB);
6062 MaxCSNum = std::max(MaxCSNum, *CSI);
6063 }
Bill Wendling2a850152011-10-05 00:02:33 +00006064 break;
6065 }
6066 }
6067
6068 // Get an ordered list of the machine basic blocks for the jump table.
6069 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00006070 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00006071 LPadList.reserve(CallSiteNumToLPad.size());
6072 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6073 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6074 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006075 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00006076 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00006077 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6078 }
Bill Wendling2a850152011-10-05 00:02:33 +00006079 }
6080
Bill Wendling5cbef192011-10-05 23:28:57 +00006081 assert(!LPadList.empty() &&
6082 "No landing pad destinations for the dispatch jump table!");
6083
Bill Wendling04f15b42011-10-06 21:29:56 +00006084 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00006085 MachineJumpTableInfo *JTI =
6086 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6087 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6088 unsigned UId = AFI->createJumpTableUId();
6089
Bill Wendling04f15b42011-10-06 21:29:56 +00006090 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006091
6092 // Shove the dispatch's address into the return slot in the function context.
6093 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6094 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006095
Bill Wendlingbb734682011-10-05 00:39:32 +00006096 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00006097 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00006098 DispatchBB->addSuccessor(TrapBB);
6099
6100 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6101 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00006102
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00006103 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00006104 MF->insert(MF->end(), DispatchBB);
6105 MF->insert(MF->end(), DispContBB);
6106 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00006107
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006108 // Insert code into the entry block that creates and registers the function
6109 // context.
6110 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6111
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00006112 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00006113 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00006114 MachineMemOperand::MOLoad |
6115 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00006116
Chad Rosiere7bd5192012-11-06 23:05:24 +00006117 MachineInstrBuilder MIB;
6118 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6119
6120 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6121 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6122
6123 // Add a register mask with no preserved registers. This results in all
6124 // registers being marked as clobbered.
6125 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00006126
Bill Wendling952cb502011-10-18 22:49:07 +00006127 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00006128 if (Subtarget->isThumb2()) {
6129 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6130 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6131 .addFrameIndex(FI)
6132 .addImm(4)
6133 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006134
Bill Wendling952cb502011-10-18 22:49:07 +00006135 if (NumLPads < 256) {
6136 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6137 .addReg(NewVReg1)
6138 .addImm(LPadList.size()));
6139 } else {
6140 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6141 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006142 .addImm(NumLPads & 0xFFFF));
6143
6144 unsigned VReg2 = VReg1;
6145 if ((NumLPads & 0xFFFF0000) != 0) {
6146 VReg2 = MRI->createVirtualRegister(TRC);
6147 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6148 .addReg(VReg1)
6149 .addImm(NumLPads >> 16));
6150 }
6151
Bill Wendling952cb502011-10-18 22:49:07 +00006152 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6153 .addReg(NewVReg1)
6154 .addReg(VReg2));
6155 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006156
Bill Wendling95ce2e92011-10-06 22:53:00 +00006157 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6158 .addMBB(TrapBB)
6159 .addImm(ARMCC::HI)
6160 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00006161
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006162 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6163 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006164 .addJumpTableIndex(MJTI)
6165 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00006166
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006167 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006168 AddDefaultCC(
6169 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006170 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6171 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006172 .addReg(NewVReg1)
6173 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6174
6175 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00006176 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00006177 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006178 .addJumpTableIndex(MJTI)
6179 .addImm(UId);
6180 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006181 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6182 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6183 .addFrameIndex(FI)
6184 .addImm(1)
6185 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006186
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006187 if (NumLPads < 256) {
6188 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6189 .addReg(NewVReg1)
6190 .addImm(NumLPads));
6191 } else {
6192 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006193 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6194 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6195
6196 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006197 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006198 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006199 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006200 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006201
6202 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6203 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6204 .addReg(VReg1, RegState::Define)
6205 .addConstantPoolIndex(Idx));
6206 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6207 .addReg(NewVReg1)
6208 .addReg(VReg1));
6209 }
6210
Bill Wendling083a8eb2011-10-06 23:37:36 +00006211 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6212 .addMBB(TrapBB)
6213 .addImm(ARMCC::HI)
6214 .addReg(ARM::CPSR);
6215
6216 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6217 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6218 .addReg(ARM::CPSR, RegState::Define)
6219 .addReg(NewVReg1)
6220 .addImm(2));
6221
6222 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006223 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006224 .addJumpTableIndex(MJTI)
6225 .addImm(UId));
6226
6227 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6228 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6229 .addReg(ARM::CPSR, RegState::Define)
6230 .addReg(NewVReg2, RegState::Kill)
6231 .addReg(NewVReg3));
6232
6233 MachineMemOperand *JTMMOLd =
6234 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6235 MachineMemOperand::MOLoad, 4, 4);
6236
6237 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6238 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6239 .addReg(NewVReg4, RegState::Kill)
6240 .addImm(0)
6241 .addMemOperand(JTMMOLd));
6242
6243 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6244 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6245 .addReg(ARM::CPSR, RegState::Define)
6246 .addReg(NewVReg5, RegState::Kill)
6247 .addReg(NewVReg3));
6248
6249 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6250 .addReg(NewVReg6, RegState::Kill)
6251 .addJumpTableIndex(MJTI)
6252 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006253 } else {
6254 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6255 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6256 .addFrameIndex(FI)
6257 .addImm(4)
6258 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006259
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006260 if (NumLPads < 256) {
6261 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6262 .addReg(NewVReg1)
6263 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006264 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006265 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6266 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006267 .addImm(NumLPads & 0xFFFF));
6268
6269 unsigned VReg2 = VReg1;
6270 if ((NumLPads & 0xFFFF0000) != 0) {
6271 VReg2 = MRI->createVirtualRegister(TRC);
6272 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6273 .addReg(VReg1)
6274 .addImm(NumLPads >> 16));
6275 }
6276
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006277 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6278 .addReg(NewVReg1)
6279 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006280 } else {
6281 MachineConstantPool *ConstantPool = MF->getConstantPool();
6282 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6283 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6284
6285 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006286 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling922ad782011-10-19 09:24:02 +00006287 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006288 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling922ad782011-10-19 09:24:02 +00006289 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6290
6291 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6292 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6293 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006294 .addConstantPoolIndex(Idx)
6295 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006296 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6297 .addReg(NewVReg1)
6298 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006299 }
6300
Bill Wendling95ce2e92011-10-06 22:53:00 +00006301 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6302 .addMBB(TrapBB)
6303 .addImm(ARMCC::HI)
6304 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006305
Bill Wendling564392b2011-10-18 22:11:18 +00006306 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006307 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006308 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006309 .addReg(NewVReg1)
6310 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006311 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6312 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006313 .addJumpTableIndex(MJTI)
6314 .addImm(UId));
6315
6316 MachineMemOperand *JTMMOLd =
6317 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6318 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006319 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006320 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006321 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6322 .addReg(NewVReg3, RegState::Kill)
6323 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006324 .addImm(0)
6325 .addMemOperand(JTMMOLd));
6326
6327 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006328 .addReg(NewVReg5, RegState::Kill)
6329 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006330 .addJumpTableIndex(MJTI)
6331 .addImm(UId);
6332 }
Bill Wendling2a850152011-10-05 00:02:33 +00006333
Bill Wendlingbb734682011-10-05 00:39:32 +00006334 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006335 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendlingbb734682011-10-05 00:39:32 +00006336 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006337 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6338 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesena0708d12012-08-20 20:52:03 +00006339 if (SeenMBBs.insert(CurMBB))
Bill Wendling2acf6382011-10-07 23:18:02 +00006340 DispContBB->addSuccessor(CurMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006341 }
6342
Bill Wendling24bb9252011-10-17 05:25:09 +00006343 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper015f2282012-03-04 03:33:22 +00006344 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006345 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006346 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6347 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6348 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006349
6350 // Remove the landing pad successor from the invoke block and replace it
6351 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006352 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6353 BB->succ_end());
6354 while (!Successors.empty()) {
6355 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006356 if (SMBB->isLandingPad()) {
6357 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006358 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006359 }
6360 }
6361
6362 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006363
6364 // Find the invoke call and mark all of the callee-saved registers as
6365 // 'implicit defined' so that they're spilled. This prevents code from
6366 // moving instructions to before the EH block, where they will never be
6367 // executed.
6368 for (MachineBasicBlock::reverse_iterator
6369 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006370 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006371
6372 DenseMap<unsigned, bool> DefRegs;
6373 for (MachineInstr::mop_iterator
6374 OI = II->operands_begin(), OE = II->operands_end();
6375 OI != OE; ++OI) {
6376 if (!OI->isReg()) continue;
6377 DefRegs[OI->getReg()] = true;
6378 }
6379
6380 MachineInstrBuilder MIB(&*II);
6381
Bill Wendling5d798592011-10-14 23:55:44 +00006382 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006383 unsigned Reg = SavedRegs[i];
6384 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006385 !ARM::tGPRRegClass.contains(Reg) &&
6386 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006387 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006388 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006389 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006390 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006391 continue;
6392 if (!DefRegs[Reg])
6393 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006394 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006395
6396 break;
6397 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006398 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006399
Bill Wendlingf7b02072011-10-18 18:30:49 +00006400 // Mark all former landing pads as non-landing pads. The dispatch is the only
6401 // landing pad now.
6402 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6403 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6404 (*I)->setIsLandingPad(false);
6405
Bill Wendlingbb734682011-10-05 00:39:32 +00006406 // The instruction is gone now.
6407 MI->eraseFromParent();
6408
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006409 return MBB;
6410}
6411
Evan Cheng218977b2010-07-13 19:27:42 +00006412static
6413MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6414 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6415 E = MBB->succ_end(); I != E; ++I)
6416 if (*I != Succ)
6417 return *I;
6418 llvm_unreachable("Expecting a BB with two successors!");
6419}
6420
Manman Ren68f25572012-06-01 19:33:18 +00006421MachineBasicBlock *ARMTargetLowering::
6422EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6423 // This pseudo instruction has 3 operands: dst, src, size
6424 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6425 // Otherwise, we will generate unrolled scalar copies.
6426 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6427 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6428 MachineFunction::iterator It = BB;
6429 ++It;
6430
6431 unsigned dest = MI->getOperand(0).getReg();
6432 unsigned src = MI->getOperand(1).getReg();
6433 unsigned SizeVal = MI->getOperand(2).getImm();
6434 unsigned Align = MI->getOperand(3).getImm();
6435 DebugLoc dl = MI->getDebugLoc();
6436
6437 bool isThumb2 = Subtarget->isThumb2();
6438 MachineFunction *MF = BB->getParent();
6439 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006440 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006441
6442 const TargetRegisterClass *TRC = isThumb2 ?
6443 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6444 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006445 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006446
6447 if (Align & 1) {
6448 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6449 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6450 UnitSize = 1;
6451 } else if (Align & 2) {
6452 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6453 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6454 UnitSize = 2;
6455 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006456 // Check whether we can use NEON instructions.
Bill Wendling67658342012-10-09 07:45:08 +00006457 if (!MF->getFunction()->getFnAttributes().
6458 hasAttribute(Attributes::NoImplicitFloat) &&
Manman Reneda9fdf2012-06-18 22:23:48 +00006459 Subtarget->hasNEON()) {
6460 if ((Align % 16 == 0) && SizeVal >= 16) {
6461 ldrOpc = ARM::VLD1q32wb_fixed;
6462 strOpc = ARM::VST1q32wb_fixed;
6463 UnitSize = 16;
6464 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6465 }
6466 else if ((Align % 8 == 0) && SizeVal >= 8) {
6467 ldrOpc = ARM::VLD1d32wb_fixed;
6468 strOpc = ARM::VST1d32wb_fixed;
6469 UnitSize = 8;
6470 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6471 }
6472 }
6473 // Can't use NEON instructions.
6474 if (UnitSize == 0) {
6475 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6476 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6477 UnitSize = 4;
6478 }
Manman Ren68f25572012-06-01 19:33:18 +00006479 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006480
Manman Ren68f25572012-06-01 19:33:18 +00006481 unsigned BytesLeft = SizeVal % UnitSize;
6482 unsigned LoopSize = SizeVal - BytesLeft;
6483
6484 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6485 // Use LDR and STR to copy.
6486 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6487 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6488 unsigned srcIn = src;
6489 unsigned destIn = dest;
6490 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006491 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006492 unsigned srcOut = MRI.createVirtualRegister(TRC);
6493 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006494 if (UnitSize >= 8) {
6495 AddDefaultPred(BuildMI(*BB, MI, dl,
6496 TII->get(ldrOpc), scratch)
6497 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6498
6499 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6500 .addReg(destIn).addImm(0).addReg(scratch));
6501 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006502 AddDefaultPred(BuildMI(*BB, MI, dl,
6503 TII->get(ldrOpc), scratch)
6504 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6505
6506 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6507 .addReg(scratch).addReg(destIn)
6508 .addImm(UnitSize));
6509 } else {
6510 AddDefaultPred(BuildMI(*BB, MI, dl,
6511 TII->get(ldrOpc), scratch)
6512 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6513 .addImm(UnitSize));
6514
6515 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6516 .addReg(scratch).addReg(destIn)
6517 .addReg(0).addImm(UnitSize));
6518 }
6519 srcIn = srcOut;
6520 destIn = destOut;
6521 }
6522
6523 // Handle the leftover bytes with LDRB and STRB.
6524 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6525 // [destOut] = STRB_POST(scratch, destIn, 1)
6526 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6527 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6528 for (unsigned i = 0; i < BytesLeft; i++) {
6529 unsigned scratch = MRI.createVirtualRegister(TRC);
6530 unsigned srcOut = MRI.createVirtualRegister(TRC);
6531 unsigned destOut = MRI.createVirtualRegister(TRC);
6532 if (isThumb2) {
6533 AddDefaultPred(BuildMI(*BB, MI, dl,
6534 TII->get(ldrOpc),scratch)
6535 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6536
6537 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6538 .addReg(scratch).addReg(destIn)
6539 .addReg(0).addImm(1));
6540 } else {
6541 AddDefaultPred(BuildMI(*BB, MI, dl,
6542 TII->get(ldrOpc),scratch)
Stepan Dyatkovskiy2c2cb3c2012-10-10 11:43:40 +00006543 .addReg(srcOut, RegState::Define).addReg(srcIn)
6544 .addReg(0).addImm(1));
Manman Ren68f25572012-06-01 19:33:18 +00006545
6546 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6547 .addReg(scratch).addReg(destIn)
6548 .addReg(0).addImm(1));
6549 }
6550 srcIn = srcOut;
6551 destIn = destOut;
6552 }
6553 MI->eraseFromParent(); // The instruction is gone now.
6554 return BB;
6555 }
6556
6557 // Expand the pseudo op to a loop.
6558 // thisMBB:
6559 // ...
6560 // movw varEnd, # --> with thumb2
6561 // movt varEnd, #
6562 // ldrcp varEnd, idx --> without thumb2
6563 // fallthrough --> loopMBB
6564 // loopMBB:
6565 // PHI varPhi, varEnd, varLoop
6566 // PHI srcPhi, src, srcLoop
6567 // PHI destPhi, dst, destLoop
6568 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6569 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6570 // subs varLoop, varPhi, #UnitSize
6571 // bne loopMBB
6572 // fallthrough --> exitMBB
6573 // exitMBB:
6574 // epilogue to handle left-over bytes
6575 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6576 // [destOut] = STRB_POST(scratch, destLoop, 1)
6577 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6578 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6579 MF->insert(It, loopMBB);
6580 MF->insert(It, exitMBB);
6581
6582 // Transfer the remainder of BB and its successor edges to exitMBB.
6583 exitMBB->splice(exitMBB->begin(), BB,
6584 llvm::next(MachineBasicBlock::iterator(MI)),
6585 BB->end());
6586 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6587
6588 // Load an immediate to varEnd.
6589 unsigned varEnd = MRI.createVirtualRegister(TRC);
6590 if (isThumb2) {
6591 unsigned VReg1 = varEnd;
6592 if ((LoopSize & 0xFFFF0000) != 0)
6593 VReg1 = MRI.createVirtualRegister(TRC);
6594 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6595 .addImm(LoopSize & 0xFFFF));
6596
6597 if ((LoopSize & 0xFFFF0000) != 0)
6598 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6599 .addReg(VReg1)
6600 .addImm(LoopSize >> 16));
6601 } else {
6602 MachineConstantPool *ConstantPool = MF->getConstantPool();
6603 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6604 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6605
6606 // MachineConstantPool wants an explicit alignment.
Micah Villmow3574eca2012-10-08 16:38:25 +00006607 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Manman Ren68f25572012-06-01 19:33:18 +00006608 if (Align == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00006609 Align = getDataLayout()->getTypeAllocSize(C->getType());
Manman Ren68f25572012-06-01 19:33:18 +00006610 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6611
6612 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6613 .addReg(varEnd, RegState::Define)
6614 .addConstantPoolIndex(Idx)
6615 .addImm(0));
6616 }
6617 BB->addSuccessor(loopMBB);
6618
6619 // Generate the loop body:
6620 // varPhi = PHI(varLoop, varEnd)
6621 // srcPhi = PHI(srcLoop, src)
6622 // destPhi = PHI(destLoop, dst)
6623 MachineBasicBlock *entryBB = BB;
6624 BB = loopMBB;
6625 unsigned varLoop = MRI.createVirtualRegister(TRC);
6626 unsigned varPhi = MRI.createVirtualRegister(TRC);
6627 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6628 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6629 unsigned destLoop = MRI.createVirtualRegister(TRC);
6630 unsigned destPhi = MRI.createVirtualRegister(TRC);
6631
6632 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6633 .addReg(varLoop).addMBB(loopMBB)
6634 .addReg(varEnd).addMBB(entryBB);
6635 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6636 .addReg(srcLoop).addMBB(loopMBB)
6637 .addReg(src).addMBB(entryBB);
6638 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6639 .addReg(destLoop).addMBB(loopMBB)
6640 .addReg(dest).addMBB(entryBB);
6641
6642 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6643 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006644 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6645 if (UnitSize >= 8) {
6646 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6647 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6648
6649 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6650 .addReg(destPhi).addImm(0).addReg(scratch));
6651 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006652 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6653 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6654
6655 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6656 .addReg(scratch).addReg(destPhi)
6657 .addImm(UnitSize));
6658 } else {
6659 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6660 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6661 .addImm(UnitSize));
6662
6663 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6664 .addReg(scratch).addReg(destPhi)
6665 .addReg(0).addImm(UnitSize));
6666 }
6667
6668 // Decrement loop variable by UnitSize.
6669 MachineInstrBuilder MIB = BuildMI(BB, dl,
6670 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6671 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6672 MIB->getOperand(5).setReg(ARM::CPSR);
6673 MIB->getOperand(5).setIsDef(true);
6674
6675 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6676 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6677
6678 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6679 BB->addSuccessor(loopMBB);
6680 BB->addSuccessor(exitMBB);
6681
6682 // Add epilogue to handle BytesLeft.
6683 BB = exitMBB;
6684 MachineInstr *StartOfExit = exitMBB->begin();
6685 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6686 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6687
6688 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6689 // [destOut] = STRB_POST(scratch, destLoop, 1)
6690 unsigned srcIn = srcLoop;
6691 unsigned destIn = destLoop;
6692 for (unsigned i = 0; i < BytesLeft; i++) {
6693 unsigned scratch = MRI.createVirtualRegister(TRC);
6694 unsigned srcOut = MRI.createVirtualRegister(TRC);
6695 unsigned destOut = MRI.createVirtualRegister(TRC);
6696 if (isThumb2) {
6697 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6698 TII->get(ldrOpc),scratch)
6699 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6700
6701 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6702 .addReg(scratch).addReg(destIn)
6703 .addImm(1));
6704 } else {
6705 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6706 TII->get(ldrOpc),scratch)
6707 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6708
6709 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6710 .addReg(scratch).addReg(destIn)
6711 .addReg(0).addImm(1));
6712 }
6713 srcIn = srcOut;
6714 destIn = destOut;
6715 }
6716
6717 MI->eraseFromParent(); // The instruction is gone now.
6718 return BB;
6719}
6720
Jim Grosbache801dc42009-12-12 01:40:06 +00006721MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006722ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006723 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006724 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006725 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006726 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006727 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006728 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006729 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006730 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006731 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006732 // The Thumb2 pre-indexed stores have the same MI operands, they just
6733 // define them differently in the .td files from the isel patterns, so
6734 // they need pseudos.
6735 case ARM::t2STR_preidx:
6736 MI->setDesc(TII->get(ARM::t2STR_PRE));
6737 return BB;
6738 case ARM::t2STRB_preidx:
6739 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6740 return BB;
6741 case ARM::t2STRH_preidx:
6742 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6743 return BB;
6744
Jim Grosbach19dec202011-08-05 20:35:44 +00006745 case ARM::STRi_preidx:
6746 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006747 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006748 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6749 // Decode the offset.
6750 unsigned Offset = MI->getOperand(4).getImm();
6751 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6752 Offset = ARM_AM::getAM2Offset(Offset);
6753 if (isSub)
6754 Offset = -Offset;
6755
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006756 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006757 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006758 .addOperand(MI->getOperand(0)) // Rn_wb
6759 .addOperand(MI->getOperand(1)) // Rt
6760 .addOperand(MI->getOperand(2)) // Rn
6761 .addImm(Offset) // offset (skip GPR==zero_reg)
6762 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006763 .addOperand(MI->getOperand(6))
6764 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006765 MI->eraseFromParent();
6766 return BB;
6767 }
6768 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006769 case ARM::STRBr_preidx:
6770 case ARM::STRH_preidx: {
6771 unsigned NewOpc;
6772 switch (MI->getOpcode()) {
6773 default: llvm_unreachable("unexpected opcode!");
6774 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6775 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6776 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6777 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006778 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6779 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6780 MIB.addOperand(MI->getOperand(i));
6781 MI->eraseFromParent();
6782 return BB;
6783 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006784 case ARM::ATOMIC_LOAD_ADD_I8:
6785 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6786 case ARM::ATOMIC_LOAD_ADD_I16:
6787 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6788 case ARM::ATOMIC_LOAD_ADD_I32:
6789 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006790
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006791 case ARM::ATOMIC_LOAD_AND_I8:
6792 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6793 case ARM::ATOMIC_LOAD_AND_I16:
6794 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6795 case ARM::ATOMIC_LOAD_AND_I32:
6796 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006797
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006798 case ARM::ATOMIC_LOAD_OR_I8:
6799 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6800 case ARM::ATOMIC_LOAD_OR_I16:
6801 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6802 case ARM::ATOMIC_LOAD_OR_I32:
6803 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006804
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006805 case ARM::ATOMIC_LOAD_XOR_I8:
6806 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6807 case ARM::ATOMIC_LOAD_XOR_I16:
6808 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6809 case ARM::ATOMIC_LOAD_XOR_I32:
6810 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006811
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006812 case ARM::ATOMIC_LOAD_NAND_I8:
6813 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6814 case ARM::ATOMIC_LOAD_NAND_I16:
6815 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6816 case ARM::ATOMIC_LOAD_NAND_I32:
6817 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006818
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006819 case ARM::ATOMIC_LOAD_SUB_I8:
6820 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6821 case ARM::ATOMIC_LOAD_SUB_I16:
6822 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6823 case ARM::ATOMIC_LOAD_SUB_I32:
6824 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006825
Jim Grosbachf7da8822011-04-26 19:44:18 +00006826 case ARM::ATOMIC_LOAD_MIN_I8:
6827 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6828 case ARM::ATOMIC_LOAD_MIN_I16:
6829 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6830 case ARM::ATOMIC_LOAD_MIN_I32:
6831 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6832
6833 case ARM::ATOMIC_LOAD_MAX_I8:
6834 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6835 case ARM::ATOMIC_LOAD_MAX_I16:
6836 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6837 case ARM::ATOMIC_LOAD_MAX_I32:
6838 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6839
6840 case ARM::ATOMIC_LOAD_UMIN_I8:
6841 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6842 case ARM::ATOMIC_LOAD_UMIN_I16:
6843 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6844 case ARM::ATOMIC_LOAD_UMIN_I32:
6845 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6846
6847 case ARM::ATOMIC_LOAD_UMAX_I8:
6848 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6849 case ARM::ATOMIC_LOAD_UMAX_I16:
6850 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6851 case ARM::ATOMIC_LOAD_UMAX_I32:
6852 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6853
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006854 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6855 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6856 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006857
6858 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6859 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6860 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006861
Eli Friedman2bdffe42011-08-31 00:31:29 +00006862
6863 case ARM::ATOMADD6432:
6864 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006865 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6866 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006867 case ARM::ATOMSUB6432:
6868 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006869 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6870 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006871 case ARM::ATOMOR6432:
6872 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006873 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006874 case ARM::ATOMXOR6432:
6875 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006876 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006877 case ARM::ATOMAND6432:
6878 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006879 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006880 case ARM::ATOMSWAP6432:
6881 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006882 case ARM::ATOMCMPXCHG6432:
6883 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6884 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6885 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006886
Evan Cheng007ea272009-08-12 05:17:19 +00006887 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006888 // To "insert" a SELECT_CC instruction, we actually have to insert the
6889 // diamond control-flow pattern. The incoming instruction knows the
6890 // destination vreg to set, the condition code register to branch on, the
6891 // true/false values to select between, and a branch opcode to use.
6892 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006893 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006894 ++It;
6895
6896 // thisMBB:
6897 // ...
6898 // TrueVal = ...
6899 // cmpTY ccX, r1, r2
6900 // bCC copy1MBB
6901 // fallthrough --> copy0MBB
6902 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006903 MachineFunction *F = BB->getParent();
6904 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6905 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006906 F->insert(It, copy0MBB);
6907 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006908
6909 // Transfer the remainder of BB and its successor edges to sinkMBB.
6910 sinkMBB->splice(sinkMBB->begin(), BB,
6911 llvm::next(MachineBasicBlock::iterator(MI)),
6912 BB->end());
6913 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6914
Dan Gohman258c58c2010-07-06 15:49:48 +00006915 BB->addSuccessor(copy0MBB);
6916 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006917
Dan Gohman14152b42010-07-06 20:24:04 +00006918 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6919 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6920
Evan Chenga8e29892007-01-19 07:51:42 +00006921 // copy0MBB:
6922 // %FalseValue = ...
6923 // # fallthrough to sinkMBB
6924 BB = copy0MBB;
6925
6926 // Update machine-CFG edges
6927 BB->addSuccessor(sinkMBB);
6928
6929 // sinkMBB:
6930 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6931 // ...
6932 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006933 BuildMI(*BB, BB->begin(), dl,
6934 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006935 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6936 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6937
Dan Gohman14152b42010-07-06 20:24:04 +00006938 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006939 return BB;
6940 }
Evan Cheng86198642009-08-07 00:34:42 +00006941
Evan Cheng218977b2010-07-13 19:27:42 +00006942 case ARM::BCCi64:
6943 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006944 // If there is an unconditional branch to the other successor, remove it.
6945 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006946
Evan Cheng218977b2010-07-13 19:27:42 +00006947 // Compare both parts that make up the double comparison separately for
6948 // equality.
6949 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6950
6951 unsigned LHS1 = MI->getOperand(1).getReg();
6952 unsigned LHS2 = MI->getOperand(2).getReg();
6953 if (RHSisZero) {
6954 AddDefaultPred(BuildMI(BB, dl,
6955 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6956 .addReg(LHS1).addImm(0));
6957 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6958 .addReg(LHS2).addImm(0)
6959 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6960 } else {
6961 unsigned RHS1 = MI->getOperand(3).getReg();
6962 unsigned RHS2 = MI->getOperand(4).getReg();
6963 AddDefaultPred(BuildMI(BB, dl,
6964 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6965 .addReg(LHS1).addReg(RHS1));
6966 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6967 .addReg(LHS2).addReg(RHS2)
6968 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6969 }
6970
6971 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6972 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6973 if (MI->getOperand(0).getImm() == ARMCC::NE)
6974 std::swap(destMBB, exitMBB);
6975
6976 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6977 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006978 if (isThumb2)
6979 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6980 else
6981 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006982
6983 MI->eraseFromParent(); // The pseudo instruction is gone now.
6984 return BB;
6985 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006986
Bill Wendling5bc85282011-10-17 20:37:20 +00006987 case ARM::Int_eh_sjlj_setjmp:
6988 case ARM::Int_eh_sjlj_setjmp_nofp:
6989 case ARM::tInt_eh_sjlj_setjmp:
6990 case ARM::t2Int_eh_sjlj_setjmp:
6991 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6992 EmitSjLjDispatchBlock(MI, BB);
6993 return BB;
6994
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006995 case ARM::ABS:
6996 case ARM::t2ABS: {
6997 // To insert an ABS instruction, we have to insert the
6998 // diamond control-flow pattern. The incoming instruction knows the
6999 // source vreg to test against 0, the destination vreg to set,
7000 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007001 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007002 // It transforms
7003 // V1 = ABS V0
7004 // into
7005 // V2 = MOVS V0
7006 // BCC (branch to SinkBB if V0 >= 0)
7007 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007008 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007009 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7010 MachineFunction::iterator BBI = BB;
7011 ++BBI;
7012 MachineFunction *Fn = BB->getParent();
7013 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7014 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7015 Fn->insert(BBI, RSBBB);
7016 Fn->insert(BBI, SinkBB);
7017
7018 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7019 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7020 bool isThumb2 = Subtarget->isThumb2();
7021 MachineRegisterInfo &MRI = Fn->getRegInfo();
7022 // In Thumb mode S must not be specified if source register is the SP or
7023 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00007024 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7025 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7026 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007027
7028 // Transfer the remainder of BB and its successor edges to sinkMBB.
7029 SinkBB->splice(SinkBB->begin(), BB,
7030 llvm::next(MachineBasicBlock::iterator(MI)),
7031 BB->end());
7032 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7033
7034 BB->addSuccessor(RSBBB);
7035 BB->addSuccessor(SinkBB);
7036
7037 // fall through to SinkMBB
7038 RSBBB->addSuccessor(SinkBB);
7039
Manman Ren307473d2012-06-15 21:32:12 +00007040 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00007041 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00007042 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7043 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007044
7045 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007046 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007047 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7048 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7049
7050 // insert rsbri in RSBBB
7051 // Note: BCC and rsbri will be converted into predicated rsbmi
7052 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007053 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007054 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00007055 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007056 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7057
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007058 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007059 // reuse ABSDstReg to not change uses of ABS instruction
7060 BuildMI(*SinkBB, SinkBB->begin(), dl,
7061 TII->get(ARM::PHI), ABSDstReg)
7062 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00007063 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007064
7065 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00007066 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00007067
7068 // return last added BB
7069 return SinkBB;
7070 }
Manman Ren68f25572012-06-01 19:33:18 +00007071 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00007072 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00007073 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00007074 }
7075}
7076
Evan Cheng37fefc22011-08-30 19:09:48 +00007077void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7078 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007079 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007080 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7081 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7082 return;
7083 }
7084
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007085 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00007086 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7087 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7088 // operand is still set to noreg. If needed, set the optional operand's
7089 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00007090 //
Andrew Trick90b7b122011-10-18 19:18:52 +00007091 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00007092
Andrew Trick3be654f2011-09-21 02:20:46 +00007093 // Rename pseudo opcodes.
7094 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7095 if (NewOpc) {
7096 const ARMBaseInstrInfo *TII =
7097 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00007098 MCID = &TII->get(NewOpc);
7099
7100 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7101 "converted opcode should be the same except for cc_out");
7102
7103 MI->setDesc(*MCID);
7104
7105 // Add the optional cc_out operand
7106 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00007107 }
Andrew Trick90b7b122011-10-18 19:18:52 +00007108 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00007109
7110 // Any ARM instruction that sets the 's' bit should specify an optional
7111 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00007112 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007113 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007114 return;
7115 }
Andrew Trick3be654f2011-09-21 02:20:46 +00007116 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7117 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007118 bool definesCPSR = false;
7119 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00007120 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00007121 i != e; ++i) {
7122 const MachineOperand &MO = MI->getOperand(i);
7123 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7124 definesCPSR = true;
7125 if (MO.isDead())
7126 deadCPSR = true;
7127 MI->RemoveOperand(i);
7128 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00007129 }
7130 }
Andrew Trick4815d562011-09-20 03:17:40 +00007131 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00007132 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00007133 return;
7134 }
7135 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00007136 if (deadCPSR) {
7137 assert(!MI->getOperand(ccOutIdx).getReg() &&
7138 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00007139 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00007140 }
Andrew Trick4815d562011-09-20 03:17:40 +00007141
Andrew Trick3be654f2011-09-21 02:20:46 +00007142 // If this instruction was defined with an optional CPSR def and its dag node
7143 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00007144 MachineOperand &MO = MI->getOperand(ccOutIdx);
7145 MO.setReg(ARM::CPSR);
7146 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00007147}
7148
Evan Chenga8e29892007-01-19 07:51:42 +00007149//===----------------------------------------------------------------------===//
7150// ARM Optimization Hooks
7151//===----------------------------------------------------------------------===//
7152
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007153// Helper function that checks if N is a null or all ones constant.
7154static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7155 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7156 if (!C)
7157 return false;
7158 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7159}
7160
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007161// Return true if N is conditionally 0 or all ones.
7162// Detects these expressions where cc is an i1 value:
7163//
7164// (select cc 0, y) [AllOnes=0]
7165// (select cc y, 0) [AllOnes=0]
7166// (zext cc) [AllOnes=0]
7167// (sext cc) [AllOnes=0/1]
7168// (select cc -1, y) [AllOnes=1]
7169// (select cc y, -1) [AllOnes=1]
7170//
7171// Invert is set when N is the null/all ones constant when CC is false.
7172// OtherOp is set to the alternative value of N.
7173static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7174 SDValue &CC, bool &Invert,
7175 SDValue &OtherOp,
7176 SelectionDAG &DAG) {
7177 switch (N->getOpcode()) {
7178 default: return false;
7179 case ISD::SELECT: {
7180 CC = N->getOperand(0);
7181 SDValue N1 = N->getOperand(1);
7182 SDValue N2 = N->getOperand(2);
7183 if (isZeroOrAllOnes(N1, AllOnes)) {
7184 Invert = false;
7185 OtherOp = N2;
7186 return true;
7187 }
7188 if (isZeroOrAllOnes(N2, AllOnes)) {
7189 Invert = true;
7190 OtherOp = N1;
7191 return true;
7192 }
7193 return false;
7194 }
7195 case ISD::ZERO_EXTEND:
7196 // (zext cc) can never be the all ones value.
7197 if (AllOnes)
7198 return false;
7199 // Fall through.
7200 case ISD::SIGN_EXTEND: {
7201 EVT VT = N->getValueType(0);
7202 CC = N->getOperand(0);
7203 if (CC.getValueType() != MVT::i1)
7204 return false;
7205 Invert = !AllOnes;
7206 if (AllOnes)
7207 // When looking for an AllOnes constant, N is an sext, and the 'other'
7208 // value is 0.
7209 OtherOp = DAG.getConstant(0, VT);
7210 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7211 // When looking for a 0 constant, N can be zext or sext.
7212 OtherOp = DAG.getConstant(1, VT);
7213 else
7214 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7215 return true;
7216 }
7217 }
7218}
7219
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007220// Combine a constant select operand into its use:
7221//
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007222// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7223// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7224// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7225// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7226// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007227//
7228// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007229// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007230//
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007231// Also recognize sext/zext from i1:
7232//
7233// (add (zext cc), x) -> (select cc (add x, 1), x)
7234// (add (sext cc), x) -> (select cc (add x, -1), x)
7235//
7236// These transformations eventually create predicated instructions.
7237//
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007238// @param N The node to transform.
7239// @param Slct The N operand that is a select.
7240// @param OtherOp The other N operand (x above).
7241// @param DCI Context.
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007242// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007243// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007244static
7245SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007246 TargetLowering::DAGCombinerInfo &DCI,
7247 bool AllOnes = false) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007248 SelectionDAG &DAG = DCI.DAG;
Owen Andersone50ed302009-08-10 22:56:29 +00007249 EVT VT = N->getValueType(0);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007250 SDValue NonConstantVal;
7251 SDValue CCOp;
7252 bool SwapSelectOps;
7253 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7254 NonConstantVal, DAG))
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007255 return SDValue();
7256
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007257 // Slct is now know to be the desired identity constant when CC is true.
7258 SDValue TrueVal = OtherOp;
7259 SDValue FalseVal = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
7260 OtherOp, NonConstantVal);
7261 // Unless SwapSelectOps says CC should be false.
7262 if (SwapSelectOps)
7263 std::swap(TrueVal, FalseVal);
7264
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007265 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007266 CCOp, TrueVal, FalseVal);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007267}
7268
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007269// Attempt combineSelectAndUse on each operand of a commutative operator N.
7270static
7271SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7272 TargetLowering::DAGCombinerInfo &DCI) {
7273 SDValue N0 = N->getOperand(0);
7274 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007275 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007276 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7277 if (Result.getNode())
7278 return Result;
7279 }
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007280 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007281 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7282 if (Result.getNode())
7283 return Result;
7284 }
7285 return SDValue();
7286}
7287
Eric Christopherfa6f5912011-06-29 21:10:36 +00007288// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007289// (only after legalization).
7290static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7291 TargetLowering::DAGCombinerInfo &DCI,
7292 const ARMSubtarget *Subtarget) {
7293
7294 // Only perform optimization if after legalize, and if NEON is available. We
7295 // also expected both operands to be BUILD_VECTORs.
7296 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7297 || N0.getOpcode() != ISD::BUILD_VECTOR
7298 || N1.getOpcode() != ISD::BUILD_VECTOR)
7299 return SDValue();
7300
7301 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7302 EVT VT = N->getValueType(0);
7303 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7304 return SDValue();
7305
7306 // Check that the vector operands are of the right form.
7307 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7308 // operands, where N is the size of the formed vector.
7309 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7310 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007311
7312 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007313 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007314 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007315 SDValue Vec = N0->getOperand(0)->getOperand(0);
7316 SDNode *V = Vec.getNode();
7317 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007318
Eric Christopherfa6f5912011-06-29 21:10:36 +00007319 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007320 // check to see if each of their operands are an EXTRACT_VECTOR with
7321 // the same vector and appropriate index.
7322 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7323 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7324 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007325
Tanya Lattner189531f2011-06-14 23:48:48 +00007326 SDValue ExtVec0 = N0->getOperand(i);
7327 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007328
Tanya Lattner189531f2011-06-14 23:48:48 +00007329 // First operand is the vector, verify its the same.
7330 if (V != ExtVec0->getOperand(0).getNode() ||
7331 V != ExtVec1->getOperand(0).getNode())
7332 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007333
Tanya Lattner189531f2011-06-14 23:48:48 +00007334 // Second is the constant, verify its correct.
7335 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7336 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007337
Tanya Lattner189531f2011-06-14 23:48:48 +00007338 // For the constant, we want to see all the even or all the odd.
7339 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7340 || C1->getZExtValue() != nextIndex+1)
7341 return SDValue();
7342
7343 // Increment index.
7344 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007345 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007346 return SDValue();
7347 }
7348
7349 // Create VPADDL node.
7350 SelectionDAG &DAG = DCI.DAG;
7351 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007352
7353 // Build operand list.
7354 SmallVector<SDValue, 8> Ops;
7355 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7356 TLI.getPointerTy()));
7357
7358 // Input is the vector.
7359 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007360
Tanya Lattner189531f2011-06-14 23:48:48 +00007361 // Get widened type and narrowed type.
7362 MVT widenType;
7363 unsigned numElem = VT.getVectorNumElements();
7364 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7365 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7366 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7367 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7368 default:
Craig Topperbc219812012-02-07 02:50:20 +00007369 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007370 }
7371
7372 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7373 widenType, &Ops[0], Ops.size());
7374 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7375}
7376
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00007377static SDValue findMUL_LOHI(SDValue V) {
7378 if (V->getOpcode() == ISD::UMUL_LOHI ||
7379 V->getOpcode() == ISD::SMUL_LOHI)
7380 return V;
7381 return SDValue();
7382}
7383
7384static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7385 TargetLowering::DAGCombinerInfo &DCI,
7386 const ARMSubtarget *Subtarget) {
7387
7388 if (Subtarget->isThumb1Only()) return SDValue();
7389
7390 // Only perform the checks after legalize when the pattern is available.
7391 if (DCI.isBeforeLegalize()) return SDValue();
7392
7393 // Look for multiply add opportunities.
7394 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7395 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7396 // a glue link from the first add to the second add.
7397 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7398 // a S/UMLAL instruction.
7399 // loAdd UMUL_LOHI
7400 // \ / :lo \ :hi
7401 // \ / \ [no multiline comment]
7402 // ADDC | hiAdd
7403 // \ :glue / /
7404 // \ / /
7405 // ADDE
7406 //
7407 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7408 SDValue AddcOp0 = AddcNode->getOperand(0);
7409 SDValue AddcOp1 = AddcNode->getOperand(1);
7410
7411 // Check if the two operands are from the same mul_lohi node.
7412 if (AddcOp0.getNode() == AddcOp1.getNode())
7413 return SDValue();
7414
7415 assert(AddcNode->getNumValues() == 2 &&
7416 AddcNode->getValueType(0) == MVT::i32 &&
7417 AddcNode->getValueType(1) == MVT::Glue &&
7418 "Expect ADDC with two result values: i32, glue");
7419
7420 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7421 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7422 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7423 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7424 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7425 return SDValue();
7426
7427 // Look for the glued ADDE.
7428 SDNode* AddeNode = AddcNode->getGluedUser();
7429 if (AddeNode == NULL)
7430 return SDValue();
7431
7432 // Make sure it is really an ADDE.
7433 if (AddeNode->getOpcode() != ISD::ADDE)
7434 return SDValue();
7435
7436 assert(AddeNode->getNumOperands() == 3 &&
7437 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7438 "ADDE node has the wrong inputs");
7439
7440 // Check for the triangle shape.
7441 SDValue AddeOp0 = AddeNode->getOperand(0);
7442 SDValue AddeOp1 = AddeNode->getOperand(1);
7443
7444 // Make sure that the ADDE operands are not coming from the same node.
7445 if (AddeOp0.getNode() == AddeOp1.getNode())
7446 return SDValue();
7447
7448 // Find the MUL_LOHI node walking up ADDE's operands.
7449 bool IsLeftOperandMUL = false;
7450 SDValue MULOp = findMUL_LOHI(AddeOp0);
7451 if (MULOp == SDValue())
7452 MULOp = findMUL_LOHI(AddeOp1);
7453 else
7454 IsLeftOperandMUL = true;
7455 if (MULOp == SDValue())
7456 return SDValue();
7457
7458 // Figure out the right opcode.
7459 unsigned Opc = MULOp->getOpcode();
7460 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7461
7462 // Figure out the high and low input values to the MLAL node.
7463 SDValue* HiMul = &MULOp;
7464 SDValue* HiAdd = NULL;
7465 SDValue* LoMul = NULL;
7466 SDValue* LowAdd = NULL;
7467
7468 if (IsLeftOperandMUL)
7469 HiAdd = &AddeOp1;
7470 else
7471 HiAdd = &AddeOp0;
7472
7473
7474 if (AddcOp0->getOpcode() == Opc) {
7475 LoMul = &AddcOp0;
7476 LowAdd = &AddcOp1;
7477 }
7478 if (AddcOp1->getOpcode() == Opc) {
7479 LoMul = &AddcOp1;
7480 LowAdd = &AddcOp0;
7481 }
7482
7483 if (LoMul == NULL)
7484 return SDValue();
7485
7486 if (LoMul->getNode() != HiMul->getNode())
7487 return SDValue();
7488
7489 // Create the merged node.
7490 SelectionDAG &DAG = DCI.DAG;
7491
7492 // Build operand list.
7493 SmallVector<SDValue, 8> Ops;
7494 Ops.push_back(LoMul->getOperand(0));
7495 Ops.push_back(LoMul->getOperand(1));
7496 Ops.push_back(*LowAdd);
7497 Ops.push_back(*HiAdd);
7498
7499 SDValue MLALNode = DAG.getNode(FinalOpc, AddcNode->getDebugLoc(),
7500 DAG.getVTList(MVT::i32, MVT::i32),
7501 &Ops[0], Ops.size());
7502
7503 // Replace the ADDs' nodes uses by the MLA node's values.
7504 SDValue HiMLALResult(MLALNode.getNode(), 1);
7505 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7506
7507 SDValue LoMLALResult(MLALNode.getNode(), 0);
7508 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7509
7510 // Return original node to notify the driver to stop replacing.
7511 SDValue resNode(AddcNode, 0);
7512 return resNode;
7513}
7514
7515/// PerformADDCCombine - Target-specific dag combine transform from
7516/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7517static SDValue PerformADDCCombine(SDNode *N,
7518 TargetLowering::DAGCombinerInfo &DCI,
7519 const ARMSubtarget *Subtarget) {
7520
7521 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7522
7523}
7524
Bob Wilson3d5792a2010-07-29 20:34:14 +00007525/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7526/// operands N0 and N1. This is a helper for PerformADDCombine that is
7527/// called with the default operands, and if that fails, with commuted
7528/// operands.
7529static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007530 TargetLowering::DAGCombinerInfo &DCI,
7531 const ARMSubtarget *Subtarget){
7532
7533 // Attempt to create vpaddl for this add.
7534 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7535 if (Result.getNode())
7536 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007537
Chris Lattnerd1980a52009-03-12 06:52:53 +00007538 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007539 if (N0.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007540 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7541 if (Result.getNode()) return Result;
7542 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007543 return SDValue();
7544}
7545
Bob Wilson3d5792a2010-07-29 20:34:14 +00007546/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7547///
7548static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007549 TargetLowering::DAGCombinerInfo &DCI,
7550 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007551 SDValue N0 = N->getOperand(0);
7552 SDValue N1 = N->getOperand(1);
7553
7554 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007555 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007556 if (Result.getNode())
7557 return Result;
7558
7559 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007560 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007561}
7562
Chris Lattnerd1980a52009-03-12 06:52:53 +00007563/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007564///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007565static SDValue PerformSUBCombine(SDNode *N,
7566 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007567 SDValue N0 = N->getOperand(0);
7568 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007569
Chris Lattnerd1980a52009-03-12 06:52:53 +00007570 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesen864c8702012-08-18 21:25:22 +00007571 if (N1.getNode()->hasOneUse()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007572 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7573 if (Result.getNode()) return Result;
7574 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007575
Chris Lattnerd1980a52009-03-12 06:52:53 +00007576 return SDValue();
7577}
7578
Evan Cheng463d3582011-03-31 19:38:48 +00007579/// PerformVMULCombine
7580/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7581/// special multiplier accumulator forwarding.
7582/// vmul d3, d0, d2
7583/// vmla d3, d1, d2
7584/// is faster than
7585/// vadd d3, d0, d1
7586/// vmul d3, d3, d2
7587static SDValue PerformVMULCombine(SDNode *N,
7588 TargetLowering::DAGCombinerInfo &DCI,
7589 const ARMSubtarget *Subtarget) {
7590 if (!Subtarget->hasVMLxForwarding())
7591 return SDValue();
7592
7593 SelectionDAG &DAG = DCI.DAG;
7594 SDValue N0 = N->getOperand(0);
7595 SDValue N1 = N->getOperand(1);
7596 unsigned Opcode = N0.getOpcode();
7597 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7598 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007599 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007600 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7601 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7602 return SDValue();
7603 std::swap(N0, N1);
7604 }
7605
7606 EVT VT = N->getValueType(0);
7607 DebugLoc DL = N->getDebugLoc();
7608 SDValue N00 = N0->getOperand(0);
7609 SDValue N01 = N0->getOperand(1);
7610 return DAG.getNode(Opcode, DL, VT,
7611 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7612 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7613}
7614
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007615static SDValue PerformMULCombine(SDNode *N,
7616 TargetLowering::DAGCombinerInfo &DCI,
7617 const ARMSubtarget *Subtarget) {
7618 SelectionDAG &DAG = DCI.DAG;
7619
7620 if (Subtarget->isThumb1Only())
7621 return SDValue();
7622
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007623 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7624 return SDValue();
7625
7626 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007627 if (VT.is64BitVector() || VT.is128BitVector())
7628 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007629 if (VT != MVT::i32)
7630 return SDValue();
7631
7632 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7633 if (!C)
7634 return SDValue();
7635
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007636 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007637 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007638
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007639 ShiftAmt = ShiftAmt & (32 - 1);
7640 SDValue V = N->getOperand(0);
7641 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007642
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007643 SDValue Res;
7644 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007645
7646 if (MulAmt >= 0) {
7647 if (isPowerOf2_32(MulAmt - 1)) {
7648 // (mul x, 2^N + 1) => (add (shl x, N), x)
7649 Res = DAG.getNode(ISD::ADD, DL, VT,
7650 V,
7651 DAG.getNode(ISD::SHL, DL, VT,
7652 V,
7653 DAG.getConstant(Log2_32(MulAmt - 1),
7654 MVT::i32)));
7655 } else if (isPowerOf2_32(MulAmt + 1)) {
7656 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7657 Res = DAG.getNode(ISD::SUB, DL, VT,
7658 DAG.getNode(ISD::SHL, DL, VT,
7659 V,
7660 DAG.getConstant(Log2_32(MulAmt + 1),
7661 MVT::i32)),
7662 V);
7663 } else
7664 return SDValue();
7665 } else {
7666 uint64_t MulAmtAbs = -MulAmt;
7667 if (isPowerOf2_32(MulAmtAbs + 1)) {
7668 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7669 Res = DAG.getNode(ISD::SUB, DL, VT,
7670 V,
7671 DAG.getNode(ISD::SHL, DL, VT,
7672 V,
7673 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7674 MVT::i32)));
7675 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7676 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7677 Res = DAG.getNode(ISD::ADD, DL, VT,
7678 V,
7679 DAG.getNode(ISD::SHL, DL, VT,
7680 V,
7681 DAG.getConstant(Log2_32(MulAmtAbs-1),
7682 MVT::i32)));
7683 Res = DAG.getNode(ISD::SUB, DL, VT,
7684 DAG.getConstant(0, MVT::i32),Res);
7685
7686 } else
7687 return SDValue();
7688 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007689
7690 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007691 Res = DAG.getNode(ISD::SHL, DL, VT,
7692 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007693
7694 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007695 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007696 return SDValue();
7697}
7698
Owen Anderson080c0922010-11-05 19:27:46 +00007699static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007700 TargetLowering::DAGCombinerInfo &DCI,
7701 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007702
Owen Anderson080c0922010-11-05 19:27:46 +00007703 // Attempt to use immediate-form VBIC
7704 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7705 DebugLoc dl = N->getDebugLoc();
7706 EVT VT = N->getValueType(0);
7707 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007708
Tanya Lattner0433b212011-04-07 15:24:20 +00007709 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7710 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007711
Owen Anderson080c0922010-11-05 19:27:46 +00007712 APInt SplatBits, SplatUndef;
7713 unsigned SplatBitSize;
7714 bool HasAnyUndefs;
7715 if (BVN &&
7716 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7717 if (SplatBitSize <= 64) {
7718 EVT VbicVT;
7719 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7720 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007721 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007722 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007723 if (Val.getNode()) {
7724 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007725 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007726 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007727 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007728 }
7729 }
7730 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007731
Evan Chengc892aeb2012-02-23 01:19:06 +00007732 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007733 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
7734 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
7735 if (Result.getNode())
7736 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007737 }
7738
Owen Anderson080c0922010-11-05 19:27:46 +00007739 return SDValue();
7740}
7741
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007742/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7743static SDValue PerformORCombine(SDNode *N,
7744 TargetLowering::DAGCombinerInfo &DCI,
7745 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007746 // Attempt to use immediate-form VORR
7747 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7748 DebugLoc dl = N->getDebugLoc();
7749 EVT VT = N->getValueType(0);
7750 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007751
Tanya Lattner0433b212011-04-07 15:24:20 +00007752 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7753 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007754
Owen Anderson60f48702010-11-03 23:15:26 +00007755 APInt SplatBits, SplatUndef;
7756 unsigned SplatBitSize;
7757 bool HasAnyUndefs;
7758 if (BVN && Subtarget->hasNEON() &&
7759 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7760 if (SplatBitSize <= 64) {
7761 EVT VorrVT;
7762 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7763 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007764 DAG, VorrVT, VT.is128BitVector(),
7765 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007766 if (Val.getNode()) {
7767 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007768 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007769 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007770 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007771 }
7772 }
7773 }
7774
Evan Chengc892aeb2012-02-23 01:19:06 +00007775 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007776 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7777 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7778 if (Result.getNode())
7779 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007780 }
7781
Nadav Rotemdf832032012-08-13 18:52:44 +00007782 // The code below optimizes (or (and X, Y), Z).
7783 // The AND operand needs to have a single user to make these optimizations
7784 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007785 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00007786 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007787 return SDValue();
7788 SDValue N1 = N->getOperand(1);
7789
7790 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7791 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7792 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7793 APInt SplatUndef;
7794 unsigned SplatBitSize;
7795 bool HasAnyUndefs;
7796
7797 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7798 APInt SplatBits0;
7799 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7800 HasAnyUndefs) && !HasAnyUndefs) {
7801 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7802 APInt SplatBits1;
7803 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7804 HasAnyUndefs) && !HasAnyUndefs &&
7805 SplatBits0 == ~SplatBits1) {
7806 // Canonicalize the vector type to make instruction selection simpler.
7807 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7808 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7809 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007810 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007811 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7812 }
7813 }
7814 }
7815
Jim Grosbach54238562010-07-17 03:30:54 +00007816 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7817 // reasonable.
7818
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007819 // BFI is only available on V6T2+
7820 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7821 return SDValue();
7822
Jim Grosbach54238562010-07-17 03:30:54 +00007823 DebugLoc DL = N->getDebugLoc();
7824 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00007825 // iff (val & mask) == val
Jim Grosbach54238562010-07-17 03:30:54 +00007826 //
7827 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru94c22712012-09-27 10:14:43 +00007828 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007829 // && mask == ~mask2
Sylvestre Ledru94c22712012-09-27 10:14:43 +00007830 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007831 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007832 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007833
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007834 if (VT != MVT::i32)
7835 return SDValue();
7836
Evan Cheng30fb13f2010-12-13 20:32:54 +00007837 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007838
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007839 // The value and the mask need to be constants so we can verify this is
7840 // actually a bitfield set. If the mask is 0xffff, we can do better
7841 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007842 SDValue MaskOp = N0.getOperand(1);
7843 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7844 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007845 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007846 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007847 if (Mask == 0xffff)
7848 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007849 SDValue Res;
7850 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007851 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7852 if (N1C) {
7853 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007854 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007855 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007856
Evan Chenga9688c42010-12-11 04:11:38 +00007857 if (ARM::isBitFieldInvertedMask(Mask)) {
7858 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007859
Evan Cheng30fb13f2010-12-13 20:32:54 +00007860 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007861 DAG.getConstant(Val, MVT::i32),
7862 DAG.getConstant(Mask, MVT::i32));
7863
7864 // Do not add new nodes to DAG combiner worklist.
7865 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007866 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007867 }
Jim Grosbach54238562010-07-17 03:30:54 +00007868 } else if (N1.getOpcode() == ISD::AND) {
7869 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007870 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7871 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007872 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007873 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007874
Eric Christopher29aeed12011-03-26 01:21:03 +00007875 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7876 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007877 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007878 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007879 // The pack halfword instruction works better for masks that fit it,
7880 // so use that when it's available.
7881 if (Subtarget->hasT2ExtractPack() &&
7882 (Mask == 0xffff || Mask == 0xffff0000))
7883 return SDValue();
7884 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007885 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007886 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007887 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007888 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007889 DAG.getConstant(Mask, MVT::i32));
7890 // Do not add new nodes to DAG combiner worklist.
7891 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007892 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007893 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007894 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007895 // The pack halfword instruction works better for masks that fit it,
7896 // so use that when it's available.
7897 if (Subtarget->hasT2ExtractPack() &&
7898 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7899 return SDValue();
7900 // 2b
7901 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007902 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007903 DAG.getConstant(lsb, MVT::i32));
7904 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007905 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007906 // Do not add new nodes to DAG combiner worklist.
7907 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007908 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007909 }
7910 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007911
Evan Cheng30fb13f2010-12-13 20:32:54 +00007912 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7913 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7914 ARM::isBitFieldInvertedMask(~Mask)) {
7915 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7916 // where lsb(mask) == #shamt and masked bits of B are known zero.
7917 SDValue ShAmt = N00.getOperand(1);
7918 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7919 unsigned LSB = CountTrailingZeros_32(Mask);
7920 if (ShAmtC != LSB)
7921 return SDValue();
7922
7923 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7924 DAG.getConstant(~Mask, MVT::i32));
7925
7926 // Do not add new nodes to DAG combiner worklist.
7927 DCI.CombineTo(N, Res, false);
7928 }
7929
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007930 return SDValue();
7931}
7932
Evan Chengc892aeb2012-02-23 01:19:06 +00007933static SDValue PerformXORCombine(SDNode *N,
7934 TargetLowering::DAGCombinerInfo &DCI,
7935 const ARMSubtarget *Subtarget) {
7936 EVT VT = N->getValueType(0);
7937 SelectionDAG &DAG = DCI.DAG;
7938
7939 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7940 return SDValue();
7941
7942 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesendcd23422012-08-18 21:25:16 +00007943 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
7944 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
7945 if (Result.getNode())
7946 return Result;
Evan Chengc892aeb2012-02-23 01:19:06 +00007947 }
7948
7949 return SDValue();
7950}
7951
Evan Chengbf188ae2011-06-15 01:12:31 +00007952/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7953/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007954static SDValue PerformBFICombine(SDNode *N,
7955 TargetLowering::DAGCombinerInfo &DCI) {
7956 SDValue N1 = N->getOperand(1);
7957 if (N1.getOpcode() == ISD::AND) {
7958 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7959 if (!N11C)
7960 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007961 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7962 unsigned LSB = CountTrailingZeros_32(~InvMask);
7963 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7964 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007965 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007966 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007967 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7968 N->getOperand(0), N1.getOperand(0),
7969 N->getOperand(2));
7970 }
7971 return SDValue();
7972}
7973
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007974/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7975/// ARMISD::VMOVRRD.
7976static SDValue PerformVMOVRRDCombine(SDNode *N,
7977 TargetLowering::DAGCombinerInfo &DCI) {
7978 // vmovrrd(vmovdrr x, y) -> x,y
7979 SDValue InDouble = N->getOperand(0);
7980 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7981 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007982
7983 // vmovrrd(load f64) -> (load i32), (load i32)
7984 SDNode *InNode = InDouble.getNode();
7985 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7986 InNode->getValueType(0) == MVT::f64 &&
7987 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7988 !cast<LoadSDNode>(InNode)->isVolatile()) {
7989 // TODO: Should this be done for non-FrameIndex operands?
7990 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7991
7992 SelectionDAG &DAG = DCI.DAG;
7993 DebugLoc DL = LD->getDebugLoc();
7994 SDValue BasePtr = LD->getBasePtr();
7995 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7996 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007997 LD->isNonTemporal(), LD->isInvariant(),
7998 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007999
8000 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8001 DAG.getConstant(4, MVT::i32));
8002 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8003 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008004 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00008005 std::min(4U, LD->getAlignment() / 2));
8006
8007 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8008 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8009 DCI.RemoveFromWorklist(LD);
8010 DAG.DeleteNode(LD);
8011 return Result;
8012 }
8013
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008014 return SDValue();
8015}
8016
8017/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8018/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8019static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8020 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8021 SDValue Op0 = N->getOperand(0);
8022 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008023 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008024 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008025 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008026 Op1 = Op1.getOperand(0);
8027 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8028 Op0.getNode() == Op1.getNode() &&
8029 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008030 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008031 N->getValueType(0), Op0.getOperand(0));
8032 return SDValue();
8033}
8034
Bob Wilson31600902010-12-21 06:43:19 +00008035/// PerformSTORECombine - Target-specific dag combine xforms for
8036/// ISD::STORE.
8037static SDValue PerformSTORECombine(SDNode *N,
8038 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00008039 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00008040 if (St->isVolatile())
8041 return SDValue();
8042
Andrew Trick49b446f2012-07-18 18:34:24 +00008043 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00008044 // pack all of the elements in one place. Next, store to memory in fewer
8045 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00008046 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00008047 EVT VT = StVal.getValueType();
8048 if (St->isTruncatingStore() && VT.isVector()) {
8049 SelectionDAG &DAG = DCI.DAG;
8050 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8051 EVT StVT = St->getMemoryVT();
8052 unsigned NumElems = VT.getVectorNumElements();
8053 assert(StVT != VT && "Cannot truncate to the same type");
8054 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8055 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8056
8057 // From, To sizes and ElemCount must be pow of two
8058 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8059
8060 // We are going to use the original vector elt for storing.
8061 // Accumulated smaller vector elements must be a multiple of the store size.
8062 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8063
8064 unsigned SizeRatio = FromEltSz / ToEltSz;
8065 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8066
8067 // Create a type on which we perform the shuffle.
8068 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8069 NumElems*SizeRatio);
8070 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8071
8072 DebugLoc DL = St->getDebugLoc();
8073 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8074 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8075 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8076
8077 // Can't shuffle using an illegal type.
8078 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8079
8080 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8081 DAG.getUNDEF(WideVec.getValueType()),
8082 ShuffleVec.data());
8083 // At this point all of the data is stored at the bottom of the
8084 // register. We now need to save it to mem.
8085
8086 // Find the largest store unit
8087 MVT StoreType = MVT::i8;
8088 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8089 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8090 MVT Tp = (MVT::SimpleValueType)tp;
8091 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8092 StoreType = Tp;
8093 }
8094 // Didn't find a legal store type.
8095 if (!TLI.isTypeLegal(StoreType))
8096 return SDValue();
8097
8098 // Bitcast the original vector into a vector of store-size units
8099 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8100 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8101 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8102 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8103 SmallVector<SDValue, 8> Chains;
8104 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8105 TLI.getPointerTy());
8106 SDValue BasePtr = St->getBasePtr();
8107
8108 // Perform one or more big stores into memory.
8109 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8110 for (unsigned I = 0; I < E; I++) {
8111 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8112 StoreType, ShuffWide,
8113 DAG.getIntPtrConstant(I));
8114 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8115 St->getPointerInfo(), St->isVolatile(),
8116 St->isNonTemporal(), St->getAlignment());
8117 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8118 Increment);
8119 Chains.push_back(Ch);
8120 }
8121 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8122 Chains.size());
8123 }
8124
8125 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008126 return SDValue();
8127
Chad Rosier96b66d62012-04-09 19:38:15 +00008128 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8129 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008130 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00008131 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00008132 SelectionDAG &DAG = DCI.DAG;
8133 DebugLoc DL = St->getDebugLoc();
8134 SDValue BasePtr = St->getBasePtr();
8135 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8136 StVal.getNode()->getOperand(0), BasePtr,
8137 St->getPointerInfo(), St->isVolatile(),
8138 St->isNonTemporal(), St->getAlignment());
8139
8140 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8141 DAG.getConstant(4, MVT::i32));
8142 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8143 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8144 St->isNonTemporal(),
8145 std::min(4U, St->getAlignment() / 2));
8146 }
8147
8148 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00008149 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8150 return SDValue();
8151
Chad Rosier96b66d62012-04-09 19:38:15 +00008152 // Bitcast an i64 store extracted from a vector to f64.
8153 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00008154 SelectionDAG &DAG = DCI.DAG;
8155 DebugLoc dl = StVal.getDebugLoc();
8156 SDValue IntVec = StVal.getOperand(0);
8157 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8158 IntVec.getValueType().getVectorNumElements());
8159 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8160 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8161 Vec, StVal.getOperand(1));
8162 dl = N->getDebugLoc();
8163 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8164 // Make the DAGCombiner fold the bitcasts.
8165 DCI.AddToWorklist(Vec.getNode());
8166 DCI.AddToWorklist(ExtElt.getNode());
8167 DCI.AddToWorklist(V.getNode());
8168 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8169 St->getPointerInfo(), St->isVolatile(),
8170 St->isNonTemporal(), St->getAlignment(),
8171 St->getTBAAInfo());
8172}
8173
8174/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8175/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8176/// i64 vector to have f64 elements, since the value can then be loaded
8177/// directly into a VFP register.
8178static bool hasNormalLoadOperand(SDNode *N) {
8179 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8180 for (unsigned i = 0; i < NumElts; ++i) {
8181 SDNode *Elt = N->getOperand(i).getNode();
8182 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8183 return true;
8184 }
8185 return false;
8186}
8187
Bob Wilson75f02882010-09-17 22:59:05 +00008188/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8189/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00008190static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8191 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00008192 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8193 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8194 // into a pair of GPRs, which is fine when the value is used as a scalar,
8195 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00008196 SelectionDAG &DAG = DCI.DAG;
8197 if (N->getNumOperands() == 2) {
8198 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8199 if (RV.getNode())
8200 return RV;
8201 }
Bob Wilson75f02882010-09-17 22:59:05 +00008202
Bob Wilson31600902010-12-21 06:43:19 +00008203 // Load i64 elements as f64 values so that type legalization does not split
8204 // them up into i32 values.
8205 EVT VT = N->getValueType(0);
8206 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8207 return SDValue();
8208 DebugLoc dl = N->getDebugLoc();
8209 SmallVector<SDValue, 8> Ops;
8210 unsigned NumElts = VT.getVectorNumElements();
8211 for (unsigned i = 0; i < NumElts; ++i) {
8212 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8213 Ops.push_back(V);
8214 // Make the DAGCombiner fold the bitcast.
8215 DCI.AddToWorklist(V.getNode());
8216 }
8217 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8218 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8219 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8220}
8221
8222/// PerformInsertEltCombine - Target-specific dag combine xforms for
8223/// ISD::INSERT_VECTOR_ELT.
8224static SDValue PerformInsertEltCombine(SDNode *N,
8225 TargetLowering::DAGCombinerInfo &DCI) {
8226 // Bitcast an i64 load inserted into a vector to f64.
8227 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8228 EVT VT = N->getValueType(0);
8229 SDNode *Elt = N->getOperand(1).getNode();
8230 if (VT.getVectorElementType() != MVT::i64 ||
8231 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8232 return SDValue();
8233
8234 SelectionDAG &DAG = DCI.DAG;
8235 DebugLoc dl = N->getDebugLoc();
8236 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8237 VT.getVectorNumElements());
8238 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8239 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8240 // Make the DAGCombiner fold the bitcasts.
8241 DCI.AddToWorklist(Vec.getNode());
8242 DCI.AddToWorklist(V.getNode());
8243 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8244 Vec, V, N->getOperand(2));
8245 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00008246}
8247
Bob Wilsonf20700c2010-10-27 20:38:28 +00008248/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8249/// ISD::VECTOR_SHUFFLE.
8250static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8251 // The LLVM shufflevector instruction does not require the shuffle mask
8252 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8253 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8254 // operands do not match the mask length, they are extended by concatenating
8255 // them with undef vectors. That is probably the right thing for other
8256 // targets, but for NEON it is better to concatenate two double-register
8257 // size vector operands into a single quad-register size vector. Do that
8258 // transformation here:
8259 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8260 // shuffle(concat(v1, v2), undef)
8261 SDValue Op0 = N->getOperand(0);
8262 SDValue Op1 = N->getOperand(1);
8263 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8264 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8265 Op0.getNumOperands() != 2 ||
8266 Op1.getNumOperands() != 2)
8267 return SDValue();
8268 SDValue Concat0Op1 = Op0.getOperand(1);
8269 SDValue Concat1Op1 = Op1.getOperand(1);
8270 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8271 Concat1Op1.getOpcode() != ISD::UNDEF)
8272 return SDValue();
8273 // Skip the transformation if any of the types are illegal.
8274 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8275 EVT VT = N->getValueType(0);
8276 if (!TLI.isTypeLegal(VT) ||
8277 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8278 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8279 return SDValue();
8280
8281 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8282 Op0.getOperand(0), Op1.getOperand(0));
8283 // Translate the shuffle mask.
8284 SmallVector<int, 16> NewMask;
8285 unsigned NumElts = VT.getVectorNumElements();
8286 unsigned HalfElts = NumElts/2;
8287 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8288 for (unsigned n = 0; n < NumElts; ++n) {
8289 int MaskElt = SVN->getMaskElt(n);
8290 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008291 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00008292 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00008293 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00008294 NewElt = HalfElts + MaskElt - NumElts;
8295 NewMask.push_back(NewElt);
8296 }
8297 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
8298 DAG.getUNDEF(VT), NewMask.data());
8299}
8300
Bob Wilson1c3ef902011-02-07 17:43:21 +00008301/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8302/// NEON load/store intrinsics to merge base address updates.
8303static SDValue CombineBaseUpdate(SDNode *N,
8304 TargetLowering::DAGCombinerInfo &DCI) {
8305 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8306 return SDValue();
8307
8308 SelectionDAG &DAG = DCI.DAG;
8309 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8310 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8311 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8312 SDValue Addr = N->getOperand(AddrOpIdx);
8313
8314 // Search for a use of the address operand that is an increment.
8315 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8316 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8317 SDNode *User = *UI;
8318 if (User->getOpcode() != ISD::ADD ||
8319 UI.getUse().getResNo() != Addr.getResNo())
8320 continue;
8321
8322 // Check that the add is independent of the load/store. Otherwise, folding
8323 // it would create a cycle.
8324 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8325 continue;
8326
8327 // Find the new opcode for the updating load/store.
8328 bool isLoad = true;
8329 bool isLaneOp = false;
8330 unsigned NewOpc = 0;
8331 unsigned NumVecs = 0;
8332 if (isIntrinsic) {
8333 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8334 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00008335 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008336 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8337 NumVecs = 1; break;
8338 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8339 NumVecs = 2; break;
8340 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8341 NumVecs = 3; break;
8342 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8343 NumVecs = 4; break;
8344 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8345 NumVecs = 2; isLaneOp = true; break;
8346 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8347 NumVecs = 3; isLaneOp = true; break;
8348 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8349 NumVecs = 4; isLaneOp = true; break;
8350 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8351 NumVecs = 1; isLoad = false; break;
8352 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8353 NumVecs = 2; isLoad = false; break;
8354 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8355 NumVecs = 3; isLoad = false; break;
8356 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8357 NumVecs = 4; isLoad = false; break;
8358 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8359 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8360 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8361 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8362 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8363 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8364 }
8365 } else {
8366 isLaneOp = true;
8367 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008368 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008369 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8370 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8371 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8372 }
8373 }
8374
8375 // Find the size of memory referenced by the load/store.
8376 EVT VecTy;
8377 if (isLoad)
8378 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008379 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008380 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8381 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8382 if (isLaneOp)
8383 NumBytes /= VecTy.getVectorNumElements();
8384
8385 // If the increment is a constant, it must match the memory ref size.
8386 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8387 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8388 uint64_t IncVal = CInc->getZExtValue();
8389 if (IncVal != NumBytes)
8390 continue;
8391 } else if (NumBytes >= 3 * 16) {
8392 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8393 // separate instructions that make it harder to use a non-constant update.
8394 continue;
8395 }
8396
8397 // Create the new updating load/store node.
8398 EVT Tys[6];
8399 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8400 unsigned n;
8401 for (n = 0; n < NumResultVecs; ++n)
8402 Tys[n] = VecTy;
8403 Tys[n++] = MVT::i32;
8404 Tys[n] = MVT::Other;
8405 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8406 SmallVector<SDValue, 8> Ops;
8407 Ops.push_back(N->getOperand(0)); // incoming chain
8408 Ops.push_back(N->getOperand(AddrOpIdx));
8409 Ops.push_back(Inc);
8410 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8411 Ops.push_back(N->getOperand(i));
8412 }
8413 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8414 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8415 Ops.data(), Ops.size(),
8416 MemInt->getMemoryVT(),
8417 MemInt->getMemOperand());
8418
8419 // Update the uses.
8420 std::vector<SDValue> NewResults;
8421 for (unsigned i = 0; i < NumResultVecs; ++i) {
8422 NewResults.push_back(SDValue(UpdN.getNode(), i));
8423 }
8424 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8425 DCI.CombineTo(N, NewResults);
8426 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8427
8428 break;
Owen Anderson76706012011-04-05 21:48:57 +00008429 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008430 return SDValue();
8431}
8432
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008433/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8434/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8435/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8436/// return true.
8437static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8438 SelectionDAG &DAG = DCI.DAG;
8439 EVT VT = N->getValueType(0);
8440 // vldN-dup instructions only support 64-bit vectors for N > 1.
8441 if (!VT.is64BitVector())
8442 return false;
8443
8444 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8445 SDNode *VLD = N->getOperand(0).getNode();
8446 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8447 return false;
8448 unsigned NumVecs = 0;
8449 unsigned NewOpc = 0;
8450 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8451 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8452 NumVecs = 2;
8453 NewOpc = ARMISD::VLD2DUP;
8454 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8455 NumVecs = 3;
8456 NewOpc = ARMISD::VLD3DUP;
8457 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8458 NumVecs = 4;
8459 NewOpc = ARMISD::VLD4DUP;
8460 } else {
8461 return false;
8462 }
8463
8464 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8465 // numbers match the load.
8466 unsigned VLDLaneNo =
8467 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8468 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8469 UI != UE; ++UI) {
8470 // Ignore uses of the chain result.
8471 if (UI.getUse().getResNo() == NumVecs)
8472 continue;
8473 SDNode *User = *UI;
8474 if (User->getOpcode() != ARMISD::VDUPLANE ||
8475 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8476 return false;
8477 }
8478
8479 // Create the vldN-dup node.
8480 EVT Tys[5];
8481 unsigned n;
8482 for (n = 0; n < NumVecs; ++n)
8483 Tys[n] = VT;
8484 Tys[n] = MVT::Other;
8485 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8486 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8487 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8488 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8489 Ops, 2, VLDMemInt->getMemoryVT(),
8490 VLDMemInt->getMemOperand());
8491
8492 // Update the uses.
8493 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8494 UI != UE; ++UI) {
8495 unsigned ResNo = UI.getUse().getResNo();
8496 // Ignore uses of the chain result.
8497 if (ResNo == NumVecs)
8498 continue;
8499 SDNode *User = *UI;
8500 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8501 }
8502
8503 // Now the vldN-lane intrinsic is dead except for its chain result.
8504 // Update uses of the chain.
8505 std::vector<SDValue> VLDDupResults;
8506 for (unsigned n = 0; n < NumVecs; ++n)
8507 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8508 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8509 DCI.CombineTo(VLD, VLDDupResults);
8510
8511 return true;
8512}
8513
Bob Wilson9e82bf12010-07-14 01:22:12 +00008514/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8515/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008516static SDValue PerformVDUPLANECombine(SDNode *N,
8517 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008518 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008519
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008520 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8521 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8522 if (CombineVLDDUP(N, DCI))
8523 return SDValue(N, 0);
8524
8525 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8526 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008527 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008528 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008529 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008530 return SDValue();
8531
8532 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8533 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8534 // The canonical VMOV for a zero vector uses a 32-bit element size.
8535 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8536 unsigned EltBits;
8537 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8538 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008539 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008540 if (EltSize > VT.getVectorElementType().getSizeInBits())
8541 return SDValue();
8542
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008543 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008544}
8545
Eric Christopherfa6f5912011-06-29 21:10:36 +00008546// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008547// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8548static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8549{
Chad Rosier118c9a02011-06-28 17:26:57 +00008550 integerPart cN;
8551 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008552 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8553 I != E; I++) {
8554 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8555 if (!C)
8556 return false;
8557
Eric Christopherfa6f5912011-06-29 21:10:36 +00008558 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008559 APFloat APF = C->getValueAPF();
8560 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8561 != APFloat::opOK || !isExact)
8562 return false;
8563
8564 c0 = (I == 0) ? cN : c0;
8565 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8566 return false;
8567 }
8568 C = c0;
8569 return true;
8570}
8571
8572/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8573/// can replace combinations of VMUL and VCVT (floating-point to integer)
8574/// when the VMUL has a constant operand that is a power of 2.
8575///
8576/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8577/// vmul.f32 d16, d17, d16
8578/// vcvt.s32.f32 d16, d16
8579/// becomes:
8580/// vcvt.s32.f32 d16, d16, #3
8581static SDValue PerformVCVTCombine(SDNode *N,
8582 TargetLowering::DAGCombinerInfo &DCI,
8583 const ARMSubtarget *Subtarget) {
8584 SelectionDAG &DAG = DCI.DAG;
8585 SDValue Op = N->getOperand(0);
8586
8587 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8588 Op.getOpcode() != ISD::FMUL)
8589 return SDValue();
8590
8591 uint64_t C;
8592 SDValue N0 = Op->getOperand(0);
8593 SDValue ConstVec = Op->getOperand(1);
8594 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8595
Eric Christopherfa6f5912011-06-29 21:10:36 +00008596 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008597 !isConstVecPow2(ConstVec, isSigned, C))
8598 return SDValue();
8599
8600 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8601 Intrinsic::arm_neon_vcvtfp2fxu;
8602 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8603 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008604 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008605 DAG.getConstant(Log2_64(C), MVT::i32));
8606}
8607
8608/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8609/// can replace combinations of VCVT (integer to floating-point) and VDIV
8610/// when the VDIV has a constant operand that is a power of 2.
8611///
8612/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8613/// vcvt.f32.s32 d16, d16
8614/// vdiv.f32 d16, d17, d16
8615/// becomes:
8616/// vcvt.f32.s32 d16, d16, #3
8617static SDValue PerformVDIVCombine(SDNode *N,
8618 TargetLowering::DAGCombinerInfo &DCI,
8619 const ARMSubtarget *Subtarget) {
8620 SelectionDAG &DAG = DCI.DAG;
8621 SDValue Op = N->getOperand(0);
8622 unsigned OpOpcode = Op.getNode()->getOpcode();
8623
8624 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8625 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8626 return SDValue();
8627
8628 uint64_t C;
8629 SDValue ConstVec = N->getOperand(1);
8630 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8631
8632 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8633 !isConstVecPow2(ConstVec, isSigned, C))
8634 return SDValue();
8635
Eric Christopherfa6f5912011-06-29 21:10:36 +00008636 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008637 Intrinsic::arm_neon_vcvtfxu2fp;
8638 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8639 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008640 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008641 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8642}
8643
8644/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008645/// operand of a vector shift operation, where all the elements of the
8646/// build_vector must have the same constant integer value.
8647static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8648 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008649 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008650 Op = Op.getOperand(0);
8651 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8652 APInt SplatBits, SplatUndef;
8653 unsigned SplatBitSize;
8654 bool HasAnyUndefs;
8655 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8656 HasAnyUndefs, ElementBits) ||
8657 SplatBitSize > ElementBits)
8658 return false;
8659 Cnt = SplatBits.getSExtValue();
8660 return true;
8661}
8662
8663/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8664/// operand of a vector shift left operation. That value must be in the range:
8665/// 0 <= Value < ElementBits for a left shift; or
8666/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008667static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008668 assert(VT.isVector() && "vector shift count is not a vector type");
8669 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8670 if (! getVShiftImm(Op, ElementBits, Cnt))
8671 return false;
8672 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8673}
8674
8675/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8676/// operand of a vector shift right operation. For a shift opcode, the value
8677/// is positive, but for an intrinsic the value count must be negative. The
8678/// absolute value must be in the range:
8679/// 1 <= |Value| <= ElementBits for a right shift; or
8680/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008681static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008682 int64_t &Cnt) {
8683 assert(VT.isVector() && "vector shift count is not a vector type");
8684 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8685 if (! getVShiftImm(Op, ElementBits, Cnt))
8686 return false;
8687 if (isIntrinsic)
8688 Cnt = -Cnt;
8689 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8690}
8691
8692/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8693static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8694 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8695 switch (IntNo) {
8696 default:
8697 // Don't do anything for most intrinsics.
8698 break;
8699
8700 // Vector shifts: check for immediate versions and lower them.
8701 // Note: This is done during DAG combining instead of DAG legalizing because
8702 // the build_vectors for 64-bit vector element shift counts are generally
8703 // not legal, and it is hard to see their values after they get legalized to
8704 // loads from a constant pool.
8705 case Intrinsic::arm_neon_vshifts:
8706 case Intrinsic::arm_neon_vshiftu:
8707 case Intrinsic::arm_neon_vshiftls:
8708 case Intrinsic::arm_neon_vshiftlu:
8709 case Intrinsic::arm_neon_vshiftn:
8710 case Intrinsic::arm_neon_vrshifts:
8711 case Intrinsic::arm_neon_vrshiftu:
8712 case Intrinsic::arm_neon_vrshiftn:
8713 case Intrinsic::arm_neon_vqshifts:
8714 case Intrinsic::arm_neon_vqshiftu:
8715 case Intrinsic::arm_neon_vqshiftsu:
8716 case Intrinsic::arm_neon_vqshiftns:
8717 case Intrinsic::arm_neon_vqshiftnu:
8718 case Intrinsic::arm_neon_vqshiftnsu:
8719 case Intrinsic::arm_neon_vqrshiftns:
8720 case Intrinsic::arm_neon_vqrshiftnu:
8721 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008722 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008723 int64_t Cnt;
8724 unsigned VShiftOpc = 0;
8725
8726 switch (IntNo) {
8727 case Intrinsic::arm_neon_vshifts:
8728 case Intrinsic::arm_neon_vshiftu:
8729 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8730 VShiftOpc = ARMISD::VSHL;
8731 break;
8732 }
8733 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8734 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8735 ARMISD::VSHRs : ARMISD::VSHRu);
8736 break;
8737 }
8738 return SDValue();
8739
8740 case Intrinsic::arm_neon_vshiftls:
8741 case Intrinsic::arm_neon_vshiftlu:
8742 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8743 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008744 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008745
8746 case Intrinsic::arm_neon_vrshifts:
8747 case Intrinsic::arm_neon_vrshiftu:
8748 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8749 break;
8750 return SDValue();
8751
8752 case Intrinsic::arm_neon_vqshifts:
8753 case Intrinsic::arm_neon_vqshiftu:
8754 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8755 break;
8756 return SDValue();
8757
8758 case Intrinsic::arm_neon_vqshiftsu:
8759 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8760 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008761 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008762
8763 case Intrinsic::arm_neon_vshiftn:
8764 case Intrinsic::arm_neon_vrshiftn:
8765 case Intrinsic::arm_neon_vqshiftns:
8766 case Intrinsic::arm_neon_vqshiftnu:
8767 case Intrinsic::arm_neon_vqshiftnsu:
8768 case Intrinsic::arm_neon_vqrshiftns:
8769 case Intrinsic::arm_neon_vqrshiftnu:
8770 case Intrinsic::arm_neon_vqrshiftnsu:
8771 // Narrowing shifts require an immediate right shift.
8772 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8773 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008774 llvm_unreachable("invalid shift count for narrowing vector shift "
8775 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008776
8777 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008778 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008779 }
8780
8781 switch (IntNo) {
8782 case Intrinsic::arm_neon_vshifts:
8783 case Intrinsic::arm_neon_vshiftu:
8784 // Opcode already set above.
8785 break;
8786 case Intrinsic::arm_neon_vshiftls:
8787 case Intrinsic::arm_neon_vshiftlu:
8788 if (Cnt == VT.getVectorElementType().getSizeInBits())
8789 VShiftOpc = ARMISD::VSHLLi;
8790 else
8791 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8792 ARMISD::VSHLLs : ARMISD::VSHLLu);
8793 break;
8794 case Intrinsic::arm_neon_vshiftn:
8795 VShiftOpc = ARMISD::VSHRN; break;
8796 case Intrinsic::arm_neon_vrshifts:
8797 VShiftOpc = ARMISD::VRSHRs; break;
8798 case Intrinsic::arm_neon_vrshiftu:
8799 VShiftOpc = ARMISD::VRSHRu; break;
8800 case Intrinsic::arm_neon_vrshiftn:
8801 VShiftOpc = ARMISD::VRSHRN; break;
8802 case Intrinsic::arm_neon_vqshifts:
8803 VShiftOpc = ARMISD::VQSHLs; break;
8804 case Intrinsic::arm_neon_vqshiftu:
8805 VShiftOpc = ARMISD::VQSHLu; break;
8806 case Intrinsic::arm_neon_vqshiftsu:
8807 VShiftOpc = ARMISD::VQSHLsu; break;
8808 case Intrinsic::arm_neon_vqshiftns:
8809 VShiftOpc = ARMISD::VQSHRNs; break;
8810 case Intrinsic::arm_neon_vqshiftnu:
8811 VShiftOpc = ARMISD::VQSHRNu; break;
8812 case Intrinsic::arm_neon_vqshiftnsu:
8813 VShiftOpc = ARMISD::VQSHRNsu; break;
8814 case Intrinsic::arm_neon_vqrshiftns:
8815 VShiftOpc = ARMISD::VQRSHRNs; break;
8816 case Intrinsic::arm_neon_vqrshiftnu:
8817 VShiftOpc = ARMISD::VQRSHRNu; break;
8818 case Intrinsic::arm_neon_vqrshiftnsu:
8819 VShiftOpc = ARMISD::VQRSHRNsu; break;
8820 }
8821
8822 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008823 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008824 }
8825
8826 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008827 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008828 int64_t Cnt;
8829 unsigned VShiftOpc = 0;
8830
8831 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8832 VShiftOpc = ARMISD::VSLI;
8833 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8834 VShiftOpc = ARMISD::VSRI;
8835 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008836 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008837 }
8838
8839 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8840 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008841 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008842 }
8843
8844 case Intrinsic::arm_neon_vqrshifts:
8845 case Intrinsic::arm_neon_vqrshiftu:
8846 // No immediate versions of these to check for.
8847 break;
8848 }
8849
8850 return SDValue();
8851}
8852
8853/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8854/// lowers them. As with the vector shift intrinsics, this is done during DAG
8855/// combining instead of DAG legalizing because the build_vectors for 64-bit
8856/// vector element shift counts are generally not legal, and it is hard to see
8857/// their values after they get legalized to loads from a constant pool.
8858static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8859 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008860 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008861 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8862 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8863 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8864 SDValue N1 = N->getOperand(1);
8865 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8866 SDValue N0 = N->getOperand(0);
8867 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8868 DAG.MaskedValueIsZero(N0.getOperand(0),
8869 APInt::getHighBitsSet(32, 16)))
8870 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8871 }
8872 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008873
8874 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008875 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8876 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008877 return SDValue();
8878
8879 assert(ST->hasNEON() && "unexpected vector shift");
8880 int64_t Cnt;
8881
8882 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008883 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008884
8885 case ISD::SHL:
8886 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8887 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008888 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008889 break;
8890
8891 case ISD::SRA:
8892 case ISD::SRL:
8893 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8894 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8895 ARMISD::VSHRs : ARMISD::VSHRu);
8896 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008897 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008898 }
8899 }
8900 return SDValue();
8901}
8902
8903/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8904/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8905static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8906 const ARMSubtarget *ST) {
8907 SDValue N0 = N->getOperand(0);
8908
8909 // Check for sign- and zero-extensions of vector extract operations of 8-
8910 // and 16-bit vector elements. NEON supports these directly. They are
8911 // handled during DAG combining because type legalization will promote them
8912 // to 32-bit types and it is messy to recognize the operations after that.
8913 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8914 SDValue Vec = N0.getOperand(0);
8915 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008916 EVT VT = N->getValueType(0);
8917 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008918 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8919
Owen Anderson825b72b2009-08-11 20:47:22 +00008920 if (VT == MVT::i32 &&
8921 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008922 TLI.isTypeLegal(Vec.getValueType()) &&
8923 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008924
8925 unsigned Opc = 0;
8926 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008927 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008928 case ISD::SIGN_EXTEND:
8929 Opc = ARMISD::VGETLANEs;
8930 break;
8931 case ISD::ZERO_EXTEND:
8932 case ISD::ANY_EXTEND:
8933 Opc = ARMISD::VGETLANEu;
8934 break;
8935 }
8936 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8937 }
8938 }
8939
8940 return SDValue();
8941}
8942
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008943/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8944/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8945static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8946 const ARMSubtarget *ST) {
8947 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008948 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008949 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8950 // a NaN; only do the transformation when it matches that behavior.
8951
8952 // For now only do this when using NEON for FP operations; if using VFP, it
8953 // is not obvious that the benefit outweighs the cost of switching to the
8954 // NEON pipeline.
8955 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8956 N->getValueType(0) != MVT::f32)
8957 return SDValue();
8958
8959 SDValue CondLHS = N->getOperand(0);
8960 SDValue CondRHS = N->getOperand(1);
8961 SDValue LHS = N->getOperand(2);
8962 SDValue RHS = N->getOperand(3);
8963 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8964
8965 unsigned Opcode = 0;
8966 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008967 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008968 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008969 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008970 IsReversed = true ; // x CC y ? y : x
8971 } else {
8972 return SDValue();
8973 }
8974
Bob Wilsone742bb52010-02-24 22:15:53 +00008975 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008976 switch (CC) {
8977 default: break;
8978 case ISD::SETOLT:
8979 case ISD::SETOLE:
8980 case ISD::SETLT:
8981 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008982 case ISD::SETULT:
8983 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008984 // If LHS is NaN, an ordered comparison will be false and the result will
8985 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8986 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8987 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8988 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8989 break;
8990 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8991 // will return -0, so vmin can only be used for unsafe math or if one of
8992 // the operands is known to be nonzero.
8993 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008994 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008995 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8996 break;
8997 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008998 break;
8999
9000 case ISD::SETOGT:
9001 case ISD::SETOGE:
9002 case ISD::SETGT:
9003 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009004 case ISD::SETUGT:
9005 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00009006 // If LHS is NaN, an ordered comparison will be false and the result will
9007 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9008 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9009 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9010 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9011 break;
9012 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9013 // will return +0, so vmax can only be used for unsafe math or if one of
9014 // the operands is known to be nonzero.
9015 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009016 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00009017 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9018 break;
9019 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009020 break;
9021 }
9022
9023 if (!Opcode)
9024 return SDValue();
9025 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
9026}
9027
Evan Chenge721f5c2011-07-13 00:42:17 +00009028/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9029SDValue
9030ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9031 SDValue Cmp = N->getOperand(4);
9032 if (Cmp.getOpcode() != ARMISD::CMPZ)
9033 // Only looking at EQ and NE cases.
9034 return SDValue();
9035
9036 EVT VT = N->getValueType(0);
9037 DebugLoc dl = N->getDebugLoc();
9038 SDValue LHS = Cmp.getOperand(0);
9039 SDValue RHS = Cmp.getOperand(1);
9040 SDValue FalseVal = N->getOperand(0);
9041 SDValue TrueVal = N->getOperand(1);
9042 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00009043 ARMCC::CondCodes CC =
9044 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00009045
9046 // Simplify
9047 // mov r1, r0
9048 // cmp r1, x
9049 // mov r0, y
9050 // moveq r0, x
9051 // to
9052 // cmp r0, x
9053 // movne r0, y
9054 //
9055 // mov r1, r0
9056 // cmp r1, x
9057 // mov r0, x
9058 // movne r0, y
9059 // to
9060 // cmp r0, x
9061 // movne r0, y
9062 /// FIXME: Turn this into a target neutral optimization?
9063 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00009064 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00009065 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9066 N->getOperand(3), Cmp);
9067 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9068 SDValue ARMcc;
9069 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9070 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9071 N->getOperand(3), NewCmp);
9072 }
9073
9074 if (Res.getNode()) {
9075 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009076 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00009077 // Capture demanded bits information that would be otherwise lost.
9078 if (KnownZero == 0xfffffffe)
9079 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9080 DAG.getValueType(MVT::i1));
9081 else if (KnownZero == 0xffffff00)
9082 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9083 DAG.getValueType(MVT::i8));
9084 else if (KnownZero == 0xffff0000)
9085 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9086 DAG.getValueType(MVT::i16));
9087 }
9088
9089 return Res;
9090}
9091
Dan Gohman475871a2008-07-27 21:46:04 +00009092SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009093 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009094 switch (N->getOpcode()) {
9095 default: break;
Arnold Schwaighofer67514e92012-09-04 14:37:49 +00009096 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattner189531f2011-06-14 23:48:48 +00009097 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009098 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00009099 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009100 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00009101 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9102 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00009103 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00009104 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00009105 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00009106 case ISD::STORE: return PerformSTORECombine(N, DCI);
9107 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9108 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00009109 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00009110 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00009111 case ISD::FP_TO_SINT:
9112 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9113 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009114 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00009115 case ISD::SHL:
9116 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009117 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00009118 case ISD::SIGN_EXTEND:
9119 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00009120 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9121 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00009122 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00009123 case ARMISD::VLD2DUP:
9124 case ARMISD::VLD3DUP:
9125 case ARMISD::VLD4DUP:
9126 return CombineBaseUpdate(N, DCI);
9127 case ISD::INTRINSIC_VOID:
9128 case ISD::INTRINSIC_W_CHAIN:
9129 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9130 case Intrinsic::arm_neon_vld1:
9131 case Intrinsic::arm_neon_vld2:
9132 case Intrinsic::arm_neon_vld3:
9133 case Intrinsic::arm_neon_vld4:
9134 case Intrinsic::arm_neon_vld2lane:
9135 case Intrinsic::arm_neon_vld3lane:
9136 case Intrinsic::arm_neon_vld4lane:
9137 case Intrinsic::arm_neon_vst1:
9138 case Intrinsic::arm_neon_vst2:
9139 case Intrinsic::arm_neon_vst3:
9140 case Intrinsic::arm_neon_vst4:
9141 case Intrinsic::arm_neon_vst2lane:
9142 case Intrinsic::arm_neon_vst3lane:
9143 case Intrinsic::arm_neon_vst4lane:
9144 return CombineBaseUpdate(N, DCI);
9145 default: break;
9146 }
9147 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009148 }
Dan Gohman475871a2008-07-27 21:46:04 +00009149 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00009150}
9151
Evan Cheng31959b12011-02-02 01:06:55 +00009152bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9153 EVT VT) const {
9154 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9155}
9156
Bill Wendlingaf566342009-08-15 21:21:19 +00009157bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Evan Chengd10eab02012-09-18 01:42:45 +00009158 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosierb3235b12012-11-09 18:25:27 +00009159 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingaf566342009-08-15 21:21:19 +00009160
9161 switch (VT.getSimpleVT().SimpleTy) {
9162 default:
9163 return false;
9164 case MVT::i8:
9165 case MVT::i16:
9166 case MVT::i32:
Evan Chengd10eab02012-09-18 01:42:45 +00009167 // Unaligned access can use (for example) LRDB, LRDH, LDR
9168 return AllowsUnaligned;
Evan Chenga99c5082012-08-15 17:44:53 +00009169 case MVT::f64:
Evan Chengd10eab02012-09-18 01:42:45 +00009170 case MVT::v2f64:
9171 // For any little-endian targets with neon, we can support unaligned ld/st
9172 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9173 // A big-endian target may also explictly support unaligned accesses
9174 return Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian());
Bill Wendlingaf566342009-08-15 21:21:19 +00009175 }
9176}
9177
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009178static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9179 unsigned AlignCheck) {
9180 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9181 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9182}
9183
9184EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9185 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00009186 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009187 bool MemcpyStrSrc,
9188 MachineFunction &MF) const {
9189 const Function *F = MF.getFunction();
9190
9191 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00009192 if (IsZeroVal &&
Bill Wendling67658342012-10-09 07:45:08 +00009193 !F->getFnAttributes().hasAttribute(Attributes::NoImplicitFloat) &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009194 Subtarget->hasNEON()) {
9195 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
9196 return MVT::v4i32;
9197 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
9198 return MVT::v2i32;
9199 }
9200 }
9201
Lang Hames5207bf22011-11-08 18:56:23 +00009202 // Lowering to i32/i16 if the size permits.
9203 if (Size >= 4) {
9204 return MVT::i32;
9205 } else if (Size >= 2) {
9206 return MVT::i16;
9207 }
9208
Lang Hames1a1d1fc2011-11-02 22:52:45 +00009209 // Let the target-independent logic figure it out.
9210 return MVT::Other;
9211}
9212
Evan Chenge6c835f2009-08-14 20:09:37 +00009213static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9214 if (V < 0)
9215 return false;
9216
9217 unsigned Scale = 1;
9218 switch (VT.getSimpleVT().SimpleTy) {
9219 default: return false;
9220 case MVT::i1:
9221 case MVT::i8:
9222 // Scale == 1;
9223 break;
9224 case MVT::i16:
9225 // Scale == 2;
9226 Scale = 2;
9227 break;
9228 case MVT::i32:
9229 // Scale == 4;
9230 Scale = 4;
9231 break;
9232 }
9233
9234 if ((V & (Scale - 1)) != 0)
9235 return false;
9236 V /= Scale;
9237 return V == (V & ((1LL << 5) - 1));
9238}
9239
9240static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9241 const ARMSubtarget *Subtarget) {
9242 bool isNeg = false;
9243 if (V < 0) {
9244 isNeg = true;
9245 V = - V;
9246 }
9247
9248 switch (VT.getSimpleVT().SimpleTy) {
9249 default: return false;
9250 case MVT::i1:
9251 case MVT::i8:
9252 case MVT::i16:
9253 case MVT::i32:
9254 // + imm12 or - imm8
9255 if (isNeg)
9256 return V == (V & ((1LL << 8) - 1));
9257 return V == (V & ((1LL << 12) - 1));
9258 case MVT::f32:
9259 case MVT::f64:
9260 // Same as ARM mode. FIXME: NEON?
9261 if (!Subtarget->hasVFP2())
9262 return false;
9263 if ((V & 3) != 0)
9264 return false;
9265 V >>= 2;
9266 return V == (V & ((1LL << 8) - 1));
9267 }
9268}
9269
Evan Chengb01fad62007-03-12 23:30:29 +00009270/// isLegalAddressImmediate - Return true if the integer value can be used
9271/// as the offset of the target addressing mode for load / store of the
9272/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00009273static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00009274 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00009275 if (V == 0)
9276 return true;
9277
Evan Cheng65011532009-03-09 19:15:00 +00009278 if (!VT.isSimple())
9279 return false;
9280
Evan Chenge6c835f2009-08-14 20:09:37 +00009281 if (Subtarget->isThumb1Only())
9282 return isLegalT1AddressImmediate(V, VT);
9283 else if (Subtarget->isThumb2())
9284 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00009285
Evan Chenge6c835f2009-08-14 20:09:37 +00009286 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00009287 if (V < 0)
9288 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00009289 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00009290 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009291 case MVT::i1:
9292 case MVT::i8:
9293 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00009294 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009295 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009296 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00009297 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009298 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00009299 case MVT::f32:
9300 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00009301 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00009302 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00009303 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00009304 return false;
9305 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00009306 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00009307 }
Evan Chenga8e29892007-01-19 07:51:42 +00009308}
9309
Evan Chenge6c835f2009-08-14 20:09:37 +00009310bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9311 EVT VT) const {
9312 int Scale = AM.Scale;
9313 if (Scale < 0)
9314 return false;
9315
9316 switch (VT.getSimpleVT().SimpleTy) {
9317 default: return false;
9318 case MVT::i1:
9319 case MVT::i8:
9320 case MVT::i16:
9321 case MVT::i32:
9322 if (Scale == 1)
9323 return true;
9324 // r + r << imm
9325 Scale = Scale & ~1;
9326 return Scale == 2 || Scale == 4 || Scale == 8;
9327 case MVT::i64:
9328 // r + r
9329 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9330 return true;
9331 return false;
9332 case MVT::isVoid:
9333 // Note, we allow "void" uses (basically, uses that aren't loads or
9334 // stores), because arm allows folding a scale into many arithmetic
9335 // operations. This should be made more precise and revisited later.
9336
9337 // Allow r << imm, but the imm has to be a multiple of two.
9338 if (Scale & 1) return false;
9339 return isPowerOf2_32(Scale);
9340 }
9341}
9342
Chris Lattner37caf8c2007-04-09 23:33:39 +00009343/// isLegalAddressingMode - Return true if the addressing mode represented
9344/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009345bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009346 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00009347 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00009348 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00009349 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009350
Chris Lattner37caf8c2007-04-09 23:33:39 +00009351 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009352 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009353 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009354
Chris Lattner37caf8c2007-04-09 23:33:39 +00009355 switch (AM.Scale) {
9356 case 0: // no scale reg, must be "r+i" or "r", or "i".
9357 break;
9358 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009359 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009360 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009361 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009362 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009363 // ARM doesn't support any R+R*scale+imm addr modes.
9364 if (AM.BaseOffs)
9365 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009366
Bob Wilson2c7dab12009-04-08 17:55:28 +00009367 if (!VT.isSimple())
9368 return false;
9369
Evan Chenge6c835f2009-08-14 20:09:37 +00009370 if (Subtarget->isThumb2())
9371 return isLegalT2ScaledAddressingMode(AM, VT);
9372
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009373 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009374 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009375 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009376 case MVT::i1:
9377 case MVT::i8:
9378 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009379 if (Scale < 0) Scale = -Scale;
9380 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009381 return true;
9382 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009383 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009384 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009385 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009386 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009387 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009388 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009389 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009390
Owen Anderson825b72b2009-08-11 20:47:22 +00009391 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009392 // Note, we allow "void" uses (basically, uses that aren't loads or
9393 // stores), because arm allows folding a scale into many arithmetic
9394 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009395
Chris Lattner37caf8c2007-04-09 23:33:39 +00009396 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009397 if (Scale & 1) return false;
9398 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009399 }
Evan Chengb01fad62007-03-12 23:30:29 +00009400 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009401 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009402}
9403
Evan Cheng77e47512009-11-11 19:05:52 +00009404/// isLegalICmpImmediate - Return true if the specified immediate is legal
9405/// icmp immediate, that is the target has icmp instructions which can compare
9406/// a register against the immediate without having to materialize the
9407/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009408bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009409 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009410 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009411 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009412 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009413 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009414 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009415 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009416}
9417
Andrew Trick8d8d9612012-07-18 18:34:27 +00009418/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9419/// *or sub* immediate, that is the target has add or sub instructions which can
9420/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009421/// immediate into a register.
9422bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009423 // Same encoding for add/sub, just flip the sign.
9424 int64_t AbsImm = llvm::abs64(Imm);
9425 if (!Subtarget->isThumb())
9426 return ARM_AM::getSOImmVal(AbsImm) != -1;
9427 if (Subtarget->isThumb2())
9428 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9429 // Thumb1 only has 8-bit unsigned immediate.
9430 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009431}
9432
Owen Andersone50ed302009-08-10 22:56:29 +00009433static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009434 bool isSEXTLoad, SDValue &Base,
9435 SDValue &Offset, bool &isInc,
9436 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009437 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9438 return false;
9439
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009441 // AddressingMode 3
9442 Base = Ptr->getOperand(0);
9443 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009444 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009445 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009446 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009447 isInc = false;
9448 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9449 return true;
9450 }
9451 }
9452 isInc = (Ptr->getOpcode() == ISD::ADD);
9453 Offset = Ptr->getOperand(1);
9454 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009455 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009456 // AddressingMode 2
9457 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009458 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009459 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009460 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009461 isInc = false;
9462 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9463 Base = Ptr->getOperand(0);
9464 return true;
9465 }
9466 }
9467
9468 if (Ptr->getOpcode() == ISD::ADD) {
9469 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009470 ARM_AM::ShiftOpc ShOpcVal=
9471 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009472 if (ShOpcVal != ARM_AM::no_shift) {
9473 Base = Ptr->getOperand(1);
9474 Offset = Ptr->getOperand(0);
9475 } else {
9476 Base = Ptr->getOperand(0);
9477 Offset = Ptr->getOperand(1);
9478 }
9479 return true;
9480 }
9481
9482 isInc = (Ptr->getOpcode() == ISD::ADD);
9483 Base = Ptr->getOperand(0);
9484 Offset = Ptr->getOperand(1);
9485 return true;
9486 }
9487
Jim Grosbache5165492009-11-09 00:11:35 +00009488 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009489 return false;
9490}
9491
Owen Andersone50ed302009-08-10 22:56:29 +00009492static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009493 bool isSEXTLoad, SDValue &Base,
9494 SDValue &Offset, bool &isInc,
9495 SelectionDAG &DAG) {
9496 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9497 return false;
9498
9499 Base = Ptr->getOperand(0);
9500 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9501 int RHSC = (int)RHS->getZExtValue();
9502 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9503 assert(Ptr->getOpcode() == ISD::ADD);
9504 isInc = false;
9505 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9506 return true;
9507 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9508 isInc = Ptr->getOpcode() == ISD::ADD;
9509 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9510 return true;
9511 }
9512 }
9513
9514 return false;
9515}
9516
Evan Chenga8e29892007-01-19 07:51:42 +00009517/// getPreIndexedAddressParts - returns true by value, base pointer and
9518/// offset pointer and addressing mode by reference if the node's address
9519/// can be legally represented as pre-indexed load / store address.
9520bool
Dan Gohman475871a2008-07-27 21:46:04 +00009521ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9522 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009523 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009524 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009525 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009526 return false;
9527
Owen Andersone50ed302009-08-10 22:56:29 +00009528 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009529 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009530 bool isSEXTLoad = false;
9531 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9532 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009533 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009534 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9535 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9536 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009537 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009538 } else
9539 return false;
9540
9541 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009542 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009543 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009544 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9545 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009546 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009547 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009548 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009549 if (!isLegal)
9550 return false;
9551
9552 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9553 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009554}
9555
9556/// getPostIndexedAddressParts - returns true by value, base pointer and
9557/// offset pointer and addressing mode by reference if this node can be
9558/// combined with a load / store to form a post-indexed load / store.
9559bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009560 SDValue &Base,
9561 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009562 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009563 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009564 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009565 return false;
9566
Owen Andersone50ed302009-08-10 22:56:29 +00009567 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009568 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009569 bool isSEXTLoad = false;
9570 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009571 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009572 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009573 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9574 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009575 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009576 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009577 } else
9578 return false;
9579
9580 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009581 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009582 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009583 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009584 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009585 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009586 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9587 isInc, DAG);
9588 if (!isLegal)
9589 return false;
9590
Evan Cheng28dad2a2010-05-18 21:31:17 +00009591 if (Ptr != Base) {
9592 // Swap base ptr and offset to catch more post-index load / store when
9593 // it's legal. In Thumb2 mode, offset must be an immediate.
9594 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9595 !Subtarget->isThumb2())
9596 std::swap(Base, Offset);
9597
9598 // Post-indexed load / store update the base pointer.
9599 if (Ptr != Base)
9600 return false;
9601 }
9602
Evan Chenge88d5ce2009-07-02 07:28:31 +00009603 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9604 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009605}
9606
Dan Gohman475871a2008-07-27 21:46:04 +00009607void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009608 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009609 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009610 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009611 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009612 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009613 switch (Op.getOpcode()) {
9614 default: break;
9615 case ARMISD::CMOV: {
9616 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009617 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009618 if (KnownZero == 0 && KnownOne == 0) return;
9619
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009620 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009621 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009622 KnownZero &= KnownZeroRHS;
9623 KnownOne &= KnownOneRHS;
9624 return;
9625 }
9626 }
9627}
9628
9629//===----------------------------------------------------------------------===//
9630// ARM Inline Assembly Support
9631//===----------------------------------------------------------------------===//
9632
Evan Cheng55d42002011-01-08 01:24:27 +00009633bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9634 // Looking for "rev" which is V6+.
9635 if (!Subtarget->hasV6Ops())
9636 return false;
9637
9638 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9639 std::string AsmStr = IA->getAsmString();
9640 SmallVector<StringRef, 4> AsmPieces;
9641 SplitString(AsmStr, AsmPieces, ";\n");
9642
9643 switch (AsmPieces.size()) {
9644 default: return false;
9645 case 1:
9646 AsmStr = AsmPieces[0];
9647 AsmPieces.clear();
9648 SplitString(AsmStr, AsmPieces, " \t,");
9649
9650 // rev $0, $1
9651 if (AsmPieces.size() == 3 &&
9652 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9653 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009654 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009655 if (Ty && Ty->getBitWidth() == 32)
9656 return IntrinsicLowering::LowerToByteSwap(CI);
9657 }
9658 break;
9659 }
9660
9661 return false;
9662}
9663
Evan Chenga8e29892007-01-19 07:51:42 +00009664/// getConstraintType - Given a constraint letter, return the type of
9665/// constraint it is for this target.
9666ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009667ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9668 if (Constraint.size() == 1) {
9669 switch (Constraint[0]) {
9670 default: break;
9671 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009672 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009673 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009674 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009675 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009676 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009677 // An address with a single base register. Due to the way we
9678 // currently handle addresses it is the same as an 'r' memory constraint.
9679 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009680 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009681 } else if (Constraint.size() == 2) {
9682 switch (Constraint[0]) {
9683 default: break;
9684 // All 'U+' constraints are addresses.
9685 case 'U': return C_Memory;
9686 }
Evan Chenga8e29892007-01-19 07:51:42 +00009687 }
Chris Lattner4234f572007-03-25 02:14:49 +00009688 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009689}
9690
John Thompson44ab89e2010-10-29 17:29:13 +00009691/// Examine constraint type and operand type and determine a weight value.
9692/// This object must already have been set up with the operand type
9693/// and the current alternative constraint selected.
9694TargetLowering::ConstraintWeight
9695ARMTargetLowering::getSingleConstraintMatchWeight(
9696 AsmOperandInfo &info, const char *constraint) const {
9697 ConstraintWeight weight = CW_Invalid;
9698 Value *CallOperandVal = info.CallOperandVal;
9699 // If we don't have a value, we can't do a match,
9700 // but allow it at the lowest weight.
9701 if (CallOperandVal == NULL)
9702 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009703 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009704 // Look at the constraint type.
9705 switch (*constraint) {
9706 default:
9707 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9708 break;
9709 case 'l':
9710 if (type->isIntegerTy()) {
9711 if (Subtarget->isThumb())
9712 weight = CW_SpecificReg;
9713 else
9714 weight = CW_Register;
9715 }
9716 break;
9717 case 'w':
9718 if (type->isFloatingPointTy())
9719 weight = CW_Register;
9720 break;
9721 }
9722 return weight;
9723}
9724
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009725typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9726RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009727ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009728 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009729 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009730 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009731 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009732 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009733 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009734 return RCPair(0U, &ARM::tGPRRegClass);
9735 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009736 case 'h': // High regs or no regs.
9737 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009738 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009739 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009740 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009741 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009742 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009743 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009744 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009745 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009746 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009747 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009748 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009749 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009750 case 'x':
9751 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009752 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009753 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009754 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009755 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009756 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009757 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009758 case 't':
9759 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009760 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009761 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009762 }
9763 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009764 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009765 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009766
Evan Chenga8e29892007-01-19 07:51:42 +00009767 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9768}
9769
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009770/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9771/// vector. If it is invalid, don't add anything to Ops.
9772void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009773 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009774 std::vector<SDValue>&Ops,
9775 SelectionDAG &DAG) const {
9776 SDValue Result(0, 0);
9777
Eric Christopher100c8332011-06-02 23:16:42 +00009778 // Currently only support length 1 constraints.
9779 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009780
Eric Christopher100c8332011-06-02 23:16:42 +00009781 char ConstraintLetter = Constraint[0];
9782 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009783 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009784 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009785 case 'I': case 'J': case 'K': case 'L':
9786 case 'M': case 'N': case 'O':
9787 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9788 if (!C)
9789 return;
9790
9791 int64_t CVal64 = C->getSExtValue();
9792 int CVal = (int) CVal64;
9793 // None of these constraints allow values larger than 32 bits. Check
9794 // that the value fits in an int.
9795 if (CVal != CVal64)
9796 return;
9797
Eric Christopher100c8332011-06-02 23:16:42 +00009798 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009799 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009800 // Constant suitable for movw, must be between 0 and
9801 // 65535.
9802 if (Subtarget->hasV6T2Ops())
9803 if (CVal >= 0 && CVal <= 65535)
9804 break;
9805 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009806 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009807 if (Subtarget->isThumb1Only()) {
9808 // This must be a constant between 0 and 255, for ADD
9809 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009810 if (CVal >= 0 && CVal <= 255)
9811 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009812 } else if (Subtarget->isThumb2()) {
9813 // A constant that can be used as an immediate value in a
9814 // data-processing instruction.
9815 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9816 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009817 } else {
9818 // A constant that can be used as an immediate value in a
9819 // data-processing instruction.
9820 if (ARM_AM::getSOImmVal(CVal) != -1)
9821 break;
9822 }
9823 return;
9824
9825 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009826 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009827 // This must be a constant between -255 and -1, for negated ADD
9828 // immediates. This can be used in GCC with an "n" modifier that
9829 // prints the negated value, for use with SUB instructions. It is
9830 // not useful otherwise but is implemented for compatibility.
9831 if (CVal >= -255 && CVal <= -1)
9832 break;
9833 } else {
9834 // This must be a constant between -4095 and 4095. It is not clear
9835 // what this constraint is intended for. Implemented for
9836 // compatibility with GCC.
9837 if (CVal >= -4095 && CVal <= 4095)
9838 break;
9839 }
9840 return;
9841
9842 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009843 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009844 // A 32-bit value where only one byte has a nonzero value. Exclude
9845 // zero to match GCC. This constraint is used by GCC internally for
9846 // constants that can be loaded with a move/shift combination.
9847 // It is not useful otherwise but is implemented for compatibility.
9848 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9849 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009850 } else if (Subtarget->isThumb2()) {
9851 // A constant whose bitwise inverse can be used as an immediate
9852 // value in a data-processing instruction. This can be used in GCC
9853 // with a "B" modifier that prints the inverted value, for use with
9854 // BIC and MVN instructions. It is not useful otherwise but is
9855 // implemented for compatibility.
9856 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9857 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009858 } else {
9859 // A constant whose bitwise inverse can be used as an immediate
9860 // value in a data-processing instruction. This can be used in GCC
9861 // with a "B" modifier that prints the inverted value, for use with
9862 // BIC and MVN instructions. It is not useful otherwise but is
9863 // implemented for compatibility.
9864 if (ARM_AM::getSOImmVal(~CVal) != -1)
9865 break;
9866 }
9867 return;
9868
9869 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009870 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009871 // This must be a constant between -7 and 7,
9872 // for 3-operand ADD/SUB immediate instructions.
9873 if (CVal >= -7 && CVal < 7)
9874 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009875 } else if (Subtarget->isThumb2()) {
9876 // A constant whose negation can be used as an immediate value in a
9877 // data-processing instruction. This can be used in GCC with an "n"
9878 // modifier that prints the negated value, for use with SUB
9879 // instructions. It is not useful otherwise but is implemented for
9880 // compatibility.
9881 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9882 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009883 } else {
9884 // A constant whose negation can be used as an immediate value in a
9885 // data-processing instruction. This can be used in GCC with an "n"
9886 // modifier that prints the negated value, for use with SUB
9887 // instructions. It is not useful otherwise but is implemented for
9888 // compatibility.
9889 if (ARM_AM::getSOImmVal(-CVal) != -1)
9890 break;
9891 }
9892 return;
9893
9894 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009895 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009896 // This must be a multiple of 4 between 0 and 1020, for
9897 // ADD sp + immediate.
9898 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9899 break;
9900 } else {
9901 // A power of two or a constant between 0 and 32. This is used in
9902 // GCC for the shift amount on shifted register operands, but it is
9903 // useful in general for any shift amounts.
9904 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9905 break;
9906 }
9907 return;
9908
9909 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009910 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009911 // This must be a constant between 0 and 31, for shift amounts.
9912 if (CVal >= 0 && CVal <= 31)
9913 break;
9914 }
9915 return;
9916
9917 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009918 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009919 // This must be a multiple of 4 between -508 and 508, for
9920 // ADD/SUB sp = sp + immediate.
9921 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9922 break;
9923 }
9924 return;
9925 }
9926 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9927 break;
9928 }
9929
9930 if (Result.getNode()) {
9931 Ops.push_back(Result);
9932 return;
9933 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009934 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009935}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009936
9937bool
9938ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9939 // The ARM target isn't yet aware of offsets.
9940 return false;
9941}
Evan Cheng39382422009-10-28 01:44:26 +00009942
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009943bool ARM::isBitFieldInvertedMask(unsigned v) {
9944 if (v == 0xffffffff)
9945 return 0;
9946 // there can be 1's on either or both "outsides", all the "inside"
9947 // bits must be 0's
9948 unsigned int lsb = 0, msb = 31;
9949 while (v & (1 << msb)) --msb;
9950 while (v & (1 << lsb)) ++lsb;
9951 for (unsigned int i = lsb; i <= msb; ++i) {
9952 if (v & (1 << i))
9953 return 0;
9954 }
9955 return 1;
9956}
9957
Evan Cheng39382422009-10-28 01:44:26 +00009958/// isFPImmLegal - Returns true if the target can instruction select the
9959/// specified FP immediate natively. If false, the legalizer will
9960/// materialize the FP immediate as a load from a constant pool.
9961bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9962 if (!Subtarget->hasVFP3())
9963 return false;
9964 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009965 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009966 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009967 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009968 return false;
9969}
Bob Wilson65ffec42010-09-21 17:56:22 +00009970
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009971/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009972/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9973/// specified in the intrinsic calls.
9974bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9975 const CallInst &I,
9976 unsigned Intrinsic) const {
9977 switch (Intrinsic) {
9978 case Intrinsic::arm_neon_vld1:
9979 case Intrinsic::arm_neon_vld2:
9980 case Intrinsic::arm_neon_vld3:
9981 case Intrinsic::arm_neon_vld4:
9982 case Intrinsic::arm_neon_vld2lane:
9983 case Intrinsic::arm_neon_vld3lane:
9984 case Intrinsic::arm_neon_vld4lane: {
9985 Info.opc = ISD::INTRINSIC_W_CHAIN;
9986 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmow3574eca2012-10-08 16:38:25 +00009987 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +00009988 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9989 Info.ptrVal = I.getArgOperand(0);
9990 Info.offset = 0;
9991 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9992 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9993 Info.vol = false; // volatile loads with NEON intrinsics not supported
9994 Info.readMem = true;
9995 Info.writeMem = false;
9996 return true;
9997 }
9998 case Intrinsic::arm_neon_vst1:
9999 case Intrinsic::arm_neon_vst2:
10000 case Intrinsic::arm_neon_vst3:
10001 case Intrinsic::arm_neon_vst4:
10002 case Intrinsic::arm_neon_vst2lane:
10003 case Intrinsic::arm_neon_vst3lane:
10004 case Intrinsic::arm_neon_vst4lane: {
10005 Info.opc = ISD::INTRINSIC_VOID;
10006 // Conservatively set memVT to the entire set of vectors stored.
10007 unsigned NumElts = 0;
10008 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010009 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +000010010 if (!ArgTy->isVectorTy())
10011 break;
Micah Villmow3574eca2012-10-08 16:38:25 +000010012 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson65ffec42010-09-21 17:56:22 +000010013 }
10014 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10015 Info.ptrVal = I.getArgOperand(0);
10016 Info.offset = 0;
10017 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10018 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10019 Info.vol = false; // volatile stores with NEON intrinsics not supported
10020 Info.readMem = false;
10021 Info.writeMem = true;
10022 return true;
10023 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010024 case Intrinsic::arm_strexd: {
10025 Info.opc = ISD::INTRINSIC_W_CHAIN;
10026 Info.memVT = MVT::i64;
10027 Info.ptrVal = I.getArgOperand(2);
10028 Info.offset = 0;
10029 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010030 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010031 Info.readMem = false;
10032 Info.writeMem = true;
10033 return true;
10034 }
10035 case Intrinsic::arm_ldrexd: {
10036 Info.opc = ISD::INTRINSIC_W_CHAIN;
10037 Info.memVT = MVT::i64;
10038 Info.ptrVal = I.getArgOperand(0);
10039 Info.offset = 0;
10040 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +000010041 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +000010042 Info.readMem = true;
10043 Info.writeMem = false;
10044 return true;
10045 }
Bob Wilson65ffec42010-09-21 17:56:22 +000010046 default:
10047 break;
10048 }
10049
10050 return false;
10051}